Soft-occupy tiles after 10s standing with a light pathfinding cost.
Entities that stay put mark their tile as slightly more expensive to path through (weaker than road preference) until they leave, without blocking movement.
@@ -291,7 +291,7 @@ function queueCatchUp (world, entityId, handle, position, targetPos, path)
291
291
bounds.minY,
292
292
bounds.maxX,
293
293
bounds.maxY,
294
world.pathfindOptions({ preferVertical: true, preferPaths: !inCombat })
294
world.pathfindOptions({ preferVertical: true, preferPaths: !inCombat, entityId })
295
295
);
296
296
const movement = world.ecs.get(handle, Component.movement);
297
297
if (movement)
@@ -291,7 +291,7 @@ function queueCatchUp (world, entityId, handle, position, targetPos, path)291
bounds.minY,291
bounds.minY,292
bounds.maxX,292
bounds.maxX,293
bounds.maxY,293
bounds.maxY,294
world.pathfindOptions({ preferVertical: true, preferPaths: !inCombat })294
world.pathfindOptions({ preferVertical: true, preferPaths: !inCombat, entityId })295
);295
);296
const movement = world.ecs.get(handle, Component.movement);296
const movement = world.ecs.get(handle, Component.movement);297
if (movement)297
if (movement)@@ -78,7 +78,11 @@ import {
78
78
staminaCostPerRunStep,
79
79
publicStaminaView
80
80
} from "../../../shared/stamina/rules.js";
81
import { stepCostForGround, PATH_GROUND_STEP_COST } from "../../../shared/navigation/pathGround.js";
81
import {
82
stepCostForGround,
83
PATH_GROUND_STEP_COST,
84
SOFT_OCCUPIED_DWELL_MS
85
} from "../../../shared/navigation/pathGround.js";
82
86
import { GroundType, normalizeGround } from "../../../shared/definitions/tiles.js";
83
87
import { clearFollow, setFollow, stepFollow, recordFollowTrail, consumeFollowTrail } from "./follow.js";
84
88
import { ensureFlightComponent, stepFreeFlight } from "./freeFlight.js";
@@ -876,7 +880,7 @@ export class WorkerWorld
876
880
bounds.minY,
877
881
bounds.maxX,
878
882
bounds.maxY,
879
this.pathfindOptions({ preferVertical: true, preferPaths: true })
883
this.pathfindOptions({ preferVertical: true, preferPaths: true, entityId })
880
884
);
881
885
movement.walkCarry = 0;
882
886
const events = [];
@@ -1484,7 +1488,7 @@ export class WorkerWorld
1484
1488
position.tileY = next.y;
1485
1489
tiles.shift();
1486
1490
if (!flying)
1487
{ this.#occupy(entityId, position.tileX, position.tileY); }
1491
{ this.#occupy(entityId, position.tileX, position.tileY, now); }
1488
1492
path.stuckSince = now;
1489
1493
path.stuckTileX = position.tileX;
1490
1494
path.stuckTileY = position.tileY;
@@ -1546,7 +1550,8 @@ export class WorkerWorld
1546
1550
bounds.maxY,
1547
1551
this.pathfindOptions({
1548
1552
preferVertical: stuckHandle != null && this.ecs.has(stuckHandle, Component.character),
1549
preferPaths: !inCombat
1553
preferPaths: !inCombat,
1554
entityId
1550
1555
})
1551
1556
);
1552
1557
path.stuckSince = now;
@@ -1651,7 +1656,8 @@ export class WorkerWorld
1651
1656
bounds.minX,
1652
1657
bounds.minY,
1653
1658
bounds.maxX,
1654
bounds.maxY
1659
bounds.maxY,
1660
this.pathfindOptions({ preferPaths: true, entityId: entity.entityId })
1655
1661
);
1656
1662
if (!tiles?.length)
1657
1663
{ continue; }
@@ -1688,7 +1694,8 @@ export class WorkerWorld
1688
1694
bounds.minX,
1689
1695
bounds.minY,
1690
1696
bounds.maxX,
1691
bounds.maxY
1697
bounds.maxY,
1698
this.pathfindOptions({ preferPaths: true, entityId })
1692
1699
);
1693
1700
if (!tiles?.length)
1694
1701
{ return false; }
@@ -1753,7 +1760,8 @@ export class WorkerWorld
1753
1760
bounds.minX,
1754
1761
bounds.minY,
1755
1762
bounds.maxX,
1756
bounds.maxY
1763
bounds.maxY,
1764
this.pathfindOptions({ preferPaths: true, entityId: best.entityId })
1757
1765
);
1758
1766
best.spawnOrigin.stationTileX = stationTileX;
1759
1767
best.spawnOrigin.stationTileY = stationTileY;
@@ -1920,7 +1928,8 @@ export class WorkerWorld
1920
1928
bounds.minX,
1921
1929
bounds.minY,
1922
1930
bounds.maxX,
1923
bounds.maxY
1931
bounds.maxY,
1932
this.pathfindOptions({ preferPaths: true, entityId: entity.entityId })
1924
1933
);
1925
1934
if (tiles?.length)
1926
1935
{ entity.path.tiles = tiles; }
@@ -2130,17 +2139,49 @@ export class WorkerWorld
2130
2139
}
2131
2140
2132
2141
/**
2133
* @param {{ preferVertical?: boolean, preferPaths?: boolean }} [options]
2142
* @param {{ preferVertical?: boolean, preferPaths?: boolean, entityId?: number | null }} [options]
2134
2143
*/
2135
pathfindOptions ({ preferVertical = false, preferPaths = false } = {})
2144
pathfindOptions ({ preferVertical = false, preferPaths = false, entityId = null } = {})
2136
2145
{
2137
2146
const options = { preferVertical: !!preferVertical };
2138
if (preferPaths)
2147
const now = Date.now();
2148
const dimensionId = entityId != null ? this.#entityDimensionId(entityId) : OVERWORLD_ID;
2149
options.stepCost = (x, y) => stepCostForGround(
2150
this.groundTypeAt(x, y, dimensionId),
2151
{
2152
preferPaths: !!preferPaths,
2153
softOccupied: entityId != null && this.#isSoftOccupiedTile(x, y, dimensionId, entityId, now)
2154
}
2155
);
2156
options.minStepCost = preferPaths ? PATH_GROUND_STEP_COST : 1;
2157
return options;
2158
}
2159
2160
/**
2161
* True when another living occupant has stood on this tile for SOFT_OCCUPIED_DWELL_MS+.
2162
* Soft cost only — never blocks pathing.
2163
*/
2164
#isSoftOccupiedTile (tileX, tileY, dimensionId, selfEntityId, now = Date.now())
2165
{
2166
const key = tileKey(tileX, tileY, dimensionId);
2167
const local = this.occupancy.get(key);
2168
if (!local?.size)
2169
{ return false; }
2170
for (const id of local)
2139
2171
{
2140
options.stepCost = (x, y) => stepCostForGround(this.groundTypeAt(x, y), { preferPaths: true });
2141
options.minStepCost = PATH_GROUND_STEP_COST;
2172
if (id === selfEntityId)
2173
{ continue; }
2174
const handle = this.ecs.handleOf(id);
2175
if (handle == null)
2176
{ continue; }
2177
const position = this.ecs.get(handle, Component.position);
2178
const enteredAt = position?.tileEnteredAt;
2179
if (enteredAt == null)
2180
{ continue; }
2181
if (now - enteredAt >= SOFT_OCCUPIED_DWELL_MS)
2182
{ return true; }
2142
2183
}
2143
return options;
2184
return false;
2144
2185
}
2145
2186
2146
2187
/**
@@ -2216,7 +2257,7 @@ export class WorkerWorld
2216
2257
return normalizeDimensionId(position?.dimensionId ?? OVERWORLD_ID);
2217
2258
}
2218
2259
2219
#occupy (entityId, tileX, tileY)
2260
#occupy (entityId, tileX, tileY, now = Date.now())
2220
2261
{
2221
2262
const key = tileKey(tileX, tileY, this.#entityDimensionId(entityId));
2222
2263
let set = this.occupancy.get(key);
@@ -2226,6 +2267,10 @@ export class WorkerWorld
2226
2267
this.occupancy.set(key, set);
2227
2268
}
2228
2269
set.add(entityId);
2270
const handle = this.ecs.handleOf(entityId);
2271
const position = handle != null ? this.ecs.get(handle, Component.position) : null;
2272
if (position)
2273
{ position.tileEnteredAt = now; }
2229
2274
}
2230
2275
2231
2276
#vacate (entityId, tileX, tileY)
@@ -78,7 +78,11 @@ import {78
staminaCostPerRunStep,78
staminaCostPerRunStep,79
publicStaminaView79
publicStaminaView80
} from "../../../shared/stamina/rules.js";80
} from "../../../shared/stamina/rules.js";81
import { stepCostForGround, PATH_GROUND_STEP_COST } from "../../../shared/navigation/pathGround.js";81
import {82
stepCostForGround,83
PATH_GROUND_STEP_COST,84
SOFT_OCCUPIED_DWELL_MS85
} from "../../../shared/navigation/pathGround.js";82
import { GroundType, normalizeGround } from "../../../shared/definitions/tiles.js";86
import { GroundType, normalizeGround } from "../../../shared/definitions/tiles.js";83
import { clearFollow, setFollow, stepFollow, recordFollowTrail, consumeFollowTrail } from "./follow.js";87
import { clearFollow, setFollow, stepFollow, recordFollowTrail, consumeFollowTrail } from "./follow.js";84
import { ensureFlightComponent, stepFreeFlight } from "./freeFlight.js";88
import { ensureFlightComponent, stepFreeFlight } from "./freeFlight.js";@@ -876,7 +880,7 @@ export class WorkerWorld876
bounds.minY,880
bounds.minY,877
bounds.maxX,881
bounds.maxX,878
bounds.maxY,882
bounds.maxY,879
this.pathfindOptions({ preferVertical: true, preferPaths: true })883
this.pathfindOptions({ preferVertical: true, preferPaths: true, entityId })880
);884
);881
movement.walkCarry = 0;885
movement.walkCarry = 0;882
const events = [];886
const events = [];@@ -1484,7 +1488,7 @@ export class WorkerWorld1484
position.tileY = next.y;1488
position.tileY = next.y;1485
tiles.shift();1489
tiles.shift();1486
if (!flying)1490
if (!flying)1487
{ this.#occupy(entityId, position.tileX, position.tileY); }1491
{ this.#occupy(entityId, position.tileX, position.tileY, now); }1488
path.stuckSince = now;1492
path.stuckSince = now;1489
path.stuckTileX = position.tileX;1493
path.stuckTileX = position.tileX;1490
path.stuckTileY = position.tileY;1494
path.stuckTileY = position.tileY;@@ -1546,7 +1550,8 @@ export class WorkerWorld1546
bounds.maxY,1550
bounds.maxY,1547
this.pathfindOptions({1551
this.pathfindOptions({1548
preferVertical: stuckHandle != null && this.ecs.has(stuckHandle, Component.character),1552
preferVertical: stuckHandle != null && this.ecs.has(stuckHandle, Component.character),1549
preferPaths: !inCombat1553
preferPaths: !inCombat,1554
entityId1550
})1555
})1551
);1556
);1552
path.stuckSince = now;1557
path.stuckSince = now;@@ -1651,7 +1656,8 @@ export class WorkerWorld1651
bounds.minX,1656
bounds.minX,1652
bounds.minY,1657
bounds.minY,1653
bounds.maxX,1658
bounds.maxX,1654
bounds.maxY1659
bounds.maxY,1660
this.pathfindOptions({ preferPaths: true, entityId: entity.entityId })1655
);1661
);1656
if (!tiles?.length)1662
if (!tiles?.length)1657
{ continue; }1663
{ continue; }@@ -1688,7 +1694,8 @@ export class WorkerWorld1688
bounds.minX,1694
bounds.minX,1689
bounds.minY,1695
bounds.minY,1690
bounds.maxX,1696
bounds.maxX,1691
bounds.maxY1697
bounds.maxY,1698
this.pathfindOptions({ preferPaths: true, entityId })1692
);1699
);1693
if (!tiles?.length)1700
if (!tiles?.length)1694
{ return false; }1701
{ return false; }@@ -1753,7 +1760,8 @@ export class WorkerWorld1753
bounds.minX,1760
bounds.minX,1754
bounds.minY,1761
bounds.minY,1755
bounds.maxX,1762
bounds.maxX,1756
bounds.maxY1763
bounds.maxY,1764
this.pathfindOptions({ preferPaths: true, entityId: best.entityId })1757
);1765
);1758
best.spawnOrigin.stationTileX = stationTileX;1766
best.spawnOrigin.stationTileX = stationTileX;1759
best.spawnOrigin.stationTileY = stationTileY;1767
best.spawnOrigin.stationTileY = stationTileY;@@ -1920,7 +1928,8 @@ export class WorkerWorld1920
bounds.minX,1928
bounds.minX,1921
bounds.minY,1929
bounds.minY,1922
bounds.maxX,1930
bounds.maxX,1923
bounds.maxY1931
bounds.maxY,1932
this.pathfindOptions({ preferPaths: true, entityId: entity.entityId })1924
);1933
);1925
if (tiles?.length)1934
if (tiles?.length)1926
{ entity.path.tiles = tiles; }1935
{ entity.path.tiles = tiles; }@@ -2130,17 +2139,49 @@ export class WorkerWorld2130
}2139
}2131
2140
2132
/**2141
/**2133
* @param {{ preferVertical?: boolean, preferPaths?: boolean }} [options]2142
* @param {{ preferVertical?: boolean, preferPaths?: boolean, entityId?: number | null }} [options]2134
*/2143
*/2135
pathfindOptions ({ preferVertical = false, preferPaths = false } = {})2144
pathfindOptions ({ preferVertical = false, preferPaths = false, entityId = null } = {})2136
{2145
{2137
const options = { preferVertical: !!preferVertical };2146
const options = { preferVertical: !!preferVertical };2138
if (preferPaths)2147
const now = Date.now();2148
const dimensionId = entityId != null ? this.#entityDimensionId(entityId) : OVERWORLD_ID;2149
options.stepCost = (x, y) => stepCostForGround(2150
this.groundTypeAt(x, y, dimensionId),2151
{2152
preferPaths: !!preferPaths,2153
softOccupied: entityId != null && this.#isSoftOccupiedTile(x, y, dimensionId, entityId, now)2154
}2155
);2156
options.minStepCost = preferPaths ? PATH_GROUND_STEP_COST : 1;2157
return options;2158
}2159
2160
/**2161
* True when another living occupant has stood on this tile for SOFT_OCCUPIED_DWELL_MS+.2162
* Soft cost only — never blocks pathing.2163
*/2164
#isSoftOccupiedTile (tileX, tileY, dimensionId, selfEntityId, now = Date.now())2165
{2166
const key = tileKey(tileX, tileY, dimensionId);2167
const local = this.occupancy.get(key);2168
if (!local?.size)2169
{ return false; }2170
for (const id of local)2139
{2171
{2140
options.stepCost = (x, y) => stepCostForGround(this.groundTypeAt(x, y), { preferPaths: true });2172
if (id === selfEntityId)2141
options.minStepCost = PATH_GROUND_STEP_COST;2173
{ continue; }2174
const handle = this.ecs.handleOf(id);2175
if (handle == null)2176
{ continue; }2177
const position = this.ecs.get(handle, Component.position);2178
const enteredAt = position?.tileEnteredAt;2179
if (enteredAt == null)2180
{ continue; }2181
if (now - enteredAt >= SOFT_OCCUPIED_DWELL_MS)2182
{ return true; }2142
}2183
}2143
return options;2184
return false;2144
}2185
}2145
2186
2146
/**2187
/**@@ -2216,7 +2257,7 @@ export class WorkerWorld2216
return normalizeDimensionId(position?.dimensionId ?? OVERWORLD_ID);2257
return normalizeDimensionId(position?.dimensionId ?? OVERWORLD_ID);2217
}2258
}2218
2259
2219
#occupy (entityId, tileX, tileY)2260
#occupy (entityId, tileX, tileY, now = Date.now())2220
{2261
{2221
const key = tileKey(tileX, tileY, this.#entityDimensionId(entityId));2262
const key = tileKey(tileX, tileY, this.#entityDimensionId(entityId));2222
let set = this.occupancy.get(key);2263
let set = this.occupancy.get(key);@@ -2226,6 +2267,10 @@ export class WorkerWorld2226
this.occupancy.set(key, set);2267
this.occupancy.set(key, set);2227
}2268
}2228
set.add(entityId);2269
set.add(entityId);2270
const handle = this.ecs.handleOf(entityId);2271
const position = handle != null ? this.ecs.get(handle, Component.position) : null;2272
if (position)2273
{ position.tileEnteredAt = now; }2229
}2274
}2230
2275
2231
#vacate (entityId, tileX, tileY)2276
#vacate (entityId, tileX, tileY)@@ -1,7 +1,7 @@
1
1
import test from "node:test";
2
2
import assert from "node:assert/strict";
3
3
import { findPath, orderedCardinals } from "../src/navigation/astar.js";
4
import { PATH_GROUND_STEP_COST, stepCostForGround } from "../../shared/navigation/pathGround.js";
4
import { PATH_GROUND_STEP_COST, SOFT_OCCUPIED_STEP_PENALTY, stepCostForGround } from "../../shared/navigation/pathGround.js";
5
5
import { GroundType } from "../../shared/definitions/tiles.js";
6
6
7
7
test("A* walks around an impassable tile", () =>
@@ -137,3 +137,68 @@ test("A* with cheap paths takes a short detour onto the road", () =>
137
137
assert.ok(onRoad.length >= 10, `expected mostly road, got ${JSON.stringify(route)}`);
138
138
assert.ok(route.some((step) => step.x === 2), "should use the vertical road column");
139
139
});
140
141
test("soft-occupied penalty is weaker than path preference", () =>
142
{
143
assert.ok(SOFT_OCCUPIED_STEP_PENALTY > 0);
144
assert.ok(SOFT_OCCUPIED_STEP_PENALTY < 1 - PATH_GROUND_STEP_COST);
145
assert.equal(stepCostForGround(GroundType.grass, { softOccupied: true }), 1 + SOFT_OCCUPIED_STEP_PENALTY);
146
assert.equal(
147
stepCostForGround(GroundType.path, { preferPaths: true, softOccupied: true }),
148
PATH_GROUND_STEP_COST + SOFT_OCCUPIED_STEP_PENALTY
149
);
150
});
151
152
test("A* soft-occupied cost prefers an equal-length route that avoids the stood-on tile", () =>
153
{
154
// (0,0) → (1,1): via (1,0) soft-occupied, or via (0,1) clear.
155
const occupied = new Set(["1,0"]);
156
const path = findPath(
157
0,
158
0,
159
1,
160
1,
161
() => true,
162
-2,
163
-2,
164
4,
165
4,
166
{
167
preferVertical: false,
168
minStepCost: 1,
169
stepCost: (x, y) => stepCostForGround(
170
GroundType.grass,
171
{ softOccupied: occupied.has(`${x},${y}`) }
172
)
173
}
174
);
175
assert.ok(path.length > 0);
176
assert.ok(!path.some((step) => step.x === 1 && step.y === 0), JSON.stringify(path));
177
assert.ok(path.some((step) => step.x === 0 && step.y === 1), JSON.stringify(path));
178
});
179
180
test("A* still crosses soft-occupied when the detour is much longer", () =>
181
{
182
// Only corridor is through (1,0); far detour would be around a wall of blocked tiles.
183
const occupied = new Set(["1,0"]);
184
const blocked = new Set(["0,1", "1,1", "2,1", "0,-1", "1,-1", "2,-1"]);
185
const path = findPath(
186
0,
187
0,
188
2,
189
0,
190
(x, y) => !blocked.has(`${x},${y}`),
191
-2,
192
-2,
193
4,
194
4,
195
{
196
minStepCost: 1,
197
stepCost: (x, y) => stepCostForGround(
198
GroundType.grass,
199
{ softOccupied: occupied.has(`${x},${y}`) }
200
)
201
}
202
);
203
assert.ok(path.some((step) => step.x === 1 && step.y === 0), JSON.stringify(path));
204
});
@@ -1,7 +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 { findPath, orderedCardinals } from "../src/navigation/astar.js";3
import { findPath, orderedCardinals } from "../src/navigation/astar.js";4
import { PATH_GROUND_STEP_COST, stepCostForGround } from "../../shared/navigation/pathGround.js";4
import { PATH_GROUND_STEP_COST, SOFT_OCCUPIED_STEP_PENALTY, stepCostForGround } from "../../shared/navigation/pathGround.js";5
import { GroundType } from "../../shared/definitions/tiles.js";5
import { GroundType } from "../../shared/definitions/tiles.js";6
6
7
test("A* walks around an impassable tile", () =>7
test("A* walks around an impassable tile", () =>@@ -137,3 +137,68 @@ test("A* with cheap paths takes a short detour onto the road", () =>137
assert.ok(onRoad.length >= 10, `expected mostly road, got ${JSON.stringify(route)}`);137
assert.ok(onRoad.length >= 10, `expected mostly road, got ${JSON.stringify(route)}`);138
assert.ok(route.some((step) => step.x === 2), "should use the vertical road column");138
assert.ok(route.some((step) => step.x === 2), "should use the vertical road column");139
});139
});140
141
test("soft-occupied penalty is weaker than path preference", () =>142
{143
assert.ok(SOFT_OCCUPIED_STEP_PENALTY > 0);144
assert.ok(SOFT_OCCUPIED_STEP_PENALTY < 1 - PATH_GROUND_STEP_COST);145
assert.equal(stepCostForGround(GroundType.grass, { softOccupied: true }), 1 + SOFT_OCCUPIED_STEP_PENALTY);146
assert.equal(147
stepCostForGround(GroundType.path, { preferPaths: true, softOccupied: true }),148
PATH_GROUND_STEP_COST + SOFT_OCCUPIED_STEP_PENALTY149
);150
});151
152
test("A* soft-occupied cost prefers an equal-length route that avoids the stood-on tile", () =>153
{154
// (0,0) → (1,1): via (1,0) soft-occupied, or via (0,1) clear.155
const occupied = new Set(["1,0"]);156
const path = findPath(157
0,158
0,159
1,160
1,161
() => true,162
-2,163
-2,164
4,165
4,166
{167
preferVertical: false,168
minStepCost: 1,169
stepCost: (x, y) => stepCostForGround(170
GroundType.grass,171
{ softOccupied: occupied.has(`${x},${y}`) }172
)173
}174
);175
assert.ok(path.length > 0);176
assert.ok(!path.some((step) => step.x === 1 && step.y === 0), JSON.stringify(path));177
assert.ok(path.some((step) => step.x === 0 && step.y === 1), JSON.stringify(path));178
});179
180
test("A* still crosses soft-occupied when the detour is much longer", () =>181
{182
// Only corridor is through (1,0); far detour would be around a wall of blocked tiles.183
const occupied = new Set(["1,0"]);184
const blocked = new Set(["0,1", "1,1", "2,1", "0,-1", "1,-1", "2,-1"]);185
const path = findPath(186
0,187
0,188
2,189
0,190
(x, y) => !blocked.has(`${x},${y}`),191
-2,192
-2,193
4,194
4,195
{196
minStepCost: 1,197
stepCost: (x, y) => stepCostForGround(198
GroundType.grass,199
{ softOccupied: occupied.has(`${x},${y}`) }200
)201
}202
);203
assert.ok(path.some((step) => step.x === 1 && step.y === 0), JSON.stringify(path));204
});