mirror of
https://github.com/Azgaar/Fantasy-Map-Generator.git
synced 2025-12-18 10:01:23 +01:00
refactor: generation script
This commit is contained in:
parent
00d8d28d76
commit
c0f6ce00ef
11 changed files with 112 additions and 97 deletions
293
src/scripts/generation/generation.ts
Normal file
293
src/scripts/generation/generation.ts
Normal file
|
|
@ -0,0 +1,293 @@
|
|||
import * as d3 from "d3";
|
||||
|
||||
import {ERROR, INFO, WARN} from "config/logging";
|
||||
import {closeDialogs} from "dialogs/utils";
|
||||
import {initLayers, renderLayer, restoreLayers} from "layers";
|
||||
// @ts-expect-error js module
|
||||
import {drawCoastline} from "modules/coastline";
|
||||
import {reMarkFeatures} from "modules/markup";
|
||||
// @ts-expect-error js module
|
||||
import {drawScaleBar, Rulers} from "modules/measurers";
|
||||
// @ts-expect-error js module
|
||||
import {unfog} from "modules/ui/editors";
|
||||
// @ts-expect-error js module
|
||||
import {applyMapSize, randomizeOptions} from "modules/ui/options";
|
||||
// @ts-expect-error js module
|
||||
import {applyStyleOnLoad} from "modules/ui/stylePresets";
|
||||
// @ts-expect-error js module
|
||||
import {addZones} from "modules/zones";
|
||||
// @ts-expect-error js module
|
||||
import {aleaPRNG} from "scripts/aleaPRNG";
|
||||
import {hideLoading, showLoading} from "scripts/loading";
|
||||
import {clearMainTip, tip} from "scripts/tooltips";
|
||||
import {parseError} from "utils/errorUtils";
|
||||
import {debounce} from "utils/functionUtils";
|
||||
import {rn} from "utils/numberUtils";
|
||||
import {generateSeed} from "utils/probabilityUtils";
|
||||
import {byId} from "utils/shorthands";
|
||||
import {rankCells} from "../rankCells";
|
||||
import {showStatistics} from "../statistics";
|
||||
import {createGrid} from "./grid";
|
||||
import {reGraph} from "./reGraph";
|
||||
|
||||
const {Zoom, Lakes, OceanLayers, Rivers, Biomes, Cultures, BurgsAndStates, Religions, Military, Markers, Names} =
|
||||
window;
|
||||
|
||||
async function generate(options?: {seed: string; graph: IGrid}) {
|
||||
try {
|
||||
const timeStart = performance.now();
|
||||
const {seed: precreatedSeed, graph: precreatedGraph} = options || {};
|
||||
|
||||
Zoom?.invoke();
|
||||
setSeed(precreatedSeed);
|
||||
|
||||
INFO && console.group("Generated Map " + seed);
|
||||
|
||||
applyMapSize();
|
||||
randomizeOptions();
|
||||
|
||||
const newGrid = await createGrid(grid, precreatedGraph);
|
||||
|
||||
const pack = reGraph(newGrid);
|
||||
reMarkFeatures(pack, newGrid);
|
||||
drawCoastline();
|
||||
|
||||
Rivers.generate();
|
||||
renderLayer("rivers");
|
||||
Lakes.defineGroup();
|
||||
Biomes.define();
|
||||
|
||||
rankCells();
|
||||
Cultures.generate();
|
||||
Cultures.expand();
|
||||
BurgsAndStates.generate();
|
||||
Religions.generate();
|
||||
BurgsAndStates.defineStateForms();
|
||||
BurgsAndStates.generateProvinces();
|
||||
BurgsAndStates.defineBurgFeatures();
|
||||
|
||||
renderLayer("states");
|
||||
renderLayer("borders");
|
||||
BurgsAndStates.drawStateLabels();
|
||||
|
||||
Rivers.specify();
|
||||
Lakes.generateName();
|
||||
|
||||
Military.generate();
|
||||
Markers.generate();
|
||||
addZones();
|
||||
|
||||
OceanLayers(newGrid);
|
||||
|
||||
drawScaleBar(window.scale);
|
||||
Names.getMapName();
|
||||
|
||||
WARN && console.warn(`TOTAL: ${rn((performance.now() - timeStart) / 1000, 2)}s`);
|
||||
showStatistics();
|
||||
INFO && console.groupEnd();
|
||||
} catch (error) {
|
||||
showGenerationError(error as Error);
|
||||
}
|
||||
}
|
||||
|
||||
function showGenerationError(error: Error) {
|
||||
clearMainTip();
|
||||
ERROR && console.error(error);
|
||||
const message = `An error has occurred on map generation. Please retry. <br />If error is critical, clear the stored data and try again.
|
||||
<p id="errorBox">${parseError(error)}</p>`;
|
||||
byId("alertMessage")!.innerHTML = message;
|
||||
|
||||
$("#alert").dialog({
|
||||
resizable: false,
|
||||
title: "Generation error",
|
||||
width: "32em",
|
||||
buttons: {
|
||||
"Clear data": function () {
|
||||
localStorage.clear();
|
||||
localStorage.setItem("version", APP_VERSION);
|
||||
},
|
||||
Regenerate: function () {
|
||||
regenerateMap("generation error");
|
||||
$(this).dialog("close");
|
||||
},
|
||||
Ignore: function () {
|
||||
$(this).dialog("close");
|
||||
}
|
||||
},
|
||||
position: {my: "center", at: "center", of: "svg"}
|
||||
});
|
||||
}
|
||||
|
||||
export async function generateMapOnLoad() {
|
||||
await applyStyleOnLoad(); // apply previously selected default or custom style
|
||||
await generate(); // generate map
|
||||
focusOn(); // based on searchParams focus on point, cell or burg from MFCG
|
||||
initLayers(); // apply saved layers data
|
||||
}
|
||||
|
||||
// clear the map
|
||||
export function undraw() {
|
||||
viewbox.selectAll("path, circle, polygon, line, text, use, #zones > g, #armies > g, #ruler > g").remove();
|
||||
|
||||
byId("deftemp")
|
||||
?.querySelectorAll("path, clipPath, svg")
|
||||
.forEach(el => el.remove());
|
||||
|
||||
// remove auto-generated emblems
|
||||
if (byId("coas")) byId("coas")!.innerHTML = "";
|
||||
|
||||
notes = [];
|
||||
rulers = new Rulers();
|
||||
|
||||
unfog();
|
||||
}
|
||||
|
||||
export const regenerateMap = debounce(async function (options) {
|
||||
WARN && console.warn("Generate new random map");
|
||||
|
||||
const cellsDesired = +byId("pointsInput").dataset.cells;
|
||||
const shouldShowLoading = cellsDesired > 10000;
|
||||
shouldShowLoading && showLoading();
|
||||
|
||||
closeDialogs("#worldConfigurator, #options3d");
|
||||
customization = 0;
|
||||
Zoom.reset(1000);
|
||||
undraw();
|
||||
await generate(options);
|
||||
restoreLayers();
|
||||
if (ThreeD.options.isOn) ThreeD.redraw();
|
||||
if ($("#worldConfigurator").is(":visible")) editWorld();
|
||||
|
||||
shouldShowLoading && hideLoading();
|
||||
clearMainTip();
|
||||
}, 250);
|
||||
|
||||
// focus on coordinates, cell or burg provided in searchParams
|
||||
function focusOn() {
|
||||
const url = new URL(window.location.href);
|
||||
const params = url.searchParams;
|
||||
|
||||
const fromMGCG = params.get("from") === "MFCG" && document.referrer;
|
||||
if (fromMGCG) {
|
||||
if (params.get("seed").length === 13) {
|
||||
// show back burg from MFCG
|
||||
const burgSeed = params.get("seed").slice(-4);
|
||||
params.set("burg", burgSeed);
|
||||
} else {
|
||||
// select burg for MFCG
|
||||
findBurgForMFCG(params);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
const scaleParam = params.get("scale");
|
||||
const cellParam = params.get("cell");
|
||||
const burgParam = params.get("burg");
|
||||
|
||||
if (scaleParam || cellParam || burgParam) {
|
||||
const scale = +scaleParam || 8;
|
||||
|
||||
if (cellParam) {
|
||||
const cell = +params.get("cell");
|
||||
const [x, y] = pack.cells.p[cell];
|
||||
Zoom.to(x, y, scale, 1600);
|
||||
return;
|
||||
}
|
||||
|
||||
if (burgParam) {
|
||||
const burg = isNaN(+burgParam) ? pack.burgs.find(burg => burg.name === burgParam) : pack.burgs[+burgParam];
|
||||
if (!burg) return;
|
||||
|
||||
const {x, y} = burg;
|
||||
Zoom.to(x, y, scale, 1600);
|
||||
return;
|
||||
}
|
||||
|
||||
const x = +params.get("x") || graphWidth / 2;
|
||||
const y = +params.get("y") || graphHeight / 2;
|
||||
Zoom.to(x, y, scale, 1600);
|
||||
}
|
||||
}
|
||||
|
||||
// find burg for MFCG and focus on it
|
||||
function findBurgForMFCG(params) {
|
||||
const {cells, burgs} = pack;
|
||||
|
||||
if (pack.burgs.length < 2) {
|
||||
ERROR && console.error("Cannot select a burg for MFCG");
|
||||
return;
|
||||
}
|
||||
|
||||
// used for selection
|
||||
const size = +params.get("size");
|
||||
const coast = +params.get("coast");
|
||||
const port = +params.get("port");
|
||||
const river = +params.get("river");
|
||||
|
||||
let selection = defineSelection(coast, port, river);
|
||||
if (!selection.length) selection = defineSelection(coast, !port, !river);
|
||||
if (!selection.length) selection = defineSelection(!coast, 0, !river);
|
||||
if (!selection.length) selection = [burgs[1]]; // select first if nothing is found
|
||||
|
||||
function defineSelection(coast, port, river) {
|
||||
if (port && river) return burgs.filter(b => b.port && cells.r[b.cell]);
|
||||
if (!port && coast && river) return burgs.filter(b => !b.port && cells.t[b.cell] === 1 && cells.r[b.cell]);
|
||||
if (!coast && !river) return burgs.filter(b => cells.t[b.cell] !== 1 && !cells.r[b.cell]);
|
||||
if (!coast && river) return burgs.filter(b => cells.t[b.cell] !== 1 && cells.r[b.cell]);
|
||||
if (coast && river) return burgs.filter(b => cells.t[b.cell] === 1 && cells.r[b.cell]);
|
||||
return [];
|
||||
}
|
||||
|
||||
// select a burg with closest population from selection
|
||||
const selected = d3.scan(selection, (a, b) => Math.abs(a.population - size) - Math.abs(b.population - size));
|
||||
const burgId = selection[selected].i;
|
||||
if (!burgId) {
|
||||
ERROR && console.error("Cannot select a burg for MFCG");
|
||||
return;
|
||||
}
|
||||
|
||||
const b = burgs[burgId];
|
||||
const referrer = new URL(document.referrer);
|
||||
for (let p of referrer.searchParams) {
|
||||
if (p[0] === "name") b.name = p[1];
|
||||
else if (p[0] === "size") b.population = +p[1];
|
||||
else if (p[0] === "seed") b.MFCG = +p[1];
|
||||
else if (p[0] === "shantytown") b.shanty = +p[1];
|
||||
else b[p[0]] = +p[1]; // other parameters
|
||||
}
|
||||
if (params.get("name") && params.get("name") != "null") b.name = params.get("name");
|
||||
|
||||
const label = burgLabels.select("[data-id='" + burgId + "']");
|
||||
if (label.size()) {
|
||||
label
|
||||
.text(b.name)
|
||||
.classed("drag", true)
|
||||
.on("mouseover", function () {
|
||||
d3.select(this).classed("drag", false);
|
||||
label.on("mouseover", null);
|
||||
});
|
||||
}
|
||||
|
||||
Zoom.to(b.x, b.y, 8, 1600);
|
||||
Zoom.invoke();
|
||||
tip("Here stands the glorious city of " + b.name, true, "success", 15000);
|
||||
}
|
||||
|
||||
// set map seed (string!)
|
||||
function setSeed(precreatedSeed) {
|
||||
if (!precreatedSeed) {
|
||||
const first = !mapHistory[0];
|
||||
const url = new URL(window.location.href);
|
||||
const params = url.searchParams;
|
||||
const urlSeed = url.searchParams.get("seed");
|
||||
if (first && params.get("from") === "MFCG" && urlSeed.length === 13) seed = urlSeed.slice(0, -4);
|
||||
else if (first && urlSeed) seed = urlSeed;
|
||||
else if (optionsSeed.value && optionsSeed.value != seed) seed = optionsSeed.value;
|
||||
else seed = generateSeed();
|
||||
} else {
|
||||
seed = precreatedSeed;
|
||||
}
|
||||
|
||||
byId("optionsSeed").value = seed;
|
||||
Math.random = aleaPRNG(seed);
|
||||
}
|
||||
54
src/scripts/generation/grid.ts
Normal file
54
src/scripts/generation/grid.ts
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
import {calculateTemperatures} from "modules/temperature";
|
||||
import {generateGrid} from "utils/graphUtils";
|
||||
import {calculateMapCoordinates, defineMapSize} from "modules/coordinates";
|
||||
import {markupGridFeatures} from "modules/markup";
|
||||
// @ts-expect-error js module
|
||||
import {generatePrecipitation} from "modules/precipitation";
|
||||
import {byId} from "utils/shorthands";
|
||||
import {rn} from "utils/numberUtils";
|
||||
|
||||
const {Lakes, HeightmapGenerator} = window;
|
||||
|
||||
export async function createGrid(globalGrid: IGrid, precreatedGraph?: IGrid): Promise<IGrid> {
|
||||
const baseGrid: IGridBase = shouldRegenerateGridPoints(globalGrid)
|
||||
? (precreatedGraph && undressGrid(precreatedGraph)) || generateGrid()
|
||||
: undressGrid(globalGrid);
|
||||
|
||||
const heights: Uint8Array = await HeightmapGenerator.generate(baseGrid);
|
||||
if (!heights) throw new Error("Heightmap generation failed");
|
||||
const heightsGrid = {...baseGrid, cells: {...baseGrid.cells, h: heights}};
|
||||
|
||||
const {featureIds, distanceField, features} = markupGridFeatures(heightsGrid);
|
||||
const markedGrid = {...heightsGrid, features, cells: {...heightsGrid.cells, f: featureIds, t: distanceField}};
|
||||
|
||||
const touchesEdges = features.some(feature => feature && feature.land && feature.border);
|
||||
defineMapSize(touchesEdges);
|
||||
window.mapCoordinates = calculateMapCoordinates();
|
||||
|
||||
Lakes.addLakesInDeepDepressions(markedGrid);
|
||||
Lakes.openNearSeaLakes(markedGrid);
|
||||
|
||||
const temperature = calculateTemperatures(markedGrid);
|
||||
const temperatureGrid = {...markedGrid, cells: {...markedGrid.cells, temp: temperature}};
|
||||
|
||||
const prec = generatePrecipitation(temperatureGrid);
|
||||
return {...temperatureGrid, cells: {...temperatureGrid.cells, prec}};
|
||||
}
|
||||
|
||||
function undressGrid(extendedGrid: IGrid): IGridBase {
|
||||
const {spacing, cellsDesired, boundary, points, cellsX, cellsY, cells, vertices} = extendedGrid;
|
||||
const {i, b, c, v} = cells;
|
||||
return {spacing, cellsDesired, boundary, points, cellsX, cellsY, cells: {i, b, c, v}, vertices};
|
||||
}
|
||||
|
||||
// check if new grid graph should be generated or we can use the existing one
|
||||
export function shouldRegenerateGridPoints(grid: IGrid) {
|
||||
const cellsDesired = Number(byId("pointsInput")?.dataset.cells);
|
||||
if (cellsDesired !== grid.cellsDesired) return true;
|
||||
|
||||
const newSpacing = rn(Math.sqrt((graphWidth * graphHeight) / cellsDesired), 2);
|
||||
const newCellsX = Math.floor((graphWidth + 0.5 * newSpacing - 1e-10) / newSpacing);
|
||||
const newCellsY = Math.floor((graphHeight + 0.5 * newSpacing - 1e-10) / newSpacing);
|
||||
|
||||
return grid.spacing !== newSpacing || grid.cellsX !== newCellsX || grid.cellsY !== newCellsY;
|
||||
}
|
||||
75
src/scripts/generation/reGraph.ts
Normal file
75
src/scripts/generation/reGraph.ts
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
import * as d3 from "d3";
|
||||
|
||||
import {TIME} from "config/logging";
|
||||
import {UINT16_MAX} from "constants";
|
||||
import {createTypedArray} from "utils/arrayUtils";
|
||||
import {calculateVoronoi, getPackPolygon} from "utils/graphUtils";
|
||||
import {rn} from "utils/numberUtils";
|
||||
import {DISTANCE_FIELD, MIN_LAND_HEIGHT} from "config/generation";
|
||||
|
||||
const {LAND_COAST, WATER_COAST, DEEPER_WATER} = DISTANCE_FIELD;
|
||||
|
||||
// recalculate Voronoi Graph to pack cells
|
||||
export function reGraph(grid: IGrid): IPackBase {
|
||||
TIME && console.time("reGraph");
|
||||
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];
|
||||
if (height < MIN_LAND_HEIGHT && type !== WATER_COAST && type !== DEEPER_WATER) continue; // exclude all deep ocean points
|
||||
|
||||
const feature = features[gridCells.f[i]];
|
||||
const isLake = feature && feature.type === "lake";
|
||||
|
||||
if (type === DEEPER_WATER && (i % 4 === 0 || isLake)) continue; // exclude non-coastal lake points
|
||||
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);
|
||||
}
|
||||
|
||||
function getCellArea(i: number) {
|
||||
const area = Math.abs(d3.polygonArea(getPackPolygon(i)));
|
||||
return Math.min(area, UINT16_MAX);
|
||||
}
|
||||
|
||||
const {cells, vertices} = calculateVoronoi(newCells.p, grid.boundary);
|
||||
|
||||
const pack: IPackBase = {
|
||||
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])),
|
||||
h: new Uint8Array(newCells.h),
|
||||
area: createTypedArray({maxValue: UINT16_MAX, from: cells.i}).map(getCellArea)
|
||||
}
|
||||
};
|
||||
|
||||
TIME && console.timeEnd("reGraph");
|
||||
return pack;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue