FIX: port burg relocation algo

This commit is contained in:
Mészáros Gergely 2021-09-17 00:16:42 +02:00
parent a83338c175
commit d32edefac8

View file

@ -300,20 +300,21 @@ window.Submap = (function () {
INFO && console.groupEnd("Generated Map " + seed); INFO && console.groupEnd("Generated Map " + seed);
} }
/* find the nearest cell having at least one *neighbor* /* find the nearest fulfilling filter f *and* having at
* fulfilling filter f, up to cell-distance `max` * least one *neighbor* fulfilling filter g, up to cell-distance `max`
* returns [cellid, neighbor] tuple or undefined if no such cell. * returns [cellid, neighbor] tuple or undefined if no such cell.
*/ */
const findNearest = (f, max=3) => centerId => { const findNearest = (f, g, max=3) => centerId => {
const met = new Set([centerId]); // cache, f might be expensive const met = new Set([centerId]); // cache, f might be expensive
const kernel = (c, dist) => { const kernel = (c, dist) => {
const ncs = pack.cells.c[c].filter(nc => !met.has(nc)); const ncs = pack.cells.c[c].filter(nc => !met.has(nc));
const n = ncs.find(f); const n = ncs.find(g);
if (n) return [c, n]; if (f(c) && n) return [c, n];
if (dist >= max || !ncs.length) return undefined; if (dist >= max || !ncs.length) return undefined;
ncs.forEach(i => met.add(i)); ncs.forEach(i => met.add(i));
const targets = ncs.filter(f)
let answer; let answer;
while (ncs.length && !answer) answer = kernel(ncs.shift(), dist+1); while (targets.length && !answer) answer = kernel(targets.shift(), dist+1);
return answer; return answer;
} }
return kernel(centerId, 1); return kernel(centerId, 1);
@ -323,6 +324,8 @@ window.Submap = (function () {
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 } const childMap = { grid, pack }
const isCoast = c => cells.t[c] === -1
const isNearCoast = c => cells.t[c] === 1
pack.burgs = parentMap.pack.burgs; pack.burgs = parentMap.pack.burgs;
// remap burgs to the best new cell // remap burgs to the best new cell
@ -341,20 +344,20 @@ window.Submap = (function () {
// pull sunken burgs out of water // pull sunken burgs out of water
if (isWater(childMap, cityCell)) { if (isWater(childMap, cityCell)) {
const searchPlace = findNearest(c => cells.t[c] === 1); const searchPlace = findNearest(isCoast, isNearCoast);
const res = searchPlace(cityCell) const res = searchPlace(cityCell)
if (!res) { if (!res) {
WARN && console.warn(`Burg ${b.name} sank like Atlantis. Unable to find coastal cells nearby. Try to reduce resample zoom level.`); WARN && console.warn(`Burg ${b.name} sank like Atlantis. Unable to find coastal cells nearby. Try to reduce resample zoom level.`);
b.removed = true; b.removed = true;
return; return;
} }
const [water, coast] = res; const [coast, water] = res;
[b.x, b.y] = b.port? getMiddlePoint(coast, water): cells.p[coast]; [b.x, b.y] = b.port? getMiddlePoint(coast, water): cells.p[coast];
if (b.port) b.port = cells.f[water]; if (b.port) b.port = cells.f[water];
b.cell = coast; b.cell = coast;
} if (b.port) { } if (b.port) {
// find coast for ports on land // find coast for ports on land
const searchPortCell = findNearest(c => cells.t[c] === -1); const searchPortCell = findNearest(isCoast, isNearCoast);
const res = searchPortCell(cityCell); const res = searchPortCell(cityCell);
if (res) { if (res) {
const [coast, water] = res; const [coast, water] = res;