| | | 1 | | //======================================================================= |
| | | 2 | | // NavigationMapOverlayState.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 GridForge.Spatial; |
| | | 10 | | |
| | | 11 | | namespace Trailblazer.Pathing; |
| | | 12 | | |
| | | 13 | | /// <summary>Stores one map's immutable overlay roots in canonical key order.</summary> |
| | | 14 | | internal sealed class NavigationMapOverlayState |
| | | 15 | | { |
| | 1 | 16 | | internal static readonly NavigationMapOverlayState Empty = new( |
| | 1 | 17 | | PersistentVoxelIndexMap<NavigationCellOverlayOperation>.Empty, |
| | 1 | 18 | | PersistentStringMap<NavigationConnectionOverlayOperation>.Empty, |
| | 1 | 19 | | PersistentStringMap<TraversalTransitionOverlayOperation>.Empty, |
| | 1 | 20 | | highWaterSequence: 0, |
| | 1 | 21 | | lastApplyCopiedNodeCount: 0, |
| | 1 | 22 | | retainedPayloadBytes: 0); |
| | | 23 | | |
| | | 24 | | private readonly PersistentVoxelIndexMap<NavigationCellOverlayOperation> _cells; |
| | | 25 | | private readonly PersistentStringMap<NavigationConnectionOverlayOperation> _connections; |
| | | 26 | | private readonly PersistentStringMap<TraversalTransitionOverlayOperation> _transitions; |
| | | 27 | | |
| | 25527 | 28 | | private NavigationMapOverlayState( |
| | 25527 | 29 | | PersistentVoxelIndexMap<NavigationCellOverlayOperation> cells, |
| | 25527 | 30 | | PersistentStringMap<NavigationConnectionOverlayOperation> connections, |
| | 25527 | 31 | | PersistentStringMap<TraversalTransitionOverlayOperation> transitions, |
| | 25527 | 32 | | long highWaterSequence, |
| | 25527 | 33 | | int lastApplyCopiedNodeCount, |
| | 25527 | 34 | | long retainedPayloadBytes) |
| | | 35 | | { |
| | 25527 | 36 | | _cells = cells; |
| | 25527 | 37 | | _connections = connections; |
| | 25527 | 38 | | _transitions = transitions; |
| | 25527 | 39 | | HighWaterSequence = highWaterSequence; |
| | 25527 | 40 | | LastApplyCopiedNodeCount = lastApplyCopiedNodeCount; |
| | 25527 | 41 | | RetainedPayloadBytes = retainedPayloadBytes; |
| | 25527 | 42 | | } |
| | | 43 | | |
| | 6365 | 44 | | internal int CellCount => _cells.Count; |
| | | 45 | | |
| | 6688 | 46 | | internal int ConnectionCount => _connections.Count; |
| | | 47 | | |
| | 37545 | 48 | | internal int TransitionCount => _transitions.Count; |
| | | 49 | | |
| | 24870 | 50 | | internal int PersistentNodeCount => checked( |
| | 24870 | 51 | | _cells.PersistentNodeCount |
| | 24870 | 52 | | + _connections.PersistentNodeCount |
| | 24870 | 53 | | + _transitions.PersistentNodeCount); |
| | | 54 | | |
| | 24412 | 55 | | internal long RetainedBytes => checked( |
| | 24412 | 56 | | 48L |
| | 24412 | 57 | | + _cells.RetainedBytes |
| | 24412 | 58 | | + _connections.RetainedBytes |
| | 24412 | 59 | | + _transitions.RetainedBytes |
| | 24412 | 60 | | + RetainedPayloadBytes); |
| | | 61 | | |
| | | 62 | | internal long RetainedPayloadBytes { get; } |
| | | 63 | | |
| | | 64 | | internal int LastApplyCopiedNodeCount { get; } |
| | | 65 | | |
| | | 66 | | internal long HighWaterSequence { get; } |
| | | 67 | | |
| | | 68 | | internal NavigationCellOverlayOperation GetCellAt(int ordinal) => |
| | 41 | 69 | | _cells.GetValueAt(ordinal); |
| | | 70 | | |
| | | 71 | | internal NavigationConnectionOverlayOperation GetConnectionAt(int ordinal) => |
| | 5 | 72 | | _connections.GetValueAt(ordinal); |
| | | 73 | | |
| | | 74 | | internal TraversalTransitionOverlayOperation GetTransitionAt(int ordinal) => |
| | 33027 | 75 | | _transitions.GetValueAt(ordinal); |
| | | 76 | | |
| | | 77 | | internal bool TryGetCell(VoxelIndex index, out NavigationCellOverlayOperation operation) => |
| | 175056 | 78 | | _cells.TryGetValue(index, out operation); |
| | | 79 | | |
| | | 80 | | internal bool TryGetConnection( |
| | | 81 | | string id, |
| | | 82 | | out NavigationConnectionOverlayOperation operation) => |
| | 4497 | 83 | | _connections.TryGetValue(id, out operation); |
| | | 84 | | |
| | | 85 | | internal bool TryGetTransition( |
| | | 86 | | string id, |
| | | 87 | | out TraversalTransitionOverlayOperation operation) => |
| | 106 | 88 | | _transitions.TryGetValue(id, out operation); |
| | | 89 | | |
| | | 90 | | internal NavigationMapOverlayState Apply(NavigationMapOverlayDelta delta, long operationSequence) |
| | | 91 | | { |
| | 20 | 92 | | PersistentVoxelIndexMap<NavigationCellOverlayOperation> cells = _cells; |
| | 20 | 93 | | PersistentStringMap<NavigationConnectionOverlayOperation> connections = _connections; |
| | 20 | 94 | | PersistentStringMap<TraversalTransitionOverlayOperation> transitions = _transitions; |
| | 20 | 95 | | int copiedNodeCount = 0; |
| | 20 | 96 | | long retainedPayloadBytes = RetainedPayloadBytes; |
| | | 97 | | |
| | 20 | 98 | | ReadOnlySpan<NavigationCellOverlayOperation> cellChanges = delta.CellSpan; |
| | 196 | 99 | | for (int i = 0; i < cellChanges.Length; i++) |
| | | 100 | | { |
| | 78 | 101 | | NavigationCellOverlayOperation change = cellChanges[i]; |
| | 78 | 102 | | if (cells.TryGetValue(change.Index, out NavigationCellOverlayOperation prior)) |
| | | 103 | | { |
| | 4 | 104 | | retainedPayloadBytes = checked( |
| | 4 | 105 | | retainedPayloadBytes |
| | 4 | 106 | | - NavigationMapOverlayDelta.EstimateRetainedPayload(prior)); |
| | | 107 | | } |
| | | 108 | | int copied; |
| | 78 | 109 | | if (change.Kind == NavigationCellOverlayOperationKind.RevertToBake) |
| | 2 | 110 | | cells = cells.Remove(change.Index, out _, out copied); |
| | | 111 | | else |
| | | 112 | | { |
| | 76 | 113 | | cells = cells.Set(change.Index, change, out copied); |
| | 76 | 114 | | retainedPayloadBytes = checked( |
| | 76 | 115 | | retainedPayloadBytes |
| | 76 | 116 | | + NavigationMapOverlayDelta.EstimateRetainedPayload(change)); |
| | | 117 | | } |
| | 78 | 118 | | copiedNodeCount = checked(copiedNodeCount + copied); |
| | | 119 | | } |
| | | 120 | | |
| | 20 | 121 | | ReadOnlySpan<NavigationConnectionOverlayOperation> connectionChanges = delta.ConnectionSpan; |
| | 52 | 122 | | for (int i = 0; i < connectionChanges.Length; i++) |
| | | 123 | | { |
| | 6 | 124 | | NavigationConnectionOverlayOperation change = connectionChanges[i]; |
| | 6 | 125 | | if (connections.TryGetValue(change.Id, out NavigationConnectionOverlayOperation prior)) |
| | | 126 | | { |
| | 2 | 127 | | retainedPayloadBytes = checked( |
| | 2 | 128 | | retainedPayloadBytes |
| | 2 | 129 | | - NavigationMapOverlayDelta.EstimateRetainedPayload(prior)); |
| | | 130 | | } |
| | | 131 | | int copied; |
| | 6 | 132 | | if (change.Kind == NavigationConnectionOverlayOperationKind.RevertToBake) |
| | 1 | 133 | | connections = connections.Remove(change.Id, out _, out copied); |
| | | 134 | | else |
| | | 135 | | { |
| | 5 | 136 | | connections = connections.Set(change.Id, change, out copied); |
| | 5 | 137 | | retainedPayloadBytes = checked( |
| | 5 | 138 | | retainedPayloadBytes |
| | 5 | 139 | | + NavigationMapOverlayDelta.EstimateRetainedPayload(change)); |
| | | 140 | | } |
| | 6 | 141 | | copiedNodeCount = checked(copiedNodeCount + copied); |
| | | 142 | | } |
| | | 143 | | |
| | 20 | 144 | | ReadOnlySpan<TraversalTransitionOverlayOperation> transitionChanges = delta.TransitionSpan; |
| | 46 | 145 | | for (int i = 0; i < transitionChanges.Length; i++) |
| | | 146 | | { |
| | 3 | 147 | | TraversalTransitionOverlayOperation change = transitionChanges[i]; |
| | 3 | 148 | | if (transitions.TryGetValue(change.Id, out TraversalTransitionOverlayOperation prior)) |
| | | 149 | | { |
| | 2 | 150 | | retainedPayloadBytes = checked( |
| | 2 | 151 | | retainedPayloadBytes |
| | 2 | 152 | | - NavigationMapOverlayDelta.EstimateRetainedPayload(prior)); |
| | | 153 | | } |
| | | 154 | | int copied; |
| | 3 | 155 | | if (change.Kind == TraversalTransitionOverlayOperationKind.RevertToBake) |
| | 1 | 156 | | transitions = transitions.Remove(change.Id, out _, out copied); |
| | | 157 | | else |
| | | 158 | | { |
| | 2 | 159 | | transitions = transitions.Set(change.Id, change, out copied); |
| | 2 | 160 | | retainedPayloadBytes = checked( |
| | 2 | 161 | | retainedPayloadBytes |
| | 2 | 162 | | + NavigationMapOverlayDelta.EstimateRetainedPayload(change)); |
| | | 163 | | } |
| | 3 | 164 | | copiedNodeCount = checked(copiedNodeCount + copied); |
| | | 165 | | } |
| | | 166 | | |
| | 20 | 167 | | return new NavigationMapOverlayState( |
| | 20 | 168 | | cells, |
| | 20 | 169 | | connections, |
| | 20 | 170 | | transitions, |
| | 20 | 171 | | operationSequence, |
| | 20 | 172 | | copiedNodeCount, |
| | 20 | 173 | | retainedPayloadBytes); |
| | | 174 | | } |
| | | 175 | | |
| | | 176 | | internal NavigationMapOverlayState Apply( |
| | | 177 | | NavigationCellOverlayOperation change, |
| | | 178 | | long operationSequence) |
| | | 179 | | { |
| | 17227 | 180 | | long retainedPayloadBytes = RetainedPayloadBytes; |
| | 17227 | 181 | | if (_cells.TryGetValue(change.Index, out NavigationCellOverlayOperation prior)) |
| | 72 | 182 | | retainedPayloadBytes -= NavigationMapOverlayDelta.EstimateRetainedPayload(prior); |
| | | 183 | | int copied; |
| | | 184 | | PersistentVoxelIndexMap<NavigationCellOverlayOperation> cells; |
| | 17227 | 185 | | if (change.Kind == NavigationCellOverlayOperationKind.RevertToBake) |
| | 54 | 186 | | cells = _cells.Remove(change.Index, out _, out copied); |
| | | 187 | | else |
| | | 188 | | { |
| | 17173 | 189 | | cells = _cells.Set(change.Index, change, out copied); |
| | 17173 | 190 | | retainedPayloadBytes = checked( |
| | 17173 | 191 | | retainedPayloadBytes + NavigationMapOverlayDelta.EstimateRetainedPayload(change)); |
| | | 192 | | } |
| | 17227 | 193 | | return new NavigationMapOverlayState( |
| | 17227 | 194 | | cells, |
| | 17227 | 195 | | _connections, |
| | 17227 | 196 | | _transitions, |
| | 17227 | 197 | | operationSequence, |
| | 17227 | 198 | | copied, |
| | 17227 | 199 | | retainedPayloadBytes); |
| | | 200 | | } |
| | | 201 | | |
| | | 202 | | internal NavigationMapOverlayState Apply( |
| | | 203 | | NavigationConnectionOverlayOperation change, |
| | | 204 | | long operationSequence) |
| | | 205 | | { |
| | 4158 | 206 | | long retainedPayloadBytes = RetainedPayloadBytes; |
| | 4158 | 207 | | if (_connections.TryGetValue(change.Id, out NavigationConnectionOverlayOperation prior)) |
| | 23 | 208 | | retainedPayloadBytes -= NavigationMapOverlayDelta.EstimateRetainedPayload(prior); |
| | | 209 | | int copied; |
| | | 210 | | PersistentStringMap<NavigationConnectionOverlayOperation> connections; |
| | 4158 | 211 | | if (change.Kind == NavigationConnectionOverlayOperationKind.RevertToBake) |
| | 2 | 212 | | connections = _connections.Remove(change.Id, out _, out copied); |
| | | 213 | | else |
| | | 214 | | { |
| | 4156 | 215 | | connections = _connections.Set(change.Id, change, out copied); |
| | 4156 | 216 | | retainedPayloadBytes = checked( |
| | 4156 | 217 | | retainedPayloadBytes + NavigationMapOverlayDelta.EstimateRetainedPayload(change)); |
| | | 218 | | } |
| | 4158 | 219 | | return new NavigationMapOverlayState( |
| | 4158 | 220 | | _cells, |
| | 4158 | 221 | | connections, |
| | 4158 | 222 | | _transitions, |
| | 4158 | 223 | | operationSequence, |
| | 4158 | 224 | | copied, |
| | 4158 | 225 | | retainedPayloadBytes); |
| | | 226 | | } |
| | | 227 | | |
| | | 228 | | internal NavigationMapOverlayState Apply( |
| | | 229 | | TraversalTransitionOverlayOperation change, |
| | | 230 | | long operationSequence) |
| | | 231 | | { |
| | 4121 | 232 | | long retainedPayloadBytes = RetainedPayloadBytes; |
| | 4121 | 233 | | if (_transitions.TryGetValue(change.Id, out TraversalTransitionOverlayOperation prior)) |
| | 9 | 234 | | retainedPayloadBytes -= NavigationMapOverlayDelta.EstimateRetainedPayload(prior); |
| | | 235 | | int copied; |
| | | 236 | | PersistentStringMap<TraversalTransitionOverlayOperation> transitions; |
| | 4121 | 237 | | if (change.Kind == TraversalTransitionOverlayOperationKind.RevertToBake) |
| | 1 | 238 | | transitions = _transitions.Remove(change.Id, out _, out copied); |
| | | 239 | | else |
| | | 240 | | { |
| | 4120 | 241 | | transitions = _transitions.Set(change.Id, change, out copied); |
| | 4120 | 242 | | retainedPayloadBytes = checked( |
| | 4120 | 243 | | retainedPayloadBytes + NavigationMapOverlayDelta.EstimateRetainedPayload(change)); |
| | | 244 | | } |
| | 4121 | 245 | | return new NavigationMapOverlayState( |
| | 4121 | 246 | | _cells, |
| | 4121 | 247 | | _connections, |
| | 4121 | 248 | | transitions, |
| | 4121 | 249 | | operationSequence, |
| | 4121 | 250 | | copied, |
| | 4121 | 251 | | retainedPayloadBytes); |
| | | 252 | | } |
| | | 253 | | } |