diff --git a/main.js b/main.js index 8c060523..bed042bb 100644 --- a/main.js +++ b/main.js @@ -1186,7 +1186,7 @@ function rankCells() { console.time('rankCells'); const cells = pack.cells, f = pack.features; cells.s = new Int16Array(cells.i.length); // cell suitability array - cells.pop = new Uint16Array(cells.i.length); // cell population array + cells.pop = new Float32Array(cells.i.length); // cell population array const flMean = d3.median(cells.fl.filter(f => f)), flMax = d3.max(cells.fl) + d3.max(cells.conf); // to normalize flux const areaMean = d3.mean(cells.area); // to adjust population by cell area diff --git a/modules/save-and-load.js b/modules/save-and-load.js index 67a1eeba..c1b01244 100644 --- a/modules/save-and-load.js +++ b/modules/save-and-load.js @@ -198,12 +198,15 @@ function getMapData() { return `${b.name}|${b.min}|${b.max}|${b.d}|${b.m}|${names}`; }).join("/"); + // round population to save resources + const pop = Array.from(pack.cells.pop).map(p => rn(p, 4)); + // data format as below const data = [params, options, coords, biomes, notesData, svg_xml, gridGeneral, grid.cells.h, grid.cells.prec, grid.cells.f, grid.cells.t, grid.cells.temp, features, cultures, states, burgs, pack.cells.biome, pack.cells.burg, pack.cells.conf, pack.cells.culture, pack.cells.fl, - pack.cells.pop, pack.cells.r, pack.cells.road, pack.cells.s, pack.cells.state, + pop, pack.cells.r, pack.cells.road, pack.cells.s, pack.cells.state, pack.cells.religion, pack.cells.province, pack.cells.crossroad, religions, provinces, namesData].join("\r\n"); const blob = new Blob([data], {type: "text/plain"}); @@ -666,7 +669,7 @@ function parseLoadedData(data) { cells.conf = Uint8Array.from(data[18].split(",")); cells.culture = Uint16Array.from(data[19].split(",")); cells.fl = Uint16Array.from(data[20].split(",")); - cells.pop = Uint16Array.from(data[21].split(",")); + cells.pop = Float32Array.from(data[21].split(",")); cells.r = Uint16Array.from(data[22].split(",")); cells.road = Uint16Array.from(data[23].split(",")); cells.s = Uint16Array.from(data[24].split(",")); diff --git a/modules/ui/burg-editor.js b/modules/ui/burg-editor.js index 402b2df2..88c7653f 100644 --- a/modules/ui/burg-editor.js +++ b/modules/ui/burg-editor.js @@ -235,7 +235,7 @@ function editBurg(id) { function changePopulation() { const id = +elSelected.attr("data-id"); - pack.burgs[id].population = burgPopulation.value / populationRate.value / urbanization.value; + pack.burgs[id].population = rn(burgPopulation.value / populationRate.value / urbanization.value, 4); } function toggleFeature() { diff --git a/modules/ui/cultures-editor.js b/modules/ui/cultures-editor.js index 7c27a9fc..4d696e68 100644 --- a/modules/ui/cultures-editor.js +++ b/modules/ui/cultures-editor.js @@ -237,7 +237,7 @@ function editCultures() { }); function applyPopulationChange() { - const ruralChange = rn(ruralPop.value / rural, 4); + const ruralChange = ruralPop.value / rural; if (isFinite(ruralChange) && ruralChange !== 1) { const cells = pack.cells.i.filter(i => pack.cells.culture[i] === culture); cells.forEach(i => pack.cells.pop[i] *= ruralChange); @@ -249,13 +249,13 @@ function editCultures() { cells.forEach(i => pack.cells.pop[i] = pop); } - const urbanChange = rn(urbanPop.value / urban, 4); + const urbanChange = urbanPop.value / urban; if (isFinite(urbanChange) && urbanChange !== 1) { - burgs.forEach(b => b.population *= urbanChange); + burgs.forEach(b => b.population = rn(b.population * urbanChange, 4)); } if (!isFinite(urbanChange) && +urbanPop.value > 0) { const points = urbanPop.value / populationRate.value / urbanization.value; - const population = rn(points / burgs.length); + const population = rn(points / burgs.length, 4); burgs.forEach(b => b.population = population); } diff --git a/modules/ui/heightmap-editor.js b/modules/ui/heightmap-editor.js index 9c1c321f..02a146fd 100644 --- a/modules/ui/heightmap-editor.js +++ b/modules/ui/heightmap-editor.js @@ -263,7 +263,7 @@ function getHeight(h) { // assign saved pack data from grid back to pack const n = pack.cells.i.length; - pack.cells.pop = new Uint16Array(n); + pack.cells.pop = new Float32Array(n); pack.cells.road = new Uint16Array(n); pack.cells.crossroad = new Uint16Array(n); pack.cells.s = new Uint16Array(n); diff --git a/modules/ui/provinces-editor.js b/modules/ui/provinces-editor.js index 6c313623..80b57343 100644 --- a/modules/ui/provinces-editor.js +++ b/modules/ui/provinces-editor.js @@ -114,9 +114,9 @@ function editProvinces() { const focused = defs.select("#fog #focusProvince"+p.i).size(); lines += `
- + - + @@ -305,7 +305,7 @@ function editProvinces() { }); function applyPopulationChange() { - const ruralChange = rn(ruralPop.value / rural, 4); + const ruralChange = ruralPop.value / rural; if (isFinite(ruralChange) && ruralChange !== 1) { cells.forEach(i => pack.cells.pop[i] *= ruralChange); } @@ -315,13 +315,13 @@ function editProvinces() { cells.forEach(i => pack.cells.pop[i] = pop); } - const urbanChange = rn(urbanPop.value / urban, 4); + const urbanChange = urbanPop.value / urban; if (isFinite(urbanChange) && urbanChange !== 1) { - p.burgs.forEach(b => pack.burgs[b].population *= urbanChange); + p.burgs.forEach(b => pack.burgs[b].population = rn(pack.burgs[b].population * urbanChange, 4)); } if (!isFinite(urbanChange) && +urbanPop.value > 0) { const points = urbanPop.value / populationRate.value / urbanization.value; - const population = rn(points / burgs.length); + const population = rn(points / burgs.length, 4); p.burgs.forEach(b => pack.burgs[b].population = population); } diff --git a/modules/ui/religions-editor.js b/modules/ui/religions-editor.js index 7a24b978..f29503db 100644 --- a/modules/ui/religions-editor.js +++ b/modules/ui/religions-editor.js @@ -263,7 +263,7 @@ function editReligions() { }); function applyPopulationChange() { - const ruralChange = rn(ruralPop.value / rural, 4); + const ruralChange = ruralPop.value / rural; if (isFinite(ruralChange) && ruralChange !== 1) { const cells = pack.cells.i.filter(i => pack.cells.religion[i] === religion); cells.forEach(i => pack.cells.pop[i] *= ruralChange); @@ -275,13 +275,13 @@ function editReligions() { cells.forEach(i => pack.cells.pop[i] = pop); } - const urbanChange = rn(urbanPop.value / urban, 4); + const urbanChange = urbanPop.value / urban; if (isFinite(urbanChange) && urbanChange !== 1) { - burgs.forEach(b => b.population *= urbanChange); + burgs.forEach(b => b.population = rn(b.population * urbanChange, 4)); } if (!isFinite(urbanChange) && +urbanPop.value > 0) { const points = urbanPop.value / populationRate.value / urbanization.value; - const population = rn(points / burgs.length); + const population = rn(points / burgs.length, 4); burgs.forEach(b => b.population = population); } diff --git a/modules/ui/states-editor.js b/modules/ui/states-editor.js index 8cfe82db..632f4a75 100644 --- a/modules/ui/states-editor.js +++ b/modules/ui/states-editor.js @@ -331,7 +331,7 @@ function editStates() { }); function applyPopulationChange() { - const ruralChange = rn(ruralPop.value / rural, 4); + const ruralChange = ruralPop.value / rural; if (isFinite(ruralChange) && ruralChange !== 1) { const cells = pack.cells.i.filter(i => pack.cells.state[i] === state); cells.forEach(i => pack.cells.pop[i] *= ruralChange); @@ -339,19 +339,19 @@ function editStates() { if (!isFinite(ruralChange) && +ruralPop.value > 0) { const points = ruralPop.value / populationRate.value; const cells = pack.cells.i.filter(i => pack.cells.state[i] === state); - const pop = rn(points / cells.length); + const pop = points / cells.length; cells.forEach(i => pack.cells.pop[i] = pop); } - const urbanChange = rn(urbanPop.value / urban, 4); + const urbanChange = urbanPop.value / urban; if (isFinite(urbanChange) && urbanChange !== 1) { const burgs = pack.burgs.filter(b => !b.removed && b.state === state); - burgs.forEach(b => b.population *= urbanChange); + burgs.forEach(b => b.population = rn(b.population * urbanChange, 4)); } if (!isFinite(urbanChange) && +urbanPop.value > 0) { const points = urbanPop.value / populationRate.value / urbanization.value; const burgs = pack.burgs.filter(b => !b.removed && b.state === state); - const population = rn(points / burgs.length); + const population = rn(points / burgs.length, 4); burgs.forEach(b => b.population = population); } diff --git a/modules/ui/zones-editor.js b/modules/ui/zones-editor.js index 663c25fb..3406f702 100644 --- a/modules/ui/zones-editor.js +++ b/modules/ui/zones-editor.js @@ -393,7 +393,7 @@ function editZones() { }); function applyPopulationChange() { - const ruralChange = rn(ruralPop.value / rural, 4); + const ruralChange = ruralPop.value / rural; if (isFinite(ruralChange) && ruralChange !== 1) { cells.forEach(i => pack.cells.pop[i] *= ruralChange); } @@ -403,13 +403,13 @@ function editZones() { cells.forEach(i => pack.cells.pop[i] = pop); } - const urbanChange = rn(urbanPop.value / urban, 4); + const urbanChange = urbanPop.value / urban; if (isFinite(urbanChange) && urbanChange !== 1) { - burgs.forEach(b => b.population *= urbanChange); + burgs.forEach(b => b.population = rn(b.population * urbanChange, 4)); } if (!isFinite(urbanChange) && +urbanPop.value > 0) { const points = urbanPop.value / populationRate.value / urbanization.value; - const population = rn(points / burgs.length); + const population = rn(points / burgs.length, 4); burgs.forEach(b => b.population = population); }