Add path ground with soft blend, half run stamina, and mild A* bias.
Outdoor blendable path tiles cost half stamina while running and are slightly preferred by out-of-combat pathfinding so characters tend to stay on the road.
@@ -276,6 +276,7 @@ function queueCatchUp (world, entityId, handle, position, targetPos, path)
276
276
{ return; }
277
277
}
278
278
const bounds = chunkBoundsAround(position.tileX, position.tileY, VISIBLE_CHUNK_RADIUS);
279
const inCombat = !!world.ecs.get(handle, Component.combat)?.targetEntityId;
279
280
const nextTiles = findPath(
280
281
position.tileX,
281
282
position.tileY,
@@ -292,7 +293,7 @@ function queueCatchUp (world, entityId, handle, position, targetPos, path)
292
293
bounds.minY,
293
294
bounds.maxX,
294
295
bounds.maxY,
295
{ preferVertical: true }
296
world.pathfindOptions({ preferVertical: true, preferPaths: !inCombat })
296
297
);
297
298
const movement = world.ecs.get(handle, Component.movement);
298
299
if (movement)
@@ -276,6 +276,7 @@ function queueCatchUp (world, entityId, handle, position, targetPos, path)276
{ return; }276
{ return; }277
}277
}278
const bounds = chunkBoundsAround(position.tileX, position.tileY, VISIBLE_CHUNK_RADIUS);278
const bounds = chunkBoundsAround(position.tileX, position.tileY, VISIBLE_CHUNK_RADIUS);279
const inCombat = !!world.ecs.get(handle, Component.combat)?.targetEntityId;279
const nextTiles = findPath(280
const nextTiles = findPath(280
position.tileX,281
position.tileX,281
position.tileY,282
position.tileY,@@ -292,7 +293,7 @@ function queueCatchUp (world, entityId, handle, position, targetPos, path)292
bounds.minY,293
bounds.minY,293
bounds.maxX,294
bounds.maxX,294
bounds.maxY,295
bounds.maxY,295
{ preferVertical: true }296
world.pathfindOptions({ preferVertical: true, preferPaths: !inCombat })296
);297
);297
const movement = world.ecs.get(handle, Component.movement);298
const movement = world.ecs.get(handle, Component.movement);298
if (movement)299
if (movement)@@ -8,7 +8,8 @@ import {
8
8
tileKey,
9
9
tileToChunk,
10
10
tileToLocal,
11
tileToRegion
11
tileToRegion,
12
tileIndex
12
13
} from "../world/coordinates.js";
13
14
import { cloneChunkData, createEmptyChunkData, isChunkTilePassable, isChunkTileClearForRangedAttacks } from "../world/chunkData.js";
14
15
import { closedNeighborEdges } from "../world/chunkFlags.js";
@@ -74,6 +75,8 @@ import {
74
75
staminaCostPerRunStep,
75
76
publicStaminaView
76
77
} from "../../../shared/stamina/rules.js";
78
import { stepCostForGround } from "../../../shared/navigation/pathGround.js";
79
import { GroundType, normalizeGround } from "../../../shared/definitions/tiles.js";
77
80
import { clearFollow, setFollow, stepFollow, recordFollowTrail, consumeFollowTrail } from "./follow.js";
78
81
import { ensureFlightComponent, stepFreeFlight } from "./freeFlight.js";
79
82
import { stepStatusEffects } from "../statusEffects/service.js";
@@ -865,7 +868,7 @@ export class WorkerWorld
865
868
bounds.minY,
866
869
bounds.maxX,
867
870
bounds.maxY,
868
{ preferVertical: true }
871
this.pathfindOptions({ preferVertical: true, preferPaths: true })
869
872
);
870
873
movement.walkCarry = 0;
871
874
}
@@ -1172,7 +1175,11 @@ export class WorkerWorld
1172
1175
}
1173
1176
const skills = this.ecs.get(handle, Component.skills);
1174
1177
syncPlayerStamina(this.ecs, handle);
1175
const cost = staminaCostPerRunStep(this.settings?.walkTilesPerSecond);
1178
const position = this.ecs.get(handle, Component.position);
1179
const onPath = position
1180
? this.groundTypeAt(position.tileX, position.tileY, position.dimensionId) === GroundType.path
1181
: false;
1182
const cost = staminaCostPerRunStep(this.settings?.walkTilesPerSecond, { onPath });
1176
1183
stamina.current = Math.max(0, (Number(stamina.current) || 0) - cost);
1177
1184
stamina.xpDebt = (Number(stamina.xpDebt) || 0) + cost;
1178
1185
let skillsChanged = false;
@@ -1499,6 +1506,8 @@ export class WorkerWorld
1499
1506
}
1500
1507
const bounds = chunkBoundsAround(position.tileX, position.tileY, VISIBLE_CHUNK_RADIUS);
1501
1508
const stuckHandle = this.ecs.handleOf(entityId);
1509
const inCombat = stuckHandle != null
1510
&& this.ecs.get(stuckHandle, Component.combat)?.targetEntityId != null;
1502
1511
path.tiles = findPath(
1503
1512
position.tileX,
1504
1513
position.tileY,
@@ -1509,7 +1518,10 @@ export class WorkerWorld
1509
1518
bounds.minY,
1510
1519
bounds.maxX,
1511
1520
bounds.maxY,
1512
{ preferVertical: stuckHandle != null && this.ecs.has(stuckHandle, Component.character) }
1521
this.pathfindOptions({
1522
preferVertical: stuckHandle != null && this.ecs.has(stuckHandle, Component.character),
1523
preferPaths: !inCombat
1524
})
1513
1525
);
1514
1526
path.stuckSince = now;
1515
1527
path.stuckTileX = position.tileX;
@@ -2080,6 +2092,28 @@ export class WorkerWorld
2080
2092
return this.isEnterable(tileX, tileY, dim);
2081
2093
}
2082
2094
2095
/** Ground type id at a world tile (defaults to world default ground). */
2096
groundTypeAt (tileX, tileY, dimensionId = OVERWORLD_ID)
2097
{
2098
const dim = normalizeDimensionId(dimensionId) ?? OVERWORLD_ID;
2099
const data = this.#chunkData(tileToChunk(tileX), tileToChunk(tileY), dim);
2100
const entry = normalizeGround(data.grounds[tileIndex(tileToLocal(tileX), tileToLocal(tileY))]);
2101
return entry?.typeId ?? this.settings.defaultGroundTypeId ?? GroundType.grass;
2102
}
2103
2104
/**
2105
* @param {{ preferVertical?: boolean, preferPaths?: boolean }} [options]
2106
*/
2107
pathfindOptions ({ preferVertical = false, preferPaths = false } = {})
2108
{
2109
const options = { preferVertical: !!preferVertical };
2110
if (preferPaths)
2111
{
2112
options.stepCost = (x, y) => stepCostForGround(this.groundTypeAt(x, y), { preferPaths: true });
2113
}
2114
return options;
2115
}
2116
2083
2117
/**
2084
2118
* Standing tile with no other mob/player (self allowed). Used for wander goals
2085
2119
* and melee attack stance.
@@ -8,7 +8,8 @@ import {8
tileKey,8
tileKey,9
tileToChunk,9
tileToChunk,10
tileToLocal,10
tileToLocal,11
tileToRegion11
tileToRegion,12
tileIndex12
} from "../world/coordinates.js";13
} from "../world/coordinates.js";13
import { cloneChunkData, createEmptyChunkData, isChunkTilePassable, isChunkTileClearForRangedAttacks } from "../world/chunkData.js";14
import { cloneChunkData, createEmptyChunkData, isChunkTilePassable, isChunkTileClearForRangedAttacks } from "../world/chunkData.js";14
import { closedNeighborEdges } from "../world/chunkFlags.js";15
import { closedNeighborEdges } from "../world/chunkFlags.js";@@ -74,6 +75,8 @@ import {74
staminaCostPerRunStep,75
staminaCostPerRunStep,75
publicStaminaView76
publicStaminaView76
} from "../../../shared/stamina/rules.js";77
} from "../../../shared/stamina/rules.js";78
import { stepCostForGround } from "../../../shared/navigation/pathGround.js";79
import { GroundType, normalizeGround } from "../../../shared/definitions/tiles.js";77
import { clearFollow, setFollow, stepFollow, recordFollowTrail, consumeFollowTrail } from "./follow.js";80
import { clearFollow, setFollow, stepFollow, recordFollowTrail, consumeFollowTrail } from "./follow.js";78
import { ensureFlightComponent, stepFreeFlight } from "./freeFlight.js";81
import { ensureFlightComponent, stepFreeFlight } from "./freeFlight.js";79
import { stepStatusEffects } from "../statusEffects/service.js";82
import { stepStatusEffects } from "../statusEffects/service.js";@@ -865,7 +868,7 @@ export class WorkerWorld865
bounds.minY,868
bounds.minY,866
bounds.maxX,869
bounds.maxX,867
bounds.maxY,870
bounds.maxY,868
{ preferVertical: true }871
this.pathfindOptions({ preferVertical: true, preferPaths: true })869
);872
);870
movement.walkCarry = 0;873
movement.walkCarry = 0;871
}874
}@@ -1172,7 +1175,11 @@ export class WorkerWorld1172
}1175
}1173
const skills = this.ecs.get(handle, Component.skills);1176
const skills = this.ecs.get(handle, Component.skills);1174
syncPlayerStamina(this.ecs, handle);1177
syncPlayerStamina(this.ecs, handle);1175
const cost = staminaCostPerRunStep(this.settings?.walkTilesPerSecond);1178
const position = this.ecs.get(handle, Component.position);1179
const onPath = position1180
? this.groundTypeAt(position.tileX, position.tileY, position.dimensionId) === GroundType.path1181
: false;1182
const cost = staminaCostPerRunStep(this.settings?.walkTilesPerSecond, { onPath });1176
stamina.current = Math.max(0, (Number(stamina.current) || 0) - cost);1183
stamina.current = Math.max(0, (Number(stamina.current) || 0) - cost);1177
stamina.xpDebt = (Number(stamina.xpDebt) || 0) + cost;1184
stamina.xpDebt = (Number(stamina.xpDebt) || 0) + cost;1178
let skillsChanged = false;1185
let skillsChanged = false;@@ -1499,6 +1506,8 @@ export class WorkerWorld1499
}1506
}1500
const bounds = chunkBoundsAround(position.tileX, position.tileY, VISIBLE_CHUNK_RADIUS);1507
const bounds = chunkBoundsAround(position.tileX, position.tileY, VISIBLE_CHUNK_RADIUS);1501
const stuckHandle = this.ecs.handleOf(entityId);1508
const stuckHandle = this.ecs.handleOf(entityId);1509
const inCombat = stuckHandle != null1510
&& this.ecs.get(stuckHandle, Component.combat)?.targetEntityId != null;1502
path.tiles = findPath(1511
path.tiles = findPath(1503
position.tileX,1512
position.tileX,1504
position.tileY,1513
position.tileY,@@ -1509,7 +1518,10 @@ export class WorkerWorld1509
bounds.minY,1518
bounds.minY,1510
bounds.maxX,1519
bounds.maxX,1511
bounds.maxY,1520
bounds.maxY,1512
{ preferVertical: stuckHandle != null && this.ecs.has(stuckHandle, Component.character) }1521
this.pathfindOptions({1522
preferVertical: stuckHandle != null && this.ecs.has(stuckHandle, Component.character),1523
preferPaths: !inCombat1524
})1513
);1525
);1514
path.stuckSince = now;1526
path.stuckSince = now;1515
path.stuckTileX = position.tileX;1527
path.stuckTileX = position.tileX;@@ -2080,6 +2092,28 @@ export class WorkerWorld2080
return this.isEnterable(tileX, tileY, dim);2092
return this.isEnterable(tileX, tileY, dim);2081
}2093
}2082
2094
2095
/** Ground type id at a world tile (defaults to world default ground). */2096
groundTypeAt (tileX, tileY, dimensionId = OVERWORLD_ID)2097
{2098
const dim = normalizeDimensionId(dimensionId) ?? OVERWORLD_ID;2099
const data = this.#chunkData(tileToChunk(tileX), tileToChunk(tileY), dim);2100
const entry = normalizeGround(data.grounds[tileIndex(tileToLocal(tileX), tileToLocal(tileY))]);2101
return entry?.typeId ?? this.settings.defaultGroundTypeId ?? GroundType.grass;2102
}2103
2104
/**2105
* @param {{ preferVertical?: boolean, preferPaths?: boolean }} [options]2106
*/2107
pathfindOptions ({ preferVertical = false, preferPaths = false } = {})2108
{2109
const options = { preferVertical: !!preferVertical };2110
if (preferPaths)2111
{2112
options.stepCost = (x, y) => stepCostForGround(this.groundTypeAt(x, y), { preferPaths: true });2113
}2114
return options;2115
}2116
2083
/**2117
/**2084
* Standing tile with no other mob/player (self allowed). Used for wander goals2118
* Standing tile with no other mob/player (self allowed). Used for wander goals2085
* and melee attack stance.2119
* and melee attack stance.@@ -1,6 +1,8 @@
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";
5
import { GroundType } from "../../shared/definitions/tiles.js";
4
6
5
7
test("A* walks around an impassable tile", () =>
6
8
{
@@ -72,3 +74,29 @@ test("A* stepCost prefers the cheaper route around an expensive corridor", () =>
72
74
assert.deepEqual(path.at(-1), { x: 2, y: 0 });
73
75
assert.ok(!path.some((step) => step.x === 1 && step.y === 0));
74
76
});
77
78
test("A* path-ground stepCost prefers a road of equal Manhattan length", () =>
79
{
80
// (0,0) → (1,1): via (1,0) path then (1,1), or via (0,1) grass then (1,1).
81
const pathTiles = new Set(["1,0"]);
82
const route = findPath(
83
0,
84
0,
85
1,
86
1,
87
() => true,
88
-2,
89
-2,
90
4,
91
4,
92
{
93
preferVertical: false,
94
stepCost: (x, y) => stepCostForGround(
95
pathTiles.has(`${x},${y}`) ? GroundType.path : GroundType.grass,
96
{ preferPaths: true }
97
)
98
}
99
);
100
assert.ok(route.some((step) => step.x === 1 && step.y === 0), JSON.stringify(route));
101
assert.ok(PATH_GROUND_STEP_COST < 1);
102
});
@@ -1,6 +1,8 @@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";5
import { GroundType } from "../../shared/definitions/tiles.js";4
6
5
test("A* walks around an impassable tile", () =>7
test("A* walks around an impassable tile", () =>6
{8
{@@ -72,3 +74,29 @@ test("A* stepCost prefers the cheaper route around an expensive corridor", () =>72
assert.deepEqual(path.at(-1), { x: 2, y: 0 });74
assert.deepEqual(path.at(-1), { x: 2, y: 0 });73
assert.ok(!path.some((step) => step.x === 1 && step.y === 0));75
assert.ok(!path.some((step) => step.x === 1 && step.y === 0));74
});76
});77
78
test("A* path-ground stepCost prefers a road of equal Manhattan length", () =>79
{80
// (0,0) → (1,1): via (1,0) path then (1,1), or via (0,1) grass then (1,1).81
const pathTiles = new Set(["1,0"]);82
const route = findPath(83
0,84
0,85
1,86
1,87
() => true,88
-2,89
-2,90
4,91
4,92
{93
preferVertical: false,94
stepCost: (x, y) => stepCostForGround(95
pathTiles.has(`${x},${y}`) ? GroundType.path : GroundType.grass,96
{ preferPaths: true }97
)98
}99
);100
assert.ok(route.some((step) => step.x === 1 && step.y === 0), JSON.stringify(route));101
assert.ok(PATH_GROUND_STEP_COST < 1);102
});@@ -14,11 +14,14 @@ import {
14
14
import { GroundType, GROUND_DEFINITIONS } from "../../shared/definitions/tiles.js";
15
15
import { normalizeGraphicsSettings, SoftTerrainMode, DEFAULT_GRAPHICS_SETTINGS } from "../../shared/graphics/settings.js";
16
16
17
test("grass and dirt are blendable outdoor grounds", () =>
17
test("grass, dirt, and path are blendable outdoor grounds", () =>
18
18
{
19
19
assert.equal(GROUND_DEFINITIONS[GroundType.grass].blendable, true);
20
20
assert.equal(GROUND_DEFINITIONS[GroundType.dirt].blendable, true);
21
21
assert.equal(GROUND_DEFINITIONS[GroundType.dirt].outdoor, true);
22
assert.equal(GROUND_DEFINITIONS[GroundType.path].blendable, true);
23
assert.equal(GROUND_DEFINITIONS[GroundType.path].outdoor, true);
24
assert.equal(GROUND_DEFINITIONS[GroundType.path].pathSurface, true);
22
25
assert.equal(GROUND_DEFINITIONS[GroundType.wood].blendable, undefined);
23
26
assert.equal(GROUND_DEFINITIONS[GroundType.water].blendable, undefined);
24
27
});
@@ -14,11 +14,14 @@ import {14
import { GroundType, GROUND_DEFINITIONS } from "../../shared/definitions/tiles.js";14
import { GroundType, GROUND_DEFINITIONS } from "../../shared/definitions/tiles.js";15
import { normalizeGraphicsSettings, SoftTerrainMode, DEFAULT_GRAPHICS_SETTINGS } from "../../shared/graphics/settings.js";15
import { normalizeGraphicsSettings, SoftTerrainMode, DEFAULT_GRAPHICS_SETTINGS } from "../../shared/graphics/settings.js";16
16
17
test("grass and dirt are blendable outdoor grounds", () =>17
test("grass, dirt, and path are blendable outdoor grounds", () =>18
{18
{19
assert.equal(GROUND_DEFINITIONS[GroundType.grass].blendable, true);19
assert.equal(GROUND_DEFINITIONS[GroundType.grass].blendable, true);20
assert.equal(GROUND_DEFINITIONS[GroundType.dirt].blendable, true);20
assert.equal(GROUND_DEFINITIONS[GroundType.dirt].blendable, true);21
assert.equal(GROUND_DEFINITIONS[GroundType.dirt].outdoor, true);21
assert.equal(GROUND_DEFINITIONS[GroundType.dirt].outdoor, true);22
assert.equal(GROUND_DEFINITIONS[GroundType.path].blendable, true);23
assert.equal(GROUND_DEFINITIONS[GroundType.path].outdoor, true);24
assert.equal(GROUND_DEFINITIONS[GroundType.path].pathSurface, true);22
assert.equal(GROUND_DEFINITIONS[GroundType.wood].blendable, undefined);25
assert.equal(GROUND_DEFINITIONS[GroundType.wood].blendable, undefined);23
assert.equal(GROUND_DEFINITIONS[GroundType.water].blendable, undefined);26
assert.equal(GROUND_DEFINITIONS[GroundType.water].blendable, undefined);24
});27
});@@ -5,7 +5,8 @@ import { SkillId, skillLevel } from "../../shared/definitions/skills.js";
5
5
import { totalXpForLevel } from "../../shared/skills/progression.js";
6
6
import {
7
7
staminaCostPerRunStep,
8
STAMINA_REGEN_PER_SECOND
8
STAMINA_REGEN_PER_SECOND,
9
PATH_RUN_STAMINA_FACTOR
9
10
} from "../../shared/stamina/rules.js";
10
11
import { makeCharacter, makeSimulation, stepTimes } from "./helpers/simulationHarness.js";
11
12
@@ -37,6 +38,12 @@ test("staminaCostPerRunStep drains one point per second of continuous running",
37
38
assert.equal(staminaCostPerRunStep(1), 0.5);
38
39
});
39
40
41
test("path tiles cost half stamina while running", () =>
42
{
43
assert.equal(staminaCostPerRunStep(2, { onPath: true }), 0.25 * PATH_RUN_STAMINA_FACTOR);
44
assert.equal(PATH_RUN_STAMINA_FACTOR, 0.5);
45
});
46
40
47
test("set-gait to run is rejected when stamina is empty", () =>
41
48
{
42
49
const harness = makeSimulation({ workerCount: 1 });
@@ -5,7 +5,8 @@ import { SkillId, skillLevel } from "../../shared/definitions/skills.js";5
import { totalXpForLevel } from "../../shared/skills/progression.js";5
import { totalXpForLevel } from "../../shared/skills/progression.js";6
import {6
import {7
staminaCostPerRunStep,7
staminaCostPerRunStep,8
STAMINA_REGEN_PER_SECOND8
STAMINA_REGEN_PER_SECOND,9
PATH_RUN_STAMINA_FACTOR9
} from "../../shared/stamina/rules.js";10
} from "../../shared/stamina/rules.js";10
import { makeCharacter, makeSimulation, stepTimes } from "./helpers/simulationHarness.js";11
import { makeCharacter, makeSimulation, stepTimes } from "./helpers/simulationHarness.js";11
12
@@ -37,6 +38,12 @@ test("staminaCostPerRunStep drains one point per second of continuous running",37
assert.equal(staminaCostPerRunStep(1), 0.5);38
assert.equal(staminaCostPerRunStep(1), 0.5);38
});39
});39
40
41
test("path tiles cost half stamina while running", () =>42
{43
assert.equal(staminaCostPerRunStep(2, { onPath: true }), 0.25 * PATH_RUN_STAMINA_FACTOR);44
assert.equal(PATH_RUN_STAMINA_FACTOR, 0.5);45
});46
40
test("set-gait to run is rejected when stamina is empty", () =>47
test("set-gait to run is rejected when stamina is empty", () =>41
{48
{42
const harness = makeSimulation({ workerCount: 1 });49
const harness = makeSimulation({ workerCount: 1 });