| | | 1 | | using FixedMathSharp; |
| | | 2 | | using GridForge.Grids; |
| | | 3 | | using System; |
| | | 4 | | using System.Runtime.CompilerServices; |
| | | 5 | | |
| | | 6 | | namespace Trailblazer.Pathing; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// A pathfinding request used for A* trail generation, including options for climb height, heuristic weighting, |
| | | 10 | | /// and path smoothing. Implements value-based comparison and hashing for guide pooling. |
| | | 11 | | /// </summary> |
| | | 12 | | public class AStarPathRequest : PathRequest, IEquatable<AStarPathRequest> |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// The maximum Y-axis height delta a unit can step or climb per voxel. |
| | | 16 | | /// Voxels exceeding this are ignored even if walkable and adjacent. |
| | | 17 | | /// </summary> |
| | | 18 | | public Fixed64 MaxClimbHeight { get; set; } |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Gets or sets the heuristic method used for evaluating or guiding the algorithm. |
| | | 22 | | /// </summary> |
| | | 23 | | /// <remarks> |
| | | 24 | | /// Set this property to specify which heuristic strategy the algorithm should use. |
| | | 25 | | /// The selected heuristic can affect the performance and outcome of the algorithm. |
| | | 26 | | /// </remarks> |
| | | 27 | | public HeuristicMethod Heuristic { get; set; } |
| | | 28 | | |
| | | 29 | | // Prevent external use of the default constructor to ensure proper initialization through factory methods. |
| | 1580 | 30 | | private AStarPathRequest() { } |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Attempts to create a new context-bound A* pathfinding request. |
| | | 34 | | /// </summary> |
| | | 35 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 36 | | public static bool TryCreate( |
| | | 37 | | TrailblazerWorldContext context, |
| | | 38 | | Vector3d origin, |
| | | 39 | | Vector3d destination, |
| | | 40 | | Fixed64 unitSize, |
| | | 41 | | out AStarPathRequest? request) |
| | | 42 | | { |
| | 61 | 43 | | request = Create(context, origin, destination, unitSize); |
| | 61 | 44 | | if (request == null) |
| | 2 | 45 | | return false; |
| | 59 | 46 | | return true; |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Creates a context-bound A* pathfinding request. |
| | | 51 | | /// </summary> |
| | | 52 | | public static AStarPathRequest? Create( |
| | | 53 | | TrailblazerWorldContext context, |
| | | 54 | | Vector3d origin, |
| | | 55 | | Vector3d destination, |
| | | 56 | | Fixed64 unitSize, |
| | | 57 | | HeuristicMethod heuristic = HeuristicMethod.Manhattan, |
| | | 58 | | bool allowUnwalkableEndpoints = false, |
| | | 59 | | bool allowTraversalTransitions = false) |
| | | 60 | | { |
| | 803 | 61 | | PathRequestContextResolver.ThrowIfUnusable(context); |
| | 803 | 62 | | if (!SolidVoxelFinder.TryGetPathEdgeVoxels( |
| | 803 | 63 | | context, |
| | 803 | 64 | | origin, |
| | 803 | 65 | | destination, |
| | 803 | 66 | | out Voxel? startNode, |
| | 803 | 67 | | out Voxel? endNode, |
| | 803 | 68 | | unitSize, |
| | 803 | 69 | | allowUnwalkableEndpoints)) |
| | | 70 | | { |
| | 13 | 71 | | return null; |
| | | 72 | | } |
| | | 73 | | |
| | 790 | 74 | | if (startNode == null || endNode == null) |
| | 0 | 75 | | return null; |
| | | 76 | | |
| | 790 | 77 | | AStarPathRequest request = new() |
| | 790 | 78 | | { |
| | 790 | 79 | | Context = context, |
| | 790 | 80 | | Origin = origin, |
| | 790 | 81 | | StartNode = startNode, |
| | 790 | 82 | | TargetPosition = destination, |
| | 790 | 83 | | EndNode = endNode, |
| | 790 | 84 | | UnitSize = unitSize, |
| | 790 | 85 | | Heuristic = heuristic, |
| | 790 | 86 | | AllowUnwalkableEndpoints = allowUnwalkableEndpoints, |
| | 790 | 87 | | AllowTraversalTransitions = allowTraversalTransitions, |
| | 790 | 88 | | MaxClimbHeight = context.VoxelSize |
| | 790 | 89 | | }; |
| | | 90 | | |
| | 790 | 91 | | if (context.Pathing.TryGetMaxSearchSize(request.StartNode, request.EndNode, out int searchSize)) |
| | 790 | 92 | | request.MaxPathSearchRange = searchSize; |
| | | 93 | | |
| | 790 | 94 | | return request; |
| | | 95 | | } |
| | | 96 | | |
| | | 97 | | /// <inheritdoc/> |
| | | 98 | | public override bool Equals(object? obj) => |
| | 3 | 99 | | obj is AStarPathRequest other && Equals(other); |
| | | 100 | | |
| | | 101 | | /// <inheritdoc/> |
| | 4 | 102 | | public bool Equals(AStarPathRequest? other) => RequestCacheKey == other?.RequestCacheKey; |
| | | 103 | | |
| | | 104 | | /// <inheritdoc/> |
| | | 105 | | public override int GetHashCode() |
| | | 106 | | { |
| | 2885 | 107 | | PathRequestHashBuilder hash = PathRequestHashBuilder.Create(); |
| | 2885 | 108 | | hash.Add(StartNode?.SpawnToken ?? 0); |
| | 2885 | 109 | | hash.Add(EndNode?.SpawnToken ?? 0); |
| | 2885 | 110 | | hash.Add(UnitSize.GetHashCode()); |
| | 2885 | 111 | | hash.Add(AllowUnwalkableEndpoints); |
| | 2885 | 112 | | hash.Add(AllowTraversalTransitions); |
| | 2885 | 113 | | hash.Add((int)Heuristic); |
| | 2885 | 114 | | hash.Add(MaxClimbHeight.GetHashCode()); |
| | 2885 | 115 | | hash.Add(MaxPathSearchRange); |
| | 2885 | 116 | | hash.Add(AllowTraversalTransitions ? Context.Pathing.State.TransitionRegistryState.RegistryVersion : 0); |
| | 2885 | 117 | | return hash.ToHashCode(); |
| | | 118 | | } |
| | | 119 | | } |