| | | 1 | | using FixedMathSharp; |
| | | 2 | | using GridForge.Grids; |
| | | 3 | | using System; |
| | | 4 | | |
| | | 5 | | namespace Trailblazer.Pathing; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Represents an abstract base class for a pathfinding request, encapsulating the parameters and |
| | | 9 | | /// state required to compute a path between two points in a voxel-based environment. |
| | | 10 | | /// </summary> |
| | | 11 | | /// <remarks> |
| | | 12 | | /// PathRequest provides a common interface and shared logic for specifying origins, destinations, and |
| | | 13 | | /// traversal options for pathfinding operations. |
| | | 14 | | /// Derived classes should implement additional behavior as needed for specific pathfinding scenarios. |
| | | 15 | | /// Thread safety is not guaranteed; synchronize access if used concurrently. |
| | | 16 | | /// </remarks> |
| | | 17 | | public abstract class PathRequest : IPathRequest |
| | | 18 | | { |
| | | 19 | | private TrailblazerWorldContext? _context; |
| | | 20 | | |
| | | 21 | | /// <inheritdoc/> |
| | | 22 | | public TrailblazerWorldContext Context |
| | | 23 | | { |
| | 4081 | 24 | | get => _context ?? throw new InvalidOperationException("Path request is not bound to a TrailblazerWorldContext." |
| | 1084 | 25 | | protected set => _context = value; |
| | | 26 | | } |
| | | 27 | | |
| | | 28 | | /// <inheritdoc/> |
| | | 29 | | public Vector3d Origin { get; protected set; } |
| | | 30 | | |
| | | 31 | | /// <inheritdoc/> |
| | | 32 | | public Voxel? StartNode { get; protected set; } |
| | | 33 | | |
| | | 34 | | /// <inheritdoc/> |
| | | 35 | | public Vector3d TargetPosition { get; protected set; } |
| | | 36 | | |
| | | 37 | | /// <inheritdoc/> |
| | | 38 | | public Voxel? EndNode { get; protected set; } |
| | | 39 | | |
| | | 40 | | /// <inheritdoc/> |
| | | 41 | | public Fixed64 UnitSize { get; protected set; } |
| | | 42 | | |
| | | 43 | | /// <inheritdoc/> |
| | | 44 | | public bool AllowUnwalkableEndpoints { get; set; } |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Whether chart-backed requests may fall back through authored traversal transitions when direct chart routing fai |
| | | 48 | | /// </summary> |
| | | 49 | | public bool AllowTraversalTransitions { get; set; } |
| | | 50 | | |
| | | 51 | | /// <inheritdoc/> |
| | | 52 | | public int MaxPathSearchRange { get; set; } |
| | | 53 | | |
| | | 54 | | /// <inheritdoc/> |
| | 2989 | 55 | | public bool HasOrigin => StartNode != null; |
| | | 56 | | |
| | | 57 | | /// <inheritdoc/> |
| | 2985 | 58 | | public bool HasDestination => EndNode != null; |
| | | 59 | | |
| | | 60 | | /// <inheritdoc/> |
| | 2989 | 61 | | public bool HasValidEndpoints => HasOrigin && HasDestination; |
| | | 62 | | |
| | | 63 | | /// <inheritdoc/> |
| | 2776 | 64 | | public bool IsValid => HasValidEndpoints && MaxPathSearchRange > 0; |
| | | 65 | | |
| | | 66 | | /// <inheritdoc/> |
| | | 67 | | public bool HasZeroDisplacement => |
| | 1594 | 68 | | !IsValid |
| | 1594 | 69 | | || StartNode == EndNode; |
| | | 70 | | |
| | | 71 | | /// <inheritdoc/> |
| | 5662 | 72 | | public int RequestCacheKey => GetHashCode(); |
| | | 73 | | |
| | | 74 | | /// <inheritdoc/> |
| | | 75 | | public bool UpdateRequest( |
| | | 76 | | Vector3d origin, |
| | | 77 | | Vector3d destination, |
| | | 78 | | Fixed64? unitSize = null) |
| | | 79 | | { |
| | 8 | 80 | | TrailblazerWorldContext context = Context; |
| | 8 | 81 | | Fixed64 resolvedUnitSize = unitSize ?? context.VoxelSize; |
| | 8 | 82 | | bool success = SolidVoxelFinder.TryGetPathEdgeVoxels( |
| | 8 | 83 | | context, |
| | 8 | 84 | | origin, |
| | 8 | 85 | | destination, |
| | 8 | 86 | | out Voxel? startVoxel, |
| | 8 | 87 | | out Voxel? endVoxel, |
| | 8 | 88 | | resolvedUnitSize, |
| | 8 | 89 | | AllowUnwalkableEndpoints); |
| | | 90 | | |
| | | 91 | | // need to set these even if null incase the new size invalidates the request |
| | 8 | 92 | | Origin = origin; |
| | 8 | 93 | | TargetPosition = destination; |
| | 8 | 94 | | StartNode = startVoxel; |
| | 8 | 95 | | EndNode = endVoxel; |
| | 8 | 96 | | UnitSize = resolvedUnitSize; |
| | 8 | 97 | | MaxPathSearchRange = 0; |
| | | 98 | | |
| | 8 | 99 | | if (!success) |
| | 3 | 100 | | return false; |
| | | 101 | | |
| | 5 | 102 | | if (StartNode != null |
| | 5 | 103 | | && EndNode != null |
| | 5 | 104 | | && context.Pathing.TryGetMaxSearchSize(StartNode, EndNode, out int searchSize)) |
| | 5 | 105 | | MaxPathSearchRange = searchSize; |
| | | 106 | | |
| | 5 | 107 | | return true; |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | /// <inheritdoc/> |
| | | 111 | | public bool TrySetOrigin(Vector3d origin, bool resetSearchRange = false) |
| | | 112 | | { |
| | 80 | 113 | | if (EndNode == null) return false; |
| | | 114 | | |
| | 76 | 115 | | bool success = SolidVoxelFinder.GetStartVoxel( |
| | 76 | 116 | | Context, |
| | 76 | 117 | | origin, |
| | 76 | 118 | | TargetPosition, |
| | 76 | 119 | | out Voxel? newVoxel, |
| | 76 | 120 | | AllowUnwalkableEndpoints, |
| | 76 | 121 | | UnitSize); |
| | | 122 | | |
| | 76 | 123 | | if (!success || newVoxel == null) return false; |
| | | 124 | | |
| | 76 | 125 | | Origin = origin; |
| | | 126 | | |
| | 76 | 127 | | if (StartNode != null) |
| | | 128 | | { |
| | | 129 | | // nothing to do here then |
| | 76 | 130 | | if (newVoxel == StartNode) |
| | 75 | 131 | | return true; |
| | | 132 | | |
| | | 133 | | // force reset if grid changed |
| | 1 | 134 | | if (newVoxel.GridIndex != StartNode.GridIndex) |
| | 0 | 135 | | resetSearchRange = true; |
| | | 136 | | } |
| | | 137 | | |
| | 1 | 138 | | StartNode = newVoxel; |
| | | 139 | | |
| | 1 | 140 | | if (resetSearchRange) |
| | | 141 | | { |
| | 1 | 142 | | MaxPathSearchRange = 0; |
| | 1 | 143 | | if (StartNode != null |
| | 1 | 144 | | && EndNode != null |
| | 1 | 145 | | && Context.Pathing.TryGetMaxSearchSize(StartNode, EndNode, out int searchSize)) |
| | 1 | 146 | | MaxPathSearchRange = searchSize; |
| | | 147 | | } |
| | | 148 | | |
| | 1 | 149 | | return true; |
| | | 150 | | } |
| | | 151 | | |
| | | 152 | | /// <inheritdoc/> |
| | | 153 | | public bool TrySetDestination(Vector3d destination, bool resetSearchRange = false) |
| | | 154 | | { |
| | 8 | 155 | | if (StartNode == null) return false; |
| | | 156 | | |
| | 6 | 157 | | bool success = SolidVoxelFinder.GetEndVoxel( |
| | 6 | 158 | | Context, |
| | 6 | 159 | | Origin, |
| | 6 | 160 | | destination, |
| | 6 | 161 | | out Voxel? newVoxel, |
| | 6 | 162 | | AllowUnwalkableEndpoints, |
| | 6 | 163 | | UnitSize); |
| | | 164 | | |
| | 6 | 165 | | if (!success || newVoxel == null) return false; |
| | | 166 | | |
| | 6 | 167 | | TargetPosition = destination; |
| | | 168 | | |
| | 6 | 169 | | if (EndNode != null) |
| | | 170 | | { |
| | | 171 | | // nothing to do here then |
| | 6 | 172 | | if (newVoxel == EndNode) |
| | 3 | 173 | | return true; |
| | | 174 | | |
| | | 175 | | // force reset if grid changed |
| | 3 | 176 | | if (newVoxel.GridIndex != EndNode.GridIndex) |
| | 0 | 177 | | resetSearchRange = true; |
| | | 178 | | } |
| | | 179 | | |
| | 3 | 180 | | EndNode = newVoxel; |
| | | 181 | | |
| | 3 | 182 | | if (resetSearchRange) |
| | | 183 | | { |
| | 2 | 184 | | MaxPathSearchRange = 0; |
| | 2 | 185 | | if (StartNode != null |
| | 2 | 186 | | && EndNode != null |
| | 2 | 187 | | && Context.Pathing.TryGetMaxSearchSize(StartNode, EndNode, out int searchSize)) |
| | 2 | 188 | | MaxPathSearchRange = searchSize; |
| | | 189 | | } |
| | | 190 | | |
| | 3 | 191 | | return true; |
| | | 192 | | } |
| | | 193 | | |
| | | 194 | | /// <inheritdoc/> |
| | | 195 | | public bool TrySetUnitSize(Fixed64 unitSize) |
| | | 196 | | { |
| | | 197 | | // no change |
| | 3 | 198 | | if (UnitSize == unitSize || !HasValidEndpoints) return false; |
| | | 199 | | |
| | 1 | 200 | | return UpdateRequest(Origin, TargetPosition, unitSize); |
| | | 201 | | } |
| | | 202 | | |
| | | 203 | | /// <inheritdoc/> |
| | | 204 | | public override abstract int GetHashCode(); |
| | | 205 | | } |