From 8687777fc1c9ea0f335d679c24ce14007819fb65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20V=C3=B6gele?= Date: Wed, 2 Mar 2022 00:32:18 +0100 Subject: [PATCH] Discourage diagonals --- js/pathfinding.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/js/pathfinding.js b/js/pathfinding.js index 3816927..544889e 100644 --- a/js/pathfinding.js +++ b/js/pathfinding.js @@ -76,17 +76,21 @@ function getNode(pos, token, initialize=true) { // TODO Work with pixels instead of grid locations if (!stepCollidesWithWall(neighborPos, pos, token)) { + const isDiagonal = node.x !== neighborPos.x && node.y !== neighborPos.y && canvas.grid.type === CONST.GRID_TYPES.SQUARE; let edgeCost; if (window.terrainRuler) { // TODO Additional cache for each token shape // TODO Use the correct token shape + // TODO 5-10-5 support let ray = new Ray(getCenterFromGridPositionObj(neighborPos), getCenterFromGridPositionObj(pos)); let measuredDistance = terrainRuler.measureDistances([{ray}], {costFunction: buildCostFunction(token, [{x: 0, y: 0}])})[0]; edgeCost = Math.round(measuredDistance / canvas.dimensions.distance); + // Charge 1.0001 instead of 1 for diagonals to discourage unnecessary diagonals + if (isDiagonal && edgeCost == 1) { + edgeCost = 1.0001; + } } else { - const isDiagonal = node.x !== neighborPos.x && node.y !== neighborPos.y && canvas.grid.type === CONST.GRID_TYPES.SQUARE; - // Count 5-10-5 diagonals as 1.5 (so two add up to 3) and 5-5-5 diagonals as 1.0001 (to discourage unnecessary diagonals) // TODO Account for difficult terrain edgeCost = isDiagonal ? (use5105 ? 1.5 : 1.0001) : 1;