| | | 1 | | //======================================================================= |
| | | 2 | | // NavigationDependencyStampWork.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 | | |
| | | 10 | | namespace Trailblazer.Pathing; |
| | | 11 | | |
| | | 12 | | /// <summary>Captures one immutable dependency stamp with bounded graph reads.</summary> |
| | | 13 | | internal sealed class NavigationDependencyStampWork |
| | | 14 | | { |
| | | 15 | | private readonly NavigationWorldGraph _graph; |
| | | 16 | | private readonly NavigationAreaPolicyKey _areaPolicy; |
| | | 17 | | private readonly NavigationSurfaceComponentKey[] _componentAddresses; |
| | | 18 | | private readonly GraphPageDependencyAddress[] _pageAddresses; |
| | | 19 | | private readonly GraphComponentDependency[] _components; |
| | | 20 | | private readonly GraphPageDependency[] _pages; |
| | | 21 | | private readonly bool _includeTransitionRules; |
| | | 22 | | private int _componentOrdinal; |
| | | 23 | | private int _pageOrdinal; |
| | | 24 | | |
| | 923 | 25 | | internal NavigationDependencyStampWork( |
| | 923 | 26 | | NavigationWorldGraph graph, |
| | 923 | 27 | | NavigationAreaPolicy areaPolicy, |
| | 923 | 28 | | NavigationSurfaceComponentKey[] componentAddresses, |
| | 923 | 29 | | int componentCount, |
| | 923 | 30 | | GraphPageDependencyAddress[] pageAddresses, |
| | 923 | 31 | | int pageCount, |
| | 923 | 32 | | bool includeTransitionRules = false) |
| | | 33 | | { |
| | 923 | 34 | | SwiftThrowHelper.ThrowIfNull(graph, nameof(graph)); |
| | 923 | 35 | | SwiftThrowHelper.ThrowIfNull(areaPolicy, nameof(areaPolicy)); |
| | 923 | 36 | | SwiftThrowHelper.ThrowIfNull(componentAddresses, nameof(componentAddresses)); |
| | 923 | 37 | | SwiftThrowHelper.ThrowIfNull(pageAddresses, nameof(pageAddresses)); |
| | 923 | 38 | | SwiftThrowHelper.ThrowIfArgumentOutOfRange( |
| | 923 | 39 | | (uint)componentCount > (uint)componentAddresses.Length, |
| | 923 | 40 | | componentCount, |
| | 923 | 41 | | nameof(componentCount)); |
| | 923 | 42 | | SwiftThrowHelper.ThrowIfArgumentOutOfRange( |
| | 923 | 43 | | (uint)pageCount > (uint)pageAddresses.Length, |
| | 923 | 44 | | pageCount, |
| | 923 | 45 | | nameof(pageCount)); |
| | 923 | 46 | | _graph = graph; |
| | 923 | 47 | | _areaPolicy = areaPolicy.Key; |
| | 923 | 48 | | _componentAddresses = componentAddresses; |
| | 923 | 49 | | _pageAddresses = pageAddresses; |
| | 923 | 50 | | _components = new GraphComponentDependency[componentCount]; |
| | 923 | 51 | | _pages = new GraphPageDependency[pageCount]; |
| | 923 | 52 | | _includeTransitionRules = includeTransitionRules; |
| | 923 | 53 | | IsValid = true; |
| | 923 | 54 | | } |
| | | 55 | | |
| | | 56 | | internal bool IsComplete { get; private set; } |
| | | 57 | | |
| | | 58 | | internal bool IsValid { get; private set; } |
| | | 59 | | |
| | | 60 | | internal GraphDependencyStamp Result { get; private set; } = null!; |
| | | 61 | | |
| | | 62 | | internal bool Advance(NavigationWorkMeter meter, int lookupStepLimit) |
| | | 63 | | { |
| | 976 | 64 | | SwiftThrowHelper.ThrowIfNull(meter, nameof(meter)); |
| | 976 | 65 | | SwiftThrowHelper.ThrowIfNegative(lookupStepLimit, nameof(lookupStepLimit)); |
| | 976 | 66 | | int remaining = Math.Min(lookupStepLimit, meter.RemainingLookupProbes); |
| | 1967 | 67 | | while (_componentOrdinal < _components.Length) |
| | | 68 | | { |
| | 1012 | 69 | | if (remaining == 0 || !meter.TryConsumeLookupProbes(1)) |
| | 19 | 70 | | return false; |
| | 993 | 71 | | remaining--; |
| | 993 | 72 | | NavigationSurfaceComponentKey representative = |
| | 993 | 73 | | _componentAddresses[_componentOrdinal]; |
| | 993 | 74 | | if ((_componentOrdinal > 0 |
| | 993 | 75 | | && _componentAddresses[_componentOrdinal - 1] |
| | 993 | 76 | | .CompareTo(representative) >= 0) |
| | 993 | 77 | | || !_graph.TryGetComponentDependency( |
| | 993 | 78 | | representative, |
| | 993 | 79 | | out _components[_componentOrdinal])) |
| | | 80 | | { |
| | 2 | 81 | | return CompleteInvalid(); |
| | | 82 | | } |
| | 991 | 83 | | _componentOrdinal++; |
| | | 84 | | } |
| | 1929 | 85 | | while (_pageOrdinal < _pages.Length) |
| | | 86 | | { |
| | 1013 | 87 | | if (remaining == 0 || !meter.TryConsumeLookupProbes(1)) |
| | 37 | 88 | | return false; |
| | 976 | 89 | | remaining--; |
| | 976 | 90 | | GraphPageDependencyAddress address = _pageAddresses[_pageOrdinal]; |
| | 976 | 91 | | if ((_pageOrdinal > 0 |
| | 976 | 92 | | && ComparePageAddresses( |
| | 976 | 93 | | _pageAddresses[_pageOrdinal - 1], |
| | 976 | 94 | | address) >= 0) |
| | 976 | 95 | | || !_graph.TryGetPageDependency( |
| | 976 | 96 | | address, |
| | 976 | 97 | | _includeTransitionRules, |
| | 976 | 98 | | out _pages[_pageOrdinal])) |
| | | 99 | | { |
| | 2 | 100 | | return CompleteInvalid(); |
| | | 101 | | } |
| | 974 | 102 | | _pageOrdinal++; |
| | | 103 | | } |
| | 916 | 104 | | Result = new GraphDependencyStamp( |
| | 916 | 105 | | _areaPolicy, |
| | 916 | 106 | | _components, |
| | 916 | 107 | | _pages, |
| | 916 | 108 | | _includeTransitionRules, |
| | 916 | 109 | | _graph.TransitionRules.Version); |
| | 916 | 110 | | IsComplete = true; |
| | 916 | 111 | | return true; |
| | | 112 | | } |
| | | 113 | | |
| | | 114 | | private bool CompleteInvalid() |
| | | 115 | | { |
| | 4 | 116 | | IsValid = false; |
| | 4 | 117 | | IsComplete = true; |
| | 4 | 118 | | Result = null!; |
| | 4 | 119 | | return true; |
| | | 120 | | } |
| | | 121 | | |
| | | 122 | | private static int ComparePageAddresses( |
| | | 123 | | GraphPageDependencyAddress left, |
| | | 124 | | GraphPageDependencyAddress right) |
| | | 125 | | { |
| | 58 | 126 | | int mapComparison = string.CompareOrdinal(left.MapId, right.MapId); |
| | 58 | 127 | | return mapComparison != 0 |
| | 58 | 128 | | ? mapComparison |
| | 58 | 129 | | : left.PageIndex.CompareTo(right.PageIndex); |
| | | 130 | | } |
| | | 131 | | } |