PathFinder.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. using Client.MirScenes;
  2. namespace Client.MirObjects
  3. {
  4. public class Heap<T> where T : IHeapItem<T>
  5. {
  6. private T[] items;
  7. private int currentItemCount;
  8. public Heap(int maxHeapSize)
  9. {
  10. items = new T[maxHeapSize];
  11. }
  12. public void Add(T item)
  13. {
  14. item.HeapIndex = currentItemCount;
  15. items[currentItemCount] = item;
  16. SortUp(item);
  17. currentItemCount++;
  18. }
  19. public T RemoveFirst()
  20. {
  21. T firstItem = items[0];
  22. currentItemCount--;
  23. items[0] = items[currentItemCount];
  24. items[0].HeapIndex = 0;
  25. SortDown(items[0]);
  26. return firstItem;
  27. }
  28. public void UpdateItem(T item)
  29. {
  30. SortUp(item);
  31. }
  32. public int Count { get { return currentItemCount; } }
  33. public bool Contains(T item)
  34. {
  35. return Equals(items[item.HeapIndex], item);
  36. }
  37. private void SortDown(T item)
  38. {
  39. while (true)
  40. {
  41. int childIndexLeft = (item.HeapIndex * 2) + 1;
  42. int childIndexRight = (item.HeapIndex * 2) + 2;
  43. int swapIndex = 0;
  44. if (childIndexLeft < currentItemCount)
  45. {
  46. swapIndex = childIndexLeft;
  47. if (childIndexRight < currentItemCount)
  48. {
  49. if (items[childIndexLeft].CompareTo(items[childIndexRight]) < 0)
  50. {
  51. swapIndex = childIndexRight;
  52. }
  53. }
  54. if (item.CompareTo(items[swapIndex]) < 0)
  55. {
  56. Swap(item, items[swapIndex]);
  57. }
  58. else
  59. {
  60. return;
  61. }
  62. }
  63. else
  64. {
  65. return;
  66. }
  67. }
  68. }
  69. private void SortUp(T item)
  70. {
  71. int parentIndex = (item.HeapIndex - 1) / 2;
  72. while (true)
  73. {
  74. T parentItem = items[parentIndex];
  75. if (item.CompareTo(parentItem) > 0)
  76. {
  77. Swap(item, parentItem);
  78. }
  79. else
  80. break;
  81. parentIndex = (item.HeapIndex - 1) / 2;
  82. }
  83. }
  84. private void Swap(T itemA, T itemB)
  85. {
  86. items[itemA.HeapIndex] = itemB;
  87. items[itemB.HeapIndex] = itemA;
  88. int itemAIndex = itemA.HeapIndex;
  89. itemA.HeapIndex = itemB.HeapIndex;
  90. itemB.HeapIndex = itemAIndex;
  91. }
  92. }
  93. public interface IHeapItem<T> : IComparable<T>
  94. {
  95. int HeapIndex
  96. {
  97. get; set;
  98. }
  99. }
  100. public class Node : IHeapItem<Node>
  101. {
  102. public bool Walkable
  103. {
  104. get { return Map.EmptyCell(Location); }
  105. }
  106. public MapControl Map;
  107. public Point Location;
  108. public Node Parent;
  109. public int GCost, HCost;
  110. public int FCost
  111. {
  112. get { return GCost + HCost; }
  113. }
  114. private int _heapIndex;
  115. public int HeapIndex
  116. {
  117. get { return _heapIndex; }
  118. set { _heapIndex = value; }
  119. }
  120. public int CompareTo(Node nodeToCompare)
  121. {
  122. int compare = FCost.CompareTo(nodeToCompare.FCost);
  123. if (compare == 0)
  124. {
  125. compare = HCost.CompareTo(nodeToCompare.HCost);
  126. }
  127. return -compare;
  128. }
  129. public Node(MapControl map, int x, int y)
  130. {
  131. Map = map;
  132. Location = new Point(x, y);
  133. }
  134. }
  135. public class PathFinder
  136. {
  137. private Node[,] Grid;
  138. public MapControl Map;
  139. public int MaxSize
  140. {
  141. get { return Map.Width * Map.Height; }
  142. }
  143. public PathFinder(MapControl map)
  144. {
  145. Map = map;
  146. CreateGrid();
  147. }
  148. private void CreateGrid()
  149. {
  150. Grid = new Node[Map.Width, Map.Height];
  151. for (int x = 0; x < Map.Width; x++)
  152. {
  153. for (int y = 0; y < Map.Height; y++)
  154. {
  155. Grid[x, y] = new Node(Map, x, y);
  156. }
  157. }
  158. }
  159. public List<Node> FindPath(Point start, Point target, int MaxNodes = 0)
  160. {
  161. Node startNode = GetNode(start);
  162. Node targetNode = GetNode(target);
  163. Heap<Node> openSet = new Heap<Node>(MaxSize);
  164. HashSet<Node> closedSet = new HashSet<Node>();
  165. openSet.Add(startNode);
  166. while (openSet.Count > 0)
  167. {
  168. Node currentNode = openSet.RemoveFirst();
  169. closedSet.Add(currentNode);
  170. if (currentNode == targetNode)
  171. {
  172. var path = RetracePath(startNode, targetNode);
  173. return MaxNodes == 0 || path.Count < MaxNodes ? path : null;
  174. }
  175. foreach (Node neighbor in GetNeighbours(currentNode))
  176. {
  177. if (!neighbor.Walkable || closedSet.Contains(neighbor))
  178. continue;
  179. int newMovementCostToNeighbor = currentNode.GCost + GetDistance(currentNode, neighbor);
  180. if (newMovementCostToNeighbor < neighbor.GCost || !openSet.Contains(neighbor))
  181. {
  182. neighbor.GCost = newMovementCostToNeighbor;
  183. neighbor.HCost = GetDistance(neighbor, targetNode);
  184. neighbor.Parent = currentNode;
  185. if (!openSet.Contains(neighbor))
  186. openSet.Add(neighbor);
  187. else
  188. {
  189. openSet.UpdateItem(neighbor);
  190. }
  191. }
  192. }
  193. }
  194. return null;
  195. }
  196. public List<Node> RetracePath(Node startNode, Node endNode)
  197. {
  198. List<Node> path = new List<Node>();
  199. Node currentNode = endNode;
  200. while (currentNode != startNode)
  201. {
  202. path.Add(currentNode);
  203. currentNode = currentNode.Parent;
  204. }
  205. path.Add(startNode);
  206. path.Reverse();
  207. return path;
  208. }
  209. private int GetDistance(Node nodeA, Node nodeB)
  210. {
  211. int distX = Math.Abs(nodeA.Location.X - nodeB.Location.X);
  212. int distY = Math.Abs(nodeA.Location.Y - nodeB.Location.Y);
  213. if (distX > distY)
  214. return 14 * distY + (10 * (distX - distY));
  215. return 14 * distX + (10 * (distY - distX));
  216. }
  217. private Node GetNode(Point location)
  218. {
  219. return Grid[location.X, location.Y];
  220. }
  221. private List<Node> GetNeighbours(Node node)
  222. {
  223. List<Node> neighbours = new List<Node>();
  224. for (int x = -1; x <= 1; x++)
  225. {
  226. for (int y = -1; y <= 1; y++)
  227. {
  228. if (x == 0 && y == 0) continue;
  229. int checkX = node.Location.X + x;
  230. int checkY = node.Location.Y + y;
  231. if (checkX >= 0 && checkX < Grid.GetLength(0) && checkY >= 0 && checkY < Grid.GetLength(1))
  232. {
  233. neighbours.Add(Grid[checkX, checkY]);
  234. }
  235. }
  236. }
  237. return neighbours;
  238. }
  239. }
  240. }