mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-21 11:00:16 +00:00
b090f2f624
## Pathfinding with HPA*
Hi! The primary objective of this PR is to replace per-tile A* with
hierarchical pathfinding - HPA*. In practice, this means we create an
abstract graph on top of the actual map with far fewer points and use it
to decide on general path structure. Only then we go back to tile-level
and build path between selected waypoints. This speeds up long distance
pathfinding by over 1000x in some cases. To make the review easier, it
comes with a benchmark and visual playground.
## PREPROCESSING
H part of HPA* means "hierarchical" and requires preprocessing.
This PR includes pre-processing as part inside `new Game()` constructor.
It takes about 135ms for `giantworldmap` on my machine, which increases
the effective initialization from ~95ms to ~230ms. This time could be
reduced in different ways, which are **out of scope** for this PR.
After confirming the initialization time is bearable on low-end devices,
I argue merging this PR as-is is acceptable tradeoff. It creates small
lag at the beginning of a round but pays for itself in the first minute
of the match.
## Nerdy details
**Architecture**
- HPA*-style hierarchical pathfinding
- 32×32 sectors on minimap with gateway nodes on borders
- Gateway graph built via BFS during preprocessing
- Water component optimization skips unreachable gateway pairs
- A* on gateway graph → local A* within sectors → Bresenham path
smoothing
- Minimap upscaling identical to currently used in MiniAStar
**Key Optimizations**
- Typed arrays instead of high-level primitives
- Stamp-based visited tracking (no need to recreate buffers, O(1)
clearing)
- Optional - enabled by default - caching of tile paths between gateways
- Line of sight smoothing for the final path
## Review Focus
Play with included tools, benchmark and visualization. Pathfinding
should be safe to merge as a black box - you do not need to understand
the details. Outcomes can be tested empirically in-game. Visualize (and
share!) edge cases with included playground. Confirm the 100x speedup is
real with benchmark.
If you plan to dive into the code, I suggest the following order:
- Pathfinding abstraction in `src/core/pathfinding/`
- Pathfinding tests in `tests/core/pathfinding/`
- NavMesh in `src/core/pathfinding/navmesh/` + integration with
`Game.ts`
- Benchmark in `tests/pathfinding/benchmark/`
Do not look at playground's code, it has been created with a clanker.
The design is 100% mine and I spent way too long polishing it, but I
haven't even once edited the code manually. There is probably no
abstraction whatsoever, just do not look at the code, let it play.
## Core Changes
#### Pathfinding (`src/core/pathfinding/navmesh/`)
- HPA* + refinement -> three phased pathfinding: A* over the graph ->
naive path -> refinement
- comes with A* and BFS optimized for for specific needs
#### Pre-Processing (`src/core/pathfinding/navmesh/`)
- identify water bodies to avoid pathfinding between disconnected nodes
- create high-level graph of gateways on top of tile map
#### Abstraction (`src/core/pathfinding/`)
- common `PathFinder` interface that can return full path and also act
as state machine (`.next()`)
- adapters for both new and legacy algorithm with fallback to legacy if
navigation mesh not available
#### Benchmark (`tests/pathfinding/benchmark/`)
- `npx tsx tests/pathfinding/benchmark/run.ts` - no guesswork, numbers
- `npx tsx tests/pathfinding/benchmark/run.ts --synthetic` - 1000s of
synthetic paths
- `npx tsc tests/pathfinding/benchmark/generate.ts` - generate more as
needed, test new maps
- includes ONE synthetic scenario to avoid PR bloat, generate more
locally / later
#### Playground (`tests/pathfinding/playground/`)
- `npx tsx tests/pathfinding/playground/server.ts` - visualize paths
with both new and legacy algorithm
## Benchmarks
### Compared with legacy in default - hand picked - scenario:
```
Initialization: 95.95ms -> 227.29ms
Pathfinding: 3038.43ms -> 6.45ms
Distance: 26972 -> 26810 tiles
```
### 42,000 synthetic routes across all maps
```
Running 42 synthetic scenarios with hpa.cached adapter...
✅ synthetic/achiran | Init: 93.42ms | Path: 139.07ms | Dist: 1481630 tiles | Routes: 1000/1000
✅ synthetic/africa | Init: 87.14ms | Path: 155.08ms | Dist: 1829414 tiles | Routes: 1000/1000
✅ synthetic/asia | Init: 57.60ms | Path: 112.55ms | Dist: 1204082 tiles | Routes: 1000/1000
✅ synthetic/australia | Init: 78.18ms | Path: 77.12ms | Dist: 978375 tiles | Routes: 1000/1000
✅ synthetic/baikal | Init: 78.26ms | Path: 152.14ms | Dist: 1600016 tiles | Routes: 1000/1000
✅ synthetic/baikalnukewars | Init: 81.44ms | Path: 165.90ms | Dist: 1699283 tiles | Routes: 1000/1000
✅ synthetic/betweentwoseas | Init: 29.29ms | Path: 114.99ms | Dist: 1338075 tiles | Routes: 1000/1000
✅ synthetic/blacksea | Init: 30.66ms | Path: 93.14ms | Dist: 949217 tiles | Routes: 1000/1000
✅ synthetic/britannia | Init: 74.12ms | Path: 85.62ms | Dist: 866752 tiles | Routes: 1000/1000
✅ synthetic/deglaciatedantarctica | Init: 105.49ms | Path: 192.93ms | Dist: 1574684 tiles | Routes: 1000/1000
✅ synthetic/didier | Init: 81.51ms | Path: 153.70ms | Dist: 1734876 tiles | Routes: 1000/1000
✅ synthetic/eastasia | Init: 49.29ms | Path: 128.63ms | Dist: 1410270 tiles | Routes: 1000/1000
✅ synthetic/europe | Init: 92.55ms | Path: 178.35ms | Dist: 1525216 tiles | Routes: 1000/1000
✅ synthetic/europeclassic | Init: 33.50ms | Path: 104.40ms | Dist: 1209759 tiles | Routes: 1000/1000
✅ synthetic/falklandislands | Init: 63.00ms | Path: 107.41ms | Dist: 1080251 tiles | Routes: 1000/1000
✅ synthetic/faroeislands | Init: 71.91ms | Path: 49.52ms | Dist: 604613 tiles | Routes: 1000/1000
✅ synthetic/fourislands | Init: 45.75ms | Path: 78.91ms | Dist: 937439 tiles | Routes: 1000/1000
✅ synthetic/gatewaytotheatlantic | Init: 81.00ms | Path: 257.06ms | Dist: 2555551 tiles | Routes: 1000/1000
✅ synthetic/giantworldmap | Init: 214.25ms | Path: 220.42ms | Dist: 1976693 tiles | Routes: 1000/1000
✅ synthetic/gulfofstlawrence | Init: 45.16ms | Path: 96.05ms | Dist: 1014604 tiles | Routes: 1000/1000
✅ synthetic/halkidiki | Init: 74.68ms | Path: 149.39ms | Dist: 1546781 tiles | Routes: 1000/1000
✅ synthetic/iceland | Init: 58.72ms | Path: 78.16ms | Dist: 1001554 tiles | Routes: 1000/1000
✅ synthetic/italia | Init: 29.78ms | Path: 139.93ms | Dist: 1412024 tiles | Routes: 1000/1000
✅ synthetic/japan | Init: 161.07ms | Path: 118.65ms | Dist: 1154393 tiles | Routes: 1000/1000
✅ synthetic/lemnos | Init: 52.59ms | Path: 136.69ms | Dist: 1481101 tiles | Routes: 1000/1000
✅ synthetic/lisbon | Init: 49.27ms | Path: 86.53ms | Dist: 1032011 tiles | Routes: 1000/1000
✅ synthetic/manicouagan | Init: 53.74ms | Path: 110.52ms | Dist: 1307630 tiles | Routes: 1000/1000
✅ synthetic/mars | Init: 29.39ms | Path: 80.55ms | Dist: 1091702 tiles | Routes: 1000/1000
✅ synthetic/mena | Init: 26.37ms | Path: 120.09ms | Dist: 1272751 tiles | Routes: 1000/1000
✅ synthetic/montreal | Init: 26.08ms | Path: 106.77ms | Dist: 1187736 tiles | Routes: 1000/1000
✅ synthetic/newyorkcity | Init: 56.60ms | Path: 181.19ms | Dist: 1753875 tiles | Routes: 1000/1000
✅ synthetic/northamerica | Init: 96.29ms | Path: 123.02ms | Dist: 1217221 tiles | Routes: 1000/1000
✅ synthetic/oceania | Init: 52.81ms | Path: 51.96ms | Dist: 482373 tiles | Routes: 1000/1000
✅ synthetic/pangaea | Init: 21.29ms | Path: 56.58ms | Dist: 716189 tiles | Routes: 1000/1000
✅ synthetic/pluto | Init: 53.89ms | Path: 141.62ms | Dist: 1304362 tiles | Routes: 1000/1000
✅ synthetic/southamerica | Init: 85.19ms | Path: 123.03ms | Dist: 1301403 tiles | Routes: 1000/1000
✅ synthetic/straitofgibraltar | Init: 76.68ms | Path: 108.30ms | Dist: 1304592 tiles | Routes: 1000/1000
✅ synthetic/straitofhormuz | Init: 38.97ms | Path: 67.78ms | Dist: 754920 tiles | Routes: 1000/1000
✅ synthetic/surrounded | Init: 95.35ms | Path: 90.18ms | Dist: 1017142 tiles | Routes: 1000/1000
✅ synthetic/svalmel | Init: 60.58ms | Path: 104.75ms | Dist: 1235501 tiles | Routes: 1000/1000
✅ synthetic/twolakes | Init: 62.05ms | Path: 94.54ms | Dist: 1140807 tiles | Routes: 1000/1000
✅ synthetic/world | Init: 41.43ms | Path: 93.42ms | Dist: 873406 tiles | Routes: 1000/1000
Completed 42 scenarios
Total Initialization Time: 2796.32ms
Total Pathfinding Time: 5026.64ms
Total Distance: 53160274 tiles
```
## Playground
**That's the fun part**. Watch NavMesh running circles around legacy
`PathFinder.Mini` in real time. Debug inner workings, test edge cases,
share URLs for debugging.
https://github.com/user-attachments/assets/34e2e3f5-fbc1-4b1f-917d-820766e98d5d
## Discord Tag
`moleole`
334 lines
10 KiB
TypeScript
334 lines
10 KiB
TypeScript
import { beforeAll, describe, expect, test, vi } from "vitest";
|
|
import { Game } from "../../../src/core/game/Game";
|
|
import { TileRef } from "../../../src/core/game/GameMap";
|
|
import { MiniAStarAdapter } from "../../../src/core/pathfinding/adapters/MiniAStarAdapter";
|
|
import { NavMeshAdapter } from "../../../src/core/pathfinding/adapters/NavMeshAdapter";
|
|
import {
|
|
PathFinder,
|
|
PathStatus,
|
|
} from "../../../src/core/pathfinding/PathFinder";
|
|
import { setup } from "../../util/Setup";
|
|
import { gameFromString } from "./utils";
|
|
|
|
type AdapterFactory = {
|
|
name: string;
|
|
create: (game: Game) => PathFinder;
|
|
};
|
|
|
|
const adapters: AdapterFactory[] = [
|
|
{
|
|
name: "MiniAStarAdapter",
|
|
create: (game) => new MiniAStarAdapter(game, { waterPath: true }),
|
|
},
|
|
{
|
|
name: "NavMeshAdapter",
|
|
create: (game) => new NavMeshAdapter(game),
|
|
},
|
|
];
|
|
|
|
// Shared world game instance
|
|
let worldGame: Game;
|
|
|
|
beforeAll(async () => {
|
|
worldGame = await setup("world", { disableNavMesh: false });
|
|
});
|
|
|
|
describe.each(adapters)("$name", ({ create }) => {
|
|
describe("findPath()", () => {
|
|
test("finds path between adjacent tiles", async () => {
|
|
const game = await gameFromString(["WWWW"]);
|
|
const adapter = create(game);
|
|
const src = game.ref(0, 0);
|
|
const dst = game.ref(1, 0);
|
|
|
|
const path = adapter.findPath(src, dst);
|
|
|
|
expect(path).not.toBeNull();
|
|
expect(path![0]).toBe(src);
|
|
expect(path![path!.length - 1]).toBe(dst);
|
|
});
|
|
|
|
test("finds path across multiple tiles", async () => {
|
|
const game = await gameFromString(["WWWWWW", "WWWWWW", "WWWWWW"]);
|
|
const adapter = create(game);
|
|
const src = game.ref(0, 0);
|
|
const dst = game.ref(5, 2);
|
|
|
|
const path = adapter.findPath(src, dst);
|
|
|
|
expect(path).not.toBeNull();
|
|
expect(path![0]).toBe(src);
|
|
expect(path![path!.length - 1]).toBe(dst);
|
|
});
|
|
|
|
test("returns single-element path for same tile", async () => {
|
|
// Old quirk of MiniAStar, we return dst tile twice
|
|
// Should probably be fixed to return [] instead
|
|
|
|
const game = await gameFromString(["WW"]);
|
|
const adapter = create(game);
|
|
const tile = game.ref(0, 0);
|
|
|
|
const path = adapter.findPath(tile, tile);
|
|
|
|
expect(path).not.toBeNull();
|
|
expect(path!.length).toBe(1);
|
|
expect(path![0]).toBe(tile);
|
|
});
|
|
|
|
test("returns null for blocked path", async () => {
|
|
const game = await gameFromString(["WWLLWW"]);
|
|
const adapter = create(game);
|
|
const src = game.ref(0, 0);
|
|
const dst = game.ref(5, 0);
|
|
|
|
const path = adapter.findPath(src, dst);
|
|
|
|
expect(path).toBeNull();
|
|
});
|
|
|
|
test("returns null for water to land", () => {
|
|
const adapter = create(worldGame);
|
|
const src = worldGame.ref(926, 283); // water
|
|
const dst = worldGame.ref(950, 230); // land
|
|
|
|
const path = adapter.findPath(src, dst);
|
|
|
|
expect(path).toBeNull();
|
|
});
|
|
|
|
test("traverses 3-tile path in 3 tiles", async () => {
|
|
// Expected: [1, 2, 3]
|
|
const game = await gameFromString(["WWWW"]);
|
|
const adapter = create(game);
|
|
const src = game.ref(0, 0);
|
|
const dst = game.ref(3, 0);
|
|
|
|
const path = adapter.findPath(src, dst);
|
|
|
|
expect(path).not.toBeNull();
|
|
expect(path).toEqual([
|
|
game.ref(0, 0),
|
|
game.ref(1, 0),
|
|
game.ref(2, 0),
|
|
game.ref(3, 0),
|
|
]);
|
|
});
|
|
});
|
|
|
|
describe("next() state machine", () => {
|
|
test("returns NEXT on first call", async () => {
|
|
const game = await gameFromString(["WWWW"]);
|
|
const adapter = create(game);
|
|
const src = game.ref(0, 0);
|
|
const dst = game.ref(3, 0);
|
|
|
|
const result = adapter.next(src, dst);
|
|
|
|
expect(result.status).toBe(PathStatus.NEXT);
|
|
});
|
|
|
|
test("returns COMPLETE when at destination", async () => {
|
|
const game = await gameFromString(["WW"]);
|
|
const adapter = create(game);
|
|
const tile = game.ref(0, 0);
|
|
|
|
const result = adapter.next(tile, tile);
|
|
|
|
expect(result.status).toBe(PathStatus.COMPLETE);
|
|
});
|
|
|
|
test("returns NOT_FOUND for blocked path", async () => {
|
|
const game = await gameFromString(["WWLLWW"]);
|
|
const adapter = create(game);
|
|
const src = game.ref(0, 0);
|
|
const dst = game.ref(5, 0);
|
|
|
|
const result = adapter.next(src, dst);
|
|
|
|
expect(result.status).toBe(PathStatus.NOT_FOUND);
|
|
});
|
|
|
|
test("traverses 3-tile path in 4 calls", async () => {
|
|
// Expected: NEXT(1) -> NEXT(2) -> NEXT(3) -> COMPLETE(4)
|
|
const game = await gameFromString(["WWWW"]);
|
|
const adapter = create(game);
|
|
const src = game.ref(0, 0);
|
|
const dst = game.ref(3, 0);
|
|
|
|
let current = src;
|
|
const steps: string[] = [];
|
|
|
|
// 3 NEXT calls to reach destination
|
|
for (let i = 1; i <= 4; i++) {
|
|
const result = adapter.next(current, dst);
|
|
expect([PathStatus.NEXT, PathStatus.COMPLETE]).toContain(result.status);
|
|
|
|
current = (result as { node: TileRef }).node;
|
|
steps.push(`${PathStatus[result.status]}(${current})`);
|
|
}
|
|
|
|
expect(steps).toEqual(["NEXT(1)", "NEXT(2)", "NEXT(3)", "COMPLETE(3)"]);
|
|
});
|
|
});
|
|
|
|
describe("Destination changes", () => {
|
|
test("reaches new destination when dest changes", async () => {
|
|
const game = await gameFromString(["WWWWWWWW"]); // 8 wide
|
|
const adapter = create(game);
|
|
const src = game.ref(0, 0);
|
|
const dst1 = game.ref(4, 0);
|
|
const dst2 = game.ref(7, 0);
|
|
|
|
// First path exists
|
|
expect(adapter.findPath(src, dst1)).not.toBeNull();
|
|
|
|
// Can still find path to new destination
|
|
expect(adapter.findPath(dst1, dst2)).not.toBeNull();
|
|
});
|
|
|
|
test("recomputes when destination changes mid-path", async () => {
|
|
const game = await gameFromString(["WWWWWWWWWWWWWWWWWWWW"]); // 20 wide
|
|
const adapter = create(game);
|
|
const src = game.ref(0, 0);
|
|
const dst1 = game.ref(10, 0);
|
|
const dst2 = game.ref(19, 0);
|
|
|
|
// Start pathing to dst1, take one step
|
|
const result1 = adapter.next(src, dst1);
|
|
expect(result1.status).toBe(PathStatus.NEXT);
|
|
|
|
// Change destination mid-path, continue from current position
|
|
let current = (result1 as { node: TileRef }).node;
|
|
let result = adapter.next(current, dst2);
|
|
for (let i = 0; i < 100 && result.status === PathStatus.NEXT; i++) {
|
|
current = (result as { node: TileRef }).node;
|
|
result = adapter.next(current, dst2);
|
|
}
|
|
|
|
expect(result.status).toBe(PathStatus.COMPLETE);
|
|
expect(current).toBe(dst2);
|
|
});
|
|
});
|
|
|
|
describe("Error handling", () => {
|
|
// MiniAStar logs console error when nulls passed, muted in test
|
|
|
|
test("returns NOT_FOUND for null source", async () => {
|
|
const game = await gameFromString(["WWWW"]);
|
|
const adapter = create(game);
|
|
const dst = game.ref(0, 0);
|
|
|
|
const consoleSpy = vi
|
|
.spyOn(console, "error")
|
|
.mockImplementation(() => {});
|
|
const result = adapter.next(null as unknown as TileRef, dst);
|
|
expect(result.status).toBe(PathStatus.NOT_FOUND);
|
|
consoleSpy.mockRestore();
|
|
});
|
|
|
|
test("returns NOT_FOUND for null destination", async () => {
|
|
const game = await gameFromString(["WWWW"]);
|
|
const adapter = create(game);
|
|
const src = game.ref(0, 0);
|
|
|
|
const consoleSpy = vi
|
|
.spyOn(console, "error")
|
|
.mockImplementation(() => {});
|
|
const result = adapter.next(src, null as unknown as TileRef);
|
|
expect(result.status).toBe(PathStatus.NOT_FOUND);
|
|
consoleSpy.mockRestore();
|
|
});
|
|
});
|
|
|
|
describe("dist parameter", () => {
|
|
test("returns COMPLETE when within dist", () => {
|
|
const adapter = create(worldGame);
|
|
const src = worldGame.ref(926, 283);
|
|
const dst = worldGame.ref(928, 283); // 2 tiles away
|
|
|
|
const result = adapter.next(src, dst, 5);
|
|
|
|
expect(result.status).toBe(PathStatus.COMPLETE);
|
|
});
|
|
|
|
test("returns NEXT when beyond dist", () => {
|
|
const adapter = create(worldGame);
|
|
const src = worldGame.ref(926, 283);
|
|
const dst = worldGame.ref(950, 257);
|
|
|
|
// Adapter may need a few ticks to compute path
|
|
let result = adapter.next(src, dst, 5);
|
|
for (let i = 0; i < 100 && result.status === PathStatus.PENDING; i++) {
|
|
result = adapter.next(src, dst, 5);
|
|
}
|
|
|
|
expect(result.status).toBe(PathStatus.NEXT);
|
|
});
|
|
});
|
|
|
|
describe("World map routes", () => {
|
|
test("Spain to France (Mediterranean)", () => {
|
|
const adapter = create(worldGame);
|
|
const path = adapter.findPath(
|
|
worldGame.ref(926, 283),
|
|
worldGame.ref(950, 257),
|
|
);
|
|
expect(path).not.toBeNull();
|
|
});
|
|
|
|
test("Miami to Rio (Atlantic)", () => {
|
|
const adapter = create(worldGame);
|
|
const path = adapter.findPath(
|
|
worldGame.ref(488, 355),
|
|
worldGame.ref(680, 658),
|
|
);
|
|
expect(path).not.toBeNull();
|
|
expect(path!.length).toBeGreaterThan(100);
|
|
});
|
|
|
|
test("France to Poland (around Europe)", () => {
|
|
const adapter = create(worldGame);
|
|
const path = adapter.findPath(
|
|
worldGame.ref(950, 257),
|
|
worldGame.ref(1033, 175),
|
|
);
|
|
expect(path).not.toBeNull();
|
|
});
|
|
|
|
test("Miami to Spain (transatlantic)", () => {
|
|
const adapter = create(worldGame);
|
|
const path = adapter.findPath(
|
|
worldGame.ref(488, 355),
|
|
worldGame.ref(926, 283),
|
|
);
|
|
expect(path).not.toBeNull();
|
|
});
|
|
|
|
test("Rio to Poland (South Atlantic to Baltic)", () => {
|
|
const adapter = create(worldGame);
|
|
const path = adapter.findPath(
|
|
worldGame.ref(680, 658),
|
|
worldGame.ref(1033, 175),
|
|
);
|
|
expect(path).not.toBeNull();
|
|
});
|
|
});
|
|
|
|
describe("Known bugs", () => {
|
|
test("path can cross 1-tile land barrier", async () => {
|
|
const game = await gameFromString(["WLLWLWWLLW"]);
|
|
const adapter = create(game);
|
|
const path = adapter.findPath(game.ref(0, 0), game.ref(9, 0));
|
|
expect(path).not.toBeNull();
|
|
});
|
|
|
|
test("path can cross diagonal land barrier", async () => {
|
|
const game = await gameFromString(["WL", "LW"]);
|
|
const adapter = create(game);
|
|
const path = adapter.findPath(game.ref(0, 0), game.ref(1, 1));
|
|
expect(path).not.toBeNull();
|
|
});
|
|
});
|
|
});
|