Auto-Smoothing,dist fix

This commit is contained in:
Mészáros Gergely 2021-09-14 00:52:06 +02:00
parent d2bd611412
commit 006ab7f329
2 changed files with 40 additions and 31 deletions

View file

@ -57,6 +57,8 @@ window.Submap = (function () {
// smooth heightmap // smooth heightmap
// smoothing never should change cell type (land->water or water->land) // smoothing never should change cell type (land->water or water->land)
if (options.smoothHeightMap) {
const gcells = grid.cells; const gcells = grid.cells;
gcells.h.forEach((h,i) => { gcells.h.forEach((h,i) => {
const hs = gcells.c[i].map(c=>gcells.h[c]) const hs = gcells.c[i].map(c=>gcells.h[c])
@ -65,6 +67,7 @@ window.Submap = (function () {
? Math.max(d3.mean(hs),20) ? Math.max(d3.mean(hs),20)
: Math.min(d3.mean(hs),19); : Math.min(d3.mean(hs),19);
}); });
}
if (options.depressRivers) { if (options.depressRivers) {
stage("Generating riverbeds.") 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 // raise every land cell a bit except riverbeds
grid.cells.h.forEach((h, i) => { grid.cells.h.forEach((h, i) => {
if (rbeds[i] || h<20) return; if (rbeds[i] || h<20) return;
@ -137,10 +139,7 @@ window.Submap = (function () {
for(const [id, gridCellId] of cells.g.entries()) { for(const [id, gridCellId] of cells.g.entries()) {
const oldGridId = reverseGridMap[gridCellId]; const oldGridId = reverseGridMap[gridCellId];
if (!oldGridId) { if (!oldGridId) throw new Error("Old grid Id must be defined!")
console.error("oldgridid must be defined for", gridCellId, reverseGridMap);
throw(new Error("oldgridid"))
}
// find old parent's children // find old parent's children
const oldChildren = oldCells.i.filter(oid=>oldCells.g[oid]==oldGridId); const oldChildren = oldCells.i.filter(oid=>oldCells.g[oid]==oldGridId);
const isWater = x => x < 1? true: false; const isWater = x => x < 1? true: false;
@ -174,8 +173,11 @@ window.Submap = (function () {
"newheight", grid.cells.h[cells.g[id]]) "newheight", grid.cells.h[cells.g[id]])
throw new Error("should be the same type") throw new Error("should be the same type")
} }
const [oldpx, oldpy]= oldCells.p[oid];
const nd = distance(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 (nd < d) [d, oldid] = [nd, oid];
}) })
if (!oldid) { if (!oldid) {
@ -185,7 +187,6 @@ window.Submap = (function () {
} }
if (isWater(cells.t[id]) !== isWater(oldCells.t[oldid])) { 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]}`); WARN && console.warn('Type discrepancy detected:', id, oldid, `${pack.cells.t[id]} != ${oldCells.t[oldid]}`);
} }
@ -227,8 +228,8 @@ window.Submap = (function () {
// Cultures.expand(); // Cultures.expand();
// transfer states, mark states without land as removed. // transfer states, mark states without land as removed.
const validStates = new Set(pack.cells.state);
stage("Porting states."); stage("Porting states.");
const validStates = new Set(pack.cells.state);
pack.states = parentMap.pack.states; pack.states = parentMap.pack.states;
// keep valid states and neighbors only // keep valid states and neighbors only
pack.states.forEach((s, i) => { pack.states.forEach((s, i) => {
@ -242,6 +243,16 @@ window.Submap = (function () {
(a,c,i) => c === -1 && !cells.state[i] ? a.push(i) && a: a, [] (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(); // BurgsAndStates.generate();
// Religions.generate(); // Religions.generate();
// BurgsAndStates.defineStateForms(); // BurgsAndStates.defineStateForms();
@ -255,9 +266,6 @@ window.Submap = (function () {
stage("Regenerating road network."); stage("Regenerating road network.");
Routes.regenerate(); Routes.regenerate();
stage("Regenerating provinces.");
BurgsAndStates.generateProvinces();
drawStates(); drawStates();
drawBorders(); drawBorders();
BurgsAndStates.drawStateLabels(); BurgsAndStates.drawStateLabels();

View file

@ -20,7 +20,7 @@ const generateSubmap = debounce(async function () {
WARN && console.warn("Resampling current map"); WARN && console.warn("Resampling current map");
closeDialogs("#worldConfigurator, #options3d"); closeDialogs("#worldConfigurator, #options3d");
const checked = id => Boolean(document.getElementById(id).checked) const checked = id => Boolean(document.getElementById(id).checked)
const settings = { const options = {
promoteTown: checked("submapPromoteTown"), promoteTown: checked("submapPromoteTown"),
depressRivers: checked("submapDepressRivers"), depressRivers: checked("submapDepressRivers"),
copyBurgs: checked("submapCopyBurgs"), copyBurgs: checked("submapCopyBurgs"),
@ -28,6 +28,7 @@ const generateSubmap = debounce(async function () {
addMilitary: checked("submapAddMilitary"), addMilitary: checked("submapAddMilitary"),
addMarkers: checked("submapAddMarkers"), addMarkers: checked("submapAddMarkers"),
addZones: checked("submapAddZones"), addZones: checked("submapAddZones"),
smoothHeightMap: scale > 2,
} }
// Create projection func from current zoom extents // Create projection func from current zoom extents
@ -64,9 +65,9 @@ const generateSubmap = debounce(async function () {
}; };
try { try {
await Submap.resample(oldstate, projection, settings); await Submap.resample(oldstate, projection, options);
} catch (error) { } catch (error) {
generateSubmapErrorHandler(error); generateSubmapErrorHandler(error, oldstate, projection, options);
} }
oldstate = null; // destroy old state to free memory oldstate = null; // destroy old state to free memory
@ -76,7 +77,7 @@ const generateSubmap = debounce(async function () {
if ($("#worldConfigurator").is(":visible")) editWorld(); if ($("#worldConfigurator").is(":visible")) editWorld();
}, 1000); }, 1000);
function generateSubmapErrorHandler(error) { function generateSubmapErrorHandler(error, oldstate, projection, options) {
ERROR && console.error(error); ERROR && console.error(error);
clearMainTip(); clearMainTip();
@ -88,12 +89,12 @@ function generateSubmapErrorHandler(error) {
title: "Generation error", title: "Generation error",
width: "32em", width: "32em",
buttons: { buttons: {
"Clear data": function () { Regenerate: async function () {
localStorage.clear(); try {
localStorage.setItem("version", version); await Submap.resample(oldstate, projection, options);
}, } catch (error) {
Regenerate: function () { generateSubmapErrorHandler(error, oldstate, projection, options);
generateSubmap(); }
$(this).dialog("close"); $(this).dialog("close");
}, },
Ignore: function () { Ignore: function () {