From 1d9ba6f17ca9bf370cbb0d645a33d7889734bdd5 Mon Sep 17 00:00:00 2001
From: Azgaar
Date: Sun, 20 Oct 2024 15:53:22 +0200
Subject: [PATCH] refactor: submap - simplify options
---
index.html | 10 -------
modules/resample.js | 56 ++++++++++++------------------------
modules/ui/submap-tool.js | 5 +---
modules/ui/transform-tool.js | 3 +-
4 files changed, 21 insertions(+), 53 deletions(-)
diff --git a/index.html b/index.html
index 9e9f9729..250fafd0 100644
--- a/index.html
+++ b/index.html
@@ -5771,16 +5771,6 @@
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!
-
- Options:
-
-
- Smooth heightmap
-
-
-
- Erode riverbeds
-
diff --git a/modules/resample.js b/modules/resample.js
index e397de82..fff67266 100644
--- a/modules/resample.js
+++ b/modules/resample.js
@@ -4,21 +4,16 @@ window.Resample = (function () {
/*
generate new map based on an existing one (resampling parentMap)
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]
- inverse: f(Number, Number) -> [Number, Number]
- }
- */
- function process(parentMap, options) {
- const {projection, inverse} = options;
-
+ projection: f(Number, Number) -> [Number, Number]
+ inverse: f(Number, Number) -> [Number, Number]
+ scale: Number
+ */
+ function process({parentMap, projection, inverse, scale}) {
grid = generateGrid();
pack = {};
notes = parentMap.notes;
- resamplePrimaryGridData(parentMap, inverse);
+ resamplePrimaryGridData(parentMap, inverse, scale);
Features.markupGrid();
addLakesInDeepDepressions();
@@ -38,20 +33,19 @@ window.Resample = (function () {
rankCells();
restoreCultures(parentMap, projection);
- restoreBurgs(parentMap, projection, options);
+ restoreBurgs(parentMap, projection, scale);
restoreStates(parentMap, projection);
restoreRoutes(parentMap, projection);
restoreReligions(parentMap, projection);
restoreProvinces(parentMap);
- restoreRiverDetails();
restoreFeatureDetails(parentMap, inverse);
restoreMarkers(parentMap, projection);
- restoreZones(parentMap, projection, options);
+ restoreZones(parentMap, projection, scale);
showStatistics();
}
- function resamplePrimaryGridData(parentMap, inverse) {
+ 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);
@@ -66,8 +60,7 @@ window.Resample = (function () {
grid.cells.prec[newGridCell] = parentMap.grid.cells.prec[parentGridCell];
});
- if (options.smoothHeightmap) smoothHeightmap();
- if (options.depressRivers) depressRivers(parentMap, inverse);
+ if (scale >= 2) 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) {
pack.cells.biome = new Uint8Array(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)};
})
.filter(Boolean);
+
+ pack.rivers.forEach(river => {
+ river.basin = Rivers.getBasin(river.i);
+ river.length = Rivers.getApproximateLength(river.points);
+ });
}
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 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 *= options.scale; // adjust for populationRate change
+ 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};
@@ -285,8 +273,8 @@ window.Resample = (function () {
});
}
- function restoreZones(parentMap, projection, options) {
- const getSearchRadius = cellId => Math.sqrt(parentMap.pack.cells.area[cellId] / Math.PI) * options.scale;
+ 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
@@ -302,12 +290,6 @@ window.Resample = (function () {
});
}
- function restoreRiverDetails() {
- pack.rivers.forEach(river => {
- river.basin = Rivers.getBasin(river.i);
- });
- }
-
function restoreFeatureDetails(parentMap, inverse) {
pack.features.forEach(feature => {
if (!feature) return;
diff --git a/modules/ui/submap-tool.js b/modules/ui/submap-tool.js
index a31052a4..f2e334f4 100644
--- a/modules/ui/submap-tool.js
+++ b/modules/ui/submap-tool.js
@@ -38,15 +38,12 @@ function openSubmapTool() {
populationRate = populationRateInput.value = rn(populationRate / scale, 2);
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 inverse = (x, y) => [x / scale + x0, y / scale + y0];
- const options = {smoothHeightmap, depressRivers, projection, inverse, scale};
resetZoom(0);
undraw();
- Resample.process(parentMap, options);
+ Resample.process({parentMap, projection, inverse, scale});
rescaleBurgStyles(scale);
drawLayers();
diff --git a/modules/ui/transform-tool.js b/modules/ui/transform-tool.js
index b8915232..d610da03 100644
--- a/modules/ui/transform-tool.js
+++ b/modules/ui/transform-tool.js
@@ -128,11 +128,10 @@ async function openTransformTool() {
const parentMap = {grid: deepCopy(grid), pack: deepCopy(pack), notes: deepCopy(notes)};
const [projection, inverse] = getProjection();
- const options = {depressRivers: false, smoothHeightmap: false, scale: 1, inverse, projection};
resetZoom(0);
undraw();
- Resample.process(parentMap, options);
+ Resample.process({parentMap, projection, inverse, scale: 1});
drawLayers();
INFO && console.groupEnd("transformMap");