Submap with options

This commit is contained in:
Mészáros Gergely 2021-08-14 21:57:10 +02:00
parent 6f97864962
commit e6e12a01fa
5 changed files with 168 additions and 65 deletions

View file

@ -3503,10 +3503,17 @@
<div id="tileStatus" style="background-color: #33333310; font-style: italic"></div> <div id="tileStatus" style="background-color: #33333310; font-style: italic"></div>
</div> </div>
<div id="submapOptionsDialog" style="display: none" class="dialog"> <div id="submapOptionsDialog" style="display: none; max-width:300px;" class="dialog">
<p style="font-style: italic; color: red">Original map will be destroyed! Don't forget to save your work!</p> <p style="font-style: italic; color: red; font-weight:bold;">Original map will be destroyed! Don't forget to save your work!</p>
<div id="submapStage"></div> <p>Population rate (Units editor -> population) and map pixel size will
<div id="submapProgress"></div> be automatically update according to the current scale factor. Options are interpreted as usual.</p>
<hl />
<div data-tip="All small cities of the parent map will be promoted to Capitals." >
<input id="submapPromoteTown" class="checkbox" type="checkbox">
<label for="submapPromoteTown" class="checkbox-label">Promote towns to cities.</label>
<input id="submapDepressRivers" class="checkbox" type="checkbox">
<label for="submapDepressRivers" class="checkbox-label">Errode riverbeds.</label>
</div>
<button id="start" data-tip="Start submap resampling" class="options glow" onclick="generateSubmap()">Generate</button> <button id="start" data-tip="Start submap resampling" class="options glow" onclick="generateSubmap()">Generate</button>
</div> </div>

26
main.js
View file

@ -762,6 +762,7 @@ function markupGridOcean() {
TIME && console.timeEnd("markupGridOcean"); TIME && console.timeEnd("markupGridOcean");
} }
// Calculate cell-distance to coast for every cell
function markup(cells, start, increment, limit) { function markup(cells, start, increment, limit) {
for (let t = start, count = Infinity; count > 0 && t > limit; t += increment) { for (let t = start, count = Infinity; count > 0 && t > limit; t += increment) {
count = 0; count = 0;
@ -1690,11 +1691,11 @@ function addZones(number = 1) {
} }
function addRebels() { function addRebels() {
const state = ra(states.filter(s => s.i && s.neighbors.some(n => n))); const state = ra(states.filter(s => s.i && !s.removed && s.neighbors.some(n => n)));
if (!state) return; if (!state) return;
const neib = ra(state.neighbors.filter(n => n)); const neib = ra(state.neighbors.filter(n => n && !states[n].removed));
const cell = cells.i.find(i => cells.state[i] === state.i && cells.c[i].some(c => cells.state[c] === neib)); const cell = cells.i.find(i => cells.state[i] === state.i && !state.removed && cells.c[i].some(c => cells.state[c] === neib));
const cellsArray = [], const cellsArray = [],
queue = [cell], queue = [cell],
power = rand(10, 30); power = rand(10, 30);
@ -1704,13 +1705,18 @@ function addZones(number = 1) {
cellsArray.push(q); cellsArray.push(q);
if (cellsArray.length > power) break; if (cellsArray.length > power) break;
cells.c[q].forEach(e => { try {
if (used[e]) return; cells.c[q].forEach(e => {
if (cells.state[e] !== state.i) return; if (used[e]) return;
used[e] = 1; if (cells.state[e] !== state.i) return;
if (e % 4 !== 0 && !cells.c[e].some(c => cells.state[c] === neib)) return; used[e] = 1;
queue.push(e); if (e % 4 !== 0 && !cells.c[e].some(c => cells.state[c] === neib)) return;
}); queue.push(e);
});
} catch (er) {
console.log('WTF: ', q, cellsArray);
throw er;
}
} }
const rebels = rw({Rebels: 5, Insurgents: 2, Mutineers: 1, Rioters: 1, Separatists: 1, Secessionists: 1, Insurrection: 2, Rebellion: 1, Conspiracy: 2}); const rebels = rw({Rebels: 5, Insurgents: 2, Mutineers: 1, Rioters: 1, Separatists: 1, Secessionists: 1, Insurrection: 2, Rebellion: 1, Conspiracy: 2});

View file

@ -114,7 +114,7 @@ window.Military = (function () {
for (const u of options.military) { for (const u of options.military) {
if (u.type === "naval" && !b.port) continue; // only ports produce naval units if (u.type === "naval" && !b.port) continue; // only ports produce naval units
const perc = +u.urban; const perc = +u.urban;
if (isNaN(perc) || perc <= 0 || !s.temp[u.name]) continue; if (isNaN(perc) || perc <= 0 || !s.temp || !s.temp[u.name]) continue;
const mod = type === "generic" ? 1 : burgTypeModifier[type][u.type]; // cell specific modifier const mod = type === "generic" ? 1 : burgTypeModifier[type][u.type]; // cell specific modifier
const army = m * perc * mod; // urban cell army const army = m * perc * mod; // urban cell army

View file

@ -4,16 +4,13 @@ Experimental submaping module
*/ */
window.Submap = (function () { window.Submap = (function () {
function resample(baseState, projection, monitor=null) { function resample(baseState, projection, options) {
// resample original map instead of regenerating // generate new map based on (resampling) existing one (baseState)
// based on a parent map (baseState) // baseState: {seed, grid, pack} from original map
// projection: map function from old to new coordinates: f(x,y) -> [x2,y2] // projection: map function from old to new coordinates or backwards
// monitor: progress signaling object. MUST have at least 2 properties: // prj(x,y,direction:bool) -> [x',y']
// stage: function (string) dispatched at state change
// progress: function (float) dispatched at progress change (@long process)
const stage = s => monitor && monitor.stage && monitor.stage(s) const stage = s => INFO && console.log('SUBMAP:', s)
const progress = p => monitor && monitor.progress && monitor.progress(p)
const timeStart = performance.now(); const timeStart = performance.now();
invokeActiveZooming(); invokeActiveZooming();
@ -21,6 +18,7 @@ window.Submap = (function () {
seed = baseState.seed; seed = baseState.seed;
Math.random = aleaPRNG(seed); Math.random = aleaPRNG(seed);
INFO && console.group("SubMap with seed: " + seed); INFO && console.group("SubMap with seed: " + seed);
DEBUG && console.log("Using Options:", options);
// create new grid // create new grid
applyMapSize(); applyMapSize();
@ -31,7 +29,7 @@ window.Submap = (function () {
const resampler = (points, qtree, f) => { const resampler = (points, qtree, f) => {
for(const [i,[x, y]] of points.entries()) { for(const [i,[x, y]] of points.entries()) {
const [tx, ty] = projection(x, y); const [tx, ty] = projection(x, y, true);
const oldid = qtree.find(tx,ty,Infinity)[2]; const oldid = qtree.find(tx,ty,Infinity)[2];
f(i, oldid); f(i, oldid);
} }
@ -45,17 +43,40 @@ window.Submap = (function () {
grid.cells.prec = new Int8Array(n); // precipitation grid.cells.prec = new Int8Array(n); // precipitation
const gridCells = baseState.grid.cells; const gridCells = baseState.grid.cells;
const forwardGridMap = baseState.grid.points.map(_=>[]); // old -> [newcelllist]
resampler(grid.points, baseState.pack.cells.q, (id, oldid) => { resampler(grid.points, baseState.pack.cells.q, (id, oldid) => {
const cid = baseState.pack.cells.g[oldid] const cid = baseState.pack.cells.g[oldid]
grid.cells.h[id] = gridCells.h[cid]; grid.cells.h[id] = gridCells.h[cid];
grid.cells.temp[id] = gridCells.temp[cid]; grid.cells.temp[id] = gridCells.temp[cid];
grid.cells.prec[id] = gridCells.prec[cid]; grid.cells.prec[id] = gridCells.prec[cid];
id%50 || progress(id * 100.0 / grid.points.length); if (options.depressRivers) forwardGridMap[oldid].push(id);
}) })
// TODO: add smooth/noise function for h, temp, prec n times // TODO: add smooth/noise function for h, temp, prec n times
stage("Detect features, ocean and generating lakes.") stage("Detect features, ocean and generating lakes.")
markFeatures(); markFeatures();
if (options.depressRivers) {
stage("Generating riverbeds.")
const rbeds = new Uint16Array(grid.cells.i.length);
// and erode riverbeds
baseState.pack.rivers.forEach(r =>
r.cells.forEach(oldc => {
const targetCells = forwardGridMap[oldc];
targetCells.forEach(c => {
if (grid.cells.t[c]<1) return;
rbeds[c] = 1;
});
})
);
// raise every land cell a bit
grid.cells.h.forEach((h, i) => {
if (!rbeds[i] || grid.cells.t[i]<1) return;
grid.cells.h[i] = Math.min(grid.cells.h[i] * 1.1, 255);
});
}
markupGridOcean(); markupGridOcean();
addLakesInDeepDepressions(); addLakesInDeepDepressions();
// openNearSeaLakes(); // openNearSeaLakes();
@ -65,42 +86,63 @@ window.Submap = (function () {
calculateMapCoordinates(); calculateMapCoordinates();
// calculateTemperatures(); // calculateTemperatures();
// generatePrecipitation(); // generatePrecipitation();
stage("Cleaning cell network.") stage("Cell cleanup.")
reGraph(); reGraph();
// remove misclassified cells
stage("Define coastline.")
drawCoastline(); drawCoastline();
// resample packed graph // resample packed graph
const packCells = baseState.pack.cells; const oldCells = baseState.pack.cells;
const reverseMap = new Map(); // cellmap from new -> oldcell const reverseMap = new Map(); // cellmap from new -> oldcell
const forwardMap = baseState.pack.cells.p.map(_=>[]); // old -> [newcelllist] const forwardMap = baseState.pack.cells.p.map(_=>[]); // old -> [newcelllist]
const pn = pack.cells.i.length; const pn = pack.cells.i.length;
pack.cells.culture = new Uint16Array(pn); const cells = pack.cells;
pack.cells.state = new Uint16Array(pn); cells.culture = new Uint16Array(pn);
pack.cells.burg = new Uint16Array(pn); cells.state = new Uint16Array(pn);
pack.cells.religion = new Uint16Array(pn); cells.burg = new Uint16Array(pn);
pack.cells.road = new Uint16Array(pn); cells.religion = new Uint16Array(pn);
pack.cells.crossroad = new Uint16Array(pn); cells.road = new Uint16Array(pn);
cells.crossroad = new Uint16Array(pn);
stage("Resampling culture, state and religion map.") stage("Resampling culture, state and religion map.")
resampler(pack.cells.p, packCells.q, (id, oldid) => { resampler(cells.p, oldCells.q, (id, oldid) => {
pack.cells.culture[id] = packCells.culture[oldid]; if (cells.t[id] * oldCells.t[oldid] < 0) {
pack.cells.state[id] = packCells.state[oldid]; // missmaped cell: water instead of land or vice versa
pack.cells.religion[id] = packCells.religion[oldid]; WARN && console.warn('Type discrepancy detected:', id, oldid, `${pack.cells.t[id]} != ${oldCells.t[oldid]}`);
const aid = cells.t[id]<0
? cells.c[id].find(c=>cells.t[c]<0)
: cells.c[id].find(c=>cells.t[c]>0);
const [x, y] = cells.p[aid];
const [tx, ty] = projection(x, y, true);
oldid = oldCells.q.find(tx,ty,Infinity)[2];
WARN && console.warn(`using cell ${aid}->${oldid} instead`);
}
cells.culture[id] = oldCells.culture[oldid];
cells.state[id] = oldCells.state[oldid];
cells.religion[id] = oldCells.religion[oldid];
reverseMap.set(id, oldid) reverseMap.set(id, oldid)
forwardMap[oldid].push(id) forwardMap[oldid].push(id)
}) })
console.log('reversemap:',forwardMap) DEBUG && console.log('reversemap:',forwardMap)
console.log('forwardmap:',reverseMap) DEBUG && console.log('forwardmap:',reverseMap)
// TODO: errode riverbeds
stage("Regenerating river network.")
Rivers.generate(); Rivers.generate();
drawRivers(); drawRivers();
Lakes.defineGroup(); Lakes.defineGroup();
// biome calculation based on (resampled) grid.cells.temp and prec // biome calculation based on (resampled) grid.cells.temp and prec
// it's safe to recalculate. // it's safe to recalculate.
stage("Regenerating Biome.")
defineBiomes(); defineBiomes();
// recalculate suitability and population // recalculate suitability and population
// TODO: normalize according to the base-map // TODO: normalize according to the base-map
@ -112,40 +154,82 @@ window.Submap = (function () {
// Cultures.expand(); // Cultures.expand();
// TODO: update culture centers // TODO: update culture centers
// transfer states and burgs. mark states without land as removed. // transfer states, mark states without land as removed.
const validStates = new Set(pack.cells.state); const validStates = new Set(pack.cells.state);
pack.states = baseState.pack.states stage("Porting states.")
pack.states.forEach(s => { pack.states = baseState.pack.states;
if (!validStates.has(s.i)) s.removed=true; // keep valid states and neighbors only
pack.states.forEach((s, i) => {
if (!validStates.has(i)) s.removed=true;
s.neighbors = s.neighbors.filter(n => validStates.has(n));
}); });
// fix extra coastline cells without state.
const newCoastCells = cells.t.reduce(
(a,c,i) => c === -1 && !cells.state[i] ? a.push(i) && a: a, []
);
// BurgsAndStates.generate(); // BurgsAndStates.generate();
// Religions.generate(); // Religions.generate();
// BurgsAndStates.defineStateForms(); // BurgsAndStates.defineStateForms();
// BurgsAndStates.defineBurgFeatures(); // BurgsAndStates.defineBurgFeatures();
// remove non-existent burgs stage("Porting and locking burgs.")
pack.burgs = baseState.pack.burgs pack.burgs = baseState.pack.burgs
const [[xmin, ymin], [xmax, ymax]] = getViewBoxExtent(); const [[xmin, ymin], [xmax, ymax]] = getViewBoxExtent();
const inMap = (x,y) => x>xmin && x<xmax && y>ymin && y<ymax; const inMap = (x,y) => x>xmin && x<xmax && y>ymin && y<ymax;
// remap burgs to the best new cell // remap burgs to the best new cell
pack.burgs.forEach((b, i) => { pack.burgs.forEach((b, i) => {
[b.x,b.y] = projection(b.x, b.y); // [b.x,b.y] = inverseProjection(b.x, b.y);
[b.x,b.y] = projection(b.x, b.y, false);
if (!inMap(b.x,b.y)) { if (!inMap(b.x,b.y)) {
// out-of-map (removed) burgs' cell will be undefined // disable out-of-map (removed) burgs
console.log('burg is out of map:', b) b.removed = true;
b.removed=true;
b.cell = undefined; b.cell = undefined;
return; return;
} }
b.cell = findCell(b.x, b.y);
let bestCell = findCell(b.x, b.y);
// move burgs out of water
if (cells.t[bestCell] == -1) {
const coasts = cells.c[bestCell].filter(c=>cells.t[c] == 1);
if (!coasts.length) {
WARN && console.warn(`Burg ${b.name} sank like Atlantis. Unable to find coastal cells nearby. Try to reduce resample zoom level.`);
b.removed = true;
return;
}
bestCell = coasts[0]; // TODO: closest instead?
}
b.cell = bestCell;
b.lock = true;
pack.cells.burg[b.cell] = i; pack.cells.burg[b.cell] = i;
if (options.promoteTown) b.capital = 1;
// find water body id for ports
if (b.port) {
const water = cells.c[b.cell].filter(c=>cells.t[c] == -1);
if (water.length) {
b.port = cells.f[water[0]];
[b.x, b.y] = getMiddlePoint(b.cell, water[0]);
} else {
WARN && console.warn(`Can't find water near port ${b.name}. :-/`);
b.port = 0;
}
} else {
[b.x, b.y] = cells.p[b.cell];
}
// TODO: move port burgs to coast b.x, b.y, // TODO: move port burgs to coast b.x, b.y,
}); });
BurgsAndStates.drawBurgs();
stage("Regenerating road network.")
Routes.regenerate();
stage("Regenerating provinces.")
BurgsAndStates.generateProvinces(); BurgsAndStates.generateProvinces();
drawStates(); drawStates();
@ -155,10 +239,12 @@ window.Submap = (function () {
Rivers.specify(); Rivers.specify();
Lakes.generateName(); Lakes.generateName();
stage("Modelling military.")
Military.generate(); Military.generate();
addMarkers(); addMarkers();
addZones(); addZones();
Names.getMapName(); Names.getMapName();
stage("Submap done.")
WARN && console.warn(`TOTAL: ${rn((performance.now() - timeStart) / 1000, 2)}s`); WARN && console.warn(`TOTAL: ${rn((performance.now() - timeStart) / 1000, 2)}s`);
showStatistics(); showStatistics();

View file

@ -13,42 +13,46 @@ function openSubmapOptions() {
}); });
} }
const generateSubmap = debounce(function (x, y, w, h) { const generateSubmap = debounce(async function () {
// Create submap from the current map // Create submap from the current map
// x,y -> top left corner of desired submap // submap limits defined by the current window size (canvas viewport)
// w,h -> width and height of the submap
WARN && console.warn("Resampling current map"); WARN && console.warn("Resampling current map");
closeDialogs("#worldConfigurator, #options3d"); closeDialogs("#worldConfigurator, #options3d");
const stageUI = document.getElementById("submapStage"); const settings = {
const progressUI = document.getElementById("submapProgress"); promoteTown: Boolean(document.getElementById("submapPromoteTown").checked),
const monitor = { depressRivers: Boolean(document.getElementById("submapDepressRivers").checked),
stage: s => stageUI.innerHTML = s,
progress: p => progressUI.innerHTML = p,
} }
// Create projection func from current zoom extents
const [[x0, y0], [x1, y1]] = getViewBoxExtent(); const [[x0, y0], [x1, y1]] = getViewBoxExtent();
const projection = (x, y) => { const projection = (x, y, inverse=false) => {
return [x * (x1-x0) / graphWidth + x0, y * (y1-y0) / graphHeight + y0] return inverse
? [x * (x1-x0) / graphWidth + x0, y * (y1-y0) / graphHeight + y0]
: [(x-x0) * graphWidth / (x1-x0), (y-y0) * graphHeight / (y1-y0)];
} }
// fix scale
distanceScaleInput.value = distanceScaleOutput.value = distanceScaleOutput.value / scale;
populationRateInput.value = populationRateOutput.value = populationRateOutput.value / scale;
customization = 0; customization = 0;
undraw(); undraw();
resetZoom(1000); resetZoom(1000);
let oldstate = { let oldstate = {
grid: _.cloneDeep(grid), grid: _.cloneDeep(grid),
pack: _.cloneDeep(pack), pack: _.cloneDeep(pack),
seed, seed,
graphWidth, graphWidth,
graphHeight, graphHeight,
} };
try { try {
Submap.resample(oldstate, projection, monitor); await Submap.resample(oldstate, projection, settings);
} catch (error) { } catch (error) {
generateSubmapErrorHandler(error); generateSubmapErrorHandler(error);
} }
oldstate = null; // destroy old state to free memory oldstate = null; // destroy old state to free memory
restoreLayers(); restoreLayers();