PathHandler.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #define DECREASE_KEY
  2. namespace PF {
  3. /** Stores temporary node data for a single pathfinding request.
  4. * Every node has one PathNode per thread used.
  5. * It stores e.g G score, H score and other temporary variables needed
  6. * for path calculation, but which are not part of the graph structure.
  7. *
  8. * \see Pathfinding.PathHandler
  9. * \see https://en.wikipedia.org/wiki/A*_search_algorithm
  10. */
  11. public class PathNode {
  12. /** Reference to the actual graph node */
  13. public GraphNode node;
  14. /** Parent node in the search tree */
  15. public PathNode parent;
  16. /** The path request (in this thread, if multithreading is used) which last used this node */
  17. public ushort pathID;
  18. #if DECREASE_KEY
  19. /** Index of the node in the binary heap.
  20. * The open list in the A* algorithm is backed by a binary heap.
  21. * To support fast 'decrease key' operations, the index of the node
  22. * is saved here.
  23. */
  24. public ushort heapIndex = BinaryHeap.NotInHeap;
  25. #endif
  26. /** Bitpacked variable which stores several fields */
  27. private uint flags;
  28. /** Cost uses the first 28 bits */
  29. private const uint CostMask = (1U << 28) - 1U;
  30. /** Flag 1 is at bit 28 */
  31. private const int Flag1Offset = 28;
  32. private const uint Flag1Mask = (uint)(1 << Flag1Offset);
  33. /** Flag 2 is at bit 29 */
  34. private const int Flag2Offset = 29;
  35. private const uint Flag2Mask = (uint)(1 << Flag2Offset);
  36. public uint cost {
  37. get {
  38. return flags & CostMask;
  39. }
  40. set {
  41. flags = (flags & ~CostMask) | value;
  42. }
  43. }
  44. /** Use as temporary flag during pathfinding.
  45. * Pathfinders (only) can use this during pathfinding to mark
  46. * nodes. When done, this flag should be reverted to its default state (false) to
  47. * avoid messing up other pathfinding requests.
  48. */
  49. public bool flag1 {
  50. get {
  51. return (flags & Flag1Mask) != 0;
  52. }
  53. set {
  54. flags = (flags & ~Flag1Mask) | (value ? Flag1Mask : 0U);
  55. }
  56. }
  57. /** Use as temporary flag during pathfinding.
  58. * Pathfinders (only) can use this during pathfinding to mark
  59. * nodes. When done, this flag should be reverted to its default state (false) to
  60. * avoid messing up other pathfinding requests.
  61. */
  62. public bool flag2 {
  63. get {
  64. return (flags & Flag2Mask) != 0;
  65. }
  66. set {
  67. flags = (flags & ~Flag2Mask) | (value ? Flag2Mask : 0U);
  68. }
  69. }
  70. /** Backing field for the G score */
  71. private uint g;
  72. /** Backing field for the H score */
  73. private uint h;
  74. /** G score, cost to get to this node */
  75. public uint G { get { return g; } set { g = value; } }
  76. /** H score, estimated cost to get to to the target */
  77. public uint H { get { return h; } set { h = value; } }
  78. /** F score. H score + G score */
  79. public uint F { get { return g+h; } }
  80. public void UpdateG (Path path) {
  81. #if ASTAR_NO_TRAVERSAL_COST
  82. g = parent.g + cost;
  83. #else
  84. g = parent.g + cost + path.GetTraversalCost(node);
  85. #endif
  86. }
  87. }
  88. /** Handles thread specific path data.
  89. */
  90. public class PathHandler {
  91. /** Current PathID.
  92. * \see #PathID
  93. */
  94. private ushort pathID;
  95. public readonly int threadID;
  96. public readonly int totalThreadCount;
  97. /**
  98. * Binary heap to keep track of nodes on the "Open list".
  99. * \see https://en.wikipedia.org/wiki/A*_search_algorithm
  100. */
  101. public readonly BinaryHeap heap = new BinaryHeap(128);
  102. /** ID for the path currently being calculated or last path that was calculated */
  103. public ushort PathID { get { return pathID; } }
  104. /** Array of all PathNodes */
  105. public PathNode[] nodes = new PathNode[0];
  106. /** StringBuilder that paths can use to build debug strings.
  107. * Better for performance and memory usage to use a single StringBuilder instead of each path creating its own
  108. */
  109. public readonly System.Text.StringBuilder DebugStringBuilder = new System.Text.StringBuilder();
  110. public PathHandler (int threadID, int totalThreadCount) {
  111. this.threadID = threadID;
  112. this.totalThreadCount = totalThreadCount;
  113. }
  114. public void InitializeForPath (Path p) {
  115. pathID = p.pathID;
  116. heap.Clear();
  117. }
  118. /** Internal method to clean up node data */
  119. public void DestroyNode (GraphNode node) {
  120. PathNode pn = GetPathNode(node);
  121. // Clean up references to help the GC
  122. pn.node = null;
  123. pn.parent = null;
  124. // This is not required for pathfinding, but not clearing it may confuse gizmo drawing for a fraction of a second.
  125. // Especially when 'Show Search Tree' is enabled
  126. pn.pathID = 0;
  127. pn.G = 0;
  128. pn.H = 0;
  129. }
  130. /** Internal method to initialize node data */
  131. public void InitializeNode (GraphNode node) {
  132. //Get the index of the node
  133. int ind = node.NodeIndex;
  134. if (ind >= nodes.Length) {
  135. // Grow by a factor of 2
  136. PathNode[] newNodes = new PathNode[System.Math.Max(128, nodes.Length*2)];
  137. nodes.CopyTo(newNodes, 0);
  138. // Initialize all PathNode instances at once
  139. // It is important that we do this here and don't for example leave the entries as NULL and initialize
  140. // them lazily. By allocating them all at once we are much more likely to allocate the PathNodes close
  141. // to each other in memory (most systems use some kind of bumb-allocator) and this improves cache locality
  142. // and reduces false sharing (which would happen if we allocated PathNodes for the different threads close
  143. // to each other). This has been profiled to give around a 4% difference in overall pathfinding performance.
  144. for (int i = nodes.Length; i < newNodes.Length; i++) newNodes[i] = new PathNode();
  145. nodes = newNodes;
  146. }
  147. nodes[ind].node = node;
  148. }
  149. public PathNode GetPathNode (int nodeIndex) {
  150. return nodes[nodeIndex];
  151. }
  152. /** Returns the PathNode corresponding to the specified node.
  153. * The PathNode is specific to this PathHandler since multiple PathHandlers
  154. * are used at the same time if multithreading is enabled.
  155. */
  156. public PathNode GetPathNode (GraphNode node) {
  157. return nodes[node.NodeIndex];
  158. }
  159. /** Set all nodes' pathIDs to 0.
  160. * \see Pathfinding.PathNode.pathID
  161. */
  162. public void ClearPathIDs () {
  163. for (int i = 0; i < nodes.Length; i++) {
  164. if (nodes[i] != null) nodes[i].pathID = 0;
  165. }
  166. }
  167. }
  168. }