refactor: submap - add middle points

This commit is contained in:
Azgaar 2024-10-26 14:27:37 +02:00
parent 1305246d7b
commit 3d79a527e2
3 changed files with 90 additions and 4 deletions

View file

@ -109,17 +109,23 @@ window.Resample = (function () {
pack.cells.conf = new Uint8Array(pack.cells.i.length);
const offset = grid.spacing * 2;
const getCellCost = cellId => {
if (pack.cells.h[cellId] < 20) return Infinity;
return pack.cells.h[cellId];
};
pack.rivers = parentMap.pack.rivers
.map(river => {
const parentPoints = river.points || river.cells.map(cellId => parentMap.pack.cells.p[cellId]);
const points = parentPoints
const newPoints = parentPoints
.map(([parentX, parentY]) => {
const [x, y] = projection(parentX, parentY);
return isInMap(x, y, offset) ? [rn(x, 2), rn(y, 2)] : null;
})
.filter(Boolean);
if (points.length < 2) return null;
if (newPoints.length < 2) return null;
const points = addIntermidiatePoints(newPoints, getCellCost);
const cells = points.map(point => findCell(...point));
cells.forEach(cellId => {
if (pack.cells.r[cellId]) pack.cells.conf[cellId] = 1;
@ -327,6 +333,23 @@ window.Resample = (function () {
);
}
// fill gaps in points array with intermidiate points
function addIntermidiatePoints(points, getCellCost) {
const newPoints = [];
for (let i = 0; i < points.length; i++) {
newPoints.push(points[i]);
if (points[i + 1]) {
const start = findCell(...points[i]);
const exit = findCell(...points[i + 1]);
const pathCells = findPath(start, exit, getCellCost);
if (pathCells) newPoints.push(...pathCells.map(cellId => pack.cells.p[cellId]));
}
}
return newPoints;
}
function isWater(graph, cellId) {
return graph.cells.h[cellId] < 20;
}

View file

@ -290,8 +290,8 @@ window.Routes = (function () {
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;
const cellCost = distanceCost * habitabilityModifier * heightModifier * connectionModifier * burgModifier;
const totalCost = priority + cellCost;
if (totalCost >= cost[neibCellId]) continue;
from[neibCellId] = next;