Levels Compatibility: Clear pathfinding cache after changing elevation (#173)

This commit is contained in:
Jonathan Calvert
2022-02-28 20:17:54 +00:00
committed by GitHub
parent 8d243ce919
commit a967a03197
+17
View File
@@ -8,6 +8,7 @@ import * as GridlessPathfinding from "../wasm/gridless_pathfinding.js"
import {PriorityQueueSet} from "./data_structures.js"; import {PriorityQueueSet} from "./data_structures.js";
let cachedNodes = undefined; let cachedNodes = undefined;
let cacheElevation;
let use5105 = false; let use5105 = false;
let gridlessPathfinders = new Map(); let gridlessPathfinders = new Map();
let gridWidth, gridHeight; let gridWidth, gridHeight;
@@ -23,6 +24,8 @@ export function isPathfindingEnabled() {
} }
export function findPath(from, to, token, previousWaypoints) { export function findPath(from, to, token, previousWaypoints) {
checkCacheValid(token);
if (canvas.grid.type === CONST.GRID_TYPES.GRIDLESS) { if (canvas.grid.type === CONST.GRID_TYPES.GRIDLESS) {
let tokenSize = Math.max(token.data.width, token.data.height) * canvas.dimensions.size; let tokenSize = Math.max(token.data.width, token.data.height) * canvas.dimensions.size;
let pathfinder = gridlessPathfinders.get(tokenSize); let pathfinder = gridlessPathfinders.get(tokenSize);
@@ -169,6 +172,20 @@ export function wipePathfindingCache() {
debugGraphics.removeChildren().forEach(c => c.destroy()); debugGraphics.removeChildren().forEach(c => c.destroy());
} }
/**
* Check if the current cache is still suitable for the path we're about to find. If not, clear the cache
*/
function checkCacheValid(token) {
// If levels is enabled, the cache is invalid if it was made for a
if (game.modules.get("levels")?.active) {
const tokenElevation = token.data.elevation;
if (tokenElevation !== cacheElevation) {
cacheElevation = tokenElevation;
wipePathfindingCache();
}
}
}
export function initializePathfinding() { export function initializePathfinding() {
gridWidth = Math.ceil(canvas.dimensions.width / canvas.grid.w); gridWidth = Math.ceil(canvas.dimensions.width / canvas.grid.w);
gridHeight = Math.ceil(canvas.dimensions.height / canvas.grid.h); gridHeight = Math.ceil(canvas.dimensions.height / canvas.grid.h);