mirror of
https://github.com/Azgaar/Fantasy-Map-Generator.git
synced 2025-12-22 20:11:24 +01:00
fixed 0 index burg bug, more accurate coast detection for burgs
This commit is contained in:
parent
f3d51e677d
commit
40cceac6ec
3 changed files with 100 additions and 75 deletions
|
|
@ -3509,6 +3509,10 @@
|
||||||
be automatically updated according to the current scale factor. If you'd like
|
be automatically updated according to the current scale factor. If you'd like
|
||||||
to generate a new parent-map, <b>don't forget to reset them!</b> Options are interpreted as usual.</p>
|
to generate a new parent-map, <b>don't forget to reset them!</b> Options are interpreted as usual.</p>
|
||||||
<hr />
|
<hr />
|
||||||
|
<div data-tip="Copy burgs from old map. Regenerate burgs if not checked." >
|
||||||
|
<input id="submapCopyBurgs" class="checkbox" type="checkbox" checked>
|
||||||
|
<label for="submapCopyBurgs" class="checkbox-label">Copy cities</label>
|
||||||
|
</div>
|
||||||
<div data-tip="All small cities of the parent map will be promoted to Capitals." >
|
<div data-tip="All small cities of the parent map will be promoted to Capitals." >
|
||||||
<input id="submapPromoteTown" class="checkbox" type="checkbox">
|
<input id="submapPromoteTown" class="checkbox" type="checkbox">
|
||||||
<label for="submapPromoteTown" class="checkbox-label">Promote towns to cities.</label>
|
<label for="submapPromoteTown" class="checkbox-label">Promote towns to cities.</label>
|
||||||
|
|
|
||||||
|
|
@ -4,9 +4,9 @@ Experimental submaping module
|
||||||
*/
|
*/
|
||||||
|
|
||||||
window.Submap = (function () {
|
window.Submap = (function () {
|
||||||
function resample(baseState, projection, options) {
|
function resample(parentMap, projection, options) {
|
||||||
// generate new map based on (resampling) existing one (baseState)
|
// generate new map based on (resampling) existing one (parentMap)
|
||||||
// baseState: {seed, grid, pack} from original map
|
// parentMap: {seed, grid, pack} from original map
|
||||||
// projection: map function from old to new coordinates or backwards
|
// projection: map function from old to new coordinates or backwards
|
||||||
// prj(x,y,direction:bool) -> [x',y']
|
// prj(x,y,direction:bool) -> [x',y']
|
||||||
|
|
||||||
|
|
@ -15,7 +15,7 @@ window.Submap = (function () {
|
||||||
invokeActiveZooming();
|
invokeActiveZooming();
|
||||||
|
|
||||||
// copy seed
|
// copy seed
|
||||||
seed = baseState.seed;
|
seed = parentMap.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);
|
DEBUG && console.log("Using Options:", options);
|
||||||
|
|
@ -42,10 +42,10 @@ window.Submap = (function () {
|
||||||
grid.cells.temp = new Int8Array(n); // temperature
|
grid.cells.temp = new Int8Array(n); // temperature
|
||||||
grid.cells.prec = new Int8Array(n); // precipitation
|
grid.cells.prec = new Int8Array(n); // precipitation
|
||||||
|
|
||||||
const gridCells = baseState.grid.cells;
|
const gridCells = parentMap.grid.cells;
|
||||||
const forwardGridMap = baseState.grid.points.map(_=>[]); // old -> [newcelllist]
|
const forwardGridMap = parentMap.grid.points.map(_=>[]); // old -> [newcelllist]
|
||||||
resampler(grid.points, baseState.pack.cells.q, (id, oldid) => {
|
resampler(grid.points, parentMap.pack.cells.q, (id, oldid) => {
|
||||||
const cid = baseState.pack.cells.g[oldid]
|
const cid = parentMap.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];
|
||||||
|
|
@ -61,14 +61,10 @@ window.Submap = (function () {
|
||||||
const rbeds = new Uint16Array(grid.cells.i.length);
|
const rbeds = new Uint16Array(grid.cells.i.length);
|
||||||
|
|
||||||
// and erode riverbeds
|
// and erode riverbeds
|
||||||
baseState.pack.rivers.forEach(r =>
|
parentMap.pack.rivers.forEach(r =>
|
||||||
r.cells.forEach(oldc => {
|
r.cells.forEach(oldc => {
|
||||||
const targetCells = forwardGridMap[oldc];
|
const targetCells = forwardGridMap[oldc];
|
||||||
if (!targetCells) {
|
if (!targetCells) throw "TargetCell shouldn't be empty.";
|
||||||
console.error('Targetcells is empty');
|
|
||||||
console.log("oldc,gridmap", oldc, forwardGridMap);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
targetCells.forEach(c => {
|
targetCells.forEach(c => {
|
||||||
if (grid.cells.t[c]<1) return;
|
if (grid.cells.t[c]<1) return;
|
||||||
rbeds[c] = 1;
|
rbeds[c] = 1;
|
||||||
|
|
@ -101,9 +97,9 @@ window.Submap = (function () {
|
||||||
drawCoastline();
|
drawCoastline();
|
||||||
|
|
||||||
// resample packed graph
|
// resample packed graph
|
||||||
const oldCells = baseState.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 = baseState.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;
|
||||||
|
|
@ -148,17 +144,16 @@ window.Submap = (function () {
|
||||||
|
|
||||||
// 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.")
|
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
|
||||||
rankCells();
|
rankCells();
|
||||||
|
|
||||||
// transfer basemap cultures
|
// transfer basemap cultures
|
||||||
pack.cultures = baseState.pack.cultures;
|
pack.cultures = parentMap.pack.cultures;
|
||||||
// fix culture centers
|
// fix culture centers
|
||||||
const validCultures = new Set(pack.cells.culture);
|
const validCultures = new Set(pack.cells.culture);
|
||||||
console.log('cultures',validCultures);
|
|
||||||
pack.cultures.forEach((c, i) => {
|
pack.cultures.forEach((c, i) => {
|
||||||
if (!validCultures.has(i)) {
|
if (!validCultures.has(i)) {
|
||||||
c.removed = true;
|
c.removed = true;
|
||||||
|
|
@ -173,8 +168,8 @@ window.Submap = (function () {
|
||||||
|
|
||||||
// transfer states, 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);
|
||||||
stage("Porting states.")
|
stage("Porting states.");
|
||||||
pack.states = baseState.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 (!validStates.has(i)) s.removed=true;
|
if (!validStates.has(i)) s.removed=true;
|
||||||
|
|
@ -191,62 +186,15 @@ window.Submap = (function () {
|
||||||
// BurgsAndStates.defineStateForms();
|
// BurgsAndStates.defineStateForms();
|
||||||
// BurgsAndStates.defineBurgFeatures();
|
// BurgsAndStates.defineBurgFeatures();
|
||||||
|
|
||||||
stage("Porting and locking burgs.")
|
stage("Porting and locking burgs.");
|
||||||
pack.burgs = baseState.pack.burgs
|
if (options.copyBurgs) copyBurgs(parentMap, projection);
|
||||||
const [[xmin, ymin], [xmax, ymax]] = getViewBoxExtent();
|
else BurgsAndStates.regenerateBurgs();
|
||||||
const inMap = (x,y) => x>xmin && x<xmax && y>ymin && y<ymax;
|
|
||||||
|
|
||||||
// remap burgs to the best new cell
|
|
||||||
pack.burgs.forEach((b, i) => {
|
|
||||||
// [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)) {
|
|
||||||
// disable out-of-map (removed) burgs
|
|
||||||
b.removed = true;
|
|
||||||
b.cell = undefined;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
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();
|
BurgsAndStates.drawBurgs();
|
||||||
|
|
||||||
stage("Regenerating road network.")
|
stage("Regenerating road network.");
|
||||||
Routes.regenerate();
|
Routes.regenerate();
|
||||||
|
|
||||||
stage("Regenerating provinces.")
|
stage("Regenerating provinces.");
|
||||||
BurgsAndStates.generateProvinces();
|
BurgsAndStates.generateProvinces();
|
||||||
|
|
||||||
drawStates();
|
drawStates();
|
||||||
|
|
@ -256,18 +204,90 @@ window.Submap = (function () {
|
||||||
Rivers.specify();
|
Rivers.specify();
|
||||||
Lakes.generateName();
|
Lakes.generateName();
|
||||||
|
|
||||||
stage("Modelling military.")
|
stage("Modelling military.");
|
||||||
Military.generate();
|
Military.generate();
|
||||||
addMarkers();
|
addMarkers();
|
||||||
addZones();
|
addZones();
|
||||||
Names.getMapName();
|
Names.getMapName();
|
||||||
stage("Submap done.")
|
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();
|
||||||
INFO && console.groupEnd("Generated Map " + seed);
|
INFO && console.groupEnd("Generated Map " + seed);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* find the nearest cell having at least one *neighbor*
|
||||||
|
* fulfilling filter f, up to cell-distance `max`
|
||||||
|
* returns [cellid, neighbor] tuple or undefined if no such cell.
|
||||||
|
*/
|
||||||
|
const findNearest = (f, max=3) => centerId => {
|
||||||
|
const met = new Set([centerId]); // f might be expensive
|
||||||
|
const kernel = (c, dist) => {
|
||||||
|
const ncs = pack.cells.c[c].filter(nc => !met.has(nc));
|
||||||
|
const n = ncs.find(f);
|
||||||
|
if (n) return [c, n];
|
||||||
|
if (dist >= max || !ncs.length) return undefined;
|
||||||
|
ncs.forEach(i => met.add(i));
|
||||||
|
let answer;
|
||||||
|
while (ncs.length && !answer) answer = kernel(ncs.shift(), dist+1);
|
||||||
|
return answer;
|
||||||
|
}
|
||||||
|
return kernel(centerId, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
function copyBurgs(parentMap, projection) {
|
||||||
|
const [[xmin, ymin], [xmax, ymax]] = getViewBoxExtent();
|
||||||
|
const inMap = (x,y) => x>xmin && x<xmax && y>ymin && y<ymax;
|
||||||
|
const cells = pack.cells;
|
||||||
|
pack.burgs = parentMap.pack.burgs;
|
||||||
|
|
||||||
|
// remap burgs to the best new cell
|
||||||
|
pack.burgs.forEach( (b, id) => {
|
||||||
|
if (id == 0) return; // skip empty city of neturals
|
||||||
|
[b.x, b.y] = projection(b.x, b.y, false);
|
||||||
|
|
||||||
|
// disable out-of-map (removed) burgs
|
||||||
|
if (!inMap(b.x,b.y)) {
|
||||||
|
b.removed = true;
|
||||||
|
b.cell = undefined;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let cityCell = findCell(b.x, b.y);
|
||||||
|
|
||||||
|
// pull sunken burgs out of water
|
||||||
|
if (cells.t[cityCell] <= 0) {
|
||||||
|
const searchPlace = findNearest(c => cells.t[c] === 1);
|
||||||
|
const res = searchPlace(cityCell)
|
||||||
|
if (!res) {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
const [water, coast] = res;
|
||||||
|
[b.x, b.y] = b.port? getMiddlePoint(coast, water): cells.p[coast];
|
||||||
|
b.cell = coast;
|
||||||
|
} if (b.port) {
|
||||||
|
// find coast for ports on land
|
||||||
|
const searchPortCell = findNearest(c => cells.t[c] === -1);
|
||||||
|
const res = searchPortCell(cityCell);
|
||||||
|
if (res) {
|
||||||
|
const [coast, water] = res;
|
||||||
|
[b.x, b.y] = getMiddlePoint(coast, water);
|
||||||
|
b.cell = coast;
|
||||||
|
} else {
|
||||||
|
WARN && console.warn(`Can't find water near port ${b.name}. :-/`);
|
||||||
|
b.port = 0;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
[b.x, b.y] = cells.p[b.cell];
|
||||||
|
}
|
||||||
|
b.lock = true;
|
||||||
|
pack.cells.burg[b.cell] = id;
|
||||||
|
if (options.promoteTown) b.capital = 1;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// export
|
// export
|
||||||
return { resample }
|
return { resample }
|
||||||
})();
|
})();
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,7 @@ const generateSubmap = debounce(async function () {
|
||||||
const settings = {
|
const settings = {
|
||||||
promoteTown: Boolean(document.getElementById("submapPromoteTown").checked),
|
promoteTown: Boolean(document.getElementById("submapPromoteTown").checked),
|
||||||
depressRivers: Boolean(document.getElementById("submapDepressRivers").checked),
|
depressRivers: Boolean(document.getElementById("submapDepressRivers").checked),
|
||||||
|
copyBurgs: Boolean(document.getElementById("submapCopyBurgs").checked),
|
||||||
addLakesInDepressions: Boolean(document.getElementById("submapAddLakeInDepression").checked),
|
addLakesInDepressions: Boolean(document.getElementById("submapAddLakeInDepression").checked),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue