From 35624a37aa07670ba69095a03f5c8c0857e123cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20V=C3=B6gele?= Date: Thu, 8 Apr 2021 10:45:14 +0200 Subject: [PATCH] Add api endpoint to determine the distance that a token has moved already --- src/api.js | 12 ++++++++++++ src/compatibility.js | 11 ++++++++++- src/foundry_imports.js | 13 ++++--------- src/main.js | 3 ++- src/ruler.js | 2 +- 5 files changed, 29 insertions(+), 12 deletions(-) diff --git a/src/api.js b/src/api.js index 5f80b7d..198f16c 100644 --- a/src/api.js +++ b/src/api.js @@ -1,5 +1,8 @@ +import {measureDistances} from "./compatibility.js"; +import {getMovementHistory} from "./movement_tracking.js"; import {GenericSpeedProvider, SpeedProvider} from "./speed_provider.js" import {settingsKey} from "./settings.js" +import {getTokenShape} from "./util.js"; export const availableSpeedProviders = {} export let currentSpeedProvider = undefined @@ -104,6 +107,15 @@ export function getCostFromSpeedProvider(token, area) { return currentSpeedProvider.getCostForStep(token, area); } +export function getMovedDistanceFromToken(token) { + const history = getMovementHistory(token); + const segments = Ruler.dragRulerGetRaysFromWaypoints(history, {x: token.x, y: token.y}).map(ray => {return {ray}}); + const shape = getTokenShape(token); + const distances = measureDistances(segments, token, shape); + // Sum up the distances + return distances.reduce((acc, val) => acc + val, 0); +} + export function registerModule(moduleId, speedProvider) { // Check if a module with the given id exists and is currently enabled const module = game.modules.get(moduleId) diff --git a/src/compatibility.js b/src/compatibility.js index a0f7343..268fba7 100644 --- a/src/compatibility.js +++ b/src/compatibility.js @@ -1,5 +1,6 @@ +import {getCostFromSpeedProvider} from "./api.js"; import {getColorForDistance} from "./main.js" -import {highlightTokenShape} from "./util.js" +import {getAreaFromPositionAndShape, highlightTokenShape} from "./util.js"; export function getHexSizeSupportTokenGridCenter(token) { const tokenCenterOffset = CONFIG.hexSizeSupport.getCenterOffset(token) @@ -12,3 +13,11 @@ export function highlightMeasurementTerrainRuler(ray, startDistance, tokenShape= highlightTokenShape.call(this, space, tokenShape, color, alpha) } } + +export function measureDistances(segments, token, shape, gridSpaces=true) { + const terrainRulerAvailable = game.modules.get("terrain-ruler")?.active && canvas.grid.type !== CONST.GRID_TYPES.GRIDLESS; + if (terrainRulerAvailable) + return game.terrainRuler.measureDistances(segments, {costFunction: (x, y) => getCostFromSpeedProvider(token, getAreaFromPositionAndShape({x, y}, shape), {x, y})}); + else + return canvas.grid.measureDistances(segments, { gridSpaces }); +} diff --git a/src/foundry_imports.js b/src/foundry_imports.js index f2bd6c3..ac59be0 100644 --- a/src/foundry_imports.js +++ b/src/foundry_imports.js @@ -1,9 +1,8 @@ -import { getCostFromSpeedProvider } from "./api.js"; -import {highlightMeasurementTerrainRuler} from "./compatibility.js"; +import {highlightMeasurementTerrainRuler, measureDistances} from "./compatibility.js"; import {getGridPositionFromPixels} from "./foundry_fixes.js"; import {getColorForDistance} from "./main.js" import {trackRays} from "./movement_tracking.js" -import {applyTokenSizeOffset, getAreaFromPositionAndShape, getSnapPointForToken, getTokenShape, highlightTokenShape, zip} from "./util.js"; +import {applyTokenSizeOffset, getSnapPointForToken, getTokenShape, highlightTokenShape, zip} from "./util.js"; // This is a modified version of Ruler.moveToken from foundry 0.7.9 export async function moveTokens(draggedToken, selectedTokens) { @@ -17,7 +16,7 @@ export async function moveTokens(draggedToken, selectedTokens) { // Get the movement rays and check collision along each Ray // These rays are center-to-center for the purposes of collision checking - const rays = this.dragRulerGetRaysFromWaypoints(this.waypoints, this.destination); + const rays = this.constructor.dragRulerGetRaysFromWaypoints(this.waypoints, this.destination); if (!game.user.isGM) { const hasCollision = selectedTokens.some(token => { const offset = calculateTokenOffset(token, draggedToken) @@ -149,11 +148,7 @@ export function measure(destination, {gridSpaces=true, snap=false} = {}) { const shape = getTokenShape(this.draggedToken) // Compute measured distance - let distances - if (terrainRulerAvailable) - distances = game.terrainRuler.measureDistances(centeredSegments, {costFunction: (x, y) => getCostFromSpeedProvider(this.draggedToken, getAreaFromPositionAndShape({x, y}, shape), {x, y})}); - else - distances = canvas.grid.measureDistances(centeredSegments, { gridSpaces }); + const distances = measureDistances(centeredSegments, this.draggedToken, shape, gridSpaces); let totalDistance = 0; for (let [i, d] of distances.entries()) { diff --git a/src/main.js b/src/main.js index 91f07d7..fd8caf6 100644 --- a/src/main.js +++ b/src/main.js @@ -1,6 +1,6 @@ "use strict" -import {currentSpeedProvider, getRangesFromSpeedProvider, getUnreachableColorFromSpeedProvider, initApi, registerModule, registerSystem} from "./api.js" +import {currentSpeedProvider, getMovedDistanceFromToken, getRangesFromSpeedProvider, getUnreachableColorFromSpeedProvider, initApi, registerModule, registerSystem} from "./api.js" import {getHexSizeSupportTokenGridCenter} from "./compatibility.js" import {moveTokens, onMouseMove} from "./foundry_imports.js" import {performMigrations} from "./migration.js" @@ -19,6 +19,7 @@ Hooks.once("init", () => { window.dragRuler = { getColorForDistance, + getMovedDistanceFromToken, registerModule, registerSystem, } diff --git a/src/ruler.js b/src/ruler.js index 95dfa10..578696a 100644 --- a/src/ruler.js +++ b/src/ruler.js @@ -99,7 +99,7 @@ export class DragRulerRuler extends Ruler { } } - dragRulerGetRaysFromWaypoints(waypoints, destination) { + static dragRulerGetRaysFromWaypoints(waypoints, destination) { if ( destination ) waypoints = waypoints.concat([destination]); return waypoints.slice(1).map((wp, i) => {