refactor: migrate resample module (#1351)

* refactor: resampling functionality

* fix: type issues

* fix: reorder polyfills import in index.ts

* refactor: reorder exports in index.ts for consistency
This commit is contained in:
Marc Emmanuel 2026-03-18 17:51:53 +01:00 committed by GitHub
parent 3f9a7702d4
commit f2fc42799b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
20 changed files with 601 additions and 417 deletions

15
package-lock.json generated
View file

@ -12,6 +12,7 @@
"alea": "^1.0.1",
"d3": "^7.9.0",
"delaunator": "^5.0.1",
"lineclip": "^2.0.0",
"polylabel": "^2.0.1"
},
"devDependencies": {
@ -19,6 +20,7 @@
"@playwright/test": "^1.57.0",
"@types/d3": "^7.4.3",
"@types/delaunator": "^5.0.3",
"@types/lineclip": "^2.0.0",
"@types/node": "^25.0.10",
"@types/polylabel": "^1.1.3",
"@vitest/browser": "^4.0.18",
@ -1347,6 +1349,13 @@
"dev": true,
"license": "MIT"
},
"node_modules/@types/lineclip": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/@types/lineclip/-/lineclip-2.0.0.tgz",
"integrity": "sha512-LsPRWfV5kC41YgraYhnAMNSNhdJwFlCsUPueSw7sG5UvMqSMxMcaOA9LWN8mZiCUe9jVIAKnLfsNiXpvnd7gKQ==",
"dev": true,
"license": "MIT"
},
"node_modules/@types/node": {
"version": "25.0.10",
"resolved": "https://registry.npmjs.org/@types/node/-/node-25.0.10.tgz",
@ -2093,6 +2102,12 @@
"node": ">=12"
}
},
"node_modules/lineclip": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/lineclip/-/lineclip-2.0.0.tgz",
"integrity": "sha512-PosanfyLckGXZbCX+aWmfmHWWhVPnLf9iKcUefaSGGw2IBOef5XdBdyl175LEqRy/sEOZ2SEz/l7K5S93BZlYQ==",
"license": "ISC"
},
"node_modules/magic-string": {
"version": "0.30.21",
"resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.30.21.tgz",

View file

@ -28,6 +28,7 @@
"@playwright/test": "^1.57.0",
"@types/d3": "^7.4.3",
"@types/delaunator": "^5.0.3",
"@types/lineclip": "^2.0.0",
"@types/node": "^25.0.10",
"@types/polylabel": "^1.1.3",
"@vitest/browser": "^4.0.18",
@ -41,6 +42,7 @@
"alea": "^1.0.1",
"d3": "^7.9.0",
"delaunator": "^5.0.1",
"lineclip": "^2.0.0",
"polylabel": "^2.0.1"
},
"engines": {

View file

@ -1,2 +0,0 @@
// lineclip by mourner, https://github.com/mapbox/lineclip
"use strict";function lineclip(t,e,n){var r,i,u,o,s,h=t.length,c=bitCode(t[0],e),f=[];for(n=n||[],r=1;r<h;r++){for(i=t[r-1],o=s=bitCode(u=t[r],e);;){if(!(c|o)){f.push(i),o!==s?(f.push(u),r<h-1&&(n.push(f),f=[])):r===h-1&&f.push(u);break}if(c&o)break;c?c=bitCode(i=intersect(i,u,c,e),e):o=bitCode(u=intersect(i,u,o,e),e)}c=s}return f.length&&n.push(f),n}function polygonclip(t,e,n=0){for(var r,i,u,o,s,h,c,f=1;f<=8;f*=2){for(r=[],u=!(bitCode(i=t[t.length-1],e)&f),s=0;s<t.length;s++){o=(c=!(bitCode(h=t[s],e)&f))!==u;var l=intersect(i,h,f,e);o&&r.push(l),n&&o&&r.push(l,l),c&&r.push(h),i=h,u=c}if(!(t=r).length)break}return r}function intersect(t,e,n,r){return 8&n?[t[0]+(e[0]-t[0])*(r[3]-t[1])/(e[1]-t[1]),r[3]]:4&n?[t[0]+(e[0]-t[0])*(r[1]-t[1])/(e[1]-t[1]),r[1]]:2&n?[r[2],t[1]+(e[1]-t[1])*(r[2]-t[0])/(e[0]-t[0])]:1&n?[r[0],t[1]+(e[1]-t[1])*(r[0]-t[0])/(e[0]-t[0])]:null}function bitCode(t,e){var n=0;return t[0]<e[0]?n|=1:t[0]>e[2]&&(n|=2),t[1]<e[1]?n|=4:t[1]>e[3]&&(n|=8),n}

View file

@ -1,386 +0,0 @@
"use strict";
window.Resample = (function () {
/*
generate new map based on an existing one (resampling parentMap)
parentMap: {grid, pack, notes} from original map
projection: f(Number, Number) -> [Number, Number]
inverse: f(Number, Number) -> [Number, Number]
scale: Number
*/
function process({projection, inverse, scale}) {
const parentMap = {grid: structuredClone(grid), pack: structuredClone(pack), notes: structuredClone(notes)};
const riversData = saveRiversData(pack.rivers);
grid = generateGrid();
pack = {};
notes = parentMap.notes;
resamplePrimaryGridData(parentMap, inverse, scale);
Features.markupGrid();
addLakesInDeepDepressions();
openNearSeaLakes();
OceanLayers();
calculateMapCoordinates();
calculateTemperatures();
reGraph();
Features.markupPack();
Ice.generate();
createDefaultRuler();
restoreCellData(parentMap, inverse, scale);
restoreRivers(riversData, projection, scale);
restoreCultures(parentMap, projection);
restoreBurgs(parentMap, projection, scale);
restoreStates(parentMap, projection);
restoreRoutes(parentMap, projection);
restoreReligions(parentMap, projection);
restoreProvinces(parentMap);
restoreFeatureDetails(parentMap, inverse);
restoreMarkers(parentMap, projection);
restoreZones(parentMap, projection, scale);
showStatistics();
}
function resamplePrimaryGridData(parentMap, inverse, scale) {
grid.cells.h = new Uint8Array(grid.points.length);
grid.cells.temp = new Int8Array(grid.points.length);
grid.cells.prec = new Uint8Array(grid.points.length);
const parentPackQ = d3.quadtree(parentMap.pack.cells.p.map(([x, y], i) => [x, y, i]));
grid.points.forEach(([x, y], newGridCell) => {
const [parentX, parentY] = inverse(x, y);
const parentPackCell = parentPackQ.find(parentX, parentY, Infinity)[2];
const parentGridCell = parentMap.pack.cells.g[parentPackCell];
grid.cells.h[newGridCell] = parentMap.grid.cells.h[parentGridCell];
grid.cells.temp[newGridCell] = parentMap.grid.cells.temp[parentGridCell];
grid.cells.prec[newGridCell] = parentMap.grid.cells.prec[parentGridCell];
});
if (scale >= 2) smoothHeightmap();
}
function smoothHeightmap() {
grid.cells.h.forEach((height, newGridCell) => {
const heights = [height, ...grid.cells.c[newGridCell].map(c => grid.cells.h[c])];
const meanHeight = d3.mean(heights);
grid.cells.h[newGridCell] = isWater(grid, newGridCell) ? Math.min(meanHeight, 19) : Math.max(meanHeight, 20);
});
}
function restoreCellData(parentMap, inverse, scale) {
pack.cells.biome = new Uint8Array(pack.cells.i.length);
pack.cells.fl = new Uint16Array(pack.cells.i.length);
pack.cells.s = new Int16Array(pack.cells.i.length);
pack.cells.pop = new Float32Array(pack.cells.i.length);
pack.cells.culture = new Uint16Array(pack.cells.i.length);
pack.cells.state = new Uint16Array(pack.cells.i.length);
pack.cells.burg = new Uint16Array(pack.cells.i.length);
pack.cells.religion = new Uint16Array(pack.cells.i.length);
pack.cells.province = new Uint16Array(pack.cells.i.length);
const parentPackCellGroups = groupCellsByType(parentMap.pack);
const parentPackLandCellsQuadtree = d3.quadtree(parentPackCellGroups.land);
for (const newPackCell of pack.cells.i) {
const [x, y] = inverse(...pack.cells.p[newPackCell]);
if (isWater(pack, newPackCell)) continue;
const parentPackCell = parentPackLandCellsQuadtree.find(x, y, Infinity)[2];
const parentCellArea = parentMap.pack.cells.area[parentPackCell];
const areaRatio = pack.cells.area[newPackCell] / parentCellArea;
const scaleRatio = areaRatio / scale;
pack.cells.biome[newPackCell] = parentMap.pack.cells.biome[parentPackCell];
pack.cells.fl[newPackCell] = parentMap.pack.cells.fl[parentPackCell];
pack.cells.s[newPackCell] = parentMap.pack.cells.s[parentPackCell] * scaleRatio;
pack.cells.pop[newPackCell] = parentMap.pack.cells.pop[parentPackCell] * scaleRatio;
pack.cells.culture[newPackCell] = parentMap.pack.cells.culture[parentPackCell];
pack.cells.state[newPackCell] = parentMap.pack.cells.state[parentPackCell];
pack.cells.religion[newPackCell] = parentMap.pack.cells.religion[parentPackCell];
pack.cells.province[newPackCell] = parentMap.pack.cells.province[parentPackCell];
}
}
function saveRiversData(parentRivers) {
return parentRivers.map(river => {
const meanderedPoints = Rivers.addMeandering(river.cells, river.points);
return {...river, meanderedPoints};
});
}
function restoreRivers(riversData, projection, scale) {
pack.cells.r = new Uint16Array(pack.cells.i.length);
pack.cells.conf = new Uint8Array(pack.cells.i.length);
pack.rivers = riversData
.map(river => {
let wasInMap = true;
const points = [];
river.meanderedPoints.forEach(([parentX, parentY]) => {
const [x, y] = projection(parentX, parentY);
const inMap = isInMap(x, y);
if (inMap || wasInMap) points.push([rn(x, 2), rn(y, 2)]);
wasInMap = inMap;
});
if (points.length < 2) return null;
const cells = points.map(point => findCell(...point));
cells.forEach(cellId => {
if (pack.cells.r[cellId]) pack.cells.conf[cellId] = 1;
pack.cells.r[cellId] = river.i;
});
const widthFactor = river.widthFactor * scale;
return {...river, cells, points, source: cells.at(0), mouth: cells.at(-2), widthFactor};
})
.filter(Boolean);
pack.rivers.forEach(river => {
river.basin = Rivers.getBasin(river.i);
river.length = Rivers.getApproximateLength(river.points);
});
}
function restoreCultures(parentMap, projection) {
const validCultures = new Set(pack.cells.culture);
const culturePoles = getPolesOfInaccessibility(pack, cellId => pack.cells.culture[cellId]);
pack.cultures = parentMap.pack.cultures.map(culture => {
if (!culture.i || culture.removed) return culture;
if (!validCultures.has(culture.i)) return {...culture, removed: true, lock: false};
const [xp, yp] = projection(...parentMap.pack.cells.p[culture.center]);
const [x, y] = [rn(xp, 2), rn(yp, 2)];
const centerCoords = isInMap(x, y) ? [x, y] : culturePoles[culture.i];
const center = findCell(...centerCoords);
return {...culture, center};
});
}
function restoreBurgs(parentMap, projection, scale) {
const packLandCellsQuadtree = d3.quadtree(groupCellsByType(pack).land);
const findLandCell = (x, y) => packLandCellsQuadtree.find(x, y, Infinity)?.[2];
pack.burgs = parentMap.pack.burgs.map(burg => {
if (!burg.i || burg.removed) return burg;
burg.population *= scale; // adjust for populationRate change
const [xp, yp] = projection(burg.x, burg.y);
if (!isInMap(xp, yp)) return {...burg, removed: true, lock: false};
const closestCell = findCell(xp, yp);
const cell = isWater(pack, closestCell) ? findLandCell(xp, yp) : closestCell;
if (pack.cells.burg[cell]) {
WARN && console.warn(`Cell ${cell} already has a burg. Removing burg ${burg.name} (${burg.i})`);
return {...burg, removed: true, lock: false};
}
pack.cells.burg[cell] = burg.i;
const [x, y] = getBurgCoordinates(burg, closestCell, cell, xp, yp);
return {...burg, cell, x, y};
});
function getBurgCoordinates(burg, closestCell, cell, xp, yp) {
const haven = pack.cells.haven[cell];
if (burg.port && haven) return getCloseToEdgePoint(cell, haven);
if (closestCell !== cell) return pack.cells.p[cell];
return [rn(xp, 2), rn(yp, 2)];
}
function getCloseToEdgePoint(cell1, cell2) {
const {cells, vertices} = pack;
const [x0, y0] = cells.p[cell1];
const commonVertices = cells.v[cell1].filter(vertex => vertices.c[vertex].some(cell => cell === cell2));
const [x1, y1] = vertices.p[commonVertices[0]];
const [x2, y2] = vertices.p[commonVertices[1]];
const xEdge = (x1 + x2) / 2;
const yEdge = (y1 + y2) / 2;
const x = rn(x0 + 0.95 * (xEdge - x0), 2);
const y = rn(y0 + 0.95 * (yEdge - y0), 2);
return [x, y];
}
}
function restoreStates(parentMap, projection) {
const validStates = new Set(pack.cells.state);
pack.states = parentMap.pack.states.map(state => {
if (!state.i || state.removed) return state;
if (validStates.has(state.i)) return state;
return {...state, removed: true, lock: false};
});
States.getPoles();
const regimentCellsMap = {};
const VERTICAL_GAP = 8;
pack.states = pack.states.map(state => {
if (!state.i || state.removed) return state;
const capital = pack.burgs[state.capital];
state.center = !capital || capital.removed ? findCell(...state.pole) : capital.cell;
const military = state.military.map(regiment => {
const cellCoords = projection(...parentMap.pack.cells.p[regiment.cell]);
const cell = isInMap(...cellCoords) ? findCell(...cellCoords) : state.center;
const [xPos, yPos] = projection(regiment.x, regiment.y);
const [xBase, yBase] = projection(regiment.bx, regiment.by);
const [xCell, yCell] = pack.cells.p[cell];
const regsOnCell = regimentCellsMap[cell] || 0;
regimentCellsMap[cell] = regsOnCell + 1;
const name =
isInMap(xPos, yPos) || regiment.name.includes("[relocated]") ? regiment.name : `[relocated] ${regiment.name}`;
const pos = isInMap(xPos, yPos)
? {x: rn(xPos, 2), y: rn(yPos, 2)}
: {x: xCell, y: yCell + regsOnCell * VERTICAL_GAP};
const base = isInMap(xBase, yBase) ? {bx: rn(xBase, 2), by: rn(yBase, 2)} : {bx: xCell, by: yCell};
return {...regiment, cell, name, ...base, ...pos};
});
const neighbors = state.neighbors.filter(stateId => validStates.has(stateId));
return {...state, neighbors, military};
});
}
function restoreRoutes(parentMap, projection) {
pack.routes = parentMap.pack.routes
.map(route => {
let wasInMap = true;
const points = [];
route.points.forEach(([parentX, parentY]) => {
const [x, y] = projection(parentX, parentY);
const inMap = isInMap(x, y);
if (inMap || wasInMap) points.push([rn(x, 2), rn(y, 2)]);
wasInMap = inMap;
});
if (points.length < 2) return null;
const bbox = [0, 0, graphWidth, graphHeight];
const clipped = lineclip(points, bbox)[0].map(([x, y]) => [rn(x, 2), rn(y, 2), findCell(x, y)]);
const firstCell = clipped[0][2];
const feature = pack.cells.f[firstCell];
return {...route, feature, points: clipped};
})
.filter(Boolean);
pack.cells.routes = Routes.buildLinks(pack.routes);
}
function restoreReligions(parentMap, projection) {
const validReligions = new Set(pack.cells.religion);
const religionPoles = getPolesOfInaccessibility(pack, cellId => pack.cells.religion[cellId]);
pack.religions = parentMap.pack.religions.map(religion => {
if (!religion.i || religion.removed) return religion;
if (!validReligions.has(religion.i)) return {...religion, removed: true, lock: false};
const [xp, yp] = projection(...parentMap.pack.cells.p[religion.center]);
const [x, y] = [rn(xp, 2), rn(yp, 2)];
const centerCoords = isInMap(x, y) ? [x, y] : religionPoles[religion.i];
const center = findCell(...centerCoords);
return {...religion, center};
});
}
function restoreProvinces(parentMap) {
const validProvinces = new Set(pack.cells.province);
pack.provinces = parentMap.pack.provinces.map(province => {
if (!province.i || province.removed) return province;
if (!validProvinces.has(province.i)) return {...province, removed: true, lock: false};
return province;
});
Provinces.getPoles();
pack.provinces.forEach(province => {
if (!province.i || province.removed) return;
const capital = pack.burgs[province.burg];
province.center = !capital?.removed ? capital.cell : findCell(...province.pole);
});
}
function restoreMarkers(parentMap, projection) {
pack.markers = parentMap.pack.markers;
pack.markers.forEach(marker => {
const [x, y] = projection(marker.x, marker.y);
if (!isInMap(x, y)) Markers.deleteMarker(marker.i);
const cell = findCell(x, y);
marker.x = rn(x, 2);
marker.y = rn(y, 2);
marker.cell = cell;
});
}
function restoreZones(parentMap, projection, scale) {
const getSearchRadius = cellId => Math.sqrt(parentMap.pack.cells.area[cellId] / Math.PI) * scale;
pack.zones = parentMap.pack.zones.map(zone => {
const cells = zone.cells
.map(cellId => {
const [x, y] = projection(...parentMap.pack.cells.p[cellId]);
if (!isInMap(x, y)) return null;
return findAll(x, y, getSearchRadius(cellId));
})
.filter(Boolean)
.flat();
return {...zone, cells: unique(cells)};
});
}
function restoreFeatureDetails(parentMap, inverse) {
const parentPackQ = d3.quadtree(parentMap.pack.cells.p.map(([x, y], i) => [x, y, i]));
pack.features.forEach(feature => {
if (!feature) return;
const [x, y] = pack.cells.p[feature.firstCell];
const [parentX, parentY] = inverse(x, y);
const parentCell = parentPackQ.find(parentX, parentY, Infinity)[2];
if (parentCell === undefined) return;
const parentFeature = parentMap.pack.features[parentMap.pack.cells.f[parentCell]];
if (parentFeature.group) feature.group = parentFeature.group;
if (parentFeature.name) feature.name = parentFeature.name;
if (parentFeature.height) feature.height = parentFeature.height;
});
}
function groupCellsByType(graph) {
return graph.cells.p.reduce(
(acc, [x, y], cellId) => {
const group = isWater(graph, cellId) ? "water" : "land";
acc[group].push([x, y, cellId]);
return acc;
},
{land: [], water: []}
);
}
function isWater(graph, cellId) {
return graph.cells.h[cellId] < 20;
}
function isInMap(x, y) {
return x >= 0 && x <= graphWidth && y >= 0 && y <= graphHeight;
}
return {process};
})();

View file

@ -8539,10 +8539,8 @@
<script defer src="config/heightmap-templates.js"></script>
<script defer src="config/precreated-heightmaps.js"></script>
<script defer src="modules/resample.js?v=1.113.6"></script>
<script defer src="libs/alea.min.js?v1.105.0"></script>
<script defer src="libs/polylabel.min.js?v1.105.0"></script>
<script defer src="libs/lineclip.min.js?v1.105.0"></script>
<script defer src="libs/simplify.js?v1.105.6"></script>
<script defer src="modules/ui/layers.js?v=1.111.0"></script>
<script defer src="modules/ui/measurers.js?v=1.99.00"></script>

View file

@ -1304,7 +1304,7 @@ class CulturesModule {
cells.culture[cellId] = 0;
}
} else {
cells.culture = new Uint16Array(cells.i.length) as unknown as number[];
cells.culture = new Uint16Array(cells.i.length);
}
for (const culture of cultures) {

View file

@ -234,6 +234,8 @@ class FeatureModule {
const [startCell, featureVertices] = getCellsData(type, firstCell);
const points = clipPoly(
featureVertices.map((vertex: number) => vertices.p[vertex]),
graphWidth,
graphHeight,
);
const area = polygonArea(points); // feature perimiter area
const absArea = Math.abs(rn(area));

View file

@ -18,3 +18,4 @@ import "./ice";
import "./military-generator";
import "./markers-generator";
import "./fonts";
import "./resample";

View file

@ -111,7 +111,6 @@ class OceanModule {
relaxed.map((v) => this.vertices.p[v]),
graphWidth,
graphHeight,
1,
);
chains.push([t, points]);
}

539
src/modules/resample.ts Normal file
View file

@ -0,0 +1,539 @@
import { mean, quadtree } from "d3";
import { clipPolyline } from "lineclip";
import type { PackedGraph } from "../types/PackedGraph";
import {
findAllCellsInRadius,
findClosestCell,
generateGrid,
getPolesOfInaccessibility,
isWater,
rn,
unique,
} from "../utils";
import type { River } from "./river-generator";
import type { Point } from "./voronoi";
declare global {
var Resample: Resampler;
}
interface ResamplerProcessOptions {
projection: (x: number, y: number) => [number, number];
inverse: (x: number, y: number) => [number, number];
scale: number;
}
type ParentMapDefinition = {
grid: any;
pack: PackedGraph;
notes: any[];
};
class Resampler {
private saveRiversData(parentRivers: PackedGraph["rivers"]) {
return parentRivers.map((river) => {
const meanderedPoints = Rivers.addMeandering(river.cells, river.points);
return { ...river, meanderedPoints };
});
}
private smoothHeightmap() {
grid.cells.h.forEach((height: number, newGridCell: number) => {
const heights = [
height,
...grid.cells.c[newGridCell].map((c: number) => grid.cells.h[c]),
];
const meanHeight = mean(heights) as number;
grid.cells.h[newGridCell] = isWater(newGridCell, grid)
? Math.min(meanHeight, 19)
: Math.max(meanHeight, 20);
});
}
private resamplePrimaryGridData(
parentMap: ParentMapDefinition,
inverse: (x: number, y: number) => [number, number],
scale: number,
) {
grid.cells.h = new Uint8Array(grid.points.length);
grid.cells.temp = new Int8Array(grid.points.length);
grid.cells.prec = new Uint8Array(grid.points.length);
const parentPackQ = quadtree(
parentMap.pack.cells.p.map(([x, y], i) => [x, y, i]),
);
grid.points.forEach(([x, y]: [number, number], newGridCell: number) => {
const [parentX, parentY] = inverse(x, y);
const parentPackCell = parentPackQ.find(parentX, parentY, Infinity)?.[2];
if (parentPackCell === undefined) return;
const parentGridCell = parentMap.pack.cells.g[parentPackCell];
grid.cells.h[newGridCell] = parentMap.grid.cells.h[parentGridCell];
grid.cells.temp[newGridCell] = parentMap.grid.cells.temp[parentGridCell];
grid.cells.prec[newGridCell] = parentMap.grid.cells.prec[parentGridCell];
});
if (scale >= 2) this.smoothHeightmap();
}
private groupCellsByType(graph: PackedGraph) {
return graph.cells.p.reduce(
(acc, [x, y], cellId) => {
const group = isWater(cellId, graph) ? "water" : "land";
acc[group].push([x, y, cellId]);
return acc;
},
{ land: [], water: [] } as Record<string, [number, number, number][]>,
);
}
private isInMap(x: number, y: number) {
return x >= 0 && x <= graphWidth && y >= 0 && y <= graphHeight;
}
private restoreCellData(
parentMap: ParentMapDefinition,
inverse: (x: number, y: number) => [number, number],
scale: number,
) {
pack.cells.biome = new Uint8Array(pack.cells.i.length);
pack.cells.fl = new Uint16Array(pack.cells.i.length);
pack.cells.s = new Int16Array(pack.cells.i.length);
pack.cells.pop = new Float32Array(pack.cells.i.length);
pack.cells.culture = new Uint16Array(pack.cells.i.length);
pack.cells.state = new Uint16Array(pack.cells.i.length);
pack.cells.burg = new Uint16Array(pack.cells.i.length);
pack.cells.religion = new Uint16Array(pack.cells.i.length);
pack.cells.province = new Uint16Array(pack.cells.i.length);
const parentPackCellGroups = this.groupCellsByType(parentMap.pack);
const parentPackLandCellsQuadtree = quadtree(parentPackCellGroups.land);
for (const newPackCell of pack.cells.i) {
const [x, y] = inverse(...pack.cells.p[newPackCell]);
if (isWater(newPackCell, pack)) continue;
const parentPackCell = parentPackLandCellsQuadtree.find(
x,
y,
Infinity,
)?.[2];
if (parentPackCell === undefined) continue;
const parentCellArea = parentMap.pack.cells.area[parentPackCell];
const areaRatio = pack.cells.area[newPackCell] / parentCellArea;
const scaleRatio = areaRatio / scale;
pack.cells.biome[newPackCell] =
parentMap.pack.cells.biome[parentPackCell];
pack.cells.fl[newPackCell] = parentMap.pack.cells.fl[parentPackCell];
pack.cells.s[newPackCell] =
parentMap.pack.cells.s[parentPackCell] * scaleRatio;
pack.cells.pop[newPackCell] =
parentMap.pack.cells.pop[parentPackCell] * scaleRatio;
pack.cells.culture[newPackCell] =
parentMap.pack.cells.culture[parentPackCell];
pack.cells.state[newPackCell] =
parentMap.pack.cells.state[parentPackCell];
pack.cells.religion[newPackCell] =
parentMap.pack.cells.religion[parentPackCell];
pack.cells.province[newPackCell] =
parentMap.pack.cells.province[parentPackCell];
}
}
private restoreRivers(
riversData: (River & { meanderedPoints?: [number, number, number][] })[],
projection: (x: number, y: number) => [number, number],
scale: number,
) {
pack.cells.r = new Uint16Array(pack.cells.i.length);
pack.cells.conf = new Uint8Array(pack.cells.i.length);
pack.rivers = riversData
.map((river) => {
let wasInMap = true;
const points: Point[] = [];
river.meanderedPoints?.forEach(([parentX, parentY]) => {
const [x, y] = projection(parentX, parentY);
const inMap = this.isInMap(x, y);
if (inMap || wasInMap) points.push([rn(x, 2), rn(y, 2)]);
wasInMap = inMap;
});
if (points.length < 2) return null;
const cells = points
.map((point) => findClosestCell(...point, Infinity, pack))
.filter((cellId) => cellId !== undefined);
cells.forEach((cellId) => {
if (pack.cells.r[cellId]) pack.cells.conf[cellId] = 1;
pack.cells.r[cellId] = river.i;
});
const widthFactor = river.widthFactor * scale;
delete river.meanderedPoints;
return {
...river,
cells,
points,
source: cells.at(0) as number,
mouth: cells.at(-2) as number,
widthFactor,
};
})
.filter((river) => river !== null);
pack.rivers.forEach((river) => {
river.basin = Rivers.getBasin(river.i);
river.length = Rivers.getApproximateLength(river.points);
});
}
private restoreCultures(
parentMap: ParentMapDefinition,
projection: (x: number, y: number) => [number, number],
) {
const validCultures = new Set(pack.cells.culture);
const culturePoles = getPolesOfInaccessibility(
pack,
(cellId) => pack.cells.culture[cellId],
);
pack.cultures = parentMap.pack.cultures.map((culture) => {
if (!culture.i || culture.removed) return culture;
if (!validCultures.has(culture.i))
return { ...culture, removed: true, lock: false };
const parentCoords = parentMap.pack.cells.p[culture.center!];
const [xp, yp] = projection(parentCoords[0], parentCoords[1]);
const [x, y] = [rn(xp, 2), rn(yp, 2)];
const [centerX, centerY] = this.isInMap(x, y)
? [x, y]
: culturePoles[culture.i];
const center = findClosestCell(centerX, centerY, Infinity, pack);
return { ...culture, center };
});
}
private getBurgCoordinates(
burg: PackedGraph["burgs"][number],
closestCell: number,
cell: number,
xp: number,
yp: number,
): Point {
const haven = pack.cells.haven[cell];
if (burg.port && haven) return this.getCloseToEdgePoint(cell, haven);
if (closestCell !== cell) return pack.cells.p[cell];
return [rn(xp, 2), rn(yp, 2)];
}
private getCloseToEdgePoint(cell1: number, cell2: number): Point {
const { cells, vertices } = pack;
const [x0, y0] = cells.p[cell1];
const commonVertices = cells.v[cell1].filter((vertex) =>
vertices.c[vertex].some((cell) => cell === cell2),
);
const [x1, y1] = vertices.p[commonVertices[0]];
const [x2, y2] = vertices.p[commonVertices[1]];
const xEdge = (x1 + x2) / 2;
const yEdge = (y1 + y2) / 2;
const x = rn(x0 + 0.95 * (xEdge - x0), 2);
const y = rn(y0 + 0.95 * (yEdge - y0), 2);
return [x, y];
}
private restoreBurgs(
parentMap: ParentMapDefinition,
projection: (x: number, y: number) => [number, number],
scale: number,
) {
const packLandCellsQuadtree = quadtree(this.groupCellsByType(pack).land);
const findLandCell = (x: number, y: number) =>
packLandCellsQuadtree.find(x, y, Infinity)?.[2];
pack.burgs = parentMap.pack.burgs.map((burg) => {
if (!burg.i || burg.removed) return burg;
burg.population! *= scale; // adjust for populationRate change
const [xp, yp] = projection(burg.x, burg.y);
if (!this.isInMap(xp, yp)) return { ...burg, removed: true, lock: false };
const closestCell = findClosestCell(xp, yp, Infinity, pack) as number;
const cell = isWater(closestCell, pack)
? (findLandCell(xp, yp) as number)
: closestCell;
if (pack.cells.burg[cell]) {
WARN &&
console.warn(
`Cell ${cell} already has a burg. Removing burg ${burg.name} (${burg.i})`,
);
return { ...burg, removed: true, lock: false };
}
pack.cells.burg[cell] = burg.i;
const [x, y] = this.getBurgCoordinates(burg, closestCell, cell, xp, yp);
return { ...burg, cell, x, y };
});
}
private restoreStates(
parentMap: ParentMapDefinition,
projection: (x: number, y: number) => [number, number],
) {
const validStates = new Set(pack.cells.state);
pack.states = parentMap.pack.states.map((state) => {
if (!state.i || state.removed) return state;
if (validStates.has(state.i)) return state;
return { ...state, removed: true, lock: false };
});
States.getPoles();
const regimentCellsMap: Record<number, number> = {};
const VERTICAL_GAP = 8;
pack.states = pack.states.map((state) => {
if (!state.i || state.removed) return state;
const capital = pack.burgs[state.capital];
const [poleX, poleY] = state.pole as Point;
state.center =
!capital || capital.removed
? findClosestCell(poleX, poleY, Infinity, pack)!
: capital.cell;
const military = state.military!.map((regiment) => {
const cellCoords = projection(...parentMap.pack.cells.p[regiment.cell]);
const cell = this.isInMap(...cellCoords)
? findClosestCell(...cellCoords, Infinity, pack)!
: state.center;
const [xPos, yPos] = projection(regiment.x, regiment.y);
const [xBase, yBase] = projection(regiment.bx, regiment.by);
const [xCell, yCell] = pack.cells.p[cell];
const regsOnCell = regimentCellsMap[cell] || 0;
regimentCellsMap[cell] = regsOnCell + 1;
const name =
this.isInMap(xPos, yPos) || regiment.name.includes("[relocated]")
? regiment.name
: `[relocated] ${regiment.name}`;
const pos = this.isInMap(xPos, yPos)
? { x: rn(xPos, 2), y: rn(yPos, 2) }
: { x: xCell, y: yCell + regsOnCell * VERTICAL_GAP };
const base = this.isInMap(xBase, yBase)
? { bx: rn(xBase, 2), by: rn(yBase, 2) }
: { bx: xCell, by: yCell };
return { ...regiment, cell, name, ...base, ...pos };
});
const neighbors = state.neighbors!.filter((stateId) =>
validStates.has(stateId),
);
return { ...state, neighbors, military };
});
}
private restoreRoutes(
parentMap: ParentMapDefinition,
projection: (x: number, y: number) => [number, number],
) {
pack.routes = parentMap.pack.routes
.map((route) => {
let wasInMap = true;
const points: Point[] = [];
route.points.forEach(([parentX, parentY]) => {
const [x, y] = projection(parentX, parentY);
const inMap = this.isInMap(x, y);
if (inMap || wasInMap) points.push([rn(x, 2), rn(y, 2)]);
wasInMap = inMap;
});
if (points.length < 2) return null;
const bbox: [number, number, number, number] = [
0,
0,
graphWidth,
graphHeight,
];
// @types/lineclip is incorrect - lineclip returns Point[][] (array of line segments), not Point[]
const clippedSegments = clipPolyline(
points,
bbox,
) as unknown as Point[][];
if (!clippedSegments[0]?.length) return null;
const clipped = clippedSegments[0].map(
([x, y]) =>
[
rn(x, 2),
rn(y, 2),
findClosestCell(x, y, Infinity, pack) as number,
] as [number, number, number],
);
const firstCell = clipped[0][2];
const feature = pack.cells.f[firstCell];
return { ...route, feature, points: clipped };
})
.filter((route) => route !== null);
pack.cells.routes = Routes.buildLinks(pack.routes);
}
private restoreReligions(
parentMap: ParentMapDefinition,
projection: (x: number, y: number) => [number, number],
) {
const validReligions = new Set(pack.cells.religion);
const religionPoles = getPolesOfInaccessibility(
pack,
(cellId) => pack.cells.religion[cellId],
);
pack.religions = parentMap.pack.religions.map((religion) => {
if (!religion.i || religion.removed) return religion;
if (!validReligions.has(religion.i))
return { ...religion, removed: true, lock: false };
const [xp, yp] = projection(...parentMap.pack.cells.p[religion.center]);
const [x, y] = [rn(xp, 2), rn(yp, 2)];
const [centerX, centerY] = this.isInMap(x, y)
? [x, y]
: religionPoles[religion.i];
const center = findClosestCell(centerX, centerY, Infinity, pack);
return { ...religion, center };
});
}
private restoreProvinces(parentMap: ParentMapDefinition) {
const validProvinces = new Set(pack.cells.province);
pack.provinces = parentMap.pack.provinces.map((province) => {
if (!province.i || province.removed) return province;
if (!validProvinces.has(province.i))
return { ...province, removed: true, lock: false };
return province;
});
Provinces.getPoles();
pack.provinces.forEach((province) => {
if (!province.i || province.removed) return;
const capital = pack.burgs[province.burg];
const [poleX, poleY] = province.pole as Point;
province.center = !capital?.removed
? capital.cell
: findClosestCell(poleX, poleY, Infinity, pack)!;
});
}
private restoreFeatureDetails(
parentMap: ParentMapDefinition,
inverse: (x: number, y: number) => [number, number],
) {
const parentPackQ = quadtree(
parentMap.pack.cells.p.map(([x, y], i) => [x, y, i]),
);
pack.features.forEach((feature) => {
if (!feature) return;
const [x, y] = pack.cells.p[feature.firstCell];
const [parentX, parentY] = inverse(x, y);
const parentCell = parentPackQ.find(parentX, parentY, Infinity)?.[2];
if (parentCell === undefined) return;
const parentFeature =
parentMap.pack.features[parentMap.pack.cells.f[parentCell]];
if (parentFeature.group) feature.group = parentFeature.group;
if (parentFeature.name) feature.name = parentFeature.name;
if (parentFeature.height) feature.height = parentFeature.height;
});
}
private restoreMarkers(
parentMap: ParentMapDefinition,
projection: (x: number, y: number) => [number, number],
) {
pack.markers = parentMap.pack.markers;
pack.markers.forEach((marker) => {
const [x, y] = projection(marker.x, marker.y);
if (!this.isInMap(x, y)) Markers.deleteMarker(marker.i);
const cell = findClosestCell(x, y, Infinity, pack);
marker.x = rn(x, 2);
marker.y = rn(y, 2);
marker.cell = cell;
});
}
private restoreZones(
parentMap: ParentMapDefinition,
projection: (x: number, y: number) => [number, number],
scale: number,
) {
const getSearchRadius = (cellId: number) =>
Math.sqrt(parentMap.pack.cells.area[cellId] / Math.PI) * scale;
pack.zones = parentMap.pack.zones.map((zone) => {
const cells = zone.cells.flatMap((cellId) => {
const [newX, newY] = projection(...parentMap.pack.cells.p[cellId]);
if (!this.isInMap(newX, newY)) return [];
return findAllCellsInRadius(newX, newY, getSearchRadius(cellId), pack);
});
return { ...zone, cells: unique(cells) };
});
}
process(options: ResamplerProcessOptions): void {
const { projection, inverse, scale } = options;
const parentMap = {
grid: structuredClone(grid),
pack: structuredClone(pack),
notes: structuredClone(notes),
};
const riversData = this.saveRiversData(pack.rivers);
grid = generateGrid(seed, graphWidth, graphHeight);
pack = {} as PackedGraph;
notes = parentMap.notes;
this.resamplePrimaryGridData(parentMap, inverse, scale);
Features.markupGrid();
addLakesInDeepDepressions();
openNearSeaLakes();
OceanLayers();
calculateMapCoordinates();
calculateTemperatures();
reGraph();
Features.markupPack();
Ice.generate();
createDefaultRuler();
this.restoreCellData(parentMap, inverse, scale);
this.restoreRivers(riversData, projection, scale);
this.restoreCultures(parentMap, projection);
this.restoreBurgs(parentMap, projection, scale);
this.restoreStates(parentMap, projection);
this.restoreRoutes(parentMap, projection);
this.restoreReligions(parentMap, projection);
this.restoreProvinces(parentMap);
this.restoreFeatureDetails(parentMap, inverse);
this.restoreMarkers(parentMap, projection);
this.restoreZones(parentMap, projection, scale);
showStatistics();
}
}
window.Resample = new Resampler();

View file

@ -1,6 +1,7 @@
import Alea from "alea";
import { curveBasis, curveCatmullRom, line, mean, min, sum } from "d3";
import { each, rn, round, rw } from "../utils";
import type { Point } from "./voronoi";
declare global {
var Rivers: RiverModule;
@ -20,6 +21,7 @@ export interface River {
name: string; // river name
type: string; // river type
cells: number[]; // cells forming the river path
points?: Point[]; // river points (for meandering)
}
class RiverModule {
@ -237,7 +239,9 @@ class RiverModule {
: defaultWidthFactor;
const meanderedPoints = this.addMeandering(riverCells);
const discharge = cells.fl[mouth]; // m3 in second
const length = this.getApproximateLength(meanderedPoints);
const length = this.getApproximateLength(
meanderedPoints.map(([x, y]) => [x, y]),
);
const sourceWidth = this.getSourceWidth(cells.fl[source]);
const width = this.getWidth(
this.getOffset({
@ -411,7 +415,7 @@ class RiverModule {
addMeandering(
riverCells: number[],
riverPoints = null,
riverPoints: Point[] | null = null,
meandering = 0.5,
): [number, number, number][] {
const { fl, h } = pack.cells;
@ -579,7 +583,7 @@ class RiverModule {
);
}
getApproximateLength(points: [number, number, number][]) {
getApproximateLength(points: Point[] = []) {
const length = points.reduce(
(s, v, i, p) =>
s + (i ? Math.hypot(v[0] - p[i - 1][0], v[1] - p[i - 1][1]) : 0),

View file

@ -92,7 +92,7 @@ function featurePathRenderer(feature: PackedGraphFeature): string {
}
const simplifiedPoints = simplify(points, 0.3);
const clippedPoints = clipPoly(simplifiedPoints, graphWidth, graphHeight, 1);
const clippedPoints = clipPoly(simplifiedPoints, graphWidth, graphHeight);
const lineGen = line().curve(curveBasisClosed);
const path = `${round(lineGen(clippedPoints) || "")}Z`;

View file

@ -53,7 +53,11 @@ const pinShapes: PinShapes = {
no: () => "",
};
const getPin = (shape = "bubble", fill = "#fff", stroke = "#000"): string => {
const getPinForShape = (
shape = "bubble",
fill = "#fff",
stroke = "#000",
): string => {
const shapeFunction = pinShapes[shape] || pinShapes.bubble;
return shapeFunction(fill, stroke);
};
@ -104,4 +108,4 @@ const markersRenderer = (): void => {
window.drawMarkers = markersRenderer;
window.drawMarker = markerRenderer;
window.getPin = getPin;
window.getPin = getPinForShape;

View file

@ -1,4 +1,5 @@
import { curveNatural, line, max, select } from "d3";
import type { TypedArray } from "../types/PackedGraph";
import {
drawPath,
drawPoint,
@ -400,7 +401,7 @@ const stateLabelsRenderer = (list?: number[]): void => {
angleRad: number,
halfwidth: number,
halfheight: number,
stateIds: number[],
stateIds: TypedArray,
stateId: number,
): boolean {
const bbox = textElement.getBBox();

View file

@ -1,3 +1,4 @@
import type { Quadtree } from "d3";
import type { Burg } from "../modules/burgs-generator";
import type { Culture } from "../modules/cultures-generator";
import type { PackedGraphFeature } from "../modules/features";
@ -7,7 +8,7 @@ import type { Route } from "../modules/routes-generator";
import type { State } from "../modules/states-generator";
import type { Zone } from "../modules/zones-generator";
type TypedArray =
export type TypedArray =
| Uint8Array
| Uint16Array
| Uint32Array
@ -24,6 +25,7 @@ export interface PackedGraph {
p: [number, number][]; // cell polygon points
b: boolean[]; // cell is on border
h: TypedArray; // cell heights
q: Quadtree<[number, number, number]>; // cell quadtree index
/** Terrain type */
t: TypedArray; // cell terrain types
r: TypedArray; // river id passing through cell
@ -34,12 +36,12 @@ export interface PackedGraph {
conf: TypedArray; // cell water confidence
haven: TypedArray; // cell is a haven
g: number[]; // cell ground type
culture: number[]; // cell culture id
culture: TypedArray; // cell culture id
biome: TypedArray; // cell biome id
harbor: TypedArray; // cell harbour presence
burg: TypedArray; // cell burg id
religion: TypedArray; // cell religion id
state: number[]; // cell state id
state: TypedArray; // cell state id
area: TypedArray; // cell area
province: TypedArray; // cell province id
routes: Record<number, Record<number, number>>;

View file

@ -89,4 +89,11 @@ declare global {
var scale: number;
var changeFont: () => void;
var getFriendlyHeight: (coords: [number, number]) => string;
var addLakesInDeepDepressions: () => void;
var openNearSeaLakes: () => void;
var calculateMapCoordinates: () => void;
var calculateTemperatures: () => void;
var reGraph: () => void;
var createDefaultRuler: () => void;
var showStatistics: () => void;
}

View file

@ -1,3 +1,4 @@
import { clipPolygon } from "lineclip";
import { last } from "./arrayUtils";
import { distanceSquared } from "./functionUtils";
import { rn } from "./numberUtils";
@ -8,14 +9,12 @@ import { rand } from "./probabilityUtils";
* @param points - Array of points [[x1, y1], [x2, y2], ...]
* @param graphWidth - Width of the graph
* @param graphHeight - Height of the graph
* @param secure - Secure clipping to avoid edge artifacts
* @returns Clipped polygon points
*/
export const clipPoly = (
points: [number, number][],
graphWidth?: number,
graphHeight?: number,
secure: number = 0,
graphWidth: number,
graphHeight: number,
) => {
if (points.length < 2) return points;
if (points.some((point) => point === undefined)) {
@ -23,7 +22,7 @@ export const clipPoly = (
return points;
}
return window.polygonclip(points, [0, 0, graphWidth, graphHeight], secure);
return clipPolygon(points, [0, 0, graphWidth, graphHeight]);
};
/**
@ -375,7 +374,6 @@ export const initializePrompt = (): void => {
declare global {
interface Window {
ERROR: boolean;
polygonclip: any;
clipPoly: typeof clipPoly;
getSegmentId: typeof getSegmentId;

View file

@ -7,6 +7,7 @@ import {
type Vertices,
Voronoi,
} from "../modules/voronoi";
import type { PackedGraph } from "../types/PackedGraph";
import { createTypedArray } from "./arrayUtils";
import { rn } from "./numberUtils";
import { byId } from "./shorthands";
@ -541,7 +542,7 @@ export function* poissonDiscSampler(
* @param {number} i - The index of the packed cell
* @returns {boolean} - True if the cell is land, false otherwise
*/
export const isLand = (i: number, packedGraph: any) => {
export const isLand = (i: number, packedGraph: PackedGraph) => {
return packedGraph.cells.h[i] >= 20;
};
@ -550,7 +551,7 @@ export const isLand = (i: number, packedGraph: any) => {
* @param {number} i - The index of the packed cell
* @returns {boolean} - True if the cell is water, false otherwise
*/
export const isWater = (i: number, packedGraph: any) => {
export const isWater = (i: number, packedGraph: PackedGraph) => {
return packedGraph.cells.h[i] < 20;
};

View file

@ -1,6 +1,5 @@
import "./polyfills";
import { lerp, lim, minmax, normalize, rn } from "./numberUtils";
import "./polyfills";
window.rn = rn;
window.lim = lim;
@ -228,8 +227,8 @@ import {
wiki,
} from "./commonUtils";
window.clipPoly = (points: [number, number][], secure?: number) =>
clipPoly(points, graphWidth, graphHeight, secure);
window.clipPoly = (points: [number, number][]) =>
clipPoly(points, graphWidth, graphHeight);
window.getSegmentId = getSegmentId;
window.debounce = debounce;
window.throttle = throttle;
@ -336,9 +335,9 @@ export {
nth,
openURL,
P,
Pint,
parseError,
parseTransform,
Pint,
poissonDiscSampler,
ra,
rand,
@ -351,10 +350,10 @@ export {
shouldRegenerateGrid,
si,
splitInTwo,
TYPED_ARRAY_MAX_VALUES,
throttle,
toHEX,
trimVowels,
TYPED_ARRAY_MAX_VALUES,
unique,
wiki,
};

File diff suppressed because one or more lines are too long