diff --git a/js/pathfinding.js b/js/pathfinding.js index 7e89dad..1f663b9 100644 --- a/js/pathfinding.js +++ b/js/pathfinding.js @@ -7,6 +7,7 @@ import {getSnapPointForTokenObj, iterPairs} from "./util.js"; import * as GridlessPathfinding from "../wasm/gridless_pathfinding.js" let cachedNodes = undefined; +let cacheElevation; let use5105 = false; let gridlessPathfinders = new Map(); let gridWidth, gridHeight; @@ -22,6 +23,8 @@ export function isPathfindingEnabled() { } export function findPath(from, to, token, previousWaypoints) { + checkCacheValid(token); + if (canvas.grid.type === CONST.GRID_TYPES.GRIDLESS) { let tokenSize = Math.max(token.data.width, token.data.height) * canvas.dimensions.size; let pathfinder = gridlessPathfinders.get(tokenSize); @@ -161,6 +164,20 @@ export function wipePathfindingCache() { 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() { gridWidth = Math.ceil(canvas.dimensions.width / canvas.grid.w); gridHeight = Math.ceil(canvas.dimensions.height / canvas.grid.h);