From 006ab7f32954912d4157736907bb165ccdd9e599 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A9sz=C3=A1ros=20Gergely?= Date: Tue, 14 Sep 2021 00:52:06 +0200 Subject: [PATCH] Auto-Smoothing,dist fix --- modules/submap.js | 50 +++++++++++++++++++++++++------------------- modules/ui/submap.js | 21 ++++++++++--------- 2 files changed, 40 insertions(+), 31 deletions(-) diff --git a/modules/submap.js b/modules/submap.js index f3eee5e8..d3af502e 100644 --- a/modules/submap.js +++ b/modules/submap.js @@ -57,14 +57,17 @@ window.Submap = (function () { // smooth heightmap // smoothing never should change cell type (land->water or water->land) - 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.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.") @@ -84,7 +87,6 @@ window.Submap = (function () { }); }) ); - console.log("rbed stats: ", rbeds.filter(x=>x).length, rbeds.length) // raise every land cell a bit except riverbeds grid.cells.h.forEach((h, i) => { if (rbeds[i] || h<20) return; @@ -137,10 +139,7 @@ window.Submap = (function () { for(const [id, gridCellId] of cells.g.entries()) { const oldGridId = reverseGridMap[gridCellId]; - if (!oldGridId) { - console.error("oldgridid must be defined for", gridCellId, reverseGridMap); - throw(new Error("oldgridid")) - } + if (!oldGridId) throw new Error("Old grid Id must be defined!") // find old parent's children const oldChildren = oldCells.i.filter(oid=>oldCells.g[oid]==oldGridId); const isWater = x => x < 1? true: false; @@ -174,8 +173,11 @@ window.Submap = (function () { "newheight", grid.cells.h[cells.g[id]]) throw new Error("should be the same type") } - - const nd = distance(oldCells.p[oid]); + const [oldpx, oldpy]= oldCells.p[oid]; + const nd = distance(projection(oldpx, oldpx, false)); + if (!nd) { + console.error("no distance!", nd, "old point", oldp) + } if (nd < d) [d, oldid] = [nd, oid]; }) if (!oldid) { @@ -185,7 +187,6 @@ window.Submap = (function () { } if (isWater(cells.t[id]) !== isWater(oldCells.t[oldid])) { - // fix missmaped cell: water instead of land or vice versa WARN && console.warn('Type discrepancy detected:', id, oldid, `${pack.cells.t[id]} != ${oldCells.t[oldid]}`); } @@ -227,13 +228,13 @@ window.Submap = (function () { // Cultures.expand(); // transfer states, mark states without land as removed. - const validStates = new Set(pack.cells.state); 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.removed) return; - if (!validStates.has(i)) s.removed=true; + if (!validStates.has(i)) s.removed = true; s.neighbors = s.neighbors.filter(n => validStates.has(n)); }); @@ -242,6 +243,16 @@ window.Submap = (function () { (a,c,i) => c === -1 && !cells.state[i] ? a.push(i) && a: a, [] ); + // 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.states.forEach((s, i) => { + if (s.removed) return; + if (!validProvinces.has(i)) s.removed = true; + }); + // BurgsAndStates.generate(); // Religions.generate(); // BurgsAndStates.defineStateForms(); @@ -255,9 +266,6 @@ window.Submap = (function () { stage("Regenerating road network."); Routes.regenerate(); - stage("Regenerating provinces."); - BurgsAndStates.generateProvinces(); - drawStates(); drawBorders(); BurgsAndStates.drawStateLabels(); diff --git a/modules/ui/submap.js b/modules/ui/submap.js index 4dd62260..ac0d1d45 100644 --- a/modules/ui/submap.js +++ b/modules/ui/submap.js @@ -20,7 +20,7 @@ const generateSubmap = debounce(async function () { WARN && console.warn("Resampling current map"); closeDialogs("#worldConfigurator, #options3d"); const checked = id => Boolean(document.getElementById(id).checked) - const settings = { + const options = { promoteTown: checked("submapPromoteTown"), depressRivers: checked("submapDepressRivers"), copyBurgs: checked("submapCopyBurgs"), @@ -28,6 +28,7 @@ const generateSubmap = debounce(async function () { addMilitary: checked("submapAddMilitary"), addMarkers: checked("submapAddMarkers"), addZones: checked("submapAddZones"), + smoothHeightMap: scale > 2, } // Create projection func from current zoom extents @@ -64,9 +65,9 @@ const generateSubmap = debounce(async function () { }; try { - await Submap.resample(oldstate, projection, settings); + await Submap.resample(oldstate, projection, options); } catch (error) { - generateSubmapErrorHandler(error); + generateSubmapErrorHandler(error, oldstate, projection, options); } oldstate = null; // destroy old state to free memory @@ -76,7 +77,7 @@ const generateSubmap = debounce(async function () { if ($("#worldConfigurator").is(":visible")) editWorld(); }, 1000); -function generateSubmapErrorHandler(error) { +function generateSubmapErrorHandler(error, oldstate, projection, options) { ERROR && console.error(error); clearMainTip(); @@ -88,12 +89,12 @@ function generateSubmapErrorHandler(error) { title: "Generation error", width: "32em", buttons: { - "Clear data": function () { - localStorage.clear(); - localStorage.setItem("version", version); - }, - Regenerate: function () { - generateSubmap(); + Regenerate: async function () { + try { + await Submap.resample(oldstate, projection, options); + } catch (error) { + generateSubmapErrorHandler(error, oldstate, projection, options); + } $(this).dialog("close"); }, Ignore: function () {