| | | 1 | | using FixedMathSharp; |
| | | 2 | | using GridForge.Grids; |
| | | 3 | | using System; |
| | | 4 | | using System.Diagnostics.CodeAnalysis; |
| | | 5 | | using System.Runtime.CompilerServices; |
| | | 6 | | |
| | | 7 | | namespace Trailblazer.Pathing; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Internal adapter request used to build staged transition-aware routes from normal chart-backed request intent. |
| | | 11 | | /// </summary> |
| | | 12 | | /// <remarks> |
| | | 13 | | /// The current implementation supports: |
| | | 14 | | /// chart -> chart direct paths, |
| | | 15 | | /// chart -> transition -> chart, |
| | | 16 | | /// chart -> transition -> volume -> transition -> chart. |
| | | 17 | | /// </remarks> |
| | | 18 | | internal sealed class HybridPathRequest : IPathRequest, IEquatable<HybridPathRequest> |
| | | 19 | | { |
| | | 20 | | #region Properties |
| | | 21 | | |
| | | 22 | | /// <inheritdoc/> |
| | | 23 | | public TrailblazerWorldContext Context { get; private set; } = null!; |
| | | 24 | | |
| | | 25 | | /// <inheritdoc/> |
| | | 26 | | public Vector3d Origin { get; private set; } |
| | | 27 | | |
| | | 28 | | /// <inheritdoc/> |
| | | 29 | | public Voxel? StartNode { get; private set; } |
| | | 30 | | |
| | | 31 | | /// <inheritdoc/> |
| | | 32 | | public Vector3d TargetPosition { get; private set; } |
| | | 33 | | |
| | | 34 | | /// <inheritdoc/> |
| | | 35 | | public Voxel? EndNode { get; private set; } |
| | | 36 | | |
| | | 37 | | /// <inheritdoc/> |
| | | 38 | | public Fixed64 UnitSize { get; private set; } |
| | | 39 | | |
| | | 40 | | /// <inheritdoc/> |
| | | 41 | | public bool AllowUnwalkableEndpoints { get; set; } |
| | | 42 | | |
| | | 43 | | /// <inheritdoc/> |
| | | 44 | | public int MaxPathSearchRange { get; set; } |
| | | 45 | | |
| | | 46 | | public HeuristicMethod Heuristic { get; set; } |
| | | 47 | | |
| | | 48 | | public Fixed64 MaxClimbHeight { get; set; } |
| | | 49 | | |
| | | 50 | | public int ExtraFloodRange { get; set; } |
| | | 51 | | |
| | | 52 | | /// <inheritdoc/> |
| | 215 | 53 | | public bool HasOrigin => StartNode != null; |
| | | 54 | | |
| | | 55 | | /// <inheritdoc/> |
| | 214 | 56 | | public bool HasDestination => EndNode != null; |
| | | 57 | | |
| | | 58 | | /// <inheritdoc/> |
| | 215 | 59 | | public bool HasValidEndpoints => HasOrigin && HasDestination; |
| | | 60 | | |
| | | 61 | | /// <inheritdoc/> |
| | 10 | 62 | | public bool IsValid => HasValidEndpoints && MaxPathSearchRange > 0 && RoutePlan != null; |
| | | 63 | | |
| | | 64 | | /// <inheritdoc/> |
| | | 65 | | public bool HasZeroDisplacement => |
| | 3 | 66 | | !IsValid |
| | 3 | 67 | | || (StartNode == EndNode |
| | 3 | 68 | | && (RoutePlan?.DirectedTransitions.Length ?? 0) == 0); |
| | | 69 | | |
| | | 70 | | /// <inheritdoc/> |
| | 4 | 71 | | public int RequestCacheKey => GetHashCode(); |
| | | 72 | | |
| | | 73 | | internal HybridRoutePlan? RoutePlan { get; private set; } |
| | | 74 | | |
| | | 75 | | internal HybridChartRequestKind ChartRequestKind { get; private set; } |
| | | 76 | | |
| | | 77 | | #endregion |
| | | 78 | | |
| | | 79 | | #region Construction and Initialization |
| | | 80 | | |
| | | 81 | | /// <summary> |
| | | 82 | | /// Private constructor to enforce the use of factory methods for creating instances of HybridPathRequest. |
| | | 83 | | /// </summary> |
| | 196 | 84 | | private HybridPathRequest() { } |
| | | 85 | | |
| | | 86 | | /// <summary> |
| | | 87 | | /// Attempts to create a new context-bound HybridPathRequest with the specified parameters. |
| | | 88 | | /// </summary> |
| | | 89 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 90 | | public static bool TryCreate( |
| | | 91 | | TrailblazerWorldContext context, |
| | | 92 | | Vector3d origin, |
| | | 93 | | Vector3d destination, |
| | | 94 | | Fixed64 unitSize, |
| | | 95 | | [NotNullWhen(true)] out HybridPathRequest? request, |
| | | 96 | | HeuristicMethod heuristic = HeuristicMethod.Manhattan, |
| | | 97 | | Fixed64? maxClimbHeight = null, |
| | | 98 | | bool allowUnwalkableEndpoints = false) |
| | | 99 | | { |
| | 13 | 100 | | request = Create(context, origin, destination, unitSize, heuristic, maxClimbHeight, allowUnwalkableEndpoints); |
| | 13 | 101 | | return request != null; |
| | | 102 | | } |
| | | 103 | | |
| | | 104 | | /// <summary> |
| | | 105 | | /// Creates a new context-bound HybridPathRequest with the specified parameters. |
| | | 106 | | /// </summary> |
| | | 107 | | public static HybridPathRequest? Create( |
| | | 108 | | TrailblazerWorldContext context, |
| | | 109 | | Vector3d origin, |
| | | 110 | | Vector3d destination, |
| | | 111 | | Fixed64 unitSize, |
| | | 112 | | HeuristicMethod heuristic = HeuristicMethod.Manhattan, |
| | | 113 | | Fixed64? maxClimbHeight = null, |
| | | 114 | | bool allowUnwalkableEndpoints = false) |
| | | 115 | | { |
| | 21 | 116 | | PathRequestContextResolver.ThrowIfUnusable(context); |
| | 21 | 117 | | if (!SolidVoxelFinder.TryGetPathEdgeVoxels( |
| | 21 | 118 | | context, |
| | 21 | 119 | | origin, |
| | 21 | 120 | | destination, |
| | 21 | 121 | | out Voxel? startNode, |
| | 21 | 122 | | out Voxel? endNode, |
| | 21 | 123 | | unitSize, |
| | 21 | 124 | | allowUnwalkableEndpoints)) |
| | | 125 | | { |
| | 2 | 126 | | return null; |
| | | 127 | | } |
| | | 128 | | |
| | 19 | 129 | | if (startNode == null || endNode == null) |
| | 0 | 130 | | return null; |
| | | 131 | | |
| | 19 | 132 | | var request = new HybridPathRequest |
| | 19 | 133 | | { |
| | 19 | 134 | | Context = context, |
| | 19 | 135 | | Origin = origin, |
| | 19 | 136 | | StartNode = startNode, |
| | 19 | 137 | | TargetPosition = destination, |
| | 19 | 138 | | EndNode = endNode, |
| | 19 | 139 | | UnitSize = unitSize, |
| | 19 | 140 | | ChartRequestKind = HybridChartRequestKind.AStar, |
| | 19 | 141 | | Heuristic = heuristic, |
| | 19 | 142 | | AllowUnwalkableEndpoints = allowUnwalkableEndpoints, |
| | 19 | 143 | | MaxClimbHeight = maxClimbHeight ?? context.VoxelSize |
| | 19 | 144 | | }; |
| | | 145 | | |
| | 19 | 146 | | if (!request.RebuildPlan()) |
| | 1 | 147 | | return null; |
| | | 148 | | |
| | 18 | 149 | | return request; |
| | | 150 | | } |
| | | 151 | | |
| | | 152 | | /// <summary> |
| | | 153 | | /// Creates a new HybridPathRequest based on an existing AStarPathRequest. |
| | | 154 | | /// This factory method is used to convert a standard A* path request into a hybrid request that can be processed by |
| | | 155 | | /// The method checks the validity of the input request and attempts to build a corresponding route plan for the hy |
| | | 156 | | /// </summary> |
| | | 157 | | /// <param name="request">The AStarPathRequest to convert into a HybridPathRequest.</param> |
| | | 158 | | /// <returns>The created HybridPathRequest if successful; otherwise, null.</returns> |
| | | 159 | | internal static HybridPathRequest? CreateFromAStar(AStarPathRequest request) |
| | | 160 | | { |
| | 61 | 161 | | if (request == null || !request.HasValidEndpoints) |
| | 1 | 162 | | return null; |
| | | 163 | | |
| | 60 | 164 | | var hybridRequest = new HybridPathRequest |
| | 60 | 165 | | { |
| | 60 | 166 | | Context = request.Context, |
| | 60 | 167 | | Origin = request.Origin, |
| | 60 | 168 | | StartNode = request.StartNode, |
| | 60 | 169 | | TargetPosition = request.TargetPosition, |
| | 60 | 170 | | EndNode = request.EndNode, |
| | 60 | 171 | | UnitSize = request.UnitSize, |
| | 60 | 172 | | ChartRequestKind = HybridChartRequestKind.AStar, |
| | 60 | 173 | | Heuristic = request.Heuristic, |
| | 60 | 174 | | AllowUnwalkableEndpoints = request.AllowUnwalkableEndpoints, |
| | 60 | 175 | | MaxClimbHeight = request.MaxClimbHeight |
| | 60 | 176 | | }; |
| | | 177 | | |
| | 60 | 178 | | return hybridRequest.RebuildPlan() ? hybridRequest : null; |
| | | 179 | | } |
| | | 180 | | |
| | | 181 | | /// <summary> |
| | | 182 | | /// Creates a new HybridPathRequest based on an existing FlowFieldPathRequest. |
| | | 183 | | /// This factory method is used to convert a standard flow field path request into a hybrid request that can be proc |
| | | 184 | | /// The method checks the validity of the input request and attempts to build a corresponding route plan for the hyb |
| | | 185 | | /// </summary> |
| | | 186 | | /// <param name="request">The FlowFieldPathRequest to convert into a HybridPathRequest.</param> |
| | | 187 | | /// <returns>The created HybridPathRequest if successful; otherwise, null.</returns> |
| | | 188 | | internal static HybridPathRequest? CreateFromFlowField(FlowFieldPathRequest request) |
| | | 189 | | { |
| | 20 | 190 | | if (request == null || !request.HasValidEndpoints) |
| | 1 | 191 | | return null; |
| | | 192 | | |
| | 19 | 193 | | var hybridRequest = new HybridPathRequest |
| | 19 | 194 | | { |
| | 19 | 195 | | Context = request.Context, |
| | 19 | 196 | | Origin = request.Origin, |
| | 19 | 197 | | StartNode = request.StartNode, |
| | 19 | 198 | | TargetPosition = request.TargetPosition, |
| | 19 | 199 | | EndNode = request.EndNode, |
| | 19 | 200 | | UnitSize = request.UnitSize, |
| | 19 | 201 | | ChartRequestKind = HybridChartRequestKind.FlowField, |
| | 19 | 202 | | AllowUnwalkableEndpoints = request.AllowUnwalkableEndpoints, |
| | 19 | 203 | | MaxClimbHeight = request.MaxClimbHeight, |
| | 19 | 204 | | ExtraFloodRange = request.ExtraFloodRange |
| | 19 | 205 | | }; |
| | | 206 | | |
| | 19 | 207 | | return hybridRequest.RebuildPlan() ? hybridRequest : null; |
| | | 208 | | } |
| | | 209 | | |
| | | 210 | | #endregion |
| | | 211 | | |
| | | 212 | | /// <inheritdoc/> |
| | | 213 | | public bool UpdateRequest( |
| | | 214 | | Vector3d origin, |
| | | 215 | | Vector3d destination, |
| | | 216 | | Fixed64? unitSize) |
| | | 217 | | { |
| | 2 | 218 | | Fixed64 resolvedUnitSize = unitSize ?? Context.VoxelSize; |
| | 2 | 219 | | bool success = SolidVoxelFinder.TryGetPathEdgeVoxels( |
| | 2 | 220 | | Context, |
| | 2 | 221 | | origin, |
| | 2 | 222 | | destination, |
| | 2 | 223 | | out Voxel? startVoxel, |
| | 2 | 224 | | out Voxel? endVoxel, |
| | 2 | 225 | | resolvedUnitSize, |
| | 2 | 226 | | AllowUnwalkableEndpoints); |
| | | 227 | | |
| | 2 | 228 | | Origin = origin; |
| | 2 | 229 | | TargetPosition = destination; |
| | 2 | 230 | | StartNode = startVoxel; |
| | 2 | 231 | | EndNode = endVoxel; |
| | 2 | 232 | | UnitSize = resolvedUnitSize; |
| | | 233 | | |
| | 2 | 234 | | if (!success || startVoxel == null || endVoxel == null) |
| | | 235 | | { |
| | 1 | 236 | | RoutePlan = null; |
| | 1 | 237 | | MaxPathSearchRange = 0; |
| | 1 | 238 | | return false; |
| | | 239 | | } |
| | | 240 | | |
| | 1 | 241 | | return RebuildPlan(); |
| | | 242 | | } |
| | | 243 | | |
| | | 244 | | /// <inheritdoc/> |
| | | 245 | | public bool TrySetOrigin(Vector3d origin, bool resetSearchRange = false) |
| | | 246 | | { |
| | 3 | 247 | | if (EndNode == null) |
| | 1 | 248 | | return false; |
| | | 249 | | |
| | 2 | 250 | | if (!SolidVoxelFinder.GetStartVoxel( |
| | 2 | 251 | | Context, |
| | 2 | 252 | | origin, |
| | 2 | 253 | | TargetPosition, |
| | 2 | 254 | | out Voxel? startNode, |
| | 2 | 255 | | AllowUnwalkableEndpoints, |
| | 2 | 256 | | UnitSize)) |
| | | 257 | | { |
| | 1 | 258 | | return false; |
| | | 259 | | } |
| | | 260 | | |
| | 1 | 261 | | if (startNode == null) |
| | 0 | 262 | | return false; |
| | | 263 | | |
| | 1 | 264 | | Origin = origin; |
| | 1 | 265 | | StartNode = startNode; |
| | 1 | 266 | | return RebuildPlan(); |
| | | 267 | | } |
| | | 268 | | |
| | | 269 | | /// <inheritdoc/> |
| | | 270 | | public bool TrySetDestination(Vector3d destination, bool resetSearchRange = false) |
| | | 271 | | { |
| | 2 | 272 | | if (StartNode == null) |
| | 1 | 273 | | return false; |
| | | 274 | | |
| | 1 | 275 | | if (!SolidVoxelFinder.GetEndVoxel( |
| | 1 | 276 | | Context, |
| | 1 | 277 | | Origin, |
| | 1 | 278 | | destination, |
| | 1 | 279 | | out Voxel? endNode, |
| | 1 | 280 | | AllowUnwalkableEndpoints, |
| | 1 | 281 | | UnitSize)) |
| | | 282 | | { |
| | 0 | 283 | | return false; |
| | | 284 | | } |
| | | 285 | | |
| | 1 | 286 | | if (endNode == null) |
| | 0 | 287 | | return false; |
| | | 288 | | |
| | 1 | 289 | | TargetPosition = destination; |
| | 1 | 290 | | EndNode = endNode; |
| | 1 | 291 | | return RebuildPlan(); |
| | | 292 | | } |
| | | 293 | | |
| | | 294 | | /// <inheritdoc/> |
| | | 295 | | public bool TrySetUnitSize(Fixed64 unitSize) |
| | | 296 | | { |
| | 2 | 297 | | if (UnitSize == unitSize) |
| | 1 | 298 | | return false; |
| | | 299 | | |
| | 1 | 300 | | return UpdateRequest(Origin, TargetPosition, unitSize); |
| | | 301 | | } |
| | | 302 | | |
| | | 303 | | /// <inheritdoc/> |
| | | 304 | | public override bool Equals(object? obj) => |
| | 2 | 305 | | obj is HybridPathRequest other && Equals(other); |
| | | 306 | | |
| | | 307 | | /// <inheritdoc/> |
| | | 308 | | public bool Equals(HybridPathRequest? other) => |
| | 3 | 309 | | other != null |
| | 3 | 310 | | && RequestCacheKey == other.RequestCacheKey; |
| | | 311 | | |
| | | 312 | | /// <summary> |
| | | 313 | | /// Generates a hash code for the current path request based on its properties and route plan. |
| | | 314 | | /// This hash code is used for caching and guide pooling, allowing for efficient retrieval of guides based on reques |
| | | 315 | | /// </summary> |
| | | 316 | | /// <returns>A hash code representing the current path request.</returns> |
| | | 317 | | public override int GetHashCode() |
| | | 318 | | { |
| | 6 | 319 | | PathRequestHashBuilder transitionHash = PathRequestHashBuilder.Create(); |
| | 6 | 320 | | if (RoutePlan != null) |
| | | 321 | | { |
| | 20 | 322 | | for (int i = 0; i < RoutePlan.DirectedTransitions.Length; i++) |
| | 4 | 323 | | transitionHash.AddOrdinalString(RoutePlan.DirectedTransitions[i].Id); |
| | | 324 | | } |
| | | 325 | | |
| | 6 | 326 | | PathRequestHashBuilder hash = PathRequestHashBuilder.Create(); |
| | 6 | 327 | | hash.Add(StartNode?.SpawnToken ?? 0); |
| | 6 | 328 | | hash.Add(EndNode?.SpawnToken ?? 0); |
| | 6 | 329 | | hash.Add(UnitSize.GetHashCode()); |
| | 6 | 330 | | hash.Add((int)ChartRequestKind); |
| | 6 | 331 | | hash.Add(AllowUnwalkableEndpoints); |
| | 6 | 332 | | hash.Add((int)Heuristic); |
| | 6 | 333 | | hash.Add(MaxClimbHeight.GetHashCode()); |
| | 6 | 334 | | hash.Add(ExtraFloodRange); |
| | 6 | 335 | | hash.Add(MaxPathSearchRange); |
| | 6 | 336 | | hash.Add(transitionHash.ToHashCode()); |
| | 6 | 337 | | return hash.ToHashCode(); |
| | | 338 | | } |
| | | 339 | | |
| | | 340 | | /// <summary> |
| | | 341 | | /// Rebuilds the route plan for the current request using the HybridRoutePlanner. |
| | | 342 | | /// This method is called whenever the request parameters are updated (e.g. origin, destination, unit size) to ensur |
| | | 343 | | /// </summary> |
| | | 344 | | /// <returns>True if the route plan was successfully rebuilt; otherwise, false.</returns> |
| | | 345 | | internal bool RebuildPlan() |
| | | 346 | | { |
| | 102 | 347 | | RoutePlan = null; |
| | 102 | 348 | | MaxPathSearchRange = 0; |
| | | 349 | | |
| | 102 | 350 | | if (!HasValidEndpoints) |
| | 0 | 351 | | return false; |
| | | 352 | | |
| | | 353 | | HybridRoutePlan? plan; |
| | 102 | 354 | | using (PathManager.EnterState(Context.Pathing.State)) |
| | | 355 | | { |
| | 102 | 356 | | if (!HybridRoutePlanner.TryPlan(this, out plan) |
| | 102 | 357 | | || plan == null) |
| | 5 | 358 | | return false; |
| | 97 | 359 | | } |
| | | 360 | | |
| | 97 | 361 | | RoutePlan = plan; |
| | | 362 | | |
| | 97 | 363 | | int totalSearchRange = 0; |
| | 804 | 364 | | for (int i = 0; i < plan.Steps.Length; i++) |
| | | 365 | | { |
| | 305 | 366 | | if (plan.Steps[i].Kind == HybridRouteStepKind.PathSegment) |
| | 69 | 367 | | totalSearchRange += plan.Steps[i].SegmentRequest.MaxPathSearchRange; |
| | | 368 | | } |
| | | 369 | | |
| | 97 | 370 | | MaxPathSearchRange = totalSearchRange > 0 ? totalSearchRange : 1; |
| | 97 | 371 | | return true; |
| | 5 | 372 | | } |
| | | 373 | | } |