From 3b74674a0956adb8b721fd1a13e9f37d6c90d26b Mon Sep 17 00:00:00 2001 From: Marc Emmanuel Date: Tue, 10 Mar 2026 11:34:32 +0100 Subject: [PATCH] refactor: resampling functionality --- package-lock.json | 14 + package.json | 2 + public/libs/lineclip.min.js | 2 - public/modules/resample.js | 384 ----------------------- public/modules/submap.js | 408 ------------------------- src/index.html | 2 - src/modules/index.ts | 1 + src/modules/ocean-layers.ts | 1 - src/modules/resample.ts | 542 +++++++++++++++++++++++++++++++++ src/modules/river-generator.ts | 10 +- src/renderers/draw-markers.ts | 8 +- src/types/PackedGraph.ts | 6 +- src/types/global.ts | 7 + src/utils/commonUtils.ts | 9 +- src/utils/graphUtils.ts | 5 +- 15 files changed, 590 insertions(+), 811 deletions(-) delete mode 100644 public/libs/lineclip.min.js delete mode 100644 public/modules/resample.js delete mode 100644 public/modules/submap.js create mode 100644 src/modules/resample.ts diff --git a/package-lock.json b/package-lock.json index 3396b8f5..e942cb93 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,9 +9,11 @@ "version": "1.113.5", "license": "MIT", "dependencies": { + "@types/lineclip": "^2.0.0", "alea": "^1.0.1", "d3": "^7.9.0", "delaunator": "^5.0.1", + "lineclip": "^2.0.0", "polylabel": "^2.0.1" }, "devDependencies": { @@ -1347,6 +1349,12 @@ "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==", + "license": "MIT" + }, "node_modules/@types/node": { "version": "25.0.10", "resolved": "https://registry.npmjs.org/@types/node/-/node-25.0.10.tgz", @@ -2093,6 +2101,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", diff --git a/package.json b/package.json index 11d2cfc7..0c00978a 100644 --- a/package.json +++ b/package.json @@ -38,9 +38,11 @@ "vitest": "^4.0.18" }, "dependencies": { + "@types/lineclip": "^2.0.0", "alea": "^1.0.1", "d3": "^7.9.0", "delaunator": "^5.0.1", + "lineclip": "^2.0.0", "polylabel": "^2.0.1" }, "engines": { diff --git a/public/libs/lineclip.min.js b/public/libs/lineclip.min.js deleted file mode 100644 index d1796476..00000000 --- a/public/libs/lineclip.min.js +++ /dev/null @@ -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;re[2]&&(n|=2),t[1]e[3]&&(n|=8),n} \ No newline at end of file diff --git a/public/modules/resample.js b/public/modules/resample.js deleted file mode 100644 index b64dde1f..00000000 --- a/public/modules/resample.js +++ /dev/null @@ -1,384 +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: deepCopy(grid), pack: deepCopy(pack), notes: deepCopy(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); - - grid.points.forEach(([x, y], newGridCell) => { - const [parentX, parentY] = inverse(x, y); - const parentPackCell = parentMap.pack.cells.q.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) { - pack.features.forEach(feature => { - if (!feature) return; - const [x, y] = pack.cells.p[feature.firstCell]; - const [parentX, parentY] = inverse(x, y); - const parentCell = parentMap.pack.cells.q.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}; -})(); diff --git a/public/modules/submap.js b/public/modules/submap.js deleted file mode 100644 index 912daafb..00000000 --- a/public/modules/submap.js +++ /dev/null @@ -1,408 +0,0 @@ -"use strict"; - -window.Submap = (function () { - const isWater = (pack, id) => pack.cells.h[id] < 20; - const inMap = (x, y) => x > 0 && x < graphWidth && y > 0 && y < graphHeight; - - /* - generate new map based on an existing one (resampling parentMap) - parentMap: {seed, grid, pack} from original map - options = { - projection: f(Number,Number)->[Number, Number] - function to calculate new coordinates - inverse: g(Number,Number)->[Number, Number] - inverse of f - depressRivers: Bool carve out riverbeds? - smoothHeightMap: Bool run smooth filter on heights - addLakesInDepressions: call FMG original funtion on heightmap - - lockMarkers: Bool Auto lock all copied markers - lockBurgs: Bool Auto lock all copied burgs - } - */ - function resample(parentMap, options) { - const projection = options.projection; - const inverse = options.inverse; - const stage = s => INFO && console.info("SUBMAP:", s); - const timeStart = performance.now(); - invokeActiveZooming(); - - // copy seed - seed = parentMap.seed; - Math.random = aleaPRNG(seed); - INFO && console.group("SubMap with seed: " + seed); - - applyGraphSize(); - grid = generateGrid(); - - drawScaleBar(scaleBar, scale); - fitScaleBar(scaleBar, svgWidth, svgHeight); - - const resampler = (points, qtree, f) => { - for (const [i, [x, y]] of points.entries()) { - const [tx, ty] = inverse(x, y); - const oldid = qtree.find(tx, ty, Infinity)[2]; - f(i, oldid); - } - }; - - stage("Resampling heightmap, temperature and precipitation"); - // resample heightmap from old WorldState - const n = grid.points.length; - grid.cells.h = new Uint8Array(n); // heightmap - grid.cells.temp = new Int8Array(n); // temperature - grid.cells.prec = new Uint8Array(n); // precipitation - const reverseGridMap = new Uint32Array(n); // cellmap from new -> oldcell - - const oldGrid = parentMap.grid; - // build cache old -> [newcelllist] - const forwardGridMap = parentMap.grid.points.map(_ => []); - resampler(grid.points, parentMap.pack.cells.q, (id, oldid) => { - const cid = parentMap.pack.cells.g[oldid]; - grid.cells.h[id] = oldGrid.cells.h[cid]; - grid.cells.temp[id] = oldGrid.cells.temp[cid]; - grid.cells.prec[id] = oldGrid.cells.prec[cid]; - if (options.depressRivers) forwardGridMap[cid].push(id); - reverseGridMap[id] = cid; - }); - // TODO: add smooth/noise function for h, temp, prec n times - - // smooth heightmap - // smoothing should never change cell type (land->water or water->land) - - if (options.smoothHeightMap) { - const gcells = grid.cells; - gcells.h.forEach((h, i) => { - const hs = gcells.c[i].map(c => gcells.h[c]); - hs.push(h); - gcells.h[i] = h >= 20 ? Math.max(d3.mean(hs), 20) : Math.min(d3.mean(hs), 19); - }); - } - - if (options.depressRivers) { - stage("Generating riverbeds"); - const rbeds = new Uint16Array(grid.cells.i.length); - - // and erode riverbeds - parentMap.pack.rivers.forEach(r => - r.cells.forEach(oldpc => { - if (oldpc < 0) return; // ignore out-of-map marker (-1) - const oldc = parentMap.pack.cells.g[oldpc]; - const targetCells = forwardGridMap[oldc]; - if (!targetCells) throw "TargetCell shouldn't be empty"; - targetCells.forEach(c => { - if (grid.cells.h[c] < 20) return; - rbeds[c] = 1; - }); - }) - ); - // raise every land cell a bit except riverbeds - grid.cells.h.forEach((h, i) => { - if (rbeds[i] || h < 20) return; - grid.cells.h[i] = Math.min(h + 2, 100); - }); - } - - stage("Detect features, ocean and generating lakes"); - Features.markupGrid(); - - addLakesInDeepDepressions(); - openNearSeaLakes(); - - OceanLayers(); - - calculateMapCoordinates(); - calculateTemperatures(); - generatePrecipitation(); - stage("Cell cleanup"); - reGraph(); - - // remove misclassified cells - stage("Define coastline"); - Features.markupPack(); - createDefaultRuler(); - - // Packed Graph - const oldCells = parentMap.pack.cells; - const forwardMap = parentMap.pack.cells.p.map(_ => []); // old -> [newcelllist] - - const pn = pack.cells.i.length; - const cells = pack.cells; - cells.culture = new Uint16Array(pn); - cells.state = new Uint16Array(pn); - cells.burg = new Uint16Array(pn); - cells.religion = new Uint16Array(pn); - cells.province = new Uint16Array(pn); - - stage("Resampling culture, state and religion map"); - for (const [id, gridCellId] of cells.g.entries()) { - const oldGridId = reverseGridMap[gridCellId]; - if (oldGridId === undefined) { - console.error("Can not find old cell id", reverseGridMap, "in", gridCellId); - continue; - } - // find old parent's children - const oldChildren = oldCells.i.filter(oid => oldCells.g[oid] == oldGridId); - let oldid; // matching cell on the original map - - if (!oldChildren.length) { - // it *must* be a (deleted) deep ocean cell - if (!oldGrid.cells.h[oldGridId] < 20) { - console.error(`Warning, ${gridCellId} should be water cell, not ${oldGrid.cells.h[oldGridId]}`); - continue; - } - // find replacement: closest water cell - const [ox, oy] = cells.p[id]; - const [tx, ty] = inverse(x, y); - oldid = oldCells.q.find(tx, ty, Infinity)[2]; - if (!oldid) { - console.warn("Warning, no id found in quad", id, "parent", gridCellId); - continue; - } - } else { - // find closest children (packcell) on the parent map - const distance = x => (x[0] - cells.p[id][0]) ** 2 + (x[1] - cells.p[id][1]) ** 2; - let d = Infinity; - oldChildren.forEach(oid => { - // this should be always true, unless some algo modded the height! - if (isWater(parentMap.pack, oid) !== isWater(pack, id)) { - console.warn(`cell sank because of addLakesInDepressions: ${oid}`); - } - const [oldpx, oldpy] = oldCells.p[oid]; - const nd = distance(projection(oldpx, oldpy)); - if (isNaN(nd)) { - console.error("Distance is not a number!", "Old point:", oldpx, oldpy); - } - if (nd < d) [d, oldid] = [nd, oid]; - }); - if (oldid === undefined) { - console.warn("Warning, no match for", id, "(parent:", gridCellId, ")"); - continue; - } - } - - if (isWater(pack, id) !== isWater(parentMap.pack, oldid)) { - WARN && console.warn("Type discrepancy detected:", id, oldid, `${pack.cells.t[id]} != ${oldCells.t[oldid]}`); - } - - cells.culture[id] = oldCells.culture[oldid]; - cells.state[id] = oldCells.state[oldid]; - cells.religion[id] = oldCells.religion[oldid]; - cells.province[id] = oldCells.province[oldid]; - // reverseMap.set(id, oldid) - forwardMap[oldid].push(id); - } - - stage("Regenerating river network"); - Rivers.generate(); - - // biome calculation based on (resampled) grid.cells.temp and prec - // it's safe to recalculate. - stage("Regenerating Biome"); - Biomes.define(); - Features.defineGroups(); - // recalculate suitability and population - // TODO: normalize according to the base-map - rankCells(); - - stage("Porting Cultures"); - pack.cultures = parentMap.pack.cultures; - // fix culture centers - const validCultures = new Set(pack.cells.culture); - pack.cultures.forEach((c, i) => { - if (!i) return; // ignore wildlands - if (!validCultures.has(i)) { - c.removed = true; - c.center = null; - return; - } - const newCenters = forwardMap[c.center]; - c.center = newCenters.length ? newCenters[0] : pack.cells.culture.findIndex(x => x === i); - }); - - stage("Porting and locking burgs"); - copyBurgs(parentMap, projection, options); - - // transfer states, mark states without land as removed. - stage("Porting states"); - const validStates = new Set(pack.cells.state); - pack.states = parentMap.pack.states; - // keep valid states and neighbors only - pack.states.forEach((s, i) => { - if (!s.i || s.removed) return; // ignore removed and neutrals - if (!validStates.has(i)) s.removed = true; - s.neighbors = s.neighbors.filter(n => validStates.has(n)); - - // find center - s.center = pack.burgs[s.capital].cell - ? pack.burgs[s.capital].cell // capital is the best bet - : pack.cells.state.findIndex(x => x === i); // otherwise use the first valid cell - }); - States.getPoles(); - - // transfer provinces, mark provinces without land as removed. - stage("Porting provinces"); - const validProvinces = new Set(pack.cells.province); - pack.provinces = parentMap.pack.provinces; - // mark uneccesary provinces - pack.provinces.forEach((p, i) => { - if (!p || p.removed) return; - if (!validProvinces.has(i)) { - p.removed = true; - return; - } - const newCenters = forwardMap[p.center]; - p.center = newCenters.length ? newCenters[0] : pack.cells.province.findIndex(x => x === i); - }); - Provinces.getPoles(); - - stage("Regenerating routes network"); - regenerateRoutes(); - - Rivers.specify(); - Lakes.defineNames(); - - stage("Porting military"); - for (const s of pack.states) { - if (!s.military) continue; - for (const m of s.military) { - [m.x, m.y] = projection(m.x, m.y); - [m.bx, m.by] = projection(m.bx, m.by); - const cc = forwardMap[m.cell]; - m.cell = cc && cc.length ? cc[0] : null; - } - s.military = s.military.filter(m => m.cell).map((m, i) => ({...m, i})); - } - - stage("Copying markers"); - for (const m of pack.markers) { - const [x, y] = projection(m.x, m.y); - if (!inMap(x, y)) { - Markers.deleteMarker(m.i); - } else { - m.x = x; - m.y = y; - m.cell = findCell(x, y); - if (options.lockMarkers) m.lock = true; - } - } - if (layerIsOn("toggleMarkers")) drawMarkers(); - - stage("Regenerating Zones"); - Zones.generate(); - Names.getMapName(); - stage("Restoring Notes"); - notes = parentMap.notes; - stage("Submap done"); - - WARN && console.warn(`TOTAL: ${rn((performance.now() - timeStart) / 1000, 2)}s`); - showStatistics(); - INFO && console.groupEnd("Generated Map " + seed); - } - - /* find the nearest cell accepted by filter f *and* having at - * least one *neighbor* fulfilling filter g, up to cell-distance `max` - * returns [cellid, neighbor] tuple or undefined if no such cell. - * accepts coordinates (x, y) - */ - const findNearest = - (f, g, max = 3) => - (px, py) => { - const d2 = c => (px - pack.cells.p[c][0]) ** 2 + (py - pack.cells.p[c][0]) ** 2; - const startCell = findCell(px, py); - const tested = new Set([startCell]); // ignore analyzed cells - const kernel = (cs, level) => { - const [bestf, bestg] = cs.filter(f).reduce( - ([cf, cg], c) => { - const neighbors = pack.cells.c[c]; - const betterg = neighbors.filter(g).reduce((u, x) => (d2(x) < d2(u) ? x : u)); - if (cf === undefined) return [c, betterg]; - return betterg && d2(cf) < d2(c) ? [c, betterg] : [cf, cg]; - }, - [undefined, undefined] - ); - if (bestf && bestg) return [bestf, bestg]; - - // no suitable pair found, retry with next ring - const targets = new Set(cs.map(c => pack.cells.c[c]).flat()); - const ring = Array.from(targets).filter(nc => !tested.has(nc)); - if (level >= max || !ring.length) return [undefined, undefined]; - ring.forEach(c => tested.add(c)); - return kernel(ring, level + 1); - }; - const pair = kernel([startCell], 1); - return pair; - }; - - function copyBurgs(parentMap, projection, options) { - const cells = pack.cells; - pack.burgs = parentMap.pack.burgs; - - // remap burgs to the best new cell - pack.burgs.forEach((b, id) => { - if (id == 0) return; // skip empty city of neturals - [b.x, b.y] = projection(b.x, b.y); - b.population = b.population * options.scale; // adjust for populationRate change - - // disable out-of-map (removed) burgs - if (!inMap(b.x, b.y)) { - b.removed = true; - b.cell = null; - return; - } - - const cityCell = findCell(b.x, b.y); - let searchFunc; - const isFreeLand = c => cells.t[c] === 1 && !cells.burg[c]; - const nearCoast = c => cells.t[c] === -1; - - // check if we need to relocate the burg - if (cells.burg[cityCell]) - // already occupied - searchFunc = findNearest(isFreeLand, _ => true, 3); - - if (isWater(pack, cityCell) || b.port) - // burg is in water or port - searchFunc = findNearest(isFreeLand, nearCoast, 6); - - if (searchFunc) { - const [newCell, neighbor] = searchFunc(b.x, b.y); - if (!newCell) { - WARN && console.warn(`Can not relocate Burg: ${b.name} sunk and destroyed. :-(`); - b.cell = null; - b.removed = true; - return; - } - - [b.x, b.y] = b.port ? getCloseToEdgePoint(newCell, neighbor) : cells.p[newCell]; - if (b.port) b.port = cells.f[neighbor]; // copy feature number - b.cell = newCell; - if (b.port && !isWater(pack, neighbor)) console.error("betrayal! negihbor must be water!", b); - } else { - b.cell = cityCell; - } - if (b.i && !b.lock) b.lock = options.lockBurgs; - cells.burg[b.cell] = id; - }); - } - - 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]; - } - - // export - return {resample, findNearest}; -})(); diff --git a/src/index.html b/src/index.html index 6ab9cc06..dffaa816 100644 --- a/src/index.html +++ b/src/index.html @@ -8539,10 +8539,8 @@ - - diff --git a/src/modules/index.ts b/src/modules/index.ts index 787e3989..a1500144 100644 --- a/src/modules/index.ts +++ b/src/modules/index.ts @@ -18,3 +18,4 @@ import "./ice"; import "./military-generator"; import "./markers-generator"; import "./fonts"; +import "./resample"; diff --git a/src/modules/ocean-layers.ts b/src/modules/ocean-layers.ts index a18b844a..80cd522f 100644 --- a/src/modules/ocean-layers.ts +++ b/src/modules/ocean-layers.ts @@ -111,7 +111,6 @@ class OceanModule { relaxed.map((v) => this.vertices.p[v]), graphWidth, graphHeight, - 1, ); chains.push([t, points]); } diff --git a/src/modules/resample.ts b/src/modules/resample.ts new file mode 100644 index 00000000..197225d8 --- /dev/null +++ b/src/modules/resample.ts @@ -0,0 +1,542 @@ +import { mean, quadtree } from "d3"; +import { clipPolyline } from "lineclip"; +import type { PackedGraph } from "../types/PackedGraph"; +import { + deepCopy, + 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); + + grid.points.forEach(([x, y]: [number, number], newGridCell: number) => { + const [parentX, parentY] = inverse(x, y); + const parentPackCell = parentMap.pack.cells.q.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, + ); + } + + 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 = {}; + 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], + ) { + pack.features.forEach((feature) => { + if (!feature) return; + const [x, y] = pack.cells.p[feature.firstCell]; + const [parentX, parentY] = inverse(x, y); + const parentCell = parentMap.pack.cells.q.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: deepCopy(grid), + pack: deepCopy(pack), + notes: deepCopy(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(); diff --git a/src/modules/river-generator.ts b/src/modules/river-generator.ts index a953aa51..b9525bb7 100644 --- a/src/modules/river-generator.ts +++ b/src/modules/river-generator.ts @@ -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), diff --git a/src/renderers/draw-markers.ts b/src/renderers/draw-markers.ts index edc5befd..9aeba942 100644 --- a/src/renderers/draw-markers.ts +++ b/src/renderers/draw-markers.ts @@ -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; diff --git a/src/types/PackedGraph.ts b/src/types/PackedGraph.ts index b8749f0a..de02b708 100644 --- a/src/types/PackedGraph.ts +++ b/src/types/PackedGraph.ts @@ -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"; @@ -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>; diff --git a/src/types/global.ts b/src/types/global.ts index 371dd583..efc45d7c 100644 --- a/src/types/global.ts +++ b/src/types/global.ts @@ -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; } diff --git a/src/utils/commonUtils.ts b/src/utils/commonUtils.ts index 6808eb11..8be12150 100644 --- a/src/utils/commonUtils.ts +++ b/src/utils/commonUtils.ts @@ -1,3 +1,4 @@ +import { clipPolygon } from "lineclip"; import { last } from "./arrayUtils"; import { distanceSquared } from "./functionUtils"; import { rn } from "./numberUtils"; @@ -13,9 +14,8 @@ import { rand } from "./probabilityUtils"; */ 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 +23,7 @@ export const clipPoly = ( return points; } - return window.polygonclip(points, [0, 0, graphWidth, graphHeight], secure); + return clipPolygon(points, [0, 0, graphWidth, graphHeight]); }; /** @@ -372,7 +372,6 @@ export const initializePrompt = (): void => { declare global { interface Window { ERROR: boolean; - polygonclip: any; clipPoly: typeof clipPoly; getSegmentId: typeof getSegmentId; diff --git a/src/utils/graphUtils.ts b/src/utils/graphUtils.ts index 9b241780..8406b14a 100644 --- a/src/utils/graphUtils.ts +++ b/src/utils/graphUtils.ts @@ -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"; @@ -525,7 +526,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; }; @@ -534,7 +535,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; };