BlueGit

timfa questward

Files

questward / shared / navigation / combatPathCost.js

combatPathCost.js

combatPathCost.js 705 B Download Raw
1/**
2 * Extra A* / Dijkstra cost when combat-pathing back onto the tile just left.
3 * Discourages oscillating between two tiles while chasing.
4 */
5export const COMBAT_BACKSTEP_COST_MULT = 2;
6
7/**
8 * @param {number} baseCost
9 * @param {number} x
10 * @param {number} y
11 * @param {number | null | undefined} prevTileX
12 * @param {number | null | undefined} prevTileY
13 */
14export function withCombatBackstepCost (baseCost, x, y, prevTileX, prevTileY)
15{
16 const cost = Number(baseCost);
17 const safe = Number.isFinite(cost) && cost > 0 ? cost : 1;
18 if (
19 prevTileX != null
20 && prevTileY != null
21 && x === (prevTileX | 0)
22 && y === (prevTileY | 0)
23 )
24 { return safe * COMBAT_BACKSTEP_COST_MULT; }
25 return safe;
26}