refactor: submap - restore routes

This commit is contained in:
Azgaar 2024-10-20 01:46:23 +02:00
parent 3dfe1b3b27
commit b00c968091
2 changed files with 41 additions and 17 deletions

View file

@ -172,29 +172,29 @@ window.Routes = (function () {
return routesMerged > 1 ? mergeRoutes(routes) : routes; return routesMerged > 1 ? mergeRoutes(routes) : routes;
} }
}
function buildLinks(routes) { function buildLinks(routes) {
const links = {}; const links = {};
for (const {points, i: routeId} of routes) { for (const {points, i: routeId} of routes) {
const cells = points.map(p => p[2]); const cells = points.map(p => p[2]);
for (let i = 0; i < cells.length - 1; i++) { for (let i = 0; i < cells.length - 1; i++) {
const cellId = cells[i]; const cellId = cells[i];
const nextCellId = cells[i + 1]; const nextCellId = cells[i + 1];
if (cellId !== nextCellId) { if (cellId !== nextCellId) {
if (!links[cellId]) links[cellId] = {}; if (!links[cellId]) links[cellId] = {};
links[cellId][nextCellId] = routeId; links[cellId][nextCellId] = routeId;
if (!links[nextCellId]) links[nextCellId] = {}; if (!links[nextCellId]) links[nextCellId] = {};
links[nextCellId][cellId] = routeId; links[nextCellId][cellId] = routeId;
}
} }
} }
return links;
} }
return links;
} }
function preparePointsArray() { function preparePointsArray() {
@ -743,6 +743,7 @@ window.Routes = (function () {
return { return {
generate, generate,
buildLinks,
connect, connect,
isConnected, isConnected,
areConnected, areConnected,

View file

@ -42,13 +42,12 @@ window.Submap = (function () {
restoreCultures(parentMap, projection); restoreCultures(parentMap, projection);
restoreBurgs(parentMap, projection, options); restoreBurgs(parentMap, projection, options);
restoreStates(parentMap, projection); restoreStates(parentMap, projection);
restoreRoutes(parentMap, projection);
restoreReligions(parentMap, projection); restoreReligions(parentMap, projection);
restoreProvinces(parentMap); restoreProvinces(parentMap);
restoreMarkers(parentMap, projection); restoreMarkers(parentMap, projection);
restoreZones(parentMap, projection, options); restoreZones(parentMap, projection, options);
Routes.generate();
Rivers.specify(); Rivers.specify();
Features.specify(); Features.specify();
@ -190,6 +189,30 @@ window.Submap = (function () {
}); });
} }
function restoreRoutes(parentMap, projection) {
pack.routes = parentMap.pack.routes
.map(route => {
const points = route.points
.map(([parentX, parentY]) => {
const [x, y] = projection(parentX, parentY);
if (!isInMap(x, y)) return null;
const cell = findCell(x, y);
return [rn(x, 2), rn(y, 2), cell];
})
.filter(Boolean);
if (points.length < 2) return null;
const firstCell = points[0][2];
const feature = pack.cells.f[firstCell];
return {...route, feature, points};
})
.filter(Boolean);
pack.cells.routes = Routes.buildLinks(pack.routes);
}
function restoreReligions(parentMap, projection) { function restoreReligions(parentMap, projection) {
const validReligions = new Set(pack.cells.religion); const validReligions = new Set(pack.cells.religion);
const religionPoles = getPolesOfInaccessibility(pack, cellId => pack.cells.religion[cellId]); const religionPoles = getPolesOfInaccessibility(pack, cellId => pack.cells.religion[cellId]);