mirror of
https://github.com/Azgaar/Fantasy-Map-Generator.git
synced 2025-12-17 17:51:24 +01:00
refactor: move files to folders
This commit is contained in:
parent
0d1b52e538
commit
dd29a89d66
10 changed files with 419 additions and 405 deletions
106
src/scripts/generation/pack/cultures/expandCultures.ts
Normal file
106
src/scripts/generation/pack/cultures/expandCultures.ts
Normal file
|
|
@ -0,0 +1,106 @@
|
||||||
|
import FlatQueue from "flatqueue";
|
||||||
|
|
||||||
|
import {DISTANCE_FIELD, ELEVATION, FOREST_BIOMES, MIN_LAND_HEIGHT} from "config/generation";
|
||||||
|
import {TIME} from "config/logging";
|
||||||
|
import {getInputNumber} from "utils/nodeUtils";
|
||||||
|
import {minmax} from "utils/numberUtils";
|
||||||
|
import {isCulture} from "utils/typeUtils";
|
||||||
|
|
||||||
|
const {LAND_COAST, LANDLOCKED, WATER_COAST} = DISTANCE_FIELD;
|
||||||
|
const {MOUNTAINS, HILLS} = ELEVATION;
|
||||||
|
|
||||||
|
// expand cultures across the map (Dijkstra-like algorithm)
|
||||||
|
export function expandCultures(
|
||||||
|
cultures: TCultures,
|
||||||
|
features: TPackFeatures,
|
||||||
|
cells: Pick<IPack["cells"], "c" | "area" | "h" | "t" | "f" | "r" | "fl" | "biome" | "pop">
|
||||||
|
) {
|
||||||
|
TIME && console.time("expandCultures");
|
||||||
|
|
||||||
|
const cultureIds = new Uint16Array(cells.h.length); // cell cultures
|
||||||
|
const queue = new FlatQueue<{cellId: number; cultureId: number}>();
|
||||||
|
|
||||||
|
cultures.filter(isCulture).forEach(culture => {
|
||||||
|
queue.push({cellId: culture.center, cultureId: culture.i}, 0);
|
||||||
|
});
|
||||||
|
|
||||||
|
const cellsNumberFactor = cells.h.length / 1.6;
|
||||||
|
const maxExpansionCost = cellsNumberFactor * getInputNumber("neutralInput"); // limit cost for culture growth
|
||||||
|
const cost: number[] = [];
|
||||||
|
|
||||||
|
while (queue.length) {
|
||||||
|
const priority = queue.peekValue()!;
|
||||||
|
const {cellId, cultureId} = queue.pop()!;
|
||||||
|
|
||||||
|
const {type, expansionism, center} = getCulture(cultureId);
|
||||||
|
const cultureBiome = cells.biome[center];
|
||||||
|
|
||||||
|
cells.c[cellId].forEach(neibCellId => {
|
||||||
|
const biomeCost = getBiomeCost(neibCellId, cultureBiome, type);
|
||||||
|
const heightCost = getHeightCost(neibCellId, cells.h[neibCellId], type);
|
||||||
|
const riverCost = getRiverCost(cells.r[neibCellId], neibCellId, type);
|
||||||
|
const typeCost = getTypeCost(cells.t[neibCellId], type);
|
||||||
|
|
||||||
|
const totalCost = priority + (biomeCost + heightCost + riverCost + typeCost) / expansionism;
|
||||||
|
if (totalCost > maxExpansionCost) return;
|
||||||
|
|
||||||
|
if (!cost[neibCellId] || totalCost < cost[neibCellId]) {
|
||||||
|
if (cells.pop[neibCellId] > 0) cultureIds[neibCellId] = cultureId; // assign culture to populated cell
|
||||||
|
cost[neibCellId] = totalCost;
|
||||||
|
queue.push({cellId: neibCellId, cultureId}, totalCost);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
TIME && console.timeEnd("expandCultures");
|
||||||
|
return cultureIds;
|
||||||
|
|
||||||
|
function getCulture(cultureId: number) {
|
||||||
|
const culture = cultures[cultureId];
|
||||||
|
if (!isCulture(culture)) throw new Error("Wilderness cannot expand");
|
||||||
|
return culture;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getBiomeCost(cellId: number, cultureBiome: number, type: TCultureType) {
|
||||||
|
const biome = cells.biome[cellId];
|
||||||
|
if (cultureBiome === biome) return 10; // tiny penalty for native biome
|
||||||
|
if (type === "Hunting") return biomesData.cost[biome] * 5; // non-native biome penalty for hunters
|
||||||
|
if (type === "Nomadic" && FOREST_BIOMES.includes(biome)) return biomesData.cost[biome] * 10; // forest biome penalty for nomads
|
||||||
|
return biomesData.cost[biome] * 2; // general non-native biome penalty
|
||||||
|
}
|
||||||
|
|
||||||
|
function getHeightCost(cellId: number, height: number, type: TCultureType) {
|
||||||
|
if (height < MIN_LAND_HEIGHT) {
|
||||||
|
const feature = features[cells.f[cellId]];
|
||||||
|
const area = cells.area[cellId];
|
||||||
|
|
||||||
|
if (type === "Lake" && feature && feature.type === "lake") return 10; // almost lake crossing penalty for Lake cultures
|
||||||
|
if (type === "Naval") return area * 2; // low sea or lake crossing penalty for Naval cultures
|
||||||
|
if (type === "Nomadic") return area * 50; // giant sea or lake crossing penalty for Nomads
|
||||||
|
return area * 6; // general sea or lake crossing penalty
|
||||||
|
}
|
||||||
|
|
||||||
|
if (type === "Highland") {
|
||||||
|
if (height >= MOUNTAINS) return 0; // no penalty for highlanders on highlands
|
||||||
|
if (height < HILLS) return 3000; // giant penalty for highlanders on lowlands
|
||||||
|
return 100; // penalty for highlanders on hills
|
||||||
|
}
|
||||||
|
|
||||||
|
if (height >= MOUNTAINS) return 200; // general mountains crossing penalty
|
||||||
|
if (height >= HILLS) return 30; // general hills crossing penalty
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getRiverCost(riverId: number, cellId: number, type: TCultureType) {
|
||||||
|
if (type === "River") return riverId ? 0 : 100; // penalty for river cultures
|
||||||
|
if (!riverId) return 0; // no penalty for others if there is no river
|
||||||
|
return minmax(cells.fl[cellId] / 10, 20, 100); // river penalty from 20 to 100 based on flux
|
||||||
|
}
|
||||||
|
|
||||||
|
function getTypeCost(t: number, type: TCultureType) {
|
||||||
|
if (t === LAND_COAST) return type === "Naval" || type === "Lake" ? 0 : type === "Nomadic" ? 60 : 20; // penalty for coastline
|
||||||
|
if (t === LANDLOCKED) return type === "Naval" || type === "Nomadic" ? 30 : 0; // low penalty for land level 2 for Navals and nomads
|
||||||
|
if (t !== WATER_COAST) return type === "Naval" || type === "Lake" ? 100 : 0; // penalty for mainland for navals
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,24 +1,15 @@
|
||||||
import * as d3 from "d3";
|
import * as d3 from "d3";
|
||||||
import FlatQueue from "flatqueue";
|
|
||||||
|
|
||||||
import {cultureSets, DEFAULT_SORT_STRING, TCultureSetName} from "config/cultureSets";
|
import {cultureSets, DEFAULT_SORT_STRING, TCultureSetName} from "config/cultureSets";
|
||||||
import {
|
import {DISTANCE_FIELD, ELEVATION, HUNTING_BIOMES, NOMADIC_BIOMES} from "config/generation";
|
||||||
DISTANCE_FIELD,
|
|
||||||
ELEVATION,
|
|
||||||
FOREST_BIOMES,
|
|
||||||
HUNTING_BIOMES,
|
|
||||||
MIN_LAND_HEIGHT,
|
|
||||||
NOMADIC_BIOMES
|
|
||||||
} from "config/generation";
|
|
||||||
import {ERROR, TIME, WARN} from "config/logging";
|
import {ERROR, TIME, WARN} from "config/logging";
|
||||||
import {getColors} from "utils/colorUtils";
|
import {getColors} from "utils/colorUtils";
|
||||||
import {abbreviate} from "utils/languageUtils";
|
import {abbreviate} from "utils/languageUtils";
|
||||||
import {getInputNumber, getInputValue, getSelectedOption} from "utils/nodeUtils";
|
import {getInputNumber, getInputValue, getSelectedOption} from "utils/nodeUtils";
|
||||||
import {minmax, rn} from "utils/numberUtils";
|
import {rn} from "utils/numberUtils";
|
||||||
import {biased, P, rand} from "utils/probabilityUtils";
|
import {biased, P, rand} from "utils/probabilityUtils";
|
||||||
import {byId} from "utils/shorthands";
|
import {byId} from "utils/shorthands";
|
||||||
import {defaultNameBases} from "config/namebases";
|
import {defaultNameBases} from "config/namebases";
|
||||||
import {isCulture} from "utils/typeUtils";
|
|
||||||
|
|
||||||
const {COA} = window;
|
const {COA} = window;
|
||||||
|
|
||||||
|
|
@ -33,16 +24,14 @@ const cultureTypeBaseExpansionism: {[key in TCultureType]: number} = {
|
||||||
};
|
};
|
||||||
|
|
||||||
const {MOUNTAINS, HILLS} = ELEVATION;
|
const {MOUNTAINS, HILLS} = ELEVATION;
|
||||||
const {LAND_COAST, LANDLOCKED, WATER_COAST} = DISTANCE_FIELD;
|
const {LAND_COAST, LANDLOCKED} = DISTANCE_FIELD;
|
||||||
|
|
||||||
export const generateCultures = function (
|
type TCellsData = Pick<
|
||||||
features: TPackFeatures,
|
|
||||||
cells: Pick<
|
|
||||||
IPack["cells"],
|
IPack["cells"],
|
||||||
"p" | "i" | "g" | "t" | "h" | "haven" | "harbor" | "f" | "r" | "fl" | "s" | "pop" | "biome"
|
"p" | "i" | "g" | "t" | "h" | "haven" | "harbor" | "f" | "r" | "fl" | "s" | "pop" | "biome"
|
||||||
>,
|
>;
|
||||||
temp: Int8Array
|
|
||||||
): TCultures {
|
export function generateCultures(features: TPackFeatures, cells: TCellsData, temp: Int8Array): TCultures {
|
||||||
TIME && console.time("generateCultures");
|
TIME && console.time("generateCultures");
|
||||||
|
|
||||||
const wildlands: TWilderness = {name: "Wildlands", i: 0, base: 1, origins: [null], shield: "round"};
|
const wildlands: TWilderness = {name: "Wildlands", i: 0, base: 1, origins: [null], shield: "round"};
|
||||||
|
|
@ -272,100 +261,4 @@ export const generateCultures = function (
|
||||||
ERROR && console.error(`Name base ${base} is not available, applying a fallback one`);
|
ERROR && console.error(`Name base ${base} is not available, applying a fallback one`);
|
||||||
return base % nameBases.length;
|
return base % nameBases.length;
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
// expand cultures across the map (Dijkstra-like algorithm)
|
|
||||||
export const expandCultures = function (
|
|
||||||
cultures: TCultures,
|
|
||||||
features: TPackFeatures,
|
|
||||||
cells: Pick<IPack["cells"], "c" | "area" | "h" | "t" | "f" | "r" | "fl" | "biome" | "pop">
|
|
||||||
) {
|
|
||||||
TIME && console.time("expandCultures");
|
|
||||||
|
|
||||||
const cultureIds = new Uint16Array(cells.h.length); // cell cultures
|
|
||||||
const queue = new FlatQueue<{cellId: number; cultureId: number}>();
|
|
||||||
|
|
||||||
cultures.filter(isCulture).forEach(culture => {
|
|
||||||
queue.push({cellId: culture.center, cultureId: culture.i}, 0);
|
|
||||||
});
|
|
||||||
|
|
||||||
const cellsNumberFactor = cells.h.length / 1.6;
|
|
||||||
const maxExpansionCost = cellsNumberFactor * getInputNumber("neutralInput"); // limit cost for culture growth
|
|
||||||
const cost: number[] = [];
|
|
||||||
|
|
||||||
while (queue.length) {
|
|
||||||
const priority = queue.peekValue()!;
|
|
||||||
const {cellId, cultureId} = queue.pop()!;
|
|
||||||
|
|
||||||
const {type, expansionism, center} = getCulture(cultureId);
|
|
||||||
const cultureBiome = cells.biome[center];
|
|
||||||
|
|
||||||
cells.c[cellId].forEach(neibCellId => {
|
|
||||||
const biomeCost = getBiomeCost(neibCellId, cultureBiome, type);
|
|
||||||
const heightCost = getHeightCost(neibCellId, cells.h[neibCellId], type);
|
|
||||||
const riverCost = getRiverCost(cells.r[neibCellId], neibCellId, type);
|
|
||||||
const typeCost = getTypeCost(cells.t[neibCellId], type);
|
|
||||||
|
|
||||||
const totalCost = priority + (biomeCost + heightCost + riverCost + typeCost) / expansionism;
|
|
||||||
if (totalCost > maxExpansionCost) return;
|
|
||||||
|
|
||||||
if (!cost[neibCellId] || totalCost < cost[neibCellId]) {
|
|
||||||
if (cells.pop[neibCellId] > 0) cultureIds[neibCellId] = cultureId; // assign culture to populated cell
|
|
||||||
cost[neibCellId] = totalCost;
|
|
||||||
queue.push({cellId: neibCellId, cultureId}, totalCost);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
TIME && console.timeEnd("expandCultures");
|
|
||||||
return cultureIds;
|
|
||||||
|
|
||||||
function getCulture(cultureId: number) {
|
|
||||||
const culture = cultures[cultureId];
|
|
||||||
if (!isCulture(culture)) throw new Error("Wilderness cannot expand");
|
|
||||||
return culture;
|
|
||||||
}
|
|
||||||
|
|
||||||
function getBiomeCost(cellId: number, cultureBiome: number, type: TCultureType) {
|
|
||||||
const biome = cells.biome[cellId];
|
|
||||||
if (cultureBiome === biome) return 10; // tiny penalty for native biome
|
|
||||||
if (type === "Hunting") return biomesData.cost[biome] * 5; // non-native biome penalty for hunters
|
|
||||||
if (type === "Nomadic" && FOREST_BIOMES.includes(biome)) return biomesData.cost[biome] * 10; // forest biome penalty for nomads
|
|
||||||
return biomesData.cost[biome] * 2; // general non-native biome penalty
|
|
||||||
}
|
|
||||||
|
|
||||||
function getHeightCost(cellId: number, height: number, type: TCultureType) {
|
|
||||||
if (height < MIN_LAND_HEIGHT) {
|
|
||||||
const feature = features[cells.f[cellId]];
|
|
||||||
const area = cells.area[cellId];
|
|
||||||
|
|
||||||
if (type === "Lake" && feature && feature.type === "lake") return 10; // almost lake crossing penalty for Lake cultures
|
|
||||||
if (type === "Naval") return area * 2; // low sea or lake crossing penalty for Naval cultures
|
|
||||||
if (type === "Nomadic") return area * 50; // giant sea or lake crossing penalty for Nomads
|
|
||||||
return area * 6; // general sea or lake crossing penalty
|
|
||||||
}
|
|
||||||
|
|
||||||
if (type === "Highland") {
|
|
||||||
if (height >= MOUNTAINS) return 0; // no penalty for highlanders on highlands
|
|
||||||
if (height < HILLS) return 3000; // giant penalty for highlanders on lowlands
|
|
||||||
return 100; // penalty for highlanders on hills
|
|
||||||
}
|
|
||||||
|
|
||||||
if (height >= MOUNTAINS) return 200; // general mountains crossing penalty
|
|
||||||
if (height >= HILLS) return 30; // general hills crossing penalty
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
function getRiverCost(riverId: number, cellId: number, type: TCultureType) {
|
|
||||||
if (type === "River") return riverId ? 0 : 100; // penalty for river cultures
|
|
||||||
if (!riverId) return 0; // no penalty for others if there is no river
|
|
||||||
return minmax(cells.fl[cellId] / 10, 20, 100); // river penalty from 20 to 100 based on flux
|
|
||||||
}
|
|
||||||
|
|
||||||
function getTypeCost(t: number, type: TCultureType) {
|
|
||||||
if (t === LAND_COAST) return type === "Naval" || type === "Lake" ? 0 : type === "Nomadic" ? 60 : 20; // penalty for coastline
|
|
||||||
if (t === LANDLOCKED) return type === "Naval" || type === "Nomadic" ? 30 : 0; // low penalty for land level 2 for Navals and nomads
|
|
||||||
if (t !== WATER_COAST) return type === "Naval" || type === "Lake" ? 100 : 0; // penalty for mainland for navals
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
@ -14,7 +14,7 @@ export interface ILakeClimateData extends IPackFeatureLake {
|
||||||
enteringFlux?: number;
|
enteringFlux?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getClimateData = function (
|
export function getClimateData(
|
||||||
lakes: IPackFeatureLake[],
|
lakes: IPackFeatureLake[],
|
||||||
heights: Float32Array,
|
heights: Float32Array,
|
||||||
drainableLakes: Dict<boolean>,
|
drainableLakes: Dict<boolean>,
|
||||||
|
|
@ -44,13 +44,9 @@ export const getClimateData = function (
|
||||||
});
|
});
|
||||||
|
|
||||||
return lakeData;
|
return lakeData;
|
||||||
};
|
}
|
||||||
|
|
||||||
export const mergeLakeData = function (
|
export function mergeLakeData(features: TPackFeatures, lakeData: ILakeClimateData[], rivers: Pick<IRiver, "i">[]) {
|
||||||
features: TPackFeatures,
|
|
||||||
lakeData: ILakeClimateData[],
|
|
||||||
rivers: Pick<IRiver, "i">[]
|
|
||||||
) {
|
|
||||||
const updatedFeatures = features.map(feature => {
|
const updatedFeatures = features.map(feature => {
|
||||||
if (!feature) return 0;
|
if (!feature) return 0;
|
||||||
if (feature.type !== "lake") return feature;
|
if (feature.type !== "lake") return feature;
|
||||||
|
|
@ -71,7 +67,7 @@ export const mergeLakeData = function (
|
||||||
});
|
});
|
||||||
|
|
||||||
return updatedFeatures as TPackFeatures;
|
return updatedFeatures as TPackFeatures;
|
||||||
};
|
}
|
||||||
|
|
||||||
function defineLakeGroup({
|
function defineLakeGroup({
|
||||||
firstCell,
|
firstCell,
|
||||||
|
|
@ -1,21 +1,15 @@
|
||||||
import * as d3 from "d3";
|
|
||||||
|
|
||||||
import {UINT16_MAX} from "config/constants";
|
|
||||||
import {DISTANCE_FIELD, MIN_LAND_HEIGHT} from "config/generation";
|
|
||||||
import {TIME} from "config/logging";
|
|
||||||
import {calculateVoronoi} from "scripts/generation/graph";
|
|
||||||
import {markupPackFeatures} from "scripts/generation/markup";
|
import {markupPackFeatures} from "scripts/generation/markup";
|
||||||
import {rankCells} from "scripts/generation/pack/rankCells";
|
import {rankCells} from "scripts/generation/pack/rankCells";
|
||||||
import {createTypedArray} from "utils/arrayUtils";
|
|
||||||
import {pick} from "utils/functionUtils";
|
import {pick} from "utils/functionUtils";
|
||||||
import {rn} from "utils/numberUtils";
|
|
||||||
import {generateCultures, expandCultures} from "./cultures";
|
|
||||||
import {generateRivers} from "./rivers";
|
|
||||||
import {generateBurgsAndStates} from "./burgsAndStates/generateBurgsAndStates";
|
import {generateBurgsAndStates} from "./burgsAndStates/generateBurgsAndStates";
|
||||||
import {generateRoutes} from "./generateRoutes";
|
import {expandCultures} from "./cultures/expandCultures";
|
||||||
|
import {generateCultures} from "./cultures/generateCultures";
|
||||||
|
import {generateProvinces} from "./provinces/generateProvinces";
|
||||||
import {generateReligions} from "./religions/generateReligions";
|
import {generateReligions} from "./religions/generateReligions";
|
||||||
|
import {repackGrid} from "./repackGrid";
|
||||||
|
import {generateRivers} from "./rivers/generateRivers";
|
||||||
|
import {generateRoutes} from "./routes/generateRoutes";
|
||||||
|
|
||||||
const {LAND_COAST, WATER_COAST, DEEPER_WATER} = DISTANCE_FIELD;
|
|
||||||
const {Biomes} = window;
|
const {Biomes} = window;
|
||||||
|
|
||||||
export function createPack(grid: IGrid): IPack {
|
export function createPack(grid: IGrid): IPack {
|
||||||
|
|
@ -149,13 +143,10 @@ export function createPack(grid: IGrid): IPack {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const {provinceIds, provinces} = generateProvinces();
|
||||||
|
|
||||||
// BurgsAndStates.generateProvinces();
|
// BurgsAndStates.generateProvinces();
|
||||||
// BurgsAndStates.defineBurgFeatures();
|
// BurgsAndStates.defineBurgFeatures();
|
||||||
|
|
||||||
// renderLayer("states");
|
|
||||||
// renderLayer("borders");
|
|
||||||
// BurgsAndStates.drawStateLabels();
|
|
||||||
|
|
||||||
// Rivers.specify();
|
// Rivers.specify();
|
||||||
// const updatedFeatures = generateLakeNames();
|
// const updatedFeatures = generateLakeNames();
|
||||||
|
|
||||||
|
|
@ -190,7 +181,7 @@ export function createPack(grid: IGrid): IPack {
|
||||||
state: stateIds,
|
state: stateIds,
|
||||||
route: cellRoutes,
|
route: cellRoutes,
|
||||||
religion: religionIds,
|
religion: religionIds,
|
||||||
province: new Uint16Array(cells.i.length)
|
province: provinceIds
|
||||||
},
|
},
|
||||||
features: mergedFeatures,
|
features: mergedFeatures,
|
||||||
rivers: rawRivers, // "name" | "basin" | "type"
|
rivers: rawRivers, // "name" | "basin" | "type"
|
||||||
|
|
@ -199,77 +190,9 @@ export function createPack(grid: IGrid): IPack {
|
||||||
burgs,
|
burgs,
|
||||||
routes,
|
routes,
|
||||||
religions,
|
religions,
|
||||||
|
provinces,
|
||||||
events
|
events
|
||||||
};
|
};
|
||||||
|
|
||||||
return pack;
|
return pack;
|
||||||
}
|
}
|
||||||
|
|
||||||
// repack grid cells: discart deep water cells, add land cells along the coast
|
|
||||||
function repackGrid(grid: IGrid) {
|
|
||||||
TIME && console.time("repackGrid");
|
|
||||||
const {cells: gridCells, points, features} = grid;
|
|
||||||
const newCells: {p: TPoints; g: number[]; h: number[]} = {p: [], g: [], h: []}; // store new data
|
|
||||||
const spacing2 = grid.spacing ** 2;
|
|
||||||
|
|
||||||
for (const i of gridCells.i) {
|
|
||||||
const height = gridCells.h[i];
|
|
||||||
const type = gridCells.t[i];
|
|
||||||
|
|
||||||
// exclude ocean points far from coast
|
|
||||||
if (height < MIN_LAND_HEIGHT && type !== WATER_COAST && type !== DEEPER_WATER) continue;
|
|
||||||
|
|
||||||
const feature = features[gridCells.f[i]];
|
|
||||||
const isLake = feature && feature.type === "lake";
|
|
||||||
|
|
||||||
// exclude non-coastal lake points
|
|
||||||
if (type === DEEPER_WATER && (i % 4 === 0 || isLake)) continue;
|
|
||||||
|
|
||||||
const [x, y] = points[i];
|
|
||||||
addNewPoint(i, x, y, height);
|
|
||||||
|
|
||||||
// add additional points for cells along coast
|
|
||||||
if (type === LAND_COAST || type === WATER_COAST) {
|
|
||||||
if (gridCells.b[i]) continue; // not for near-border cells
|
|
||||||
gridCells.c[i].forEach(e => {
|
|
||||||
if (i > e) return;
|
|
||||||
if (gridCells.t[e] === type) {
|
|
||||||
const dist2 = (y - points[e][1]) ** 2 + (x - points[e][0]) ** 2;
|
|
||||||
if (dist2 < spacing2) return; // too close to each other
|
|
||||||
const x1 = rn((x + points[e][0]) / 2, 1);
|
|
||||||
const y1 = rn((y + points[e][1]) / 2, 1);
|
|
||||||
addNewPoint(i, x1, y1, height);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function addNewPoint(i: number, x: number, y: number, height: number) {
|
|
||||||
newCells.p.push([x, y]);
|
|
||||||
newCells.g.push(i);
|
|
||||||
newCells.h.push(height);
|
|
||||||
}
|
|
||||||
|
|
||||||
const {cells, vertices} = calculateVoronoi(newCells.p, grid.boundary);
|
|
||||||
|
|
||||||
function getCellArea(i: number) {
|
|
||||||
const polygon = cells.v[i].map(v => vertices.p[v]);
|
|
||||||
const area = Math.abs(d3.polygonArea(polygon));
|
|
||||||
return Math.min(area, UINT16_MAX);
|
|
||||||
}
|
|
||||||
|
|
||||||
const pack = {
|
|
||||||
vertices,
|
|
||||||
cells: {
|
|
||||||
...cells,
|
|
||||||
p: newCells.p,
|
|
||||||
g: createTypedArray({maxValue: grid.points.length, from: newCells.g}),
|
|
||||||
q: d3.quadtree(newCells.p.map(([x, y], i) => [x, y, i])) as unknown as Quadtree,
|
|
||||||
h: new Uint8Array(newCells.h),
|
|
||||||
area: createTypedArray({maxValue: UINT16_MAX, from: cells.i}).map(getCellArea)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
TIME && console.timeEnd("repackGrid");
|
|
||||||
return pack;
|
|
||||||
}
|
|
||||||
|
|
|
||||||
11
src/scripts/generation/pack/provinces/generateProvinces.ts
Normal file
11
src/scripts/generation/pack/provinces/generateProvinces.ts
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
import {TIME} from "config/logging";
|
||||||
|
|
||||||
|
export function generateProvinces() {
|
||||||
|
TIME && console.time("generateProvinces");
|
||||||
|
|
||||||
|
const provinceIds = new Uint16Array(1000); // cells.i.length
|
||||||
|
const provinces = [] as TProvinces;
|
||||||
|
|
||||||
|
TIME && console.timeEnd("generateProvinces");
|
||||||
|
return {provinceIds, provinces};
|
||||||
|
}
|
||||||
79
src/scripts/generation/pack/repackGrid.ts
Normal file
79
src/scripts/generation/pack/repackGrid.ts
Normal file
|
|
@ -0,0 +1,79 @@
|
||||||
|
import * as d3 from "d3";
|
||||||
|
|
||||||
|
import {UINT16_MAX} from "config/constants";
|
||||||
|
import {DISTANCE_FIELD, MIN_LAND_HEIGHT} from "config/generation";
|
||||||
|
import {TIME} from "config/logging";
|
||||||
|
import {createTypedArray} from "utils/arrayUtils";
|
||||||
|
import {rn} from "utils/numberUtils";
|
||||||
|
import {calculateVoronoi} from "../graph";
|
||||||
|
|
||||||
|
const {LAND_COAST, WATER_COAST, DEEPER_WATER} = DISTANCE_FIELD;
|
||||||
|
|
||||||
|
// repack grid cells: discart deep water cells, add land cells along the coast
|
||||||
|
export function repackGrid(grid: IGrid) {
|
||||||
|
TIME && console.time("repackGrid");
|
||||||
|
const {cells: gridCells, points, features} = grid;
|
||||||
|
const newCells: {p: TPoints; g: number[]; h: number[]} = {p: [], g: [], h: []}; // store new data
|
||||||
|
const spacing2 = grid.spacing ** 2;
|
||||||
|
|
||||||
|
for (const i of gridCells.i) {
|
||||||
|
const height = gridCells.h[i];
|
||||||
|
const type = gridCells.t[i];
|
||||||
|
|
||||||
|
// exclude ocean points far from coast
|
||||||
|
if (height < MIN_LAND_HEIGHT && type !== WATER_COAST && type !== DEEPER_WATER) continue;
|
||||||
|
|
||||||
|
const feature = features[gridCells.f[i]];
|
||||||
|
const isLake = feature && feature.type === "lake";
|
||||||
|
|
||||||
|
// exclude non-coastal lake points
|
||||||
|
if (type === DEEPER_WATER && (i % 4 === 0 || isLake)) continue;
|
||||||
|
|
||||||
|
const [x, y] = points[i];
|
||||||
|
addNewPoint(i, x, y, height);
|
||||||
|
|
||||||
|
// add additional points for cells along coast
|
||||||
|
if (type === LAND_COAST || type === WATER_COAST) {
|
||||||
|
if (gridCells.b[i]) continue; // not for near-border cells
|
||||||
|
gridCells.c[i].forEach(e => {
|
||||||
|
if (i > e) return;
|
||||||
|
if (gridCells.t[e] === type) {
|
||||||
|
const dist2 = (y - points[e][1]) ** 2 + (x - points[e][0]) ** 2;
|
||||||
|
if (dist2 < spacing2) return; // too close to each other
|
||||||
|
const x1 = rn((x + points[e][0]) / 2, 1);
|
||||||
|
const y1 = rn((y + points[e][1]) / 2, 1);
|
||||||
|
addNewPoint(i, x1, y1, height);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function addNewPoint(i: number, x: number, y: number, height: number) {
|
||||||
|
newCells.p.push([x, y]);
|
||||||
|
newCells.g.push(i);
|
||||||
|
newCells.h.push(height);
|
||||||
|
}
|
||||||
|
|
||||||
|
const {cells, vertices} = calculateVoronoi(newCells.p, grid.boundary);
|
||||||
|
|
||||||
|
function getCellArea(i: number) {
|
||||||
|
const polygon = cells.v[i].map(v => vertices.p[v]);
|
||||||
|
const area = Math.abs(d3.polygonArea(polygon));
|
||||||
|
return Math.min(area, UINT16_MAX);
|
||||||
|
}
|
||||||
|
|
||||||
|
const pack = {
|
||||||
|
vertices,
|
||||||
|
cells: {
|
||||||
|
...cells,
|
||||||
|
p: newCells.p,
|
||||||
|
g: createTypedArray({maxValue: grid.points.length, from: newCells.g}),
|
||||||
|
q: d3.quadtree(newCells.p.map(([x, y], i) => [x, y, i])) as unknown as Quadtree,
|
||||||
|
h: new Uint8Array(newCells.h),
|
||||||
|
area: createTypedArray({maxValue: UINT16_MAX, from: cells.i}).map(getCellArea)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
TIME && console.timeEnd("repackGrid");
|
||||||
|
return pack;
|
||||||
|
}
|
||||||
|
|
@ -1,13 +1,13 @@
|
||||||
import * as d3 from "d3";
|
import * as d3 from "d3";
|
||||||
|
|
||||||
import {INFO, TIME, WARN} from "config/logging";
|
import {TIME} from "config/logging";
|
||||||
import {rn} from "utils/numberUtils";
|
import {rn} from "utils/numberUtils";
|
||||||
import {aleaPRNG} from "scripts/aleaPRNG";
|
import {aleaPRNG} from "scripts/aleaPRNG";
|
||||||
import {DISTANCE_FIELD, MAX_HEIGHT, MIN_LAND_HEIGHT} from "config/generation";
|
import {DISTANCE_FIELD, MIN_LAND_HEIGHT} from "config/generation";
|
||||||
import {getInputNumber} from "utils/nodeUtils";
|
|
||||||
import {pick} from "utils/functionUtils";
|
import {pick} from "utils/functionUtils";
|
||||||
import {byId} from "utils/shorthands";
|
import {byId} from "utils/shorthands";
|
||||||
import {mergeLakeData, getClimateData, ILakeClimateData} from "./lakes";
|
import {mergeLakeData, getClimateData, ILakeClimateData} from "../lakes/lakes";
|
||||||
|
import {resolveDepressions} from "./resolveDepressions";
|
||||||
|
|
||||||
const {Rivers} = window;
|
const {Rivers} = window;
|
||||||
const {LAND_COAST} = DISTANCE_FIELD;
|
const {LAND_COAST} = DISTANCE_FIELD;
|
||||||
|
|
@ -276,155 +276,10 @@ export function generateRivers(
|
||||||
}
|
}
|
||||||
|
|
||||||
// add distance to water value to land cells to make map less depressed
|
// add distance to water value to land cells to make map less depressed
|
||||||
const applyDistanceField = ({h, c, t}: Pick<IPack["cells"], "h" | "c" | "t">) => {
|
function applyDistanceField({h, c, t}: Pick<IPack["cells"], "h" | "c" | "t">) {
|
||||||
return new Float32Array(h.length).map((_, index) => {
|
return new Float32Array(h.length).map((_, index) => {
|
||||||
if (h[index] < MIN_LAND_HEIGHT || t[index] < LAND_COAST) return h[index];
|
if (h[index] < MIN_LAND_HEIGHT || t[index] < LAND_COAST) return h[index];
|
||||||
const mean = d3.mean(c[index].map(c => t[c])) || 0;
|
const mean = d3.mean(c[index].map(c => t[c])) || 0;
|
||||||
return h[index] + t[index] / 100 + mean / 10000;
|
return h[index] + t[index] / 100 + mean / 10000;
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
|
|
||||||
// depression filling algorithm (for a correct water flux modeling)
|
|
||||||
const resolveDepressions = function (
|
|
||||||
cells: Pick<IPack["cells"], "i" | "c" | "b" | "f">,
|
|
||||||
features: TPackFeatures,
|
|
||||||
initialCellHeights: Float32Array
|
|
||||||
): [Float32Array, Dict<boolean>] {
|
|
||||||
TIME && console.time("resolveDepressions");
|
|
||||||
|
|
||||||
const MAX_INTERATIONS = getInputNumber("resolveDepressionsStepsOutput");
|
|
||||||
const checkLakeMaxIteration = MAX_INTERATIONS * 0.85;
|
|
||||||
const elevateLakeMaxIteration = MAX_INTERATIONS * 0.75;
|
|
||||||
|
|
||||||
const LAND_ELEVATION_INCREMENT = 0.1;
|
|
||||||
const LAKE_ELEVATION_INCREMENT = 0.2;
|
|
||||||
|
|
||||||
const lakes = features.filter(feature => feature && feature.type === "lake") as IPackFeatureLake[];
|
|
||||||
lakes.sort((a, b) => a.height - b.height); // lowest lakes go first
|
|
||||||
|
|
||||||
const getHeight = (i: number) => currentLakeHeights[cells.f[i]] || currentCellHeights[i];
|
|
||||||
const getMinHeight = (cellsIds: number[]) => Math.min(...cellsIds.map(getHeight));
|
|
||||||
const getMinLandHeight = (cellsIds: number[]) => Math.min(...cellsIds.map(i => currentCellHeights[i]));
|
|
||||||
|
|
||||||
const landCells = cells.i.filter(i => initialCellHeights[i] >= MIN_LAND_HEIGHT && !cells.b[i]);
|
|
||||||
landCells.sort((a, b) => initialCellHeights[a] - initialCellHeights[b]); // lowest cells go first
|
|
||||||
|
|
||||||
const currentCellHeights = Float32Array.from(initialCellHeights);
|
|
||||||
const currentLakeHeights = Object.fromEntries(lakes.map(({i, height}) => [i, height]));
|
|
||||||
const currentDrainableLakes = checkLakesDrainability();
|
|
||||||
const depressions: number[] = [];
|
|
||||||
|
|
||||||
let bestDepressions = Infinity;
|
|
||||||
let bestCellHeights: typeof currentCellHeights | null = null;
|
|
||||||
let bestDrainableLakes: typeof currentDrainableLakes | null = null;
|
|
||||||
|
|
||||||
for (let iteration = 0; depressions.at(-1) !== 0 && iteration < MAX_INTERATIONS; iteration++) {
|
|
||||||
let depressionsLeft = 0;
|
|
||||||
|
|
||||||
// elevate potentially drainable lakes
|
|
||||||
if (iteration < checkLakeMaxIteration) {
|
|
||||||
for (const lake of lakes) {
|
|
||||||
if (currentDrainableLakes[lake.i] !== true) continue;
|
|
||||||
|
|
||||||
const minShoreHeight = getMinLandHeight(lake.shoreline);
|
|
||||||
if (minShoreHeight >= MAX_HEIGHT || currentLakeHeights[lake.i] > minShoreHeight) continue;
|
|
||||||
|
|
||||||
if (iteration > elevateLakeMaxIteration) {
|
|
||||||
// reset heights
|
|
||||||
for (const shoreCellId of lake.shoreline) {
|
|
||||||
currentCellHeights[shoreCellId] = initialCellHeights[shoreCellId];
|
|
||||||
}
|
|
||||||
currentLakeHeights[lake.i] = lake.height;
|
|
||||||
|
|
||||||
currentDrainableLakes[lake.i] = false;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
currentLakeHeights[lake.i] = minShoreHeight + LAKE_ELEVATION_INCREMENT;
|
|
||||||
depressionsLeft++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const cellId of landCells) {
|
|
||||||
const minHeight = getMinHeight(cells.c[cellId]);
|
|
||||||
if (minHeight >= MAX_HEIGHT || currentCellHeights[cellId] > minHeight) continue;
|
|
||||||
|
|
||||||
currentCellHeights[cellId] = minHeight + LAND_ELEVATION_INCREMENT;
|
|
||||||
depressionsLeft++;
|
|
||||||
}
|
|
||||||
|
|
||||||
depressions.push(depressionsLeft);
|
|
||||||
if (depressionsLeft < bestDepressions) {
|
|
||||||
bestDepressions = depressionsLeft;
|
|
||||||
bestCellHeights = Float32Array.from(currentCellHeights);
|
|
||||||
bestDrainableLakes = structuredClone(currentDrainableLakes);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
TIME && console.timeEnd("resolveDepressions");
|
|
||||||
|
|
||||||
const depressionsLeft = depressions.at(-1);
|
|
||||||
if (depressionsLeft) {
|
|
||||||
if (bestCellHeights && bestDrainableLakes) {
|
|
||||||
WARN &&
|
|
||||||
console.warn(`Cannot resolve all depressions. Depressions: ${depressions[0]}. Best result: ${bestDepressions}`);
|
|
||||||
return [bestCellHeights, bestDrainableLakes];
|
|
||||||
}
|
|
||||||
|
|
||||||
WARN && console.warn(`Cannot resolve depressions. Depressions: ${depressionsLeft}`);
|
|
||||||
return [initialCellHeights, {}];
|
|
||||||
}
|
|
||||||
|
|
||||||
INFO && console.info(`ⓘ resolved all ${depressions[0]} depressions in ${depressions.length} iterations`);
|
|
||||||
return [currentCellHeights, currentDrainableLakes];
|
|
||||||
|
|
||||||
// define lakes that potentially can be open (drained into another water body)
|
|
||||||
function checkLakesDrainability() {
|
|
||||||
const canBeDrained: Dict<boolean> = {}; // all false by default
|
|
||||||
|
|
||||||
const ELEVATION_LIMIT = getInputNumber("lakeElevationLimitOutput");
|
|
||||||
const drainAllLakes = ELEVATION_LIMIT === MAX_HEIGHT - MIN_LAND_HEIGHT;
|
|
||||||
|
|
||||||
for (const lake of lakes) {
|
|
||||||
if (drainAllLakes) {
|
|
||||||
canBeDrained[lake.i] = true;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
canBeDrained[lake.i] = false;
|
|
||||||
const minShoreHeight = getMinHeight(lake.shoreline);
|
|
||||||
const minHeightShoreCell =
|
|
||||||
lake.shoreline.find(cellId => initialCellHeights[cellId] === minShoreHeight) || lake.shoreline[0];
|
|
||||||
|
|
||||||
const queue = [minHeightShoreCell];
|
|
||||||
const checked = [];
|
|
||||||
checked[minHeightShoreCell] = true;
|
|
||||||
const breakableHeight = lake.height + ELEVATION_LIMIT;
|
|
||||||
|
|
||||||
loopCellsAroundLake: while (queue.length) {
|
|
||||||
const cellId = queue.pop()!;
|
|
||||||
|
|
||||||
for (const neibCellId of cells.c[cellId]) {
|
|
||||||
if (checked[neibCellId]) continue;
|
|
||||||
if (initialCellHeights[neibCellId] >= breakableHeight) continue;
|
|
||||||
|
|
||||||
if (initialCellHeights[neibCellId] < MIN_LAND_HEIGHT) {
|
|
||||||
const waterFeatureMet = features[cells.f[neibCellId]];
|
|
||||||
const isOceanMet = waterFeatureMet && waterFeatureMet.type === "ocean";
|
|
||||||
const isLakeMet = waterFeatureMet && waterFeatureMet.type === "lake";
|
|
||||||
|
|
||||||
if (isOceanMet || (isLakeMet && lake.height > waterFeatureMet.height)) {
|
|
||||||
canBeDrained[lake.i] = true;
|
|
||||||
break loopCellsAroundLake;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
checked[neibCellId] = true;
|
|
||||||
queue.push(neibCellId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return canBeDrained;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
148
src/scripts/generation/pack/rivers/resolveDepressions.ts
Normal file
148
src/scripts/generation/pack/rivers/resolveDepressions.ts
Normal file
|
|
@ -0,0 +1,148 @@
|
||||||
|
import {MIN_LAND_HEIGHT, MAX_HEIGHT} from "config/generation";
|
||||||
|
import {TIME, WARN, INFO} from "config/logging";
|
||||||
|
import {getInputNumber} from "utils/nodeUtils";
|
||||||
|
|
||||||
|
// depression filling algorithm (for a correct water flux modeling)
|
||||||
|
export function resolveDepressions(
|
||||||
|
cells: Pick<IPack["cells"], "i" | "c" | "b" | "f">,
|
||||||
|
features: TPackFeatures,
|
||||||
|
initialCellHeights: Float32Array
|
||||||
|
): [Float32Array, Dict<boolean>] {
|
||||||
|
TIME && console.time("resolveDepressions");
|
||||||
|
|
||||||
|
const MAX_INTERATIONS = getInputNumber("resolveDepressionsStepsOutput");
|
||||||
|
const checkLakeMaxIteration = MAX_INTERATIONS * 0.85;
|
||||||
|
const elevateLakeMaxIteration = MAX_INTERATIONS * 0.75;
|
||||||
|
|
||||||
|
const LAND_ELEVATION_INCREMENT = 0.1;
|
||||||
|
const LAKE_ELEVATION_INCREMENT = 0.2;
|
||||||
|
|
||||||
|
const lakes = features.filter(feature => feature && feature.type === "lake") as IPackFeatureLake[];
|
||||||
|
lakes.sort((a, b) => a.height - b.height); // lowest lakes go first
|
||||||
|
|
||||||
|
const getHeight = (i: number) => currentLakeHeights[cells.f[i]] || currentCellHeights[i];
|
||||||
|
const getMinHeight = (cellsIds: number[]) => Math.min(...cellsIds.map(getHeight));
|
||||||
|
const getMinLandHeight = (cellsIds: number[]) => Math.min(...cellsIds.map(i => currentCellHeights[i]));
|
||||||
|
|
||||||
|
const landCells = cells.i.filter(i => initialCellHeights[i] >= MIN_LAND_HEIGHT && !cells.b[i]);
|
||||||
|
landCells.sort((a, b) => initialCellHeights[a] - initialCellHeights[b]); // lowest cells go first
|
||||||
|
|
||||||
|
const currentCellHeights = Float32Array.from(initialCellHeights);
|
||||||
|
const currentLakeHeights = Object.fromEntries(lakes.map(({i, height}) => [i, height]));
|
||||||
|
const currentDrainableLakes = checkLakesDrainability();
|
||||||
|
const depressions: number[] = [];
|
||||||
|
|
||||||
|
let bestDepressions = Infinity;
|
||||||
|
let bestCellHeights: typeof currentCellHeights | null = null;
|
||||||
|
let bestDrainableLakes: typeof currentDrainableLakes | null = null;
|
||||||
|
|
||||||
|
for (let iteration = 0; depressions.at(-1) !== 0 && iteration < MAX_INTERATIONS; iteration++) {
|
||||||
|
let depressionsLeft = 0;
|
||||||
|
|
||||||
|
// elevate potentially drainable lakes
|
||||||
|
if (iteration < checkLakeMaxIteration) {
|
||||||
|
for (const lake of lakes) {
|
||||||
|
if (currentDrainableLakes[lake.i] !== true) continue;
|
||||||
|
|
||||||
|
const minShoreHeight = getMinLandHeight(lake.shoreline);
|
||||||
|
if (minShoreHeight >= MAX_HEIGHT || currentLakeHeights[lake.i] > minShoreHeight) continue;
|
||||||
|
|
||||||
|
if (iteration > elevateLakeMaxIteration) {
|
||||||
|
// reset heights
|
||||||
|
for (const shoreCellId of lake.shoreline) {
|
||||||
|
currentCellHeights[shoreCellId] = initialCellHeights[shoreCellId];
|
||||||
|
}
|
||||||
|
currentLakeHeights[lake.i] = lake.height;
|
||||||
|
|
||||||
|
currentDrainableLakes[lake.i] = false;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
currentLakeHeights[lake.i] = minShoreHeight + LAKE_ELEVATION_INCREMENT;
|
||||||
|
depressionsLeft++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const cellId of landCells) {
|
||||||
|
const minHeight = getMinHeight(cells.c[cellId]);
|
||||||
|
if (minHeight >= MAX_HEIGHT || currentCellHeights[cellId] > minHeight) continue;
|
||||||
|
|
||||||
|
currentCellHeights[cellId] = minHeight + LAND_ELEVATION_INCREMENT;
|
||||||
|
depressionsLeft++;
|
||||||
|
}
|
||||||
|
|
||||||
|
depressions.push(depressionsLeft);
|
||||||
|
if (depressionsLeft < bestDepressions) {
|
||||||
|
bestDepressions = depressionsLeft;
|
||||||
|
bestCellHeights = Float32Array.from(currentCellHeights);
|
||||||
|
bestDrainableLakes = structuredClone(currentDrainableLakes);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TIME && console.timeEnd("resolveDepressions");
|
||||||
|
|
||||||
|
const depressionsLeft = depressions.at(-1);
|
||||||
|
if (depressionsLeft) {
|
||||||
|
if (bestCellHeights && bestDrainableLakes) {
|
||||||
|
WARN &&
|
||||||
|
console.warn(`Cannot resolve all depressions. Depressions: ${depressions[0]}. Best result: ${bestDepressions}`);
|
||||||
|
return [bestCellHeights, bestDrainableLakes];
|
||||||
|
}
|
||||||
|
|
||||||
|
WARN && console.warn(`Cannot resolve depressions. Depressions: ${depressionsLeft}`);
|
||||||
|
return [initialCellHeights, {}];
|
||||||
|
}
|
||||||
|
|
||||||
|
INFO && console.info(`ⓘ resolved all ${depressions[0]} depressions in ${depressions.length} iterations`);
|
||||||
|
return [currentCellHeights, currentDrainableLakes];
|
||||||
|
|
||||||
|
// define lakes that potentially can be open (drained into another water body)
|
||||||
|
function checkLakesDrainability() {
|
||||||
|
const canBeDrained: Dict<boolean> = {}; // all false by default
|
||||||
|
|
||||||
|
const ELEVATION_LIMIT = getInputNumber("lakeElevationLimitOutput");
|
||||||
|
const drainAllLakes = ELEVATION_LIMIT === MAX_HEIGHT - MIN_LAND_HEIGHT;
|
||||||
|
|
||||||
|
for (const lake of lakes) {
|
||||||
|
if (drainAllLakes) {
|
||||||
|
canBeDrained[lake.i] = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
canBeDrained[lake.i] = false;
|
||||||
|
const minShoreHeight = getMinHeight(lake.shoreline);
|
||||||
|
const minHeightShoreCell =
|
||||||
|
lake.shoreline.find(cellId => initialCellHeights[cellId] === minShoreHeight) || lake.shoreline[0];
|
||||||
|
|
||||||
|
const queue = [minHeightShoreCell];
|
||||||
|
const checked = [];
|
||||||
|
checked[minHeightShoreCell] = true;
|
||||||
|
const breakableHeight = lake.height + ELEVATION_LIMIT;
|
||||||
|
|
||||||
|
loopCellsAroundLake: while (queue.length) {
|
||||||
|
const cellId = queue.pop()!;
|
||||||
|
|
||||||
|
for (const neibCellId of cells.c[cellId]) {
|
||||||
|
if (checked[neibCellId]) continue;
|
||||||
|
if (initialCellHeights[neibCellId] >= breakableHeight) continue;
|
||||||
|
|
||||||
|
if (initialCellHeights[neibCellId] < MIN_LAND_HEIGHT) {
|
||||||
|
const waterFeatureMet = features[cells.f[neibCellId]];
|
||||||
|
const isOceanMet = waterFeatureMet && waterFeatureMet.type === "ocean";
|
||||||
|
const isLakeMet = waterFeatureMet && waterFeatureMet.type === "lake";
|
||||||
|
|
||||||
|
if (isOceanMet || (isLakeMet && lake.height > waterFeatureMet.height)) {
|
||||||
|
canBeDrained[lake.i] = true;
|
||||||
|
break loopCellsAroundLake;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
checked[neibCellId] = true;
|
||||||
|
queue.push(neibCellId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return canBeDrained;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,10 +1,10 @@
|
||||||
import Delaunator from "delaunator";
|
|
||||||
import FlatQueue from "flatqueue";
|
import FlatQueue from "flatqueue";
|
||||||
|
|
||||||
import {TIME} from "config/logging";
|
import {TIME} from "config/logging";
|
||||||
import {ELEVATION, MIN_LAND_HEIGHT, ROUTES} from "config/generation";
|
import {ELEVATION, MIN_LAND_HEIGHT, ROUTES} from "config/generation";
|
||||||
import {dist2} from "utils/functionUtils";
|
import {dist2} from "utils/functionUtils";
|
||||||
import {isBurg} from "utils/typeUtils";
|
import {isBurg} from "utils/typeUtils";
|
||||||
|
import {calculateUrquhartEdges} from "./urquhart";
|
||||||
|
|
||||||
type TCellsData = Pick<IPack["cells"], "c" | "p" | "g" | "h" | "t" | "biome" | "burg">;
|
type TCellsData = Pick<IPack["cells"], "c" | "p" | "g" | "h" | "t" | "biome" | "burg">;
|
||||||
|
|
||||||
|
|
@ -292,44 +292,3 @@ function getRouteSegments(pathCells: number[], connections: Map<string, boolean>
|
||||||
|
|
||||||
return segments;
|
return segments;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Urquhart graph is obtained by removing the longest edge from each triangle in the Delaunay triangulation
|
|
||||||
// this gives us an aproximation of a desired road network, i.e. connections between burgs
|
|
||||||
// code from https://observablehq.com/@mbostock/urquhart-graph
|
|
||||||
function calculateUrquhartEdges(points: TPoints) {
|
|
||||||
const score = (p0: number, p1: number) => dist2(points[p0], points[p1]);
|
|
||||||
|
|
||||||
const {halfedges, triangles} = Delaunator.from(points);
|
|
||||||
const n = triangles.length;
|
|
||||||
|
|
||||||
const removed = new Uint8Array(n);
|
|
||||||
const edges = [];
|
|
||||||
|
|
||||||
for (let e = 0; e < n; e += 3) {
|
|
||||||
const p0 = triangles[e],
|
|
||||||
p1 = triangles[e + 1],
|
|
||||||
p2 = triangles[e + 2];
|
|
||||||
|
|
||||||
const p01 = score(p0, p1),
|
|
||||||
p12 = score(p1, p2),
|
|
||||||
p20 = score(p2, p0);
|
|
||||||
|
|
||||||
removed[
|
|
||||||
p20 > p01 && p20 > p12
|
|
||||||
? Math.max(e + 2, halfedges[e + 2])
|
|
||||||
: p12 > p01 && p12 > p20
|
|
||||||
? Math.max(e + 1, halfedges[e + 1])
|
|
||||||
: Math.max(e, halfedges[e])
|
|
||||||
] = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (let e = 0; e < n; ++e) {
|
|
||||||
if (e > halfedges[e] && !removed[e]) {
|
|
||||||
const t0 = triangles[e];
|
|
||||||
const t1 = triangles[e % 3 === 2 ? e - 2 : e + 1];
|
|
||||||
edges.push([t0, t1]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return edges;
|
|
||||||
}
|
|
||||||
44
src/scripts/generation/pack/routes/urquhart.ts
Normal file
44
src/scripts/generation/pack/routes/urquhart.ts
Normal file
|
|
@ -0,0 +1,44 @@
|
||||||
|
import Delaunator from "delaunator";
|
||||||
|
|
||||||
|
import {dist2} from "utils/functionUtils";
|
||||||
|
|
||||||
|
// Urquhart graph is obtained by removing the longest edge from each triangle in the Delaunay triangulation
|
||||||
|
// this gives us an aproximation of a desired road network, i.e. connections between burgs
|
||||||
|
// code from https://observablehq.com/@mbostock/urquhart-graph
|
||||||
|
export function calculateUrquhartEdges(points: TPoints) {
|
||||||
|
const score = (p0: number, p1: number) => dist2(points[p0], points[p1]);
|
||||||
|
|
||||||
|
const {halfedges, triangles} = Delaunator.from(points);
|
||||||
|
const n = triangles.length;
|
||||||
|
|
||||||
|
const removed = new Uint8Array(n);
|
||||||
|
const edges = [];
|
||||||
|
|
||||||
|
for (let e = 0; e < n; e += 3) {
|
||||||
|
const p0 = triangles[e],
|
||||||
|
p1 = triangles[e + 1],
|
||||||
|
p2 = triangles[e + 2];
|
||||||
|
|
||||||
|
const p01 = score(p0, p1),
|
||||||
|
p12 = score(p1, p2),
|
||||||
|
p20 = score(p2, p0);
|
||||||
|
|
||||||
|
removed[
|
||||||
|
p20 > p01 && p20 > p12
|
||||||
|
? Math.max(e + 2, halfedges[e + 2])
|
||||||
|
: p12 > p01 && p12 > p20
|
||||||
|
? Math.max(e + 1, halfedges[e + 1])
|
||||||
|
: Math.max(e, halfedges[e])
|
||||||
|
] = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let e = 0; e < n; ++e) {
|
||||||
|
if (e > halfedges[e] && !removed[e]) {
|
||||||
|
const t0 = triangles[e];
|
||||||
|
const t1 = triangles[e % 3 === 2 ? e - 2 : e + 1];
|
||||||
|
edges.push([t0, t1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return edges;
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue