Original map will be destroyed! Don't forget to save your work!
-
-
+
+
Original map will be destroyed! Don't forget to save your work!
+
Population rate (Units editor -> population) and map pixel size will
+ be automatically update according to the current scale factor. Options are interpreted as usual.
+
+
+
+
+
+
+
diff --git a/main.js b/main.js
index b7c797b8..9b0ef9b8 100644
--- a/main.js
+++ b/main.js
@@ -762,6 +762,7 @@ function markupGridOcean() {
TIME && console.timeEnd("markupGridOcean");
}
+// Calculate cell-distance to coast for every cell
function markup(cells, start, increment, limit) {
for (let t = start, count = Infinity; count > 0 && t > limit; t += increment) {
count = 0;
@@ -1690,11 +1691,11 @@ function addZones(number = 1) {
}
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;
- const neib = ra(state.neighbors.filter(n => n));
- const cell = cells.i.find(i => cells.state[i] === state.i && cells.c[i].some(c => cells.state[c] === neib));
+ const neib = ra(state.neighbors.filter(n => n && !states[n].removed));
+ const cell = cells.i.find(i => cells.state[i] === state.i && !state.removed && cells.c[i].some(c => cells.state[c] === neib));
const cellsArray = [],
queue = [cell],
power = rand(10, 30);
@@ -1704,13 +1705,18 @@ function addZones(number = 1) {
cellsArray.push(q);
if (cellsArray.length > power) break;
- cells.c[q].forEach(e => {
- if (used[e]) return;
- if (cells.state[e] !== state.i) return;
- used[e] = 1;
- if (e % 4 !== 0 && !cells.c[e].some(c => cells.state[c] === neib)) return;
- queue.push(e);
- });
+ try {
+ cells.c[q].forEach(e => {
+ if (used[e]) return;
+ if (cells.state[e] !== state.i) return;
+ used[e] = 1;
+ 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});
diff --git a/modules/military-generator.js b/modules/military-generator.js
index 0089373d..93ef24ce 100644
--- a/modules/military-generator.js
+++ b/modules/military-generator.js
@@ -114,7 +114,7 @@ window.Military = (function () {
for (const u of options.military) {
if (u.type === "naval" && !b.port) continue; // only ports produce naval units
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 army = m * perc * mod; // urban cell army
diff --git a/modules/submap.js b/modules/submap.js
index 6f4cb421..0af4ed54 100644
--- a/modules/submap.js
+++ b/modules/submap.js
@@ -4,16 +4,13 @@ Experimental submaping module
*/
window.Submap = (function () {
- function resample(baseState, projection, monitor=null) {
- // resample original map instead of regenerating
- // based on a parent map (baseState)
- // projection: map function from old to new coordinates: f(x,y) -> [x2,y2]
- // monitor: progress signaling object. MUST have at least 2 properties:
- // stage: function (string) dispatched at state change
- // progress: function (float) dispatched at progress change (@long process)
+ function resample(baseState, projection, options) {
+ // generate new map based on (resampling) existing one (baseState)
+ // baseState: {seed, grid, pack} from original map
+ // projection: map function from old to new coordinates or backwards
+ // prj(x,y,direction:bool) -> [x',y']
- const stage = s => monitor && monitor.stage && monitor.stage(s)
- const progress = p => monitor && monitor.progress && monitor.progress(p)
+ const stage = s => INFO && console.log('SUBMAP:', s)
const timeStart = performance.now();
invokeActiveZooming();
@@ -21,6 +18,7 @@ window.Submap = (function () {
seed = baseState.seed;
Math.random = aleaPRNG(seed);
INFO && console.group("SubMap with seed: " + seed);
+ DEBUG && console.log("Using Options:", options);
// create new grid
applyMapSize();
@@ -31,7 +29,7 @@ window.Submap = (function () {
const resampler = (points, qtree, f) => {
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];
f(i, oldid);
}
@@ -45,17 +43,40 @@ window.Submap = (function () {
grid.cells.prec = new Int8Array(n); // precipitation
const gridCells = baseState.grid.cells;
+ const forwardGridMap = baseState.grid.points.map(_=>[]); // old -> [newcelllist]
resampler(grid.points, baseState.pack.cells.q, (id, oldid) => {
const cid = baseState.pack.cells.g[oldid]
grid.cells.h[id] = gridCells.h[cid];
grid.cells.temp[id] = gridCells.temp[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
stage("Detect features, ocean and generating lakes.")
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();
addLakesInDeepDepressions();
// openNearSeaLakes();
@@ -65,42 +86,63 @@ window.Submap = (function () {
calculateMapCoordinates();
// calculateTemperatures();
// generatePrecipitation();
- stage("Cleaning cell network.")
+ stage("Cell cleanup.")
reGraph();
+
+ // remove misclassified cells
+
+ stage("Define coastline.")
drawCoastline();
// resample packed graph
- const packCells = baseState.pack.cells;
+ const oldCells = baseState.pack.cells;
const reverseMap = new Map(); // cellmap from new -> oldcell
const forwardMap = baseState.pack.cells.p.map(_=>[]); // old -> [newcelllist]
const pn = pack.cells.i.length;
- pack.cells.culture = new Uint16Array(pn);
- pack.cells.state = new Uint16Array(pn);
- pack.cells.burg = new Uint16Array(pn);
- pack.cells.religion = new Uint16Array(pn);
- pack.cells.road = new Uint16Array(pn);
- pack.cells.crossroad = new Uint16Array(pn);
+ const cells = pack.cells;
+ cells.culture = new Uint16Array(pn);
+ cells.state = new Uint16Array(pn);
+ cells.burg = new Uint16Array(pn);
+ cells.religion = new Uint16Array(pn);
+ cells.road = new Uint16Array(pn);
+ cells.crossroad = new Uint16Array(pn);
stage("Resampling culture, state and religion map.")
- resampler(pack.cells.p, packCells.q, (id, oldid) => {
- pack.cells.culture[id] = packCells.culture[oldid];
- pack.cells.state[id] = packCells.state[oldid];
- pack.cells.religion[id] = packCells.religion[oldid];
+ resampler(cells.p, oldCells.q, (id, oldid) => {
+ if (cells.t[id] * oldCells.t[oldid] < 0) {
+ // missmaped cell: water instead of land or vice versa
+ 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)
forwardMap[oldid].push(id)
})
- console.log('reversemap:',forwardMap)
- console.log('forwardmap:',reverseMap)
+ DEBUG && console.log('reversemap:',forwardMap)
+ DEBUG && console.log('forwardmap:',reverseMap)
+ // TODO: errode riverbeds
+
+ stage("Regenerating river network.")
Rivers.generate();
drawRivers();
Lakes.defineGroup();
// biome calculation based on (resampled) grid.cells.temp and prec
// it's safe to recalculate.
+ stage("Regenerating Biome.")
defineBiomes();
// recalculate suitability and population
// TODO: normalize according to the base-map
@@ -112,40 +154,82 @@ window.Submap = (function () {
// Cultures.expand();
// 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);
- pack.states = baseState.pack.states
- pack.states.forEach(s => {
- if (!validStates.has(s.i)) s.removed=true;
+ stage("Porting states.")
+ pack.states = baseState.pack.states;
+ // 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();
// Religions.generate();
// BurgsAndStates.defineStateForms();
// BurgsAndStates.defineBurgFeatures();
- // remove non-existent burgs
+ stage("Porting and locking burgs.")
pack.burgs = baseState.pack.burgs
-
-
const [[xmin, ymin], [xmax, ymax]] = getViewBoxExtent();
const inMap = (x,y) => x>xmin && xymin && y {
- [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)) {
- // out-of-map (removed) burgs' cell will be undefined
- console.log('burg is out of map:', b)
- b.removed=true;
+ // disable out-of-map (removed) burgs
+ b.removed = true;
b.cell = undefined;
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;
+ 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,
});
+ BurgsAndStates.drawBurgs();
+ stage("Regenerating road network.")
+ Routes.regenerate();
+
+ stage("Regenerating provinces.")
BurgsAndStates.generateProvinces();
drawStates();
@@ -155,10 +239,12 @@ window.Submap = (function () {
Rivers.specify();
Lakes.generateName();
+ stage("Modelling military.")
Military.generate();
addMarkers();
addZones();
Names.getMapName();
+ stage("Submap done.")
WARN && console.warn(`TOTAL: ${rn((performance.now() - timeStart) / 1000, 2)}s`);
showStatistics();
diff --git a/modules/ui/submap.js b/modules/ui/submap.js
index 108ffde8..6a99461b 100644
--- a/modules/ui/submap.js
+++ b/modules/ui/submap.js
@@ -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
- // x,y -> top left corner of desired submap
- // w,h -> width and height of the submap
+ // submap limits defined by the current window size (canvas viewport)
WARN && console.warn("Resampling current map");
closeDialogs("#worldConfigurator, #options3d");
- const stageUI = document.getElementById("submapStage");
- const progressUI = document.getElementById("submapProgress");
- const monitor = {
- stage: s => stageUI.innerHTML = s,
- progress: p => progressUI.innerHTML = p,
+ const settings = {
+ promoteTown: Boolean(document.getElementById("submapPromoteTown").checked),
+ depressRivers: Boolean(document.getElementById("submapDepressRivers").checked),
}
+ // Create projection func from current zoom extents
const [[x0, y0], [x1, y1]] = getViewBoxExtent();
- const projection = (x, y) => {
- return [x * (x1-x0) / graphWidth + x0, y * (y1-y0) / graphHeight + y0]
+ const projection = (x, y, inverse=false) => {
+ 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;
+
undraw();
resetZoom(1000);
-
let oldstate = {
grid: _.cloneDeep(grid),
pack: _.cloneDeep(pack),
seed,
graphWidth,
graphHeight,
- }
+ };
try {
- Submap.resample(oldstate, projection, monitor);
+ await Submap.resample(oldstate, projection, settings);
} catch (error) {
generateSubmapErrorHandler(error);
}
+
oldstate = null; // destroy old state to free memory
restoreLayers();