| | | 1 | | //======================================================================= |
| | | 2 | | // NavigationBaselineRebuild.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.Runtime.InteropServices; |
| | | 10 | | using GridForge.Grids; |
| | | 11 | | using GridForge.Grids.Topology; |
| | | 12 | | using GridForge.Spatial; |
| | | 13 | | |
| | | 14 | | namespace Trailblazer.Pathing; |
| | | 15 | | |
| | | 16 | | /// <summary>Accumulates one immutable requested-address baseline under a finite frame budget.</summary> |
| | | 17 | | internal sealed class NavigationBaselineRebuild |
| | | 18 | | { |
| | | 19 | | private readonly NavigationMap _map; |
| | | 20 | | private readonly NavigationMapInstance _source; |
| | | 21 | | private readonly long _bakeVersion; |
| | | 22 | | private readonly long _overlayHighWater; |
| | | 23 | | private readonly long _dynamicSlotGeneration; |
| | | 24 | | private readonly int _dynamicSlotCount; |
| | | 25 | | private readonly int _addressCount; |
| | | 26 | | private readonly long _pageVersion; |
| | | 27 | | private readonly bool _discoversDefault; |
| | | 28 | | private GridCoveredAddressCursor? _coveredCursor; |
| | | 29 | | private NavigationMapInstance? _defaultCandidate; |
| | 717 | 30 | | private PersistentVoxelIndexMap<NavigationDynamicCellSlot> _defaultSeedSlots = |
| | 717 | 31 | | PersistentVoxelIndexMap<NavigationDynamicCellSlot>.Empty; |
| | 717 | 32 | | private PersistentIntMap<VoxelIndex> _defaultSeedIndexes = |
| | 717 | 33 | | PersistentIntMap<VoxelIndex>.Empty; |
| | 717 | 34 | | private NavigationSurfaceComponentKeySet _structuralChangedStates = |
| | 717 | 35 | | NavigationSurfaceComponentKeySet.Empty; |
| | | 36 | | private GridCoveredAddressGeneration _coveredGeneration; |
| | | 37 | | private bool _coveredGenerationBound; |
| | 717 | 38 | | private PersistentIntMap<NavigationPhysicalPage> _pages = |
| | 717 | 39 | | PersistentIntMap<NavigationPhysicalPage>.Empty; |
| | | 40 | | private int _cursor; |
| | | 41 | | private int _bakedCursor; |
| | | 42 | | private int _dynamicCursor; |
| | | 43 | | private int _defaultSeedCursor; |
| | | 44 | | private int _omittedDefaultSeedSlotCount; |
| | | 45 | | private int _omittedPhysicalDefaultSlotCount; |
| | | 46 | | private ulong _capturedChangeSequence; |
| | | 47 | | private long _worldSpawnToken; |
| | 717 | 48 | | private ushort _gridIndex = ushort.MaxValue; |
| | | 49 | | private long _gridSpawnToken; |
| | | 50 | | private ulong _gridLastChangeSequence; |
| | | 51 | | private bool _hasIdentity; |
| | | 52 | | private bool _completed; |
| | | 53 | | private bool _capacityBlocked; |
| | | 54 | | private bool _hasNewDefaultSlot; |
| | | 55 | | private bool _defaultPhysicalAddressSetChanged; |
| | | 56 | | private NavigationGridBaselineCapture _completedCapture; |
| | | 57 | | |
| | 717 | 58 | | internal NavigationBaselineRebuild(NavigationMapInstance instance) |
| | | 59 | | { |
| | 717 | 60 | | _source = instance; |
| | 717 | 61 | | _map = instance.Map; |
| | 717 | 62 | | _bakeVersion = instance.BakeVersion; |
| | 717 | 63 | | _overlayHighWater = instance.Overlay.HighWaterSequence; |
| | 717 | 64 | | _dynamicSlotGeneration = instance.DynamicSlotGeneration; |
| | 717 | 65 | | _dynamicSlotCount = instance.DynamicSlotCount; |
| | 717 | 66 | | _addressCount = instance.AddressCount; |
| | 717 | 67 | | _pageVersion = instance.InstanceVersion; |
| | 717 | 68 | | _discoversDefault = instance.Map.DefaultCell.HasValue; |
| | 717 | 69 | | if (_discoversDefault) |
| | 86 | 70 | | _coveredCursor = new GridCoveredAddressCursor(generationCapacity: 1); |
| | 717 | 71 | | } |
| | | 72 | | |
| | 172 | 73 | | internal string MapId => _map.MapId; |
| | | 74 | | |
| | 1141 | 75 | | internal long RetainedBytes => _discoversDefault |
| | 1141 | 76 | | ? checked(320L |
| | 1141 | 77 | | + _coveredCursor!.RetainedBytes |
| | 1141 | 78 | | + (_defaultCandidate?.DefaultBaselineRetainedBytes |
| | 1141 | 79 | | ?? _defaultSeedSlots.RetainedBytes + _defaultSeedIndexes.RetainedBytes) |
| | 1141 | 80 | | + _structuralChangedStates.RetainedBytes) |
| | 1141 | 81 | | : checked(192L + _pages.RetainedBytes + ((long)_pages.Count * 320L)); |
| | | 82 | | |
| | 1137 | 83 | | internal int PersistentPageCount => _discoversDefault |
| | 1137 | 84 | | ? checked(1 |
| | 1137 | 85 | | + (_defaultCandidate?.DefaultBaselinePersistentPages |
| | 1137 | 86 | | ?? 2 |
| | 1137 | 87 | | + _defaultSeedSlots.PersistentNodeCount |
| | 1137 | 88 | | + _defaultSeedIndexes.PersistentNodeCount) |
| | 1137 | 89 | | + _structuralChangedStates.PersistentPageCount) |
| | 1137 | 90 | | : 1 + (_pages.PersistentNodeCount * 2); |
| | | 91 | | |
| | 228 | 92 | | internal bool IsComplete => _completed; |
| | | 93 | | |
| | 13 | 94 | | internal bool IsCapacityBlocked => _capacityBlocked; |
| | | 95 | | |
| | 236 | 96 | | internal bool RequiresCoveredDiscovery => _discoversDefault; |
| | | 97 | | |
| | | 98 | | internal bool TryGetCompletedCapture(out NavigationGridBaselineCapture capture) |
| | | 99 | | { |
| | 102 | 100 | | capture = _completedCapture; |
| | 102 | 101 | | return _completed; |
| | | 102 | | } |
| | | 103 | | |
| | | 104 | | internal static void GetRetainedTotals( |
| | | 105 | | PersistentStringMap<NavigationBaselineRebuild> rebuilds, |
| | | 106 | | out long retainedBytes, |
| | | 107 | | out int persistentPageCount) |
| | | 108 | | { |
| | 63096 | 109 | | if (rebuilds.Count == 0) |
| | | 110 | | { |
| | 62320 | 111 | | retainedBytes = 0; |
| | 62320 | 112 | | persistentPageCount = 0; |
| | 62320 | 113 | | return; |
| | | 114 | | } |
| | | 115 | | |
| | 776 | 116 | | retainedBytes = rebuilds.RetainedBytes; |
| | 776 | 117 | | persistentPageCount = 1 + rebuilds.PersistentNodeCount; |
| | 3202 | 118 | | for (int i = 0; i < rebuilds.Count; i++) |
| | | 119 | | { |
| | 825 | 120 | | NavigationBaselineRebuild rebuild = rebuilds.GetValueAt(i); |
| | 825 | 121 | | retainedBytes = checked(retainedBytes + rebuild.RetainedBytes); |
| | 825 | 122 | | persistentPageCount = checked(persistentPageCount + rebuild.PersistentPageCount); |
| | | 123 | | } |
| | 776 | 124 | | } |
| | | 125 | | |
| | | 126 | | internal static int CountCapacityBlocked( |
| | | 127 | | PersistentStringMap<NavigationBaselineRebuild> rebuilds) |
| | | 128 | | { |
| | 2201 | 129 | | int count = 0; |
| | 4416 | 130 | | for (int i = 0; i < rebuilds.Count; i++) |
| | | 131 | | { |
| | 7 | 132 | | if (rebuilds.GetValueAt(i).IsCapacityBlocked) |
| | 1 | 133 | | count++; |
| | | 134 | | } |
| | 2201 | 135 | | return count; |
| | | 136 | | } |
| | | 137 | | |
| | | 138 | | internal bool Matches(NavigationMapInstance instance) => |
| | 103 | 139 | | ReferenceEquals(_map, instance.Map) |
| | 103 | 140 | | && _bakeVersion == instance.BakeVersion |
| | 103 | 141 | | && _overlayHighWater == instance.Overlay.HighWaterSequence |
| | 103 | 142 | | && _dynamicSlotGeneration == instance.DynamicSlotGeneration |
| | 103 | 143 | | && _dynamicSlotCount == instance.DynamicSlotCount; |
| | | 144 | | |
| | | 145 | | internal static bool MatchesCapturedGridIdentity( |
| | | 146 | | long capturedWorldSpawnToken, |
| | | 147 | | ushort capturedGridIndex, |
| | | 148 | | long capturedGridSpawnToken, |
| | | 149 | | ulong capturedGridLastChangeSequence, |
| | | 150 | | long expectedWorldSpawnToken, |
| | | 151 | | ushort expectedGridIndex, |
| | | 152 | | long expectedGridSpawnToken, |
| | | 153 | | ulong expectedGridLastChangeSequence) => |
| | 117 | 154 | | capturedWorldSpawnToken == expectedWorldSpawnToken |
| | 117 | 155 | | && capturedGridIndex == expectedGridIndex |
| | 117 | 156 | | && capturedGridSpawnToken == expectedGridSpawnToken |
| | 117 | 157 | | && capturedGridLastChangeSequence == expectedGridLastChangeSequence; |
| | | 158 | | |
| | | 159 | | internal static bool IsCoveredBaselineCurrent( |
| | | 160 | | bool captured, |
| | | 161 | | GridNavigationBaseline? baseline, |
| | | 162 | | ulong expectedCapturedChangeSequence, |
| | | 163 | | long expectedWorldSpawnToken, |
| | | 164 | | ushort expectedGridIndex, |
| | | 165 | | long expectedGridSpawnToken, |
| | | 166 | | ulong expectedGridLastChangeSequence) => |
| | 83 | 167 | | captured |
| | 83 | 168 | | && baseline != null |
| | 83 | 169 | | && baseline.CapturedChangeSequence == expectedCapturedChangeSequence |
| | 83 | 170 | | && MatchesCapturedGridIdentity( |
| | 83 | 171 | | baseline.WorldSpawnToken, |
| | 83 | 172 | | baseline.GridIndex, |
| | 83 | 173 | | baseline.GridSpawnToken, |
| | 83 | 174 | | baseline.GridLastChangeSequence, |
| | 83 | 175 | | expectedWorldSpawnToken, |
| | 83 | 176 | | expectedGridIndex, |
| | 83 | 177 | | expectedGridSpawnToken, |
| | 83 | 178 | | expectedGridLastChangeSequence); |
| | | 179 | | |
| | | 180 | | internal int Advance( |
| | | 181 | | GridWorld world, |
| | | 182 | | NavigationMapInstance instance, |
| | | 183 | | int maximumAddresses, |
| | | 184 | | long maximumRetainedBytes, |
| | | 185 | | int maximumPersistentPages, |
| | | 186 | | Span<VoxelIndex> addressScratch, |
| | | 187 | | Span<GridCoveredAddress> coveredAddressScratch, |
| | | 188 | | out NavigationGridBaselineCapture capture, |
| | | 189 | | out bool completed) |
| | | 190 | | { |
| | 808 | 191 | | capture = default; |
| | 808 | 192 | | completed = false; |
| | 808 | 193 | | _completed = false; |
| | 808 | 194 | | if (_capacityBlocked) |
| | 2 | 195 | | return 0; |
| | 806 | 196 | | if (maximumAddresses <= 0) |
| | 16 | 197 | | return 0; |
| | 790 | 198 | | if (_discoversDefault) |
| | | 199 | | { |
| | 125 | 200 | | return AdvanceDefaultDiscovery( |
| | 125 | 201 | | world, |
| | 125 | 202 | | maximumAddresses, |
| | 125 | 203 | | maximumRetainedBytes, |
| | 125 | 204 | | maximumPersistentPages, |
| | 125 | 205 | | addressScratch, |
| | 125 | 206 | | coveredAddressScratch, |
| | 125 | 207 | | out capture, |
| | 125 | 208 | | out completed); |
| | | 209 | | } |
| | | 210 | | |
| | 665 | 211 | | int count = Math.Min(maximumAddresses, _addressCount - _cursor); |
| | 665 | 212 | | Span<VoxelIndex> requestedScratch = addressScratch.Slice(0, count); |
| | 665 | 213 | | int bakedCursor = _bakedCursor; |
| | 665 | 214 | | int dynamicCursor = _dynamicCursor; |
| | 665 | 215 | | instance.CopyCanonicalAddressChunk( |
| | 665 | 216 | | ref bakedCursor, |
| | 665 | 217 | | ref dynamicCursor, |
| | 665 | 218 | | requestedScratch); |
| | 665 | 219 | | ReadOnlySpan<VoxelIndex> requested = requestedScratch; |
| | 665 | 220 | | if (!world.TryCaptureNavigationBaseline( |
| | 665 | 221 | | _map.GridBinding.Key, |
| | 665 | 222 | | requested, |
| | 665 | 223 | | out GridNavigationBaseline? baseline) |
| | 665 | 224 | | || baseline == null) |
| | | 225 | | { |
| | 1 | 226 | | completed = true; |
| | 1 | 227 | | _completed = true; |
| | 1 | 228 | | capture = new NavigationGridBaselineCapture(0, baseline: null, isDelta: false); |
| | 1 | 229 | | _completedCapture = capture; |
| | 1 | 230 | | return count; |
| | | 231 | | } |
| | | 232 | | |
| | 664 | 233 | | if (_hasIdentity |
| | 664 | 234 | | && !MatchesCapturedGridIdentity( |
| | 664 | 235 | | baseline.WorldSpawnToken, |
| | 664 | 236 | | baseline.GridIndex, |
| | 664 | 237 | | baseline.GridSpawnToken, |
| | 664 | 238 | | baseline.GridLastChangeSequence, |
| | 664 | 239 | | _worldSpawnToken, |
| | 664 | 240 | | _gridIndex, |
| | 664 | 241 | | _gridSpawnToken, |
| | 664 | 242 | | _gridLastChangeSequence)) |
| | | 243 | | { |
| | 1 | 244 | | ResetProgress(); |
| | 1 | 245 | | return count; |
| | | 246 | | } |
| | | 247 | | |
| | 663 | 248 | | if (!_hasIdentity) |
| | | 249 | | { |
| | 631 | 250 | | _capturedChangeSequence = baseline.CapturedChangeSequence; |
| | 631 | 251 | | _worldSpawnToken = baseline.WorldSpawnToken; |
| | 631 | 252 | | _gridIndex = baseline.GridIndex; |
| | 631 | 253 | | _gridSpawnToken = baseline.GridSpawnToken; |
| | 631 | 254 | | _gridLastChangeSequence = baseline.GridLastChangeSequence; |
| | 631 | 255 | | _hasIdentity = true; |
| | | 256 | | } |
| | 663 | 257 | | _capturedChangeSequence = baseline.CapturedChangeSequence; |
| | | 258 | | |
| | 663 | 259 | | PersistentIntMap<NavigationPhysicalPage> pages = instance.AppendPhysicalBaselinePages( |
| | 663 | 260 | | _pages, |
| | 663 | 261 | | baseline.VoxelStates, |
| | 663 | 262 | | _pageVersion); |
| | 663 | 263 | | long retainedBytes = EstimateRetainedBytes(pages); |
| | 663 | 264 | | int persistentPageCount = EstimatePersistentPageCount(pages); |
| | 663 | 265 | | if (retainedBytes > maximumRetainedBytes |
| | 663 | 266 | | || persistentPageCount > maximumPersistentPages) |
| | | 267 | | { |
| | 1 | 268 | | _capacityBlocked = true; |
| | 1 | 269 | | return count; |
| | | 270 | | } |
| | | 271 | | |
| | 662 | 272 | | _pages = pages; |
| | 662 | 273 | | _bakedCursor = bakedCursor; |
| | 662 | 274 | | _dynamicCursor = dynamicCursor; |
| | 662 | 275 | | _cursor += count; |
| | 662 | 276 | | if (_cursor != _addressCount) |
| | 36 | 277 | | return count; |
| | | 278 | | |
| | 626 | 279 | | completed = true; |
| | 626 | 280 | | _completed = true; |
| | 626 | 281 | | capture = new NavigationGridBaselineCapture( |
| | 626 | 282 | | _addressCount, |
| | 626 | 283 | | _pages, |
| | 626 | 284 | | _capturedChangeSequence, |
| | 626 | 285 | | _worldSpawnToken, |
| | 626 | 286 | | _gridIndex, |
| | 626 | 287 | | _gridSpawnToken, |
| | 626 | 288 | | _gridLastChangeSequence, |
| | 626 | 289 | | _map.GridBinding.Key); |
| | 626 | 290 | | _completedCapture = capture; |
| | 626 | 291 | | return count; |
| | | 292 | | } |
| | | 293 | | |
| | | 294 | | private void ResetProgress() |
| | | 295 | | { |
| | 3 | 296 | | _pages = PersistentIntMap<NavigationPhysicalPage>.Empty; |
| | 3 | 297 | | _cursor = 0; |
| | 3 | 298 | | _bakedCursor = 0; |
| | 3 | 299 | | _dynamicCursor = 0; |
| | 3 | 300 | | _capturedChangeSequence = 0; |
| | 3 | 301 | | _worldSpawnToken = 0; |
| | 3 | 302 | | _gridIndex = ushort.MaxValue; |
| | 3 | 303 | | _gridSpawnToken = 0; |
| | 3 | 304 | | _gridLastChangeSequence = 0; |
| | 3 | 305 | | _hasIdentity = false; |
| | 3 | 306 | | _completed = false; |
| | 3 | 307 | | _capacityBlocked = false; |
| | 3 | 308 | | _completedCapture = default; |
| | 3 | 309 | | if (_discoversDefault) |
| | | 310 | | { |
| | 2 | 311 | | _defaultCandidate = null; |
| | 2 | 312 | | _defaultSeedSlots = PersistentVoxelIndexMap<NavigationDynamicCellSlot>.Empty; |
| | 2 | 313 | | _defaultSeedIndexes = PersistentIntMap<VoxelIndex>.Empty; |
| | 2 | 314 | | _defaultSeedCursor = 0; |
| | 2 | 315 | | _omittedDefaultSeedSlotCount = 0; |
| | 2 | 316 | | _omittedPhysicalDefaultSlotCount = 0; |
| | 2 | 317 | | _hasNewDefaultSlot = false; |
| | 2 | 318 | | _defaultPhysicalAddressSetChanged = false; |
| | 2 | 319 | | _coveredGeneration = default; |
| | 2 | 320 | | _coveredGenerationBound = false; |
| | 2 | 321 | | _structuralChangedStates = NavigationSurfaceComponentKeySet.Empty; |
| | | 322 | | } |
| | 3 | 323 | | } |
| | | 324 | | |
| | | 325 | | private int AdvanceDefaultDiscovery( |
| | | 326 | | GridWorld world, |
| | | 327 | | int maximumAddresses, |
| | | 328 | | long maximumRetainedBytes, |
| | | 329 | | int maximumPersistentPages, |
| | | 330 | | Span<VoxelIndex> addressScratch, |
| | | 331 | | Span<GridCoveredAddress> coveredAddressScratch, |
| | | 332 | | out NavigationGridBaselineCapture capture, |
| | | 333 | | out bool completed) |
| | | 334 | | { |
| | 125 | 335 | | capture = default; |
| | 125 | 336 | | completed = false; |
| | 125 | 337 | | int seedProbes = AdvanceDefaultSeed( |
| | 125 | 338 | | maximumAddresses, |
| | 125 | 339 | | maximumRetainedBytes, |
| | 125 | 340 | | maximumPersistentPages); |
| | 125 | 341 | | if (_capacityBlocked || seedProbes != 0 || _defaultCandidate == null) |
| | 45 | 342 | | return seedProbes; |
| | 80 | 343 | | GridCoveredAddressCursor cursor = _coveredCursor!; |
| | 80 | 344 | | if (cursor.Status == GridCoveredAddressCursorStatus.Stale) |
| | | 345 | | { |
| | 73 | 346 | | if (!world.TryCaptureNavigationBaseline( |
| | 73 | 347 | | _map.GridBinding.Key, |
| | 73 | 348 | | ReadOnlySpan<VoxelIndex>.Empty, |
| | 73 | 349 | | out GridNavigationBaseline? identity) |
| | 73 | 350 | | || identity == null) |
| | | 351 | | { |
| | 1 | 352 | | completed = true; |
| | 1 | 353 | | _completed = true; |
| | 1 | 354 | | capture = new NavigationGridBaselineCapture( |
| | 1 | 355 | | 0, |
| | 1 | 356 | | baseline: null, |
| | 1 | 357 | | isDelta: false); |
| | 1 | 358 | | _completedCapture = capture; |
| | 1 | 359 | | return 0; |
| | | 360 | | } |
| | | 361 | | |
| | 72 | 362 | | _capturedChangeSequence = identity.CapturedChangeSequence; |
| | 72 | 363 | | _worldSpawnToken = identity.WorldSpawnToken; |
| | 72 | 364 | | _gridIndex = identity.GridIndex; |
| | 72 | 365 | | _gridSpawnToken = identity.GridSpawnToken; |
| | 72 | 366 | | _gridLastChangeSequence = identity.GridLastChangeSequence; |
| | 72 | 367 | | _hasIdentity = true; |
| | 72 | 368 | | _coveredGeneration = new GridCoveredAddressGeneration( |
| | 72 | 369 | | identity.ConfigurationKey, |
| | 72 | 370 | | identity.GridIndex, |
| | 72 | 371 | | identity.GridSpawnToken, |
| | 72 | 372 | | identity.GridLastChangeSequence); |
| | 72 | 373 | | bool began = world.TryBeginCoveredAddresses( |
| | 72 | 374 | | cursor, |
| | 72 | 375 | | identity.ConfigurationKey.BoundsMin, |
| | 72 | 376 | | identity.ConfigurationKey.BoundsMax, |
| | 72 | 377 | | eligibleGenerationCount: 1, |
| | 72 | 378 | | identity.ConfigurationKey); |
| | | 379 | | System.Diagnostics.Debug.Assert(began, |
| | | 380 | | "The one-generation baseline cursor is constructed with exact capacity one."); |
| | | 381 | | } |
| | | 382 | | |
| | 79 | 383 | | ReadOnlySpan<GridCoveredAddressGeneration> input = _coveredGenerationBound |
| | 79 | 384 | | ? ReadOnlySpan<GridCoveredAddressGeneration>.Empty |
| | 79 | 385 | | : MemoryMarshal.CreateReadOnlySpan(ref _coveredGeneration, 1); |
| | 79 | 386 | | int outputLimit = Math.Min( |
| | 79 | 387 | | maximumAddresses, |
| | 79 | 388 | | Math.Min(addressScratch.Length, coveredAddressScratch.Length)); |
| | 79 | 389 | | GridCoveredAddressCursorStatus status = world.AdvanceCoveredAddresses( |
| | 79 | 390 | | cursor, |
| | 79 | 391 | | input, |
| | 79 | 392 | | coveredAddressScratch, |
| | 79 | 393 | | lookupProbeLimit: _coveredGenerationBound ? 0 : 1, |
| | 79 | 394 | | addressProbeLimit: maximumAddresses, |
| | 79 | 395 | | outputLimit, |
| | 79 | 396 | | out _, |
| | 79 | 397 | | out int addressProbes, |
| | 79 | 398 | | out int inputsConsumed, |
| | 79 | 399 | | out int outputCount); |
| | 79 | 400 | | _coveredGenerationBound |= inputsConsumed != 0; |
| | 79 | 401 | | bool invalidated = status == GridCoveredAddressCursorStatus.Stale; |
| | | 402 | | System.Diagnostics.Debug.Assert(!invalidated || outputCount == 0, |
| | | 403 | | "A stale covered-address cursor discards the run without emitting output."); |
| | | 404 | | |
| | 910 | 405 | | for (int i = 0; i < outputCount; i++) |
| | 376 | 406 | | addressScratch[i] = coveredAddressScratch[i].VoxelIndex; |
| | 79 | 407 | | bool captured = false; |
| | 79 | 408 | | GridNavigationBaseline? baseline = null; |
| | 79 | 409 | | if (outputCount > 0) |
| | | 410 | | { |
| | 78 | 411 | | captured = world.TryCaptureNavigationBaseline( |
| | 78 | 412 | | _map.GridBinding.Key, |
| | 78 | 413 | | addressScratch.Slice(0, outputCount), |
| | 78 | 414 | | out baseline); |
| | | 415 | | } |
| | 79 | 416 | | return FinalizeCoveredAddressAdvance( |
| | 79 | 417 | | status, |
| | 79 | 418 | | addressProbes, |
| | 79 | 419 | | outputCount, |
| | 79 | 420 | | invalidated, |
| | 79 | 421 | | captured, |
| | 79 | 422 | | baseline, |
| | 79 | 423 | | cursor.RunStamp.ChangeSequence, |
| | 79 | 424 | | cursor.RunStamp.WorldSpawnToken, |
| | 79 | 425 | | maximumRetainedBytes, |
| | 79 | 426 | | maximumPersistentPages, |
| | 79 | 427 | | out capture, |
| | 79 | 428 | | out completed); |
| | | 429 | | } |
| | | 430 | | |
| | | 431 | | internal int FinalizeCoveredAddressAdvance( |
| | | 432 | | GridCoveredAddressCursorStatus status, |
| | | 433 | | int addressProbes, |
| | | 434 | | int outputCount, |
| | | 435 | | bool invalidated, |
| | | 436 | | bool captured, |
| | | 437 | | GridNavigationBaseline? baseline, |
| | | 438 | | ulong expectedCapturedChangeSequence, |
| | | 439 | | long expectedWorldSpawnToken, |
| | | 440 | | long maximumRetainedBytes, |
| | | 441 | | int maximumPersistentPages, |
| | | 442 | | out NavigationGridBaselineCapture capture, |
| | | 443 | | out bool completed) |
| | | 444 | | { |
| | 80 | 445 | | capture = default; |
| | 80 | 446 | | completed = false; |
| | 80 | 447 | | if (outputCount > 0) |
| | | 448 | | { |
| | 79 | 449 | | invalidated |= !IsCoveredBaselineCurrent( |
| | 79 | 450 | | captured, |
| | 79 | 451 | | baseline, |
| | 79 | 452 | | expectedCapturedChangeSequence, |
| | 79 | 453 | | expectedWorldSpawnToken, |
| | 79 | 454 | | _gridIndex, |
| | 79 | 455 | | _gridSpawnToken, |
| | 79 | 456 | | _gridLastChangeSequence); |
| | 79 | 457 | | if (!invalidated) |
| | | 458 | | { |
| | 78 | 459 | | NavigationMapInstance next = _defaultCandidate!.AppendDefaultBaselineStates( |
| | 78 | 460 | | _source, |
| | 78 | 461 | | baseline!.VoxelStates, |
| | 78 | 462 | | baseline.CapturedChangeSequence, |
| | 78 | 463 | | _pageVersion); |
| | 908 | 464 | | for (int i = 0; i < baseline.VoxelStates.Length; i++) |
| | | 465 | | { |
| | 376 | 466 | | VoxelIndex index = baseline.VoxelStates[i].VoxelIndex; |
| | 376 | 467 | | bool sourcePresent = _source.IsPhysicallyPresent(index); |
| | 376 | 468 | | bool nextPresent = next.IsPhysicallyPresent(index); |
| | 376 | 469 | | _defaultPhysicalAddressSetChanged |= sourcePresent != nextPresent; |
| | 376 | 470 | | bool sourceHasDynamic = _source.TryGetDynamicSlot( |
| | 376 | 471 | | index, |
| | 376 | 472 | | out NavigationDynamicCellSlot sourceSlot); |
| | 376 | 473 | | bool nextHasDynamic = next.TryGetDynamicSlot( |
| | 376 | 474 | | index, |
| | 376 | 475 | | out NavigationDynamicCellSlot nextSlot); |
| | 376 | 476 | | bool rediscoveredOmittedSlot = sourceHasDynamic |
| | 376 | 477 | | && nextHasDynamic |
| | 376 | 478 | | && sourceSlot.Slot == nextSlot.Slot |
| | 376 | 479 | | && !_defaultSeedSlots.TryGetValue(index, out _); |
| | 376 | 480 | | if (rediscoveredOmittedSlot) |
| | | 481 | | { |
| | | 482 | | System.Diagnostics.Debug.Assert(_omittedDefaultSeedSlotCount > 0, |
| | | 483 | | "Each unique rediscovered omitted seed owns one pending seed count."); |
| | 51 | 484 | | _omittedDefaultSeedSlotCount--; |
| | 51 | 485 | | UpdateStructuralStates( |
| | 51 | 486 | | index, |
| | 51 | 487 | | _source.GetEffectiveMedia(index), |
| | 51 | 488 | | add: false); |
| | | 489 | | } |
| | 376 | 490 | | if (sourcePresent |
| | 376 | 491 | | && nextPresent |
| | 376 | 492 | | && rediscoveredOmittedSlot) |
| | | 493 | | { |
| | | 494 | | System.Diagnostics.Debug.Assert(_omittedPhysicalDefaultSlotCount > 0, |
| | | 495 | | "Each physically present rediscovered omitted seed owns one pending physical count."); |
| | 50 | 496 | | _omittedPhysicalDefaultSlotCount--; |
| | | 497 | | } |
| | 376 | 498 | | if (!sourceHasDynamic && nextHasDynamic) |
| | | 499 | | { |
| | 125 | 500 | | _hasNewDefaultSlot = true; |
| | 125 | 501 | | UpdateStructuralStates(index, next.GetEffectiveMedia(index), add: true); |
| | | 502 | | } |
| | | 503 | | } |
| | 78 | 504 | | if (EstimateDefaultRetainedBytes(next) > maximumRetainedBytes |
| | 78 | 505 | | || EstimateDefaultPersistentPages(next) > maximumPersistentPages) |
| | | 506 | | { |
| | 1 | 507 | | _capacityBlocked = true; |
| | 1 | 508 | | return addressProbes; |
| | | 509 | | } |
| | 77 | 510 | | _defaultCandidate = next; |
| | 77 | 511 | | _cursor = checked(_cursor + outputCount); |
| | 77 | 512 | | _capturedChangeSequence = baseline.CapturedChangeSequence; |
| | | 513 | | } |
| | | 514 | | } |
| | 79 | 515 | | if (invalidated) |
| | 2 | 516 | | return ResetAfterDefaultDiscoveryInvalidation(addressProbes); |
| | | 517 | | |
| | 77 | 518 | | if (status != GridCoveredAddressCursorStatus.Complete) |
| | 7 | 519 | | return addressProbes; |
| | | 520 | | |
| | 70 | 521 | | NavigationMapInstance prepared = _defaultCandidate!.CompleteDefaultBaseline( |
| | 70 | 522 | | _pageVersion, |
| | 70 | 523 | | _omittedDefaultSeedSlotCount == 0 && !_hasNewDefaultSlot); |
| | 70 | 524 | | completed = true; |
| | 70 | 525 | | _completed = true; |
| | 70 | 526 | | capture = new NavigationGridBaselineCapture( |
| | 70 | 527 | | prepared, |
| | 70 | 528 | | _structuralChangedStates, |
| | 70 | 529 | | _defaultPhysicalAddressSetChanged || _omittedPhysicalDefaultSlotCount != 0, |
| | 70 | 530 | | prepared.AddressCount, |
| | 70 | 531 | | _capturedChangeSequence, |
| | 70 | 532 | | _worldSpawnToken, |
| | 70 | 533 | | _gridIndex, |
| | 70 | 534 | | _gridSpawnToken, |
| | 70 | 535 | | _gridLastChangeSequence, |
| | 70 | 536 | | _map.GridBinding.Key); |
| | 70 | 537 | | _completedCapture = capture; |
| | 70 | 538 | | return addressProbes; |
| | | 539 | | } |
| | | 540 | | |
| | | 541 | | private int ResetAfterDefaultDiscoveryInvalidation(int addressProbes) |
| | | 542 | | { |
| | 2 | 543 | | ResetProgress(); |
| | 2 | 544 | | return addressProbes; |
| | | 545 | | } |
| | | 546 | | |
| | | 547 | | private int AdvanceDefaultSeed( |
| | | 548 | | int maximumAddresses, |
| | | 549 | | long maximumRetainedBytes, |
| | | 550 | | int maximumPersistentPages) |
| | | 551 | | { |
| | 125 | 552 | | if (_defaultCandidate != null) |
| | 43 | 553 | | return 0; |
| | 82 | 554 | | int remaining = _source.DynamicSlotCount - _defaultSeedCursor; |
| | 82 | 555 | | int count = Math.Min(maximumAddresses, remaining); |
| | 82 | 556 | | PersistentVoxelIndexMap<NavigationDynamicCellSlot> slots = _defaultSeedSlots; |
| | 82 | 557 | | PersistentIntMap<VoxelIndex> indexes = _defaultSeedIndexes; |
| | 82 | 558 | | int omittedSlotCount = _omittedDefaultSeedSlotCount; |
| | 82 | 559 | | int omittedPhysicalSlotCount = _omittedPhysicalDefaultSlotCount; |
| | 82 | 560 | | NavigationSurfaceComponentKeySet structuralChangedStates = |
| | 82 | 561 | | _structuralChangedStates; |
| | 356 | 562 | | for (int i = 0; i < count; i++) |
| | | 563 | | { |
| | 96 | 564 | | _source.TryGetDefaultBaselineSeedSlot( |
| | 96 | 565 | | _defaultSeedCursor + i, |
| | 96 | 566 | | out NavigationDynamicCellSlot slot, |
| | 96 | 567 | | out bool retain); |
| | 96 | 568 | | if (retain) |
| | | 569 | | { |
| | 24 | 570 | | slots = slots.Set(slot.Index, slot); |
| | 24 | 571 | | indexes = indexes.Set(slot.Slot, slot.Index); |
| | | 572 | | } |
| | | 573 | | else |
| | | 574 | | { |
| | 72 | 575 | | omittedSlotCount++; |
| | 72 | 576 | | AddMediaStates( |
| | 72 | 577 | | _map.MapId, |
| | 72 | 578 | | slot.Index, |
| | 72 | 579 | | _source.GetEffectiveMedia(slot.Index), |
| | 72 | 580 | | ref structuralChangedStates); |
| | 72 | 581 | | if (_source.IsPhysicallyPresent(slot.Index)) |
| | 62 | 582 | | omittedPhysicalSlotCount++; |
| | | 583 | | } |
| | | 584 | | } |
| | 82 | 585 | | bool seedComplete = _defaultSeedCursor + count == _source.DynamicSlotCount; |
| | 82 | 586 | | NavigationMapInstance? candidate = seedComplete |
| | 82 | 587 | | ? _source.CreateDefaultBaselineSeed(_pageVersion, slots, indexes) |
| | 82 | 588 | | : null; |
| | 82 | 589 | | long retainedBytes = candidate == null |
| | 82 | 590 | | ? checked( |
| | 82 | 591 | | 320L |
| | 82 | 592 | | + _coveredCursor!.RetainedBytes |
| | 82 | 593 | | + slots.RetainedBytes |
| | 82 | 594 | | + indexes.RetainedBytes |
| | 82 | 595 | | + structuralChangedStates.RetainedBytes) |
| | 82 | 596 | | : EstimateDefaultRetainedBytes(candidate); |
| | 82 | 597 | | int retainedPages = candidate == null |
| | 82 | 598 | | ? checked( |
| | 82 | 599 | | 3 |
| | 82 | 600 | | + slots.PersistentNodeCount |
| | 82 | 601 | | + indexes.PersistentNodeCount |
| | 82 | 602 | | + structuralChangedStates.PersistentPageCount) |
| | 82 | 603 | | : EstimateDefaultPersistentPages(candidate); |
| | 82 | 604 | | if (retainedBytes > maximumRetainedBytes |
| | 82 | 605 | | || retainedPages > maximumPersistentPages) |
| | | 606 | | { |
| | 2 | 607 | | _capacityBlocked = true; |
| | 2 | 608 | | return count; |
| | | 609 | | } |
| | 80 | 610 | | _defaultSeedSlots = slots; |
| | 80 | 611 | | _defaultSeedIndexes = indexes; |
| | 80 | 612 | | _defaultSeedCursor += count; |
| | 80 | 613 | | _omittedDefaultSeedSlotCount = omittedSlotCount; |
| | 80 | 614 | | _omittedPhysicalDefaultSlotCount = omittedPhysicalSlotCount; |
| | 80 | 615 | | _structuralChangedStates = structuralChangedStates; |
| | 80 | 616 | | _defaultCandidate = candidate; |
| | 80 | 617 | | return count; |
| | | 618 | | } |
| | | 619 | | |
| | | 620 | | private void UpdateStructuralStates(VoxelIndex index, TraversalMedia media, bool add) |
| | | 621 | | { |
| | 176 | 622 | | for (TraversalMedium medium = TraversalMedium.Solid; |
| | 704 | 623 | | medium <= TraversalMedium.Liquid; |
| | 528 | 624 | | medium++) |
| | | 625 | | { |
| | 528 | 626 | | TraversalMedia bit = (TraversalMedia)NavigationMediumSlots<byte>.GetBit(medium); |
| | 528 | 627 | | if ((media & bit) == 0) |
| | | 628 | | continue; |
| | 187 | 629 | | var key = new NavigationSurfaceComponentKey( |
| | 187 | 630 | | new NavigationCellAddress(_map.MapId, index), |
| | 187 | 631 | | medium); |
| | 187 | 632 | | _structuralChangedStates = add |
| | 187 | 633 | | ? _structuralChangedStates.Add(key) |
| | 187 | 634 | | : _structuralChangedStates.Remove(key); |
| | | 635 | | } |
| | 176 | 636 | | } |
| | | 637 | | |
| | | 638 | | private static void AddMediaStates( |
| | | 639 | | string mapId, |
| | | 640 | | VoxelIndex index, |
| | | 641 | | TraversalMedia media, |
| | | 642 | | ref NavigationSurfaceComponentKeySet states) |
| | | 643 | | { |
| | 72 | 644 | | for (TraversalMedium medium = TraversalMedium.Solid; |
| | 288 | 645 | | medium <= TraversalMedium.Liquid; |
| | 216 | 646 | | medium++) |
| | | 647 | | { |
| | 216 | 648 | | TraversalMedia bit = (TraversalMedia)NavigationMediumSlots<byte>.GetBit(medium); |
| | 216 | 649 | | if ((media & bit) != 0) |
| | | 650 | | { |
| | 72 | 651 | | states = states.Add(new NavigationSurfaceComponentKey( |
| | 72 | 652 | | new NavigationCellAddress(mapId, index), |
| | 72 | 653 | | medium)); |
| | | 654 | | } |
| | | 655 | | } |
| | 72 | 656 | | } |
| | | 657 | | |
| | 155 | 658 | | private long EstimateDefaultRetainedBytes(NavigationMapInstance candidate) => checked( |
| | 155 | 659 | | 320L |
| | 155 | 660 | | + _coveredCursor!.RetainedBytes |
| | 155 | 661 | | + candidate.DefaultBaselineRetainedBytes |
| | 155 | 662 | | + _structuralChangedStates.RetainedBytes); |
| | | 663 | | |
| | 154 | 664 | | private int EstimateDefaultPersistentPages(NavigationMapInstance candidate) => checked( |
| | 154 | 665 | | 1 |
| | 154 | 666 | | + candidate.DefaultBaselinePersistentPages |
| | 154 | 667 | | + _structuralChangedStates.PersistentPageCount); |
| | | 668 | | |
| | | 669 | | private static long EstimateRetainedBytes( |
| | 663 | 670 | | PersistentIntMap<NavigationPhysicalPage> pages) => checked( |
| | 663 | 671 | | 192L |
| | 663 | 672 | | + pages.RetainedBytes |
| | 663 | 673 | | + ((long)pages.Count * 320L)); |
| | | 674 | | |
| | | 675 | | private static int EstimatePersistentPageCount( |
| | | 676 | | PersistentIntMap<NavigationPhysicalPage> pages) => |
| | 663 | 677 | | 1 + (pages.PersistentNodeCount * 2); |
| | | 678 | | } |