Rename functions to use snake case
This commit is contained in:
@@ -3,7 +3,7 @@ import {getCenterFromGridPositionObj, getGridPositionFromPixels, getGridPosition
|
|||||||
import {Line} from "./geometry.js";
|
import {Line} from "./geometry.js";
|
||||||
import {disableSnap, moveWithoutAnimation} from "./keybindings.js";
|
import {disableSnap, moveWithoutAnimation} from "./keybindings.js";
|
||||||
import {trackRays} from "./movement_tracking.js"
|
import {trackRays} from "./movement_tracking.js"
|
||||||
import {find_path, is_pathfinding_enabled} from "./pathfinding.js";
|
import {findPath, isPathfindingEnabled} from "./pathfinding.js";
|
||||||
import {settingsKey} from "./settings.js";
|
import {settingsKey} from "./settings.js";
|
||||||
import {recalculate} from "./socket.js";
|
import {recalculate} from "./socket.js";
|
||||||
import {applyTokenSizeOffset, enumeratedZip, getSnapPointForEntity, getSnapPointForToken, getTokenShape, highlightTokenShape, sum} from "./util.js";
|
import {applyTokenSizeOffset, enumeratedZip, getSnapPointForEntity, getSnapPointForToken, getTokenShape, highlightTokenShape, sum} from "./util.js";
|
||||||
@@ -133,7 +133,7 @@ function scheduleMeasurement(destination, event) {
|
|||||||
const mt = event._measureTime || 0;
|
const mt = event._measureTime || 0;
|
||||||
const originalEvent = event.data.originalEvent;
|
const originalEvent = event.data.originalEvent;
|
||||||
if (Date.now() - mt > measurementInterval) {
|
if (Date.now() - mt > measurementInterval) {
|
||||||
this.measure(destination, {snap: !disableSnap, pathfinding: is_pathfinding_enabled()});
|
this.measure(destination, {snap: !disableSnap, pathfinding: isPathfindingEnabled()});
|
||||||
event._measureTime = Date.now();
|
event._measureTime = Date.now();
|
||||||
this._state = Ruler.STATES.MEASURING;
|
this._state = Ruler.STATES.MEASURING;
|
||||||
cancelScheduledMeasurement.call(this);
|
cancelScheduledMeasurement.call(this);
|
||||||
@@ -169,7 +169,7 @@ export function measure(destination, options={}) {
|
|||||||
this.waypoints = this.waypoints.filter(waypoint => !waypoint.isPathfinding);
|
this.waypoints = this.waypoints.filter(waypoint => !waypoint.isPathfinding);
|
||||||
|
|
||||||
if (isToken && options.pathfinding) {
|
if (isToken && options.pathfinding) {
|
||||||
let path = find_path(getGridPositionFromPixelsObj(this.waypoints[this.waypoints.length - 1]), getGridPositionFromPixelsObj(destination));
|
let path = findPath(getGridPositionFromPixelsObj(this.waypoints[this.waypoints.length - 1]), getGridPositionFromPixelsObj(destination));
|
||||||
if (path) {
|
if (path) {
|
||||||
path = path.map(point => getCenterFromGridPositionObj(point));
|
path = path.map(point => getCenterFromGridPositionObj(point));
|
||||||
path.forEach(point => point.isPathfinding = true);
|
path.forEach(point => point.isPathfinding = true);
|
||||||
|
|||||||
+40
-40
@@ -1,35 +1,57 @@
|
|||||||
import {getCenterFromGridPositionObj} from "./foundry_fixes.js";
|
import {getCenterFromGridPositionObj} from "./foundry_fixes.js";
|
||||||
import { togglePathfinding } from "./keybindings.js";
|
import {togglePathfinding} from "./keybindings.js";
|
||||||
import {settingsKey} from "./settings.js";
|
import {settingsKey} from "./settings.js";
|
||||||
|
|
||||||
// TODO Wipe cache if walls layer is being modified
|
// TODO Wipe cache if walls layer is being modified
|
||||||
let cached_nodes = undefined;
|
let cachedNodes = undefined;
|
||||||
|
|
||||||
export function is_pathfinding_enabled() {
|
export function isPathfindingEnabled() {
|
||||||
if (!game.settings.get(settingsKey, "allowPathfinding"))
|
if (!game.settings.get(settingsKey, "allowPathfinding"))
|
||||||
return false;
|
return false;
|
||||||
return game.settings.get(settingsKey, "autoPathfinding") != togglePathfinding;
|
return game.settings.get(settingsKey, "autoPathfinding") != togglePathfinding;
|
||||||
}
|
}
|
||||||
|
|
||||||
function get_node(pos, layer=0, initialize=true) {
|
export function findPath(from, to) {
|
||||||
if (!cached_nodes)
|
const lastNode = calculatePath(from, to);
|
||||||
cached_nodes = new Map();
|
if (!lastNode)
|
||||||
|
return null;
|
||||||
|
const path = [];
|
||||||
|
let currentNode = lastNode;
|
||||||
|
while (currentNode) {
|
||||||
|
// TODO Check if the distance doesn't change
|
||||||
|
if (path.length >= 2 && !canvas.walls.checkCollision(new Ray(getCenterFromGridPositionObj(currentNode.node), getCenterFromGridPositionObj(path[path.length - 2]))))
|
||||||
|
// Replace last waypoint if the current waypoint leads to a valid path
|
||||||
|
path[path.length - 1] = {x: currentNode.node.x, y: currentNode.node.y};
|
||||||
|
else
|
||||||
|
path.push({x: currentNode.node.x, y: currentNode.node.y});
|
||||||
|
currentNode = currentNode.previous;
|
||||||
|
}
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function wipePathfindingCache() {
|
||||||
|
cachedNodes = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getNode(pos, layer=0, initialize=true) {
|
||||||
|
if (!cachedNodes)
|
||||||
|
cachedNodes = new Map();
|
||||||
if (!cachedNodes[layer])
|
if (!cachedNodes[layer])
|
||||||
// TODO Check if ceil is the right thing to do here
|
// TODO Check if ceil is the right thing to do here
|
||||||
cached_nodes.set(layer, new Array(Math.ceil(canvas.dimensions.sceneHeight / canvas.dimensions.size)));
|
cachedNodes.set(layer, new Array(Math.ceil(canvas.dimensions.sceneHeight / canvas.dimensions.size)));
|
||||||
if (!cached_nodes[layer][pos.y])
|
if (!cachedNodes[layer][pos.y])
|
||||||
cached_nodes[layer][pos.y] = new Array(Math.ceil(canvas.dimensions.sceneWidth / canvas.dimensions.size));
|
cachedNodes[layer][pos.y] = new Array(Math.ceil(canvas.dimensions.sceneWidth / canvas.dimensions.size));
|
||||||
if (!cached_nodes[layer][pos.y][pos.x]) {
|
if (!cachedNodes[layer][pos.y][pos.x]) {
|
||||||
cached_nodes[layer][pos.y][pos.x] = {x: pos.x, y: pos.y, layer: layer};
|
cachedNodes[layer][pos.y][pos.x] = {x: pos.x, y: pos.y, layer: layer};
|
||||||
}
|
}
|
||||||
|
|
||||||
const node = cached_nodes[layer][pos.y][pos.x];
|
const node = cachedNodes[layer][pos.y][pos.x];
|
||||||
if (initialize && !node.edges) {
|
if (initialize && !node.edges) {
|
||||||
node.edges = [];
|
node.edges = [];
|
||||||
for (const neighborPos of neighbors(pos)) {
|
for (const neighborPos of neighbors(pos)) {
|
||||||
// TODO Work with pixels instead of grid locations
|
// TODO Work with pixels instead of grid locations
|
||||||
if (!canvas.walls.checkCollision(new Ray(getCenterFromGridPositionObj(pos), getCenterFromGridPositionObj(neighborPos)))) {
|
if (!canvas.walls.checkCollision(new Ray(getCenterFromGridPositionObj(pos), getCenterFromGridPositionObj(neighborPos)))) {
|
||||||
const neighbor = get_node(neighborPos, layer, false);
|
const neighbor = getNode(neighborPos, layer, false);
|
||||||
// TODO We currently assume a cost of one for all transitions. Change this for 5/10/5 or difficult terrain support
|
// TODO We currently assume a cost of one for all transitions. Change this for 5/10/5 or difficult terrain support
|
||||||
// We charge an extra 0.0001 for diagonals to unnecessary diagonal steps
|
// We charge an extra 0.0001 for diagonals to unnecessary diagonal steps
|
||||||
const isDiagonal = node.x !== neighborPos.x && node.y !== neighborPos.y;
|
const isDiagonal = node.x !== neighborPos.x && node.y !== neighborPos.y;
|
||||||
@@ -50,8 +72,8 @@ function* neighbors(pos) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function calculate_path(from, to) {
|
function calculatePath(from, to) {
|
||||||
const nextNodes = [{node: get_node(to), cost: 0, estimated: estimate_cost(to, from), previous: null}];
|
const nextNodes = [{node: getNode(to), cost: 0, estimated: estimateCost(to, from), previous: null}];
|
||||||
const previousNodes = new Set();
|
const previousNodes = new Set();
|
||||||
while (nextNodes.length > 0) {
|
while (nextNodes.length > 0) {
|
||||||
// Sort by estimated cost, high to low
|
// Sort by estimated cost, high to low
|
||||||
@@ -63,10 +85,10 @@ function calculate_path(from, to) {
|
|||||||
return currentNode;
|
return currentNode;
|
||||||
previousNodes.add(currentNode.node);
|
previousNodes.add(currentNode.node);
|
||||||
for (const edge of currentNode.node.edges) {
|
for (const edge of currentNode.node.edges) {
|
||||||
const neighborNode = get_node(edge.target);
|
const neighborNode = getNode(edge.target);
|
||||||
if (previousNodes.has(neighborNode))
|
if (previousNodes.has(neighborNode))
|
||||||
continue;
|
continue;
|
||||||
const neighbor = {node: neighborNode, cost: currentNode.cost + edge.cost, estimated: currentNode.cost + edge.cost + estimate_cost(neighborNode, from), previous: currentNode};
|
const neighbor = {node: neighborNode, cost: currentNode.cost + edge.cost, estimated: currentNode.cost + edge.cost + estimateCost(neighborNode, from), previous: currentNode};
|
||||||
const neighborIndex = nextNodes.findIndex(node => node.node === neighbor.node);
|
const neighborIndex = nextNodes.findIndex(node => node.node === neighbor.node);
|
||||||
if (neighborIndex >= 0) {
|
if (neighborIndex >= 0) {
|
||||||
// If the neighbor is cheaper to reach via the current route than through previously discovered routes, replace it
|
// If the neighbor is cheaper to reach via the current route than through previously discovered routes, replace it
|
||||||
@@ -81,28 +103,6 @@ function calculate_path(from, to) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function estimate_cost(pos, target) {
|
function estimateCost(pos, target) {
|
||||||
return Math.max(Math.abs(pos.x - target.x), Math.abs(pos.y - target.y));
|
return Math.max(Math.abs(pos.x - target.x), Math.abs(pos.y - target.y));
|
||||||
}
|
}
|
||||||
|
|
||||||
export function find_path(from, to) {
|
|
||||||
const lastNode = calculate_path(from, to);
|
|
||||||
if (!lastNode)
|
|
||||||
return null;
|
|
||||||
const path = [];
|
|
||||||
let currentNode = lastNode;
|
|
||||||
while (currentNode) {
|
|
||||||
// TODO Check if the distance doesn't change
|
|
||||||
if (path.length >= 2 && !canvas.walls.checkCollision(new Ray(getCenterFromGridPositionObj(currentNode.node), getCenterFromGridPositionObj(path[path.length - 2]))))
|
|
||||||
// Replace last waypoint if the current waypoint leads to a valid path
|
|
||||||
path[path.length - 1] = {x: currentNode.node.x, y: currentNode.node.y};
|
|
||||||
else
|
|
||||||
path.push({x: currentNode.node.x, y: currentNode.node.y});
|
|
||||||
currentNode = currentNode.previous;
|
|
||||||
}
|
|
||||||
return path;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function wipe_cache() {
|
|
||||||
cached_nodes = undefined;
|
|
||||||
}
|
|
||||||
|
|||||||
+2
-2
@@ -2,7 +2,7 @@ import {currentSpeedProvider, getColorForDistanceAndToken, getRangesFromSpeedPro
|
|||||||
import {getHexSizeSupportTokenGridCenter} from "./compatibility.js";
|
import {getHexSizeSupportTokenGridCenter} from "./compatibility.js";
|
||||||
import {cancelScheduledMeasurement, measure} from "./foundry_imports.js"
|
import {cancelScheduledMeasurement, measure} from "./foundry_imports.js"
|
||||||
import {getMovementHistory} from "./movement_tracking.js";
|
import {getMovementHistory} from "./movement_tracking.js";
|
||||||
import {wipe_cache} from "./pathfinding.js";
|
import {wipePathfindingCache} from "./pathfinding.js";
|
||||||
import {settingsKey} from "./settings.js";
|
import {settingsKey} from "./settings.js";
|
||||||
import {getSnapPointForEntity} from "./util.js";
|
import {getSnapPointForEntity} from "./util.js";
|
||||||
|
|
||||||
@@ -177,7 +177,7 @@ export function extendRuler() {
|
|||||||
return;
|
return;
|
||||||
const ruler = canvas.controls.ruler;
|
const ruler = canvas.controls.ruler;
|
||||||
ruler.clear();
|
ruler.clear();
|
||||||
wipe_cache();
|
wipePathfindingCache();
|
||||||
ruler._state = Ruler.STATES.STARTING;
|
ruler._state = Ruler.STATES.STARTING;
|
||||||
let entityCenter;
|
let entityCenter;
|
||||||
if (isToken && canvas.grid.isHex && game.modules.get("hex-size-support")?.active && CONFIG.hexSizeSupport.getAltSnappingFlag(entity))
|
if (isToken && canvas.grid.isHex && game.modules.get("hex-size-support")?.active && CONFIG.hexSizeSupport.getAltSnappingFlag(entity))
|
||||||
|
|||||||
Reference in New Issue
Block a user