GraphUpdateUtilities.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System.Collections.Generic;
  2. using Pathfinding.Util;
  3. namespace Pathfinding {
  4. /// <summary>
  5. /// Contains useful functions for updating graphs.
  6. /// This class works a lot with the GraphNode class, a useful function to get nodes is <see cref="AstarPath.GetNearest"/>.
  7. ///
  8. /// See: <see cref="AstarPath.GetNearest"/>
  9. /// See: <see cref="Pathfinding.PathUtilities"/>
  10. ///
  11. /// Since: Added in 3.1
  12. /// </summary>
  13. public static class GraphUpdateUtilities {
  14. /// <summary>
  15. /// Updates graphs and checks if all nodes are still reachable from each other.
  16. /// Graphs are updated, then a check is made to see if the nodes are still reachable from each other.
  17. /// If they are not, the graphs are reverted to before the update and false is returned.
  18. /// This is slower than a normal graph update.
  19. /// All queued graph updates and thread safe callbacks will be flushed during this function.
  20. ///
  21. /// Returns: True if the given nodes are still reachable from each other after the guo has been applied. False otherwise.
  22. ///
  23. /// <code>
  24. /// var guo = new GraphUpdateObject(tower.GetComponent<Collider>().bounds);
  25. /// var spawnPointNode = AstarPath.active.GetNearest(spawnPoint.position).node;
  26. /// var goalNode = AstarPath.active.GetNearest(goalPoint.position).node;
  27. ///
  28. /// if (GraphUpdateUtilities.UpdateGraphsNoBlock(guo, spawnPointNode, goalNode, false)) {
  29. /// // Valid tower position
  30. /// // Since the last parameter (which is called "alwaysRevert") in the method call was false
  31. /// // The graph is now updated and the game can just continue
  32. /// } else {
  33. /// // Invalid tower position. It blocks the path between the spawn point and the goal
  34. /// // The effect on the graph has been reverted
  35. /// Destroy(tower);
  36. /// }
  37. /// </code>
  38. /// </summary>
  39. /// <param name="guo">The GraphUpdateObject to update the graphs with</param>
  40. /// <param name="node1">Node which should have a valid path to node2. All nodes should be walkable or false will be returned.</param>
  41. /// <param name="node2">Node which should have a valid path to node1. All nodes should be walkable or false will be returned.</param>
  42. /// <param name="alwaysRevert">If true, reverts the graphs to the old state even if no blocking occurred</param>
  43. public static bool UpdateGraphsNoBlock (GraphUpdateObject guo, GraphNode node1, GraphNode node2, bool alwaysRevert = false) {
  44. List<GraphNode> buffer = ListPool<GraphNode>.Claim();
  45. buffer.Add(node1);
  46. buffer.Add(node2);
  47. bool worked = UpdateGraphsNoBlock(guo, buffer, alwaysRevert);
  48. ListPool<GraphNode>.Release(ref buffer);
  49. return worked;
  50. }
  51. /// <summary>
  52. /// Updates graphs and checks if all nodes are still reachable from each other.
  53. /// Graphs are updated, then a check is made to see if the nodes are still reachable from each other.
  54. /// If they are not, the graphs are reverted to before the update and false is returned.
  55. /// This is slower than a normal graph update.
  56. /// All queued graph updates will be flushed during this function.
  57. ///
  58. /// Returns: True if the given nodes are still reachable from each other after the guo has been applied. False otherwise.
  59. /// </summary>
  60. /// <param name="guo">The GraphUpdateObject to update the graphs with</param>
  61. /// <param name="nodes">Nodes which should have valid paths between them. All nodes should be walkable or false will be returned.</param>
  62. /// <param name="alwaysRevert">If true, reverts the graphs to the old state even if no blocking occurred</param>
  63. public static bool UpdateGraphsNoBlock (GraphUpdateObject guo, List<GraphNode> nodes, bool alwaysRevert = false) {
  64. bool worked;
  65. // Pause pathfinding while modifying the graphs
  66. var graphLock = AstarPath.active.PausePathfinding();
  67. try {
  68. // Make sure any pending graph updates have been done before we start
  69. AstarPath.active.FlushGraphUpdates();
  70. // Make sure all nodes are walkable
  71. for (int i = 0; i < nodes.Count; i++) if (!nodes[i].Walkable) return false;
  72. // Track changed nodes to enable reversion of the guo
  73. guo.trackChangedNodes = true;
  74. AstarPath.active.UpdateGraphs(guo);
  75. // Update the graphs immediately
  76. AstarPath.active.FlushGraphUpdates();
  77. // Check if all nodes are in the same area and that they are walkable, i.e that there are paths between all of them
  78. worked = PathUtilities.IsPathPossible(nodes);
  79. // If it did not work, revert the GUO
  80. if (!worked || alwaysRevert) {
  81. guo.RevertFromBackup();
  82. // Recalculate connected components
  83. AstarPath.active.hierarchicalGraph.RecalculateIfNecessary();
  84. }
  85. } finally {
  86. graphLock.Release();
  87. }
  88. // Disable tracking nodes, not strictly necessary, but will slightly reduce the cance that some user causes errors
  89. guo.trackChangedNodes = false;
  90. return worked;
  91. }
  92. }
  93. }