@pocketrts/grid
Grid algorithms for real-time strategy games on an 8-connected cell grid: flow fields shared by whole armies and computed under a fixed per-step budget, bounded local A*, straight-line reach, nearest free cell, distance fields, bucket grids for neighbour queries, field of view, polygons, 8-connected wall chains, line formations and a complete elastic-column march system. Pure logic over typed arrays, deterministic (only @pocketrts/determinism math), no DOM. Extracted from Pocket War, where it moves thousands of units.
Try it: apps/grid-demo (pnpm --filter @pocketrts/grid-demo dev).
Install
pnpm add @pocketrts/grid
ES module, TypeScript types included. Depends on @pocketrts/determinism.
Concepts
- Cells are numbered row-major:
cell = y * w + x. Directions are indices into DIRX / DIRY (clockwise from east, y down; odd = diagonal).
- The game owns the grid. The package reads what it is given (cost and block arrays, step-rule callbacks) and never stores game objects.
- Diagonal steps normally follow
cornerFree: no squeezing between two diagonal obstacles, so a one-cell-wide 8-connected wall (see ringChain) really closes.
- Everything is deterministic for identical inputs, including budgeted flow-field progress (the budget counts nodes, not time), so it is safe in lockstep simulations.
Interface