refactor: submap - simplify options

This commit is contained in:
Azgaar 2024-10-20 15:53:22 +02:00
parent 72be5f8220
commit 1d9ba6f17c
4 changed files with 21 additions and 53 deletions

View file

@ -5771,16 +5771,6 @@
This operation is destructive and irreversible. It will create a completely new map based on the current one. This operation is destructive and irreversible. It will create a completely new map based on the current one.
Don't forget to save the .map file to your machine first! Don't forget to save the .map file to your machine first!
</p> </p>
<p>Options:</p>
<div data-tip="Smooth heightmap to get more natural terrain">
<input id="submapSmoothHeightmap" class="checkbox" type="checkbox" checked />
<label for="submapSmoothHeightmap" class="checkbox-label">Smooth heightmap</label>
</div>
<div data-tip="Rivers will erode land (helps to get more similar river network)">
<input id="submapDepressRivers" class="checkbox" type="checkbox" checked />
<label for="submapDepressRivers" class="checkbox-label">Erode riverbeds</label>
</div>
</div> </div>
<div id="transformTool" style="display: none" class="dialog"> <div id="transformTool" style="display: none" class="dialog">

View file

@ -4,21 +4,16 @@ window.Resample = (function () {
/* /*
generate new map based on an existing one (resampling parentMap) generate new map based on an existing one (resampling parentMap)
parentMap: {grid, pack, notes} from original map parentMap: {grid, pack, notes} from original map
options = {
smoothHeightmap: Bool; run smooth filter on heights
depressRivers: Bool; lower elevation of riverbed cells
projection: f(Number, Number) -> [Number, Number] projection: f(Number, Number) -> [Number, Number]
inverse: f(Number, Number) -> [Number, Number] inverse: f(Number, Number) -> [Number, Number]
} scale: Number
*/ */
function process(parentMap, options) { function process({parentMap, projection, inverse, scale}) {
const {projection, inverse} = options;
grid = generateGrid(); grid = generateGrid();
pack = {}; pack = {};
notes = parentMap.notes; notes = parentMap.notes;
resamplePrimaryGridData(parentMap, inverse); resamplePrimaryGridData(parentMap, inverse, scale);
Features.markupGrid(); Features.markupGrid();
addLakesInDeepDepressions(); addLakesInDeepDepressions();
@ -38,20 +33,19 @@ window.Resample = (function () {
rankCells(); rankCells();
restoreCultures(parentMap, projection); restoreCultures(parentMap, projection);
restoreBurgs(parentMap, projection, options); restoreBurgs(parentMap, projection, scale);
restoreStates(parentMap, projection); restoreStates(parentMap, projection);
restoreRoutes(parentMap, projection); restoreRoutes(parentMap, projection);
restoreReligions(parentMap, projection); restoreReligions(parentMap, projection);
restoreProvinces(parentMap); restoreProvinces(parentMap);
restoreRiverDetails();
restoreFeatureDetails(parentMap, inverse); restoreFeatureDetails(parentMap, inverse);
restoreMarkers(parentMap, projection); restoreMarkers(parentMap, projection);
restoreZones(parentMap, projection, options); restoreZones(parentMap, projection, scale);
showStatistics(); showStatistics();
} }
function resamplePrimaryGridData(parentMap, inverse) { function resamplePrimaryGridData(parentMap, inverse, scale) {
grid.cells.h = new Uint8Array(grid.points.length); grid.cells.h = new Uint8Array(grid.points.length);
grid.cells.temp = new Int8Array(grid.points.length); grid.cells.temp = new Int8Array(grid.points.length);
grid.cells.prec = new Uint8Array(grid.points.length); grid.cells.prec = new Uint8Array(grid.points.length);
@ -66,8 +60,7 @@ window.Resample = (function () {
grid.cells.prec[newGridCell] = parentMap.grid.cells.prec[parentGridCell]; grid.cells.prec[newGridCell] = parentMap.grid.cells.prec[parentGridCell];
}); });
if (options.smoothHeightmap) smoothHeightmap(); if (scale >= 2) smoothHeightmap();
if (options.depressRivers) depressRivers(parentMap, inverse);
} }
function smoothHeightmap() { function smoothHeightmap() {
@ -78,16 +71,6 @@ window.Resample = (function () {
}); });
} }
function depressRivers(parentMap, inverse) {
// lower elevation of cells with rivers by 1
grid.cells.points.forEach(([x, y], newGridCell) => {
const [parentX, parentY] = inverse(x, y);
const parentPackCell = parentMap.pack.cells.q.find(parentX, parentY, Infinity)[2];
const hasRiver = Boolean(parentMap.pack.cells.r[parentPackCell]);
if (hasRiver && grid.cells.h[newGridCell] > 20) grid.cells.h[newGridCell] -= 1;
});
}
function restoreCellData(parentMap, inverse) { function restoreCellData(parentMap, inverse) {
pack.cells.biome = new Uint8Array(pack.cells.i.length); pack.cells.biome = new Uint8Array(pack.cells.i.length);
pack.cells.fl = new Uint16Array(pack.cells.i.length); pack.cells.fl = new Uint16Array(pack.cells.i.length);
@ -138,6 +121,11 @@ window.Resample = (function () {
return {...river, cells, points, source: cells.at(0), mouth: cells.at(-2)}; return {...river, cells, points, source: cells.at(0), mouth: cells.at(-2)};
}) })
.filter(Boolean); .filter(Boolean);
pack.rivers.forEach(river => {
river.basin = Rivers.getBasin(river.i);
river.length = Rivers.getApproximateLength(river.points);
});
} }
function restoreCultures(parentMap, projection) { function restoreCultures(parentMap, projection) {
@ -155,13 +143,13 @@ window.Resample = (function () {
}); });
} }
function restoreBurgs(parentMap, projection, options) { function restoreBurgs(parentMap, projection, scale) {
const packLandCellsQuadtree = d3.quadtree(groupCellsByType(pack).land); const packLandCellsQuadtree = d3.quadtree(groupCellsByType(pack).land);
const findLandCell = (x, y) => packLandCellsQuadtree.find(x, y, Infinity)?.[2]; const findLandCell = (x, y) => packLandCellsQuadtree.find(x, y, Infinity)?.[2];
pack.burgs = parentMap.pack.burgs.map(burg => { pack.burgs = parentMap.pack.burgs.map(burg => {
if (!burg.i || burg.removed) return burg; if (!burg.i || burg.removed) return burg;
burg.population *= options.scale; // adjust for populationRate change burg.population *= scale; // adjust for populationRate change
const [xp, yp] = projection(burg.x, burg.y); const [xp, yp] = projection(burg.x, burg.y);
if (!isInMap(xp, yp)) return {...burg, removed: true, lock: false}; if (!isInMap(xp, yp)) return {...burg, removed: true, lock: false};
@ -285,8 +273,8 @@ window.Resample = (function () {
}); });
} }
function restoreZones(parentMap, projection, options) { function restoreZones(parentMap, projection, scale) {
const getSearchRadius = cellId => Math.sqrt(parentMap.pack.cells.area[cellId] / Math.PI) * options.scale; const getSearchRadius = cellId => Math.sqrt(parentMap.pack.cells.area[cellId] / Math.PI) * scale;
pack.zones = parentMap.pack.zones.map(zone => { pack.zones = parentMap.pack.zones.map(zone => {
const cells = zone.cells const cells = zone.cells
@ -302,12 +290,6 @@ window.Resample = (function () {
}); });
} }
function restoreRiverDetails() {
pack.rivers.forEach(river => {
river.basin = Rivers.getBasin(river.i);
});
}
function restoreFeatureDetails(parentMap, inverse) { function restoreFeatureDetails(parentMap, inverse) {
pack.features.forEach(feature => { pack.features.forEach(feature => {
if (!feature) return; if (!feature) return;

View file

@ -38,15 +38,12 @@ function openSubmapTool() {
populationRate = populationRateInput.value = rn(populationRate / scale, 2); populationRate = populationRateInput.value = rn(populationRate / scale, 2);
const parentMap = {grid: deepCopy(grid), pack: deepCopy(pack), notes: deepCopy(notes)}; const parentMap = {grid: deepCopy(grid), pack: deepCopy(pack), notes: deepCopy(notes)};
const smoothHeightmap = byId("submapSmoothHeightmap").checked;
const depressRivers = byId("submapDepressRivers").checked;
const projection = (x, y) => [(x - x0) * scale, (y - y0) * scale]; const projection = (x, y) => [(x - x0) * scale, (y - y0) * scale];
const inverse = (x, y) => [x / scale + x0, y / scale + y0]; const inverse = (x, y) => [x / scale + x0, y / scale + y0];
const options = {smoothHeightmap, depressRivers, projection, inverse, scale};
resetZoom(0); resetZoom(0);
undraw(); undraw();
Resample.process(parentMap, options); Resample.process({parentMap, projection, inverse, scale});
rescaleBurgStyles(scale); rescaleBurgStyles(scale);
drawLayers(); drawLayers();

View file

@ -128,11 +128,10 @@ async function openTransformTool() {
const parentMap = {grid: deepCopy(grid), pack: deepCopy(pack), notes: deepCopy(notes)}; const parentMap = {grid: deepCopy(grid), pack: deepCopy(pack), notes: deepCopy(notes)};
const [projection, inverse] = getProjection(); const [projection, inverse] = getProjection();
const options = {depressRivers: false, smoothHeightmap: false, scale: 1, inverse, projection};
resetZoom(0); resetZoom(0);
undraw(); undraw();
Resample.process(parentMap, options); Resample.process({parentMap, projection, inverse, scale: 1});
drawLayers(); drawLayers();
INFO && console.groupEnd("transformMap"); INFO && console.groupEnd("transformMap");