import type { WalkableGrid } from './walkable-grid.js'; // Geodesic distance field over the walkable grid, integer octile costs: // straight 5, diagonal 7 (~1:1.4). One tile = 4px = 5 units. export const COST_STRAIGHT = 5; export const COST_DIAG = 7; export const UNITS_PER_PX = COST_STRAIGHT / 4; export const PX_PER_UNIT = 4 / COST_STRAIGHT; export interface FieldResult { /** Geodesic cost in units from the source; Infinity if unreached. Valid until the next compute(). */ distOf(cell: number): number; /** Per angle bucket, the farthest settled cell with dist <= ringUnits; -1 for empty buckets. */ ringCandidates: Int32Array; } export class FieldCalculator { private readonly dist: Int32Array; private readonly stamp: Int32Array; private gen = 0; private source = -1; constructor(private readonly grid: WalkableGrid) { this.dist = new Int32Array(grid.width * grid.height); this.stamp = new Int32Array(grid.width * grid.height); } /** * Dial's-bucket Dijkstra from sourceCell, settling cells up to maxCostUnits. * Diagonal steps require both orthogonal neighbors walkable (no corner * cutting, matching the server's supercover line checks). */ compute(sourceCell: number, maxCostUnits: number, ringUnits: number, bucketCount = 16): FieldResult { const { grid, dist, stamp } = this; const W = grid.width; this.gen++; this.source = sourceCell; const gen = this.gen; const ringCandidates = new Int32Array(bucketCount).fill(-1); const ringDist = new Int32Array(bucketCount).fill(-1); const sx = sourceCell % W; const sy = Math.floor(sourceCell / W); const buckets: (number[] | undefined)[] = new Array(maxCostUnits + COST_DIAG + 1); dist[sourceCell] = 0; stamp[sourceCell] = gen; buckets[0] = [sourceCell]; for (let c = 0; c <= maxCostUnits; c++) { const bucket = buckets[c]; if (!bucket) continue; for (const cell of bucket) { if (dist[cell] !== c || stamp[cell] !== gen) continue; // stale entry const tx = cell % W; const ty = Math.floor(cell / W); if (c > 0 && c <= ringUnits) { const b = Math.floor(((Math.atan2(ty - sy, tx - sx) + Math.PI) / (2 * Math.PI)) * bucketCount) % bucketCount; if (c > ringDist[b]) { ringDist[b] = c; ringCandidates[b] = cell; } } const e = grid.isWalkable(tx + 1, ty); const w = grid.isWalkable(tx - 1, ty); const s = grid.isWalkable(tx, ty + 1); const n = grid.isWalkable(tx, ty - 1); this.relax(cell + 1, c + COST_STRAIGHT, e, buckets, gen); this.relax(cell - 1, c + COST_STRAIGHT, w, buckets, gen); this.relax(cell + W, c + COST_STRAIGHT, s, buckets, gen); this.relax(cell - W, c + COST_STRAIGHT, n, buckets, gen); this.relax(cell + W + 1, c + COST_DIAG, e && s && grid.isWalkable(tx + 1, ty + 1), buckets, gen); this.relax(cell + W - 1, c + COST_DIAG, w && s && grid.isWalkable(tx - 1, ty + 1), buckets, gen); this.relax(cell - W + 1, c + COST_DIAG, e && n && grid.isWalkable(tx + 1, ty - 1), buckets, gen); this.relax(cell - W - 1, c + COST_DIAG, w && n && grid.isWalkable(tx - 1, ty - 1), buckets, gen); } buckets[c] = undefined; } const distOf = (cell: number) => (stamp[cell] === gen ? dist[cell] : Infinity); return { distOf, ringCandidates }; } private relax(cell: number, cost: number, walkable: boolean, buckets: (number[] | undefined)[], gen: number): void { if (!walkable) return; if (this.stamp[cell] === gen && this.dist[cell] <= cost) return; this.dist[cell] = cost; this.stamp[cell] = gen; (buckets[cost] ??= []).push(cell); } /** * Walk from fromCell down the latest field's gradient toward its source, * stopping once >= budgetUnits is spent (the last step may overshoot). * Unreached cells stay frozen in place. Returns visited cells incl. ends. */ descend(fromCell: number, budgetUnits: number): { end: number; cells: number[] } { const cells = [fromCell]; if (this.distOfCurrent(fromCell) === Infinity) return { end: fromCell, cells }; let cur = fromCell; let spent = 0; while (spent < budgetUnits && this.distOfCurrent(cur) > 0) { const next = this.bestNeighbor(cur); if (next < 0) break; spent += this.distOfCurrent(cur) - this.distOfCurrent(next); cur = next; cells.push(cur); } return { end: cur, cells }; } /** Shrimp step path: cells from the latest field's source to fromCell. */ pathToSource(fromCell: number): number[] { return this.descend(fromCell, Number.MAX_SAFE_INTEGER).cells.reverse(); } private distOfCurrent(cell: number): number { return this.stamp[cell] === this.gen ? this.dist[cell] : Infinity; } private bestNeighbor(cell: number): number { const { grid } = this; const W = grid.width; const tx = cell % W; const ty = Math.floor(cell / W); const e = grid.isWalkable(tx + 1, ty); const w = grid.isWalkable(tx - 1, ty); const s = grid.isWalkable(tx, ty + 1); const n = grid.isWalkable(tx, ty - 1); let best = -1; let bestDist = this.distOfCurrent(cell); const consider = (c: number, walkable: boolean) => { if (!walkable) return; const d = this.distOfCurrent(c); if (d < bestDist) { bestDist = d; best = c; } }; consider(cell + 1, e); consider(cell - 1, w); consider(cell + W, s); consider(cell - W, n); consider(cell + W + 1, e && s && grid.isWalkable(tx + 1, ty + 1)); consider(cell + W - 1, w && s && grid.isWalkable(tx - 1, ty + 1)); consider(cell - W + 1, e && n && grid.isWalkable(tx + 1, ty - 1)); consider(cell - W - 1, w && n && grid.isWalkable(tx - 1, ty - 1)); return best; } }