| | | 1 | | //======================================================================= |
| | | 2 | | // NavigationFlowFieldWorkspace.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 FixedMathSharp; |
| | | 10 | | |
| | | 11 | | namespace Trailblazer.Pathing; |
| | | 12 | | |
| | | 13 | | internal struct NavigationFlowFieldSearchNode |
| | | 14 | | { |
| | | 15 | | internal NavigationCellAddress Address; |
| | | 16 | | internal Fixed64 IntegrationCost; |
| | | 17 | | internal NavigationSelectedEdgeRef SelectedEdge; |
| | | 18 | | internal int HeapIndex; |
| | | 19 | | internal bool SelectedIsTransition; |
| | | 20 | | internal bool Closed; |
| | | 21 | | } |
| | | 22 | | |
| | | 23 | | /// <summary>Owns reusable fixed-capacity buffers for one exclusive flow query.</summary> |
| | | 24 | | internal sealed class NavigationFlowFieldWorkspace |
| | | 25 | | { |
| | | 26 | | private readonly NavigationMediumStateRef[] _keys; |
| | | 27 | | private readonly NavigationFlowFieldSearchNode[] _records; |
| | | 28 | | private readonly long[] _stamps; |
| | | 29 | | private readonly int[] _activeSlots; |
| | | 30 | | private readonly int _mask; |
| | | 31 | | private readonly int _nodeCapacity; |
| | | 32 | | private int _nodeCount; |
| | 8502 | 33 | | private long _generation = 1; |
| | | 34 | | |
| | 8502 | 35 | | internal NavigationFlowFieldWorkspace( |
| | 8502 | 36 | | int mapCapacity, |
| | 8502 | 37 | | int dependencyPageCapacity, |
| | 8502 | 38 | | int dependencyComponentCapacity, |
| | 8502 | 39 | | int nodeCapacity, |
| | 8502 | 40 | | int rayCoveredAddressCapacity, |
| | 8502 | 41 | | int rayTraceIntervalCapacity) |
| | | 42 | | { |
| | 8502 | 43 | | SwiftThrowHelper.ThrowIfNegative( |
| | 8502 | 44 | | dependencyPageCapacity, |
| | 8502 | 45 | | nameof(dependencyPageCapacity)); |
| | 8502 | 46 | | SwiftThrowHelper.ThrowIfNegative( |
| | 8502 | 47 | | dependencyComponentCapacity, |
| | 8502 | 48 | | nameof(dependencyComponentCapacity)); |
| | 8502 | 49 | | SwiftThrowHelper.ThrowIfNegative(nodeCapacity, nameof(nodeCapacity)); |
| | 8502 | 50 | | int tableSize = 1; |
| | 8502 | 51 | | int required = checked(Math.Max(1, nodeCapacity * 2)); |
| | 115107 | 52 | | while (tableSize < required) |
| | 106605 | 53 | | tableSize = checked(tableSize * 2); |
| | 8502 | 54 | | _keys = new NavigationMediumStateRef[tableSize]; |
| | 8502 | 55 | | _records = new NavigationFlowFieldSearchNode[tableSize]; |
| | 8502 | 56 | | _stamps = new long[tableSize]; |
| | 8502 | 57 | | _activeSlots = nodeCapacity == 0 ? Array.Empty<int>() : new int[nodeCapacity]; |
| | 8502 | 58 | | _mask = tableSize - 1; |
| | 8502 | 59 | | _nodeCapacity = nodeCapacity; |
| | 8502 | 60 | | HeapSlots = nodeCapacity == 0 ? Array.Empty<int>() : new int[nodeCapacity]; |
| | 8502 | 61 | | SettledSlots = nodeCapacity == 0 ? Array.Empty<int>() : new int[nodeCapacity]; |
| | 8502 | 62 | | EndpointWorkspace = new NavigationEndpointWorkspace( |
| | 8502 | 63 | | mapCapacity, |
| | 8502 | 64 | | dependencyPageCapacity, |
| | 8502 | 65 | | dependencyComponentCapacity); |
| | 8502 | 66 | | RayWorkspace = new NavigationRayWorkspace( |
| | 8502 | 67 | | mapCapacity, |
| | 8502 | 68 | | dependencyPageCapacity, |
| | 8502 | 69 | | dependencyComponentCapacity, |
| | 8502 | 70 | | rayCoveredAddressCapacity, |
| | 8502 | 71 | | rayTraceIntervalCapacity); |
| | 8502 | 72 | | } |
| | | 73 | | |
| | | 74 | | internal NavigationEndpointWorkspace EndpointWorkspace { get; } |
| | | 75 | | |
| | | 76 | | internal NavigationRayWorkspace RayWorkspace { get; } |
| | | 77 | | |
| | | 78 | | internal int[] HeapSlots { get; } |
| | | 79 | | |
| | | 80 | | internal int[] SettledSlots { get; } |
| | | 81 | | |
| | 42 | 82 | | internal int NodeCapacity => _nodeCapacity; |
| | | 83 | | |
| | 42 | 84 | | internal int DependencyPageCapacity => DependencyPages.Length; |
| | | 85 | | |
| | 42 | 86 | | internal int DependencyComponentCapacity => DependencyComponents.Length; |
| | | 87 | | |
| | 1452 | 88 | | internal GraphPageDependencyAddress[] DependencyPages => EndpointWorkspace.Pages; |
| | | 89 | | |
| | | 90 | | internal NavigationSurfaceComponentKey[] DependencyComponents => |
| | 1452 | 91 | | EndpointWorkspace.Components; |
| | | 92 | | |
| | | 93 | | internal int HeapCount { get; set; } |
| | | 94 | | |
| | | 95 | | internal int SettledCount { get; set; } |
| | | 96 | | |
| | 2118 | 97 | | internal int DependencyPageCount => EndpointWorkspace.PageCount; |
| | | 98 | | |
| | 2118 | 99 | | internal int DependencyComponentCount => EndpointWorkspace.ComponentCount; |
| | | 100 | | |
| | | 101 | | internal void Reset() |
| | | 102 | | { |
| | 2 | 103 | | ResetSearch(); |
| | 2 | 104 | | EndpointWorkspace.Reset(); |
| | 2 | 105 | | RayWorkspace.Reset(); |
| | 2 | 106 | | } |
| | | 107 | | |
| | | 108 | | internal void ResetSearch() |
| | | 109 | | { |
| | 1650 | 110 | | for (int i = 0; i < _nodeCount; i++) |
| | | 111 | | { |
| | 86 | 112 | | int slot = _activeSlots[i]; |
| | 86 | 113 | | _keys[slot] = default; |
| | 86 | 114 | | _records[slot] = default; |
| | 86 | 115 | | _activeSlots[i] = 0; |
| | | 116 | | } |
| | | 117 | | unchecked |
| | | 118 | | { |
| | 739 | 119 | | _generation++; |
| | | 120 | | } |
| | 739 | 121 | | _nodeCount = 0; |
| | 739 | 122 | | HeapCount = 0; |
| | 739 | 123 | | SettledCount = 0; |
| | 739 | 124 | | } |
| | | 125 | | |
| | | 126 | | internal bool TryGetSlot(NavigationMediumStateRef node, out int slot) |
| | | 127 | | { |
| | 6915 | 128 | | slot = node.GetHashCode() & _mask; |
| | 6921 | 129 | | while (_stamps[slot] == _generation) |
| | | 130 | | { |
| | 2996 | 131 | | if (_keys[slot].Equals(node)) |
| | 2990 | 132 | | return true; |
| | 6 | 133 | | slot = (slot + 1) & _mask; |
| | | 134 | | } |
| | 3925 | 135 | | return false; |
| | | 136 | | } |
| | | 137 | | |
| | | 138 | | internal bool TryGetOrAdd( |
| | | 139 | | NavigationMediumStateRef node, |
| | | 140 | | out int slot, |
| | | 141 | | out bool added) |
| | | 142 | | { |
| | 6914 | 143 | | if (TryGetSlot(node, out slot)) |
| | | 144 | | { |
| | 2990 | 145 | | added = false; |
| | 2990 | 146 | | return true; |
| | | 147 | | } |
| | 3924 | 148 | | if (_nodeCount >= _nodeCapacity) |
| | | 149 | | { |
| | 5 | 150 | | added = false; |
| | 5 | 151 | | return false; |
| | | 152 | | } |
| | 3919 | 153 | | _keys[slot] = node; |
| | 3919 | 154 | | _records[slot] = default; |
| | 3919 | 155 | | _stamps[slot] = _generation; |
| | 3919 | 156 | | _activeSlots[_nodeCount++] = slot; |
| | 3919 | 157 | | added = true; |
| | 3919 | 158 | | return true; |
| | | 159 | | } |
| | | 160 | | |
| | | 161 | | internal ref NavigationFlowFieldSearchNode GetRecord(int slot) => |
| | 35193 | 162 | | ref _records[slot]; |
| | | 163 | | |
| | 9965 | 164 | | internal NavigationMediumStateRef GetNode(int slot) => _keys[slot]; |
| | | 165 | | |
| | | 166 | | internal bool TryRecordComponent(NavigationSurfaceComponentKey key) |
| | | 167 | | { |
| | 6914 | 168 | | return EndpointWorkspace.TryRecordComponent(key); |
| | | 169 | | } |
| | | 170 | | |
| | | 171 | | internal bool TryRecordPage(string mapId, int pageIndex) |
| | | 172 | | { |
| | 7648 | 173 | | return EndpointWorkspace.TryRecordPage(mapId, pageIndex); |
| | | 174 | | } |
| | | 175 | | } |