Files
OpenFrontIO/tests/core/pathfinding/MultiSourceAnyTargetBFS.test.ts
scamiv 2776294220 Enhance game map handling with microGameMap integration
- Added `microGameMap` to support a new resolution level for compact games, allowing for more efficient map loading and pathfinding.
- Updated `createGame` and `GameImpl` to incorporate `microGameMap`, ensuring proper handling of different map resolutions.
- Modified `loadTerrainMap` to always expose the 16x map for coarse heuristics, improving navigation capabilities.
2025-12-27 16:35:10 +01:00

59 lines
1.7 KiB
TypeScript

import { MultiSourceAnyTargetBFS } from "../../../src/core/pathfinding/MultiSourceAnyTargetBFS";
type TileRef = number;
function makeGridWaterMap(w: number, h: number, water: boolean[]) {
const num = w * h;
if (water.length !== num) throw new Error("bad water array");
return {
width: () => w,
height: () => h,
x: (ref: TileRef) => ref % w,
y: (ref: TileRef) => Math.floor(ref / w),
isWater: (ref: TileRef) => water[ref] === true,
neighbors: (ref: TileRef) => {
const out: TileRef[] = [];
const x = ref % w;
if (ref >= w) out.push(ref - w);
if (ref < (h - 1) * w) out.push(ref + w);
if (x !== 0) out.push(ref - 1);
if (x !== w - 1) out.push(ref + 1);
return out;
},
} as any;
}
describe("MultiSourceAnyTargetBFS", () => {
it("returns king-move (Chebyshev) diagonal routes when enabled", () => {
// 3x3, all water.
const gm = makeGridWaterMap(3, 3, new Array(9).fill(true));
const bfs = new MultiSourceAnyTargetBFS(9);
const res = bfs.findWaterPath(gm, [0], [8], { kingMoves: true });
expect(res).not.toBeNull();
expect(res!.path).toEqual([0, 4, 8]);
});
it("prevents diagonal corner cutting when enabled", () => {
// 2x2:
// S (water) X (land)
// X (land) T (water)
const gm = makeGridWaterMap(2, 2, [true, false, false, true]);
const bfs = new MultiSourceAnyTargetBFS(4);
const blocked = bfs.findWaterPath(gm, [0], [3], {
kingMoves: true,
noCornerCutting: true,
});
expect(blocked).toBeNull();
const allowed = bfs.findWaterPath(gm, [0], [3], {
kingMoves: true,
noCornerCutting: false,
});
expect(allowed).not.toBeNull();
expect(allowed!.path).toEqual([0, 3]);
});
});