Penalize combat path backsteps at 2x tile cost.
Track the previous standing tile and prefer routes that do not immediately reverse while chasing, including ranged approach Dijkstra costs.
@@ -65,6 +65,7 @@ import { awardCombatHitXp, awardCombatAvoidanceXp, awardDamageHealthXp, syncPlay
65
65
import { chunkBoundsAround, findPath } from "../navigation/astar.js";
66
66
import { tileToChunk } from "../world/coordinates.js";
67
67
import { findPathToNearestGoal } from "../../../shared/navigation/bfs.js";
68
import { withCombatBackstepCost } from "../../../shared/navigation/combatPathCost.js";
68
69
import { CHUNK_SIZE, GAIT, VISIBLE_CHUNK_RADIUS } from "../../../shared/protocol/constants.js";
69
70
import { stackQuantity, insertStack } from "../../../shared/items/inventory.js";
70
71
import { placeOrMergeGroundStack, bagView } from "../items/bags.js";
@@ -1320,6 +1321,20 @@ function shouldSeekCoverApproach (world, handle)
1320
1321
return !mobHasRangedRetaliation(world, handle);
1321
1322
}
1322
1323
1324
/** Double cost for stepping back onto the tile just left while chasing. */
1325
function combatApproachStepCost (position, baseCost = null)
1326
{
1327
const prevX = position?.prevTileX;
1328
const prevY = position?.prevTileY;
1329
return (x, y) => withCombatBackstepCost(
1330
baseCost ? baseCost(x, y) : 1,
1331
x,
1332
y,
1333
prevX,
1334
prevY
1335
);
1336
}
1337
1323
1338
function coverStepCost (world, threatPos, goalX, goalY, x, y)
1324
1339
{
1325
1340
// Final adjacent step is always in LOS — don't punish the last lunge.
@@ -1401,7 +1416,10 @@ function findCoverMeleeApproachPath (world, entityId, handle, position, targetPo
1401
1416
bounds.maxY,
1402
1417
{
1403
1418
preferVertical,
1404
stepCost: (x, y) => coverStepCost(world, targetPos, goal.x, goal.y, x, y)
1419
stepCost: combatApproachStepCost(
1420
position,
1421
(x, y) => coverStepCost(world, targetPos, goal.x, goal.y, x, y)
1422
)
1405
1423
}
1406
1424
);
1407
1425
if (!tiles.length)
@@ -1604,7 +1622,7 @@ export function queueMeleeApproach (world, entityId, handle, position, targetPos
1604
1622
bounds.minY,
1605
1623
bounds.maxX,
1606
1624
bounds.maxY,
1607
{ preferVertical }
1625
{ preferVertical, stepCost: combatApproachStepCost(position) }
1608
1626
);
1609
1627
if (!tiles.length)
1610
1628
{
@@ -1677,7 +1695,8 @@ export function queueStandInRangeApproach (
1677
1695
bounds.minX,
1678
1696
bounds.minY,
1679
1697
bounds.maxX,
1680
bounds.maxY
1698
bounds.maxY,
1699
{ stepCost: combatApproachStepCost(position) }
1681
1700
);
1682
1701
if (!tiles.length)
1683
1702
{
@@ -65,6 +65,7 @@ import { awardCombatHitXp, awardCombatAvoidanceXp, awardDamageHealthXp, syncPlay65
import { chunkBoundsAround, findPath } from "../navigation/astar.js";65
import { chunkBoundsAround, findPath } from "../navigation/astar.js";66
import { tileToChunk } from "../world/coordinates.js";66
import { tileToChunk } from "../world/coordinates.js";67
import { findPathToNearestGoal } from "../../../shared/navigation/bfs.js";67
import { findPathToNearestGoal } from "../../../shared/navigation/bfs.js";68
import { withCombatBackstepCost } from "../../../shared/navigation/combatPathCost.js";68
import { CHUNK_SIZE, GAIT, VISIBLE_CHUNK_RADIUS } from "../../../shared/protocol/constants.js";69
import { CHUNK_SIZE, GAIT, VISIBLE_CHUNK_RADIUS } from "../../../shared/protocol/constants.js";69
import { stackQuantity, insertStack } from "../../../shared/items/inventory.js";70
import { stackQuantity, insertStack } from "../../../shared/items/inventory.js";70
import { placeOrMergeGroundStack, bagView } from "../items/bags.js";71
import { placeOrMergeGroundStack, bagView } from "../items/bags.js";@@ -1320,6 +1321,20 @@ function shouldSeekCoverApproach (world, handle)1320
return !mobHasRangedRetaliation(world, handle);1321
return !mobHasRangedRetaliation(world, handle);1321
}1322
}1322
1323
1324
/** Double cost for stepping back onto the tile just left while chasing. */1325
function combatApproachStepCost (position, baseCost = null)1326
{1327
const prevX = position?.prevTileX;1328
const prevY = position?.prevTileY;1329
return (x, y) => withCombatBackstepCost(1330
baseCost ? baseCost(x, y) : 1,1331
x,1332
y,1333
prevX,1334
prevY1335
);1336
}1337
1323
function coverStepCost (world, threatPos, goalX, goalY, x, y)1338
function coverStepCost (world, threatPos, goalX, goalY, x, y)1324
{1339
{1325
// Final adjacent step is always in LOS — don't punish the last lunge.1340
// Final adjacent step is always in LOS — don't punish the last lunge.@@ -1401,7 +1416,10 @@ function findCoverMeleeApproachPath (world, entityId, handle, position, targetPo1401
bounds.maxY,1416
bounds.maxY,1402
{1417
{1403
preferVertical,1418
preferVertical,1404
stepCost: (x, y) => coverStepCost(world, targetPos, goal.x, goal.y, x, y)1419
stepCost: combatApproachStepCost(1420
position,1421
(x, y) => coverStepCost(world, targetPos, goal.x, goal.y, x, y)1422
)1405
}1423
}1406
);1424
);1407
if (!tiles.length)1425
if (!tiles.length)@@ -1604,7 +1622,7 @@ export function queueMeleeApproach (world, entityId, handle, position, targetPos1604
bounds.minY,1622
bounds.minY,1605
bounds.maxX,1623
bounds.maxX,1606
bounds.maxY,1624
bounds.maxY,1607
{ preferVertical }1625
{ preferVertical, stepCost: combatApproachStepCost(position) }1608
);1626
);1609
if (!tiles.length)1627
if (!tiles.length)1610
{1628
{@@ -1677,7 +1695,8 @@ export function queueStandInRangeApproach (1677
bounds.minX,1695
bounds.minX,1678
bounds.minY,1696
bounds.minY,1679
bounds.maxX,1697
bounds.maxX,1680
bounds.maxY1698
bounds.maxY,1699
{ stepCost: combatApproachStepCost(position) }1681
);1700
);1682
if (!tiles.length)1701
if (!tiles.length)1683
{1702
{@@ -95,6 +95,7 @@ import {
95
95
PATH_GROUND_STEP_COST,
96
96
SOFT_OCCUPIED_DWELL_MS
97
97
} from "../../../shared/navigation/pathGround.js";
98
import { withCombatBackstepCost } from "../../../shared/navigation/combatPathCost.js";
98
99
import { GroundType, normalizeGround } from "../../../shared/definitions/tiles.js";
99
100
import { clearFollow, setFollow, stepFollow, recordFollowTrail, consumeFollowTrail } from "./follow.js";
100
101
import { ensureFlightComponent, stepFreeFlight } from "./freeFlight.js";
@@ -1600,6 +1601,8 @@ export class WorkerWorld
1600
1601
const flying = this.#isFlying(handle);
1601
1602
if (!flying)
1602
1603
{ this.#vacate(entityId, fromTileX, fromTileY); }
1604
position.prevTileX = fromTileX;
1605
position.prevTileY = fromTileY;
1603
1606
position.tileX = next.x;
1604
1607
position.tileY = next.y;
1605
1608
tiles.shift();
@@ -1672,7 +1675,8 @@ export class WorkerWorld
1672
1675
this.pathfindOptions({
1673
1676
preferVertical: stuckHandle != null && this.ecs.has(stuckHandle, Component.character),
1674
1677
preferPaths: !inCombat,
1675
entityId
1678
entityId,
1679
penalizeCombatBackstep: inCombat
1676
1680
})
1677
1681
);
1678
1682
path.stuckSince = now;
@@ -2260,20 +2264,36 @@ export class WorkerWorld
2260
2264
}
2261
2265
2262
2266
/**
2263
* @param {{ preferVertical?: boolean, preferPaths?: boolean, entityId?: number | null }} [options]
2267
* @param {{ preferVertical?: boolean, preferPaths?: boolean, entityId?: number | null, penalizeCombatBackstep?: boolean }} [options]
2264
2268
*/
2265
pathfindOptions ({ preferVertical = false, preferPaths = false, entityId = null } = {})
2269
pathfindOptions ({ preferVertical = false, preferPaths = false, entityId = null, penalizeCombatBackstep = false } = {})
2266
2270
{
2267
2271
const options = { preferVertical: !!preferVertical };
2268
2272
const now = Date.now();
2269
2273
const dimensionId = entityId != null ? this.#entityDimensionId(entityId) : OVERWORLD_ID;
2270
options.stepCost = (x, y) => stepCostForGround(
2271
this.groundTypeAt(x, y, dimensionId),
2274
let prevTileX = null;
2275
let prevTileY = null;
2276
if (penalizeCombatBackstep && entityId != null)
2277
{
2278
const handle = this.ecs.handleOf(entityId);
2279
const position = handle != null ? this.ecs.get(handle, Component.position) : null;
2280
if (position?.prevTileX != null && position?.prevTileY != null)
2272
2281
{
2273
preferPaths: !!preferPaths,
2274
softOccupied: entityId != null && this.#isSoftOccupiedTile(x, y, dimensionId, entityId, now)
2282
prevTileX = position.prevTileX | 0;
2283
prevTileY = position.prevTileY | 0;
2275
2284
}
2276
);
2285
}
2286
options.stepCost = (x, y) =>
2287
{
2288
const base = stepCostForGround(
2289
this.groundTypeAt(x, y, dimensionId),
2290
{
2291
preferPaths: !!preferPaths,
2292
softOccupied: entityId != null && this.#isSoftOccupiedTile(x, y, dimensionId, entityId, now)
2293
}
2294
);
2295
return withCombatBackstepCost(base, x, y, prevTileX, prevTileY);
2296
};
2277
2297
options.minStepCost = preferPaths ? PATH_GROUND_STEP_COST : 1;
2278
2298
return options;
2279
2299
}
@@ -95,6 +95,7 @@ import {95
PATH_GROUND_STEP_COST,95
PATH_GROUND_STEP_COST,96
SOFT_OCCUPIED_DWELL_MS96
SOFT_OCCUPIED_DWELL_MS97
} from "../../../shared/navigation/pathGround.js";97
} from "../../../shared/navigation/pathGround.js";98
import { withCombatBackstepCost } from "../../../shared/navigation/combatPathCost.js";98
import { GroundType, normalizeGround } from "../../../shared/definitions/tiles.js";99
import { GroundType, normalizeGround } from "../../../shared/definitions/tiles.js";99
import { clearFollow, setFollow, stepFollow, recordFollowTrail, consumeFollowTrail } from "./follow.js";100
import { clearFollow, setFollow, stepFollow, recordFollowTrail, consumeFollowTrail } from "./follow.js";100
import { ensureFlightComponent, stepFreeFlight } from "./freeFlight.js";101
import { ensureFlightComponent, stepFreeFlight } from "./freeFlight.js";@@ -1600,6 +1601,8 @@ export class WorkerWorld1600
const flying = this.#isFlying(handle);1601
const flying = this.#isFlying(handle);1601
if (!flying)1602
if (!flying)1602
{ this.#vacate(entityId, fromTileX, fromTileY); }1603
{ this.#vacate(entityId, fromTileX, fromTileY); }1604
position.prevTileX = fromTileX;1605
position.prevTileY = fromTileY;1603
position.tileX = next.x;1606
position.tileX = next.x;1604
position.tileY = next.y;1607
position.tileY = next.y;1605
tiles.shift();1608
tiles.shift();@@ -1672,7 +1675,8 @@ export class WorkerWorld1672
this.pathfindOptions({1675
this.pathfindOptions({1673
preferVertical: stuckHandle != null && this.ecs.has(stuckHandle, Component.character),1676
preferVertical: stuckHandle != null && this.ecs.has(stuckHandle, Component.character),1674
preferPaths: !inCombat,1677
preferPaths: !inCombat,1675
entityId1678
entityId,1679
penalizeCombatBackstep: inCombat1676
})1680
})1677
);1681
);1678
path.stuckSince = now;1682
path.stuckSince = now;@@ -2260,20 +2264,36 @@ export class WorkerWorld2260
}2264
}2261
2265
2262
/**2266
/**2263
* @param {{ preferVertical?: boolean, preferPaths?: boolean, entityId?: number | null }} [options]2267
* @param {{ preferVertical?: boolean, preferPaths?: boolean, entityId?: number | null, penalizeCombatBackstep?: boolean }} [options]2264
*/2268
*/2265
pathfindOptions ({ preferVertical = false, preferPaths = false, entityId = null } = {})2269
pathfindOptions ({ preferVertical = false, preferPaths = false, entityId = null, penalizeCombatBackstep = false } = {})2266
{2270
{2267
const options = { preferVertical: !!preferVertical };2271
const options = { preferVertical: !!preferVertical };2268
const now = Date.now();2272
const now = Date.now();2269
const dimensionId = entityId != null ? this.#entityDimensionId(entityId) : OVERWORLD_ID;2273
const dimensionId = entityId != null ? this.#entityDimensionId(entityId) : OVERWORLD_ID;2270
options.stepCost = (x, y) => stepCostForGround(2274
let prevTileX = null;2271
this.groundTypeAt(x, y, dimensionId),2275
let prevTileY = null;2276
if (penalizeCombatBackstep && entityId != null)2277
{2278
const handle = this.ecs.handleOf(entityId);2279
const position = handle != null ? this.ecs.get(handle, Component.position) : null;2280
if (position?.prevTileX != null && position?.prevTileY != null)2272
{2281
{2273
preferPaths: !!preferPaths,2282
prevTileX = position.prevTileX | 0;2274
softOccupied: entityId != null && this.#isSoftOccupiedTile(x, y, dimensionId, entityId, now)2283
prevTileY = position.prevTileY | 0;2275
}2284
}2276
);2285
}2286
options.stepCost = (x, y) =>2287
{2288
const base = stepCostForGround(2289
this.groundTypeAt(x, y, dimensionId),2290
{2291
preferPaths: !!preferPaths,2292
softOccupied: entityId != null && this.#isSoftOccupiedTile(x, y, dimensionId, entityId, now)2293
}2294
);2295
return withCombatBackstepCost(base, x, y, prevTileX, prevTileY);2296
};2277
options.minStepCost = preferPaths ? PATH_GROUND_STEP_COST : 1;2297
options.minStepCost = preferPaths ? PATH_GROUND_STEP_COST : 1;2278
return options;2298
return options;2279
}2299
}@@ -2,6 +2,7 @@ import test from "node:test";
2
2
import assert from "node:assert/strict";
3
3
import { findPath, orderedCardinals } from "../src/navigation/astar.js";
4
4
import { PATH_GROUND_STEP_COST, SOFT_OCCUPIED_STEP_PENALTY, stepCostForGround } from "../../shared/navigation/pathGround.js";
5
import { withCombatBackstepCost } from "../../shared/navigation/combatPathCost.js";
5
6
import { GroundType } from "../../shared/definitions/tiles.js";
6
7
7
8
test("A* walks around an impassable tile", () =>
@@ -202,3 +203,28 @@ test("A* still crosses soft-occupied when the detour is much longer", () =>
202
203
);
203
204
assert.ok(path.some((step) => step.x === 1 && step.y === 0), JSON.stringify(path));
204
205
});
206
207
test("A* combat backstep cost prefers not re-entering the previous tile", () =>
208
{
209
// Open field start (0,0) goal (1,1). Paths: (1,0)-(1,1) or (0,1)-(1,1).
210
// Prev tile (1,0) → first path costs 2+1=3, second costs 1+1=2 → prefer (0,1).
211
const prevX = 1;
212
const prevY = 0;
213
const path = findPath(
214
0,
215
0,
216
1,
217
1,
218
() => true,
219
-2,
220
-2,
221
4,
222
4,
223
{
224
stepCost: (x, y) => withCombatBackstepCost(1, x, y, prevX, prevY)
225
}
226
);
227
assert.ok(path.length > 0);
228
assert.deepEqual(path[0], { x: 0, y: 1 }, JSON.stringify(path));
229
assert.ok(!path.some((step) => step.x === 1 && step.y === 0), JSON.stringify(path));
230
});
@@ -2,6 +2,7 @@ import test from "node:test";2
import assert from "node:assert/strict";2
import assert from "node:assert/strict";3
import { findPath, orderedCardinals } from "../src/navigation/astar.js";3
import { findPath, orderedCardinals } from "../src/navigation/astar.js";4
import { PATH_GROUND_STEP_COST, SOFT_OCCUPIED_STEP_PENALTY, stepCostForGround } from "../../shared/navigation/pathGround.js";4
import { PATH_GROUND_STEP_COST, SOFT_OCCUPIED_STEP_PENALTY, stepCostForGround } from "../../shared/navigation/pathGround.js";5
import { withCombatBackstepCost } from "../../shared/navigation/combatPathCost.js";5
import { GroundType } from "../../shared/definitions/tiles.js";6
import { GroundType } from "../../shared/definitions/tiles.js";6
7
7
test("A* walks around an impassable tile", () =>8
test("A* walks around an impassable tile", () =>@@ -202,3 +203,28 @@ test("A* still crosses soft-occupied when the detour is much longer", () =>202
);203
);203
assert.ok(path.some((step) => step.x === 1 && step.y === 0), JSON.stringify(path));204
assert.ok(path.some((step) => step.x === 1 && step.y === 0), JSON.stringify(path));204
});205
});206
207
test("A* combat backstep cost prefers not re-entering the previous tile", () =>208
{209
// Open field start (0,0) goal (1,1). Paths: (1,0)-(1,1) or (0,1)-(1,1).210
// Prev tile (1,0) → first path costs 2+1=3, second costs 1+1=2 → prefer (0,1).211
const prevX = 1;212
const prevY = 0;213
const path = findPath(214
0,215
0,216
1,217
1,218
() => true,219
-2,220
-2,221
4,222
4,223
{224
stepCost: (x, y) => withCombatBackstepCost(1, x, y, prevX, prevY)225
}226
);227
assert.ok(path.length > 0);228
assert.deepEqual(path[0], { x: 0, y: 1 }, JSON.stringify(path));229
assert.ok(!path.some((step) => step.x === 1 && step.y === 0), JSON.stringify(path));230
});@@ -1,6 +1,7 @@
1
1
import test from "node:test";
2
2
import assert from "node:assert/strict";
3
3
import { findPathToNearestGoal } from "../../shared/navigation/bfs.js";
4
import { withCombatBackstepCost } from "../../shared/navigation/combatPathCost.js";
4
5
5
6
test("BFS picks the nearest stand tile toward the target, not the top-left ring tile", () =>
6
7
{
@@ -38,3 +39,20 @@ test("BFS returns empty when already at a goal", () =>
38
39
);
39
40
assert.deepEqual(path, []);
40
41
});
42
43
test("weighted nearest-goal search avoids an expensive backstep tile", () =>
44
{
45
// Start (0,0); goals are (1,0) and (0,1). Prev is (1,0) so prefer (0,1).
46
const path = findPathToNearestGoal(
47
0,
48
0,
49
(x, y) => (x === 1 && y === 0) || (x === 0 && y === 1),
50
() => true,
51
-2,
52
-2,
53
4,
54
4,
55
{ stepCost: (x, y) => withCombatBackstepCost(1, x, y, 1, 0) }
56
);
57
assert.deepEqual(path, [{ x: 0, y: 1 }]);
58
});
@@ -1,6 +1,7 @@1
import test from "node:test";1
import test from "node:test";2
import assert from "node:assert/strict";2
import assert from "node:assert/strict";3
import { findPathToNearestGoal } from "../../shared/navigation/bfs.js";3
import { findPathToNearestGoal } from "../../shared/navigation/bfs.js";4
import { withCombatBackstepCost } from "../../shared/navigation/combatPathCost.js";4
5
5
test("BFS picks the nearest stand tile toward the target, not the top-left ring tile", () =>6
test("BFS picks the nearest stand tile toward the target, not the top-left ring tile", () =>6
{7
{@@ -38,3 +39,20 @@ test("BFS returns empty when already at a goal", () =>38
);39
);39
assert.deepEqual(path, []);40
assert.deepEqual(path, []);40
});41
});42
43
test("weighted nearest-goal search avoids an expensive backstep tile", () =>44
{45
// Start (0,0); goals are (1,0) and (0,1). Prev is (1,0) so prefer (0,1).46
const path = findPathToNearestGoal(47
0,48
0,49
(x, y) => (x === 1 && y === 0) || (x === 0 && y === 1),50
() => true,51
-2,52
-2,53
4,54
4,55
{ stepCost: (x, y) => withCombatBackstepCost(1, x, y, 1, 0) }56
);57
assert.deepEqual(path, [{ x: 0, y: 1 }]);58
});