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

View file

@ -42,13 +42,12 @@ window.Submap = (function () {
restoreCultures(parentMap, projection);
restoreBurgs(parentMap, projection, options);
restoreStates(parentMap, projection);
restoreRoutes(parentMap, projection);
restoreReligions(parentMap, projection);
restoreProvinces(parentMap);
restoreMarkers(parentMap, projection);
restoreZones(parentMap, projection, options);
Routes.generate();
Rivers.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) {
const validReligions = new Set(pack.cells.religion);
const religionPoles = getPolesOfInaccessibility(pack, cellId => pack.cells.religion[cellId]);