| | | 1 | | //======================================================================= |
| | | 2 | | // NavigationTraversalEdgeEnumerator.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; |
| | | 10 | | using NavigationTransitionEdgeStatus = Trailblazer.Pathing.NavigationTraversalEvaluationStatus; |
| | | 11 | | using NavigationVolumeEdgeStatus = Trailblazer.Pathing.NavigationTraversalEvaluationStatus; |
| | | 12 | | |
| | | 13 | | namespace Trailblazer.Pathing; |
| | | 14 | | |
| | | 15 | | /// <summary>Identifies which existing authority produced one evaluated traversal edge.</summary> |
| | | 16 | | internal enum NavigationTraversalEdgeKind : byte |
| | | 17 | | { |
| | | 18 | | Surface = 0, |
| | | 19 | | Volume = 1, |
| | | 20 | | Transition = 2 |
| | | 21 | | } |
| | | 22 | | |
| | | 23 | | /// <summary>Reports bounded progress through canonical outgoing traversal edges.</summary> |
| | | 24 | | internal enum NavigationTraversalEdgeAdvanceStatus : byte |
| | | 25 | | { |
| | | 26 | | Pending = 0, |
| | | 27 | | Edge = 1, |
| | | 28 | | Complete = 2, |
| | | 29 | | Blocked = 3, |
| | | 30 | | BudgetExceeded = 4, |
| | | 31 | | CapacityExceeded = 5, |
| | | 32 | | CostOverflow = 6, |
| | | 33 | | Stale = 7 |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <summary>Dispatches one medium-state's outgoing edges to the existing evaluators.</summary> |
| | | 37 | | internal struct NavigationTraversalEdgeEnumerator |
| | | 38 | | { |
| | | 39 | | private readonly NavigationWorldGraph? _graph; |
| | | 40 | | private readonly NavigationMediumStateRef _source; |
| | | 41 | | private NavigationSurfaceEdgeEnumerator _surfaceEdges; |
| | | 42 | | private TraversalEvaluator _surfaceEvaluator; |
| | | 43 | | private NavigationGraphEdge _surfaceEdge; |
| | | 44 | | private NavigationSurfaceEdgeRouteWork _surfaceRouteWork; |
| | | 45 | | private NavigationVolumeEdgeEvaluator _volumeEvaluator; |
| | | 46 | | private NavigationAutomaticSeamIndex.EndpointEnumerator _volumeSeams; |
| | | 47 | | private NavigationAutomaticSeamRef _volumeSeamLookahead; |
| | | 48 | | private NavigationTransitionPage.Enumerator _transitions; |
| | | 49 | | private NavigationTransitionEdgeEvaluator _transitionEvaluator; |
| | | 50 | | private NavigationPublishedTransition _definitionLookahead; |
| | | 51 | | private int _surfaceOrdinal; |
| | | 52 | | private int _volumeEdgeOrdinal; |
| | | 53 | | private int _transitionOrdinal; |
| | | 54 | | private uint _visitedVolumeDirections; |
| | | 55 | | private bool _surfaceRouteActive; |
| | | 56 | | private int _emittedSurfaceOrdinal; |
| | | 57 | | private bool _hasVolumeSeamLookahead; |
| | | 58 | | private bool _volumeSeamsComplete; |
| | | 59 | | private bool _allowTransitions; |
| | | 60 | | private bool _baseComplete; |
| | | 61 | | private bool _transitionStarted; |
| | | 62 | | private bool _hasPendingTransition; |
| | | 63 | | private bool _pendingIsRule; |
| | | 64 | | private bool _explicitTransitionsComplete; |
| | | 65 | | private bool _hasDefinitionLookahead; |
| | | 66 | | private bool _definitionNeedsDebit; |
| | | 67 | | private bool _hasLastRule; |
| | | 68 | | private NavigationCellAddress _lastRuleTargetAddress; |
| | | 69 | | private TraversalMedium _lastRuleTargetMedium; |
| | | 70 | | private TraversalTransitionType _lastRuleType; |
| | | 71 | | private string _lastRuleId; |
| | | 72 | | private TraversalTransitionRule _scannedRule; |
| | | 73 | | private NavigationMediumStateRef _scannedRuleTarget; |
| | | 74 | | private NavigationCellAddress _scannedRuleAddress; |
| | | 75 | | private int _ruleScanIndex; |
| | | 76 | | private int _ruleContactDirection; |
| | | 77 | | private bool _ruleScanActive; |
| | | 78 | | private bool _hasScannedRule; |
| | | 79 | | |
| | | 80 | | internal NavigationTraversalEdgeEnumerator( |
| | | 81 | | GridWorld world, |
| | | 82 | | NavigationWorldGraph graph, |
| | | 83 | | NavigationMediumStateRef source, |
| | | 84 | | NavigationAgentProfile profile, |
| | | 85 | | NavigationAreaPolicy areaPolicy, |
| | | 86 | | NavigationRayWorkspace workspace, |
| | | 87 | | bool allowTransitions, |
| | | 88 | | int emittedSurfaceOrdinal) |
| | | 89 | | { |
| | 9923 | 90 | | SwiftThrowHelper.ThrowIfNull(world, nameof(world)); |
| | 9923 | 91 | | SwiftThrowHelper.ThrowIfNull(graph, nameof(graph)); |
| | 9923 | 92 | | SwiftThrowHelper.ThrowIfNull(areaPolicy, nameof(areaPolicy)); |
| | 9923 | 93 | | SwiftThrowHelper.ThrowIfNull(workspace, nameof(workspace)); |
| | 9923 | 94 | | bool foundSourceAddress = graph.TryGetNodeAddress( |
| | 9923 | 95 | | source.Node, |
| | 9923 | 96 | | out NavigationCellAddress sourceAddress); |
| | | 97 | | System.Diagnostics.Debug.Assert( |
| | | 98 | | source.IsValid && foundSourceAddress, |
| | | 99 | | "Traversal enumerators own a valid source from their immutable graph."); |
| | 9923 | 100 | | _graph = graph; |
| | 9923 | 101 | | _source = source; |
| | 9923 | 102 | | _surfaceEdges = source.Medium == TraversalMedium.Solid |
| | 9923 | 103 | | ? graph.EnumerateSurfaceEdges(source.Node) |
| | 9923 | 104 | | : default; |
| | 9923 | 105 | | _surfaceEvaluator = source.Medium == TraversalMedium.Solid |
| | 9923 | 106 | | ? new TraversalEvaluator(graph, profile, areaPolicy, TraversalMedium.Solid) |
| | 9923 | 107 | | : default; |
| | 9923 | 108 | | _surfaceEdge = default; |
| | 9923 | 109 | | _surfaceRouteWork = default; |
| | 9923 | 110 | | _volumeEvaluator = source.Medium == TraversalMedium.Gas |
| | 9923 | 111 | | || source.Medium == TraversalMedium.Liquid |
| | 9923 | 112 | | ? new NavigationVolumeEdgeEvaluator( |
| | 9923 | 113 | | world, |
| | 9923 | 114 | | graph, |
| | 9923 | 115 | | profile, |
| | 9923 | 116 | | areaPolicy, |
| | 9923 | 117 | | source.Medium, |
| | 9923 | 118 | | workspace) |
| | 9923 | 119 | | : default; |
| | 9923 | 120 | | _volumeSeams = (source.Medium == TraversalMedium.Gas |
| | 9923 | 121 | | || source.Medium == TraversalMedium.Liquid) |
| | 9923 | 122 | | ? graph.AutomaticSeams.GetActiveEndpointEnumerator(sourceAddress) |
| | 9923 | 123 | | : default; |
| | 9923 | 124 | | _volumeSeamLookahead = default; |
| | 9923 | 125 | | _transitions = allowTransitions |
| | 9923 | 126 | | ? graph.EnumerateOutgoingTransitionCandidates(source) |
| | 9923 | 127 | | : default; |
| | 9923 | 128 | | _transitionEvaluator = allowTransitions |
| | 9923 | 129 | | ? new NavigationTransitionEdgeEvaluator( |
| | 9923 | 130 | | world, |
| | 9923 | 131 | | graph, |
| | 9923 | 132 | | profile, |
| | 9923 | 133 | | areaPolicy, |
| | 9923 | 134 | | workspace) |
| | 9923 | 135 | | : default; |
| | 9923 | 136 | | _definitionLookahead = default; |
| | 9923 | 137 | | _surfaceOrdinal = -1; |
| | 9923 | 138 | | _volumeEdgeOrdinal = -1; |
| | 9923 | 139 | | _transitionOrdinal = -1; |
| | 9923 | 140 | | _visitedVolumeDirections = 0; |
| | 9923 | 141 | | _surfaceRouteActive = false; |
| | 9923 | 142 | | _emittedSurfaceOrdinal = emittedSurfaceOrdinal; |
| | 9923 | 143 | | _hasVolumeSeamLookahead = false; |
| | 9923 | 144 | | _volumeSeamsComplete = source.Medium != TraversalMedium.Gas |
| | 9923 | 145 | | && source.Medium != TraversalMedium.Liquid; |
| | 9923 | 146 | | _allowTransitions = allowTransitions; |
| | 9923 | 147 | | _baseComplete = false; |
| | 9923 | 148 | | _transitionStarted = false; |
| | 9923 | 149 | | _hasPendingTransition = false; |
| | 9923 | 150 | | _pendingIsRule = false; |
| | 9923 | 151 | | _explicitTransitionsComplete = !allowTransitions; |
| | 9923 | 152 | | _hasDefinitionLookahead = false; |
| | 9923 | 153 | | _definitionNeedsDebit = false; |
| | 9923 | 154 | | _hasLastRule = false; |
| | 9923 | 155 | | _lastRuleTargetAddress = default; |
| | 9923 | 156 | | _lastRuleTargetMedium = default; |
| | 9923 | 157 | | _lastRuleType = default; |
| | 9923 | 158 | | _lastRuleId = string.Empty; |
| | 9923 | 159 | | _scannedRule = default; |
| | 9923 | 160 | | _scannedRuleTarget = default; |
| | 9923 | 161 | | _scannedRuleAddress = default; |
| | 9923 | 162 | | _ruleScanIndex = 0; |
| | 9923 | 163 | | _ruleContactDirection = -1; |
| | 9923 | 164 | | _ruleScanActive = false; |
| | 9923 | 165 | | _hasScannedRule = false; |
| | 9923 | 166 | | CurrentTarget = default; |
| | 9923 | 167 | | CurrentKind = default; |
| | 9923 | 168 | | CurrentOrdinal = -1; |
| | 9923 | 169 | | CurrentCost = default; |
| | 9923 | 170 | | CurrentSurfaceEdge = default; |
| | 9923 | 171 | | CurrentVolumeIsShortcut = false; |
| | 9923 | 172 | | CurrentTransitionId = string.Empty; |
| | 9923 | 173 | | CurrentTransitionOwnerMapId = string.Empty; |
| | 9923 | 174 | | CurrentTransitionType = default; |
| | 9923 | 175 | | CurrentTransitionHints = default; |
| | 9923 | 176 | | CurrentTransitionIdentityKind = default; |
| | 9923 | 177 | | CurrentTransitionSourceAction = default; |
| | 9923 | 178 | | CurrentTransitionDestinationAction = default; |
| | 9923 | 179 | | RequiresWorldStamp = source.Medium == TraversalMedium.Gas |
| | 9923 | 180 | | || source.Medium == TraversalMedium.Liquid; |
| | 9923 | 181 | | } |
| | | 182 | | |
| | | 183 | | internal NavigationMediumStateRef CurrentTarget { get; private set; } |
| | | 184 | | |
| | | 185 | | internal NavigationTraversalEdgeKind CurrentKind { get; private set; } |
| | | 186 | | |
| | | 187 | | internal int CurrentOrdinal { get; private set; } |
| | | 188 | | |
| | | 189 | | internal Fixed64 CurrentCost { get; private set; } |
| | | 190 | | |
| | | 191 | | internal NavigationGraphEdge CurrentSurfaceEdge { get; private set; } |
| | | 192 | | |
| | | 193 | | internal bool CurrentVolumeIsShortcut { get; private set; } |
| | | 194 | | |
| | | 195 | | internal string CurrentTransitionId { get; private set; } |
| | | 196 | | |
| | | 197 | | internal string CurrentTransitionOwnerMapId { get; private set; } |
| | | 198 | | |
| | | 199 | | internal TraversalTransitionType CurrentTransitionType { get; private set; } |
| | | 200 | | |
| | | 201 | | internal TraversalTransitionLocomotionHints CurrentTransitionHints { get; private set; } |
| | | 202 | | |
| | | 203 | | internal NavigationTransitionIdentityKind CurrentTransitionIdentityKind { get; private set; } |
| | | 204 | | |
| | | 205 | | internal Vector3d CurrentTransitionSourceAction { get; private set; } |
| | | 206 | | |
| | | 207 | | internal Vector3d CurrentTransitionDestinationAction { get; private set; } |
| | | 208 | | |
| | | 209 | | internal bool RequiresWorldStamp { get; private set; } |
| | | 210 | | |
| | 162 | 211 | | internal bool RequiresConnectionProgress => _surfaceRouteActive |
| | 162 | 212 | | && _surfaceEdge.Kind == NavigationGraphEdgeKind.Explicit; |
| | | 213 | | |
| | 3842 | 214 | | internal bool HasCurrentSurfacePoint => _surfaceRouteWork.HasCurrentPoint; |
| | | 215 | | |
| | | 216 | | internal NavigationAStarGuidePoint CurrentSurfacePoint => |
| | 1815 | 217 | | _surfaceRouteWork.CurrentPoint; |
| | | 218 | | |
| | | 219 | | internal bool CurrentSurfacePointIsTargetFootAnchor => |
| | 1815 | 220 | | _surfaceRouteWork.CurrentPointIsTargetFootAnchor; |
| | | 221 | | |
| | 1814 | 222 | | internal void ConsumeCurrentSurfacePoint() => _surfaceRouteWork.ConsumePoint(); |
| | | 223 | | |
| | | 224 | | internal NavigationTraversalEdgeAdvanceStatus AdvanceOne( |
| | | 225 | | NavigationWorkMeter meter, |
| | | 226 | | NavigationDependencyWorkspace dependencies, |
| | | 227 | | ref int edgeStepRemaining, |
| | | 228 | | ref int connectionStepRemaining) |
| | | 229 | | { |
| | 36076 | 230 | | SwiftThrowHelper.ThrowIfNull(meter, nameof(meter)); |
| | 36076 | 231 | | SwiftThrowHelper.ThrowIfNull(dependencies, nameof(dependencies)); |
| | 36076 | 232 | | SwiftThrowHelper.ThrowIfNegative(edgeStepRemaining, nameof(edgeStepRemaining)); |
| | 36076 | 233 | | SwiftThrowHelper.ThrowIfNegative( |
| | 36076 | 234 | | connectionStepRemaining, |
| | 36076 | 235 | | nameof(connectionStepRemaining)); |
| | | 236 | | System.Diagnostics.Debug.Assert(_graph != null, |
| | | 237 | | "Traversal enumerators must be initialized with an immutable graph source."); |
| | 36076 | 238 | | if (_baseComplete) |
| | 555 | 239 | | return AdvanceTransition(meter, dependencies, ref edgeStepRemaining); |
| | 35521 | 240 | | if (_source.Medium == TraversalMedium.Gas |
| | 35521 | 241 | | || _source.Medium == TraversalMedium.Liquid) |
| | | 242 | | { |
| | 2903 | 243 | | return AdvanceVolume(meter, dependencies, ref edgeStepRemaining); |
| | | 244 | | } |
| | 32618 | 245 | | if (_surfaceRouteActive) |
| | | 246 | | { |
| | 2306 | 247 | | return AdvanceSurfaceRoute( |
| | 2306 | 248 | | meter, |
| | 2306 | 249 | | dependencies, |
| | 2306 | 250 | | ref connectionStepRemaining); |
| | | 251 | | } |
| | | 252 | | |
| | 30312 | 253 | | NavigationSurfaceEdgeAdvanceStatus status = _surfaceEdges.AdvanceOne( |
| | 30312 | 254 | | meter, |
| | 30312 | 255 | | ref edgeStepRemaining); |
| | 30312 | 256 | | if (status == NavigationSurfaceEdgeAdvanceStatus.Complete) |
| | | 257 | | { |
| | 2748 | 258 | | _baseComplete = true; |
| | 2748 | 259 | | return AdvanceTransition(meter, dependencies, ref edgeStepRemaining); |
| | | 260 | | } |
| | 27564 | 261 | | if (status == NavigationSurfaceEdgeAdvanceStatus.Blocked) |
| | | 262 | | { |
| | 161 | 263 | | return edgeStepRemaining == 0 && meter.RemainingEvaluatedEdges > 0 |
| | 161 | 264 | | ? NavigationTraversalEdgeAdvanceStatus.Blocked |
| | 161 | 265 | | : NavigationTraversalEdgeAdvanceStatus.BudgetExceeded; |
| | | 266 | | } |
| | 27403 | 267 | | if (status != NavigationSurfaceEdgeAdvanceStatus.Edge) |
| | 9297 | 268 | | return NavigationTraversalEdgeAdvanceStatus.Pending; |
| | | 269 | | |
| | 18106 | 270 | | _surfaceOrdinal = _surfaceEdges.CurrentOrdinal; |
| | 18106 | 271 | | _surfaceEdge = _surfaceEdges.Current; |
| | 18106 | 272 | | _graph!.TryGetNodeAddress( |
| | 18106 | 273 | | _surfaceEdge.Target, |
| | 18106 | 274 | | out NavigationCellAddress targetAddress); |
| | 18106 | 275 | | if (!dependencies.TryRecordPage( |
| | 18106 | 276 | | targetAddress.MapId, |
| | 18106 | 277 | | _surfaceEdge.Target.CellSlot / NavigationSemanticPage.SlotCount)) |
| | | 278 | | { |
| | 2 | 279 | | return NavigationTraversalEdgeAdvanceStatus.CapacityExceeded; |
| | | 280 | | } |
| | 18104 | 281 | | if (_emittedSurfaceOrdinal >= 0) |
| | | 282 | | { |
| | 980 | 283 | | if (_surfaceOrdinal < _emittedSurfaceOrdinal) |
| | 435 | 284 | | return NavigationTraversalEdgeAdvanceStatus.Pending; |
| | | 285 | | } |
| | 17669 | 286 | | NavigationSurfaceEdgeRouteStatus begin = _surfaceRouteWork.Begin( |
| | 17669 | 287 | | _surfaceEvaluator, |
| | 17669 | 288 | | _source.Node, |
| | 17669 | 289 | | _surfaceEdge, |
| | 17669 | 290 | | emitPoints: _emittedSurfaceOrdinal >= 0); |
| | 17669 | 291 | | _surfaceRouteActive = begin == NavigationSurfaceEdgeRouteStatus.Pending; |
| | 17669 | 292 | | return _surfaceRouteActive |
| | 17669 | 293 | | ? AdvanceSurfaceRoute( |
| | 17669 | 294 | | meter, |
| | 17669 | 295 | | dependencies, |
| | 17669 | 296 | | ref connectionStepRemaining) |
| | 17669 | 297 | | : MapSurfaceRoute(begin); |
| | | 298 | | } |
| | | 299 | | |
| | | 300 | | private NavigationTraversalEdgeAdvanceStatus AdvanceVolume( |
| | | 301 | | NavigationWorkMeter meter, |
| | | 302 | | NavigationDependencyWorkspace dependencies, |
| | | 303 | | ref int edgeStepRemaining) |
| | | 304 | | { |
| | 3075 | 305 | | while (TrySelectNextVolumeCandidate( |
| | 3075 | 306 | | out int directionOrdinal, |
| | 3075 | 307 | | out NavigationMediumStateRef target, |
| | 3075 | 308 | | out bool isPrimary, |
| | 3075 | 309 | | out NavigationAutomaticSeamRef seam, |
| | 3075 | 310 | | out bool hasSeam)) |
| | | 311 | | { |
| | 2673 | 312 | | if (edgeStepRemaining == 0) |
| | | 313 | | { |
| | 25 | 314 | | return meter.RemainingEvaluatedEdges == 0 |
| | 25 | 315 | | ? NavigationTraversalEdgeAdvanceStatus.BudgetExceeded |
| | 25 | 316 | | : NavigationTraversalEdgeAdvanceStatus.Blocked; |
| | | 317 | | } |
| | 2648 | 318 | | if (!meter.TryConsumeEvaluatedEdges(1)) |
| | | 319 | | { |
| | 1 | 320 | | return NavigationTraversalEdgeAdvanceStatus.BudgetExceeded; |
| | | 321 | | } |
| | 2647 | 322 | | meter.RecordVolumeCandidate(isPrimary); |
| | 2647 | 323 | | edgeStepRemaining--; |
| | 2647 | 324 | | if (hasSeam) |
| | | 325 | | { |
| | 74 | 326 | | _volumeSeamLookahead = default; |
| | 74 | 327 | | _hasVolumeSeamLookahead = false; |
| | | 328 | | } |
| | | 329 | | else |
| | | 330 | | { |
| | 2573 | 331 | | _visitedVolumeDirections |= 1U << directionOrdinal; |
| | | 332 | | } |
| | 2647 | 333 | | _volumeEdgeOrdinal++; |
| | 2647 | 334 | | NavigationVolumeEdgeStatus status = _volumeEvaluator.Evaluate( |
| | 2647 | 335 | | _source, |
| | 2647 | 336 | | target, |
| | 2647 | 337 | | isPrimary, |
| | 2647 | 338 | | seam, |
| | 2647 | 339 | | hasSeam, |
| | 2647 | 340 | | meter, |
| | 2647 | 341 | | dependencies, |
| | 2647 | 342 | | out Fixed64 cost); |
| | 2647 | 343 | | if (status == NavigationVolumeEdgeStatus.BudgetExceeded) |
| | 2 | 344 | | return NavigationTraversalEdgeAdvanceStatus.BudgetExceeded; |
| | 2645 | 345 | | if (status == NavigationVolumeEdgeStatus.CapacityExceeded) |
| | 2 | 346 | | return NavigationTraversalEdgeAdvanceStatus.CapacityExceeded; |
| | 2643 | 347 | | if (status == NavigationVolumeEdgeStatus.CostOverflow) |
| | 2 | 348 | | return NavigationTraversalEdgeAdvanceStatus.CostOverflow; |
| | 2641 | 349 | | if (status == NavigationVolumeEdgeStatus.Stale) |
| | 4 | 350 | | return NavigationTraversalEdgeAdvanceStatus.Stale; |
| | 2637 | 351 | | if (status != NavigationVolumeEdgeStatus.Passable) |
| | | 352 | | continue; |
| | | 353 | | |
| | 2465 | 354 | | CurrentTarget = target; |
| | 2465 | 355 | | CurrentKind = NavigationTraversalEdgeKind.Volume; |
| | 2465 | 356 | | CurrentSurfaceEdge = default; |
| | 2465 | 357 | | CurrentVolumeIsShortcut = !isPrimary; |
| | 2465 | 358 | | CurrentOrdinal = _volumeEdgeOrdinal; |
| | 2465 | 359 | | CurrentCost = cost; |
| | 2465 | 360 | | return NavigationTraversalEdgeAdvanceStatus.Edge; |
| | | 361 | | } |
| | 402 | 362 | | _baseComplete = true; |
| | 402 | 363 | | return AdvanceTransition(meter, dependencies, ref edgeStepRemaining); |
| | | 364 | | } |
| | | 365 | | |
| | | 366 | | private NavigationTraversalEdgeAdvanceStatus AdvanceTransition( |
| | | 367 | | NavigationWorkMeter meter, |
| | | 368 | | NavigationDependencyWorkspace dependencies, |
| | | 369 | | ref int edgeStepRemaining) |
| | | 370 | | { |
| | 3705 | 371 | | if (!_allowTransitions) |
| | 2939 | 372 | | return Complete(); |
| | 766 | 373 | | if (!_transitionStarted) |
| | | 374 | | { |
| | 211 | 375 | | dependencies.RecordTransitionDependency(); |
| | 211 | 376 | | _graph!.TryGetNodeAddress( |
| | 211 | 377 | | _source.Node, |
| | 211 | 378 | | out NavigationCellAddress sourceAddress); |
| | 211 | 379 | | if (!dependencies.TryRecordPage( |
| | 211 | 380 | | sourceAddress.MapId, |
| | 211 | 381 | | _source.Node.CellSlot / NavigationSemanticPage.SlotCount)) |
| | | 382 | | { |
| | 1 | 383 | | return NavigationTraversalEdgeAdvanceStatus.CapacityExceeded; |
| | | 384 | | } |
| | 210 | 385 | | _transitionStarted = true; |
| | | 386 | | } |
| | | 387 | | |
| | 765 | 388 | | if (!_hasPendingTransition) |
| | | 389 | | { |
| | 543 | 390 | | NavigationTraversalEdgeAdvanceStatus prepare = PrepareNextTransition( |
| | 543 | 391 | | meter, |
| | 543 | 392 | | ref edgeStepRemaining); |
| | 543 | 393 | | if (prepare != NavigationTraversalEdgeAdvanceStatus.Pending |
| | 543 | 394 | | || !_hasPendingTransition) |
| | | 395 | | { |
| | 326 | 396 | | return prepare; |
| | | 397 | | } |
| | 217 | 398 | | return NavigationTraversalEdgeAdvanceStatus.Pending; |
| | | 399 | | } |
| | | 400 | | |
| | 222 | 401 | | if (edgeStepRemaining == 0) |
| | | 402 | | { |
| | 6 | 403 | | return meter.RemainingTransitionPairs == 0 |
| | 6 | 404 | | ? NavigationTraversalEdgeAdvanceStatus.BudgetExceeded |
| | 6 | 405 | | : NavigationTraversalEdgeAdvanceStatus.Blocked; |
| | | 406 | | } |
| | 216 | 407 | | if (!meter.TryConsumeTransitionPairs(1)) |
| | 1 | 408 | | return NavigationTraversalEdgeAdvanceStatus.BudgetExceeded; |
| | 215 | 409 | | edgeStepRemaining--; |
| | 215 | 410 | | NavigationPublishedTransition transition = _definitionLookahead; |
| | 215 | 411 | | TraversalTransitionRule rule = _scannedRule; |
| | 215 | 412 | | NavigationMediumStateRef ruleTarget = _scannedRuleTarget; |
| | 215 | 413 | | bool isRule = _pendingIsRule; |
| | 215 | 414 | | _hasPendingTransition = false; |
| | 215 | 415 | | _pendingIsRule = false; |
| | 215 | 416 | | _hasScannedRule = false; |
| | 215 | 417 | | _scannedRule = default; |
| | 215 | 418 | | _scannedRuleTarget = default; |
| | 215 | 419 | | _scannedRuleAddress = default; |
| | 215 | 420 | | if (!isRule) |
| | | 421 | | { |
| | 101 | 422 | | _definitionLookahead = default; |
| | 101 | 423 | | _hasDefinitionLookahead = false; |
| | | 424 | | } |
| | 215 | 425 | | _transitionOrdinal++; |
| | | 426 | | NavigationTransitionEdgeStatus status; |
| | | 427 | | NavigationMediumStateRef target; |
| | | 428 | | NavigationTransitionEdgeEvidence evidence; |
| | 215 | 429 | | if (isRule) |
| | | 430 | | { |
| | 114 | 431 | | target = ruleTarget; |
| | 114 | 432 | | status = _transitionEvaluator.EvaluateRule( |
| | 114 | 433 | | _source, |
| | 114 | 434 | | target, |
| | 114 | 435 | | rule, |
| | 114 | 436 | | meter, |
| | 114 | 437 | | dependencies, |
| | 114 | 438 | | out evidence); |
| | | 439 | | } |
| | | 440 | | else |
| | | 441 | | { |
| | 101 | 442 | | status = _transitionEvaluator.EvaluateDefinition( |
| | 101 | 443 | | _source, |
| | 101 | 444 | | transition, |
| | 101 | 445 | | meter, |
| | 101 | 446 | | dependencies, |
| | 101 | 447 | | out target, |
| | 101 | 448 | | out evidence); |
| | | 449 | | } |
| | 215 | 450 | | if (status == NavigationTransitionEdgeStatus.BudgetExceeded) |
| | 2 | 451 | | return NavigationTraversalEdgeAdvanceStatus.BudgetExceeded; |
| | 213 | 452 | | if (status == NavigationTransitionEdgeStatus.CapacityExceeded) |
| | 2 | 453 | | return NavigationTraversalEdgeAdvanceStatus.CapacityExceeded; |
| | 211 | 454 | | if (status == NavigationTransitionEdgeStatus.CostOverflow) |
| | 1 | 455 | | return NavigationTraversalEdgeAdvanceStatus.CostOverflow; |
| | 210 | 456 | | if (status == NavigationTransitionEdgeStatus.Stale) |
| | 1 | 457 | | return NavigationTraversalEdgeAdvanceStatus.Stale; |
| | 209 | 458 | | if (status != NavigationTransitionEdgeStatus.Passable) |
| | 62 | 459 | | return NavigationTraversalEdgeAdvanceStatus.Pending; |
| | | 460 | | |
| | 147 | 461 | | CurrentTarget = target; |
| | 147 | 462 | | CurrentKind = NavigationTraversalEdgeKind.Transition; |
| | 147 | 463 | | CurrentSurfaceEdge = default; |
| | 147 | 464 | | CurrentVolumeIsShortcut = false; |
| | 147 | 465 | | CurrentOrdinal = (_source.Medium == TraversalMedium.Solid |
| | 147 | 466 | | ? _surfaceOrdinal |
| | 147 | 467 | | : _volumeEdgeOrdinal) + 1 + _transitionOrdinal; |
| | 147 | 468 | | CurrentCost = evidence.Cost; |
| | 147 | 469 | | CurrentTransitionId = isRule ? rule.Id : transition.Owner.TransitionId; |
| | 147 | 470 | | CurrentTransitionOwnerMapId = isRule ? string.Empty : transition.Owner.MapId; |
| | 147 | 471 | | CurrentTransitionType = isRule ? rule.Type : transition.Definition.Type; |
| | 147 | 472 | | CurrentTransitionHints = isRule |
| | 147 | 473 | | ? rule.LocomotionHints |
| | 147 | 474 | | : transition.Definition.LocomotionHints; |
| | 147 | 475 | | CurrentTransitionIdentityKind = isRule |
| | 147 | 476 | | ? NavigationTransitionIdentityKind.Rule |
| | 147 | 477 | | : NavigationTransitionIdentityKind.Definition; |
| | 147 | 478 | | CurrentTransitionSourceAction = evidence.SourceAction; |
| | 147 | 479 | | CurrentTransitionDestinationAction = evidence.DestinationAction; |
| | 147 | 480 | | return NavigationTraversalEdgeAdvanceStatus.Edge; |
| | | 481 | | } |
| | | 482 | | |
| | | 483 | | private NavigationTraversalEdgeAdvanceStatus PrepareNextTransition( |
| | | 484 | | NavigationWorkMeter meter, |
| | | 485 | | ref int edgeStepRemaining) |
| | | 486 | | { |
| | 543 | 487 | | bool hasDefinition = _hasDefinitionLookahead; |
| | 543 | 488 | | NavigationPublishedTransition definition = _definitionLookahead; |
| | 543 | 489 | | if (!hasDefinition && !_explicitTransitionsComplete) |
| | | 490 | | { |
| | 252 | 491 | | if (_transitions.MoveNext()) |
| | | 492 | | { |
| | 104 | 493 | | _definitionLookahead = _transitions.Current; |
| | 104 | 494 | | MarkWorldDependency( |
| | 104 | 495 | | _definitionLookahead.Definition.DestinationMedium); |
| | 104 | 496 | | _hasDefinitionLookahead = true; |
| | 104 | 497 | | _definitionNeedsDebit = true; |
| | 104 | 498 | | hasDefinition = true; |
| | 104 | 499 | | definition = _definitionLookahead; |
| | | 500 | | } |
| | | 501 | | else |
| | | 502 | | { |
| | 148 | 503 | | _explicitTransitionsComplete = true; |
| | | 504 | | } |
| | | 505 | | } |
| | 543 | 506 | | if (_definitionNeedsDebit) |
| | | 507 | | { |
| | 105 | 508 | | if (!TryConsumeTransitionCandidate( |
| | 105 | 509 | | meter, |
| | 105 | 510 | | ref edgeStepRemaining, |
| | 105 | 511 | | out NavigationTraversalEdgeAdvanceStatus blocked)) |
| | | 512 | | { |
| | 3 | 513 | | return blocked; |
| | | 514 | | } |
| | 102 | 515 | | _definitionNeedsDebit = false; |
| | | 516 | | } |
| | 540 | 517 | | if (!_ruleScanActive) |
| | | 518 | | { |
| | 332 | 519 | | _ruleScanActive = true; |
| | 332 | 520 | | _ruleScanIndex = 0; |
| | 332 | 521 | | _hasScannedRule = false; |
| | 332 | 522 | | _scannedRule = default; |
| | 332 | 523 | | _scannedRuleTarget = default; |
| | 332 | 524 | | _scannedRuleAddress = default; |
| | 332 | 525 | | _ruleContactDirection = -1; |
| | | 526 | | } |
| | 1645 | 527 | | while (_ruleScanIndex < _graph!.TransitionRules.Count) |
| | | 528 | | { |
| | 1315 | 529 | | TraversalTransitionRule candidate = _graph.TransitionRules[_ruleScanIndex]; |
| | 1315 | 530 | | MarkWorldDependency(candidate.DestinationMedium); |
| | 1315 | 531 | | if (_ruleContactDirection < 0) |
| | | 532 | | { |
| | 325 | 533 | | if (!TryConsumeTransitionCandidate( |
| | 325 | 534 | | meter, |
| | 325 | 535 | | ref edgeStepRemaining, |
| | 325 | 536 | | out NavigationTraversalEdgeAdvanceStatus blocked)) |
| | | 537 | | { |
| | 25 | 538 | | return blocked; |
| | | 539 | | } |
| | 300 | 540 | | if (candidate.SourceMedium != _source.Medium) |
| | | 541 | | { |
| | 75 | 542 | | CompleteRuleContactScan(); |
| | 75 | 543 | | continue; |
| | | 544 | | } |
| | 225 | 545 | | _ruleContactDirection = 0; |
| | | 546 | | } |
| | 1215 | 547 | | if (candidate.Scope == TraversalTransitionRuleScope.SameCell) |
| | | 548 | | { |
| | 123 | 549 | | if (!TryConsumeTransitionCandidate( |
| | 123 | 550 | | meter, |
| | 123 | 551 | | ref edgeStepRemaining, |
| | 123 | 552 | | out NavigationTraversalEdgeAdvanceStatus blocked)) |
| | | 553 | | { |
| | 16 | 554 | | return blocked; |
| | | 555 | | } |
| | 107 | 556 | | bool foundCandidateAddress = _graph.TryGetNodeAddress( |
| | 107 | 557 | | _source.Node, |
| | 107 | 558 | | out NavigationCellAddress candidateAddress); |
| | | 559 | | System.Diagnostics.Debug.Assert(foundCandidateAddress, |
| | | 560 | | "Same-cell rules retain their immutable graph source address."); |
| | 107 | 561 | | ConsiderRuleCandidate( |
| | 107 | 562 | | candidate, |
| | 107 | 563 | | new NavigationMediumStateRef( |
| | 107 | 564 | | _source.Node, |
| | 107 | 565 | | candidate.DestinationMedium), |
| | 107 | 566 | | candidateAddress, |
| | 107 | 567 | | ref _hasScannedRule, |
| | 107 | 568 | | ref _scannedRule, |
| | 107 | 569 | | ref _scannedRuleTarget, |
| | 107 | 570 | | ref _scannedRuleAddress); |
| | 107 | 571 | | CompleteRuleContactScan(); |
| | 107 | 572 | | continue; |
| | | 573 | | } |
| | 1092 | 574 | | int primaryCount = _graph.GetPrimaryDirectionCount(_source.Node); |
| | 1092 | 575 | | if (_ruleContactDirection < primaryCount) |
| | | 576 | | { |
| | 810 | 577 | | if (!TryConsumeTransitionCandidate( |
| | 810 | 578 | | meter, |
| | 810 | 579 | | ref edgeStepRemaining, |
| | 810 | 580 | | out NavigationTraversalEdgeAdvanceStatus blocked)) |
| | | 581 | | { |
| | 102 | 582 | | return blocked; |
| | | 583 | | } |
| | 708 | 584 | | int direction = _ruleContactDirection++; |
| | 708 | 585 | | if (_graph.TryGetPrimaryNeighbor( |
| | 708 | 586 | | _source.Node, |
| | 708 | 587 | | direction, |
| | 708 | 588 | | out NavigationNodeRef candidateNode)) |
| | | 589 | | { |
| | 90 | 590 | | bool foundCandidateAddress = _graph.TryGetNodeAddress( |
| | 90 | 591 | | candidateNode, |
| | 90 | 592 | | out NavigationCellAddress candidateAddress); |
| | | 593 | | System.Diagnostics.Debug.Assert(foundCandidateAddress, |
| | | 594 | | "Primary neighbors retain an address in their immutable graph."); |
| | 90 | 595 | | ConsiderRuleCandidate( |
| | 90 | 596 | | candidate, |
| | 90 | 597 | | new NavigationMediumStateRef( |
| | 90 | 598 | | candidateNode, |
| | 90 | 599 | | candidate.DestinationMedium), |
| | 90 | 600 | | candidateAddress, |
| | 90 | 601 | | ref _hasScannedRule, |
| | 90 | 602 | | ref _scannedRule, |
| | 90 | 603 | | ref _scannedRuleTarget, |
| | 90 | 604 | | ref _scannedRuleAddress); |
| | | 605 | | } |
| | 90 | 606 | | continue; |
| | | 607 | | } |
| | 282 | 608 | | if (_ruleContactDirection == primaryCount) |
| | | 609 | | { |
| | 118 | 610 | | bool foundSeamSource = _graph.TryGetNodeAddress( |
| | 118 | 611 | | _source.Node, |
| | 118 | 612 | | out NavigationCellAddress seamSourceAddress); |
| | | 613 | | System.Diagnostics.Debug.Assert(foundSeamSource, |
| | | 614 | | "Volume rule scans retain their immutable graph source address."); |
| | 118 | 615 | | _volumeSeams = _graph.AutomaticSeams.GetActiveEndpointEnumerator( |
| | 118 | 616 | | seamSourceAddress); |
| | 118 | 617 | | _volumeSeamLookahead = default; |
| | 118 | 618 | | _hasVolumeSeamLookahead = false; |
| | 118 | 619 | | _volumeSeamsComplete = false; |
| | 118 | 620 | | _ruleContactDirection++; |
| | | 621 | | } |
| | | 622 | | System.Diagnostics.Debug.Assert(!_volumeSeamsComplete, |
| | | 623 | | "Completed volume-seam contact scans advance to the next transition rule immediately."); |
| | 282 | 624 | | if (edgeStepRemaining == 0) |
| | 66 | 625 | | return NavigationTraversalEdgeAdvanceStatus.Blocked; |
| | 216 | 626 | | if (!_volumeSeams.MoveNext()) |
| | | 627 | | { |
| | 116 | 628 | | _volumeSeamsComplete = true; |
| | 116 | 629 | | CompleteRuleContactScan(); |
| | 116 | 630 | | continue; |
| | | 631 | | } |
| | 100 | 632 | | if (!TryConsumeTransitionCandidate( |
| | 100 | 633 | | meter, |
| | 100 | 634 | | ref edgeStepRemaining, |
| | 100 | 635 | | out NavigationTraversalEdgeAdvanceStatus seamBlocked)) |
| | | 636 | | { |
| | 1 | 637 | | return seamBlocked; |
| | | 638 | | } |
| | 99 | 639 | | NavigationAutomaticSeamRef seam = _volumeSeams.Current; |
| | 99 | 640 | | bool foundSeamTarget = _graph.TryGetNodeRef( |
| | 99 | 641 | | seam.Destination, |
| | 99 | 642 | | out NavigationNodeRef seamTarget); |
| | | 643 | | System.Diagnostics.Debug.Assert(foundSeamTarget, |
| | | 644 | | "Active seam rows reference published graph nodes."); |
| | 99 | 645 | | ConsiderRuleCandidate( |
| | 99 | 646 | | candidate, |
| | 99 | 647 | | new NavigationMediumStateRef( |
| | 99 | 648 | | seamTarget, |
| | 99 | 649 | | candidate.DestinationMedium), |
| | 99 | 650 | | seam.Destination, |
| | 99 | 651 | | ref _hasScannedRule, |
| | 99 | 652 | | ref _scannedRule, |
| | 99 | 653 | | ref _scannedRuleTarget, |
| | 99 | 654 | | ref _scannedRuleAddress); |
| | | 655 | | } |
| | | 656 | | |
| | 330 | 657 | | bool hasRule = _hasScannedRule; |
| | 330 | 658 | | TraversalTransitionRule selectedRule = _scannedRule; |
| | 330 | 659 | | NavigationCellAddress selectedRuleAddress = _scannedRuleAddress; |
| | 330 | 660 | | _ruleScanActive = false; |
| | 330 | 661 | | _ruleScanIndex = 0; |
| | 330 | 662 | | _ruleContactDirection = -1; |
| | | 663 | | |
| | 330 | 664 | | if (!hasDefinition && !hasRule) |
| | 113 | 665 | | return Complete(); |
| | 217 | 666 | | bool selectRule = hasRule && (!hasDefinition |
| | 217 | 667 | | || CompareTransitionKey( |
| | 217 | 668 | | selectedRuleAddress, |
| | 217 | 669 | | selectedRule.DestinationMedium, |
| | 217 | 670 | | selectedRule.Type, |
| | 217 | 671 | | NavigationTransitionIdentityKind.Rule, |
| | 217 | 672 | | selectedRule.Id, |
| | 217 | 673 | | definition.Definition.Destination, |
| | 217 | 674 | | definition.Definition.DestinationMedium, |
| | 217 | 675 | | definition.Definition.Type, |
| | 217 | 676 | | NavigationTransitionIdentityKind.Definition, |
| | 217 | 677 | | definition.Owner.TransitionId) < 0); |
| | 217 | 678 | | _hasPendingTransition = true; |
| | 217 | 679 | | _pendingIsRule = selectRule; |
| | 217 | 680 | | if (selectRule) |
| | | 681 | | { |
| | 116 | 682 | | _hasLastRule = true; |
| | 116 | 683 | | _lastRuleTargetAddress = selectedRuleAddress; |
| | 116 | 684 | | _lastRuleTargetMedium = selectedRule.DestinationMedium; |
| | 116 | 685 | | _lastRuleType = selectedRule.Type; |
| | 116 | 686 | | _lastRuleId = selectedRule.Id; |
| | | 687 | | } |
| | | 688 | | else |
| | | 689 | | { |
| | 101 | 690 | | _definitionLookahead = definition; |
| | | 691 | | } |
| | 217 | 692 | | return NavigationTraversalEdgeAdvanceStatus.Pending; |
| | | 693 | | } |
| | | 694 | | |
| | | 695 | | private void CompleteRuleContactScan() |
| | | 696 | | { |
| | 298 | 697 | | _ruleScanIndex++; |
| | 298 | 698 | | _ruleContactDirection = -1; |
| | 298 | 699 | | _volumeSeamLookahead = default; |
| | 298 | 700 | | _hasVolumeSeamLookahead = false; |
| | 298 | 701 | | _volumeSeamsComplete = true; |
| | 298 | 702 | | } |
| | | 703 | | |
| | | 704 | | private void ConsiderRuleCandidate( |
| | | 705 | | TraversalTransitionRule candidate, |
| | | 706 | | NavigationMediumStateRef candidateTarget, |
| | | 707 | | NavigationCellAddress candidateAddress, |
| | | 708 | | ref bool hasRule, |
| | | 709 | | ref TraversalTransitionRule selectedRule, |
| | | 710 | | ref NavigationMediumStateRef selectedRuleTarget, |
| | | 711 | | ref NavigationCellAddress selectedRuleAddress) |
| | | 712 | | { |
| | 296 | 713 | | if (!IsAfterLastRule(candidateAddress, candidate) |
| | 296 | 714 | | || (hasRule |
| | 296 | 715 | | && CompareTransitionKey( |
| | 296 | 716 | | selectedRuleAddress, |
| | 296 | 717 | | selectedRule.DestinationMedium, |
| | 296 | 718 | | selectedRule.Type, |
| | 296 | 719 | | NavigationTransitionIdentityKind.Rule, |
| | 296 | 720 | | selectedRule.Id, |
| | 296 | 721 | | candidateAddress, |
| | 296 | 722 | | candidate.DestinationMedium, |
| | 296 | 723 | | candidate.Type, |
| | 296 | 724 | | NavigationTransitionIdentityKind.Rule, |
| | 296 | 725 | | candidate.Id) <= 0)) |
| | | 726 | | { |
| | 161 | 727 | | return; |
| | | 728 | | } |
| | 135 | 729 | | hasRule = true; |
| | 135 | 730 | | selectedRule = candidate; |
| | 135 | 731 | | selectedRuleTarget = candidateTarget; |
| | 135 | 732 | | selectedRuleAddress = candidateAddress; |
| | 135 | 733 | | } |
| | | 734 | | |
| | | 735 | | private bool IsAfterLastRule( |
| | | 736 | | NavigationCellAddress target, |
| | 296 | 737 | | TraversalTransitionRule rule) => !_hasLastRule |
| | 296 | 738 | | || CompareTransitionKey( |
| | 296 | 739 | | _lastRuleTargetAddress, |
| | 296 | 740 | | _lastRuleTargetMedium, |
| | 296 | 741 | | _lastRuleType, |
| | 296 | 742 | | NavigationTransitionIdentityKind.Rule, |
| | 296 | 743 | | _lastRuleId, |
| | 296 | 744 | | target, |
| | 296 | 745 | | rule.DestinationMedium, |
| | 296 | 746 | | rule.Type, |
| | 296 | 747 | | NavigationTransitionIdentityKind.Rule, |
| | 296 | 748 | | rule.Id) < 0; |
| | | 749 | | |
| | | 750 | | internal static int CompareTransitionKey( |
| | | 751 | | NavigationCellAddress leftAddress, |
| | | 752 | | TraversalMedium leftMedium, |
| | | 753 | | TraversalTransitionType leftType, |
| | | 754 | | NavigationTransitionIdentityKind leftKind, |
| | | 755 | | string leftId, |
| | | 756 | | NavigationCellAddress rightAddress, |
| | | 757 | | TraversalMedium rightMedium, |
| | | 758 | | TraversalTransitionType rightType, |
| | | 759 | | NavigationTransitionIdentityKind rightKind, |
| | | 760 | | string rightId) |
| | | 761 | | { |
| | 290 | 762 | | int comparison = leftAddress.CompareTo(rightAddress); |
| | 290 | 763 | | if (comparison != 0) |
| | 162 | 764 | | return comparison; |
| | 128 | 765 | | comparison = ((int)leftMedium).CompareTo((int)rightMedium); |
| | 128 | 766 | | if (comparison != 0) |
| | 3 | 767 | | return comparison; |
| | 125 | 768 | | comparison = ((int)leftType).CompareTo((int)rightType); |
| | 125 | 769 | | if (comparison != 0) |
| | 20 | 770 | | return comparison; |
| | 105 | 771 | | comparison = ((int)leftKind).CompareTo((int)rightKind); |
| | 105 | 772 | | return comparison != 0 ? comparison : string.CompareOrdinal(leftId, rightId); |
| | | 773 | | } |
| | | 774 | | |
| | | 775 | | internal static bool TryConsumeTransitionCandidate( |
| | | 776 | | NavigationWorkMeter meter, |
| | | 777 | | ref int edgeStepRemaining, |
| | | 778 | | out NavigationTraversalEdgeAdvanceStatus blocked) |
| | | 779 | | { |
| | 1784 | 780 | | if (edgeStepRemaining == 0) |
| | | 781 | | { |
| | 189 | 782 | | blocked = meter.RemainingTransitionCandidates == 0 |
| | 189 | 783 | | ? NavigationTraversalEdgeAdvanceStatus.BudgetExceeded |
| | 189 | 784 | | : NavigationTraversalEdgeAdvanceStatus.Blocked; |
| | 189 | 785 | | return false; |
| | | 786 | | } |
| | 1595 | 787 | | if (!meter.TryConsumeTransitionCandidates(1)) |
| | | 788 | | { |
| | 4 | 789 | | blocked = NavigationTraversalEdgeAdvanceStatus.BudgetExceeded; |
| | 4 | 790 | | return false; |
| | | 791 | | } |
| | 1591 | 792 | | edgeStepRemaining--; |
| | 1591 | 793 | | blocked = default; |
| | 1591 | 794 | | return true; |
| | | 795 | | } |
| | | 796 | | |
| | | 797 | | private bool TrySelectNextVolumeCandidate( |
| | | 798 | | out int directionOrdinal, |
| | | 799 | | out NavigationMediumStateRef target, |
| | | 800 | | out bool isPrimary, |
| | | 801 | | out NavigationAutomaticSeamRef seam, |
| | | 802 | | out bool hasSeam) |
| | | 803 | | { |
| | 3075 | 804 | | directionOrdinal = -1; |
| | 3075 | 805 | | target = default; |
| | 3075 | 806 | | isPrimary = false; |
| | 3075 | 807 | | seam = default; |
| | 3075 | 808 | | hasSeam = false; |
| | 3075 | 809 | | NavigationCellAddress selectedAddress = default; |
| | 3075 | 810 | | int directionCount = _graph!.GetCompleteDirectionCount(_source.Node); |
| | 151890 | 811 | | for (int candidateOrdinal = 0; candidateOrdinal < directionCount; candidateOrdinal++) |
| | | 812 | | { |
| | 72870 | 813 | | if ((_visitedVolumeDirections & (1U << candidateOrdinal)) != 0 |
| | 72870 | 814 | | || !_graph.TryGetCompleteNeighbor( |
| | 72870 | 815 | | _source.Node, |
| | 72870 | 816 | | candidateOrdinal, |
| | 72870 | 817 | | out NavigationNodeRef candidateNode, |
| | 72870 | 818 | | out bool candidateIsPrimary)) |
| | | 819 | | { |
| | | 820 | | continue; |
| | | 821 | | } |
| | 16762 | 822 | | bool foundCandidateAddress = _graph.TryGetNodeAddress( |
| | 16762 | 823 | | candidateNode, |
| | 16762 | 824 | | out NavigationCellAddress candidateAddress); |
| | | 825 | | System.Diagnostics.Debug.Assert(foundCandidateAddress, |
| | | 826 | | "Complete neighbors retain an address in their immutable graph."); |
| | 16762 | 827 | | if (directionOrdinal >= 0 |
| | 16762 | 828 | | && selectedAddress.CompareTo(candidateAddress) <= 0) |
| | | 829 | | { |
| | | 830 | | continue; |
| | | 831 | | } |
| | | 832 | | |
| | 6267 | 833 | | directionOrdinal = candidateOrdinal; |
| | 6267 | 834 | | target = new NavigationMediumStateRef(candidateNode, _source.Medium); |
| | 6267 | 835 | | isPrimary = candidateIsPrimary; |
| | 6267 | 836 | | selectedAddress = candidateAddress; |
| | | 837 | | } |
| | | 838 | | |
| | 3075 | 839 | | if (!_hasVolumeSeamLookahead && !_volumeSeamsComplete) |
| | | 840 | | { |
| | 799 | 841 | | if (_volumeSeams.MoveNext()) |
| | | 842 | | { |
| | 74 | 843 | | _volumeSeamLookahead = _volumeSeams.Current; |
| | 74 | 844 | | _hasVolumeSeamLookahead = true; |
| | | 845 | | } |
| | | 846 | | else |
| | | 847 | | { |
| | 725 | 848 | | _volumeSeamsComplete = true; |
| | | 849 | | } |
| | | 850 | | } |
| | 3075 | 851 | | if (_hasVolumeSeamLookahead) |
| | | 852 | | { |
| | 124 | 853 | | bool foundSeamTarget = _graph.TryGetNodeRef( |
| | 124 | 854 | | _volumeSeamLookahead.Destination, |
| | 124 | 855 | | out NavigationNodeRef seamTarget); |
| | | 856 | | System.Diagnostics.Debug.Assert(foundSeamTarget, |
| | | 857 | | "Active seam rows reference published graph nodes."); |
| | 124 | 858 | | if (directionOrdinal < 0 |
| | 124 | 859 | | || _volumeSeamLookahead.Destination.CompareTo(selectedAddress) < 0) |
| | | 860 | | { |
| | 96 | 861 | | directionOrdinal = 0; |
| | 96 | 862 | | target = new NavigationMediumStateRef(seamTarget, _source.Medium); |
| | 96 | 863 | | isPrimary = true; |
| | 96 | 864 | | seam = _volumeSeamLookahead; |
| | 96 | 865 | | hasSeam = true; |
| | | 866 | | } |
| | | 867 | | } |
| | 3075 | 868 | | return directionOrdinal >= 0; |
| | | 869 | | } |
| | | 870 | | |
| | | 871 | | private NavigationTraversalEdgeAdvanceStatus AdvanceSurfaceRoute( |
| | | 872 | | NavigationWorkMeter meter, |
| | | 873 | | NavigationDependencyWorkspace dependencies, |
| | | 874 | | ref int connectionStepRemaining) |
| | | 875 | | { |
| | 19971 | 876 | | NavigationSurfaceEdgeRouteStatus status = _surfaceRouteWork.Advance( |
| | 19971 | 877 | | meter, |
| | 19971 | 878 | | ref connectionStepRemaining); |
| | 19971 | 879 | | if (_surfaceRouteWork.TryTakeDependencyNode(out NavigationNodeRef dependencyNode)) |
| | | 880 | | { |
| | 402 | 881 | | bool foundDependencyAddress = _graph!.TryGetNodeAddress( |
| | 402 | 882 | | dependencyNode, |
| | 402 | 883 | | out NavigationCellAddress dependencyAddress); |
| | | 884 | | System.Diagnostics.Debug.Assert(foundDependencyAddress, |
| | | 885 | | "Explicit route dependencies are nodes in their immutable graph."); |
| | 402 | 886 | | if ((!dependencyNode.Equals(_surfaceEdge.Target) |
| | 402 | 887 | | && _graph.TryGetSurfaceComponent( |
| | 402 | 888 | | dependencyAddress, |
| | 402 | 889 | | TraversalMedium.Solid, |
| | 402 | 890 | | out NavigationSurfaceComponentKey dependencyComponent, |
| | 402 | 891 | | out _) |
| | 402 | 892 | | && !dependencies.TryRecordComponent(dependencyComponent)) |
| | 402 | 893 | | || !dependencies.TryRecordPage( |
| | 402 | 894 | | dependencyAddress.MapId, |
| | 402 | 895 | | dependencyNode.CellSlot / NavigationSemanticPage.SlotCount)) |
| | | 896 | | { |
| | 2 | 897 | | _surfaceRouteActive = false; |
| | 2 | 898 | | return NavigationTraversalEdgeAdvanceStatus.CapacityExceeded; |
| | | 899 | | } |
| | | 900 | | } |
| | 19969 | 901 | | if (status == NavigationSurfaceEdgeRouteStatus.Point) |
| | 2089 | 902 | | return NavigationTraversalEdgeAdvanceStatus.Pending; |
| | 17880 | 903 | | if (status == NavigationSurfaceEdgeRouteStatus.Pending) |
| | | 904 | | { |
| | 218 | 905 | | return connectionStepRemaining == 0 |
| | 218 | 906 | | && meter.RemainingConnectionLegs > 0 |
| | 218 | 907 | | ? NavigationTraversalEdgeAdvanceStatus.Blocked |
| | 218 | 908 | | : NavigationTraversalEdgeAdvanceStatus.Pending; |
| | | 909 | | } |
| | 17662 | 910 | | _surfaceRouteActive = false; |
| | 17662 | 911 | | return status == NavigationSurfaceEdgeRouteStatus.Passable |
| | 17662 | 912 | | ? SetCurrent(_surfaceRouteWork.Cost) |
| | 17662 | 913 | | : MapSurfaceRoute(status); |
| | | 914 | | } |
| | | 915 | | |
| | | 916 | | private static NavigationTraversalEdgeAdvanceStatus MapSurfaceRoute( |
| | 48 | 917 | | NavigationSurfaceEdgeRouteStatus status) => status switch |
| | 48 | 918 | | { |
| | 48 | 919 | | NavigationSurfaceEdgeRouteStatus.BudgetExceeded => |
| | 7 | 920 | | NavigationTraversalEdgeAdvanceStatus.BudgetExceeded, |
| | 48 | 921 | | NavigationSurfaceEdgeRouteStatus.CostOverflow => |
| | 3 | 922 | | NavigationTraversalEdgeAdvanceStatus.CostOverflow, |
| | 38 | 923 | | _ => NavigationTraversalEdgeAdvanceStatus.Pending |
| | 48 | 924 | | }; |
| | | 925 | | |
| | | 926 | | private NavigationTraversalEdgeAdvanceStatus SetCurrent(Fixed64 cost) |
| | | 927 | | { |
| | 17618 | 928 | | CurrentTarget = new NavigationMediumStateRef( |
| | 17618 | 929 | | _surfaceEdge.Target, |
| | 17618 | 930 | | TraversalMedium.Solid); |
| | 17618 | 931 | | CurrentKind = NavigationTraversalEdgeKind.Surface; |
| | 17618 | 932 | | CurrentOrdinal = _surfaceOrdinal; |
| | 17618 | 933 | | CurrentCost = cost; |
| | 17618 | 934 | | CurrentSurfaceEdge = _surfaceEdge; |
| | 17618 | 935 | | CurrentVolumeIsShortcut = false; |
| | 17618 | 936 | | return NavigationTraversalEdgeAdvanceStatus.Edge; |
| | | 937 | | } |
| | | 938 | | |
| | | 939 | | private void MarkWorldDependency(TraversalMedium medium) |
| | | 940 | | { |
| | 1419 | 941 | | if (medium == TraversalMedium.Gas || medium == TraversalMedium.Liquid) |
| | 1386 | 942 | | RequiresWorldStamp = true; |
| | 1419 | 943 | | } |
| | | 944 | | |
| | | 945 | | private NavigationTraversalEdgeAdvanceStatus Complete() |
| | | 946 | | { |
| | 3052 | 947 | | CurrentTarget = default; |
| | 3052 | 948 | | CurrentKind = default; |
| | 3052 | 949 | | CurrentOrdinal = -1; |
| | 3052 | 950 | | CurrentCost = default; |
| | 3052 | 951 | | CurrentSurfaceEdge = default; |
| | 3052 | 952 | | CurrentVolumeIsShortcut = false; |
| | 3052 | 953 | | CurrentTransitionId = string.Empty; |
| | 3052 | 954 | | CurrentTransitionOwnerMapId = string.Empty; |
| | 3052 | 955 | | CurrentTransitionType = default; |
| | 3052 | 956 | | CurrentTransitionHints = default; |
| | 3052 | 957 | | CurrentTransitionIdentityKind = default; |
| | 3052 | 958 | | CurrentTransitionSourceAction = default; |
| | 3052 | 959 | | CurrentTransitionDestinationAction = default; |
| | 3052 | 960 | | return NavigationTraversalEdgeAdvanceStatus.Complete; |
| | | 961 | | } |
| | | 962 | | } |