@leptoquark1/openage-pfadfinder
v2.1.0
Published
TypeScript port of the Pathfinding Subsystem
Maintainers
Readme
openage Pfadfinder
TypeScript port of the Pathfinding Subsystem
A dependency-free, native ESM TypeScript library implementing two-stage hierarchical flow-field pathfinding — a port of the openage pathfinding system based on Crowd Pathfinding and Steering Using Flow Field Tiles by Elijah Emerson.
Learn more about the underlying Pathfinding in openage Documentation or use the interactive Guide in the interactive Demo.
→ Try the interactive Demo
Installation
pnpm add @leptoquark1/openage-pfadfinder
npm install @leptoquark1/openage-pfadfinder
yarn add @leptoquark1/openage-pfadfinderThe package is ESM-only and ships TypeScript declarations.
Usage
import { Pathfinder, Grid, Sector, CostField, COST_MIN, COST_IMPASSABLE } from '@leptoquark1/openage-pfadfinder';
// Create a 4×4 grid with 10×10 sectors
const grid = new Grid(0, { width: 4, height: 4 }, 10);
// Populate sectors with cost fields
let sectorId = 0;
for (let sy = 0; sy < 4; sy++) {
for (let sx = 0; sx < 4; sx++) {
const cf = new CostField(10);
cf.setCosts(new Uint8Array(100).fill(COST_MIN));
grid.addSector(new Sector(sectorId++, { x: sx, y: sy }, cf));
}
}
// Initialize portals and portal graph
grid.initPortals();
grid.initPortalNodes();
// Create pathfinder
const pathfinder = new Pathfinder();
pathfinder.addGrid(grid);
// Find a path
const path = pathfinder.getPath({
gridId: 0,
start: { ne: 2, se: 3 },
target: { ne: 35, se: 28 },
});
console.log(path.result); // PathResult.FOUND (0)
console.log(path.waypoints); // [{ ne: 2, se: 3 }, { ne: 12, se: 10 }, ...]For the full API reference — every class, field and helper with signatures — see docs/API.md.
Architecture
Two-stage hierarchical pathfinding:
- High-Level (Portal A*) — A* search on the portal graph to find which sectors to traverse
- Low-Level (Flow Fields) — Per-sector cost → integration → flow field pipeline
PathRequest → Grid → identify start/target sectors
→ Portal A* (high-level path through sectors)
→ Flow-Field integration per sector (low-level directions)
→ Waypoint extraction
→ Path returnedCore Concepts
| Component | Description |
| -------------------- | --------------------------------------------------------------------------------------------- |
| CostField | Per-cell movement cost (Uint8Array, 1–254 passable, 255 impassable) |
| IntegrationField | Accumulated cost from target via wavefront + LOS (Uint16Array + Uint8Array flags) |
| FlowField | Per-cell movement direction toward lowest-cost neighbor (Uint8Array, 4-bit dir + 4-bit flags) |
| Sector | Square grid subdivision with its own CostField + Portals |
| Portal | Bidirectional gateway between adjacent sectors |
| Grid | Collection of sectors, manages portal graph |
| Integrator | Orchestrates field computation with caching |
| Pathfinder | Main interface — getPath() coordinates both stages |
Algorithms
- LOS Integration — Wavefront with Bresenham line-of-sight shadowcasting
- Cost Integration — Dijkstra-like wavefront expansion (4-connected)
- Flow Field Build — Gradient descent on integration costs (8-connected)
- Portal A* — A* on the portal graph with Euclidean heuristic
- Field Caching — Integration/flow fields cached per portal-sector pair
Performance
All fields use Typed Arrays (Uint8Array/Uint16Array) matching the C++ uint8_t/uint16_t data
layout. The BinaryHeap provides O(log n) priority queue operations for A*.
| Aspect | C++ openage | TypeScript |
| ----------------- | ------------------- | ---------------------- |
| Cost Field | vector<uint8_t> | Uint8Array |
| Integration Cost | uint16_t | Uint16Array |
| Integration Flags | uint8_t | Uint8Array |
| Flow Field | uint8_t (4+4 bit) | Uint8Array (4+4 bit) |
| Priority Queue | PairingHeap | BinaryHeap |
Project structure
packages/pfadfinder/ Published library (@leptoquark1/openage-pfadfinder)
src/index.ts Public package entry point (subpath ".")
src/lib/core/ Cost/integration/flow fields, integrator
src/lib/graph/ Grid, Sector, Portal
src/lib/internal/ BinaryHeap, Bresenham line
src/lib/pathfinding/ Pathfinder, portal graph A*, field cache, routing
src/shared/ Constants, bit ops, coordinates, cost-map helpers, types
test/ Vitest test suite
apps/demo/ Interactive playground (React + Vite, deployed to GitHub Pages)
docs/ API reference, release and benchmark documentation
.github/workflows/ CI and release automation
.changeset/ Changesets configurationDevelopment
Requires Node.js >=20.
pnpm install
pnpm build # library dist/, also needed by the demo's typecheck and tests
pnpm lint
pnpm typecheck
pnpm test
pnpm test:coverage
pnpm test:perf # timing-sensitive demo benchmarks (excluded from `pnpm test`)
pnpm benchmark # updateCosts single vs. batched, Markdown table (`--json` for JSON, `--pretty` for a terminal table)
pnpm benchmark latest # compare the latest release ("Then") with this build ("Now")
pnpm benchmark <other-build> # compare another build ("Then") with this one ("Now")
pnpm benchmark --docs # also write the results to docs/BENCHMARK.md
pnpm check:packageRun everything that CI runs:
pnpm checkDemo
An interactive playground showing the flow-field pathfinding in action: targets, roaming mobs, moving obstacles and a live performance panel.
Running the demo requires a built library:
pnpm buildRun demo with:
pnpm --filter @leptoquark1/openage-pfadfinder-demo devWatch both with:
pnpm -r --parallel devLicense
GPL-3.0-or-later (same as openage)
