Files
sor/Server/src/SOR.Game/Grid/Pathfinding.cs
T
2cfa60c7dd Add hex grid support (sides 3–10) for boards, pathfinding, LOS, and client rendering
- New HexGrid.cs: axial coord helpers, BFS neighbors, cube-lerp LOS, jump-range
- Board: HexSides property, GetNeighbors() dispatches hex/square, IsInBounds() hex-aware
- BoardShapeGenerator: MakeHexagon() with edge-based starting zones (2–6 players);
  FromTemplate() now carries HexSides through to the Board
- Pathfinding: Walk/Flight BFS uses board.GetNeighbors() (hex+square unified)
- LineOfSight: delegates to HexGrid.HasLos when hexSides > 0
- GameRules/GameRuleSet/BoardTemplate/BoardTemplateSnapshot: GridType + HexSides fields
- GameMessages BoardDto: GridType + HexSides sent to client
- GameEngine: PlaceArchSpirit range uses HexGrid.Distance on hex boards;
  Teleport LOS passes board.HexSides; ToBoardDto emits GridType/HexSides
- EF migration 20260401000000_AddHexGridSupport: grid_type + hex_sides columns on
  game_rule_sets and board_templates; applied to pg-forge-01
- Client game_screen.gd: hex coord→world (pointy-top flat layout), CylinderMesh
  tiles (radial_segments=6), _board_distance cube metric, _bfs_reachable 6-dir,
  _client_has_los cube-lerp, highlight mesh for hex tiles; reads gridType/hexSides
  from BoardDto

Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
2026-04-01 16:44:17 -04:00

70 lines
2.5 KiB
C#

namespace SOR.Game.Grid;
/// <summary>
/// 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.
/// </summary>
public static class Pathfinding
{
/// <summary>
/// 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).
/// </summary>
public static Dictionary<Coord, int> GetReachableWithCost(
Board board, Coord from, int maxMp, IReadOnlySet<Coord> blocked)
{
var result = new Dictionary<Coord, int>();
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;
}
/// <summary>
/// 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.
/// </summary>
public static IEnumerable<Coord> 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;
}
}
}