MazePathfinder.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. /// <summary>
  5. /// Handles pathfinding through the maze using A* algorithm
  6. /// Takes into account terrain movement costs
  7. /// </summary>
  8. public class MazePathfinder
  9. {
  10. private MazeData mazeData;
  11. public class PathNode
  12. {
  13. public Vector2Int Position { get; set; }
  14. public PathNode Parent { get; set; }
  15. public float GCost { get; set; } // Cost from start
  16. public float HCost { get; set; } // Heuristic cost to goal
  17. public float FCost => GCost + HCost;
  18. public PathNode(Vector2Int position, PathNode parent, float gCost, float hCost)
  19. {
  20. Position = position;
  21. Parent = parent;
  22. GCost = gCost;
  23. HCost = hCost;
  24. }
  25. }
  26. public MazePathfinder(MazeData mazeData)
  27. {
  28. this.mazeData = mazeData;
  29. }
  30. /// <summary>
  31. /// Finds a path from start to goal using A* with a min-heap open set (O(n log n)).
  32. /// </summary>
  33. public List<Vector2Int> FindPath(Vector2Int start, Vector2Int goal)
  34. {
  35. // Min-heap keyed by FCost
  36. var openHeap = new MinHeap<PathNode>();
  37. var closedSet = new HashSet<Vector2Int>();
  38. // Fast lookup: position → best node in open set
  39. var openLookup = new Dictionary<Vector2Int, PathNode>();
  40. var startNode = new PathNode(start, null, 0, Heuristic(start, goal));
  41. openHeap.Enqueue(startNode, startNode.FCost);
  42. openLookup[start] = startNode;
  43. while (openHeap.Count > 0)
  44. {
  45. var currentNode = openHeap.Dequeue();
  46. openLookup.Remove(currentNode.Position);
  47. if (currentNode.Position == goal)
  48. return ReconstructPath(currentNode);
  49. closedSet.Add(currentNode.Position);
  50. foreach (var neighbor in GetNeighbors(currentNode.Position))
  51. {
  52. if (closedSet.Contains(neighbor)) continue;
  53. float movementCost = GetMovementCost(neighbor);
  54. float tentativeG = currentNode.GCost + movementCost;
  55. if (openLookup.TryGetValue(neighbor, out var existingNode))
  56. {
  57. if (tentativeG < existingNode.GCost)
  58. {
  59. existingNode.Parent = currentNode;
  60. existingNode.GCost = tentativeG;
  61. openHeap.UpdatePriority(existingNode, existingNode.FCost);
  62. }
  63. }
  64. else
  65. {
  66. float hCost = Heuristic(neighbor, goal);
  67. var newNode = new PathNode(neighbor, currentNode, tentativeG, hCost);
  68. openHeap.Enqueue(newNode, newNode.FCost);
  69. openLookup[neighbor] = newNode;
  70. }
  71. }
  72. }
  73. return new List<Vector2Int>();
  74. }
  75. /// <summary>
  76. /// Gets the movable neighbors of a position
  77. /// </summary>
  78. private List<Vector2Int> GetNeighbors(Vector2Int position)
  79. {
  80. var neighbors = new List<Vector2Int>();
  81. Vector2Int[] directions = new[]
  82. {
  83. new Vector2Int(position.x + 1, position.y),
  84. new Vector2Int(position.x - 1, position.y),
  85. new Vector2Int(position.x, position.y + 1),
  86. new Vector2Int(position.x, position.y - 1),
  87. // Optionally add diagonals:
  88. // new Vector2Int(position.x + 1, position.y + 1),
  89. // new Vector2Int(position.x - 1, position.y + 1),
  90. // new Vector2Int(position.x + 1, position.y - 1),
  91. // new Vector2Int(position.x - 1, position.y - 1),
  92. };
  93. foreach (var dir in directions)
  94. {
  95. if (mazeData.IsWalkable(dir.x, dir.y))
  96. {
  97. neighbors.Add(dir);
  98. }
  99. }
  100. return neighbors;
  101. }
  102. /// <summary>
  103. /// Gets the movement cost for a tile based on terrain
  104. /// </summary>
  105. private float GetMovementCost(Vector2Int position)
  106. {
  107. var tile = mazeData.GetTile(position.x, position.y);
  108. if (tile == null) return float.MaxValue;
  109. return tile.GetMovementCost();
  110. }
  111. /// <summary>
  112. /// Heuristic function (Manhattan distance)
  113. /// </summary>
  114. private float Heuristic(Vector2Int from, Vector2Int to)
  115. {
  116. return Mathf.Abs(from.x - to.x) + Mathf.Abs(from.y - to.y);
  117. }
  118. /// <summary>
  119. /// Reconstructs the path from goal to start
  120. /// </summary>
  121. private List<Vector2Int> ReconstructPath(PathNode node)
  122. {
  123. var path = new List<Vector2Int>();
  124. while (node != null)
  125. {
  126. path.Add(node.Position);
  127. node = node.Parent;
  128. }
  129. path.Reverse();
  130. return path;
  131. }
  132. /// <summary>
  133. /// Finds all reachable tiles from a starting position within a certain distance
  134. /// </summary>
  135. public HashSet<Vector2Int> FindReachableTiles(Vector2Int start, float maxDistance)
  136. {
  137. var reachable = new HashSet<Vector2Int>();
  138. var queue = new Queue<(Vector2Int pos, float distance)>();
  139. queue.Enqueue((start, 0));
  140. reachable.Add(start);
  141. while (queue.Count > 0)
  142. {
  143. var (current, distance) = queue.Dequeue();
  144. var neighbors = GetNeighbors(current);
  145. foreach (var neighbor in neighbors)
  146. {
  147. if (!reachable.Contains(neighbor))
  148. {
  149. float cost = GetMovementCost(neighbor);
  150. float newDistance = distance + cost;
  151. if (newDistance <= maxDistance)
  152. {
  153. reachable.Add(neighbor);
  154. queue.Enqueue((neighbor, newDistance));
  155. }
  156. }
  157. }
  158. }
  159. return reachable;
  160. }
  161. }