| | | 1 | | //======================================================================= |
| | | 2 | | // NavigationBakedCellLookup.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>Identifies the measured baked-cell lookup representation.</summary> |
| | | 14 | | public enum NavigationCellLookupKind |
| | | 15 | | { |
| | | 16 | | /// <summary>Canonical baked entries are searched without address-volume storage.</summary> |
| | | 17 | | Sorted = 0, |
| | | 18 | | /// <summary>A direct address table is cheaper at the authored navigation density.</summary> |
| | | 19 | | Direct = 1 |
| | | 20 | | } |
| | | 21 | | |
| | | 22 | | /// <summary>Selects lookup representation from authored density, independent of GridForge storage kind.</summary> |
| | | 23 | | internal sealed class NavigationBakedCellLookup |
| | | 24 | | { |
| | | 25 | | // Keep the direct table below 4 MiB per map and require at least 50% authored density. |
| | | 26 | | internal const long MaximumDirectTableBytes = 4_194_304; |
| | | 27 | | internal const int MinimumDirectDensityDivisor = 2; |
| | | 28 | | private readonly NavigationMap _map; |
| | | 29 | | private readonly int[]? _directSlots; |
| | | 30 | | |
| | 2027 | 31 | | private NavigationBakedCellLookup(NavigationMap map, int[]? directSlots) |
| | | 32 | | { |
| | 2027 | 33 | | _map = map; |
| | 2027 | 34 | | _directSlots = directSlots; |
| | 2027 | 35 | | Kind = directSlots == null ? NavigationCellLookupKind.Sorted : NavigationCellLookupKind.Direct; |
| | 2027 | 36 | | } |
| | | 37 | | |
| | | 38 | | internal NavigationCellLookupKind Kind { get; } |
| | | 39 | | |
| | 38088 | 40 | | internal long RetainedBytes => _directSlots == null ? 16L : 24L + ((long)_directSlots.Length * sizeof(int)); |
| | | 41 | | |
| | | 42 | | internal static NavigationBakedCellLookup Create(NavigationMap map) |
| | | 43 | | { |
| | 2027 | 44 | | int addressCount = map.GridBinding.AddressCount; |
| | 2027 | 45 | | long directBytes = (long)addressCount * sizeof(int); |
| | 2027 | 46 | | if (addressCount > map.CellSpan.Length * MinimumDirectDensityDivisor |
| | 2027 | 47 | | || directBytes > MaximumDirectTableBytes) |
| | 1161 | 48 | | return new NavigationBakedCellLookup(map, null); |
| | | 49 | | |
| | 866 | 50 | | var slots = new int[addressCount]; |
| | 866 | 51 | | Array.Fill(slots, -1); |
| | 38710 | 52 | | for (int i = 0; i < map.CellSpan.Length; i++) |
| | 18489 | 53 | | slots[GetLinearAddress(map, map.CellSpan[i].Index)] = i; |
| | 866 | 54 | | return new NavigationBakedCellLookup(map, slots); |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | internal int Find(VoxelIndex index) |
| | | 58 | | { |
| | 4691609 | 59 | | if (_directSlots == null) |
| | 3721653 | 60 | | return _map.FindCellIndex(index); |
| | 969956 | 61 | | if (!_map.GridBinding.IsValidIndex(index)) |
| | 2 | 62 | | return -1; |
| | 969954 | 63 | | return _directSlots[GetLinearAddress(_map, index)]; |
| | | 64 | | } |
| | | 65 | | |
| | 988443 | 66 | | private static int GetLinearAddress(NavigationMap map, VoxelIndex index) => checked( |
| | 988443 | 67 | | ((index.x * map.GridBinding.Height) + index.y) * map.GridBinding.Length + index.z); |
| | | 68 | | } |