| | | 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 | | /// Represents a chart-optional guided travel request through raw voxel volume. |
| | | 11 | | /// </summary> |
| | | 12 | | /// <remarks> |
| | | 13 | | /// Volume requests resolve directly against raw voxels instead of surface partitions. Traversal membership |
| | | 14 | | /// can come from authored <see cref="VolumeChartPartition"/> data, host-configured <see cref="VolumeMediumRules"/>, |
| | | 15 | | /// or both, depending on the requested <see cref="Medium"/>. |
| | | 16 | | /// </remarks> |
| | | 17 | | public sealed class VolumePathRequest : IPathRequest, IEquatable<VolumePathRequest> |
| | | 18 | | { |
| | | 19 | | /// <inheritdoc/> |
| | | 20 | | public TrailblazerWorldContext Context { get; private set; } = null!; |
| | | 21 | | |
| | | 22 | | /// <inheritdoc/> |
| | | 23 | | public Vector3d Origin { get; private set; } |
| | | 24 | | |
| | | 25 | | /// <inheritdoc/> |
| | | 26 | | public Voxel? StartNode { get; private set; } |
| | | 27 | | |
| | | 28 | | /// <inheritdoc/> |
| | | 29 | | public Vector3d TargetPosition { get; private set; } |
| | | 30 | | |
| | | 31 | | /// <inheritdoc/> |
| | | 32 | | public Voxel? EndNode { get; private set; } |
| | | 33 | | |
| | | 34 | | /// <inheritdoc/> |
| | | 35 | | public Fixed64 UnitSize { get; private set; } |
| | | 36 | | |
| | | 37 | | /// <inheritdoc/> |
| | | 38 | | public bool AllowUnwalkableEndpoints { get; set; } |
| | | 39 | | |
| | | 40 | | /// <inheritdoc/> |
| | | 41 | | public int MaxPathSearchRange { get; set; } |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Gets or sets the heuristic method used to guide the algorithm's decision-making process. |
| | | 45 | | /// </summary> |
| | | 46 | | /// <remarks> |
| | | 47 | | /// Selecting an appropriate heuristic can significantly impact the performance and accuracy of |
| | | 48 | | /// the algorithm. Refer to the documentation for HeuristicMethod for available options and their intended use |
| | | 49 | | /// cases. |
| | | 50 | | /// </remarks> |
| | | 51 | | public HeuristicMethod Heuristic { get; set; } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Gets the medium used for traversal operations. |
| | | 55 | | /// </summary> |
| | | 56 | | /// <remarks> |
| | | 57 | | /// The traversal medium determines the method or environment by which traversal is performed. |
| | | 58 | | /// The value is set internally and cannot be modified directly by consumers of the class. |
| | | 59 | | /// </remarks> |
| | | 60 | | public TraversalMedium Medium { get; private set; } |
| | | 61 | | |
| | | 62 | | /// <inheritdoc/> |
| | 748 | 63 | | public bool HasOrigin => StartNode != null; |
| | | 64 | | |
| | | 65 | | /// <inheritdoc/> |
| | 742 | 66 | | public bool HasDestination => EndNode != null; |
| | | 67 | | |
| | | 68 | | /// <inheritdoc/> |
| | 747 | 69 | | public bool HasValidEndpoints => HasOrigin && HasDestination; |
| | | 70 | | |
| | | 71 | | /// <inheritdoc/> |
| | 554 | 72 | | public bool IsValid => HasValidEndpoints && MaxPathSearchRange > 0; |
| | | 73 | | |
| | | 74 | | /// <inheritdoc/> |
| | | 75 | | public bool HasZeroDisplacement => |
| | 255 | 76 | | !IsValid |
| | 255 | 77 | | || StartNode == EndNode; |
| | | 78 | | |
| | | 79 | | /// <inheritdoc/> |
| | 2515 | 80 | | public int RequestCacheKey => GetHashCode(); |
| | | 81 | | |
| | 370 | 82 | | private VolumePathRequest() { } |
| | | 83 | | |
| | | 84 | | /// <summary> |
| | | 85 | | /// Attempts to create a new context-bound volume pathfinding request. |
| | | 86 | | /// </summary> |
| | | 87 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 88 | | public static bool TryCreate( |
| | | 89 | | TrailblazerWorldContext context, |
| | | 90 | | Vector3d origin, |
| | | 91 | | Vector3d destination, |
| | | 92 | | Fixed64 unitSize, |
| | | 93 | | [NotNullWhen(true)] out VolumePathRequest? request, |
| | | 94 | | HeuristicMethod heuristic = HeuristicMethod.Euclidean, |
| | | 95 | | bool allowUnwalkableEndpoints = false, |
| | | 96 | | TraversalMedium medium = TraversalMedium.Gas) |
| | | 97 | | { |
| | 8 | 98 | | request = Create( |
| | 8 | 99 | | context, |
| | 8 | 100 | | origin, |
| | 8 | 101 | | destination, |
| | 8 | 102 | | unitSize, |
| | 8 | 103 | | heuristic, |
| | 8 | 104 | | allowUnwalkableEndpoints, |
| | 8 | 105 | | medium); |
| | | 106 | | |
| | 8 | 107 | | return request != null; |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | /// <summary> |
| | | 111 | | /// Creates a context-bound volume pathfinding request. |
| | | 112 | | /// </summary> |
| | | 113 | | public static VolumePathRequest? Create( |
| | | 114 | | TrailblazerWorldContext context, |
| | | 115 | | Vector3d origin, |
| | | 116 | | Vector3d destination, |
| | | 117 | | Fixed64 unitSize, |
| | | 118 | | HeuristicMethod heuristic = HeuristicMethod.Euclidean, |
| | | 119 | | bool allowUnwalkableEndpoints = false, |
| | | 120 | | TraversalMedium medium = TraversalMedium.Gas) |
| | | 121 | | { |
| | 210 | 122 | | PathRequestContextResolver.ThrowIfUnusable(context); |
| | 210 | 123 | | if (!VolumeVoxelFinder.TryGetPathEdgeVoxels( |
| | 210 | 124 | | context, |
| | 210 | 125 | | origin, |
| | 210 | 126 | | destination, |
| | 210 | 127 | | out Voxel? startNode, |
| | 210 | 128 | | out Voxel? endNode, |
| | 210 | 129 | | unitSize, |
| | 210 | 130 | | allowUnwalkableEndpoints, |
| | 210 | 131 | | medium)) |
| | | 132 | | { |
| | 25 | 133 | | return null; |
| | | 134 | | } |
| | | 135 | | |
| | 185 | 136 | | if (startNode == null || endNode == null) |
| | 0 | 137 | | return null; |
| | | 138 | | |
| | 185 | 139 | | var request = new VolumePathRequest |
| | 185 | 140 | | { |
| | 185 | 141 | | Context = context, |
| | 185 | 142 | | Origin = origin, |
| | 185 | 143 | | StartNode = startNode, |
| | 185 | 144 | | TargetPosition = destination, |
| | 185 | 145 | | EndNode = endNode, |
| | 185 | 146 | | UnitSize = unitSize, |
| | 185 | 147 | | Heuristic = heuristic, |
| | 185 | 148 | | AllowUnwalkableEndpoints = allowUnwalkableEndpoints, |
| | 185 | 149 | | Medium = medium |
| | 185 | 150 | | }; |
| | | 151 | | |
| | 185 | 152 | | if (context.Pathing.TryGetMaxSearchSize(startNode, endNode, out int searchSize)) |
| | 185 | 153 | | request.MaxPathSearchRange = searchSize; |
| | | 154 | | |
| | 185 | 155 | | return request; |
| | | 156 | | } |
| | | 157 | | |
| | | 158 | | /// <inheritdoc/> |
| | | 159 | | public bool UpdateRequest( |
| | | 160 | | Vector3d origin, |
| | | 161 | | Vector3d destination, |
| | | 162 | | Fixed64? unitSize) |
| | | 163 | | { |
| | 5 | 164 | | Fixed64 resolvedUnitSize = unitSize ?? Context.VoxelSize; |
| | 5 | 165 | | bool hasEndpoints = VolumeVoxelFinder.TryGetPathEdgeVoxels( |
| | 5 | 166 | | Context, |
| | 5 | 167 | | origin, |
| | 5 | 168 | | destination, |
| | 5 | 169 | | out Voxel? startNode, |
| | 5 | 170 | | out Voxel? endNode, |
| | 5 | 171 | | resolvedUnitSize, |
| | 5 | 172 | | AllowUnwalkableEndpoints, |
| | 5 | 173 | | Medium); |
| | | 174 | | |
| | 5 | 175 | | Origin = origin; |
| | 5 | 176 | | TargetPosition = destination; |
| | 5 | 177 | | StartNode = hasEndpoints ? startNode : null; |
| | 5 | 178 | | EndNode = hasEndpoints ? endNode : null; |
| | 5 | 179 | | UnitSize = resolvedUnitSize; |
| | 5 | 180 | | MaxPathSearchRange = 0; |
| | | 181 | | |
| | 5 | 182 | | if (hasEndpoints |
| | 5 | 183 | | && StartNode != null |
| | 5 | 184 | | && EndNode != null |
| | 5 | 185 | | && Context.Pathing.TryGetMaxSearchSize(StartNode, EndNode, out int searchSize)) |
| | 2 | 186 | | MaxPathSearchRange = searchSize; |
| | | 187 | | |
| | 5 | 188 | | return HasValidEndpoints; |
| | | 189 | | } |
| | | 190 | | |
| | | 191 | | /// <inheritdoc/> |
| | | 192 | | public bool TrySetOrigin(Vector3d origin, bool resetSearchRange = false) |
| | | 193 | | { |
| | 17 | 194 | | if (EndNode == null) |
| | 1 | 195 | | return false; |
| | | 196 | | |
| | 16 | 197 | | if (!VolumeVoxelFinder.GetStartVoxel( |
| | 16 | 198 | | Context, |
| | 16 | 199 | | origin, |
| | 16 | 200 | | TargetPosition, |
| | 16 | 201 | | out Voxel? startNode, |
| | 16 | 202 | | AllowUnwalkableEndpoints, |
| | 16 | 203 | | UnitSize, |
| | 16 | 204 | | Medium)) |
| | | 205 | | { |
| | 1 | 206 | | return false; |
| | | 207 | | } |
| | | 208 | | |
| | 15 | 209 | | if (startNode == null) |
| | 0 | 210 | | return false; |
| | | 211 | | |
| | 15 | 212 | | Origin = origin; |
| | | 213 | | |
| | 15 | 214 | | if (StartNode != null) |
| | | 215 | | { |
| | 15 | 216 | | if (startNode == StartNode) |
| | 13 | 217 | | return true; |
| | | 218 | | |
| | 2 | 219 | | if (startNode.GridIndex != StartNode.GridIndex) |
| | 0 | 220 | | resetSearchRange = true; |
| | | 221 | | } |
| | | 222 | | |
| | 2 | 223 | | StartNode = startNode; |
| | | 224 | | |
| | 2 | 225 | | if (resetSearchRange) |
| | | 226 | | { |
| | 1 | 227 | | MaxPathSearchRange = 0; |
| | 1 | 228 | | if (HasDestination && Context.Pathing.TryGetMaxSearchSize(StartNode, EndNode, out int searchSize)) |
| | 1 | 229 | | MaxPathSearchRange = searchSize; |
| | | 230 | | } |
| | | 231 | | |
| | 2 | 232 | | return true; |
| | | 233 | | } |
| | | 234 | | |
| | | 235 | | /// <inheritdoc/> |
| | | 236 | | public bool TrySetDestination(Vector3d destination, bool resetSearchRange = false) |
| | | 237 | | { |
| | 5 | 238 | | if (StartNode == null) |
| | 1 | 239 | | return false; |
| | | 240 | | |
| | 4 | 241 | | if (!VolumeVoxelFinder.GetEndVoxel( |
| | 4 | 242 | | Context, |
| | 4 | 243 | | Origin, |
| | 4 | 244 | | destination, |
| | 4 | 245 | | out Voxel? endNode, |
| | 4 | 246 | | AllowUnwalkableEndpoints, |
| | 4 | 247 | | UnitSize, |
| | 4 | 248 | | Medium)) |
| | | 249 | | { |
| | 1 | 250 | | return false; |
| | | 251 | | } |
| | | 252 | | |
| | 3 | 253 | | if (endNode == null) |
| | 0 | 254 | | return false; |
| | | 255 | | |
| | 3 | 256 | | TargetPosition = destination; |
| | | 257 | | |
| | 3 | 258 | | if (EndNode != null) |
| | | 259 | | { |
| | 3 | 260 | | if (endNode == EndNode) |
| | 1 | 261 | | return true; |
| | | 262 | | |
| | 2 | 263 | | if (endNode.GridIndex != EndNode.GridIndex) |
| | 0 | 264 | | resetSearchRange = true; |
| | | 265 | | } |
| | | 266 | | |
| | 2 | 267 | | EndNode = endNode; |
| | | 268 | | |
| | 2 | 269 | | if (resetSearchRange) |
| | | 270 | | { |
| | 1 | 271 | | MaxPathSearchRange = 0; |
| | 1 | 272 | | if (HasOrigin && Context.Pathing.TryGetMaxSearchSize(StartNode, EndNode, out int searchSize)) |
| | 1 | 273 | | MaxPathSearchRange = searchSize; |
| | | 274 | | } |
| | | 275 | | |
| | 2 | 276 | | return true; |
| | | 277 | | } |
| | | 278 | | |
| | | 279 | | /// <inheritdoc/> |
| | | 280 | | public bool TrySetUnitSize(Fixed64 unitSize) |
| | | 281 | | { |
| | 3 | 282 | | if (UnitSize == unitSize || !HasValidEndpoints) |
| | 1 | 283 | | return false; |
| | | 284 | | |
| | 2 | 285 | | return UpdateRequest(Origin, TargetPosition, unitSize); |
| | | 286 | | } |
| | | 287 | | |
| | | 288 | | /// <inheritdoc/> |
| | | 289 | | public override bool Equals(object? obj) => |
| | 3 | 290 | | obj is VolumePathRequest other && Equals(other); |
| | | 291 | | |
| | | 292 | | /// <inheritdoc/> |
| | | 293 | | public bool Equals(VolumePathRequest? other) => |
| | 5 | 294 | | other != null |
| | 5 | 295 | | && RequestCacheKey == other.RequestCacheKey; |
| | | 296 | | |
| | | 297 | | /// <inheritdoc/> |
| | | 298 | | public override int GetHashCode() |
| | | 299 | | { |
| | 2517 | 300 | | PathRequestHashBuilder hash = PathRequestHashBuilder.Create(); |
| | 2517 | 301 | | hash.Add(StartNode?.SpawnToken ?? 0); |
| | 2517 | 302 | | hash.Add(EndNode?.SpawnToken ?? 0); |
| | 2517 | 303 | | hash.Add(UnitSize.GetHashCode()); |
| | 2517 | 304 | | hash.Add(AllowUnwalkableEndpoints); |
| | 2517 | 305 | | hash.Add((int)Heuristic); |
| | 2517 | 306 | | hash.Add((int)Medium); |
| | 2517 | 307 | | hash.Add(MaxPathSearchRange); |
| | 2517 | 308 | | hash.Add(Context.Pathing.State.VolumeRulesState.RegistryVersion); |
| | 2517 | 309 | | return hash.ToHashCode(); |
| | | 310 | | } |
| | | 311 | | } |