BlueGit

timfa questward

Prefer the longer remaining axis in A* pathfinding.

Stops mobs with a small X offset and large Y gap from swapping columns forever; axis-tie still uses player-vs-mob preference on square diagonals.

timfa Tim Falken · pushed by timfa

Authored 2026-08-31 · 2 months ago

Hash
c39d4e1530c76c058d43ae9986e6fbb0e326bfbc
Parent
cd53e91f
2 files changed +49 −17
server/src/navigation/astar.js +31 −7
@@ -73,15 +73,32 @@ const CARDINALS = Object.freeze([
73 73 Object.freeze({ x: 0, y: -1 })
74 74 ]);
75 75
76 /** Tiny f-cost nudge so equal-length paths prefer the caller's axis. */
76 /** Tiny f-cost nudge so equal-length paths prefer the preferred axis. */
77 77 const AXIS_TIE_BIAS = 0.001;
78 78
79 79 /**
80 * Neighbour order: most distance-to-goal reduction first, then preferred axis
81 * (horizontal for mobs, vertical for players) so opposing pathfinders diverge.
80 * Prefer the axis with more remaining Manhattan distance to the goal.
81 * When |dx| === |dy|, use `axisTiePreferVertical` (players vertical, mobs horizontal)
82 * so opposing pathfinders still diverge on square diagonals.
82 83 */
83 export function orderedCardinals (fromX, fromY, goalX, goalY, preferVertical = false)
84 export function preferVerticalAxis (fromX, fromY, goalX, goalY, axisTiePreferVertical = false)
84 85 {
86 const dx = Math.abs(goalX - fromX);
87 const dy = Math.abs(goalY - fromY);
88 if (dy > dx)
89 { return true; }
90 if (dx > dy)
91 { return false; }
92 return !!axisTiePreferVertical;
93 }
94
95 /**
96 * Neighbour order: most distance-to-goal reduction first, then the longer remaining
97 * axis (or axis-tie preference when equal).
98 */
99 export function orderedCardinals (fromX, fromY, goalX, goalY, axisTiePreferVertical = false)
100 {
101 const preferVertical = preferVerticalAxis(fromX, fromY, goalX, goalY, axisTiePreferVertical);
85 102 const currentDist = Math.abs(goalX - fromX) + Math.abs(goalY - fromY);
86 103 return CARDINALS
87 104 .map((dir) =>
@@ -112,12 +129,12 @@ export function orderedCardinals (fromX, fromY, goalX, goalY, preferVertical = f
112 129
113 130 /**
114 131 * @param {object} [options]
115 * @param {boolean} [options.preferVertical] players prefer vertical; mobs (default) prefer horizontal
132 * @param {boolean} [options.preferVertical] axis-tie break only: players vertical, mobs horizontal
116 133 * @param {(x: number, y: number) => number} [options.stepCost] movement cost into a tile (default 1)
117 134 */
118 135 export function findPath (startX, startY, goalX, goalY, isWalkable, minX, minY, maxX, maxY, options = {})
119 136 {
120 const preferVertical = !!options.preferVertical;
137 const axisTiePreferVertical = !!options.preferVertical;
121 138 const stepCost = typeof options.stepCost === "function" ? options.stepCost : null;
122 139 if (!isWalkable(goalX, goalY) || !isWalkable(startX, startY))
123 140 { return []; }
@@ -159,7 +176,14 @@ export function findPath (startX, startY, goalX, goalY, isWalkable, minX, minY,
159 176 if (current.x === goalX && current.y === goalY)
160 177 { return reconstruct(cameFrom, current.x, current.y, minX, minY, width); }
161 178
162 for (const dir of orderedCardinals(current.x, current.y, goalX, goalY, preferVertical))
179 const preferVertical = preferVerticalAxis(
180 current.x,
181 current.y,
182 goalX,
183 goalY,
184 axisTiePreferVertical
185 );
186 for (const dir of orderedCardinals(current.x, current.y, goalX, goalY, axisTiePreferVertical))
163 187 {
164 188 const nx = current.x + dir.x;
165 189 const ny = current.y + dir.y;
@@ -73,15 +73,32 @@ const CARDINALS = Object.freeze([
73 Object.freeze({ x: 0, y: -1 })
73 Object.freeze({ x: 0, y: -1 })
74]);
74]);
75
75
76/** Tiny f-cost nudge so equal-length paths prefer the caller's axis. */
76/** Tiny f-cost nudge so equal-length paths prefer the preferred axis. */
77const AXIS_TIE_BIAS = 0.001;
77const AXIS_TIE_BIAS = 0.001;
78
78
79/**
79/**
80 * Neighbour order: most distance-to-goal reduction first, then preferred axis
80 * Prefer the axis with more remaining Manhattan distance to the goal.
81 * (horizontal for mobs, vertical for players) so opposing pathfinders diverge.
81 * When |dx| === |dy|, use `axisTiePreferVertical` (players vertical, mobs horizontal)
82 * so opposing pathfinders still diverge on square diagonals.
82 */
83 */
83export function orderedCardinals (fromX, fromY, goalX, goalY, preferVertical = false)
84export function preferVerticalAxis (fromX, fromY, goalX, goalY, axisTiePreferVertical = false)
84{
85{
86 const dx = Math.abs(goalX - fromX);
87 const dy = Math.abs(goalY - fromY);
88 if (dy > dx)
89 { return true; }
90 if (dx > dy)
91 { return false; }
92 return !!axisTiePreferVertical;
93}
94
95/**
96 * Neighbour order: most distance-to-goal reduction first, then the longer remaining
97 * axis (or axis-tie preference when equal).
98 */
99export function orderedCardinals (fromX, fromY, goalX, goalY, axisTiePreferVertical = false)
100{
101 const preferVertical = preferVerticalAxis(fromX, fromY, goalX, goalY, axisTiePreferVertical);
85 const currentDist = Math.abs(goalX - fromX) + Math.abs(goalY - fromY);
102 const currentDist = Math.abs(goalX - fromX) + Math.abs(goalY - fromY);
86 return CARDINALS
103 return CARDINALS
87 .map((dir) =>
104 .map((dir) =>
@@ -112,12 +129,12 @@ export function orderedCardinals (fromX, fromY, goalX, goalY, preferVertical = f
112
129
113/**
130/**
114 * @param {object} [options]
131 * @param {object} [options]
115 * @param {boolean} [options.preferVertical] players prefer vertical; mobs (default) prefer horizontal
132 * @param {boolean} [options.preferVertical] axis-tie break only: players vertical, mobs horizontal
116 * @param {(x: number, y: number) => number} [options.stepCost] movement cost into a tile (default 1)
133 * @param {(x: number, y: number) => number} [options.stepCost] movement cost into a tile (default 1)
117 */
134 */
118export function findPath (startX, startY, goalX, goalY, isWalkable, minX, minY, maxX, maxY, options = {})
135export function findPath (startX, startY, goalX, goalY, isWalkable, minX, minY, maxX, maxY, options = {})
119{
136{
120 const preferVertical = !!options.preferVertical;
137 const axisTiePreferVertical = !!options.preferVertical;
121 const stepCost = typeof options.stepCost === "function" ? options.stepCost : null;
138 const stepCost = typeof options.stepCost === "function" ? options.stepCost : null;
122 if (!isWalkable(goalX, goalY) || !isWalkable(startX, startY))
139 if (!isWalkable(goalX, goalY) || !isWalkable(startX, startY))
123 { return []; }
140 { return []; }
@@ -159,7 +176,14 @@ export function findPath (startX, startY, goalX, goalY, isWalkable, minX, minY,
159 if (current.x === goalX && current.y === goalY)
176 if (current.x === goalX && current.y === goalY)
160 { return reconstruct(cameFrom, current.x, current.y, minX, minY, width); }
177 { return reconstruct(cameFrom, current.x, current.y, minX, minY, width); }
161
178
162 for (const dir of orderedCardinals(current.x, current.y, goalX, goalY, preferVertical))
179 const preferVertical = preferVerticalAxis(
180 current.x,
181 current.y,
182 goalX,
183 goalY,
184 axisTiePreferVertical
185 );
186 for (const dir of orderedCardinals(current.x, current.y, goalX, goalY, axisTiePreferVertical))
163 {
187 {
164 const nx = current.x + dir.x;
188 const nx = current.x + dir.x;
165 const ny = current.y + dir.y;
189 const ny = current.y + dir.y;
server/tests/astar.test.js +18 −10
@@ -17,29 +17,37 @@ test("A* returns empty when the goal is blocked", () =>
17 17 assert.deepEqual(path, []);
18 18 });
19 19
20 test("ordered cardinals prefer distance reduction then axis", () =>
20 test("ordered cardinals prefer longer remaining axis over axis-tie flag", () =>
21 21 {
22 const horizontalPrefer = orderedCardinals(0, 0, -1, -3, false);
23 assert.deepEqual(horizontalPrefer[0], { x: -1, y: 0 });
24 assert.deepEqual(horizontalPrefer[1], { x: 0, y: -1 });
22 // |dy|=3 > |dx|=1 → vertical first even when axis-tie prefers horizontal.
23 const tallGoal = orderedCardinals(0, 0, -1, -3, false);
24 assert.deepEqual(tallGoal[0], { x: 0, y: -1 });
25 assert.deepEqual(tallGoal[1], { x: -1, y: 0 });
25 26
26 const verticalPrefer = orderedCardinals(0, 0, -1, -3, true);
27 assert.deepEqual(verticalPrefer[0], { x: 0, y: -1 });
28 assert.deepEqual(verticalPrefer[1], { x: -1, y: 0 });
27 // |dx|=3 > |dy|=1 → horizontal first even when axis-tie prefers vertical.
28 const wideGoal = orderedCardinals(0, 0, -3, -1, true);
29 assert.deepEqual(wideGoal[0], { x: -1, y: 0 });
30 assert.deepEqual(wideGoal[1], { x: 0, y: -1 });
29 31
30 32 const horizontalOnly = orderedCardinals(0, 0, 3, 0, false);
31 33 assert.deepEqual(horizontalOnly[0], { x: 1, y: 0 });
32 34 });
33 35
34 test("player and mob axis bias diverge on diagonal goals", () =>
36 test("axis-tie preference diverges only when |dx| equals |dy|", () =>
35 37 {
36 38 const open = () => true;
37 const mobPath = findPath(0, 0, -1, -3, open, -8, -8, 8, 8, { preferVertical: false });
38 const playerPath = findPath(0, 0, -1, -3, open, -8, -8, 8, 8, { preferVertical: true });
39 const mobPath = findPath(0, 0, -2, -2, open, -8, -8, 8, 8, { preferVertical: false });
40 const playerPath = findPath(0, 0, -2, -2, open, -8, -8, 8, 8, { preferVertical: true });
39 41 assert.equal(mobPath.length, 4);
40 42 assert.equal(playerPath.length, 4);
41 43 assert.deepEqual(mobPath[0], { x: -1, y: 0 });
42 44 assert.deepEqual(playerPath[0], { x: 0, y: -1 });
45
46 // Tall goal: both close the larger vertical gap first (no mob/player swap dance).
47 const tallMob = findPath(0, 0, -1, -3, open, -8, -8, 8, 8, { preferVertical: false });
48 const tallPlayer = findPath(0, 0, -1, -3, open, -8, -8, 8, 8, { preferVertical: true });
49 assert.deepEqual(tallMob[0], { x: 0, y: -1 });
50 assert.deepEqual(tallPlayer[0], { x: 0, y: -1 });
43 51 });
44 52
45 53 test("A* stepCost prefers the cheaper route around an expensive corridor", () =>
@@ -17,29 +17,37 @@ test("A* returns empty when the goal is blocked", () =>
17 assert.deepEqual(path, []);
17 assert.deepEqual(path, []);
18});
18});
19
19
20test("ordered cardinals prefer distance reduction then axis", () =>
20test("ordered cardinals prefer longer remaining axis over axis-tie flag", () =>
21{
21{
22 const horizontalPrefer = orderedCardinals(0, 0, -1, -3, false);
22 // |dy|=3 > |dx|=1 → vertical first even when axis-tie prefers horizontal.
23 assert.deepEqual(horizontalPrefer[0], { x: -1, y: 0 });
23 const tallGoal = orderedCardinals(0, 0, -1, -3, false);
24 assert.deepEqual(horizontalPrefer[1], { x: 0, y: -1 });
24 assert.deepEqual(tallGoal[0], { x: 0, y: -1 });
25 assert.deepEqual(tallGoal[1], { x: -1, y: 0 });
25
26
26 const verticalPrefer = orderedCardinals(0, 0, -1, -3, true);
27 // |dx|=3 > |dy|=1 → horizontal first even when axis-tie prefers vertical.
27 assert.deepEqual(verticalPrefer[0], { x: 0, y: -1 });
28 const wideGoal = orderedCardinals(0, 0, -3, -1, true);
28 assert.deepEqual(verticalPrefer[1], { x: -1, y: 0 });
29 assert.deepEqual(wideGoal[0], { x: -1, y: 0 });
30 assert.deepEqual(wideGoal[1], { x: 0, y: -1 });
29
31
30 const horizontalOnly = orderedCardinals(0, 0, 3, 0, false);
32 const horizontalOnly = orderedCardinals(0, 0, 3, 0, false);
31 assert.deepEqual(horizontalOnly[0], { x: 1, y: 0 });
33 assert.deepEqual(horizontalOnly[0], { x: 1, y: 0 });
32});
34});
33
35
34test("player and mob axis bias diverge on diagonal goals", () =>
36test("axis-tie preference diverges only when |dx| equals |dy|", () =>
35{
37{
36 const open = () => true;
38 const open = () => true;
37 const mobPath = findPath(0, 0, -1, -3, open, -8, -8, 8, 8, { preferVertical: false });
39 const mobPath = findPath(0, 0, -2, -2, open, -8, -8, 8, 8, { preferVertical: false });
38 const playerPath = findPath(0, 0, -1, -3, open, -8, -8, 8, 8, { preferVertical: true });
40 const playerPath = findPath(0, 0, -2, -2, open, -8, -8, 8, 8, { preferVertical: true });
39 assert.equal(mobPath.length, 4);
41 assert.equal(mobPath.length, 4);
40 assert.equal(playerPath.length, 4);
42 assert.equal(playerPath.length, 4);
41 assert.deepEqual(mobPath[0], { x: -1, y: 0 });
43 assert.deepEqual(mobPath[0], { x: -1, y: 0 });
42 assert.deepEqual(playerPath[0], { x: 0, y: -1 });
44 assert.deepEqual(playerPath[0], { x: 0, y: -1 });
45
46 // Tall goal: both close the larger vertical gap first (no mob/player swap dance).
47 const tallMob = findPath(0, 0, -1, -3, open, -8, -8, 8, 8, { preferVertical: false });
48 const tallPlayer = findPath(0, 0, -1, -3, open, -8, -8, 8, 8, { preferVertical: true });
49 assert.deepEqual(tallMob[0], { x: 0, y: -1 });
50 assert.deepEqual(tallPlayer[0], { x: 0, y: -1 });
43});
51});
44
52
45test("A* stepCost prefers the cheaper route around an expensive corridor", () =>
53test("A* stepCost prefers the cheaper route around an expensive corridor", () =>