1
0

PathfindingSystem.BFS.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using Robust.Shared.Map;
  2. using Robust.Shared.Random;
  3. using Robust.Shared.Utility;
  4. namespace Content.Server.NPC.Pathfinding;
  5. public sealed partial class PathfindingSystem
  6. {
  7. private PathResult UpdateBFSPath(IRobustRandom random, BFSPathRequest request)
  8. {
  9. if (request.Task.IsCanceled)
  10. {
  11. return PathResult.NoPath;
  12. }
  13. // TODO: Need partial planning that uses best node.
  14. PathPoly? currentNode = null;
  15. // First run
  16. if (!request.Started)
  17. {
  18. request.Frontier = new PriorityQueue<(float, PathPoly)>(PathPolyComparer);
  19. request.Started = true;
  20. }
  21. // Re-validate nodes
  22. else
  23. {
  24. // Theoretically this shouldn't be happening, but practically...
  25. if (request.Frontier.Count == 0)
  26. {
  27. return PathResult.NoPath;
  28. }
  29. (_, currentNode) = request.Frontier.Peek();
  30. if (!currentNode.IsValid())
  31. {
  32. return PathResult.NoPath;
  33. }
  34. // Re-validate parents too.
  35. if (request.CameFrom.TryGetValue(currentNode, out var parentNode) && !parentNode.IsValid())
  36. {
  37. return PathResult.NoPath;
  38. }
  39. }
  40. DebugTools.Assert(!request.Task.IsCompleted);
  41. request.Stopwatch.Restart();
  42. var startNode = GetPoly(request.Start);
  43. if (startNode == null)
  44. {
  45. return PathResult.NoPath;
  46. }
  47. request.Frontier.Add((0.0f, startNode));
  48. request.CostSoFar[startNode] = 0.0f;
  49. var count = 0;
  50. while (request.Frontier.Count > 0 && count < NodeLimit && count < request.ExpansionLimit)
  51. {
  52. // Handle whether we need to pause if we've taken too long
  53. if (count % 20 == 0 && count > 0 && request.Stopwatch.Elapsed > PathTime)
  54. {
  55. // I had this happen once in testing but I don't think it should be possible?
  56. DebugTools.Assert(request.Frontier.Count > 0);
  57. return PathResult.Continuing;
  58. }
  59. count++;
  60. // Actual pathfinding here
  61. (_, currentNode) = request.Frontier.Take();
  62. foreach (var neighbor in currentNode.Neighbors)
  63. {
  64. var tileCost = GetTileCost(request, currentNode, neighbor);
  65. if (tileCost.Equals(0f))
  66. {
  67. continue;
  68. }
  69. // f = g + h
  70. // gScore is distance to the start node
  71. // hScore is distance to the end node
  72. var gScore = request.CostSoFar[currentNode] + tileCost;
  73. if (request.CostSoFar.TryGetValue(neighbor, out var nextValue) && gScore >= nextValue)
  74. {
  75. continue;
  76. }
  77. request.CameFrom[neighbor] = currentNode;
  78. request.CostSoFar[neighbor] = gScore;
  79. request.Frontier.Add((gScore, neighbor));
  80. }
  81. }
  82. if (request.CostSoFar.Count == 0)
  83. {
  84. return PathResult.NoPath;
  85. }
  86. // Pick a random node to use?
  87. (currentNode, _) = random.Pick(request.CostSoFar);
  88. var route = ReconstructPath(request.CameFrom, currentNode);
  89. var path = new Queue<EntityCoordinates>(route.Count);
  90. foreach (var node in route)
  91. {
  92. // Due to partial planning some nodes may have been invalidated.
  93. if (!node.IsValid())
  94. {
  95. return PathResult.NoPath;
  96. }
  97. path.Enqueue(node.Coordinates);
  98. }
  99. DebugTools.Assert(route.Count > 0);
  100. request.Polys = route;
  101. return PathResult.Path;
  102. }
  103. }