fix(02-02): cost 守卫允许 Infinity,仅拦 NaN 与负数

This commit is contained in:
unanmed 2026-09-09 19:30:12 +08:00
parent 3beef86ac0
commit e27015c4d2
3 changed files with 25 additions and 3 deletions

View File

@ -136,13 +136,14 @@ export class PathfindingFinder implements IPathfinder {
}
/**
* 1
* NaN 1
* Infinity
* @param block
*/
private getNodeCost(block: ILayerLocation): number {
if (!this.cost) return 1;
const value = this.cost(block);
if (!Number.isFinite(value) || value < 0) {
if (Number.isNaN(value) || value < 0) {
logger.warn(174);
return 1;
}

View File

@ -410,6 +410,27 @@ describe('pathfinding system', () => {
expect(result.ret).toHaveLength(2);
});
// 验证 Infinity 是合法损失值:不告警且路径绕开高损失格
it('allows Infinity as a legitimate cost without warning', () => {
const fixture = createSystem([1, 1, 1, 1, 1, 1, 1, 1, 1], 3);
injectPredicate(fixture);
const cost: PathCostFunction = block =>
block.locator.x === 1 && block.locator.y === 1
? Number.POSITIVE_INFINITY
: 1;
fixture.system.finder.useCostFunction(cost);
const result = modules.logger.catch(() =>
fixture.system.finder.find({ x: 0, y: 1 }, { x: 2, y: 1 })
);
expect(result.info.map(info => info.code)).not.toContain(174);
expect(result.ret).toHaveLength(4);
for (const step of result.ret) {
expect(step.to).not.toEqual({ x: 1, y: 1 });
}
});
// 验证不可达目标返回空数组且不移动对象
it('returns an empty path and never moves for unreachable targets', () => {
const fixture = createSystem([1, 6, 1, 1, 6, 1, 1, 6, 1], 3);

View File

@ -239,6 +239,6 @@
"171": "Event id '$1' not found in event store, event will be skipped.",
"172": "Event returned non-boolean value '$1' during reduction. JavaScript short-circuit semantics will be used.",
"173": "Pathfinding input is invalid or a required binding (map state, map layer) is missing. An empty result will be returned.",
"174": "Pathfinding cost function returned a negative value. Default cost 1 will be used instead."
"174": "Pathfinding cost function returned a negative or NaN value. Default cost 1 will be used instead."
}
}