namespace SOR.Game.Grid; /// /// Pathfinding algorithms for all four movement types on a square grid. /// /// Walk / Flight — BFS returning tiles reachable within a given MP budget. /// Walk: blocked by any occupied tile. /// Flight: blocked only by tiles occupied by other Flight units. /// Callers are responsible for building the appropriate blocked set. /// /// Jump / Teleport — range set: all in-bounds tiles within Manhattan [1..maxMp]. /// Each landing costs exactly 1 MP regardless of distance. /// Teleport additionally requires LOS — callers filter with LineOfSight.HasLos. /// public static class Pathfinding { /// /// BFS for Walk/Flight movement. /// Returns all reachable non-blocked tiles mapped to their step cost. /// The starting tile is excluded from results (can't "move" to current position). /// public static Dictionary GetReachableWithCost( Board board, Coord from, int maxMp, IReadOnlySet blocked) { var result = new Dictionary(); var queue = new Queue<(Coord coord, int cost)>(); result[from] = 0; queue.Enqueue((from, 0)); while (queue.Count > 0) { var (current, cost) = queue.Dequeue(); if (cost >= maxMp) continue; foreach (var neighbor in board.GetNeighbors(current)) { if (blocked.Contains(neighbor)) continue; if (result.ContainsKey(neighbor)) continue; result[neighbor] = cost + 1; queue.Enqueue((neighbor, cost + 1)); } } result.Remove(from); return result; } /// /// Jump/Teleport range: all in-bounds tiles within Manhattan distance [minRange..maxMp] /// from the starting position. /// /// For Teleport, callers should additionally filter by LineOfSight.HasLos. /// public static IEnumerable GetJumpRange( Board board, Coord from, int minRange, int maxRange) { for (int dy = -maxRange; dy <= maxRange; dy++) for (int dx = -maxRange; dx <= maxRange; dx++) { var target = new Coord(from.X + dx, from.Y + dy); if (!board.IsInBounds(target)) continue; var dist = from.ManhattanDistance(target); if (dist < minRange || dist > maxRange) continue; yield return target; } } }