PathRequest.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System.Threading;
  2. using System.Threading.Tasks;
  3. using Content.Shared.NPC;
  4. using Robust.Shared.Map;
  5. using Robust.Shared.Timing;
  6. using Robust.Shared.Utility;
  7. namespace Content.Server.NPC.Pathfinding;
  8. /// <summary>
  9. /// Stores the in-progress data of a pathfinding request.
  10. /// </summary>
  11. public abstract class PathRequest
  12. {
  13. public EntityCoordinates Start;
  14. public Task<PathResult> Task => Tcs.Task;
  15. public readonly TaskCompletionSource<PathResult> Tcs;
  16. public List<PathPoly> Polys = new();
  17. public bool Started = false;
  18. #region Pathfinding state
  19. public readonly Stopwatch Stopwatch = new();
  20. public PriorityQueue<ValueTuple<float, PathPoly>> Frontier = default!;
  21. public readonly Dictionary<PathPoly, float> CostSoFar = new();
  22. public readonly Dictionary<PathPoly, PathPoly> CameFrom = new();
  23. #endregion
  24. #region Data
  25. public readonly PathFlags Flags;
  26. public readonly int CollisionLayer;
  27. public readonly int CollisionMask;
  28. #endregion
  29. public PathRequest(EntityCoordinates start, PathFlags flags, int layer, int mask, CancellationToken cancelToken)
  30. {
  31. Start = start;
  32. Flags = flags;
  33. CollisionLayer = layer;
  34. CollisionMask = mask;
  35. Tcs = new TaskCompletionSource<PathResult>(cancelToken);
  36. }
  37. }
  38. public sealed class AStarPathRequest : PathRequest
  39. {
  40. public EntityCoordinates End;
  41. /// <summary>
  42. /// How close we need to be to the end node to be considered as arrived.
  43. /// </summary>
  44. public float Distance;
  45. public AStarPathRequest(
  46. EntityCoordinates start,
  47. EntityCoordinates end,
  48. PathFlags flags,
  49. float distance,
  50. int layer,
  51. int mask,
  52. CancellationToken cancelToken) : base(start, flags, layer, mask, cancelToken)
  53. {
  54. Distance = distance;
  55. End = end;
  56. }
  57. }
  58. public sealed class BFSPathRequest : PathRequest
  59. {
  60. /// <summary>
  61. /// How far away we're allowed to expand in distance.
  62. /// </summary>
  63. public float ExpansionRange;
  64. /// <summary>
  65. /// How many nodes we're allowed to expand
  66. /// </summary>
  67. public int ExpansionLimit;
  68. public BFSPathRequest(
  69. float expansionRange,
  70. int expansionLimit,
  71. EntityCoordinates start,
  72. PathFlags flags,
  73. int layer,
  74. int mask,
  75. CancellationToken cancelToken) : base(start, flags, layer, mask, cancelToken)
  76. {
  77. ExpansionRange = expansionRange;
  78. ExpansionLimit = expansionLimit;
  79. }
  80. }
  81. /// <summary>
  82. /// Stores the final result of a pathfinding request
  83. /// </summary>
  84. public sealed class PathResultEvent
  85. {
  86. public PathResult Result;
  87. public readonly List<PathPoly> Path;
  88. public PathResultEvent(PathResult result, List<PathPoly> path)
  89. {
  90. Result = result;
  91. Path = path;
  92. }
  93. }