| | | 1 | | //======================================================================= |
| | | 2 | | // NavigationFlowFieldGuideLease.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 System; |
| | | 9 | | using System.Diagnostics; |
| | | 10 | | using FixedMathSharp; |
| | | 11 | | using GridForge.Grids; |
| | | 12 | | using GridForge.Grids.Topology; |
| | | 13 | | |
| | | 14 | | namespace Trailblazer.Pathing; |
| | | 15 | | |
| | | 16 | | /// <summary>Owns the generation-linearized state of one flow field sampler.</summary> |
| | | 17 | | internal sealed class NavigationFlowFieldGuideLease |
| | | 18 | | { |
| | 8223 | 19 | | private readonly object _sync = new(); |
| | | 20 | | private readonly GridCoveredAddressCursor _coveredAddressCursor; |
| | | 21 | | private readonly GridCoveredAddressGeneration[] _coveredAddressGenerations; |
| | | 22 | | private readonly GridCoveredAddress[] _coveredAddressOutput; |
| | | 23 | | private NavigationFlowFieldPayloadCache? _owner; |
| | | 24 | | private NavigationWorldGraphStore? _store; |
| | | 25 | | private NavigationCellAddress _currentSource; |
| | | 26 | | private TraversalMedium _currentMedium; |
| | | 27 | | private Fixed64 _originIntegrationCost; |
| | | 28 | | private NavigationGuideStatus _status; |
| | | 29 | | private int _payloadSlot; |
| | | 30 | | private ulong _payloadGeneration; |
| | | 31 | | private ulong _generation; |
| | | 32 | | private long _sampleOrdinal; |
| | | 33 | | private bool _hasPendingTransition; |
| | | 34 | | |
| | 8223 | 35 | | internal NavigationFlowFieldGuideLease(int coveredAddressGenerationCapacity) |
| | | 36 | | { |
| | 8223 | 37 | | _coveredAddressCursor = new GridCoveredAddressCursor( |
| | 8223 | 38 | | coveredAddressGenerationCapacity); |
| | 8223 | 39 | | _coveredAddressGenerations = coveredAddressGenerationCapacity == 0 |
| | 8223 | 40 | | ? Array.Empty<GridCoveredAddressGeneration>() |
| | 8223 | 41 | | : new GridCoveredAddressGeneration[coveredAddressGenerationCapacity]; |
| | 8223 | 42 | | _coveredAddressOutput = new GridCoveredAddress[1]; |
| | 8223 | 43 | | _payloadSlot = -1; |
| | 8223 | 44 | | } |
| | | 45 | | |
| | | 46 | | internal ulong Generation |
| | | 47 | | { |
| | 3807 | 48 | | get { lock (_sync) return _generation; } |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | internal void Bind( |
| | | 52 | | NavigationFlowFieldPayloadCache owner, |
| | | 53 | | NavigationWorldGraphStore store, |
| | | 54 | | int payloadSlot, |
| | | 55 | | ulong payloadGeneration, |
| | | 56 | | NavigationCellAddress resolvedOrigin, |
| | | 57 | | TraversalMedium startMedium, |
| | | 58 | | Fixed64 originIntegrationCost) |
| | | 59 | | { |
| | 1179 | 60 | | lock (_sync) |
| | | 61 | | { |
| | | 62 | | // The cache rents only detached guides and does not return one to its pool |
| | | 63 | | // until TryDetach has cleared this complete ownership tuple. |
| | | 64 | | Debug.Assert(_owner == null); |
| | 1179 | 65 | | _generation = NavigationGenerationCounter.Advance( |
| | 1179 | 66 | | _generation, |
| | 1179 | 67 | | "The flow guide generation is exhausted."); |
| | 1179 | 68 | | _owner = owner; |
| | 1179 | 69 | | _store = store; |
| | 1179 | 70 | | _payloadSlot = payloadSlot; |
| | 1179 | 71 | | _payloadGeneration = payloadGeneration; |
| | 1179 | 72 | | _currentSource = resolvedOrigin; |
| | 1179 | 73 | | _currentMedium = startMedium; |
| | 1179 | 74 | | _originIntegrationCost = originIntegrationCost; |
| | 1179 | 75 | | _sampleOrdinal = 0; |
| | 1179 | 76 | | _hasPendingTransition = false; |
| | 1179 | 77 | | _status = NavigationGuideStatus.Success; |
| | 1179 | 78 | | } |
| | 1179 | 79 | | } |
| | | 80 | | |
| | | 81 | | internal NavigationGuideStatus GetStatus(ulong generation) |
| | | 82 | | { |
| | 34 | 83 | | lock (_sync) |
| | | 84 | | { |
| | 34 | 85 | | if (!IsGenerationActiveUnderLock(generation)) |
| | 3 | 86 | | return NavigationGuideStatus.Stale; |
| | 31 | 87 | | if (_status != NavigationGuideStatus.Success) |
| | 6 | 88 | | return _status; |
| | 25 | 89 | | return TryGetCurrentPayloadUnderLock(out _) |
| | 25 | 90 | | ? NavigationGuideStatus.Success |
| | 25 | 91 | | : MarkStaleUnderLock(); |
| | | 92 | | } |
| | 34 | 93 | | } |
| | | 94 | | |
| | | 95 | | internal Fixed64 GetOriginIntegrationCost(ulong generation) |
| | | 96 | | { |
| | 4 | 97 | | lock (_sync) |
| | | 98 | | { |
| | 4 | 99 | | return IsGenerationActiveUnderLock(generation) |
| | 4 | 100 | | ? _originIntegrationCost |
| | 4 | 101 | | : Fixed64.Zero; |
| | | 102 | | } |
| | 4 | 103 | | } |
| | | 104 | | |
| | | 105 | | internal NavigationGuideStatus TrySample( |
| | | 106 | | ulong generation, |
| | | 107 | | Vector3d actualFootPosition, |
| | | 108 | | ref GuideSampleWorkMeter meter, |
| | | 109 | | out NavigationFlowSample sample) |
| | | 110 | | { |
| | 1363 | 111 | | lock (_sync) |
| | | 112 | | { |
| | 1363 | 113 | | sample = default; |
| | 1363 | 114 | | if (!IsGenerationActiveUnderLock(generation)) |
| | 1 | 115 | | return NavigationGuideStatus.Stale; |
| | 1362 | 116 | | if (_status != NavigationGuideStatus.Success) |
| | 2 | 117 | | return _status; |
| | 1360 | 118 | | if (!TryGetCurrentPayloadUnderLock(out NavigationFlowFieldPayload payload)) |
| | 2 | 119 | | return MarkStaleUnderLock(); |
| | 1358 | 120 | | NavigationFlowFieldPayloadCache owner = _owner!; |
| | 1358 | 121 | | GridWorld world = owner.World; |
| | 1358 | 122 | | ulong worldSequence = world.ChangeSequence; |
| | 1358 | 123 | | NavigationWorldGraphStore store = _store!; |
| | 1358 | 124 | | NavigationWorldGraphLease? graphLease = store.TryAcquire(); |
| | 1358 | 125 | | if (graphLease == null) |
| | 2 | 126 | | return NavigationGuideStatus.CapacityExceeded; |
| | 1356 | 127 | | using (graphLease) |
| | | 128 | | { |
| | 1356 | 129 | | NavigationWorldGraph graph = graphLease.Graph; |
| | 1356 | 130 | | bool dependencyCurrent = graph.IsDependencyCurrent(payload.Dependencies); |
| | 1356 | 131 | | NavigationCellAddress candidateSource = _currentSource; |
| | 1356 | 132 | | NavigationFlowFieldNode currentNode = default; |
| | 1356 | 133 | | Vector3d target = default; |
| | 1356 | 134 | | Vector3d candidateHeading = default; |
| | 1356 | 135 | | NavigationGuideStatus status = |
| | 1356 | 136 | | NavigationSelectedEdgeProgressWork.TrySample( |
| | 1356 | 137 | | dependencyCurrent, |
| | 1356 | 138 | | world, |
| | 1356 | 139 | | store, |
| | 1356 | 140 | | graph, |
| | 1356 | 141 | | payload, |
| | 1356 | 142 | | _currentSource, |
| | 1356 | 143 | | _currentMedium, |
| | 1356 | 144 | | actualFootPosition, |
| | 1356 | 145 | | ref meter, |
| | 1356 | 146 | | _coveredAddressCursor, |
| | 1356 | 147 | | _coveredAddressGenerations, |
| | 1356 | 148 | | _coveredAddressOutput, |
| | 1356 | 149 | | owner.ImmediateRayWorkspace, |
| | 1356 | 150 | | out currentNode, |
| | 1356 | 151 | | out candidateSource, |
| | 1356 | 152 | | out target, |
| | 1356 | 153 | | out candidateHeading); |
| | 1356 | 154 | | if (!dependencyCurrent |
| | 1356 | 155 | | || !TryGetCurrentPayloadUnderLock(out _) |
| | 1356 | 156 | | || world.ChangeSequence != worldSequence) |
| | | 157 | | { |
| | 1 | 158 | | return MarkStaleUnderLock(); |
| | | 159 | | } |
| | 1355 | 160 | | if (status == NavigationGuideStatus.Stale) |
| | 1 | 161 | | return MarkStaleUnderLock(); |
| | 1354 | 162 | | if (status == NavigationGuideStatus.Success) |
| | | 163 | | { |
| | 1282 | 164 | | bool sourceChanged = candidateSource != _currentSource; |
| | | 165 | | // Selected edges target earlier-settled nodes in this int-bounded |
| | | 166 | | // payload, so one lease cannot exhaust a long source identity. |
| | | 167 | | Debug.Assert(_sampleOrdinal < payload.Nodes.Length); |
| | 1282 | 168 | | long candidateSampleOrdinal = AdvanceSampleOrdinal( |
| | 1282 | 169 | | _sampleOrdinal, |
| | 1282 | 170 | | sourceChanged); |
| | 1282 | 171 | | if (currentNode.TransitionInstructionOrdinal >= 0) |
| | | 172 | | { |
| | 22 | 173 | | NavigationGuideStatus transitionStatus = |
| | 22 | 174 | | TrySampleTransitionUnderLock( |
| | 22 | 175 | | generation, |
| | 22 | 176 | | world, |
| | 22 | 177 | | store, |
| | 22 | 178 | | graph, |
| | 22 | 179 | | owner, |
| | 22 | 180 | | payload, |
| | 22 | 181 | | currentNode, |
| | 22 | 182 | | candidateSource, |
| | 22 | 183 | | candidateSampleOrdinal, |
| | 22 | 184 | | actualFootPosition, |
| | 22 | 185 | | ref meter, |
| | 22 | 186 | | out sample); |
| | 22 | 187 | | bool transitionEpochCurrent = IsSampleEpochCurrent( |
| | 22 | 188 | | TryGetCurrentPayloadUnderLock(out _), |
| | 22 | 189 | | world.ChangeSequence, |
| | 22 | 190 | | worldSequence); |
| | 22 | 191 | | transitionStatus = ResolveTransitionSampleStatus( |
| | 22 | 192 | | transitionEpochCurrent, |
| | 22 | 193 | | transitionStatus, |
| | 22 | 194 | | ref sample); |
| | 22 | 195 | | if (transitionStatus == NavigationGuideStatus.Stale) |
| | 1 | 196 | | return MarkStaleUnderLock(); |
| | 21 | 197 | | if (transitionStatus != NavigationGuideStatus.Success) |
| | 4 | 198 | | return transitionStatus; |
| | 17 | 199 | | _currentSource = candidateSource; |
| | 17 | 200 | | _sampleOrdinal = candidateSampleOrdinal; |
| | 17 | 201 | | _hasPendingTransition = sample.HasTransition; |
| | 17 | 202 | | return NavigationGuideStatus.Success; |
| | | 203 | | } |
| | 1260 | 204 | | _currentSource = candidateSource; |
| | 1260 | 205 | | _sampleOrdinal = candidateSampleOrdinal; |
| | 1260 | 206 | | sample = new NavigationFlowSample( |
| | 1260 | 207 | | candidateHeading, |
| | 1260 | 208 | | target, |
| | 1260 | 209 | | _currentMedium, |
| | 1260 | 210 | | default, |
| | 1260 | 211 | | hasTransition: false); |
| | | 212 | | } |
| | 1332 | 213 | | return status; |
| | | 214 | | } |
| | | 215 | | } |
| | 1363 | 216 | | } |
| | | 217 | | |
| | | 218 | | internal NavigationGuideStatus CompletePendingTransition( |
| | | 219 | | ulong generation, |
| | | 220 | | in NavigationTransitionInstruction instruction) |
| | | 221 | | { |
| | 12 | 222 | | lock (_sync) |
| | | 223 | | { |
| | 12 | 224 | | if (!IsGenerationActiveUnderLock(generation) |
| | 12 | 225 | | || !_hasPendingTransition |
| | 12 | 226 | | || !TryGetCurrentPayloadUnderLock(out NavigationFlowFieldPayload payload) |
| | 12 | 227 | | || !payload.TryGetNode( |
| | 12 | 228 | | _currentSource, |
| | 12 | 229 | | _currentMedium, |
| | 12 | 230 | | out NavigationFlowFieldNode current) |
| | 12 | 231 | | || !instruction.MatchesCompletion( |
| | 12 | 232 | | this, |
| | 12 | 233 | | generation, |
| | 12 | 234 | | _sampleOrdinal)) |
| | | 235 | | { |
| | 4 | 236 | | return NavigationGuideStatus.Stale; |
| | | 237 | | } |
| | 8 | 238 | | NavigationTransitionInstruction expected = |
| | 8 | 239 | | payload.TransitionInstructions[current.TransitionInstructionOrdinal]; |
| | | 240 | | Debug.Assert(_sampleOrdinal < payload.Nodes.Length); |
| | 8 | 241 | | _status = ResolveTransitionCompletion( |
| | 8 | 242 | | expected.DestinationAddress, |
| | 8 | 243 | | expected.DestinationMedium, |
| | 8 | 244 | | AdvanceSampleOrdinal(_sampleOrdinal, advance: true), |
| | 8 | 245 | | ref _currentSource, |
| | 8 | 246 | | ref _currentMedium, |
| | 8 | 247 | | ref _hasPendingTransition, |
| | 8 | 248 | | ref _sampleOrdinal); |
| | 8 | 249 | | return _status; |
| | | 250 | | } |
| | 12 | 251 | | } |
| | | 252 | | |
| | | 253 | | internal static NavigationGuideStatus ResolveTransitionSampleStatus( |
| | | 254 | | bool epochCurrent, |
| | | 255 | | NavigationGuideStatus status, |
| | | 256 | | ref NavigationFlowSample sample) |
| | | 257 | | { |
| | 26 | 258 | | if (status != NavigationGuideStatus.Success || epochCurrent) |
| | 25 | 259 | | return status; |
| | 1 | 260 | | sample = default; |
| | 1 | 261 | | return NavigationGuideStatus.Stale; |
| | | 262 | | } |
| | | 263 | | |
| | | 264 | | internal static long AdvanceSampleOrdinal(long current, bool advance) |
| | | 265 | | { |
| | | 266 | | Debug.Assert(!advance || current < long.MaxValue); |
| | 1292 | 267 | | return current + (advance ? 1L : 0L); |
| | | 268 | | } |
| | | 269 | | |
| | | 270 | | internal static bool IsSampleEpochCurrent( |
| | | 271 | | bool payloadCurrent, |
| | | 272 | | ulong currentWorldSequence, |
| | | 273 | | ulong expectedWorldSequence) => |
| | 25 | 274 | | payloadCurrent && currentWorldSequence == expectedWorldSequence; |
| | | 275 | | |
| | | 276 | | internal static NavigationGuideStatus ResolveTransitionCompletion( |
| | | 277 | | NavigationCellAddress destination, |
| | | 278 | | TraversalMedium destinationMedium, |
| | | 279 | | long nextSampleOrdinal, |
| | | 280 | | ref NavigationCellAddress currentSource, |
| | | 281 | | ref TraversalMedium currentMedium, |
| | | 282 | | ref bool hasPendingTransition, |
| | | 283 | | ref long sampleOrdinal) |
| | | 284 | | { |
| | 9 | 285 | | currentSource = destination; |
| | 9 | 286 | | currentMedium = destinationMedium; |
| | 9 | 287 | | hasPendingTransition = false; |
| | 9 | 288 | | sampleOrdinal = nextSampleOrdinal; |
| | 9 | 289 | | return NavigationGuideStatus.Success; |
| | | 290 | | } |
| | | 291 | | |
| | | 292 | | internal bool TryDetach( |
| | | 293 | | ulong generation, |
| | | 294 | | out NavigationFlowFieldPayloadCache owner, |
| | | 295 | | out int payloadSlot) |
| | | 296 | | { |
| | 1183 | 297 | | lock (_sync) |
| | | 298 | | { |
| | 1183 | 299 | | owner = null!; |
| | 1183 | 300 | | payloadSlot = -1; |
| | 1183 | 301 | | if (!IsGenerationActiveUnderLock(generation)) |
| | 5 | 302 | | return false; |
| | 1178 | 303 | | owner = _owner!; |
| | 1178 | 304 | | payloadSlot = _payloadSlot; |
| | 1178 | 305 | | _owner = null; |
| | 1178 | 306 | | _store = null; |
| | 1178 | 307 | | _currentSource = default; |
| | 1178 | 308 | | _currentMedium = TraversalMedium.Unknown; |
| | 1178 | 309 | | _originIntegrationCost = Fixed64.Zero; |
| | 1178 | 310 | | _sampleOrdinal = 0; |
| | 1178 | 311 | | _hasPendingTransition = false; |
| | 1178 | 312 | | _status = NavigationGuideStatus.Stale; |
| | 1178 | 313 | | _payloadSlot = -1; |
| | 1178 | 314 | | _payloadGeneration = 0; |
| | 1178 | 315 | | return true; |
| | | 316 | | } |
| | 1183 | 317 | | } |
| | | 318 | | |
| | | 319 | | internal void Dispose(ulong generation) |
| | | 320 | | { |
| | 1183 | 321 | | if (!TryDetach( |
| | 1183 | 322 | | generation, |
| | 1183 | 323 | | out NavigationFlowFieldPayloadCache owner, |
| | 1183 | 324 | | out int payloadSlot)) |
| | | 325 | | { |
| | 5 | 326 | | return; |
| | | 327 | | } |
| | 1178 | 328 | | owner.ReturnDetachedGuide(this, generation, payloadSlot); |
| | 1178 | 329 | | } |
| | | 330 | | |
| | | 331 | | private bool IsGenerationActiveUnderLock(ulong generation) => |
| | 2596 | 332 | | generation != 0 |
| | 2596 | 333 | | && generation == _generation |
| | 2596 | 334 | | && _owner != null |
| | 2596 | 335 | | && _store != null |
| | 2596 | 336 | | && _payloadSlot >= 0; |
| | | 337 | | |
| | | 338 | | private bool TryGetCurrentPayloadUnderLock( |
| | | 339 | | out NavigationFlowFieldPayload payload) |
| | | 340 | | { |
| | 2773 | 341 | | NavigationFlowFieldPayloadCache? owner = _owner; |
| | 2773 | 342 | | NavigationWorldGraphStore? store = _store; |
| | 2773 | 343 | | if (owner != null |
| | 2773 | 344 | | && store != null |
| | 2773 | 345 | | && owner.TryGetGuidePayload( |
| | 2773 | 346 | | _payloadSlot, |
| | 2773 | 347 | | _payloadGeneration, |
| | 2773 | 348 | | out payload) == NavigationFlowFieldStatus.Success |
| | 2773 | 349 | | && owner.IsPayloadCurrent(store.Current, payload)) |
| | | 350 | | { |
| | 2767 | 351 | | return true; |
| | | 352 | | } |
| | 6 | 353 | | payload = null!; |
| | 6 | 354 | | return false; |
| | | 355 | | } |
| | | 356 | | |
| | | 357 | | private NavigationGuideStatus MarkStaleUnderLock() |
| | | 358 | | { |
| | 8 | 359 | | _status = NavigationGuideStatus.Stale; |
| | 8 | 360 | | return _status; |
| | | 361 | | } |
| | | 362 | | |
| | | 363 | | private NavigationGuideStatus TrySampleTransitionUnderLock( |
| | | 364 | | ulong generation, |
| | | 365 | | GridWorld world, |
| | | 366 | | NavigationWorldGraphStore store, |
| | | 367 | | NavigationWorldGraph graph, |
| | | 368 | | NavigationFlowFieldPayloadCache owner, |
| | | 369 | | NavigationFlowFieldPayload payload, |
| | | 370 | | in NavigationFlowFieldNode node, |
| | | 371 | | NavigationCellAddress candidateSource, |
| | | 372 | | long candidateSampleOrdinal, |
| | | 373 | | Vector3d actualFootPosition, |
| | | 374 | | ref GuideSampleWorkMeter meter, |
| | | 375 | | out NavigationFlowSample sample) |
| | | 376 | | { |
| | 22 | 377 | | sample = default; |
| | 22 | 378 | | NavigationTransitionInstruction instruction = |
| | 22 | 379 | | payload.TransitionInstructions[node.TransitionInstructionOrdinal]; |
| | | 380 | | NavigationGuideStatus headingStatus; |
| | | 381 | | Vector3d heading; |
| | 22 | 382 | | if (_hasPendingTransition) |
| | | 383 | | { |
| | 1 | 384 | | headingStatus = NavigationGuideStatus.Success; |
| | 1 | 385 | | heading = Vector3d.Zero; |
| | | 386 | | } |
| | | 387 | | else |
| | | 388 | | { |
| | 21 | 389 | | headingStatus = NavigationSelectedEdgeProgressWork.TrySampleTransitionApproach( |
| | 21 | 390 | | world, |
| | 21 | 391 | | store, |
| | 21 | 392 | | graph, |
| | 21 | 393 | | payload, |
| | 21 | 394 | | candidateSource, |
| | 21 | 395 | | _currentMedium, |
| | 21 | 396 | | actualFootPosition, |
| | 21 | 397 | | instruction.SourcePosition, |
| | 21 | 398 | | ref meter, |
| | 21 | 399 | | owner.ImmediateRayWorkspace, |
| | 21 | 400 | | out heading); |
| | | 401 | | } |
| | 22 | 402 | | if (headingStatus != NavigationGuideStatus.Success) |
| | 5 | 403 | | return headingStatus; |
| | 17 | 404 | | bool hasTransition = heading == Vector3d.Zero; |
| | | 405 | | Debug.Assert(!hasTransition || candidateSampleOrdinal < payload.Nodes.Length); |
| | 17 | 406 | | sample = new NavigationFlowSample( |
| | 17 | 407 | | heading, |
| | 17 | 408 | | instruction.SourcePosition, |
| | 17 | 409 | | _currentMedium, |
| | 17 | 410 | | hasTransition |
| | 17 | 411 | | ? instruction.WithCompletionStamp( |
| | 17 | 412 | | this, |
| | 17 | 413 | | generation, |
| | 17 | 414 | | candidateSampleOrdinal) |
| | 17 | 415 | | : default, |
| | 17 | 416 | | hasTransition); |
| | 17 | 417 | | return NavigationGuideStatus.Success; |
| | | 418 | | } |
| | | 419 | | } |