Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vite specify rivers and lakes #871

Merged
merged 2 commits into from
Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/modules/names-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,7 @@ window.Names = (function () {

// generate name using Markov's chain
const getBase = function (base, min, max, dupl) {
if (base === undefined) {
ERROR && console.error("Please define a base");
return;
}
if (base === undefined) return ERROR && console.error("Please define a base");
if (!chains[base]) updateChain(base);

const data = chains[base];
Expand Down
6 changes: 5 additions & 1 deletion src/scripts/generation/generation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ async function generate(options?: IGenerationOptions) {
const newGrid = await createGrid(grid, precreatedGraph);
const newPack = createPack(newGrid);

// TODO: draw default ruler
// OceanLayers(newGrid);

// draw default ruler
// drawScaleBar(window.scale);
// Names.getMapName();

// redefine global grid and pack
grid = newGrid;
Expand Down
17 changes: 17 additions & 0 deletions src/scripts/generation/pack/lakes/generateLakeNames.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const {Names} = window;

export function generateLakeNames(features: TPackFeatures, culture: UintArray, cultures: TCultures) {
const updatedFeatures = features.map(feature => {
if (feature === 0) return 0;
if (feature.type !== "lake") return feature;

const landCell = feature.shoreline[0];
const cultureId = culture[landCell];
const namebase = cultures[cultureId].base;

const name: string = Names.getBase(namebase);
return {...feature, name};
});

return updatedFeatures as TPackFeatures;
}
16 changes: 0 additions & 16 deletions src/scripts/generation/pack/lakes/lakes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import * as d3 from "d3";
import {rn} from "utils/numberUtils";
import {getRealHeight} from "utils/unitUtils";

const {Names} = window;

export interface ILakeClimateData extends IPackFeatureLake {
flux: number;
temp: number;
Expand Down Expand Up @@ -100,17 +98,3 @@ function defineLakeGroup({

return "freshwater";
}

export function generateLakeNames(features: TPackFeatures, culture: UintArray) {
const updatedFeatures = features.map(feature => {
if (!feature) return 0;
if (feature.type !== "lake") return feature;

const landCell = feature.shoreline[0];
const cultureId = culture[landCell];
const name = Names.getCulture(cultureId) as string;
return {...feature, name};
});

return updatedFeatures as TPackFeatures;
}
31 changes: 15 additions & 16 deletions src/scripts/generation/pack/pack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import {pick} from "utils/functionUtils";
import {generateBurgsAndStates} from "./burgsAndStates/generateBurgsAndStates";
import {expandCultures} from "./cultures/expandCultures";
import {generateCultures} from "./cultures/generateCultures";
import {generateLakeNames} from "./lakes/generateLakeNames";
import {generateProvinces} from "./provinces/generateProvinces";
import {generateReligions} from "./religions/generateReligions";
import {repackGrid} from "./repackGrid";
import {generateRivers} from "./rivers/generateRivers";
import {specifyRivers} from "./rivers/specifyRivers";
import {generateRoutes} from "./routes/generateRoutes";

const {Biomes} = window;
Expand All @@ -16,11 +18,13 @@ export function createPack(grid: IGrid): IPack {
const {temp, prec} = grid.cells;
const {vertices, cells} = repackGrid(grid);

const {features, featureIds, distanceField, haven, harbor} = markupPackFeatures(
grid,
vertices,
pick(cells, "v", "c", "b", "p", "h")
);
const {
features: rawFeatures,
featureIds,
distanceField,
haven,
harbor
} = markupPackFeatures(grid, vertices, pick(cells, "v", "c", "b", "p", "h"));

const {
heights,
Expand All @@ -31,7 +35,7 @@ export function createPack(grid: IGrid): IPack {
mergedFeatures
} = generateRivers(
{...pick(cells, "i", "c", "b", "g", "h", "p"), f: featureIds, t: distanceField, haven},
features,
rawFeatures,
prec,
temp
);
Expand Down Expand Up @@ -155,17 +159,12 @@ export function createPack(grid: IGrid): IPack {
burg: burgIds
});

// Rivers.specify();
// const updatedFeatures = generateLakeNames();
const rivers = specifyRivers(rawRivers, cultureIds, cultures);
const features = generateLakeNames(mergedFeatures, cultureIds, cultures);

// Military.generate();
// Markers.generate();
// addZones();

// OceanLayers(newGrid);

// drawScaleBar(window.scale);
// Names.getMapName();
// addZones(); // add to pack data

const events: IEvents = {conflicts};

Expand All @@ -191,8 +190,8 @@ export function createPack(grid: IGrid): IPack {
religion: religionIds,
province: provinceIds
},
features: mergedFeatures,
rivers: rawRivers, // "name" | "basin" | "type"
features,
rivers,
cultures,
states,
burgs,
Expand Down
1 change: 0 additions & 1 deletion src/scripts/generation/pack/religions/generateReligions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {TIME} from "config/logging";
import {drawPoint} from "utils/debugUtils";
import {pick} from "utils/functionUtils";
import {generateFolkReligions} from "./generateFolkReligions";
import {generateOrganizedReligions} from "./generateOrganizedReligions";
Expand Down
52 changes: 52 additions & 0 deletions src/scripts/generation/pack/rivers/specifyRivers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import {each, rw} from "utils/probabilityUtils";

const {Names} = window;

// weighted arrays of river type names
const riverTypes = {
main: {
big: {River: 1},
small: {Creek: 9, River: 3, Brook: 3, Stream: 1}
},
fork: {
big: {Fork: 1},
small: {Branch: 1}
}
};

export function specifyRivers(
rawRivers: Omit<IRiver, "name" | "basin" | "type">[],
cultureIds: Uint16Array,
cultures: TCultures
): TRivers {
const thresholdIndex = Math.ceil(rawRivers.length * 0.15);
const smallLength = rawRivers.map(r => r.length).sort((a, b) => a - b)[thresholdIndex];

const rivers: TRivers = rawRivers.map(river => {
const basin = getBasin(rawRivers, river.i);
const name = getName(river.mouth, cultureIds, cultures);
const type = getType(smallLength, river.i, river.length, river.parent);

return {...river, basin, name, type};
});

return rivers;
}

function getBasin(rivers: {i: number; parent: number}[], riverId: number): number {
const parent = rivers.find(river => river.i === riverId)?.parent;
if (!parent || riverId === parent) return riverId;
return getBasin(rivers, parent);
}

const getName = function (cellId: number, cultureIds: Uint16Array, cultures: TCultures) {
const culture = cultures[cultureIds[cellId]];
const namebase = culture.base;
return Names.getBase(namebase);
};

const getType = function (smallLength: number, riverId: number, length: number, parent: number) {
const isSmall = length < smallLength;
const isFork = parent && parent !== riverId && each(3)(riverId);
return rw(riverTypes[isFork ? "fork" : "main"][isSmall ? "small" : "big"] as {[key: string]: number});
};