| | | 1 | | //======================================================================= |
| | | 2 | | // NavigationSurfaceEdgeRouteWork.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2024-present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using FixedMathSharp; |
| | | 9 | | using GridForge.Grids.Topology; |
| | | 10 | | |
| | | 11 | | namespace Trailblazer.Pathing; |
| | | 12 | | |
| | | 13 | | /// <summary>Reports resumable semantic and swept-geometry edge progress.</summary> |
| | | 14 | | internal enum NavigationSurfaceEdgeRouteStatus : byte |
| | | 15 | | { |
| | | 16 | | Pending = 0, |
| | | 17 | | Point = 1, |
| | | 18 | | Passable = 2, |
| | | 19 | | Impassable = 3, |
| | | 20 | | BudgetExceeded = 4, |
| | | 21 | | CostOverflow = 5 |
| | | 22 | | } |
| | | 23 | | |
| | | 24 | | /// <summary>Evaluates and expands one exact authored surface edge without allocation.</summary> |
| | | 25 | | internal struct NavigationSurfaceEdgeRouteWork |
| | | 26 | | { |
| | | 27 | | private TraversalEvaluator _evaluator; |
| | | 28 | | private NavigationGraphEdge _edge; |
| | | 29 | | private NavigationNodeRef _source; |
| | | 30 | | private TraversalExplicitEdgeWork _explicitWork; |
| | | 31 | | private GridNavigationPortal _incomingPortal; |
| | | 32 | | private Vector3d _incomingTargetAnchor; |
| | | 33 | | private NavigationNodeRef _dependencyNode; |
| | | 34 | | private Fixed64 _cost; |
| | | 35 | | private NavigationCellAddress _pointAddress0; |
| | | 36 | | private NavigationCellAddress _pointAddress1; |
| | | 37 | | private Vector3d _pointPosition0; |
| | | 38 | | private Vector3d _pointPosition1; |
| | | 39 | | private Vector3d _pointPosition2; |
| | | 40 | | private Vector3d _pointPosition3; |
| | | 41 | | private Vector3d _pointPosition4; |
| | | 42 | | private NavigationSurfaceEdgeRouteStatus _completionStatus; |
| | | 43 | | private int _portalOrdinal; |
| | | 44 | | private int _pointOrdinal; |
| | | 45 | | private int _pointCount; |
| | | 46 | | private int _secondPointAddressOrdinal; |
| | | 47 | | private int _targetPointOrdinal; |
| | | 48 | | private bool _emitPoints; |
| | | 49 | | private bool _explicit; |
| | | 50 | | private bool _hasDependencyNode; |
| | | 51 | | |
| | | 52 | | internal NavigationSurfaceEdgeRouteStatus Begin( |
| | | 53 | | TraversalEvaluator evaluator, |
| | | 54 | | NavigationNodeRef source, |
| | | 55 | | in NavigationGraphEdge edge, |
| | | 56 | | bool emitPoints) |
| | | 57 | | { |
| | 17670 | 58 | | Reset(); |
| | 17670 | 59 | | _evaluator = evaluator; |
| | 17670 | 60 | | _source = source; |
| | 17670 | 61 | | _edge = edge; |
| | 17670 | 62 | | _emitPoints = emitPoints; |
| | 17670 | 63 | | _completionStatus = NavigationSurfaceEdgeRouteStatus.Pending; |
| | 17670 | 64 | | if (edge.Kind != NavigationGraphEdgeKind.Explicit) |
| | 17523 | 65 | | return _completionStatus; |
| | | 66 | | |
| | 147 | 67 | | TraversalExplicitEdgeStatus status = evaluator.BeginExplicitEdge( |
| | 147 | 68 | | source, |
| | 147 | 69 | | edge, |
| | 147 | 70 | | out _explicitWork); |
| | 147 | 71 | | if (status != TraversalExplicitEdgeStatus.Pending) |
| | 4 | 72 | | return Complete(NavigationSurfaceEdgeRouteStatus.Impassable); |
| | 143 | 73 | | _explicit = true; |
| | 143 | 74 | | return _completionStatus; |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | internal NavigationSurfaceEdgeRouteStatus Advance( |
| | | 78 | | NavigationWorkMeter meter, |
| | | 79 | | ref int connectionStepRemaining) |
| | | 80 | | { |
| | 19972 | 81 | | if (_pointOrdinal < _pointCount) |
| | 1485 | 82 | | return NavigationSurfaceEdgeRouteStatus.Point; |
| | 18487 | 83 | | if (_completionStatus != NavigationSurfaceEdgeRouteStatus.Pending) |
| | 542 | 84 | | return _completionStatus; |
| | 17945 | 85 | | return _explicit |
| | 17945 | 86 | | ? AdvanceExplicit(meter, ref connectionStepRemaining) |
| | 17945 | 87 | | : AdvanceSimple(); |
| | | 88 | | } |
| | | 89 | | |
| | 17618 | 90 | | internal Fixed64 Cost => _cost; |
| | | 91 | | |
| | 3842 | 92 | | internal bool HasCurrentPoint => _pointOrdinal < _pointCount; |
| | | 93 | | |
| | 1815 | 94 | | internal NavigationAStarGuidePoint CurrentPoint => new( |
| | 1815 | 95 | | _pointOrdinal < _secondPointAddressOrdinal |
| | 1815 | 96 | | ? _pointAddress0 |
| | 1815 | 97 | | : _pointAddress1, |
| | 1815 | 98 | | _pointOrdinal switch |
| | 1815 | 99 | | { |
| | 604 | 100 | | 0 => _pointPosition0, |
| | 604 | 101 | | 1 => _pointPosition1, |
| | 543 | 102 | | 2 => _pointPosition2, |
| | 32 | 103 | | 3 => _pointPosition3, |
| | 32 | 104 | | _ => _pointPosition4 |
| | 1815 | 105 | | }, |
| | 1815 | 106 | | TraversalMedium.Solid); |
| | | 107 | | |
| | | 108 | | internal bool CurrentPointIsTargetFootAnchor => |
| | 1815 | 109 | | _pointOrdinal == _targetPointOrdinal; |
| | | 110 | | |
| | | 111 | | internal void ConsumePoint() |
| | | 112 | | { |
| | | 113 | | System.Diagnostics.Debug.Assert(HasCurrentPoint); |
| | 1814 | 114 | | _pointOrdinal++; |
| | 1814 | 115 | | } |
| | | 116 | | |
| | | 117 | | internal bool TryTakeDependencyNode(out NavigationNodeRef node) |
| | | 118 | | { |
| | 19971 | 119 | | node = _dependencyNode; |
| | 19971 | 120 | | if (!_hasDependencyNode) |
| | 19569 | 121 | | return false; |
| | 402 | 122 | | _dependencyNode = default; |
| | 402 | 123 | | _hasDependencyNode = false; |
| | 402 | 124 | | return true; |
| | | 125 | | } |
| | | 126 | | |
| | | 127 | | internal void Reset() |
| | | 128 | | { |
| | 17670 | 129 | | _evaluator = default; |
| | 17670 | 130 | | _edge = default; |
| | 17670 | 131 | | _source = default; |
| | 17670 | 132 | | _explicitWork = default; |
| | 17670 | 133 | | _incomingPortal = default; |
| | 17670 | 134 | | _incomingTargetAnchor = default; |
| | 17670 | 135 | | _dependencyNode = default; |
| | 17670 | 136 | | _cost = default; |
| | 17670 | 137 | | _pointAddress0 = default; |
| | 17670 | 138 | | _pointAddress1 = default; |
| | 17670 | 139 | | _pointPosition0 = default; |
| | 17670 | 140 | | _pointPosition1 = default; |
| | 17670 | 141 | | _pointPosition2 = default; |
| | 17670 | 142 | | _pointPosition3 = default; |
| | 17670 | 143 | | _pointPosition4 = default; |
| | 17670 | 144 | | _completionStatus = default; |
| | 17670 | 145 | | _portalOrdinal = 0; |
| | 17670 | 146 | | _pointOrdinal = 0; |
| | 17670 | 147 | | _pointCount = 0; |
| | 17670 | 148 | | _secondPointAddressOrdinal = 0; |
| | 17670 | 149 | | _targetPointOrdinal = -1; |
| | 17670 | 150 | | _emitPoints = false; |
| | 17670 | 151 | | _explicit = false; |
| | 17670 | 152 | | _hasDependencyNode = false; |
| | 17670 | 153 | | } |
| | | 154 | | |
| | | 155 | | private NavigationSurfaceEdgeRouteStatus AdvanceSimple() |
| | | 156 | | { |
| | 17523 | 157 | | TraversalEvaluationStatus evaluation = _evaluator.EvaluateEdge( |
| | 17523 | 158 | | _source, |
| | 17523 | 159 | | _edge, |
| | 17523 | 160 | | out TraversalEdgeEvidence evidence); |
| | 17523 | 161 | | if (evaluation == TraversalEvaluationStatus.CostOverflow) |
| | 2 | 162 | | return Complete(NavigationSurfaceEdgeRouteStatus.CostOverflow); |
| | 17521 | 163 | | if (evaluation != TraversalEvaluationStatus.Passable) |
| | 29 | 164 | | return Complete(NavigationSurfaceEdgeRouteStatus.Impassable); |
| | 17492 | 165 | | bool validSourceSegment = GridCellGeometry.IsNavigationBodySegmentValid( |
| | 17492 | 166 | | evidence.SourcePrism, |
| | 17492 | 167 | | evidence.SourceFootAnchor, |
| | 17492 | 168 | | evidence.SourcePortalAnchor, |
| | 17492 | 169 | | _evaluator.Profile.Shape.Radius, |
| | 17492 | 170 | | _evaluator.Profile.Shape.Height, |
| | 17492 | 171 | | default, |
| | 17492 | 172 | | evidence.Portal, |
| | 17492 | 173 | | GridNavigationBodySegmentEndpointAllowance.None); |
| | 17492 | 174 | | bool validTargetSegment = GridCellGeometry.IsNavigationBodySegmentValid( |
| | 17492 | 175 | | evidence.TargetPrism, |
| | 17492 | 176 | | evidence.TargetPortalAnchor, |
| | 17492 | 177 | | evidence.TargetFootAnchor, |
| | 17492 | 178 | | _evaluator.Profile.Shape.Radius, |
| | 17492 | 179 | | _evaluator.Profile.Shape.Height, |
| | 17492 | 180 | | evidence.Portal, |
| | 17492 | 181 | | default, |
| | 17492 | 182 | | GridNavigationBodySegmentEndpointAllowance.None); |
| | | 183 | | System.Diagnostics.Debug.Assert( |
| | | 184 | | evidence.Portal.FaceKind != VoxelContactFaceKind.Horizontal |
| | | 185 | | || (GridCellGeometry.TryGetNavigationPortalTraversalParameters( |
| | | 186 | | evidence.SourcePrism, |
| | | 187 | | evidence.TargetPrism, |
| | | 188 | | evidence.Portal, |
| | | 189 | | evidence.SourcePortalAnchor, |
| | | 190 | | evidence.TargetPortalAnchor, |
| | | 191 | | _evaluator.Profile.Shape.Radius, |
| | | 192 | | _evaluator.Profile.Shape.Height, |
| | | 193 | | out Fixed64 sourceParameter, |
| | | 194 | | out Fixed64 targetParameter) |
| | | 195 | | && sourceParameter == Fixed64.Zero |
| | | 196 | | && targetParameter == Fixed64.One), |
| | | 197 | | "published surface portals retain the exact validated profile crossing"); |
| | | 198 | | System.Diagnostics.Debug.Assert(validSourceSegment && validTargetSegment); |
| | | 199 | | |
| | 17492 | 200 | | _cost = evidence.Cost; |
| | 17492 | 201 | | SetPoints( |
| | 17492 | 202 | | evidence.SourceAddress, |
| | 17492 | 203 | | evidence.TargetAddress, |
| | 17492 | 204 | | evidence.SourcePortalAnchor, |
| | 17492 | 205 | | evidence.TargetPortalAnchor, |
| | 17492 | 206 | | evidence.TargetFootAnchor); |
| | 17492 | 207 | | _targetPointOrdinal = 2; |
| | 17492 | 208 | | _completionStatus = NavigationSurfaceEdgeRouteStatus.Passable; |
| | 17492 | 209 | | return _pointCount == 0 |
| | 17492 | 210 | | ? _completionStatus |
| | 17492 | 211 | | : NavigationSurfaceEdgeRouteStatus.Point; |
| | | 212 | | } |
| | | 213 | | |
| | | 214 | | private NavigationSurfaceEdgeRouteStatus AdvanceExplicit( |
| | | 215 | | NavigationWorkMeter meter, |
| | | 216 | | ref int connectionStepRemaining) |
| | | 217 | | { |
| | 422 | 218 | | if (connectionStepRemaining == 0) |
| | | 219 | | { |
| | 16 | 220 | | return meter.RemainingConnectionLegs == 0 |
| | 16 | 221 | | ? Complete(NavigationSurfaceEdgeRouteStatus.BudgetExceeded) |
| | 16 | 222 | | : NavigationSurfaceEdgeRouteStatus.Pending; |
| | | 223 | | } |
| | 406 | 224 | | if (!meter.TryConsumeConnectionLegs(1)) |
| | 3 | 225 | | return Complete(NavigationSurfaceEdgeRouteStatus.BudgetExceeded); |
| | 403 | 226 | | connectionStepRemaining--; |
| | | 227 | | |
| | 403 | 228 | | TraversalExplicitEdgeStatus semantic = _evaluator.AdvanceExplicitEdge( |
| | 403 | 229 | | ref _explicitWork, |
| | 403 | 230 | | out TraversalEdgeEvidence evidence); |
| | 403 | 231 | | NavigationNodeRef dependencyNode = evidence.DependencyNode; |
| | | 232 | | System.Diagnostics.Debug.Assert( |
| | | 233 | | dependencyNode.IsValid, |
| | | 234 | | "every published explicit corridor leg resolves an immutable graph node"); |
| | 403 | 235 | | _dependencyNode = dependencyNode; |
| | 403 | 236 | | _hasDependencyNode = true; |
| | 403 | 237 | | if (semantic == TraversalExplicitEdgeStatus.CostOverflow) |
| | 1 | 238 | | return Complete(NavigationSurfaceEdgeRouteStatus.CostOverflow); |
| | 402 | 239 | | if (semantic == TraversalExplicitEdgeStatus.Impassable) |
| | 3 | 240 | | return Complete(NavigationSurfaceEdgeRouteStatus.Impassable); |
| | | 241 | | |
| | 399 | 242 | | NavigationExplicitConnectionRecord record = _edge.ExplicitConnection; |
| | 399 | 243 | | NavigationConnection connection = record.Definition; |
| | 399 | 244 | | GridNavigationPortal outgoing = evidence.Portal; |
| | 399 | 245 | | Vector3d sourceAnchor = evidence.SourcePortalAnchor; |
| | 399 | 246 | | Vector3d targetAnchor = evidence.TargetPortalAnchor; |
| | | 247 | | |
| | 399 | 248 | | if (_portalOrdinal == 0) |
| | | 249 | | { |
| | 136 | 250 | | if (!GridCellGeometry.IsNavigationBodySegmentValid( |
| | 136 | 251 | | evidence.SourcePrism, |
| | 136 | 252 | | evidence.SourceFootAnchor, |
| | 136 | 253 | | connection.EntryAnchor, |
| | 136 | 254 | | _evaluator.Profile.Shape.Radius, |
| | 136 | 255 | | _evaluator.Profile.Shape.Height, |
| | 136 | 256 | | default, |
| | 136 | 257 | | outgoing, |
| | 136 | 258 | | GridNavigationBodySegmentEndpointAllowance.None) |
| | 136 | 259 | | || !GridCellGeometry.IsNavigationBodySegmentValid( |
| | 136 | 260 | | evidence.SourcePrism, |
| | 136 | 261 | | connection.EntryAnchor, |
| | 136 | 262 | | sourceAnchor, |
| | 136 | 263 | | _evaluator.Profile.Shape.Radius, |
| | 136 | 264 | | _evaluator.Profile.Shape.Height, |
| | 136 | 265 | | default, |
| | 136 | 266 | | outgoing, |
| | 136 | 267 | | GridNavigationBodySegmentEndpointAllowance.None)) |
| | | 268 | | { |
| | 1 | 269 | | return Complete(NavigationSurfaceEdgeRouteStatus.Impassable); |
| | | 270 | | } |
| | 135 | 271 | | SetPoints( |
| | 135 | 272 | | evidence.SourceAddress, |
| | 135 | 273 | | connection.EntryAnchor, |
| | 135 | 274 | | sourceAnchor); |
| | | 275 | | } |
| | | 276 | | else |
| | | 277 | | { |
| | 263 | 278 | | if (!GridCellGeometry.IsNavigationBodySegmentValid( |
| | 263 | 279 | | evidence.SourcePrism, |
| | 263 | 280 | | _incomingTargetAnchor, |
| | 263 | 281 | | sourceAnchor, |
| | 263 | 282 | | _evaluator.Profile.Shape.Radius, |
| | 263 | 283 | | _evaluator.Profile.Shape.Height, |
| | 263 | 284 | | _incomingPortal, |
| | 263 | 285 | | outgoing, |
| | 263 | 286 | | GridNavigationBodySegmentEndpointAllowance.None)) |
| | | 287 | | { |
| | 1 | 288 | | return Complete(NavigationSurfaceEdgeRouteStatus.Impassable); |
| | | 289 | | } |
| | 262 | 290 | | SetPoints( |
| | 262 | 291 | | evidence.SourceAddress, |
| | 262 | 292 | | _incomingTargetAnchor, |
| | 262 | 293 | | sourceAnchor); |
| | | 294 | | } |
| | | 295 | | |
| | 397 | 296 | | _portalOrdinal++; |
| | 397 | 297 | | _incomingPortal = outgoing; |
| | 397 | 298 | | _incomingTargetAnchor = targetAnchor; |
| | 397 | 299 | | if (semantic == TraversalExplicitEdgeStatus.Passable) |
| | | 300 | | { |
| | 129 | 301 | | if (!GridCellGeometry.IsNavigationBodySegmentValid( |
| | 129 | 302 | | evidence.TargetPrism, |
| | 129 | 303 | | targetAnchor, |
| | 129 | 304 | | connection.ExitAnchor, |
| | 129 | 305 | | _evaluator.Profile.Shape.Radius, |
| | 129 | 306 | | _evaluator.Profile.Shape.Height, |
| | 129 | 307 | | outgoing, |
| | 129 | 308 | | default, |
| | 129 | 309 | | GridNavigationBodySegmentEndpointAllowance.None) |
| | 129 | 310 | | || !GridCellGeometry.IsNavigationBodySegmentValid( |
| | 129 | 311 | | evidence.TargetPrism, |
| | 129 | 312 | | connection.ExitAnchor, |
| | 129 | 313 | | evidence.TargetFootAnchor, |
| | 129 | 314 | | _evaluator.Profile.Shape.Radius, |
| | 129 | 315 | | _evaluator.Profile.Shape.Height, |
| | 129 | 316 | | outgoing, |
| | 129 | 317 | | default, |
| | 129 | 318 | | GridNavigationBodySegmentEndpointAllowance.None)) |
| | | 319 | | { |
| | 2 | 320 | | return Complete(NavigationSurfaceEdgeRouteStatus.Impassable); |
| | | 321 | | } |
| | 127 | 322 | | AppendPoints( |
| | 127 | 323 | | evidence.TargetAddress, |
| | 127 | 324 | | targetAnchor, |
| | 127 | 325 | | connection.ExitAnchor, |
| | 127 | 326 | | evidence.TargetFootAnchor); |
| | 127 | 327 | | _targetPointOrdinal = _pointCount - 1; |
| | 127 | 328 | | _cost = evidence.Cost; |
| | 127 | 329 | | _completionStatus = NavigationSurfaceEdgeRouteStatus.Passable; |
| | | 330 | | } |
| | 395 | 331 | | return _pointCount == 0 |
| | 395 | 332 | | ? _completionStatus |
| | 395 | 333 | | : NavigationSurfaceEdgeRouteStatus.Point; |
| | | 334 | | } |
| | | 335 | | |
| | | 336 | | private NavigationSurfaceEdgeRouteStatus Complete( |
| | | 337 | | NavigationSurfaceEdgeRouteStatus status) |
| | | 338 | | { |
| | 50 | 339 | | _completionStatus = status; |
| | 50 | 340 | | return status; |
| | | 341 | | } |
| | | 342 | | |
| | | 343 | | private void SetPoints( |
| | | 344 | | NavigationCellAddress address, |
| | | 345 | | Vector3d first, |
| | | 346 | | Vector3d second) |
| | | 347 | | { |
| | 397 | 348 | | if (!_emitPoints) |
| | 304 | 349 | | return; |
| | 93 | 350 | | _pointAddress0 = address; |
| | 93 | 351 | | _pointAddress1 = default; |
| | 93 | 352 | | _pointPosition0 = first; |
| | 93 | 353 | | _pointPosition1 = second; |
| | 93 | 354 | | _pointPosition2 = default; |
| | 93 | 355 | | _pointPosition3 = default; |
| | 93 | 356 | | _pointPosition4 = default; |
| | 93 | 357 | | _pointOrdinal = 0; |
| | 93 | 358 | | _pointCount = 2; |
| | 93 | 359 | | _secondPointAddressOrdinal = 2; |
| | 93 | 360 | | _targetPointOrdinal = -1; |
| | 93 | 361 | | } |
| | | 362 | | |
| | | 363 | | private void SetPoints( |
| | | 364 | | NavigationCellAddress firstAddress, |
| | | 365 | | NavigationCellAddress secondAddress, |
| | | 366 | | Vector3d first, |
| | | 367 | | Vector3d second, |
| | | 368 | | Vector3d third) |
| | | 369 | | { |
| | 17492 | 370 | | if (!_emitPoints) |
| | 16981 | 371 | | return; |
| | 511 | 372 | | _pointAddress0 = firstAddress; |
| | 511 | 373 | | _pointAddress1 = secondAddress; |
| | 511 | 374 | | _pointPosition0 = first; |
| | 511 | 375 | | _pointPosition1 = second; |
| | 511 | 376 | | _pointPosition2 = third; |
| | 511 | 377 | | _pointPosition3 = default; |
| | 511 | 378 | | _pointPosition4 = default; |
| | 511 | 379 | | _pointOrdinal = 0; |
| | 511 | 380 | | _pointCount = 3; |
| | 511 | 381 | | _secondPointAddressOrdinal = 1; |
| | 511 | 382 | | _targetPointOrdinal = -1; |
| | 511 | 383 | | } |
| | | 384 | | |
| | | 385 | | private void AppendPoints( |
| | | 386 | | NavigationCellAddress address, |
| | | 387 | | Vector3d first, |
| | | 388 | | Vector3d second, |
| | | 389 | | Vector3d third) |
| | | 390 | | { |
| | 127 | 391 | | if (!_emitPoints) |
| | 95 | 392 | | return; |
| | 32 | 393 | | _pointAddress1 = address; |
| | 32 | 394 | | _pointPosition2 = first; |
| | 32 | 395 | | _pointPosition3 = second; |
| | 32 | 396 | | _pointPosition4 = third; |
| | 32 | 397 | | _pointCount = 5; |
| | 32 | 398 | | _secondPointAddressOrdinal = 2; |
| | 32 | 399 | | } |
| | | 400 | | } |