| | | 1 | | using FixedMathSharp; |
| | | 2 | | using SwiftCollections; |
| | | 3 | | using System; |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Runtime.CompilerServices; |
| | | 6 | | |
| | | 7 | | namespace Trailblazer.Pathing; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Represents dense authored traversal data across surface and volume spaces. |
| | | 11 | | /// Provides utility methods for querying authored cells and converting world positions into discrete grid indices. |
| | | 12 | | /// </summary> |
| | | 13 | | [Serializable] |
| | | 14 | | public class NavigationChart |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// The name identifier for this navigation chart. |
| | | 18 | | /// </summary> |
| | | 19 | | public readonly string Name; |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// The minimum world-space bounds of the grid. Determines the starting point for grid indexing. |
| | | 23 | | /// </summary> |
| | | 24 | | public readonly Vector3d MinBounds; |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Higher values take precedence when this chart overlaps another chart on the same voxel. |
| | | 28 | | /// </summary> |
| | | 29 | | public readonly int Priority; |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// The maximum world-space bounds of the grid, computed as MinBounds + grid size * Interval. |
| | | 33 | | /// Represents the exclusive upper bound of the grid. |
| | | 34 | | /// </summary> |
| | | 35 | | public readonly Vector3d MaxBounds; |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// The distance between chart cells along each axis. This must match the owning context's voxel size when registere |
| | | 39 | | /// </summary> |
| | | 40 | | public readonly Fixed64 Interval; |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// The number of cells along the X axis. |
| | | 44 | | /// </summary> |
| | | 45 | | public readonly int SizeX; |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// The number of cells along the Y axis. |
| | | 49 | | /// </summary> |
| | | 50 | | public readonly int SizeY; |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// The number of cells along the Z axis. |
| | | 54 | | /// </summary> |
| | | 55 | | public readonly int SizeZ; |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// A flattened 3D map of authored chart cells indexed in row-major order across Y, X, then Z. |
| | | 59 | | /// </summary> |
| | | 60 | | private readonly NavigationChartCell[] _cells; |
| | | 61 | | |
| | 970 | 62 | | private readonly SwiftHashSet<int> _authoredCellIndices = new(); |
| | | 63 | | |
| | 970 | 64 | | private readonly SwiftHashSet<int> _surfaceCellIndices = new(); |
| | | 65 | | |
| | 970 | 66 | | private readonly SwiftHashSet<int> _generatedTransitionCellIndices = new(); |
| | | 67 | | |
| | 970 | 68 | | private int[] _cachedAuthoredCellIndices = Array.Empty<int>(); |
| | | 69 | | |
| | 970 | 70 | | private int[] _cachedSurfaceCellIndices = Array.Empty<int>(); |
| | | 71 | | |
| | 970 | 72 | | private int[] _cachedGeneratedTransitionCellIndices = Array.Empty<int>(); |
| | | 73 | | |
| | | 74 | | private bool _authoredCellIndicesDirty; |
| | | 75 | | |
| | | 76 | | private bool _surfaceCellIndicesDirty; |
| | | 77 | | |
| | | 78 | | private bool _generatedTransitionCellIndicesDirty; |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// Creates a new navigation chart using a pre-flattened map array and spatial parameters. |
| | | 82 | | /// </summary> |
| | | 83 | | /// <param name="name">The chart's unique identifier.</param> |
| | | 84 | | /// <param name="map">A flattened boolean array representing authored surface cells and empty cells.</param> |
| | | 85 | | /// <param name="sizeX">Number of cells along the X axis.</param> |
| | | 86 | | /// <param name="sizeY">Number of cells along the Y axis.</param> |
| | | 87 | | /// <param name="sizeZ">Number of cells along the Z axis.</param> |
| | | 88 | | /// <param name="minBounds">The minimum world-space bounds of the grid.</param> |
| | | 89 | | /// <param name="maxBounds">The maximum world-space bounds of the grid.</param> |
| | | 90 | | /// <param name="interval">Distance between adjacent chart cells. Must be greater than zero.</param> |
| | | 91 | | /// <param name="medium">The authored traversal medium emitted for each <c>true</c> cell.</param> |
| | | 92 | | /// <param name="priority">The authored precedence used when this chart overlaps another chart on the same voxel.</p |
| | | 93 | | /// <exception cref="ArgumentNullException">Thrown when <paramref name="map"/> is null.</exception> |
| | | 94 | | /// <exception cref="ArgumentOutOfRangeException"> |
| | | 95 | | /// Thrown when <paramref name="interval"/> is not greater than zero, when any dimension is not greater than zero, |
| | | 96 | | /// or when <paramref name="medium"/> is not <see cref="TraversalMedium.Solid"/>, |
| | | 97 | | /// <see cref="TraversalMedium.Gas"/>, or <see cref="TraversalMedium.Liquid"/>. |
| | | 98 | | /// </exception> |
| | | 99 | | public NavigationChart( |
| | | 100 | | string name, |
| | | 101 | | bool[] map, |
| | | 102 | | int sizeX, |
| | | 103 | | int sizeY, |
| | | 104 | | int sizeZ, |
| | | 105 | | Vector3d minBounds, |
| | | 106 | | Vector3d maxBounds, |
| | | 107 | | Fixed64 interval, |
| | | 108 | | TraversalMedium medium = TraversalMedium.Solid, |
| | | 109 | | int priority = 0) |
| | 1 | 110 | | : this( |
| | 1 | 111 | | name, |
| | 1 | 112 | | CreateCells(map, medium), |
| | 1 | 113 | | sizeX, |
| | 1 | 114 | | sizeY, |
| | 1 | 115 | | sizeZ, |
| | 1 | 116 | | minBounds, |
| | 1 | 117 | | maxBounds, |
| | 1 | 118 | | interval, |
| | 1 | 119 | | priority) |
| | 1 | 120 | | { } |
| | | 121 | | |
| | | 122 | | /// <summary> |
| | | 123 | | /// Creates a new navigation chart using a pre-flattened cell array and spatial parameters. |
| | | 124 | | /// </summary> |
| | | 125 | | /// <param name="name">The chart's unique identifier.</param> |
| | | 126 | | /// <param name="cells">A flattened array representing authored chart cell payloads.</param> |
| | | 127 | | /// <param name="sizeX">Number of cells along the X axis.</param> |
| | | 128 | | /// <param name="sizeY">Number of cells along the Y axis.</param> |
| | | 129 | | /// <param name="sizeZ">Number of cells along the Z axis.</param> |
| | | 130 | | /// <param name="minBounds">The minimum world-space bounds of the grid.</param> |
| | | 131 | | /// <param name="maxBounds">The maximum world-space bounds of the grid.</param> |
| | | 132 | | /// <param name="interval">Distance between adjacent chart cells. Must be greater than zero.</param> |
| | | 133 | | /// <param name="priority">The authored precedence used when this chart overlaps another chart on the same voxel.</p |
| | | 134 | | /// <exception cref="ArgumentOutOfRangeException"> |
| | | 135 | | /// Thrown when <paramref name="interval"/> is not greater than zero, or when any dimension is not greater than zero |
| | | 136 | | /// </exception> |
| | 970 | 137 | | public NavigationChart( |
| | 970 | 138 | | string name, |
| | 970 | 139 | | NavigationChartCell[] cells, |
| | 970 | 140 | | int sizeX, |
| | 970 | 141 | | int sizeY, |
| | 970 | 142 | | int sizeZ, |
| | 970 | 143 | | Vector3d minBounds, |
| | 970 | 144 | | Vector3d maxBounds, |
| | 970 | 145 | | Fixed64 interval, |
| | 970 | 146 | | int priority = 0) |
| | | 147 | | { |
| | 970 | 148 | | Name = name; |
| | 970 | 149 | | _cells = cells ?? throw new ArgumentNullException(nameof(cells)); |
| | 970 | 150 | | SizeX = sizeX; |
| | 970 | 151 | | SizeY = sizeY; |
| | 970 | 152 | | SizeZ = sizeZ; |
| | 970 | 153 | | MinBounds = minBounds; |
| | 970 | 154 | | MaxBounds = maxBounds; |
| | 970 | 155 | | Interval = interval; |
| | 970 | 156 | | Priority = priority; |
| | | 157 | | |
| | 970 | 158 | | if (interval <= Fixed64.Zero) |
| | 1 | 159 | | throw new ArgumentOutOfRangeException(nameof(interval), interval, "Navigation chart interval must be greater |
| | | 160 | | |
| | 969 | 161 | | if (sizeX <= 0) |
| | 0 | 162 | | throw new ArgumentOutOfRangeException(nameof(sizeX), sizeX, "Navigation chart size must be greater than zero |
| | 969 | 163 | | if (sizeY <= 0) |
| | 0 | 164 | | throw new ArgumentOutOfRangeException(nameof(sizeY), sizeY, "Navigation chart size must be greater than zero |
| | 969 | 165 | | if (sizeZ <= 0) |
| | 0 | 166 | | throw new ArgumentOutOfRangeException(nameof(sizeZ), sizeZ, "Navigation chart size must be greater than zero |
| | | 167 | | |
| | 969 | 168 | | int expectedCellCount = checked(sizeX * sizeY * sizeZ); |
| | 969 | 169 | | if (_cells.Length != expectedCellCount) |
| | 1 | 170 | | throw new ArgumentException($"Expected {expectedCellCount} chart cells but received {_cells.Length}.", nameo |
| | | 171 | | |
| | 968 | 172 | | BuildCellIndexCaches(); |
| | 968 | 173 | | } |
| | | 174 | | |
| | | 175 | | /// <summary> |
| | | 176 | | /// Converts 3D grid indices (x, y, z) into a 1D flattened index for accessing the internal map. |
| | | 177 | | /// </summary> |
| | | 178 | | /// <param name="x">The X index in the grid.</param> |
| | | 179 | | /// <param name="y">The Y index in the grid.</param> |
| | | 180 | | /// <param name="z">The Z index in the grid.</param> |
| | | 181 | | /// <returns>The flattened index corresponding to the provided 3D coordinates.</returns> |
| | | 182 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 676 | 183 | | private int ToIndex(int x, int y, int z) => (y * SizeX * SizeZ) + (x * SizeZ) + z; |
| | | 184 | | |
| | | 185 | | /// <summary> |
| | | 186 | | /// Attempts to convert a world-space position into the grid's local indices. |
| | | 187 | | /// </summary> |
| | | 188 | | /// <param name="pos">The world-space position.</param> |
| | | 189 | | /// <param name="x">Resulting X index.</param> |
| | | 190 | | /// <param name="y">Resulting Y index.</param> |
| | | 191 | | /// <param name="z">Resulting Z index.</param> |
| | | 192 | | /// <returns>True if the position is within bounds; otherwise, false.</returns> |
| | | 193 | | public bool TryWorldToIndex(Vector3d pos, out int x, out int y, out int z) |
| | | 194 | | { |
| | 149 | 195 | | x = ((pos.x - MinBounds.x) / Interval).FloorToInt(); |
| | 149 | 196 | | y = ((pos.y - MinBounds.y) / Interval).FloorToInt(); |
| | 149 | 197 | | z = ((pos.z - MinBounds.z) / Interval).FloorToInt(); |
| | | 198 | | |
| | 149 | 199 | | bool valid = x >= 0 && x < SizeX && |
| | 149 | 200 | | y >= 0 && y < SizeY && |
| | 149 | 201 | | z >= 0 && z < SizeZ; |
| | | 202 | | |
| | 149 | 203 | | if (!valid) |
| | | 204 | | { |
| | 6 | 205 | | x = y = z = -1; |
| | 6 | 206 | | return false; |
| | | 207 | | } |
| | | 208 | | |
| | 143 | 209 | | return true; |
| | | 210 | | } |
| | | 211 | | |
| | | 212 | | /// <summary> |
| | | 213 | | /// Checks if the given world-space position corresponds to an authored surface traversal cell. |
| | | 214 | | /// </summary> |
| | | 215 | | /// <param name="worldPos">The position to query.</param> |
| | | 216 | | /// <returns>True if traversable; otherwise, false.</returns> |
| | | 217 | | public bool IsWalkable(Vector3d worldPos) |
| | | 218 | | { |
| | 7 | 219 | | if (!TryGetCell(worldPos, out NavigationChartCell cell)) |
| | 2 | 220 | | return false; |
| | | 221 | | |
| | 5 | 222 | | return cell.HasSolid; |
| | | 223 | | } |
| | | 224 | | |
| | | 225 | | /// <summary> |
| | | 226 | | /// Attempts to retrieve the authored chart cell at the provided world-space position. |
| | | 227 | | /// </summary> |
| | | 228 | | /// <param name="worldPos">The world-space position to query.</param> |
| | | 229 | | /// <param name="cell">The authored chart cell payload.</param> |
| | | 230 | | /// <returns>True if the position resolves inside this chart; otherwise, false.</returns> |
| | | 231 | | public bool TryGetCell(Vector3d worldPos, out NavigationChartCell cell) |
| | | 232 | | { |
| | 37 | 233 | | if (!TryWorldToIndex(worldPos, out int x, out int y, out int z)) |
| | | 234 | | { |
| | 3 | 235 | | cell = default; |
| | 3 | 236 | | return false; |
| | | 237 | | } |
| | | 238 | | |
| | 34 | 239 | | cell = GetCell(x, y, z); |
| | 34 | 240 | | return true; |
| | | 241 | | } |
| | | 242 | | |
| | | 243 | | /// <summary> |
| | | 244 | | /// Returns all authored surface traversal positions within the chart. |
| | | 245 | | /// </summary> |
| | | 246 | | /// <returns>A collection of traversable surface positions.</returns> |
| | | 247 | | public IEnumerable<Vector3d> GetWalkablePositions() |
| | | 248 | | { |
| | 6 | 249 | | foreach ((Vector3d position, _) in GetSurfaceCells()) |
| | 2 | 250 | | yield return position; |
| | 1 | 251 | | } |
| | | 252 | | |
| | | 253 | | /// <summary> |
| | | 254 | | /// Returns each authored surface traversal position together with its authored cell payload. |
| | | 255 | | /// </summary> |
| | | 256 | | internal IEnumerable<(Vector3d Position, NavigationChartCell Cell)> GetSurfaceCells() |
| | | 257 | | { |
| | 3 | 258 | | int[] surfaceIndices = GetSortedSurfaceCellIndices(); |
| | 16 | 259 | | for (int i = 0; i < surfaceIndices.Length; i++) |
| | | 260 | | { |
| | 5 | 261 | | int flatIndex = surfaceIndices[i]; |
| | 5 | 262 | | DecodeIndex(flatIndex, out int x, out int y, out int z); |
| | 5 | 263 | | yield return (GetWorldPosition(x, y, z), _cells[flatIndex]); |
| | | 264 | | } |
| | 3 | 265 | | } |
| | | 266 | | |
| | | 267 | | /// <summary> |
| | | 268 | | /// Returns each authored traversal position together with its authored cell payload. |
| | | 269 | | /// </summary> |
| | | 270 | | internal IEnumerable<(Vector3d Position, NavigationChartCell Cell)> GetAuthoredCells() |
| | | 271 | | { |
| | 1238 | 272 | | int[] authoredIndices = GetSortedAuthoredCellIndices(); |
| | 13994 | 273 | | for (int i = 0; i < authoredIndices.Length; i++) |
| | | 274 | | { |
| | 5767 | 275 | | int flatIndex = authoredIndices[i]; |
| | 5767 | 276 | | DecodeIndex(flatIndex, out int x, out int y, out int z); |
| | 5767 | 277 | | yield return (GetWorldPosition(x, y, z), _cells[flatIndex]); |
| | | 278 | | } |
| | 1230 | 279 | | } |
| | | 280 | | |
| | | 281 | | /// <summary> |
| | | 282 | | /// Creates a navigation chart from a 3D boolean array representing authored traversal voxels. |
| | | 283 | | /// </summary> |
| | | 284 | | /// <param name="name">Name identifier for the chart.</param> |
| | | 285 | | /// <param name="sourceMap">3D map of authored cells (true = authored traversal).</param> |
| | | 286 | | /// <param name="minBounds">The minimum world-space bounds of the grid.</param> |
| | | 287 | | /// <param name="interval">The spacing between each chart cell. Must be greater than zero.</param> |
| | | 288 | | /// <param name="medium">The authored traversal medium emitted for each <c>true</c> cell.</param> |
| | | 289 | | /// <param name="priority">The authored precedence used when this chart overlaps another chart on the same voxel.</p |
| | | 290 | | /// <returns>A constructed NavigationChart instance.</returns> |
| | | 291 | | /// <exception cref="ArgumentNullException">Thrown when <paramref name="sourceMap"/> is null.</exception> |
| | | 292 | | /// <exception cref="ArgumentOutOfRangeException"> |
| | | 293 | | /// Thrown when <paramref name="interval"/> is not greater than zero, or when <paramref name="medium"/> is not <see |
| | | 294 | | /// <see cref="TraversalMedium.Gas"/>, or <see cref="TraversalMedium.Liquid"/>. |
| | | 295 | | /// </exception> |
| | | 296 | | public static NavigationChart From3D( |
| | | 297 | | string name, |
| | | 298 | | bool[,,] sourceMap, |
| | | 299 | | Vector3d minBounds, |
| | | 300 | | Fixed64 interval, |
| | | 301 | | TraversalMedium medium = TraversalMedium.Solid, |
| | | 302 | | int priority = 0) |
| | | 303 | | { |
| | 461 | 304 | | SwiftThrowHelper.ThrowIfNull(sourceMap, nameof(sourceMap)); |
| | | 305 | | |
| | 461 | 306 | | int sizeY = sourceMap.GetLength(0); |
| | 461 | 307 | | int sizeX = sourceMap.GetLength(1); |
| | 461 | 308 | | int sizeZ = sourceMap.GetLength(2); |
| | | 309 | | |
| | 461 | 310 | | Vector3d maxBounds = minBounds + new Vector3d( |
| | 461 | 311 | | sizeX * interval, |
| | 461 | 312 | | sizeY * interval, |
| | 461 | 313 | | sizeZ * interval |
| | 461 | 314 | | ); |
| | | 315 | | |
| | 461 | 316 | | var flat = new bool[sizeX * sizeY * sizeZ]; |
| | 2614 | 317 | | for (int y = 0; y < sizeY; y++) |
| | 6870 | 318 | | for (int x = 0; x < sizeX; x++) |
| | 19758 | 319 | | for (int z = 0; z < sizeZ; z++) |
| | 7290 | 320 | | flat[(y * sizeX * sizeZ) + (x * sizeZ) + z] = sourceMap[y, x, z]; |
| | | 321 | | |
| | 461 | 322 | | return new( |
| | 461 | 323 | | name, |
| | 461 | 324 | | CreateCells(flat, medium), |
| | 461 | 325 | | sizeX, |
| | 461 | 326 | | sizeY, |
| | 461 | 327 | | sizeZ, |
| | 461 | 328 | | minBounds, |
| | 461 | 329 | | maxBounds, |
| | 461 | 330 | | interval, |
| | 461 | 331 | | priority); |
| | | 332 | | } |
| | | 333 | | |
| | | 334 | | /// <summary> |
| | | 335 | | /// Creates a navigation chart from a 3D array of authored chart cell payloads. |
| | | 336 | | /// </summary> |
| | | 337 | | /// <param name="name">Name identifier for the chart.</param> |
| | | 338 | | /// <param name="sourceMap">3D map of authored chart cells.</param> |
| | | 339 | | /// <param name="minBounds">The minimum world-space bounds of the grid.</param> |
| | | 340 | | /// <param name="interval">The spacing between each chart cell. Must be greater than zero.</param> |
| | | 341 | | /// <param name="priority">The authored precedence used when this chart overlaps another chart on the same voxel.</p |
| | | 342 | | /// <returns>A constructed NavigationChart instance.</returns> |
| | | 343 | | /// <exception cref="ArgumentNullException">Thrown when <paramref name="sourceMap"/> is null.</exception> |
| | | 344 | | /// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="interval"/> is not greater than zero.< |
| | | 345 | | public static NavigationChart From3D( |
| | | 346 | | string name, |
| | | 347 | | NavigationChartCell[,,] sourceMap, |
| | | 348 | | Vector3d minBounds, |
| | | 349 | | Fixed64 interval, |
| | | 350 | | int priority = 0) |
| | | 351 | | { |
| | 509 | 352 | | SwiftThrowHelper.ThrowIfNull(sourceMap, nameof(sourceMap)); |
| | | 353 | | |
| | 509 | 354 | | int sizeY = sourceMap.GetLength(0); |
| | 509 | 355 | | int sizeX = sourceMap.GetLength(1); |
| | 509 | 356 | | int sizeZ = sourceMap.GetLength(2); |
| | | 357 | | |
| | 509 | 358 | | Vector3d maxBounds = minBounds + new Vector3d( |
| | 509 | 359 | | sizeX * interval, |
| | 509 | 360 | | sizeY * interval, |
| | 509 | 361 | | sizeZ * interval |
| | 509 | 362 | | ); |
| | | 363 | | |
| | 509 | 364 | | var flat = new NavigationChartCell[sizeX * sizeY * sizeZ]; |
| | 3658 | 365 | | for (int y = 0; y < sizeY; y++) |
| | 10444 | 366 | | for (int x = 0; x < sizeX; x++) |
| | 30428 | 367 | | for (int z = 0; z < sizeZ; z++) |
| | 11312 | 368 | | flat[(y * sizeX * sizeZ) + (x * sizeZ) + z] = sourceMap[y, x, z]; |
| | | 369 | | |
| | 509 | 370 | | return new( |
| | 509 | 371 | | name, |
| | 509 | 372 | | flat, |
| | 509 | 373 | | sizeX, |
| | 509 | 374 | | sizeY, |
| | 509 | 375 | | sizeZ, |
| | 509 | 376 | | minBounds, |
| | 509 | 377 | | maxBounds, |
| | 509 | 378 | | interval, |
| | 509 | 379 | | priority); |
| | | 380 | | } |
| | | 381 | | |
| | | 382 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 383 | | internal bool IsInBounds(int x, int y, int z) |
| | | 384 | | { |
| | 1237 | 385 | | return x >= 0 && x < SizeX |
| | 1237 | 386 | | && y >= 0 && y < SizeY |
| | 1237 | 387 | | && z >= 0 && z < SizeZ; |
| | | 388 | | } |
| | | 389 | | |
| | | 390 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 391 | | internal Vector3d GetWorldPosition(int x, int y, int z) |
| | | 392 | | { |
| | 6734 | 393 | | return new Vector3d( |
| | 6734 | 394 | | MinBounds.x + x * Interval, |
| | 6734 | 395 | | MinBounds.y + y * Interval, |
| | 6734 | 396 | | MinBounds.z + z * Interval); |
| | | 397 | | } |
| | | 398 | | |
| | | 399 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 400 | | internal void DecodeIndex(int flatIndex, out int x, out int y, out int z) |
| | | 401 | | { |
| | 5878 | 402 | | int yStride = SizeX * SizeZ; |
| | 5878 | 403 | | y = flatIndex / yStride; |
| | 5878 | 404 | | int remainder = flatIndex - (y * yStride); |
| | 5878 | 405 | | x = remainder / SizeZ; |
| | 5878 | 406 | | z = remainder - (x * SizeZ); |
| | 5878 | 407 | | } |
| | | 408 | | |
| | | 409 | | internal bool TrySetCell(int x, int y, int z, NavigationChartCell cell, out NavigationChartCell previousCell) |
| | | 410 | | { |
| | 33 | 411 | | if (!IsInBounds(x, y, z)) |
| | | 412 | | { |
| | 5 | 413 | | previousCell = default; |
| | 5 | 414 | | return false; |
| | | 415 | | } |
| | | 416 | | |
| | 28 | 417 | | int index = ToIndex(x, y, z); |
| | 28 | 418 | | previousCell = _cells[index]; |
| | 28 | 419 | | if (previousCell.Equals(cell)) |
| | 4 | 420 | | return false; |
| | | 421 | | |
| | 24 | 422 | | _cells[index] = cell; |
| | 24 | 423 | | UpdateIndexMembership( |
| | 24 | 424 | | _authoredCellIndices, |
| | 24 | 425 | | index, |
| | 24 | 426 | | previousCell.HasTraversalData, |
| | 24 | 427 | | cell.HasTraversalData, |
| | 24 | 428 | | ref _authoredCellIndicesDirty); |
| | 24 | 429 | | UpdateIndexMembership( |
| | 24 | 430 | | _surfaceCellIndices, |
| | 24 | 431 | | index, |
| | 24 | 432 | | previousCell.HasSolid, |
| | 24 | 433 | | cell.HasSolid, |
| | 24 | 434 | | ref _surfaceCellIndicesDirty); |
| | 24 | 435 | | UpdateIndexMembership( |
| | 24 | 436 | | _generatedTransitionCellIndices, |
| | 24 | 437 | | index, |
| | 24 | 438 | | ShouldTrackGeneratedTransitionCell(previousCell), |
| | 24 | 439 | | ShouldTrackGeneratedTransitionCell(cell), |
| | 24 | 440 | | ref _generatedTransitionCellIndicesDirty); |
| | 24 | 441 | | return true; |
| | | 442 | | } |
| | | 443 | | |
| | | 444 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 648 | 445 | | internal NavigationChartCell GetCell(int x, int y, int z) => _cells[ToIndex(x, y, z)]; |
| | | 446 | | |
| | 1953 | 447 | | internal int[] GetGeneratedTransitionIndices() => GetSortedGeneratedTransitionCellIndices(); |
| | | 448 | | |
| | | 449 | | private void BuildCellIndexCaches() |
| | | 450 | | { |
| | 39142 | 451 | | for (int i = 0; i < _cells.Length; i++) |
| | | 452 | | { |
| | 18603 | 453 | | NavigationChartCell cell = _cells[i]; |
| | 18603 | 454 | | if (cell.HasTraversalData) |
| | 3187 | 455 | | _authoredCellIndices.Add(i); |
| | | 456 | | |
| | 18603 | 457 | | if (cell.HasSolid) |
| | 2685 | 458 | | _surfaceCellIndices.Add(i); |
| | | 459 | | |
| | 18603 | 460 | | if (ShouldTrackGeneratedTransitionCell(cell)) |
| | 78 | 461 | | _generatedTransitionCellIndices.Add(i); |
| | | 462 | | } |
| | | 463 | | |
| | 968 | 464 | | _authoredCellIndicesDirty = true; |
| | 968 | 465 | | _surfaceCellIndicesDirty = true; |
| | 968 | 466 | | _generatedTransitionCellIndicesDirty = true; |
| | 968 | 467 | | } |
| | | 468 | | |
| | | 469 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 470 | | private static bool ShouldTrackGeneratedTransitionCell(NavigationChartCell cell) |
| | | 471 | | { |
| | 18651 | 472 | | return cell.CanGenerateTransition |
| | 18651 | 473 | | || (cell.Flags & NavigationChartCellFlags.ClimbSurfaceHint) != 0; |
| | | 474 | | } |
| | | 475 | | |
| | | 476 | | private static void UpdateIndexMembership( |
| | | 477 | | SwiftHashSet<int> indices, |
| | | 478 | | int flatIndex, |
| | | 479 | | bool wasPresent, |
| | | 480 | | bool isPresent, |
| | | 481 | | ref bool cacheDirty) |
| | | 482 | | { |
| | 72 | 483 | | if (wasPresent == isPresent) |
| | 32 | 484 | | return; |
| | | 485 | | |
| | 40 | 486 | | if (isPresent) |
| | 18 | 487 | | indices.Add(flatIndex); |
| | | 488 | | else |
| | 22 | 489 | | indices.Remove(flatIndex); |
| | | 490 | | |
| | 40 | 491 | | cacheDirty = true; |
| | 40 | 492 | | } |
| | | 493 | | |
| | | 494 | | private int[] GetSortedAuthoredCellIndices() |
| | | 495 | | { |
| | 1238 | 496 | | return GetSortedIndexCache( |
| | 1238 | 497 | | _authoredCellIndices, |
| | 1238 | 498 | | ref _cachedAuthoredCellIndices, |
| | 1238 | 499 | | ref _authoredCellIndicesDirty); |
| | | 500 | | } |
| | | 501 | | |
| | | 502 | | private int[] GetSortedSurfaceCellIndices() |
| | | 503 | | { |
| | 3 | 504 | | return GetSortedIndexCache( |
| | 3 | 505 | | _surfaceCellIndices, |
| | 3 | 506 | | ref _cachedSurfaceCellIndices, |
| | 3 | 507 | | ref _surfaceCellIndicesDirty); |
| | | 508 | | } |
| | | 509 | | |
| | | 510 | | private int[] GetSortedGeneratedTransitionCellIndices() |
| | | 511 | | { |
| | 1953 | 512 | | return GetSortedIndexCache( |
| | 1953 | 513 | | _generatedTransitionCellIndices, |
| | 1953 | 514 | | ref _cachedGeneratedTransitionCellIndices, |
| | 1953 | 515 | | ref _generatedTransitionCellIndicesDirty); |
| | | 516 | | } |
| | | 517 | | |
| | | 518 | | private static int[] GetSortedIndexCache( |
| | | 519 | | SwiftHashSet<int> source, |
| | | 520 | | ref int[] cache, |
| | | 521 | | ref bool cacheDirty) |
| | | 522 | | { |
| | 3194 | 523 | | if (!cacheDirty) |
| | 1362 | 524 | | return cache; |
| | | 525 | | |
| | 1832 | 526 | | if (source.Count == 0) |
| | | 527 | | { |
| | 906 | 528 | | cache = Array.Empty<int>(); |
| | 906 | 529 | | cacheDirty = false; |
| | 906 | 530 | | return cache; |
| | | 531 | | } |
| | | 532 | | |
| | 926 | 533 | | int[] sorted = new int[source.Count]; |
| | 926 | 534 | | int index = 0; |
| | 8172 | 535 | | foreach (int value in source) |
| | 3160 | 536 | | sorted[index++] = value; |
| | | 537 | | |
| | 926 | 538 | | Array.Sort(sorted); |
| | 926 | 539 | | cache = sorted; |
| | 926 | 540 | | cacheDirty = false; |
| | 926 | 541 | | return cache; |
| | | 542 | | } |
| | | 543 | | |
| | | 544 | | private static NavigationChartCell[] CreateCells(bool[] map, TraversalMedium medium) |
| | | 545 | | { |
| | 462 | 546 | | SwiftThrowHelper.ThrowIfNull(map, nameof(map)); |
| | | 547 | | |
| | 462 | 548 | | NavigationChartCell traversableCell = medium switch |
| | 462 | 549 | | { |
| | 457 | 550 | | TraversalMedium.Solid => NavigationChartCell.Solid, |
| | 2 | 551 | | TraversalMedium.Gas => NavigationChartCell.Gas, |
| | 1 | 552 | | TraversalMedium.Liquid => NavigationChartCell.Liquid, |
| | 2 | 553 | | _ => throw new ArgumentOutOfRangeException( |
| | 2 | 554 | | nameof(medium), |
| | 2 | 555 | | medium, |
| | 2 | 556 | | "Boolean chart factories support Solid, Gas, or Liquid traversal only.") |
| | 462 | 557 | | }; |
| | | 558 | | |
| | 460 | 559 | | var cells = new NavigationChartCell[map.Length]; |
| | 15504 | 560 | | for (int i = 0; i < map.Length; i++) |
| | 7292 | 561 | | cells[i] = map[i] ? traversableCell : NavigationChartCell.Empty; |
| | | 562 | | |
| | 460 | 563 | | return cells; |
| | | 564 | | } |
| | | 565 | | } |