mirror of
https://github.com/Azgaar/Fantasy-Map-Generator.git
synced 2025-12-17 17:51:24 +01:00
refactor: ckeanup
This commit is contained in:
parent
fe4d4eac86
commit
bb78d5168b
6 changed files with 23 additions and 93 deletions
|
|
@ -9,7 +9,7 @@ import {byId} from "utils/shorthands";
|
||||||
import {ERROR} from "../config/logging";
|
import {ERROR} from "../config/logging";
|
||||||
import {lim, minmax} from "../utils/numberUtils";
|
import {lim, minmax} from "../utils/numberUtils";
|
||||||
import {aleaPRNG} from "scripts/aleaPRNG";
|
import {aleaPRNG} from "scripts/aleaPRNG";
|
||||||
import {openNearSeaLakes, addLakesInDeepDepressions} from "scripts/generation/grid/lakes";
|
import {addLakesInDeepDepressions} from "scripts/generation/grid/lakes";
|
||||||
|
|
||||||
window.HeightmapGenerator = (function () {
|
window.HeightmapGenerator = (function () {
|
||||||
let grid = null;
|
let grid = null;
|
||||||
|
|
@ -78,14 +78,7 @@ window.HeightmapGenerator = (function () {
|
||||||
Math.random = aleaPRNG(seed);
|
Math.random = aleaPRNG(seed);
|
||||||
|
|
||||||
const rawHeights = id in heightmapTemplates ? fromTemplate(graph, id) : await fromPrecreated(graph, id);
|
const rawHeights = id in heightmapTemplates ? fromTemplate(graph, id) : await fromPrecreated(graph, id);
|
||||||
const removedLakesHeights = openNearSeaLakes(rawHeights, graph.cells.c, graph.cells.i, graph.cells.b);
|
const addedLakesHeights = addLakesInDeepDepressions(rawHeights, graph.cells.c, graph.cells.i);
|
||||||
const addedLakesHeights = addLakesInDeepDepressions(
|
|
||||||
removedLakesHeights,
|
|
||||||
graph.cells.c,
|
|
||||||
graph.cells.v,
|
|
||||||
graph.vertices,
|
|
||||||
graph.cells.i
|
|
||||||
);
|
|
||||||
|
|
||||||
TIME && console.timeEnd("defineHeightmap");
|
TIME && console.timeEnd("defineHeightmap");
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ import {getMiddlePoint} from "utils/lineUtils";
|
||||||
import {rn} from "utils/numberUtils";
|
import {rn} from "utils/numberUtils";
|
||||||
import {aleaPRNG} from "scripts/aleaPRNG";
|
import {aleaPRNG} from "scripts/aleaPRNG";
|
||||||
import {renderLayer} from "layers";
|
import {renderLayer} from "layers";
|
||||||
import {markupGridFeatures} from "modules/markup";
|
import {markupGridFeatures} from "scripts/generation/markup";
|
||||||
import {generateGrid} from "scripts/generation/graph";
|
import {generateGrid} from "scripts/generation/graph";
|
||||||
|
|
||||||
window.Submap = (function () {
|
window.Submap = (function () {
|
||||||
|
|
|
||||||
|
|
@ -64,8 +64,8 @@ async function generate(options?: IGenerationOptions) {
|
||||||
// temp rendering for debug
|
// temp rendering for debug
|
||||||
// renderLayer("cells");
|
// renderLayer("cells");
|
||||||
renderLayer("features");
|
renderLayer("features");
|
||||||
// renderLayer("heightmap");
|
renderLayer("heightmap");
|
||||||
// renderLayer("rivers", pack);
|
renderLayer("rivers", pack);
|
||||||
|
|
||||||
WARN && console.warn(`TOTAL: ${rn((performance.now() - timeStart) / 1000, 2)}s`);
|
WARN && console.warn(`TOTAL: ${rn((performance.now() - timeStart) / 1000, 2)}s`);
|
||||||
// showStatistics();
|
// showStatistics();
|
||||||
|
|
|
||||||
|
|
@ -1,77 +1,10 @@
|
||||||
import {TIME} from "config/logging";
|
import {TIME} from "config/logging";
|
||||||
import {getInputNumber, getInputValue} from "utils/nodeUtils";
|
import {getInputNumber} from "utils/nodeUtils";
|
||||||
import {DISTANCE_FIELD, MAX_HEIGHT, MIN_LAND_HEIGHT} from "config/generation";
|
import {MAX_HEIGHT, MIN_LAND_HEIGHT} from "config/generation";
|
||||||
import {drawPolygon} from "utils/debugUtils";
|
|
||||||
|
|
||||||
const {LAND_COAST, WATER_COAST} = DISTANCE_FIELD;
|
|
||||||
|
|
||||||
// near sea lakes usually get a lot of water inflow
|
|
||||||
// most of them would brake threshold and flow out to sea (see Ancylus Lake)
|
|
||||||
// connect these type of lakes to the main water body to improve the heightmap
|
|
||||||
export function openNearSeaLakes(
|
|
||||||
heights: Uint8Array,
|
|
||||||
neighbours: number[][],
|
|
||||||
indexes: UintArray,
|
|
||||||
borderCells: IGraphCells["b"]
|
|
||||||
) {
|
|
||||||
if (getInputValue("templateInput") === "Atoll") return; // no need for Atolls
|
|
||||||
|
|
||||||
TIME && console.time("openNearSeaLakes");
|
|
||||||
const MAX_BREACHABLE_HEIGHT = 22; // max height that can be breached by water
|
|
||||||
|
|
||||||
const features: Dict<"ocean" | "lake"> = {};
|
|
||||||
const featureIds: Dict<number> = {};
|
|
||||||
let featureId = 0;
|
|
||||||
|
|
||||||
const lakeCoastalCells: Dict<number[]> = {};
|
|
||||||
|
|
||||||
for (const cellId of indexes) {
|
|
||||||
if (featureIds[cellId]) continue;
|
|
||||||
if (heights[cellId] >= MIN_LAND_HEIGHT) continue;
|
|
||||||
|
|
||||||
featureId += 1;
|
|
||||||
const breachableCoastalCells: number[] = [];
|
|
||||||
let isLake = true; // lakes are features surrounded by land cells
|
|
||||||
|
|
||||||
const queue = [cellId];
|
|
||||||
featureIds[cellId] = featureId;
|
|
||||||
|
|
||||||
while (queue.length) {
|
|
||||||
const nextCellId = queue.pop()!;
|
|
||||||
|
|
||||||
for (const neighborId of neighbours[nextCellId]) {
|
|
||||||
if (isLake && borderCells[neighborId]) isLake = false;
|
|
||||||
if (featureIds[neighborId]) continue;
|
|
||||||
const height = heights[neighborId];
|
|
||||||
|
|
||||||
if (height < MIN_LAND_HEIGHT) {
|
|
||||||
featureIds[neighborId] = featureId;
|
|
||||||
queue.push(neighborId);
|
|
||||||
} else if (isLake && height <= MAX_BREACHABLE_HEIGHT) {
|
|
||||||
breachableCoastalCells.push(neighborId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
features[featureId] = isLake ? "lake" : "ocean";
|
|
||||||
if (isLake) lakeCoastalCells[featureId] = breachableCoastalCells;
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log(featureIds, features, lakeCoastalCells);
|
|
||||||
|
|
||||||
TIME && console.timeEnd("openNearSeaLakes");
|
|
||||||
return heights;
|
|
||||||
}
|
|
||||||
|
|
||||||
// some deeply depressed areas may not be resolved on river generation
|
// some deeply depressed areas may not be resolved on river generation
|
||||||
// this areas tend to collect precipitation, so we can add a lake there to help the resolver
|
// this areas tend to collect precipitation, so we can add a lake there to help the resolver
|
||||||
export function addLakesInDeepDepressions(
|
export function addLakesInDeepDepressions(heights: Uint8Array, neighbours: number[][], indexes: UintArray) {
|
||||||
heights: Uint8Array,
|
|
||||||
neighbours: number[][],
|
|
||||||
cellVertices: number[][],
|
|
||||||
vertices: IGraphVertices,
|
|
||||||
indexes: UintArray
|
|
||||||
) {
|
|
||||||
const ELEVATION_LIMIT = getInputNumber("lakeElevationLimitOutput");
|
const ELEVATION_LIMIT = getInputNumber("lakeElevationLimitOutput");
|
||||||
if (ELEVATION_LIMIT === MAX_HEIGHT - MIN_LAND_HEIGHT) return heights; // any depression can be resolved
|
if (ELEVATION_LIMIT === MAX_HEIGHT - MIN_LAND_HEIGHT) return heights; // any depression can be resolved
|
||||||
|
|
||||||
|
|
@ -114,9 +47,6 @@ export function addLakesInDeepDepressions(
|
||||||
if (inDeepDepression) {
|
if (inDeepDepression) {
|
||||||
currentHeights[cellId] = MIN_LAND_HEIGHT - 1;
|
currentHeights[cellId] = MIN_LAND_HEIGHT - 1;
|
||||||
console.log(`ⓘ Added lake at deep depression. Cell: ${cellId}`);
|
console.log(`ⓘ Added lake at deep depression. Cell: ${cellId}`);
|
||||||
|
|
||||||
const polygon = cellVertices[cellId].map(vertex => vertices.p[vertex]);
|
|
||||||
drawPolygon(polygon, {stroke: "red", strokeWidth: 1, fill: "none"});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -26,11 +26,18 @@ export function createPack(grid: IGrid): IPack {
|
||||||
pick(cells, "v", "c", "b", "p", "h")
|
pick(cells, "v", "c", "b", "p", "h")
|
||||||
);
|
);
|
||||||
|
|
||||||
const {heights, flux, r, conf, rivers, mergedFeatures} = generateRivers(
|
const {
|
||||||
grid.cells.prec,
|
heights,
|
||||||
grid.cells.temp,
|
flux,
|
||||||
|
r,
|
||||||
|
conf,
|
||||||
|
rivers: rawRivers,
|
||||||
|
mergedFeatures
|
||||||
|
} = generateRivers(
|
||||||
{...pick(cells, "i", "c", "b", "g", "h", "p"), f: featureIds, t: distanceField, haven},
|
{...pick(cells, "i", "c", "b", "g", "h", "p"), f: featureIds, t: distanceField, haven},
|
||||||
features
|
features,
|
||||||
|
grid.cells.prec,
|
||||||
|
grid.cells.temp
|
||||||
);
|
);
|
||||||
|
|
||||||
// Biomes.define(newPack, grid);
|
// Biomes.define(newPack, grid);
|
||||||
|
|
@ -76,7 +83,7 @@ export function createPack(grid: IGrid): IPack {
|
||||||
conf
|
conf
|
||||||
},
|
},
|
||||||
features: mergedFeatures,
|
features: mergedFeatures,
|
||||||
rivers
|
rivers: rawRivers
|
||||||
};
|
};
|
||||||
|
|
||||||
return pack;
|
return pack;
|
||||||
|
|
|
||||||
|
|
@ -13,10 +13,10 @@ const {Rivers} = window;
|
||||||
const {LAND_COAST} = DISTANCE_FIELD;
|
const {LAND_COAST} = DISTANCE_FIELD;
|
||||||
|
|
||||||
export function generateRivers(
|
export function generateRivers(
|
||||||
precipitation: IGrid["cells"]["prec"],
|
|
||||||
temperature: IGrid["cells"]["temp"],
|
|
||||||
cells: Pick<IPack["cells"], "i" | "c" | "p" | "b" | "g" | "t" | "h" | "f" | "haven">,
|
cells: Pick<IPack["cells"], "i" | "c" | "p" | "b" | "g" | "t" | "h" | "f" | "haven">,
|
||||||
features: TPackFeatures
|
features: TPackFeatures,
|
||||||
|
precipitation: IGrid["cells"]["prec"],
|
||||||
|
temperature: IGrid["cells"]["temp"]
|
||||||
) {
|
) {
|
||||||
TIME && console.time("generateRivers");
|
TIME && console.time("generateRivers");
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue