If Terran Ruler is enabled, store the length of the traveled path so changes to difficult terrain aren't reflected in the tokens movement history

This commit is contained in:
Manuel Vögele
2021-04-12 16:11:29 +02:00
parent 5d93c61f0c
commit 9787a41fd1
4 changed files with 34 additions and 8 deletions
+18 -5
View File
@@ -14,10 +14,23 @@ export function highlightMeasurementTerrainRuler(ray, startDistance, tokenShape=
}
}
export function measureDistances(segments, token, shape, gridSpaces=true) {
export function measureDistances(segments, token, shape, gridSpaces=true, options={}) {
const opts = duplicate(options)
opts.gridSpaces = gridSpaces;
const terrainRulerAvailable = game.modules.get("terrain-ruler")?.active && canvas.grid.type !== CONST.GRID_TYPES.GRIDLESS;
if (terrainRulerAvailable)
return terrainRuler.measureDistances(segments, {costFunction: (x, y) => getCostFromSpeedProvider(token, getAreaFromPositionAndShape({x, y}, shape), {x, y})});
else
return canvas.grid.measureDistances(segments, { gridSpaces });
if (terrainRulerAvailable) {
const firstNewSegmentIndex = segments.findIndex(segment => !segment.ray.dragRulerVisitedSpaces);
const previousSegments = segments.slice(0, firstNewSegmentIndex);
const newSegments = segments.slice(firstNewSegmentIndex);
const distances = previousSegments.map(segment => segment.ray.dragRulerVisitedSpaces[segment.ray.dragRulerVisitedSpaces.length - 1].distance);
previousSegments.forEach(segment => segment.ray.terrainRulerVisitedSpaces = duplicate(segment.ray.dragRulerVisitedSpaces));
opts.costFunction = (x, y) => getCostFromSpeedProvider(token, getAreaFromPositionAndShape({x, y}, shape), {x, y});
if (previousSegments.length > 0)
opts.terrainRulerInitialState = previousSegments[previousSegments.length - 1];
return distances.concat(terrainRuler.measureDistances(newSegments, opts));
}
else {
return canvas.grid.measureDistances(segments, options);
}
}