feat: routes - cleanup code

This commit is contained in:
Azgaar 2024-04-27 21:53:41 +02:00
parent 22edfb0dec
commit 597f6ddd75
2 changed files with 13 additions and 113 deletions

View file

@ -108,8 +108,6 @@ window.Routes = (function () {
TIME && console.time("generateSeaRoutes");
const seaRoutes = [];
let skip = false;
for (const [featureId, featurePorts] of Object.entries(portsByFeature)) {
const points = featurePorts.map(burg => [burg.x, burg.y]);
const urquhartEdges = calculateUrquhartEdges(points);
@ -117,27 +115,6 @@ window.Routes = (function () {
urquhartEdges.forEach(([fromId, toId]) => {
const start = featurePorts[fromId].cell;
const exit = featurePorts[toId].cell;
if (skip) return;
if (start === 444 && exit === 297) {
// if (segment.join(",") === "124,122,120") debugger;
skip = true;
for (const con of connections) {
const [from, to] = con[0].split("-").map(Number);
const [x1, y1] = cells.p[from];
const [x2, y2] = cells.p[to];
debug
.append("line")
.attr("x1", x1)
.attr("y1", y1)
.attr("x2", x2)
.attr("y2", y2)
.attr("stroke", "red")
.attr("stroke-width", 0.2);
}
}
const segments = findPathSegments({isWater: true, connections, start, exit});
for (const segment of segments) {
addConnections(segment, ROUTES.SEA_ROUTE);
@ -195,8 +172,8 @@ window.Routes = (function () {
const TYPE_MODIFIERS = {
"-1": 1, // coastline
"-2": 1.8, // sea
"-3": 3, // open sea
"-4": 5, // ocean
"-3": 4, // open sea
"-4": 6, // ocean
default: 8 // far ocean
};
@ -209,8 +186,6 @@ window.Routes = (function () {
const queue = new FlatQueue();
queue.push(start, 0);
const isDebug = start === 444 && exit === 297;
return isWater ? findWaterPath() : findLandPath();
function findLandPath() {
@ -219,26 +194,26 @@ window.Routes = (function () {
const next = queue.pop();
for (const neibCellId of cells.c[next]) {
if (cells.h[neibCellId] < 20) continue; // ignore water cells
if (neibCellId === exit) {
from[neibCellId] = next;
return from;
}
if (cells.h[neibCellId] < 20) continue; // ignore water cells
const habitability = biomesData.habitability[cells.biome[neibCellId]];
if (!habitability) continue; // inhabitable cells are not passable (eg. lava, glacier)
const distanceCost = dist2(cells.p[next], cells.p[neibCellId]);
const habitabilityModifier = 1 + Math.max(100 - habitability, 0) / 1000; // [1, 1.1];
const heightModifier = 1 + Math.max(cells.h[neibCellId] - 25, 25) / 25; // [1, 3];
const connectionModifier = connections.has(`${next}-${neibCellId}`) ? 1 : 3;
const connectionModifier = connections.has(`${next}-${neibCellId}`) ? 1 : 2;
const burgModifier = cells.burg[neibCellId] ? 1 : 3;
const cellsCost = distanceCost * habitabilityModifier * heightModifier * connectionModifier * burgModifier;
const totalCost = priority + cellsCost;
if (from[neibCellId] || totalCost >= cost[neibCellId]) continue;
if (totalCost >= cost[neibCellId]) continue;
from[neibCellId] = next;
if (neibCellId === exit) return from;
cost[neibCellId] = totalCost;
queue.push(neibCellId, totalCost);
}
@ -251,16 +226,13 @@ window.Routes = (function () {
while (queue.length) {
const priority = queue.peekValue();
const next = queue.pop();
isDebug && console.log("next", next);
for (const neibCellId of cells.c[next]) {
if (neibCellId === exit) {
isDebug && console.log(`neib ${neibCellId} is exit`);
from[neibCellId] = next;
return from;
}
// if (from[neibCellId]) continue; // don't go back
if (cells.h[neibCellId] >= 20) continue; // ignore land cells
if (temp[cells.g[neibCellId]] < MIN_PASSABLE_SEA_TEMP) continue; // ignore too cold cells
@ -271,19 +243,8 @@ window.Routes = (function () {
const cellsCost = distanceCost * typeModifier * connectionModifier;
const totalCost = priority + cellsCost;
if (isDebug) {
const lost = totalCost >= cost[neibCellId];
console.log(
`neib ${neibCellId}`,
`cellCost ${rn(cellsCost)}`,
`new ${rn(totalCost)} ${lost ? ">=" : "<"} prev ${rn(cost[neibCellId])}.`,
`${lost ? "lost" : "won"}`
);
}
if (totalCost >= cost[neibCellId]) continue;
from[neibCellId] = next;
cost[neibCellId] = totalCost;
queue.push(neibCellId, totalCost);
}