Scale A* heuristic by minStepCost so cheap paths win.
Manhattan assumed cost 1 per tile, so path step costs below 1 made the heuristic inadmissible and A* returned short grass routes instead of roads. Prefer-paths pathfinding now passes PATH_GROUND_STEP_COST as the floor.
@@ -75,7 +75,7 @@ import {
75
75
staminaCostPerRunStep,
76
76
publicStaminaView
77
77
} from "../../../shared/stamina/rules.js";
78
import { stepCostForGround } from "../../../shared/navigation/pathGround.js";
78
import { stepCostForGround, PATH_GROUND_STEP_COST } from "../../../shared/navigation/pathGround.js";
79
79
import { GroundType, normalizeGround } from "../../../shared/definitions/tiles.js";
80
80
import { clearFollow, setFollow, stepFollow, recordFollowTrail, consumeFollowTrail } from "./follow.js";
81
81
import { ensureFlightComponent, stepFreeFlight } from "./freeFlight.js";
@@ -2110,6 +2110,7 @@ export class WorkerWorld
2110
2110
if (preferPaths)
2111
2111
{
2112
2112
options.stepCost = (x, y) => stepCostForGround(this.groundTypeAt(x, y), { preferPaths: true });
2113
options.minStepCost = PATH_GROUND_STEP_COST;
2113
2114
}
2114
2115
return options;
2115
2116
}
@@ -75,7 +75,7 @@ import {75
staminaCostPerRunStep,75
staminaCostPerRunStep,76
publicStaminaView76
publicStaminaView77
} from "../../../shared/stamina/rules.js";77
} from "../../../shared/stamina/rules.js";78
import { stepCostForGround } from "../../../shared/navigation/pathGround.js";78
import { stepCostForGround, PATH_GROUND_STEP_COST } from "../../../shared/navigation/pathGround.js";79
import { GroundType, normalizeGround } from "../../../shared/definitions/tiles.js";79
import { GroundType, normalizeGround } from "../../../shared/definitions/tiles.js";80
import { clearFollow, setFollow, stepFollow, recordFollowTrail, consumeFollowTrail } from "./follow.js";80
import { clearFollow, setFollow, stepFollow, recordFollowTrail, consumeFollowTrail } from "./follow.js";81
import { ensureFlightComponent, stepFreeFlight } from "./freeFlight.js";81
import { ensureFlightComponent, stepFreeFlight } from "./freeFlight.js";@@ -2110,6 +2110,7 @@ export class WorkerWorld2110
if (preferPaths)2110
if (preferPaths)2111
{2111
{2112
options.stepCost = (x, y) => stepCostForGround(this.groundTypeAt(x, y), { preferPaths: true });2112
options.stepCost = (x, y) => stepCostForGround(this.groundTypeAt(x, y), { preferPaths: true });2113
options.minStepCost = PATH_GROUND_STEP_COST;2113
}2114
}2114
return options;2115
return options;2115
}2116
}@@ -91,6 +91,7 @@ test("A* path-ground stepCost prefers a road of equal Manhattan length", () =>
91
91
4,
92
92
{
93
93
preferVertical: false,
94
minStepCost: PATH_GROUND_STEP_COST,
94
95
stepCost: (x, y) => stepCostForGround(
95
96
pathTiles.has(`${x},${y}`) ? GroundType.path : GroundType.grass,
96
97
{ preferPaths: true }
@@ -100,3 +101,39 @@ test("A* path-ground stepCost prefers a road of equal Manhattan length", () =>
100
101
assert.ok(route.some((step) => step.x === 1 && step.y === 0), JSON.stringify(route));
101
102
assert.ok(PATH_GROUND_STEP_COST < 1);
102
103
});
104
105
test("A* with cheap paths takes a short detour onto the road", () =>
106
{
107
// Player above middle of top bar; vertical road on the right; goal middle of bottom.
108
// Straight down is 12 grass steps; via road is +2 tiles but much cheaper at 0.1/step.
109
const pathTiles = new Set();
110
for (let x = 0; x <= 2; x++)
111
{ pathTiles.add(`${x},1`); }
112
for (let y = 2; y <= 11; y++)
113
{ pathTiles.add(`2,${y}`); }
114
for (let x = 0; x <= 2; x++)
115
{ pathTiles.add(`${x},12`); }
116
117
const route = findPath(
118
1,
119
0,
120
1,
121
12,
122
() => true,
123
-2,
124
-2,
125
20,
126
20,
127
{
128
preferVertical: true,
129
minStepCost: PATH_GROUND_STEP_COST,
130
stepCost: (x, y) => stepCostForGround(
131
pathTiles.has(`${x},${y}`) ? GroundType.path : GroundType.grass,
132
{ preferPaths: true }
133
)
134
}
135
);
136
const onRoad = route.filter((step) => pathTiles.has(`${step.x},${step.y}`));
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");
139
});
@@ -91,6 +91,7 @@ test("A* path-ground stepCost prefers a road of equal Manhattan length", () =>91
4,91
4,92
{92
{93
preferVertical: false,93
preferVertical: false,94
minStepCost: PATH_GROUND_STEP_COST,94
stepCost: (x, y) => stepCostForGround(95
stepCost: (x, y) => stepCostForGround(95
pathTiles.has(`${x},${y}`) ? GroundType.path : GroundType.grass,96
pathTiles.has(`${x},${y}`) ? GroundType.path : GroundType.grass,96
{ preferPaths: true }97
{ preferPaths: true }@@ -100,3 +101,39 @@ test("A* path-ground stepCost prefers a road of equal Manhattan length", () =>100
assert.ok(route.some((step) => step.x === 1 && step.y === 0), JSON.stringify(route));101
assert.ok(route.some((step) => step.x === 1 && step.y === 0), JSON.stringify(route));101
assert.ok(PATH_GROUND_STEP_COST < 1);102
assert.ok(PATH_GROUND_STEP_COST < 1);102
});103
});104
105
test("A* with cheap paths takes a short detour onto the road", () =>106
{107
// Player above middle of top bar; vertical road on the right; goal middle of bottom.108
// Straight down is 12 grass steps; via road is +2 tiles but much cheaper at 0.1/step.109
const pathTiles = new Set();110
for (let x = 0; x <= 2; x++)111
{ pathTiles.add(`${x},1`); }112
for (let y = 2; y <= 11; y++)113
{ pathTiles.add(`2,${y}`); }114
for (let x = 0; x <= 2; x++)115
{ pathTiles.add(`${x},12`); }116
117
const route = findPath(118
1,119
0,120
1,121
12,122
() => true,123
-2,124
-2,125
20,126
20,127
{128
preferVertical: true,129
minStepCost: PATH_GROUND_STEP_COST,130
stepCost: (x, y) => stepCostForGround(131
pathTiles.has(`${x},${y}`) ? GroundType.path : GroundType.grass,132
{ preferPaths: true }133
)134
}135
);136
const onRoad = route.filter((step) => pathTiles.has(`${step.x},${step.y}`));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");139
});