mirror of
https://github.com/Azgaar/Fantasy-Map-Generator.git
synced 2025-12-18 02:01:22 +01:00
refactor: generation script
This commit is contained in:
parent
c0f6ce00ef
commit
87d8c1024d
31 changed files with 364 additions and 324 deletions
|
|
@ -85,7 +85,7 @@ window.Biomes = (function () {
|
|||
}
|
||||
|
||||
// assign biome id for each cell
|
||||
function define() {
|
||||
function define(pack, grid) {
|
||||
TIME && console.time("defineBiomes");
|
||||
const {cells} = pack;
|
||||
const {temp, prec} = grid.cells;
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ import {round} from "utils/stringUtils";
|
|||
import {Ruler} from "modules/measurers";
|
||||
|
||||
// Detect and draw the coastline
|
||||
export function drawCoastline() {
|
||||
export function drawCoastline(pack) {
|
||||
TIME && console.time("drawCoastline");
|
||||
|
||||
const {cells, vertices, features} = pack;
|
||||
|
|
|
|||
|
|
@ -208,8 +208,8 @@ export function resolveVersionConflicts(version) {
|
|||
coastline.selectAll("path").remove();
|
||||
lakes.selectAll("path").remove();
|
||||
|
||||
reMarkFeatures();
|
||||
drawCoastline();
|
||||
reMarkFeatures(pack, newGrid);
|
||||
drawCoastline(pack);
|
||||
}
|
||||
|
||||
if (version < 1.11) {
|
||||
|
|
|
|||
|
|
@ -1,18 +1,19 @@
|
|||
import * as d3 from "d3";
|
||||
|
||||
import {INFO} from "config/logging";
|
||||
import {closeDialogs} from "dialogs/utils";
|
||||
import {updatePresetInput} from "layers";
|
||||
import {reMarkFeatures} from "modules/markup";
|
||||
import {setDefaultEventHandlers} from "scripts/events";
|
||||
import {regenerateMap} from "scripts/generation/generation";
|
||||
import {calculateVoronoi} from "scripts/generation/graph";
|
||||
import {ldb} from "scripts/indexedDB";
|
||||
import {tip} from "scripts/tooltips";
|
||||
import {last} from "utils/arrayUtils";
|
||||
import {parseError} from "utils/errorUtils";
|
||||
import {calculateVoronoi, findCell} from "utils/graphUtils";
|
||||
import {findCell} from "utils/graphUtils";
|
||||
import {link} from "utils/linkUtils";
|
||||
import {minmax, rn} from "utils/numberUtils";
|
||||
import {regenerateMap} from "scripts/generation/generation";
|
||||
import {reMarkFeatures} from "modules/markup";
|
||||
import {closeDialogs} from "dialogs/utils";
|
||||
|
||||
// add drag to upload logic, pull request from @evyatron
|
||||
export function addDragToUpload() {
|
||||
|
|
|
|||
|
|
@ -2,13 +2,14 @@ import * as d3 from "d3";
|
|||
|
||||
import {TIME} from "config/logging";
|
||||
import {rn} from "utils/numberUtils";
|
||||
// @ts-expect-error js module
|
||||
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";
|
||||
import {byId} from "utils/shorthands";
|
||||
|
||||
window.Lakes = (function () {
|
||||
const setClimateData = function (h) {
|
||||
const setClimateData = function (h: Uint8Array, pack: IPack, grid: IGrid) {
|
||||
const cells = pack.cells;
|
||||
const lakeOutCells = new Uint16Array(cells.i.length);
|
||||
|
||||
|
|
@ -39,37 +40,39 @@ window.Lakes = (function () {
|
|||
};
|
||||
|
||||
// get array of land cells aroound lake
|
||||
const getShoreline = function (lake) {
|
||||
const getShoreline = function (lake: IPackFeatureLake, pack: IPack) {
|
||||
const uniqueCells = new Set();
|
||||
lake.vertices.forEach(v => pack.vertices.c[v].forEach(c => pack.cells.h[c] >= 20 && uniqueCells.add(c)));
|
||||
lake.vertices.forEach(v =>
|
||||
pack.vertices.c[v].forEach(c => pack.cells.h[c] >= MIN_LAND_HEIGHT && uniqueCells.add(c))
|
||||
);
|
||||
lake.shoreline = [...uniqueCells];
|
||||
};
|
||||
|
||||
const prepareLakeData = h => {
|
||||
const prepareLakeData = (h: Uint8Array, pack: IPack) => {
|
||||
const cells = pack.cells;
|
||||
const ELEVATION_LIMIT = +document.getElementById("lakeElevationLimitOutput").value;
|
||||
const ELEVATION_LIMIT = getInputNumber("lakeElevationLimitOutput");
|
||||
|
||||
pack.features.forEach(f => {
|
||||
if (f.type !== "lake") return;
|
||||
delete f.flux;
|
||||
delete f.inlets;
|
||||
delete f.outlet;
|
||||
delete f.height;
|
||||
delete f.closed;
|
||||
!f.shoreline && Lakes.getShoreline(f);
|
||||
pack.features.forEach(feature => {
|
||||
if (!feature || feature.type !== "lake") return;
|
||||
delete feature.flux;
|
||||
delete feature.inlets;
|
||||
delete feature.outlet;
|
||||
delete feature.height;
|
||||
delete feature.closed;
|
||||
!feature.shoreline && getShoreline(feature, pack);
|
||||
|
||||
// lake surface height is as lowest land cells around
|
||||
const min = f.shoreline.sort((a, b) => h[a] - h[b])[0];
|
||||
f.height = h[min] - 0.1;
|
||||
const min = feature.shoreline.sort((a, b) => h[a] - h[b])[0];
|
||||
feature.height = h[min] - 0.1;
|
||||
|
||||
// check if lake can be open (not in deep depression)
|
||||
if (ELEVATION_LIMIT === 80) {
|
||||
f.closed = false;
|
||||
feature.closed = false;
|
||||
return;
|
||||
}
|
||||
|
||||
let deep = true;
|
||||
const threshold = f.height + ELEVATION_LIMIT;
|
||||
const threshold = feature.height + ELEVATION_LIMIT;
|
||||
const queue = [min];
|
||||
const checked = [];
|
||||
checked[min] = true;
|
||||
|
|
@ -84,7 +87,7 @@ window.Lakes = (function () {
|
|||
|
||||
if (h[n] < 20) {
|
||||
const nFeature = pack.features[cells.f[n]];
|
||||
if (nFeature.type === "ocean" || f.height > nFeature.height) {
|
||||
if ((nFeature && nFeature.type === "ocean") || feature.height > nFeature.height) {
|
||||
deep = false;
|
||||
break;
|
||||
}
|
||||
|
|
@ -95,11 +98,11 @@ window.Lakes = (function () {
|
|||
}
|
||||
}
|
||||
|
||||
f.closed = deep;
|
||||
feature.closed = deep;
|
||||
});
|
||||
};
|
||||
|
||||
const cleanupLakeData = function () {
|
||||
const cleanupLakeData = function (pack: IPack) {
|
||||
for (const feature of pack.features) {
|
||||
if (feature.type !== "lake") continue;
|
||||
delete feature.river;
|
||||
|
|
@ -117,14 +120,15 @@ window.Lakes = (function () {
|
|||
}
|
||||
};
|
||||
|
||||
const defineGroup = function () {
|
||||
const defineGroup = function (pack: IPack) {
|
||||
for (const feature of pack.features) {
|
||||
if (feature.type !== "lake") continue;
|
||||
const lakeEl = lakes.select(`[data-f="${feature.i}"]`).node();
|
||||
if (!lakeEl) continue;
|
||||
if (feature && feature.type === "lake") {
|
||||
const lakeEl = lakes.select(`[data-f="${feature.i}"]`).node();
|
||||
if (!lakeEl) continue;
|
||||
|
||||
feature.group = getGroup(feature);
|
||||
document.getElementById(feature.group).appendChild(lakeEl);
|
||||
feature.group = getGroup(feature);
|
||||
byId(feature.group)?.appendChild(lakeEl);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ import {rw, each} from "utils/probabilityUtils";
|
|||
import {aleaPRNG} from "scripts/aleaPRNG";
|
||||
|
||||
window.Rivers = (function () {
|
||||
const generate = function (allowErosion = true) {
|
||||
const generate = function (pack, grid, allowErosion = true) {
|
||||
TIME && console.time("generateRivers");
|
||||
Math.random = aleaPRNG(seed);
|
||||
const {cells, features} = pack;
|
||||
|
|
@ -25,14 +25,14 @@ window.Rivers = (function () {
|
|||
cells.conf = new Uint8Array(cells.i.length); // confluences array
|
||||
let riverNext = 1; // first river id is 1
|
||||
|
||||
const h = alterHeights();
|
||||
Lakes.prepareLakeData(h);
|
||||
resolveDepressions(h);
|
||||
const h = alterHeights(pack.cells);
|
||||
Lakes.prepareLakeData(h, pack);
|
||||
resolveDepressions(pack, h);
|
||||
drainWater();
|
||||
defineRivers();
|
||||
|
||||
calculateConfluenceFlux();
|
||||
Lakes.cleanupLakeData();
|
||||
Lakes.cleanupLakeData(pack);
|
||||
|
||||
if (allowErosion) {
|
||||
cells.h = Uint8Array.from(h); // apply gradient
|
||||
|
|
@ -42,14 +42,12 @@ window.Rivers = (function () {
|
|||
TIME && console.timeEnd("generateRivers");
|
||||
|
||||
function drainWater() {
|
||||
//const MIN_FLUX_TO_FORM_RIVER = 10 * distanceScale;
|
||||
const MIN_FLUX_TO_FORM_RIVER = 30;
|
||||
const cellsNumberModifier = (pointsInput.dataset.cells / 10000) ** 0.25;
|
||||
|
||||
const prec = grid.cells.prec;
|
||||
const area = pack.cells.area;
|
||||
const land = cells.i.filter(i => h[i] >= 20).sort((a, b) => h[b] - h[a]);
|
||||
const lakeOutCells = Lakes.setClimateData(h);
|
||||
const lakeOutCells = Lakes.setClimateData(h, pack, grid);
|
||||
|
||||
land.forEach(function (i) {
|
||||
cells.fl[i] += prec[cells.g[i]] / cellsNumberModifier; // add flux from precipitation
|
||||
|
|
@ -195,7 +193,7 @@ window.Rivers = (function () {
|
|||
const parent = riverParents[key] || 0;
|
||||
|
||||
const widthFactor = !parent || parent === riverId ? mainStemWidthFactor : defaultWidthFactor;
|
||||
const meanderedPoints = addMeandering(riverCells);
|
||||
const meanderedPoints = addMeandering(pack, riverCells);
|
||||
const discharge = cells.fl[mouth]; // m3 in second
|
||||
const length = getApproximateLength(meanderedPoints);
|
||||
const width = getWidth(getOffset(discharge, meanderedPoints.length, widthFactor, 0));
|
||||
|
|
@ -245,8 +243,7 @@ window.Rivers = (function () {
|
|||
};
|
||||
|
||||
// add distance to water value to land cells to make map less depressed
|
||||
const alterHeights = () => {
|
||||
const {h, c, t} = pack.cells;
|
||||
const alterHeights = ({h, c, t}) => {
|
||||
return Array.from(h).map((h, i) => {
|
||||
if (h < 20 || t[i] < 1) return h;
|
||||
return h + t[i] / 100 + d3.mean(c[i].map(c => t[c])) / 10000;
|
||||
|
|
@ -254,7 +251,7 @@ window.Rivers = (function () {
|
|||
};
|
||||
|
||||
// depression filling algorithm (for a correct water flux modeling)
|
||||
const resolveDepressions = function (h) {
|
||||
const resolveDepressions = function (pack, h) {
|
||||
const {cells, features} = pack;
|
||||
const maxIterations = +document.getElementById("resolveDepressionsStepsOutput").value;
|
||||
const checkLakeMaxIteration = maxIterations * 0.85;
|
||||
|
|
@ -272,7 +269,7 @@ window.Rivers = (function () {
|
|||
for (let iteration = 0; depressions && iteration < maxIterations; iteration++) {
|
||||
if (progress.length > 5 && d3.sum(progress) > 0) {
|
||||
// bad progress, abort and set heights back
|
||||
h = alterHeights();
|
||||
h = alterHeights(pack.cells);
|
||||
depressions = progress[0];
|
||||
break;
|
||||
}
|
||||
|
|
@ -313,11 +310,11 @@ window.Rivers = (function () {
|
|||
};
|
||||
|
||||
// add points at 1/3 and 2/3 of a line between adjacents river cells
|
||||
const addMeandering = function (riverCells, riverPoints = null, meandering = 0.5) {
|
||||
const addMeandering = (pack, riverCells, riverPoints = null, meandering = 0.5) => {
|
||||
const {fl, conf, h} = pack.cells;
|
||||
const meandered = [];
|
||||
const lastStep = riverCells.length - 1;
|
||||
const points = getRiverPoints(riverCells, riverPoints);
|
||||
const points = getRiverPoints(pack, riverCells, riverPoints);
|
||||
let step = h[riverCells[0]] < 20 ? 1 : 10;
|
||||
|
||||
let fluxPrev = 0;
|
||||
|
|
@ -373,17 +370,17 @@ window.Rivers = (function () {
|
|||
return meandered;
|
||||
};
|
||||
|
||||
const getRiverPoints = (riverCells, riverPoints) => {
|
||||
const getRiverPoints = (pack, riverCells, riverPoints) => {
|
||||
if (riverPoints) return riverPoints;
|
||||
|
||||
const {p} = pack.cells;
|
||||
return riverCells.map((cell, i) => {
|
||||
if (cell === -1) return getBorderPoint(riverCells[i - 1]);
|
||||
if (cell === -1) return getBorderPoint(pack, riverCells[i - 1]);
|
||||
return p[cell];
|
||||
});
|
||||
};
|
||||
|
||||
const getBorderPoint = i => {
|
||||
const getBorderPoint = (pack, i) => {
|
||||
const [x, y] = pack.cells.p[i];
|
||||
const min = Math.min(y, graphHeight - y, x, graphWidth - x);
|
||||
if (min === y) return [x, 0];
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import {rn} from "utils/numberUtils";
|
|||
import {aleaPRNG} from "scripts/aleaPRNG";
|
||||
import {renderLayer} from "layers";
|
||||
import {markupGridFeatures} from "modules/markup";
|
||||
import {generateGrid} from "scripts/generation/graph";
|
||||
|
||||
window.Submap = (function () {
|
||||
const isWater = (pack, id) => pack.cells.h[id] < 20;
|
||||
|
|
@ -130,8 +131,8 @@ window.Submap = (function () {
|
|||
|
||||
// remove misclassified cells
|
||||
stage("Define coastline.");
|
||||
reMarkFeatures();
|
||||
drawCoastline();
|
||||
reMarkFeatures(pack, newGrid);
|
||||
drawCoastline(pack);
|
||||
|
||||
/****************************************************/
|
||||
/* Packed Graph */
|
||||
|
|
@ -210,8 +211,8 @@ window.Submap = (function () {
|
|||
}
|
||||
|
||||
stage("Regenerating river network.");
|
||||
Rivers.generate();
|
||||
renderLayer("rivers");
|
||||
Rivers.generate(pack, grid);
|
||||
renderLayer("rivers", pack);
|
||||
Lakes.defineGroup();
|
||||
|
||||
// biome calculation based on (resampled) grid.cells.temp and prec
|
||||
|
|
|
|||
|
|
@ -168,6 +168,8 @@ optionsContent.on("click", function (event) {
|
|||
else if (id === "translateExtent") toggleTranslateExtent(event.target);
|
||||
else if (id === "speakerTest") testSpeaker();
|
||||
else if (id === "themeColorRestore") restoreDefaultThemeColor();
|
||||
else if (id === "configureWorld") openDialog("worldConfigurator");
|
||||
else if (id === "optionsReset") restoreDefaultOptions();
|
||||
});
|
||||
|
||||
function mapSizeInputChange() {
|
||||
|
|
@ -1025,6 +1027,7 @@ export function toggle3dOptions() {
|
|||
isLoaded = true;
|
||||
|
||||
byId("options3dUpdate").on("click", ThreeD.update);
|
||||
byId("options3dConfigureWorld").on("click", () => openDialog("worldConfigurator"));
|
||||
byId("options3dSave").on("click", ThreeD.saveScreenshot);
|
||||
byId("options3dOBJSave").on("click", ThreeD.saveOBJ);
|
||||
|
||||
|
|
|
|||
|
|
@ -102,7 +102,7 @@ export function createRiver() {
|
|||
const defaultWidthFactor = rn(1 / (pointsInput.dataset.cells / 10000) ** 0.25, 2);
|
||||
const widthFactor = 1.2 * defaultWidthFactor;
|
||||
|
||||
const meanderedPoints = addMeandering(riverCells);
|
||||
const meanderedPoints = addMeandering(pack, riverCells);
|
||||
|
||||
const discharge = cells.fl[mouth]; // m3 in second
|
||||
const length = getApproximateLength(meanderedPoints);
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ export function editRiver(id) {
|
|||
|
||||
const river = getRiver();
|
||||
const {cells, points} = river;
|
||||
const riverPoints = Rivers.getRiverPoints(cells, points);
|
||||
const riverPoints = Rivers.getRiverPoints(pack, cells, points);
|
||||
drawControlPoints(riverPoints);
|
||||
drawCells(cells);
|
||||
|
||||
|
|
@ -98,7 +98,7 @@ export function editRiver(id) {
|
|||
function updateRiverWidth(river) {
|
||||
const {addMeandering, getWidth, getOffset} = Rivers;
|
||||
const {cells, discharge, widthFactor, sourceWidth} = river;
|
||||
const meanderedPoints = addMeandering(cells);
|
||||
const meanderedPoints = addMeandering(pack, cells);
|
||||
river.width = getWidth(getOffset(discharge, meanderedPoints.length, widthFactor, sourceWidth));
|
||||
|
||||
const width = `${rn(river.width * distanceScaleInput.value, 3)} ${distanceUnitInput.value}`;
|
||||
|
|
@ -169,7 +169,7 @@ export function editRiver(id) {
|
|||
river.cells = river.points.map(([x, y]) => findCell(x, y));
|
||||
|
||||
const {widthFactor, sourceWidth} = river;
|
||||
const meanderedPoints = Rivers.addMeandering(river.cells, river.points);
|
||||
const meanderedPoints = Rivers.addMeandering(pack, river.cells, river.points);
|
||||
|
||||
const path = Rivers.getRiverPath(meanderedPoints, widthFactor, sourceWidth);
|
||||
elSelected.attr("d", path);
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import {debounce} from "utils/functionUtils";
|
|||
import {restoreLayers} from "layers";
|
||||
import {undraw} from "scripts/generation/generation";
|
||||
import {closeDialogs} from "dialogs/utils";
|
||||
import {openDialog} from "dialogs";
|
||||
|
||||
window.UISubmap = (function () {
|
||||
byId("submapPointsInput").addEventListener("input", function () {
|
||||
|
|
@ -312,7 +313,7 @@ window.UISubmap = (function () {
|
|||
|
||||
restoreLayers();
|
||||
if (ThreeD.options.isOn) ThreeD.redraw();
|
||||
if ($("#worldConfigurator").is(":visible")) editWorld();
|
||||
if ($("#worldConfigurator").is(":visible")) openDialog("worldConfigurator");
|
||||
}
|
||||
|
||||
function changeStyles(scale) {
|
||||
|
|
|
|||
|
|
@ -131,11 +131,11 @@ async function openEmblemEditor() {
|
|||
}
|
||||
|
||||
function regenerateRivers() {
|
||||
Rivers.generate();
|
||||
Rivers.generate(pack, grid);
|
||||
Lakes.defineGroup();
|
||||
Rivers.specify();
|
||||
if (!layerIsOn("toggleRivers")) toggleLayer("toggleRivers");
|
||||
else renderLayer("rivers");
|
||||
else renderLayer("rivers", pack);
|
||||
}
|
||||
|
||||
function recalculatePopulation() {
|
||||
|
|
@ -588,8 +588,8 @@ function addRiverOnClick() {
|
|||
const initialFlux = grid.cells.prec[cells.g[i]];
|
||||
cells.fl[i] = initialFlux;
|
||||
|
||||
const h = alterHeights();
|
||||
resolveDepressions(h);
|
||||
const h = alterHeights(pacl.cells);
|
||||
resolveDepressions(pack, h);
|
||||
|
||||
while (i) {
|
||||
cells.r[i] = riverId;
|
||||
|
|
@ -663,7 +663,7 @@ function addRiverOnClick() {
|
|||
const defaultWidthFactor = rn(1 / (pointsInput.dataset.cells / 10000) ** 0.25, 2);
|
||||
const widthFactor =
|
||||
river?.widthFactor || (!parent || parent === riverId ? defaultWidthFactor * 1.2 : defaultWidthFactor);
|
||||
const meanderedPoints = addMeandering(riverCells);
|
||||
const meanderedPoints = addMeandering(pack, riverCells);
|
||||
|
||||
const discharge = cells.fl[mouth]; // m3 in second
|
||||
const length = getApproximateLength(meanderedPoints);
|
||||
|
|
@ -704,7 +704,7 @@ function addRiverOnClick() {
|
|||
riversG.append("path").attr("id", id).attr("d", path);
|
||||
|
||||
if (d3.event.shiftKey === false) {
|
||||
Lakes.cleanupLakeData();
|
||||
Lakes.cleanupLakeData(pack);
|
||||
unpressClickToAddButton();
|
||||
document.getElementById("addNewRiver").classList.remove("pressed");
|
||||
if (addNewRiver.offsetParent) riversOverviewRefresh.click();
|
||||
|
|
|
|||
|
|
@ -7,8 +7,7 @@ import {renderLayer} from "layers";
|
|||
|
||||
let isLoaded = false;
|
||||
|
||||
export function editWorld() {
|
||||
if (customization) return;
|
||||
export function open() {
|
||||
$("#worldConfigurator").dialog({
|
||||
title: "Configure World",
|
||||
resizable: false,
|
||||
|
|
@ -63,7 +62,7 @@ export function editWorld() {
|
|||
calculateTemperatures(grid);
|
||||
generatePrecipitation(grid);
|
||||
const heights = new Uint8Array(pack.cells.h);
|
||||
Rivers.generate();
|
||||
Rivers.generate(pack, grid);
|
||||
Lakes.defineGroup();
|
||||
Rivers.specify();
|
||||
pack.cells.h = new Float32Array(heights);
|
||||
|
|
@ -73,7 +72,7 @@ export function editWorld() {
|
|||
if (layerIsOn("togglePrec")) renderLayer("precipitation");
|
||||
if (layerIsOn("toggleBiomes")) renderLayer("biomes");
|
||||
if (layerIsOn("toggleCoordinates")) drawCoordinates();
|
||||
if (layerIsOn("toggleRivers")) renderLayer("rivers");
|
||||
if (layerIsOn("toggleRivers")) renderLayer("rivers", pack);
|
||||
if (document.getElementById("canvas3d")) setTimeout(ThreeD.update(), 500);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue