| | | 1 | | //======================================================================= |
| | | 2 | | // NavigationMap.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.Collections.Generic; |
| | | 10 | | using System.Collections.ObjectModel; |
| | | 11 | | using System.Runtime.CompilerServices; |
| | | 12 | | using FixedMathSharp; |
| | | 13 | | using GridForge.Configuration; |
| | | 14 | | using GridForge.Grids.Topology; |
| | | 15 | | using GridForge.Spatial; |
| | | 16 | | using SwiftCollections.Utility; |
| | | 17 | | |
| | | 18 | | namespace Trailblazer.Pathing; |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Stores one immutable, canonically ordered navigation bake for one normalized grid. |
| | | 22 | | /// </summary> |
| | | 23 | | public sealed class NavigationMap : IEquatable<NavigationMap> |
| | | 24 | | { |
| | 1 | 25 | | private static readonly HexDirection[] NativeHexDirections = |
| | 1 | 26 | | { |
| | 1 | 27 | | HexDirection.QNegative, |
| | 1 | 28 | | HexDirection.QNegativeRPositive, |
| | 1 | 29 | | HexDirection.RNegative, |
| | 1 | 30 | | HexDirection.RPositive, |
| | 1 | 31 | | HexDirection.QPositiveRNegative, |
| | 1 | 32 | | HexDirection.QPositive |
| | 1 | 33 | | }; |
| | | 34 | | |
| | 1 | 35 | | private static readonly RectangularDirection[] NativeRectangularDirections = |
| | 1 | 36 | | { |
| | 1 | 37 | | RectangularDirection.West, |
| | 1 | 38 | | RectangularDirection.South, |
| | 1 | 39 | | RectangularDirection.North, |
| | 1 | 40 | | RectangularDirection.East |
| | 1 | 41 | | }; |
| | | 42 | | |
| | | 43 | | private readonly NavigationCellEntry[] _cells; |
| | | 44 | | private readonly NavigationConnection[] _connections; |
| | | 45 | | private readonly TraversalTransitionDefinition[] _transitions; |
| | | 46 | | private readonly TraversalTransitionRule[] _transitionRules; |
| | | 47 | | private readonly NavigationCell? _defaultCell; |
| | | 48 | | private readonly GridNavigationPortal[] _nativePortalTemplates; |
| | | 49 | | private readonly ReadOnlyCollection<NavigationCellEntry> _cellView; |
| | | 50 | | private readonly ReadOnlyCollection<NavigationConnection> _connectionView; |
| | | 51 | | private readonly ReadOnlyCollection<TraversalTransitionDefinition> _transitionView; |
| | | 52 | | private readonly ReadOnlyCollection<TraversalTransitionRule> _transitionRuleView; |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// The stable host-owned map identifier. |
| | | 56 | | /// </summary> |
| | | 57 | | public string MapId { get; } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// The normalized storage-neutral GridForge configuration binding. |
| | | 61 | | /// </summary> |
| | | 62 | | public NormalizedGridConfiguration GridBinding { get; } |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// Authored cells in stable lexicographic topology-local index order. |
| | | 66 | | /// </summary> |
| | 3345 | 67 | | public IReadOnlyList<NavigationCellEntry> Cells => _cellView; |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// Directed source-owned connections in ordinal ID order. |
| | | 71 | | /// </summary> |
| | 2586 | 72 | | public IReadOnlyList<NavigationConnection> Connections => _connectionView; |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// Directed source-owned semantic transitions in ordinal ID order. |
| | | 76 | | /// </summary> |
| | 2326 | 77 | | public IReadOnlyList<TraversalTransitionDefinition> Transitions => _transitionView; |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Bounded procedural semantic transition rules in canonical ordinal ID order. |
| | | 81 | | /// </summary> |
| | 2 | 82 | | public IReadOnlyList<TraversalTransitionRule> TransitionRules => _transitionRuleView; |
| | | 83 | | |
| | | 84 | | /// <summary> |
| | | 85 | | /// Gets the optional complete fallback cell used when no explicit cell wins. |
| | | 86 | | /// </summary> |
| | 81861 | 87 | | public NavigationCell? DefaultCell => _defaultCell; |
| | | 88 | | |
| | 22724193 | 89 | | internal ReadOnlySpan<NavigationCellEntry> CellSpan => _cells; |
| | | 90 | | |
| | 6156 | 91 | | internal ReadOnlySpan<NavigationConnection> ConnectionSpan => _connections; |
| | | 92 | | |
| | 34607 | 93 | | internal ReadOnlySpan<TraversalTransitionDefinition> TransitionSpan => _transitions; |
| | | 94 | | |
| | 18263 | 95 | | internal ReadOnlySpan<TraversalTransitionRule> TransitionRuleSpan => _transitionRules; |
| | | 96 | | |
| | 4 | 97 | | internal int NativePortalTemplateCount => _nativePortalTemplates.Length; |
| | | 98 | | |
| | 2015 | 99 | | internal long NativePortalTemplateRetainedBytes => checked( |
| | 2015 | 100 | | 24L + ((long)_nativePortalTemplates.Length * Unsafe.SizeOf<GridNavigationPortal>())); |
| | | 101 | | |
| | 2037 | 102 | | internal NavigationMap( |
| | 2037 | 103 | | string mapId, |
| | 2037 | 104 | | NormalizedGridConfiguration gridBinding, |
| | 2037 | 105 | | NavigationCellEntry[] cells, |
| | 2037 | 106 | | NavigationConnection[] connections, |
| | 2037 | 107 | | TraversalTransitionDefinition[] transitions, |
| | 2037 | 108 | | TraversalTransitionRule[] transitionRules, |
| | 2037 | 109 | | NavigationCell? defaultCell) |
| | | 110 | | { |
| | 2037 | 111 | | MapId = mapId; |
| | 2037 | 112 | | GridBinding = gridBinding; |
| | 2037 | 113 | | _cells = cells; |
| | 2037 | 114 | | _connections = connections; |
| | 2037 | 115 | | _transitions = transitions; |
| | 2037 | 116 | | _transitionRules = transitionRules; |
| | 2037 | 117 | | _defaultCell = defaultCell; |
| | 2037 | 118 | | _nativePortalTemplates = CompileNativePortalTemplates(gridBinding); |
| | 2037 | 119 | | _cellView = Array.AsReadOnly(_cells); |
| | 2037 | 120 | | _connectionView = Array.AsReadOnly(_connections); |
| | 2037 | 121 | | _transitionView = Array.AsReadOnly(_transitions); |
| | 2037 | 122 | | _transitionRuleView = Array.AsReadOnly(_transitionRules); |
| | 2037 | 123 | | } |
| | | 124 | | |
| | | 125 | | /// <inheritdoc/> |
| | | 126 | | public bool Equals(NavigationMap? other) |
| | | 127 | | { |
| | 20 | 128 | | if (ReferenceEquals(this, other)) |
| | 1 | 129 | | return true; |
| | 19 | 130 | | if (other is null |
| | 19 | 131 | | || !string.Equals(MapId, other.MapId, StringComparison.Ordinal) |
| | 19 | 132 | | || GridBinding.Key != other.GridBinding.Key |
| | 19 | 133 | | || !_defaultCell.Equals(other._defaultCell) |
| | 19 | 134 | | || _cells.Length != other._cells.Length |
| | 19 | 135 | | || _connections.Length != other._connections.Length |
| | 19 | 136 | | || _transitions.Length != other._transitions.Length |
| | 19 | 137 | | || _transitionRules.Length != other._transitionRules.Length) |
| | | 138 | | { |
| | 8 | 139 | | return false; |
| | | 140 | | } |
| | | 141 | | |
| | 52 | 142 | | for (int i = 0; i < _cells.Length; i++) |
| | | 143 | | { |
| | 16 | 144 | | if (!_cells[i].Equals(other._cells[i])) |
| | 1 | 145 | | return false; |
| | | 146 | | } |
| | | 147 | | |
| | 26 | 148 | | for (int i = 0; i < _connections.Length; i++) |
| | | 149 | | { |
| | 4 | 150 | | if (!_connections[i].Equals(other._connections[i])) |
| | 1 | 151 | | return false; |
| | | 152 | | } |
| | | 153 | | |
| | 26 | 154 | | for (int i = 0; i < _transitions.Length; i++) |
| | | 155 | | { |
| | 5 | 156 | | if (!_transitions[i].Equals(other._transitions[i])) |
| | 1 | 157 | | return false; |
| | | 158 | | } |
| | | 159 | | |
| | 22 | 160 | | for (int i = 0; i < _transitionRules.Length; i++) |
| | | 161 | | { |
| | 4 | 162 | | if (!_transitionRules[i].Equals(other._transitionRules[i])) |
| | 1 | 163 | | return false; |
| | | 164 | | } |
| | | 165 | | |
| | 7 | 166 | | return true; |
| | | 167 | | } |
| | | 168 | | |
| | | 169 | | /// <inheritdoc/> |
| | 18 | 170 | | public override bool Equals(object? obj) => Equals(obj as NavigationMap); |
| | | 171 | | |
| | | 172 | | /// <inheritdoc/> |
| | | 173 | | public override int GetHashCode() |
| | | 174 | | { |
| | 12 | 175 | | int mapHash = SwiftHashTools.GetDeterministicStringEqualityComparer().GetHashCode(MapId); |
| | 12 | 176 | | int hash = SwiftHashTools.CombineHashCodes(mapHash, GridBinding.Key.GetHashCode()); |
| | 12 | 177 | | hash = SwiftHashTools.CombineHashCodes(hash, GridBinding.Width); |
| | 12 | 178 | | hash = SwiftHashTools.CombineHashCodes(hash, GridBinding.Height); |
| | 12 | 179 | | hash = SwiftHashTools.CombineHashCodes(hash, GridBinding.Length); |
| | 12 | 180 | | hash = SwiftHashTools.CombineHashCodes(hash, _defaultCell.GetHashCode()); |
| | 52 | 181 | | for (int i = 0; i < _cells.Length; i++) |
| | 14 | 182 | | hash = SwiftHashTools.CombineHashCodes(hash, _cells[i].GetHashCode()); |
| | 32 | 183 | | for (int i = 0; i < _connections.Length; i++) |
| | 4 | 184 | | hash = SwiftHashTools.CombineHashCodes(hash, _connections[i].GetHashCode()); |
| | 36 | 185 | | for (int i = 0; i < _transitions.Length; i++) |
| | 6 | 186 | | hash = SwiftHashTools.CombineHashCodes(hash, _transitions[i].GetHashCode()); |
| | 32 | 187 | | for (int i = 0; i < _transitionRules.Length; i++) |
| | 4 | 188 | | hash = SwiftHashTools.CombineHashCodes(hash, _transitionRules[i].GetHashCode()); |
| | 12 | 189 | | return hash; |
| | | 190 | | } |
| | | 191 | | |
| | | 192 | | internal bool ContainsCell(GridForge.Spatial.VoxelIndex index) => |
| | 17213 | 193 | | FindCellIndex(index) >= 0; |
| | | 194 | | |
| | | 195 | | internal int FindCellIndex(GridForge.Spatial.VoxelIndex index) |
| | | 196 | | { |
| | 3738866 | 197 | | int low = 0; |
| | 3738866 | 198 | | int high = _cells.Length - 1; |
| | 7798721 | 199 | | while (low <= high) |
| | | 200 | | { |
| | 4300673 | 201 | | int middle = low + ((high - low) >> 1); |
| | 4300673 | 202 | | int comparison = _cells[middle].Index.CompareTo(index); |
| | 4300673 | 203 | | if (comparison == 0) |
| | 240818 | 204 | | return middle; |
| | 4059855 | 205 | | if (comparison < 0) |
| | 3761889 | 206 | | low = middle + 1; |
| | | 207 | | else |
| | 297966 | 208 | | high = middle - 1; |
| | | 209 | | } |
| | | 210 | | |
| | 3498048 | 211 | | return -1; |
| | | 212 | | } |
| | | 213 | | |
| | | 214 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 215 | | internal GridNavigationPortal GetNativePortalTemplate(int directionIndex) => |
| | 788973 | 216 | | _nativePortalTemplates[directionIndex]; |
| | | 217 | | |
| | | 218 | | internal static int GetNativeSurfaceDirectionCount(GridTopologyKind topology) => |
| | 1668688 | 219 | | topology == GridTopologyKind.HexPrism |
| | 1668688 | 220 | | ? NativeHexDirections.Length |
| | 1668688 | 221 | | : NativeRectangularDirections.Length; |
| | | 222 | | |
| | | 223 | | internal static VoxelIndex GetNativeSurfaceOffset( |
| | | 224 | | GridTopologyKind topology, |
| | | 225 | | int directionIndex) |
| | | 226 | | { |
| | 1851904 | 227 | | if (topology == GridTopologyKind.HexPrism) |
| | 5456 | 228 | | return HexDirectionUtility.GetOffset(NativeHexDirections[directionIndex]); |
| | | 229 | | |
| | 1846448 | 230 | | RectangularDirection rectangular = NativeRectangularDirections[directionIndex]; |
| | 1846448 | 231 | | (int x, int y, int z) offset = RectangularDirectionUtility.Offsets[(int)rectangular]; |
| | 1846448 | 232 | | return new VoxelIndex(offset.x, offset.y, offset.z); |
| | | 233 | | } |
| | | 234 | | |
| | | 235 | | private static GridNavigationPortal[] CompileNativePortalTemplates( |
| | | 236 | | NormalizedGridConfiguration binding) |
| | | 237 | | { |
| | 2037 | 238 | | GridTopologyKind topology = binding.Configuration.TopologyKind; |
| | 2037 | 239 | | int count = GetNativeSurfaceDirectionCount(topology); |
| | 2037 | 240 | | var templates = new GridNavigationPortal[count]; |
| | 20606 | 241 | | for (int directionIndex = 0; directionIndex < count; directionIndex++) |
| | | 242 | | { |
| | 8266 | 243 | | VoxelIndex offset = GetNativeSurfaceOffset(topology, directionIndex); |
| | 8266 | 244 | | var sourceIndex = new VoxelIndex( |
| | 8266 | 245 | | offset.x < 0 ? 1 : 0, |
| | 8266 | 246 | | 0, |
| | 8266 | 247 | | offset.z < 0 ? 1 : 0); |
| | 8266 | 248 | | var targetIndex = new VoxelIndex( |
| | 8266 | 249 | | sourceIndex.x + offset.x, |
| | 8266 | 250 | | sourceIndex.y + offset.y, |
| | 8266 | 251 | | sourceIndex.z + offset.z); |
| | 8266 | 252 | | if (!binding.IsValidIndex(sourceIndex) || !binding.IsValidIndex(targetIndex)) |
| | | 253 | | continue; |
| | | 254 | | |
| | 5510 | 255 | | bool hasSource = binding.TryGetCellPrism(sourceIndex, out GridCellPrism source); |
| | 5510 | 256 | | bool hasTarget = binding.TryGetCellPrism(targetIndex, out GridCellPrism target); |
| | 5510 | 257 | | bool hasPortal = GridCellGeometry.TryCreateNavigationPortal( |
| | 5510 | 258 | | source, |
| | 5510 | 259 | | target, |
| | 5510 | 260 | | out GridNavigationPortal portal); |
| | 5510 | 261 | | bool hasTranslation = Vector3d.TrySubtract( |
| | 5510 | 262 | | Vector3d.Zero, |
| | 5510 | 263 | | source.Center, |
| | 5510 | 264 | | out Vector3d translation); |
| | 5510 | 265 | | bool translated = portal.TryTranslate(translation, out templates[directionIndex]); |
| | | 266 | | System.Diagnostics.Debug.Assert( |
| | | 267 | | hasSource && hasTarget && hasPortal && hasTranslation && translated); |
| | | 268 | | } |
| | 2037 | 269 | | return templates; |
| | | 270 | | } |
| | | 271 | | } |