Merge branch 'master' of https://github.com/Azgaar/Fantasy-Map-Generator into burg-groups

This commit is contained in:
Azgaar 2024-12-12 13:29:40 +01:00
commit e402120b8d
42 changed files with 1526 additions and 1067 deletions

View file

@ -56,3 +56,17 @@ function drawRouteConnections() {
}
}
}
function drawPoint([x, y], {color = "red", radius = 0.5}) {
debug.append("circle").attr("cx", x).attr("cy", y).attr("r", radius).attr("fill", color);
}
function drawPath(points, {color = "red", width = 0.5}) {
const lineGen = d3.line().curve(d3.curveBundle);
debug
.append("path")
.attr("d", round(lineGen(points)))
.attr("stroke", color)
.attr("stroke-width", width)
.attr("fill", "none");
}

View file

@ -241,10 +241,8 @@ void (function addFindAll() {
};
const tree_filter = function (x, y, radius) {
var t = {x, y, x0: this._x0, y0: this._y0, x3: this._x1, y3: this._y1, quads: [], node: this._root};
if (t.node) {
t.quads.push(new Quad(t.node, t.x0, t.y0, t.x3, t.y3));
}
const t = {x, y, x0: this._x0, y0: this._y0, x3: this._x1, y3: this._y1, quads: [], node: this._root};
if (t.node) t.quads.push(new Quad(t.node, t.x0, t.y0, t.x3, t.y3));
radiusSearchInit(t, radius);
var i = 0;

View file

@ -78,11 +78,10 @@ function getBorderPath(vertices, vertexChain, discontinue) {
}
const operation = discontinued ? "M" : "L";
const command = operation === lastOperation ? "" : operation;
discontinued = false;
lastOperation = operation;
const command = operation === "L" && operation === lastOperation ? "" : operation;
return ` ${command}${vertices.p[vertexId]}`;
});
@ -177,3 +176,60 @@ function connectVertices({vertices, startingVertex, ofSameType, addToChecked, cl
if (closeRing) chain.push(startingVertex);
return chain;
}
/**
* Finds the shortest path between two cells using a cost-based pathfinding algorithm.
* @param {number} start - The ID of the starting cell.
* @param {(id: number) => boolean} isExit - A function that returns true if the cell is the exit cell.
* @param {(current: number, next: number) => number} getCost - A function that returns the path cost from current cell to the next cell. Must return `Infinity` for impassable connections.
* @returns {number[] | null} An array of cell IDs of the path from start to exit, or null if no path is found or start and exit are the same.
*/
function findPath(start, isExit, getCost) {
if (isExit(start)) return null;
const from = [];
const cost = [];
const queue = new FlatQueue();
queue.push(start, 0);
while (queue.length) {
const currentCost = queue.peekValue();
const current = queue.pop();
for (const next of pack.cells.c[current]) {
if (isExit(next)) {
from[next] = current;
return restorePath(next, start, from);
}
const nextCost = getCost(current, next);
if (nextCost === Infinity) continue; // impassable cell
const totalCost = currentCost + nextCost;
if (totalCost >= cost[next]) continue; // has cheaper path
from[next] = current;
cost[next] = totalCost;
queue.push(next, totalCost);
}
}
return null;
}
// supplementary function for findPath
function restorePath(exit, start, from) {
const pathCells = [];
let current = exit;
let prev = exit;
while (current !== start) {
pathCells.push(current);
prev = from[current];
current = prev;
}
pathCells.push(current);
return pathCells.reverse();
}

View file

@ -57,6 +57,14 @@ JSON.isValid = str => {
}
};
JSON.safeParse = str => {
try {
return JSON.parse(str);
} catch (e) {
return null;
}
};
function sanitizeId(string) {
if (!string) throw new Error("No string provided");