| | | 1 | | using FixedMathSharp; |
| | | 2 | | using GridForge; |
| | | 3 | | using GridForge.Grids; |
| | | 4 | | using GridForge.Utility; |
| | | 5 | | using System.Diagnostics.CodeAnalysis; |
| | | 6 | | using System.Runtime.CompilerServices; |
| | | 7 | | |
| | | 8 | | namespace Trailblazer.Pathing; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Resolves and validates raw voxel volumes without requiring navigation chart partitions. |
| | | 12 | | /// </summary> |
| | | 13 | | public static class VolumeVoxelFinder |
| | | 14 | | { |
| | | 15 | | /// <summary> |
| | | 16 | | /// Attempts to determine the voxels at the origin and target endpoints in one explicit context. |
| | | 17 | | /// </summary> |
| | | 18 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 19 | | public static bool TryGetPathEdgeVoxels( |
| | | 20 | | TrailblazerWorldContext context, |
| | | 21 | | Vector3d origin, |
| | | 22 | | Vector3d target, |
| | | 23 | | [MaybeNullWhen(false)] out Voxel originVoxel, |
| | | 24 | | [MaybeNullWhen(false)] out Voxel targetVoxel, |
| | | 25 | | Fixed64 unitSize, |
| | | 26 | | bool allowUnwalkableEndpoints = false, |
| | | 27 | | TraversalMedium medium = TraversalMedium.Gas) |
| | | 28 | | { |
| | 219 | 29 | | targetVoxel = null; |
| | 219 | 30 | | if (!GetStartVoxel(context, origin, target, out originVoxel, allowUnwalkableEndpoints, unitSize, medium)) |
| | 13 | 31 | | return false; |
| | | 32 | | |
| | 206 | 33 | | if (!GetEndVoxel(context, origin, target, out targetVoxel, allowUnwalkableEndpoints, unitSize, medium)) |
| | 17 | 34 | | return false; |
| | | 35 | | |
| | 189 | 36 | | return true; |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Attempts to determine the start voxel in one explicit context. |
| | | 41 | | /// </summary> |
| | | 42 | | public static bool GetStartVoxel( |
| | | 43 | | TrailblazerWorldContext context, |
| | | 44 | | Vector3d origin, |
| | | 45 | | Vector3d target, |
| | | 46 | | [MaybeNullWhen(false)] out Voxel originVoxel, |
| | | 47 | | bool allowUnwalkableEndpoints = false, |
| | | 48 | | Fixed64? unitSize = null, |
| | | 49 | | TraversalMedium medium = TraversalMedium.Gas) |
| | | 50 | | { |
| | 238 | 51 | | return TryGetEndpointVoxel( |
| | 238 | 52 | | context, |
| | 238 | 53 | | origin, |
| | 238 | 54 | | target, |
| | 238 | 55 | | out originVoxel, |
| | 238 | 56 | | allowUnwalkableEndpoints, |
| | 238 | 57 | | unitSize ?? context.VoxelSize, |
| | 238 | 58 | | medium); |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Attempts to determine the end voxel in one explicit context. |
| | | 63 | | /// </summary> |
| | | 64 | | public static bool GetEndVoxel( |
| | | 65 | | TrailblazerWorldContext context, |
| | | 66 | | Vector3d origin, |
| | | 67 | | Vector3d target, |
| | | 68 | | [MaybeNullWhen(false)] out Voxel targetVoxel, |
| | | 69 | | bool allowUnwalkableEndpoints = false, |
| | | 70 | | Fixed64? unitSize = null, |
| | | 71 | | TraversalMedium medium = TraversalMedium.Gas) |
| | | 72 | | { |
| | 211 | 73 | | return TryGetEndpointVoxel( |
| | 211 | 74 | | context, |
| | 211 | 75 | | target, |
| | 211 | 76 | | origin, |
| | 211 | 77 | | out targetVoxel, |
| | 211 | 78 | | allowUnwalkableEndpoints, |
| | 211 | 79 | | unitSize ?? context.VoxelSize, |
| | 211 | 80 | | medium); |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Determines whether a direct, traversable path exists in one explicit context. |
| | | 85 | | /// </summary> |
| | | 86 | | public static bool IsDirectPathClear( |
| | | 87 | | TrailblazerWorldContext context, |
| | | 88 | | Vector3d start, |
| | | 89 | | Vector3d end, |
| | | 90 | | Fixed64 unitSize, |
| | | 91 | | bool allowUnwalkableEndpoints, |
| | | 92 | | TraversalMedium medium = TraversalMedium.Gas, |
| | | 93 | | Voxel? startNode = null, |
| | | 94 | | Voxel? endNode = null) |
| | | 95 | | { |
| | 20 | 96 | | PathRequestContextResolver.ThrowIfUnusable(context); |
| | 20 | 97 | | PathingWorldState state = context.Pathing.State; |
| | 20 | 98 | | if (!VolumeMediumRules.IsConfigured(state, medium)) |
| | 1 | 99 | | return false; |
| | | 100 | | |
| | 19 | 101 | | bool foundAny = false; |
| | | 102 | | |
| | 65 | 103 | | foreach (GridVoxelSet gridVoxelSet in GridTracer.TraceLine(context.World, start, end)) |
| | | 104 | | { |
| | 125 | 105 | | foreach (Voxel voxel in gridVoxelSet.Voxels) |
| | | 106 | | { |
| | 49 | 107 | | foundAny = true; |
| | | 108 | | |
| | 49 | 109 | | bool isRelaxedEndpoint = allowUnwalkableEndpoints |
| | 49 | 110 | | && ((startNode != null && voxel.WorldIndex == startNode.WorldIndex) |
| | 49 | 111 | | || (endNode != null && voxel.WorldIndex == endNode.WorldIndex)); |
| | 49 | 112 | | if (isRelaxedEndpoint) |
| | | 113 | | { |
| | 4 | 114 | | if (!PassesMedium(state, voxel, medium)) |
| | 1 | 115 | | return false; |
| | | 116 | | |
| | | 117 | | continue; |
| | | 118 | | } |
| | | 119 | | |
| | 45 | 120 | | if (!IsTraversable(state, voxel, unitSize, medium)) |
| | 10 | 121 | | return false; |
| | | 122 | | } |
| | | 123 | | } |
| | | 124 | | |
| | 8 | 125 | | return foundAny; |
| | 11 | 126 | | } |
| | | 127 | | |
| | | 128 | | /// <summary> |
| | | 129 | | /// Determines whether the specified voxel can be traversed in one explicit context. |
| | | 130 | | /// </summary> |
| | | 131 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 132 | | public static bool IsTraversable( |
| | | 133 | | TrailblazerWorldContext context, |
| | | 134 | | Voxel voxel, |
| | | 135 | | Fixed64 unitSize, |
| | | 136 | | TraversalMedium medium = TraversalMedium.Gas) |
| | | 137 | | { |
| | 8361 | 138 | | PathRequestContextResolver.ThrowIfUnusable(context); |
| | 8361 | 139 | | return IsTraversable(context.Pathing.State, voxel, unitSize, medium); |
| | | 140 | | } |
| | | 141 | | |
| | | 142 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 143 | | internal static bool IsTraversable( |
| | | 144 | | PathingWorldState state, |
| | | 145 | | Voxel voxel, |
| | | 146 | | Fixed64 unitSize, |
| | | 147 | | TraversalMedium medium = TraversalMedium.Gas) |
| | | 148 | | { |
| | 8628 | 149 | | return IsBaseTraversable(voxel, unitSize) |
| | 8628 | 150 | | && PassesMedium(state, voxel, medium); |
| | | 151 | | } |
| | | 152 | | |
| | | 153 | | /// <summary> |
| | | 154 | | /// Attempts to find the closest traversable neighboring voxel in one explicit context. |
| | | 155 | | /// </summary> |
| | | 156 | | public static bool TryGetClosestTraversableVoxel( |
| | | 157 | | TrailblazerWorldContext context, |
| | | 158 | | Voxel voxel, |
| | | 159 | | [MaybeNullWhen(false)] out Voxel closestNeighbor, |
| | | 160 | | Fixed64 unitSize, |
| | | 161 | | TraversalMedium medium = TraversalMedium.Gas) |
| | | 162 | | { |
| | 2 | 163 | | return EndpointVoxelResolver.TryGetClosestTraversableVoxel( |
| | 2 | 164 | | context, |
| | 2 | 165 | | voxel, |
| | 2 | 166 | | out closestNeighbor, |
| | 2 | 167 | | unitSize, |
| | 2 | 168 | | new VolumeEndpointPolicy(context, medium)); |
| | | 169 | | } |
| | | 170 | | |
| | | 171 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 172 | | private static bool IsBaseTraversable(Voxel voxel, Fixed64 unitSize) |
| | | 173 | | { |
| | 8984 | 174 | | if (voxel == null || voxel.IsBlocked) |
| | 149 | 175 | | return false; |
| | | 176 | | |
| | 8835 | 177 | | if (voxel.TryGetPartition(out VolumeChartPartition? volumePartition) |
| | 8835 | 178 | | && volumePartition != null) |
| | 3533 | 179 | | return !volumePartition.IsImpassable(unitSize); |
| | | 180 | | |
| | 5302 | 181 | | if (voxel.TryGetPartition(out SolidChartPartition? partition) |
| | 5302 | 182 | | && partition != null) |
| | 671 | 183 | | return !partition.IsImpassable(unitSize); |
| | | 184 | | |
| | 4631 | 185 | | return false; |
| | | 186 | | } |
| | | 187 | | |
| | | 188 | | private static bool TryGetEndpointVoxel( |
| | | 189 | | TrailblazerWorldContext context, |
| | | 190 | | Vector3d position, |
| | | 191 | | Vector3d traceToward, |
| | | 192 | | [MaybeNullWhen(false)] out Voxel voxel, |
| | | 193 | | bool allowUnwalkableEndpoints, |
| | | 194 | | Fixed64 unitSize, |
| | | 195 | | TraversalMedium medium) |
| | | 196 | | { |
| | 449 | 197 | | return EndpointVoxelResolver.TryGetEndpointVoxel( |
| | 449 | 198 | | context, |
| | 449 | 199 | | position, |
| | 449 | 200 | | traceToward, |
| | 449 | 201 | | out voxel, |
| | 449 | 202 | | allowUnwalkableEndpoints, |
| | 449 | 203 | | unitSize, |
| | 449 | 204 | | new VolumeEndpointPolicy(context, medium)); |
| | | 205 | | } |
| | | 206 | | |
| | | 207 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 208 | | private static bool PassesMedium(PathingWorldState state, Voxel voxel, TraversalMedium medium) |
| | | 209 | | { |
| | 4289 | 210 | | return VolumeMediumRules.Matches(state, voxel, medium); |
| | | 211 | | } |
| | | 212 | | |
| | | 213 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 214 | | private static bool RequiresSizeFallback( |
| | | 215 | | PathingWorldState state, |
| | | 216 | | Voxel voxel, |
| | | 217 | | Fixed64 unitSize, |
| | | 218 | | Fixed64 voxelSize, |
| | | 219 | | TraversalMedium medium) |
| | | 220 | | { |
| | 23 | 221 | | if (unitSize == voxelSize |
| | 23 | 222 | | || voxel == null |
| | 23 | 223 | | || voxel.IsBlocked |
| | 23 | 224 | | || !PassesMedium(state, voxel, medium)) |
| | | 225 | | { |
| | 19 | 226 | | return false; |
| | | 227 | | } |
| | | 228 | | |
| | 4 | 229 | | if (voxel.TryGetPartition(out VolumeChartPartition? volumePartition) |
| | 4 | 230 | | && volumePartition != null) |
| | 3 | 231 | | return volumePartition.IsImpassable(unitSize); |
| | | 232 | | |
| | 1 | 233 | | return voxel.TryGetPartition(out SolidChartPartition? partition) |
| | 1 | 234 | | && partition != null |
| | 1 | 235 | | && partition.IsImpassable(unitSize); |
| | | 236 | | } |
| | | 237 | | |
| | | 238 | | internal static bool HasClearance(TrailblazerWorldContext context, Voxel origin, Fixed64 unitSize) |
| | | 239 | | { |
| | 3536 | 240 | | PathRequestContextResolver.ThrowIfUnusable(context); |
| | 3536 | 241 | | Fixed64 voxelSize = context.VoxelSize; |
| | 3536 | 242 | | if (unitSize <= voxelSize) |
| | 3519 | 243 | | return true; |
| | | 244 | | |
| | 17 | 245 | | int requiredRadius = (unitSize / voxelSize).CeilToInt() - 1; |
| | 17 | 246 | | if (requiredRadius <= 0) |
| | 0 | 247 | | return true; |
| | | 248 | | |
| | 17 | 249 | | if (!context.World.TryGetGrid(origin.GridIndex, out VoxelGrid? grid)) |
| | 0 | 250 | | return false; |
| | | 251 | | |
| | 82 | 252 | | for (int x = -requiredRadius; x <= requiredRadius; x++) |
| | | 253 | | { |
| | 210 | 254 | | for (int y = -requiredRadius; y <= requiredRadius; y++) |
| | | 255 | | { |
| | 594 | 256 | | for (int z = -requiredRadius; z <= requiredRadius; z++) |
| | | 257 | | { |
| | 225 | 258 | | if (x == 0 && y == 0 && z == 0) |
| | | 259 | | continue; |
| | | 260 | | |
| | 217 | 261 | | if (!origin.TryGetNeighborFromOffset(grid!, (x, y, z), out Voxel? neighbor) |
| | 217 | 262 | | || neighbor!.IsBlocked) |
| | | 263 | | { |
| | 9 | 264 | | return false; |
| | | 265 | | } |
| | | 266 | | } |
| | | 267 | | } |
| | | 268 | | } |
| | | 269 | | |
| | 8 | 270 | | return true; |
| | | 271 | | } |
| | | 272 | | |
| | | 273 | | private readonly struct VolumeEndpointPolicy : IVoxelEndpointResolutionPolicy |
| | | 274 | | { |
| | | 275 | | private readonly PathingWorldState _state; |
| | | 276 | | private readonly Fixed64 _voxelSize; |
| | | 277 | | private readonly TraversalMedium _medium; |
| | | 278 | | |
| | | 279 | | public VolumeEndpointPolicy(TrailblazerWorldContext context, TraversalMedium medium) |
| | | 280 | | { |
| | 451 | 281 | | _state = context.Pathing.State; |
| | 451 | 282 | | _voxelSize = context.VoxelSize; |
| | 451 | 283 | | _medium = medium; |
| | 451 | 284 | | } |
| | | 285 | | |
| | | 286 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 287 | | public bool CanResolve() |
| | | 288 | | { |
| | 449 | 289 | | return VolumeMediumRules.IsConfigured(_state, _medium); |
| | | 290 | | } |
| | | 291 | | |
| | | 292 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 293 | | public bool TryAcceptDirectVoxel( |
| | | 294 | | Voxel voxel, |
| | | 295 | | Fixed64 unitSize, |
| | | 296 | | bool allowUnwalkableEndpoints) |
| | | 297 | | { |
| | 438 | 298 | | return PassesMedium(_state, voxel, _medium) |
| | 438 | 299 | | && (allowUnwalkableEndpoints || IsBaseTraversable(voxel, unitSize)); |
| | | 300 | | } |
| | | 301 | | |
| | | 302 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 303 | | public bool RequiresSizeFallback(Voxel voxel, Fixed64 unitSize) |
| | | 304 | | { |
| | 23 | 305 | | return VolumeVoxelFinder.RequiresSizeFallback(_state, voxel, unitSize, _voxelSize, _medium); |
| | | 306 | | } |
| | | 307 | | |
| | | 308 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 309 | | public bool IsTraversable(Voxel voxel, Fixed64 unitSize) |
| | | 310 | | { |
| | 222 | 311 | | return VolumeVoxelFinder.IsTraversable(_state, voxel, unitSize, _medium); |
| | | 312 | | } |
| | | 313 | | |
| | | 314 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 315 | | public bool TryGetFinalFallbackVoxel( |
| | | 316 | | Vector3d position, |
| | | 317 | | Voxel directVoxel, |
| | | 318 | | Fixed64 unitSize, |
| | | 319 | | [MaybeNullWhen(false)] out Voxel voxel) |
| | | 320 | | { |
| | 3 | 321 | | voxel = null; |
| | 3 | 322 | | return false; |
| | | 323 | | } |
| | | 324 | | } |
| | | 325 | | } |