From 126393fea34201d20a91891c9dad1922c6d5b3c5 Mon Sep 17 00:00:00 2001 From: Azgaar Date: Tue, 13 Aug 2024 16:36:51 +0200 Subject: [PATCH] feat: routes - auto-update part 1 --- main.js | 3 +-- modules/dynamic/auto-update.js | 44 ++++++++++++++++++++++++++++++---- versioning.js | 1 + 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/main.js b/main.js index ea9490a3..34ec1527 100644 --- a/main.js +++ b/main.js @@ -1176,7 +1176,6 @@ function reGraph() { for (const i of gridCells.i) { const height = gridCells.h[i]; const type = gridCells.t[i]; - const isOnBorder = gridCells.b[i]; // exclude most of ocean points if (height < 20) { @@ -1195,7 +1194,7 @@ function reGraph() { // add additional points for cells along coast if (type === 1 || type === -1) { - if (isOnBorder) continue; // not for near-border cells + if (gridCells.b[i]) continue; // not for near-border cells gridCells.c[i].forEach(function (e) { if (i > e) return; if (gridCells.t[e] === type) { diff --git a/modules/dynamic/auto-update.js b/modules/dynamic/auto-update.js index df84767a..6c4921ff 100644 --- a/modules/dynamic/auto-update.js +++ b/modules/dynamic/auto-update.js @@ -863,10 +863,44 @@ export function resolveVersionConflicts(version) { if (version < 1.99) { // v1.99.00 changed routes generation algorithm and data format - // 1. cells.road => cells.routes and now it an object of objects {i1: {i2: routeId, i3: routeId}} - // 2. cells.crossroad is removed - // 3. pack.routes is added as an array of objects - // 4. rendering is changed - // v1.98.00 changed compass layer and rose element id + delete cells.road; + delete cells.crossroad; + + pack.routes = []; + pack.cells.routes = {}; + + const POINT_DISTANCE = 10; + + routes.selectAll("g").each(function () { + const group = this.id; + if (!group) return; + + for (const node of this.querySelectorAll("path")) { + const totalLength = node.getTotalLength(); + if (!totalLength) debugger; + const increment = totalLength / Math.ceil(totalLength / POINT_DISTANCE); + const points = []; + + for (let i = 0; i <= totalLength; i += increment) { + const point = node.getPointAtLength(i); + const x = rn(point.x, 2); + const y = rn(point.y, 2); + const cellId = findCell(x, y); + points.push([x, y, cellId]); + } + + if (points.length < 2) return; + + const secondCellId = points[1][2]; + const feature = pack.cells.f[secondCellId]; + + pack.routes.push({i: pack.routes.length, group, feature, points}); + } + }); + + console.log(pack.routes); + + routes.selectAll("path").remove(); + if (layerIsOn("toggleRoutes")) drawRoutes(); } } diff --git a/versioning.js b/versioning.js index 9a22c740..a9f372d5 100644 --- a/versioning.js +++ b/versioning.js @@ -29,6 +29,7 @@ const version = "1.99.00"; // generator version, update each time