mirror of
https://github.com/Azgaar/Fantasy-Map-Generator.git
synced 2026-04-09 17:06:05 +02:00
refactor: resampling functionality
This commit is contained in:
parent
7a49098425
commit
3b74674a09
15 changed files with 590 additions and 811 deletions
14
package-lock.json
generated
14
package-lock.json
generated
|
|
@ -9,9 +9,11 @@
|
||||||
"version": "1.113.5",
|
"version": "1.113.5",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@types/lineclip": "^2.0.0",
|
||||||
"alea": "^1.0.1",
|
"alea": "^1.0.1",
|
||||||
"d3": "^7.9.0",
|
"d3": "^7.9.0",
|
||||||
"delaunator": "^5.0.1",
|
"delaunator": "^5.0.1",
|
||||||
|
"lineclip": "^2.0.0",
|
||||||
"polylabel": "^2.0.1"
|
"polylabel": "^2.0.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|
@ -1347,6 +1349,12 @@
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"license": "MIT"
|
"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": {
|
"node_modules/@types/node": {
|
||||||
"version": "25.0.10",
|
"version": "25.0.10",
|
||||||
"resolved": "https://registry.npmjs.org/@types/node/-/node-25.0.10.tgz",
|
"resolved": "https://registry.npmjs.org/@types/node/-/node-25.0.10.tgz",
|
||||||
|
|
@ -2093,6 +2101,12 @@
|
||||||
"node": ">=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": {
|
"node_modules/magic-string": {
|
||||||
"version": "0.30.21",
|
"version": "0.30.21",
|
||||||
"resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.30.21.tgz",
|
"resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.30.21.tgz",
|
||||||
|
|
|
||||||
|
|
@ -38,9 +38,11 @@
|
||||||
"vitest": "^4.0.18"
|
"vitest": "^4.0.18"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@types/lineclip": "^2.0.0",
|
||||||
"alea": "^1.0.1",
|
"alea": "^1.0.1",
|
||||||
"d3": "^7.9.0",
|
"d3": "^7.9.0",
|
||||||
"delaunator": "^5.0.1",
|
"delaunator": "^5.0.1",
|
||||||
|
"lineclip": "^2.0.0",
|
||||||
"polylabel": "^2.0.1"
|
"polylabel": "^2.0.1"
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
|
|
|
||||||
2
public/libs/lineclip.min.js
vendored
2
public/libs/lineclip.min.js
vendored
|
|
@ -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}
|
|
||||||
|
|
@ -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};
|
|
||||||
})();
|
|
||||||
|
|
@ -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};
|
|
||||||
})();
|
|
||||||
|
|
@ -8539,10 +8539,8 @@
|
||||||
|
|
||||||
<script defer src="config/heightmap-templates.js"></script>
|
<script defer src="config/heightmap-templates.js"></script>
|
||||||
<script defer src="config/precreated-heightmaps.js"></script>
|
<script defer src="config/precreated-heightmaps.js"></script>
|
||||||
<script defer src="modules/resample.js?v=1.112.1"></script>
|
|
||||||
<script defer src="libs/alea.min.js?v1.105.0"></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/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="libs/simplify.js?v1.105.6"></script>
|
||||||
<script defer src="modules/ui/layers.js?v=1.111.0"></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>
|
<script defer src="modules/ui/measurers.js?v=1.99.00"></script>
|
||||||
|
|
|
||||||
|
|
@ -18,3 +18,4 @@ import "./ice";
|
||||||
import "./military-generator";
|
import "./military-generator";
|
||||||
import "./markers-generator";
|
import "./markers-generator";
|
||||||
import "./fonts";
|
import "./fonts";
|
||||||
|
import "./resample";
|
||||||
|
|
|
||||||
|
|
@ -111,7 +111,6 @@ class OceanModule {
|
||||||
relaxed.map((v) => this.vertices.p[v]),
|
relaxed.map((v) => this.vertices.p[v]),
|
||||||
graphWidth,
|
graphWidth,
|
||||||
graphHeight,
|
graphHeight,
|
||||||
1,
|
|
||||||
);
|
);
|
||||||
chains.push([t, points]);
|
chains.push([t, points]);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
542
src/modules/resample.ts
Normal file
542
src/modules/resample.ts
Normal file
|
|
@ -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<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],
|
||||||
|
) {
|
||||||
|
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();
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
import Alea from "alea";
|
import Alea from "alea";
|
||||||
import { curveBasis, curveCatmullRom, line, mean, min, sum } from "d3";
|
import { curveBasis, curveCatmullRom, line, mean, min, sum } from "d3";
|
||||||
import { each, rn, round, rw } from "../utils";
|
import { each, rn, round, rw } from "../utils";
|
||||||
|
import type { Point } from "./voronoi";
|
||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
var Rivers: RiverModule;
|
var Rivers: RiverModule;
|
||||||
|
|
@ -20,6 +21,7 @@ export interface River {
|
||||||
name: string; // river name
|
name: string; // river name
|
||||||
type: string; // river type
|
type: string; // river type
|
||||||
cells: number[]; // cells forming the river path
|
cells: number[]; // cells forming the river path
|
||||||
|
points?: Point[]; // river points (for meandering)
|
||||||
}
|
}
|
||||||
|
|
||||||
class RiverModule {
|
class RiverModule {
|
||||||
|
|
@ -237,7 +239,9 @@ class RiverModule {
|
||||||
: defaultWidthFactor;
|
: defaultWidthFactor;
|
||||||
const meanderedPoints = this.addMeandering(riverCells);
|
const meanderedPoints = this.addMeandering(riverCells);
|
||||||
const discharge = cells.fl[mouth]; // m3 in second
|
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 sourceWidth = this.getSourceWidth(cells.fl[source]);
|
||||||
const width = this.getWidth(
|
const width = this.getWidth(
|
||||||
this.getOffset({
|
this.getOffset({
|
||||||
|
|
@ -411,7 +415,7 @@ class RiverModule {
|
||||||
|
|
||||||
addMeandering(
|
addMeandering(
|
||||||
riverCells: number[],
|
riverCells: number[],
|
||||||
riverPoints = null,
|
riverPoints: Point[] | null = null,
|
||||||
meandering = 0.5,
|
meandering = 0.5,
|
||||||
): [number, number, number][] {
|
): [number, number, number][] {
|
||||||
const { fl, h } = pack.cells;
|
const { fl, h } = pack.cells;
|
||||||
|
|
@ -579,7 +583,7 @@ class RiverModule {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
getApproximateLength(points: [number, number, number][]) {
|
getApproximateLength(points: Point[] = []) {
|
||||||
const length = points.reduce(
|
const length = points.reduce(
|
||||||
(s, v, i, p) =>
|
(s, v, i, p) =>
|
||||||
s + (i ? Math.hypot(v[0] - p[i - 1][0], v[1] - p[i - 1][1]) : 0),
|
s + (i ? Math.hypot(v[0] - p[i - 1][0], v[1] - p[i - 1][1]) : 0),
|
||||||
|
|
|
||||||
|
|
@ -53,7 +53,11 @@ const pinShapes: PinShapes = {
|
||||||
no: () => "",
|
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;
|
const shapeFunction = pinShapes[shape] || pinShapes.bubble;
|
||||||
return shapeFunction(fill, stroke);
|
return shapeFunction(fill, stroke);
|
||||||
};
|
};
|
||||||
|
|
@ -104,4 +108,4 @@ const markersRenderer = (): void => {
|
||||||
|
|
||||||
window.drawMarkers = markersRenderer;
|
window.drawMarkers = markersRenderer;
|
||||||
window.drawMarker = markerRenderer;
|
window.drawMarker = markerRenderer;
|
||||||
window.getPin = getPin;
|
window.getPin = getPinForShape;
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
import type { Quadtree } from "d3";
|
||||||
import type { Burg } from "../modules/burgs-generator";
|
import type { Burg } from "../modules/burgs-generator";
|
||||||
import type { Culture } from "../modules/cultures-generator";
|
import type { Culture } from "../modules/cultures-generator";
|
||||||
import type { PackedGraphFeature } from "../modules/features";
|
import type { PackedGraphFeature } from "../modules/features";
|
||||||
|
|
@ -24,6 +25,7 @@ export interface PackedGraph {
|
||||||
p: [number, number][]; // cell polygon points
|
p: [number, number][]; // cell polygon points
|
||||||
b: boolean[]; // cell is on border
|
b: boolean[]; // cell is on border
|
||||||
h: TypedArray; // cell heights
|
h: TypedArray; // cell heights
|
||||||
|
q: Quadtree<[number, number, number]>; // cell quadtree index
|
||||||
/** Terrain type */
|
/** Terrain type */
|
||||||
t: TypedArray; // cell terrain types
|
t: TypedArray; // cell terrain types
|
||||||
r: TypedArray; // river id passing through cell
|
r: TypedArray; // river id passing through cell
|
||||||
|
|
@ -34,12 +36,12 @@ export interface PackedGraph {
|
||||||
conf: TypedArray; // cell water confidence
|
conf: TypedArray; // cell water confidence
|
||||||
haven: TypedArray; // cell is a haven
|
haven: TypedArray; // cell is a haven
|
||||||
g: number[]; // cell ground type
|
g: number[]; // cell ground type
|
||||||
culture: number[]; // cell culture id
|
culture: TypedArray; // cell culture id
|
||||||
biome: TypedArray; // cell biome id
|
biome: TypedArray; // cell biome id
|
||||||
harbor: TypedArray; // cell harbour presence
|
harbor: TypedArray; // cell harbour presence
|
||||||
burg: TypedArray; // cell burg id
|
burg: TypedArray; // cell burg id
|
||||||
religion: TypedArray; // cell religion id
|
religion: TypedArray; // cell religion id
|
||||||
state: number[]; // cell state id
|
state: TypedArray; // cell state id
|
||||||
area: TypedArray; // cell area
|
area: TypedArray; // cell area
|
||||||
province: TypedArray; // cell province id
|
province: TypedArray; // cell province id
|
||||||
routes: Record<number, Record<number, number>>;
|
routes: Record<number, Record<number, number>>;
|
||||||
|
|
|
||||||
|
|
@ -89,4 +89,11 @@ declare global {
|
||||||
var scale: number;
|
var scale: number;
|
||||||
var changeFont: () => void;
|
var changeFont: () => void;
|
||||||
var getFriendlyHeight: (coords: [number, number]) => string;
|
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;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
import { clipPolygon } from "lineclip";
|
||||||
import { last } from "./arrayUtils";
|
import { last } from "./arrayUtils";
|
||||||
import { distanceSquared } from "./functionUtils";
|
import { distanceSquared } from "./functionUtils";
|
||||||
import { rn } from "./numberUtils";
|
import { rn } from "./numberUtils";
|
||||||
|
|
@ -13,9 +14,8 @@ import { rand } from "./probabilityUtils";
|
||||||
*/
|
*/
|
||||||
export const clipPoly = (
|
export const clipPoly = (
|
||||||
points: [number, number][],
|
points: [number, number][],
|
||||||
graphWidth?: number,
|
graphWidth: number,
|
||||||
graphHeight?: number,
|
graphHeight: number,
|
||||||
secure: number = 0,
|
|
||||||
) => {
|
) => {
|
||||||
if (points.length < 2) return points;
|
if (points.length < 2) return points;
|
||||||
if (points.some((point) => point === undefined)) {
|
if (points.some((point) => point === undefined)) {
|
||||||
|
|
@ -23,7 +23,7 @@ export const clipPoly = (
|
||||||
return points;
|
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 {
|
declare global {
|
||||||
interface Window {
|
interface Window {
|
||||||
ERROR: boolean;
|
ERROR: boolean;
|
||||||
polygonclip: any;
|
|
||||||
|
|
||||||
clipPoly: typeof clipPoly;
|
clipPoly: typeof clipPoly;
|
||||||
getSegmentId: typeof getSegmentId;
|
getSegmentId: typeof getSegmentId;
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ import {
|
||||||
type Vertices,
|
type Vertices,
|
||||||
Voronoi,
|
Voronoi,
|
||||||
} from "../modules/voronoi";
|
} from "../modules/voronoi";
|
||||||
|
import type { PackedGraph } from "../types/PackedGraph";
|
||||||
import { createTypedArray } from "./arrayUtils";
|
import { createTypedArray } from "./arrayUtils";
|
||||||
import { rn } from "./numberUtils";
|
import { rn } from "./numberUtils";
|
||||||
import { byId } from "./shorthands";
|
import { byId } from "./shorthands";
|
||||||
|
|
@ -525,7 +526,7 @@ export function* poissonDiscSampler(
|
||||||
* @param {number} i - The index of the packed cell
|
* @param {number} i - The index of the packed cell
|
||||||
* @returns {boolean} - True if the cell is land, false otherwise
|
* @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;
|
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
|
* @param {number} i - The index of the packed cell
|
||||||
* @returns {boolean} - True if the cell is water, false otherwise
|
* @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;
|
return packedGraph.cells.h[i] < 20;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue