Pathfinding Improvements: Bound Algorithm to Canvas (#168)

This commit is contained in:
Jonathan Calvert
2022-02-28 08:52:12 +00:00
committed by GitHub
parent b031acaa6e
commit 576db2dc5a
3 changed files with 25 additions and 13 deletions
+1
View File
@@ -1,3 +1,4 @@
/foundry*.js /foundry*.js
artifact/ artifact/
wasm/ wasm/
*.lock
+5 -2
View File
@@ -7,7 +7,7 @@ import {disableSnap, registerKeybindings} from "./keybindings.js";
import {libWrapper} from "./libwrapper_shim.js"; import {libWrapper} from "./libwrapper_shim.js";
import {performMigrations} from "./migration.js" import {performMigrations} from "./migration.js"
import {removeLastHistoryEntryIfAt, resetMovementHistory} from "./movement_tracking.js"; import {removeLastHistoryEntryIfAt, resetMovementHistory} from "./movement_tracking.js";
import {wipePathfindingCache} from "./pathfinding.js"; import {wipePathfindingCache, initializePathfinding} from "./pathfinding.js";
import {extendRuler} from "./ruler.js"; import {extendRuler} from "./ruler.js";
import {registerSettings, RightClickAction, settingsKey} from "./settings.js" import {registerSettings, RightClickAction, settingsKey} from "./settings.js"
import {recalculate} from "./socket.js"; import {recalculate} from "./socket.js";
@@ -21,7 +21,10 @@ export let debugGraphics = undefined;
initGridlessPathfinding().then(() => { initGridlessPathfinding().then(() => {
Hooks.on("canvasInit", wipePathfindingCache); Hooks.on("canvasInit", wipePathfindingCache);
Hooks.on("canvasReady", wipePathfindingCache); Hooks.on("canvasReady", () => {
wipePathfindingCache();
initializePathfinding();
});
Hooks.on("createWall", wipePathfindingCache); Hooks.on("createWall", wipePathfindingCache);
Hooks.on("updateWall", wipePathfindingCache); Hooks.on("updateWall", wipePathfindingCache);
Hooks.on("deleteWall", wipePathfindingCache); Hooks.on("deleteWall", wipePathfindingCache);
+19 -11
View File
@@ -9,6 +9,7 @@ import * as GridlessPathfinding from "../wasm/gridless_pathfinding.js"
let cachedNodes = undefined; let cachedNodes = undefined;
let use5105 = false; let use5105 = false;
let gridlessPathfinders = new Map(); let gridlessPathfinders = new Map();
let gridWidth, gridHeight;
export function isPathfindingEnabled() { export function isPathfindingEnabled() {
if (this.user !== game.user) if (this.user !== game.user)
@@ -51,16 +52,6 @@ export function findPath(from, to, token, previousWaypoints) {
} }
} }
export function wipePathfindingCache() {
cachedNodes = undefined;
for (const pathfinder of gridlessPathfinders.values()) {
GridlessPathfinding.free(pathfinder);
}
gridlessPathfinders.clear();
if (debugGraphics)
debugGraphics.removeChildren().forEach(c => c.destroy());
}
function getNode(pos, token, initialize=true) { function getNode(pos, token, initialize=true) {
pos = {layer: 0, ...pos}; // Copy pos and set pos.layer to the default value if it's unset pos = {layer: 0, ...pos}; // Copy pos and set pos.layer to the default value if it's unset
if (!cachedNodes) if (!cachedNodes)
@@ -77,8 +68,10 @@ function getNode(pos, token, initialize=true) {
if (initialize && !node.edges) { if (initialize && !node.edges) {
node.edges = []; node.edges = [];
for (const neighborPos of canvas.grid.grid.getNeighbors(pos.y, pos.x).map(([y, x]) => {return {x, y};})) { for (const neighborPos of canvas.grid.grid.getNeighbors(pos.y, pos.x).map(([y, x]) => {return {x, y};})) {
if (neighborPos.x < 0 || neighborPos.y < 0) if (neighborPos.x < 0 || neighborPos.y < 0 || neighborPos.x > gridWidth || neighborPos.y > gridHeight) {
continue; continue;
}
// TODO Work with pixels instead of grid locations // TODO Work with pixels instead of grid locations
if (!stepCollidesWithWall(pos, neighborPos, token)) { if (!stepCollidesWithWall(pos, neighborPos, token)) {
const isDiagonal = node.x !== neighborPos.x && node.y !== neighborPos.y && canvas.grid.type === CONST.GRID_TYPES.SQUARE; const isDiagonal = node.x !== neighborPos.x && node.y !== neighborPos.y && canvas.grid.type === CONST.GRID_TYPES.SQUARE;
@@ -158,6 +151,21 @@ function stepCollidesWithWall(from, to, token) {
return canvas.walls.checkCollision(new Ray(stepStart, stepEnd)); return canvas.walls.checkCollision(new Ray(stepStart, stepEnd));
} }
export function wipePathfindingCache() {
cachedNodes = undefined;
for (const pathfinder of gridlessPathfinders.values()) {
GridlessPathfinding.free(pathfinder);
}
gridlessPathfinders.clear();
if (debugGraphics)
debugGraphics.removeChildren().forEach(c => c.destroy());
}
export function initializePathfinding() {
gridWidth = Math.ceil(canvas.dimensions.width / canvas.grid.w);
gridHeight = Math.ceil(canvas.dimensions.height / canvas.grid.h);
}
function paintGriddedPathfindingDebug(lastNode, token) { function paintGriddedPathfindingDebug(lastNode, token) {
if (!CONFIG.debug.dragRuler) if (!CONFIG.debug.dragRuler)
return; return;