refactor: refactor greneration routine

This commit is contained in:
Azgaar 2022-07-10 23:31:03 +03:00
parent d1208b12ec
commit 6b2de4d20e
19 changed files with 401 additions and 292 deletions

View file

@ -151,7 +151,7 @@ export function loadMapFromURL(maplink, random) {
});
}
function showUploadErrorMessage(error, URL, random) {
export function showUploadErrorMessage(error, URL, random) {
ERROR && console.error(error);
alertMessage.innerHTML = /* html */ `Cannot load map from the ${link(URL, "link provided")}. ${
random ? `A new random map is generated. ` : ""
@ -168,7 +168,7 @@ function showUploadErrorMessage(error, URL, random) {
});
}
function uploadMap(file, callback) {
export function uploadMap(file, callback) {
uploadMap.timeStart = performance.now();
const OLDEST_SUPPORTED_VERSION = 0.7;
const currentVersion = parseFloat(version);

View file

@ -4,6 +4,8 @@ import {TIME} from "config/logging";
import {rn} from "utils/numberUtils";
import {aleaPRNG} from "scripts/aleaPRNG";
import {byId} from "utils/shorthands";
import {getInputNumber, getInputValue} from "utils/nodeUtils";
import {DISTANCE_FIELD, MIN_LAND_HEIGHT} from "config/generation";
window.Lakes = (function () {
const setClimateData = function (h) {
@ -154,17 +156,21 @@ window.Lakes = (function () {
return "freshwater";
}
function addLakesInDeepDepressions() {
TIME && console.time("addLakesInDeepDepressions");
const {cells, features} = grid;
const {c, h, b} = cells;
const ELEVATION_LIMIT = +byId("lakeElevationLimitOutput").value;
const {LAND_COAST, WATER_COAST} = DISTANCE_FIELD;
function addLakesInDeepDepressions(grid: IGraph & Partial<IGrid>) {
const ELEVATION_LIMIT = getInputNumber("lakeElevationLimitOutput");
if (ELEVATION_LIMIT === 80) return;
for (const i of cells.i) {
if (b[i] || h[i] < 20) continue;
TIME && console.time("addLakesInDeepDepressions");
const {cells, features} = grid;
if (!features) throw new Error("addLakesInDeepDepressions: features are not defined");
const {c, h, b} = cells;
const minHeight = d3.min(c[i].map(c => h[c]));
for (const i of cells.i) {
if (b[i] || h[i] < MIN_LAND_HEIGHT) continue;
const minHeight = d3.min(c[i].map(c => h[c])) || 0;
if (h[i] > minHeight) continue;
let deep = true;
@ -175,12 +181,12 @@ window.Lakes = (function () {
// check if elevated cell can potentially pour to water
while (deep && queue.length) {
const q = queue.pop();
const q = queue.pop()!;
for (const n of c[q]) {
if (checked[n]) continue;
if (h[n] >= threshold) continue;
if (h[n] < 20) {
if (h[n] < MIN_LAND_HEIGHT) {
deep = false;
break;
}
@ -197,56 +203,68 @@ window.Lakes = (function () {
}
}
function addLake(lakeCells) {
const f = features.length;
function addLake(lakeCells: number[]) {
const featureId = features!.length;
lakeCells.forEach(i => {
cells.h[i] = 19;
cells.t[i] = -1;
cells.f[i] = f;
c[i].forEach(n => !lakeCells.includes(n) && (cells.t[c] = 1));
});
for (const lakeCellId of lakeCells) {
cells.h[lakeCellId] = MIN_LAND_HEIGHT - 1;
cells.t[lakeCellId] = WATER_COAST;
cells.f[lakeCellId] = featureId;
features.push({i: f, land: false, border: false, type: "lake"});
for (const neibCellId of c[lakeCellId]) {
if (!lakeCells.includes(neibCellId)) cells.t[neibCellId] = LAND_COAST;
}
}
features!.push({i: featureId, land: false, border: false, type: "lake"});
}
TIME && console.timeEnd("addLakesInDeepDepressions");
}
// near sea lakes usually get a lot of water inflow, most of them should brake threshold and flow out to sea (see Ancylus Lake)
function openNearSeaLakes() {
if (byId("templateInput").value === "Atoll") return; // no need for Atolls
function openNearSeaLakes(grid: IGraph & Partial<IGrid>) {
if (getInputValue("templateInput") === "Atoll") return; // no need for Atolls
const {cells, features} = grid;
if (!features?.find(f => f && f.type === "lake")) return; // no lakes
const cells = grid.cells;
const features = grid.features;
if (!features.find(f => f.type === "lake")) return; // no lakes
TIME && console.time("openLakes");
const LIMIT = 22; // max height that can be breached by water
for (const i of cells.i) {
const lake = cells.f[i];
if (features[lake].type !== "lake") continue; // not a lake cell
const isLake = (featureId: number) => featureId && (features[featureId] as IGridFeature).type === "lake";
const isOcean = (featureId: number) => featureId && (features[featureId] as IGridFeature).type === "ocean";
check_neighbours: for (const c of cells.c[i]) {
if (cells.t[c] !== 1 || cells.h[c] > LIMIT) continue; // water cannot brake this
for (const cellId of cells.i) {
const featureId = cells.f[cellId];
if (!isLake(featureId)) continue; // not a lake cell
for (const n of cells.c[c]) {
const ocean = cells.f[n];
if (features[ocean].type !== "ocean") continue; // not an ocean
removeLake(c, lake, ocean);
check_neighbours: for (const neibCellId of cells.c[cellId]) {
// water cannot brake the barrier
if (cells.t[neibCellId] !== WATER_COAST || cells.h[neibCellId] > LIMIT) continue;
for (const neibOfNeibCellId of cells.c[neibCellId]) {
const neibOfNeibFeatureId = cells.f[neibOfNeibCellId];
if (!isOcean(neibOfNeibFeatureId)) continue; // not an ocean
removeLake(neibCellId, featureId, neibOfNeibFeatureId);
break check_neighbours;
}
}
}
function removeLake(threshold, lake, ocean) {
cells.h[threshold] = 19;
cells.t[threshold] = -1;
cells.f[threshold] = ocean;
cells.c[threshold].forEach(function (c) {
if (cells.h[c] >= 20) cells.t[c] = 1; // mark as coastline
});
features[lake].type = "ocean"; // mark former lake as ocean
function removeLake(barrierCellId: number, lakeFeatureId: number, oceanFeatureId: number) {
cells.h[barrierCellId] = MIN_LAND_HEIGHT - 1;
cells.t[barrierCellId] = WATER_COAST;
cells.f[barrierCellId] = oceanFeatureId;
for (const neibCellId of cells.c[barrierCellId]) {
if (cells.h[neibCellId] >= MIN_LAND_HEIGHT) cells.t[neibCellId] = LAND_COAST;
}
if (features && lakeFeatureId) {
// mark former lake as ocean
(features[lakeFeatureId] as IGridFeature).type = "ocean";
}
}
TIME && console.timeEnd("openLakes");

View file

@ -1,145 +0,0 @@
import {TIME} from "config/logging";
import {aleaPRNG} from "scripts/aleaPRNG";
// Mark features (ocean, lakes, islands) and calculate distance field
export function markFeatures() {
TIME && console.time("markFeatures");
Math.random = aleaPRNG(seed); // get the same result on heightmap edit in Erase mode
const cells = grid.cells;
const heights = grid.cells.h;
cells.f = new Uint16Array(cells.i.length); // cell feature number
cells.t = new Int8Array(cells.i.length); // cell type: 1 = land coast; -1 = water near coast
grid.features = [0];
for (let i = 1, queue = [0]; queue[0] !== -1; i++) {
cells.f[queue[0]] = i; // feature number
const land = heights[queue[0]] >= 20;
let border = false; // true if feature touches map border
while (queue.length) {
const q = queue.pop();
if (cells.b[q]) border = true;
cells.c[q].forEach(c => {
const cLand = heights[c] >= 20;
if (land === cLand && !cells.f[c]) {
cells.f[c] = i;
queue.push(c);
} else if (land && !cLand) {
cells.t[q] = 1;
cells.t[c] = -1;
}
});
}
const type = land ? "island" : border ? "ocean" : "lake";
grid.features.push({i, land, border, type});
queue[0] = cells.f.findIndex(f => !f); // find unmarked cell
}
TIME && console.timeEnd("markFeatures");
}
export function markupGridOcean() {
TIME && console.time("markupGridOcean");
markup(grid.cells, -2, -1, -10);
TIME && console.timeEnd("markupGridOcean");
}
// Calculate cell-distance to coast for every cell
export function markup(cells, start, increment, limit) {
for (let t = start, count = Infinity; count > 0 && t > limit; t += increment) {
count = 0;
const prevT = t - increment;
for (let i = 0; i < cells.i.length; i++) {
if (cells.t[i] !== prevT) continue;
for (const c of cells.c[i]) {
if (cells.t[c]) continue;
cells.t[c] = t;
count++;
}
}
}
}
// Re-mark features (ocean, lakes, islands)
export function reMarkFeatures() {
TIME && console.time("reMarkFeatures");
const {cells} = pack;
const features = [0];
cells.f = new Uint16Array(cells.i.length); // cell feature number
cells.t = new Int8Array(cells.i.length); // cell type: 1 = land along coast; -1 = water along coast;
cells.haven = cells.i.length < 65535 ? new Uint16Array(cells.i.length) : new Uint32Array(cells.i.length); // cell haven (opposite water cell);
cells.harbor = new Uint8Array(cells.i.length); // cell harbor (number of adjacent water cells);
const defineHaven = i => {
const water = cells.c[i].filter(c => cells.h[c] < 20);
const dist2 = water.map(c => (cells.p[i][0] - cells.p[c][0]) ** 2 + (cells.p[i][1] - cells.p[c][1]) ** 2);
const closest = water[dist2.indexOf(Math.min.apply(Math, dist2))];
cells.haven[i] = closest;
cells.harbor[i] = water.length;
};
if (!cells.i.length) return; // no cells -> there is nothing to do
for (let i = 1, queue = [0]; queue[0] !== -1; i++) {
const start = queue[0]; // first cell
cells.f[start] = i; // assign feature number
const land = cells.h[start] >= 20;
let border = false; // true if feature touches map border
let cellNumber = 1; // to count cells number in a feature
while (queue.length) {
const q = queue.pop();
if (cells.b[q]) border = true;
cells.c[q].forEach(function (e) {
const eLand = cells.h[e] >= 20;
if (land && !eLand) {
cells.t[q] = 1;
cells.t[e] = -1;
if (!cells.haven[q]) defineHaven(q);
} else if (land && eLand) {
if (!cells.t[e] && cells.t[q] === 1) cells.t[e] = 2;
else if (!cells.t[q] && cells.t[e] === 1) cells.t[q] = 2;
}
if (!cells.f[e] && land === eLand) {
queue.push(e);
cells.f[e] = i;
cellNumber++;
}
});
}
const type = land ? "island" : border ? "ocean" : "lake";
let group;
if (type === "ocean") group = defineOceanGroup(cellNumber);
else if (type === "island") group = defineIslandGroup(start, cellNumber);
features.push({i, land, border, type, cells: cellNumber, firstCell: start, group});
queue[0] = cells.f.findIndex(f => !f); // find unmarked cell
}
// markupPackLand
markup(pack.cells, 3, 1, 0);
function defineOceanGroup(number) {
if (number > grid.cells.i.length / 25) return "ocean";
if (number > grid.cells.i.length / 100) return "sea";
return "gulf";
}
function defineIslandGroup(cell, number) {
if (cell && features[cells.f[cell - 1]].type === "lake") return "lake_island";
if (number > grid.cells.i.length / 10) return "continent";
if (number > grid.cells.i.length / 1000) return "island";
return "isle";
}
pack.features = features;
TIME && console.timeEnd("reMarkFeatures");
}

186
src/modules/markup.ts Normal file
View file

@ -0,0 +1,186 @@
import {MIN_LAND_HEIGHT, DISTANCE_FIELD} from "config/generation";
import {TIME} from "config/logging";
import {INT8_MAX} from "constants";
// @ts-expect-error js module
import {aleaPRNG} from "scripts/aleaPRNG";
const {UNMARKED, LAND_COAST, WATER_COAST} = DISTANCE_FIELD;
// define features (grid.features: ocean, lakes, islands) and calculate distance field (cells.t)
export function markupGridFeatures(grid: IGraph & {cells: {h: UintArray}}) {
TIME && console.time("markupGridFeatures");
Math.random = aleaPRNG(seed); // get the same result on heightmap edit in Erase mode
if (!grid.cells || !grid.cells.h) {
throw new Error("markupGridFeatures: grid.cells.h is required");
}
const cells = grid.cells;
const heights = cells.h;
const n = cells.i.length;
const featureIds = new Uint16Array(n); // starts from 1
let distanceField = new Int8Array(n);
const features: TGridFeatures = [0];
const queue = [0];
for (let featureId = 1; queue[0] !== -1; featureId++) {
const firstCell = queue[0];
featureIds[firstCell] = featureId;
const land = heights[firstCell] >= MIN_LAND_HEIGHT;
let border = false; // set true if feature touches map edge
while (queue.length) {
const cellId = queue.pop()!;
if (cells.b[cellId]) border = true;
for (const neighborId of cells.c[cellId]) {
const isNeibLand = heights[neighborId] >= MIN_LAND_HEIGHT;
if (land === isNeibLand && featureIds[neighborId] === UNMARKED) {
featureIds[neighborId] = featureId;
queue.push(neighborId);
} else if (land && !isNeibLand) {
distanceField[cellId] = LAND_COAST;
distanceField[neighborId] = WATER_COAST;
}
}
}
const type = land ? "island" : border ? "ocean" : "lake";
features.push({i: featureId, land, border, type});
queue[0] = featureIds.findIndex(f => f === UNMARKED); // find unmarked cell
}
// markup deep ocean cells
distanceField = markup({graph: grid, distanceField, start: -2, increment: -1, limit: -10});
TIME && console.timeEnd("markupGridFeatures");
return {featureIds, distanceField, features};
}
// calculate distance to coast for every cell
function markup({
graph,
distanceField,
start,
increment,
limit
}: {
graph: IGraph;
distanceField: Int8Array;
start: number;
increment: number;
limit: number;
}) {
const cellsLength = graph.cells.i.length;
const neighbors = graph.cells.c;
for (let distance = start, marked = Infinity; marked > 0 && distance > limit; distance += increment) {
marked = 0;
const prevDistance = distance - increment;
for (let cellId = 0; cellId < cellsLength; cellId++) {
if (distanceField[cellId] !== prevDistance) continue;
for (const neighborId of neighbors[cellId]) {
if (distanceField[neighborId] !== UNMARKED) continue;
distanceField[neighborId] = distance;
marked++;
}
}
}
return distanceField;
}
// Re-mark features (ocean, lakes, islands)
export function reMarkFeatures() {
TIME && console.time("reMarkFeatures");
const {cells} = pack;
const features: TPackFeatures = [0];
const n = cells.i.length;
cells.f = new Uint16Array(n); // cell feature number
cells.t = new Int8Array(n); // cell type: 1 = land along coast; -1 = water along coast;
cells.haven = n < 65535 ? new Uint16Array(n) : new Uint32Array(n); // cell haven (opposite water cell);
cells.harbor = new Uint8Array(n); // cell harbor (number of adjacent water cells);
const defineHaven = (i: number) => {
const water = cells.c[i].filter(c => cells.h[c] < 20);
const dist2 = water.map(c => (cells.p[i][0] - cells.p[c][0]) ** 2 + (cells.p[i][1] - cells.p[c][1]) ** 2);
const closest = water[dist2.indexOf(Math.min.apply(Math, dist2))];
cells.haven[i] = closest;
cells.harbor[i] = water.length;
};
for (let i = 1, queue = [0]; queue[0] !== -1; i++) {
const start = queue[0]; // first cell
cells.f[start] = i; // assign feature number
const land = cells.h[start] >= 20;
let border = false; // true if feature touches map border
let cellNumber = 1; // to count cells number in a feature
while (queue.length) {
const firstCellId = queue.pop()!;
if (cells.b[firstCellId]) border = true;
cells.c[firstCellId].forEach(function (e) {
const eLand = cells.h[e] >= 20;
if (land && !eLand) {
cells.t[firstCellId] = 1;
cells.t[e] = -1;
if (!cells.haven[firstCellId]) defineHaven(firstCellId);
} else if (land && eLand) {
if (!cells.t[e] && cells.t[firstCellId] === 1) cells.t[e] = 2;
else if (!cells.t[firstCellId] && cells.t[e] === 1) cells.t[firstCellId] = 2;
}
if (!cells.f[e] && land === eLand) {
queue.push(e);
cells.f[e] = i;
cellNumber++;
}
});
}
if (land) {
const group = defineIslandGroup(start, cellNumber);
const feature: IPackFeatureIsland = {i, type: "island", group, land, border, cells: cellNumber, firstCell: start};
features.push(feature);
} else if (border) {
const group = defineOceanGroup(cellNumber);
const feature: IPackFeatureOcean = {i, type: "ocean", group, land, border, cells: cellNumber, firstCell: start};
features.push(feature);
} else {
const group = "freshwater"; // temp, to be defined later
const name = ""; // temp, to be defined later
const cells = cellNumber;
const feature: IPackFeatureLake = {i, type: "lake", group, name, land, border, cells, firstCell: start};
features.push(feature);
}
queue[0] = cells.f.findIndex(f => f === UNMARKED); // find unmarked cell
}
// markupPackLand
markup({graph: pack, distanceField: pack.cells.t, start: 3, increment: 1, limit: INT8_MAX});
function defineOceanGroup(number: number) {
if (number > grid.cells.i.length / 25) return "ocean";
if (number > grid.cells.i.length / 100) return "sea";
return "gulf";
}
function defineIslandGroup(cell, number) {
if (cell && features[cells.f[cell - 1]].type === "lake") return "lake_island";
if (number > grid.cells.i.length / 10) return "continent";
if (number > grid.cells.i.length / 1000) return "island";
return "isle";
}
pack.features = features;
TIME && console.timeEnd("reMarkFeatures");
}

View file

@ -6,6 +6,7 @@ import {getMiddlePoint} from "utils/lineUtils";
import {rn} from "utils/numberUtils";
import {aleaPRNG} from "scripts/aleaPRNG";
import {renderLayer} from "layers";
import {markupGridFeatures} from "modules/markup";
window.Submap = (function () {
const isWater = (pack, id) => pack.cells.h[id] < 20;
@ -113,13 +114,12 @@ window.Submap = (function () {
}
stage("Detect features, ocean and generating lakes.");
markFeatures();
markupGridOcean();
markupGridFeatures();
// Warning: addLakesInDeepDepressions can be very slow!
if (options.addLakesInDepressions) {
Lakes.addLakesInDeepDepressions();
Lakes.openNearSeaLakes();
Lakes.addLakesInDeepDepressions(grid);
Lakes.openNearSeaLakes(grid);
}
OceanLayers();