|
| 1 | +import type { Point, Vector2D } from '../types.js'; |
| 2 | + |
| 3 | +export interface FlowFieldOptions { |
| 4 | + grid: number[][]; |
| 5 | + goal: Point; |
| 6 | + allowDiagonal?: boolean; |
| 7 | +} |
| 8 | + |
| 9 | +export interface FlowFieldResult { |
| 10 | + cost: number[][]; |
| 11 | + flow: Vector2D[][]; |
| 12 | +} |
| 13 | + |
| 14 | +const ORTHOGONAL_NEIGHBORS: ReadonlyArray<Point> = [ |
| 15 | + { x: 1, y: 0 }, |
| 16 | + { x: -1, y: 0 }, |
| 17 | + { x: 0, y: 1 }, |
| 18 | + { x: 0, y: -1 }, |
| 19 | +]; |
| 20 | + |
| 21 | +const DIAGONAL_NEIGHBORS: ReadonlyArray<Point> = [ |
| 22 | + { x: 1, y: 1 }, |
| 23 | + { x: -1, y: 1 }, |
| 24 | + { x: 1, y: -1 }, |
| 25 | + { x: -1, y: -1 }, |
| 26 | +]; |
| 27 | + |
| 28 | +/** |
| 29 | + * Builds a flow field pointing toward the goal cell using uniform-cost integration. |
| 30 | + * Useful for: multi-unit steering, crowd navigation, RTS flow maps. |
| 31 | + */ |
| 32 | +export function computeFlowField(options: FlowFieldOptions): FlowFieldResult { |
| 33 | + const { grid, goal, allowDiagonal = true } = options; |
| 34 | + validateGrid(grid, goal); |
| 35 | + |
| 36 | + const height = grid.length; |
| 37 | + const width = grid[0]?.length ?? 0; |
| 38 | + const cost: number[][] = Array.from({ length: height }, () => Array<number>(width).fill(Number.POSITIVE_INFINITY)); |
| 39 | + const flow: Vector2D[][] = Array.from({ length: height }, () => |
| 40 | + Array.from({ length: width }, () => ({ x: 0, y: 0 } as Vector2D)) |
| 41 | + ); |
| 42 | + |
| 43 | + const queue: Array<{ point: Point; priority: number }> = []; |
| 44 | + if (isWalkable(grid, goal.x, goal.y)) { |
| 45 | + cost[goal.y][goal.x] = 0; |
| 46 | + queue.push({ point: goal, priority: 0 }); |
| 47 | + } |
| 48 | + |
| 49 | + while (queue.length > 0) { |
| 50 | + queue.sort((a, b) => a.priority - b.priority); |
| 51 | + const current = queue.shift()!; |
| 52 | + const currentCost = cost[current.point.y][current.point.x]; |
| 53 | + |
| 54 | + const neighbors = allowDiagonal |
| 55 | + ? [...ORTHOGONAL_NEIGHBORS, ...DIAGONAL_NEIGHBORS] |
| 56 | + : ORTHOGONAL_NEIGHBORS; |
| 57 | + |
| 58 | + for (const dir of neighbors) { |
| 59 | + const nx = current.point.x + dir.x; |
| 60 | + const ny = current.point.y + dir.y; |
| 61 | + if (!isWalkable(grid, nx, ny)) { |
| 62 | + continue; |
| 63 | + } |
| 64 | + const stepCost = dir.x !== 0 && dir.y !== 0 ? Math.SQRT2 : 1; |
| 65 | + const tentative = currentCost + stepCost; |
| 66 | + if (tentative < cost[ny][nx]) { |
| 67 | + cost[ny][nx] = tentative; |
| 68 | + queue.push({ point: { x: nx, y: ny }, priority: tentative }); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + for (let y = 0; y < height; y += 1) { |
| 74 | + for (let x = 0; x < width; x += 1) { |
| 75 | + if (!isWalkable(grid, x, y) || !Number.isFinite(cost[y][x])) { |
| 76 | + flow[y][x] = { x: 0, y: 0 }; |
| 77 | + continue; |
| 78 | + } |
| 79 | + if (x === goal.x && y === goal.y) { |
| 80 | + flow[y][x] = { x: 0, y: 0 }; |
| 81 | + continue; |
| 82 | + } |
| 83 | + let bestNeighbor: Point | null = null; |
| 84 | + let bestCost = cost[y][x]; |
| 85 | + |
| 86 | + const neighbors = allowDiagonal |
| 87 | + ? [...ORTHOGONAL_NEIGHBORS, ...DIAGONAL_NEIGHBORS] |
| 88 | + : ORTHOGONAL_NEIGHBORS; |
| 89 | + |
| 90 | + for (const dir of neighbors) { |
| 91 | + const nx = x + dir.x; |
| 92 | + const ny = y + dir.y; |
| 93 | + if (nx < 0 || ny < 0 || ny >= height || nx >= width) { |
| 94 | + continue; |
| 95 | + } |
| 96 | + const neighborCost = cost[ny][nx]; |
| 97 | + if (neighborCost < bestCost) { |
| 98 | + bestCost = neighborCost; |
| 99 | + bestNeighbor = { x: nx, y: ny }; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + if (!bestNeighbor) { |
| 104 | + flow[y][x] = { x: 0, y: 0 }; |
| 105 | + continue; |
| 106 | + } |
| 107 | + |
| 108 | + const dx = bestNeighbor.x - x; |
| 109 | + const dy = bestNeighbor.y - y; |
| 110 | + const magnitude = Math.hypot(dx, dy) || 1; |
| 111 | + flow[y][x] = { x: dx / magnitude, y: dy / magnitude }; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + return { cost, flow }; |
| 116 | +} |
| 117 | + |
| 118 | +function isWalkable(grid: number[][], x: number, y: number): boolean { |
| 119 | + return grid[y]?.[x] === 0; |
| 120 | +} |
| 121 | + |
| 122 | +function validateGrid(grid: number[][], goal: Point): void { |
| 123 | + if (!Array.isArray(grid) || grid.length === 0) { |
| 124 | + throw new TypeError('grid must be a non-empty 2D array'); |
| 125 | + } |
| 126 | + const width = grid[0]?.length; |
| 127 | + if (!grid.every((row) => Array.isArray(row) && row.length === width)) { |
| 128 | + throw new TypeError('grid rows must be arrays of equal length'); |
| 129 | + } |
| 130 | + if (!isWithin(grid, goal.x, goal.y)) { |
| 131 | + throw new RangeError('goal must be inside the grid bounds'); |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +function isWithin(grid: number[][], x: number, y: number): boolean { |
| 136 | + return y >= 0 && y < grid.length && x >= 0 && x < (grid[0]?.length ?? 0); |
| 137 | +} |
0 commit comments