From 0dfdb23bfb0a97957de61babb3fd5e69b7660b30 Mon Sep 17 00:00:00 2001 From: Jonathan Calvert <38069151+JDCalvert@users.noreply.github.com> Date: Mon, 28 Feb 2022 20:17:54 +0000 Subject: [PATCH] Levels Compatibility: Clear pathfinding cache after changing elevation (#173) --- js/pathfinding.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) 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);