Recompute centers (states, cultures, provinces)

This commit is contained in:
Mészáros Gergely 2021-09-16 23:40:07 +02:00
parent 4d929668ee
commit 488bfe6a4e

View file

@ -5,7 +5,6 @@ Experimental submaping module
window.Submap = (function () { window.Submap = (function () {
const isWater = (map, id) => map.grid.cells.h[map.pack.cells.g[id]] < 20? true: false; const isWater = (map, id) => map.grid.cells.h[map.pack.cells.g[id]] < 20? true: false;
const childMap = { grid, pack }
function resample(parentMap, projection, options) { function resample(parentMap, projection, options) {
// generate new map based on an existing one (resampling parentMap) // generate new map based on an existing one (resampling parentMap)
@ -15,6 +14,7 @@ window.Submap = (function () {
const stage = s => INFO && console.log('SUBMAP:', s) const stage = s => INFO && console.log('SUBMAP:', s)
const timeStart = performance.now(); const timeStart = performance.now();
const childMap = { grid, pack }
invokeActiveZooming(); invokeActiveZooming();
// copy seed // copy seed
@ -59,7 +59,7 @@ window.Submap = (function () {
// TODO: add smooth/noise function for h, temp, prec n times // TODO: add smooth/noise function for h, temp, prec n times
// smooth heightmap // smooth heightmap
// smoothing never should change cell type (land->water or water->land) // smoothing should never change cell type (land->water or water->land)
if (options.smoothHeightMap) { if (options.smoothHeightMap) {
const gcells = grid.cells; const gcells = grid.cells;
@ -99,7 +99,6 @@ window.Submap = (function () {
stage("Detect features, ocean and generating lakes.") stage("Detect features, ocean and generating lakes.")
markFeatures(); markFeatures();
markupGridOcean(); markupGridOcean();
// Warning: addLakesInDeepDepressions can be very slow! // Warning: addLakesInDeepDepressions can be very slow!
@ -125,7 +124,7 @@ window.Submap = (function () {
/****************************************************/ /****************************************************/
const oldCells = parentMap.pack.cells; const oldCells = parentMap.pack.cells;
// const reverseMap = new Map(); // cellmap from new -> oldcell // const reverseMap = new Map(); // cellmap from new -> oldcell
// const forwardMap = parentMap.pack.cells.p.map(_=>[]); // old -> [newcelllist] const forwardMap = parentMap.pack.cells.p.map(_=>[]); // old -> [newcelllist]
const pn = pack.cells.i.length; const pn = pack.cells.i.length;
const cells = pack.cells; const cells = pack.cells;
@ -216,43 +215,67 @@ window.Submap = (function () {
// fix culture centers // fix culture centers
const validCultures = new Set(pack.cells.culture); const validCultures = new Set(pack.cells.culture);
pack.cultures.forEach((c, i) => { pack.cultures.forEach((c, i) => {
if (!i) return // ignore wildlands
if (!validCultures.has(i)) { if (!validCultures.has(i)) {
c.removed = true; c.removed = true;
c.center = undefined; c.center = undefined;
} else { return
c.center = pack.cells.culture.findIndex(x => x===i);
} }
const newCenters = forwardMap[c.center]
c.center = newCenters.length
? newCenters[0]
: pack.cells.culture.findIndex(x => x===i);
}); });
stage("Porting and locking burgs.");
if (options.copyBurgs) copyBurgs(parentMap, projection, options);
// transfer states, mark states without land as removed. // transfer states, mark states without land as removed.
stage("Porting states."); stage("Porting states.");
const validStates = new Set(pack.cells.state); 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) => {
if (s.removed) return; if (!s.i || s.removed) return; // ignore removed and neutrals
if (!validStates.has(i)) s.removed = true; if (!validStates.has(i)) s.removed = true;
s.neighbors = s.neighbors.filter(n => validStates.has(n)); s.neighbors = s.neighbors.filter(n => validStates.has(n));
// find center
let capital
if (options.copyBurgs) // capital is the best bet
capital = pack.burgs[s.capital].cell;
s.center = capital
? capital
: pack.cells.state.findIndex(x => x===i);
}); });
/* probably not needed now
// fix extra coastline cells without state. // fix extra coastline cells without state.
const newCoastCells = cells.t.reduce( const newCoastCells = cells.t.reduce(
(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. // transfer provinces, mark provinces without land as removed.
stage("Porting provinces."); stage("Porting provinces.");
const validProvinces = new Set(pack.cells.province); const validProvinces = new Set(pack.cells.province);
pack.provinces = parentMap.pack.provinces; pack.provinces = parentMap.pack.provinces;
// mark uneccesary provinces // mark uneccesary provinces
pack.provinces.forEach((s, i) => { pack.provinces.forEach((p, i) => {
if (s.removed) return; if (!p || p.removed) return;
if (!validProvinces.has(i)) s.removed = true; if (!validProvinces.has(i)) {
p.removed = true;
return
}
const newCenters = forwardMap[p.center]
p.center = newCenters.length
? newCenters[0]
: pack.cells.province.findIndex(x => x===i);
}); });
stage("Porting and locking burgs."); // regenerate (if not copied) and display burgs
if (options.copyBurgs) copyBurgs(parentMap, projection, options); if (!options.copyBurgs) BurgsAndStates.regenerateBurgs();
else BurgsAndStates.regenerateBurgs();
BurgsAndStates.drawBurgs(); BurgsAndStates.drawBurgs();
stage("Regenerating road network."); stage("Regenerating road network.");
@ -299,6 +322,7 @@ window.Submap = (function () {
function copyBurgs(parentMap, projection, options) { function copyBurgs(parentMap, projection, options) {
const inMap = (x,y) => x>0 && x<graphWidth && y>0 && y<graphHeight; const inMap = (x,y) => x>0 && x<graphWidth && y>0 && y<graphHeight;
const cells = pack.cells; const cells = pack.cells;
const childMap = { grid, pack }
pack.burgs = parentMap.pack.burgs; pack.burgs = parentMap.pack.burgs;
// remap burgs to the best new cell // remap burgs to the best new cell