From 3dfe1b3b27d3016c6d0ad056cb8c459e9aa0ad8e Mon Sep 17 00:00:00 2001 From: Azgaar Date: Sun, 20 Oct 2024 01:12:07 +0200 Subject: [PATCH] refactor: submap - relocate burgs --- modules/burgs-and-states.js | 3 ++- modules/submap.js | 25 ++++++++++++++++--------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/modules/burgs-and-states.js b/modules/burgs-and-states.js index 90ab2bf2..98b0db67 100644 --- a/modules/burgs-and-states.js +++ b/modules/burgs-and-states.js @@ -878,6 +878,7 @@ window.BurgsAndStates = (() => { generateDiplomacy, defineStateForms, getFullName, - updateCultures + updateCultures, + getCloseToEdgePoint }; })(); diff --git a/modules/submap.js b/modules/submap.js index c94cd1e4..4db47f23 100644 --- a/modules/submap.js +++ b/modules/submap.js @@ -133,28 +133,35 @@ window.Submap = (function () { function restoreBurgs(parentMap, projection, options) { const packLandCellsQuadtree = d3.quadtree(groupCellsByType(pack).land); + const findLandCell = (x, y) => packLandCellsQuadtree.find(x, y, Infinity)?.[2]; pack.burgs = parentMap.pack.burgs.map(burg => { if (!burg.i || burg.removed) return burg; burg.population *= options.scale; // adjust for populationRate change const [xp, yp] = projection(burg.x, burg.y); - const [x, y] = [rn(xp, 2), rn(yp, 2)]; - if (!isInMap(x, y)) return {...burg, removed: true, lock: false}; + if (!isInMap(xp, yp)) return {...burg, removed: true, lock: false}; + + const closestCell = findCell(xp, yp); + const cell = isWater(pack, closestCell) ? findLandCell(xp, yp) : closestCell; - const cell = packLandCellsQuadtree.find(x, y, Infinity)?.[2]; - if (!cell) { - ERROR && console.error(`Could not find cell for burg ${burg.name} (${burg.i}). Had to remove it`); - return {...burg, removed: true, lock: false}; - } if (pack.cells.burg[cell]) { - WARN && console.warn(`Cell ${cell} already has a burg. Had to remove burg ${burg.name} (${burg.i})`); + WARN && console.warn(`Cell ${cell} already has a burg. Removing burg ${burg.name} (${burg.i})`); return {...burg, removed: true, lock: false}; } pack.cells.burg[cell] = burg.i; - return {...burg, x, y, cell}; + const [x, y] = getBurgCoordinates(burg, closestCell, cell, xp, yp); + return {...burg, cell, x, y}; }); + + function getBurgCoordinates(burg, closestCell, cell, xp, yp) { + const haven = pack.cells.haven[cell]; + if (burg.port && haven) return BurgsAndStates.getCloseToEdgePoint(cell, haven); + + if (closestCell !== cell) return pack.cells.p[cell]; + return [rn(xp, 2), rn(yp, 2)]; + } } function restoreStates(parentMap, projection) {