| | | 1 | | //======================================================================= |
| | | 2 | | // Voxel.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 FixedMathSharp; |
| | | 9 | | using GridForge.Grids.Topology; |
| | | 10 | | using GridForge.Spatial; |
| | | 11 | | using SwiftCollections; |
| | | 12 | | using SwiftCollections.Pool; |
| | | 13 | | using System; |
| | | 14 | | using System.Runtime.CompilerServices; |
| | | 15 | | |
| | | 16 | | namespace GridForge.Grids; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Represents a voxel within a 3D grid, handling spatial positioning, obstacles, occupants, and neighbor relationships. |
| | | 20 | | /// </summary> |
| | | 21 | | public class Voxel : IEquatable<Voxel> |
| | | 22 | | { |
| | | 23 | | #region Properties & Fields |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Unique token identifying this voxel instance. |
| | | 27 | | /// </summary> |
| | | 28 | | public int SpawnToken { get; private set; } |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// The world-scoped runtime identity of this voxel within the grid system. |
| | | 32 | | /// </summary> |
| | | 33 | | public WorldVoxelIndex WorldIndex { get; set; } |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// The world-local index of the grid this voxel belongs to. |
| | | 37 | | /// </summary> |
| | 3 | 38 | | public ushort GridIndex => WorldIndex.GridIndex; |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// The local coordinates of this voxel within its grid. |
| | | 42 | | /// </summary> |
| | 10949 | 43 | | public VoxelIndex Index => WorldIndex.VoxelIndex; |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// The grid-local key of the scan cell that this voxel belongs to. |
| | | 47 | | /// </summary> |
| | | 48 | | public int ScanCellKey { get; private set; } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// The world-space position of this voxel. |
| | | 52 | | /// </summary> |
| | | 53 | | public Vector3d WorldPosition { get; private set; } |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Stores a unique <see cref="BoundsKey" /> for each obstacle added to this voxel to prevent adding duplicates |
| | | 57 | | /// </summary> |
| | | 58 | | public SwiftHashSet<BoundsKey>? ObstacleTracker { get; internal set; } |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// The current number of obstacles on this voxel. |
| | | 62 | | /// </summary> |
| | | 63 | | public byte ObstacleCount { get; internal set; } |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// The current number of occupants on this voxel. |
| | | 67 | | /// </summary> |
| | | 68 | | public byte OccupantCount { get; internal set; } |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Handles management of partitioned data. |
| | | 72 | | /// </summary> |
| | 85462 | 73 | | private readonly PartitionProvider<IVoxelPartition> _partitionProvider = new(); |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// Indicates whether this voxel has any active partitions. |
| | | 77 | | /// </summary> |
| | 8805 | 78 | | public bool IsPartioned => !_partitionProvider.IsEmpty; |
| | | 79 | | |
| | 85462 | 80 | | private readonly object _partitionLock = new(); |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// Determines if this voxel is a boundary voxel. |
| | | 84 | | /// </summary> |
| | | 85 | | public bool IsBoundaryVoxel { get; private set; } |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// The current version of the grid at the time this voxel was created. |
| | | 89 | | /// </summary> |
| | | 90 | | public uint CachedGridVersion { get; internal set; } |
| | | 91 | | |
| | | 92 | | /// <summary> |
| | | 93 | | /// Indicates whether this voxel is allocated within a grid. |
| | | 94 | | /// </summary> |
| | | 95 | | public bool IsAllocated { get; private set; } |
| | | 96 | | |
| | | 97 | | /// <summary> |
| | | 98 | | /// Determines whether this voxel is blocked due to obstacles. |
| | | 99 | | /// </summary> |
| | 18079 | 100 | | public bool IsBlocked => IsAllocated && ObstacleCount > 0; |
| | | 101 | | |
| | | 102 | | /// <summary> |
| | | 103 | | /// Determines if this voxel can accept additional obstacles. |
| | | 104 | | /// </summary> |
| | 754 | 105 | | public bool IsBlockable => IsAllocated |
| | 754 | 106 | | && ObstacleCount < GridObstacleManager.MaxObstacleCount |
| | 754 | 107 | | && !IsOccupied; |
| | | 108 | | |
| | | 109 | | /// <summary> |
| | | 110 | | /// Determines whether this voxel is occupied by entities. |
| | | 111 | | /// </summary> |
| | 18548 | 112 | | public bool IsOccupied => IsAllocated && OccupantCount > 0; |
| | | 113 | | |
| | | 114 | | /// <summary> |
| | | 115 | | /// Checks if this voxel has open slots for new occupants. |
| | | 116 | | /// </summary> |
| | 407 | 117 | | public bool HasVacancy => !IsBlocked && OccupantCount < GridOccupantManager.MaxOccupantCount; |
| | | 118 | | |
| | | 119 | | internal bool HasEventSubscribers => |
| | 13 | 120 | | _onObstacleAdded != null |
| | 13 | 121 | | || _onObstacleRemoved != null |
| | 13 | 122 | | || _onObstaclesCleared != null |
| | 13 | 123 | | || _onOccupantAdded != null |
| | 13 | 124 | | || _onOccupantRemoved != null; |
| | | 125 | | |
| | | 126 | | #endregion |
| | | 127 | | |
| | | 128 | | #region Events |
| | | 129 | | |
| | | 130 | | /// <summary> |
| | | 131 | | /// Event triggered when an obstacle is added. |
| | | 132 | | /// </summary> |
| | | 133 | | private Action<ObstacleEventInfo>? _onObstacleAdded; |
| | | 134 | | |
| | | 135 | | /// <inheritdoc cref="_onObstacleAdded"/> |
| | | 136 | | public event Action<ObstacleEventInfo> OnObstacleAdded |
| | | 137 | | { |
| | 3 | 138 | | add => _onObstacleAdded += value; |
| | 3 | 139 | | remove => _onObstacleAdded -= value; |
| | | 140 | | } |
| | | 141 | | |
| | | 142 | | /// <summary> |
| | | 143 | | /// Event triggered when an obstacle is removed. |
| | | 144 | | /// </summary> |
| | | 145 | | private Action<ObstacleEventInfo>? _onObstacleRemoved; |
| | | 146 | | |
| | | 147 | | /// <inheritdoc cref="_onObstacleRemoved"/> |
| | | 148 | | public event Action<ObstacleEventInfo> OnObstacleRemoved |
| | | 149 | | { |
| | 2 | 150 | | add => _onObstacleRemoved += value; |
| | 2 | 151 | | remove => _onObstacleRemoved -= value; |
| | | 152 | | } |
| | | 153 | | |
| | | 154 | | /// <summary> |
| | | 155 | | /// Event triggered when all obstacles on the voxel are cleared at once. |
| | | 156 | | /// </summary> |
| | | 157 | | private Action<ObstacleClearEventInfo>? _onObstaclesCleared; |
| | | 158 | | |
| | | 159 | | /// <inheritdoc cref="_onObstaclesCleared"/> |
| | | 160 | | public event Action<ObstacleClearEventInfo> OnObstaclesCleared |
| | | 161 | | { |
| | 2 | 162 | | add => _onObstaclesCleared += value; |
| | 2 | 163 | | remove => _onObstaclesCleared -= value; |
| | | 164 | | } |
| | | 165 | | |
| | | 166 | | /// <summary> |
| | | 167 | | /// Event triggered when an occupant is added. |
| | | 168 | | /// </summary> |
| | | 169 | | private Action<OccupantEventInfo>? _onOccupantAdded; |
| | | 170 | | |
| | | 171 | | /// <inheritdoc cref="_onOccupantAdded"/> |
| | | 172 | | public event Action<OccupantEventInfo> OnOccupantAdded |
| | | 173 | | { |
| | 2 | 174 | | add => _onOccupantAdded += value; |
| | 2 | 175 | | remove => _onOccupantAdded -= value; |
| | | 176 | | } |
| | | 177 | | |
| | | 178 | | /// <summary> |
| | | 179 | | /// Event triggered when an occupant is removed. |
| | | 180 | | /// </summary> |
| | | 181 | | private Action<OccupantEventInfo>? _onOccupantRemoved; |
| | | 182 | | |
| | | 183 | | /// <inheritdoc cref="_onOccupantRemoved"/> |
| | | 184 | | public event Action<OccupantEventInfo> OnOccupantRemoved |
| | | 185 | | { |
| | 2 | 186 | | add => _onOccupantRemoved += value; |
| | 2 | 187 | | remove => _onOccupantRemoved -= value; |
| | | 188 | | } |
| | | 189 | | |
| | | 190 | | #endregion |
| | | 191 | | |
| | | 192 | | #region Initialization & Reset |
| | | 193 | | |
| | | 194 | | /// <summary> |
| | | 195 | | /// Configures the voxel with its position, grid version, and boundary status. |
| | | 196 | | /// </summary> |
| | | 197 | | internal void Initialize( |
| | | 198 | | WorldVoxelIndex worldVoxelIndex, |
| | | 199 | | Vector3d worldPosition, |
| | | 200 | | int scanCellKey, |
| | | 201 | | bool isBoundaryVoxel, |
| | | 202 | | uint gridVersion) |
| | | 203 | | { |
| | 95899 | 204 | | ScanCellKey = scanCellKey; |
| | 95899 | 205 | | IsBoundaryVoxel = isBoundaryVoxel; |
| | | 206 | | |
| | 95899 | 207 | | WorldIndex = worldVoxelIndex; |
| | 95899 | 208 | | WorldPosition = worldPosition; |
| | | 209 | | |
| | 95899 | 210 | | SpawnToken = GetHashCode(); |
| | 95899 | 211 | | CachedGridVersion = gridVersion; |
| | 95899 | 212 | | IsAllocated = true; |
| | 95899 | 213 | | } |
| | | 214 | | |
| | | 215 | | /// <summary> |
| | | 216 | | /// Resets the voxel, clearing all allocated data and returning it to pools. |
| | | 217 | | /// </summary> |
| | | 218 | | internal void Reset(VoxelGrid? ownerGrid = null) |
| | | 219 | | { |
| | 191798 | 220 | | if (!IsAllocated) |
| | 95899 | 221 | | return; |
| | | 222 | | |
| | 95899 | 223 | | RemovePartitions(); |
| | 95899 | 224 | | ReleaseObstacleState(ownerGrid); |
| | 95899 | 225 | | ClearRuntimeState(); |
| | 95899 | 226 | | } |
| | | 227 | | |
| | | 228 | | private void RemovePartitions() |
| | | 229 | | { |
| | 95899 | 230 | | if (!_partitionProvider.IsEmpty) |
| | | 231 | | { |
| | 8 | 232 | | lock (_partitionLock) |
| | | 233 | | { |
| | 34 | 234 | | foreach (IVoxelPartition partition in _partitionProvider.Partitions) |
| | | 235 | | { |
| | | 236 | | try |
| | | 237 | | { |
| | 9 | 238 | | partition.OnRemoveFromVoxel(this); |
| | 8 | 239 | | } |
| | 1 | 240 | | catch (Exception ex) |
| | | 241 | | { |
| | 1 | 242 | | GridForgeLogger.Channel.Error( |
| | 1 | 243 | | $"Attempting to call {nameof(partition.OnRemoveFromVoxel)} on {partition.GetType().Name}: {e |
| | 1 | 244 | | } |
| | | 245 | | } |
| | | 246 | | |
| | 8 | 247 | | _partitionProvider.Clear(); |
| | 8 | 248 | | } |
| | | 249 | | } |
| | 95899 | 250 | | } |
| | | 251 | | |
| | | 252 | | private void ReleaseObstacleState(VoxelGrid? ownerGrid) |
| | | 253 | | { |
| | 95899 | 254 | | if (ownerGrid != null && ObstacleCount > 0) |
| | 558 | 255 | | ownerGrid.ClearObstacles(this); |
| | 95341 | 256 | | else if (ObstacleTracker != null) |
| | 2 | 257 | | SwiftHashSetPool<BoundsKey>.Shared.Release(ObstacleTracker); |
| | | 258 | | |
| | 95899 | 259 | | ObstacleTracker = null; |
| | 95899 | 260 | | ObstacleCount = 0; |
| | 95899 | 261 | | } |
| | | 262 | | |
| | | 263 | | private void ClearRuntimeState() |
| | | 264 | | { |
| | 95899 | 265 | | IsBoundaryVoxel = false; |
| | | 266 | | |
| | 95899 | 267 | | SpawnToken = 0; |
| | 95899 | 268 | | ScanCellKey = 0; |
| | 95899 | 269 | | WorldIndex = default; |
| | | 270 | | |
| | 95899 | 271 | | OccupantCount = 0; |
| | 95899 | 272 | | _onObstacleAdded = null; |
| | 95899 | 273 | | _onObstacleRemoved = null; |
| | 95899 | 274 | | _onObstaclesCleared = null; |
| | 95899 | 275 | | _onOccupantAdded = null; |
| | 95899 | 276 | | _onOccupantRemoved = null; |
| | | 277 | | |
| | 95899 | 278 | | IsAllocated = false; |
| | 95899 | 279 | | } |
| | | 280 | | |
| | | 281 | | #endregion |
| | | 282 | | |
| | | 283 | | #region Notifications |
| | | 284 | | |
| | | 285 | | internal void NotifyObstacleAdded(ObstacleEventInfo eventInfo) |
| | | 286 | | { |
| | 746 | 287 | | Action<ObstacleEventInfo>? handlers = _onObstacleAdded; |
| | 746 | 288 | | if (handlers == null) |
| | 744 | 289 | | return; |
| | | 290 | | |
| | 2 | 291 | | var handlerDelegates = handlers.GetInvocationList(); |
| | 12 | 292 | | for (int i = 0; i < handlerDelegates.Length; i++) |
| | | 293 | | { |
| | | 294 | | try |
| | | 295 | | { |
| | 4 | 296 | | ((Action<ObstacleEventInfo>)handlerDelegates[i])(eventInfo); |
| | 2 | 297 | | } |
| | 2 | 298 | | catch (Exception ex) |
| | | 299 | | { |
| | 2 | 300 | | GridForgeLogger.Channel.Error($"[Voxel {WorldIndex}] Obstacle add error: {ex.Message}"); |
| | 2 | 301 | | } |
| | | 302 | | } |
| | 2 | 303 | | } |
| | | 304 | | |
| | | 305 | | internal void NotifyObstacleRemoved(ObstacleEventInfo eventInfo) |
| | | 306 | | { |
| | 71 | 307 | | Action<ObstacleEventInfo>? handlers = _onObstacleRemoved; |
| | 71 | 308 | | if (handlers == null) |
| | 70 | 309 | | return; |
| | | 310 | | |
| | 1 | 311 | | var handlerDelegates = handlers.GetInvocationList(); |
| | 6 | 312 | | for (int i = 0; i < handlerDelegates.Length; i++) |
| | | 313 | | { |
| | | 314 | | try |
| | | 315 | | { |
| | 2 | 316 | | ((Action<ObstacleEventInfo>)handlerDelegates[i])(eventInfo); |
| | 1 | 317 | | } |
| | 1 | 318 | | catch (Exception ex) |
| | | 319 | | { |
| | 1 | 320 | | GridForgeLogger.Channel.Error($"[Voxel {WorldIndex}] Obstacle remove error: {ex.Message}"); |
| | 1 | 321 | | } |
| | | 322 | | } |
| | 1 | 323 | | } |
| | | 324 | | |
| | | 325 | | internal void NotifyObstaclesCleared(ObstacleClearEventInfo eventInfo) |
| | | 326 | | { |
| | 560 | 327 | | Action<ObstacleClearEventInfo>? handlers = _onObstaclesCleared; |
| | 560 | 328 | | if (handlers == null) |
| | 559 | 329 | | return; |
| | | 330 | | |
| | 1 | 331 | | var handlerDelegates = handlers.GetInvocationList(); |
| | 6 | 332 | | for (int i = 0; i < handlerDelegates.Length; i++) |
| | | 333 | | { |
| | | 334 | | try |
| | | 335 | | { |
| | 2 | 336 | | ((Action<ObstacleClearEventInfo>)handlerDelegates[i])(eventInfo); |
| | 1 | 337 | | } |
| | 1 | 338 | | catch (Exception ex) |
| | | 339 | | { |
| | 1 | 340 | | GridForgeLogger.Channel.Error($"[Voxel {WorldIndex}] Obstacle clear error: {ex.Message}"); |
| | 1 | 341 | | } |
| | | 342 | | } |
| | 1 | 343 | | } |
| | | 344 | | |
| | | 345 | | internal void NotifyOccupantAdded(OccupantEventInfo eventInfo) |
| | | 346 | | { |
| | 402 | 347 | | Action<OccupantEventInfo>? handlers = _onOccupantAdded; |
| | 402 | 348 | | if (handlers == null) |
| | 401 | 349 | | return; |
| | | 350 | | |
| | 1 | 351 | | var handlerDelegates = handlers.GetInvocationList(); |
| | 6 | 352 | | for (int i = 0; i < handlerDelegates.Length; i++) |
| | | 353 | | { |
| | | 354 | | try |
| | | 355 | | { |
| | 2 | 356 | | ((Action<OccupantEventInfo>)handlerDelegates[i])(eventInfo); |
| | 1 | 357 | | } |
| | 1 | 358 | | catch (Exception ex) |
| | | 359 | | { |
| | 1 | 360 | | GridForgeLogger.Channel.Error($"[Voxel {WorldIndex}] Occupant add error: {ex.Message}"); |
| | 1 | 361 | | } |
| | | 362 | | } |
| | 1 | 363 | | } |
| | | 364 | | |
| | | 365 | | internal void NotifyOccupantRemoved(OccupantEventInfo eventInfo) |
| | | 366 | | { |
| | 153 | 367 | | Action<OccupantEventInfo>? handlers = _onOccupantRemoved; |
| | 153 | 368 | | if (handlers == null) |
| | 152 | 369 | | return; |
| | | 370 | | |
| | 1 | 371 | | var handlerDelegates = handlers.GetInvocationList(); |
| | 6 | 372 | | for (int i = 0; i < handlerDelegates.Length; i++) |
| | | 373 | | { |
| | | 374 | | try |
| | | 375 | | { |
| | 2 | 376 | | ((Action<OccupantEventInfo>)handlerDelegates[i])(eventInfo); |
| | 1 | 377 | | } |
| | 1 | 378 | | catch (Exception ex) |
| | | 379 | | { |
| | 1 | 380 | | GridForgeLogger.Channel.Error($"[Voxel {WorldIndex}] Occupant remove error: {ex.Message}"); |
| | 1 | 381 | | } |
| | | 382 | | } |
| | 1 | 383 | | } |
| | | 384 | | |
| | | 385 | | #endregion |
| | | 386 | | |
| | | 387 | | #region Partition Management |
| | | 388 | | |
| | | 389 | | /// <summary> |
| | | 390 | | /// Adds a partition to this voxel, allowing specialized behaviors. |
| | | 391 | | /// </summary> |
| | | 392 | | public bool TryAddPartition(IVoxelPartition partition) |
| | | 393 | | { |
| | 16 | 394 | | if (partition == null) |
| | 1 | 395 | | return false; |
| | | 396 | | |
| | 15 | 397 | | Type partitionType = partition.GetType(); |
| | 15 | 398 | | string partitionName = partitionType.Name; |
| | | 399 | | |
| | 15 | 400 | | lock (_partitionLock) |
| | | 401 | | { |
| | 15 | 402 | | if (!_partitionProvider.TryAdd(partitionType, partition)) |
| | 2 | 403 | | return false; |
| | 13 | 404 | | } |
| | | 405 | | |
| | | 406 | | try |
| | | 407 | | { |
| | 13 | 408 | | partition.SetParentIndex(WorldIndex); |
| | 13 | 409 | | partition.OnAddToVoxel(this); |
| | 12 | 410 | | return true; |
| | | 411 | | } |
| | 1 | 412 | | catch (Exception ex) |
| | | 413 | | { |
| | 1 | 414 | | lock (_partitionLock) |
| | 1 | 415 | | _partitionProvider.TryRemove(partitionType, out _); |
| | | 416 | | |
| | 1 | 417 | | GridForgeLogger.Channel.Error($"Error attempting to attach partition {partitionName}: {ex.Message}"); |
| | 1 | 418 | | return false; |
| | | 419 | | } |
| | 15 | 420 | | } |
| | | 421 | | |
| | | 422 | | /// <summary> |
| | | 423 | | /// Removes a partition from this voxel. |
| | | 424 | | /// </summary> |
| | | 425 | | public bool TryRemovePartition<T>() where T : IVoxelPartition |
| | | 426 | | { |
| | 5 | 427 | | Type partitionType = typeof(T); |
| | 5 | 428 | | string partitionName = partitionType.Name; |
| | | 429 | | |
| | 5 | 430 | | IVoxelPartition? partition = null; |
| | 5 | 431 | | lock (_partitionLock) |
| | 5 | 432 | | _partitionProvider.TryRemove(partitionType, out partition); |
| | | 433 | | |
| | 5 | 434 | | if (partition == null) |
| | | 435 | | { |
| | 2 | 436 | | GridForgeLogger.Channel.Warn($"Partition {partitionName} not found on this voxel."); |
| | 2 | 437 | | return false; |
| | | 438 | | } |
| | | 439 | | |
| | | 440 | | try |
| | | 441 | | { |
| | 3 | 442 | | partition.OnRemoveFromVoxel(this); |
| | 2 | 443 | | } |
| | 1 | 444 | | catch (Exception ex) |
| | | 445 | | { |
| | 1 | 446 | | GridForgeLogger.Channel.Error($"Attempting to call {nameof(partition.OnRemoveFromVoxel)} on {partitionName}: |
| | 1 | 447 | | } |
| | | 448 | | |
| | 3 | 449 | | return true; |
| | | 450 | | } |
| | | 451 | | |
| | | 452 | | /// <summary> |
| | | 453 | | /// Checks whether or not this voxel contains a specific partition. |
| | | 454 | | /// </summary> |
| | | 455 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 456 | | public bool HasPartition<T>() where T : IVoxelPartition |
| | | 457 | | { |
| | 5 | 458 | | lock (_partitionLock) |
| | 5 | 459 | | return _partitionProvider.Has<T>(); |
| | 5 | 460 | | } |
| | | 461 | | |
| | | 462 | | /// <summary> |
| | | 463 | | /// Retrieves a partition from the voxel by type. |
| | | 464 | | /// </summary> |
| | | 465 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 466 | | public bool TryGetPartition<T>(out T? partition) where T : IVoxelPartition |
| | | 467 | | { |
| | 9 | 468 | | lock (_partitionLock) |
| | 9 | 469 | | return _partitionProvider.TryGet(out partition); |
| | 9 | 470 | | } |
| | | 471 | | |
| | | 472 | | /// <summary> |
| | | 473 | | /// Retrieves a partition from the voxel by type and returns null if it doesn't exist. |
| | | 474 | | /// </summary> |
| | | 475 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 476 | | public T? GetPartitionOrDefault<T>() where T : class, IVoxelPartition |
| | | 477 | | { |
| | 5 | 478 | | lock (_partitionLock) |
| | 5 | 479 | | return _partitionProvider.TryGet(out T? partition) |
| | 5 | 480 | | ? partition |
| | 5 | 481 | | : null; |
| | 5 | 482 | | } |
| | | 483 | | |
| | | 484 | | #endregion |
| | | 485 | | |
| | | 486 | | #region Neighbor Handling |
| | | 487 | | |
| | | 488 | | /// <summary> |
| | | 489 | | /// Clears and fills caller-owned storage with neighboring voxels whose |
| | | 490 | | /// world-space voxel footprints touch this voxel's footprint. |
| | | 491 | | /// </summary> |
| | | 492 | | /// <param name="ownerGrid">The active grid that owns this voxel.</param> |
| | | 493 | | /// <param name="results">Caller-owned storage cleared and filled with contact neighbors.</param> |
| | | 494 | | /// <param name="scope">The grid groups included by the contact query.</param> |
| | | 495 | | /// <param name="tolerance">Optional fixed-point tolerance applied to footprint contact checks.</param> |
| | | 496 | | public void GetNeighborsInto( |
| | | 497 | | VoxelGrid ownerGrid, |
| | | 498 | | SwiftList<Voxel> results, |
| | | 499 | | VoxelNeighborScope scope = VoxelNeighborScope.All, |
| | | 500 | | Fixed64? tolerance = null) |
| | | 501 | | { |
| | 23 | 502 | | SwiftThrowHelper.ThrowIfNull(results, nameof(results)); |
| | | 503 | | |
| | 23 | 504 | | results.Clear(); |
| | 23 | 505 | | if (!IsValidOwnerGrid(ownerGrid)) |
| | 1 | 506 | | return; |
| | | 507 | | |
| | 22 | 508 | | VoxelNeighborResolver.AddContactNeighbors(this, ownerGrid, results, scope, tolerance); |
| | 22 | 509 | | } |
| | | 510 | | |
| | | 511 | | /// <summary> |
| | | 512 | | /// Determines whether this voxel has at least one footprint-contact neighbor in the requested scope. |
| | | 513 | | /// </summary> |
| | | 514 | | /// <param name="ownerGrid">The active grid that owns this voxel.</param> |
| | | 515 | | /// <param name="scope">The grid groups included by the contact query.</param> |
| | | 516 | | /// <param name="tolerance">Optional fixed-point tolerance applied to footprint contact checks.</param> |
| | | 517 | | /// <returns>True when at least one contact exists; otherwise false.</returns> |
| | | 518 | | public bool HasNeighbor( |
| | | 519 | | VoxelGrid ownerGrid, |
| | | 520 | | VoxelNeighborScope scope = VoxelNeighborScope.All, |
| | | 521 | | Fixed64? tolerance = null) |
| | | 522 | | { |
| | 10 | 523 | | if (!IsValidOwnerGrid(ownerGrid)) |
| | 1 | 524 | | return false; |
| | | 525 | | |
| | 9 | 526 | | return VoxelNeighborResolver.HasContactNeighbor(this, ownerGrid, scope, tolerance); |
| | | 527 | | } |
| | | 528 | | |
| | | 529 | | /// <summary> |
| | | 530 | | /// Retrieves the rectangular-prism neighbor voxel in the supplied topology-local direction. |
| | | 531 | | /// </summary> |
| | | 532 | | /// <param name="ownerGrid">The active grid that owns this voxel.</param> |
| | | 533 | | /// <param name="direction">The rectangular-prism direction to resolve.</param> |
| | | 534 | | /// <param name="neighbor">The resolved same-topology neighbor when found.</param> |
| | | 535 | | /// <returns>True when a same-topology neighbor exists in the supplied direction; otherwise false.</returns> |
| | | 536 | | public bool TryGetNeighbor( |
| | | 537 | | VoxelGrid ownerGrid, |
| | | 538 | | RectangularDirection direction, |
| | | 539 | | out Voxel? neighbor) |
| | | 540 | | { |
| | 75 | 541 | | neighbor = null; |
| | 75 | 542 | | if (!IsValidOwnerGrid(ownerGrid)) |
| | 4 | 543 | | return false; |
| | | 544 | | |
| | 71 | 545 | | return VoxelNeighborResolver.TryGetNeighbor(this, ownerGrid, direction, out neighbor); |
| | | 546 | | } |
| | | 547 | | |
| | | 548 | | /// <summary> |
| | | 549 | | /// Retrieves the hex-prism neighbor voxel in the supplied topology-local direction. |
| | | 550 | | /// </summary> |
| | | 551 | | /// <param name="ownerGrid">The active grid that owns this voxel.</param> |
| | | 552 | | /// <param name="direction">The hex-prism direction to resolve.</param> |
| | | 553 | | /// <param name="neighbor">The resolved same-topology neighbor when found.</param> |
| | | 554 | | /// <returns>True when a same-topology neighbor exists in the supplied direction; otherwise false.</returns> |
| | | 555 | | public bool TryGetNeighbor( |
| | | 556 | | VoxelGrid ownerGrid, |
| | | 557 | | HexDirection direction, |
| | | 558 | | out Voxel? neighbor) |
| | | 559 | | { |
| | 6 | 560 | | neighbor = null; |
| | 6 | 561 | | if (!IsValidOwnerGrid(ownerGrid)) |
| | 1 | 562 | | return false; |
| | | 563 | | |
| | 5 | 564 | | return VoxelNeighborResolver.TryGetNeighbor(this, ownerGrid, direction, out neighbor); |
| | | 565 | | } |
| | | 566 | | |
| | | 567 | | /// <summary> |
| | | 568 | | /// Clears and fills caller-owned storage with rectangular-prism neighbors in deterministic direction order. |
| | | 569 | | /// </summary> |
| | | 570 | | /// <param name="ownerGrid">The active grid that owns this voxel.</param> |
| | | 571 | | /// <param name="results">Caller-owned storage cleared and filled with direction-labeled neighbors.</param> |
| | | 572 | | public void GetRectangularNeighborsInto( |
| | | 573 | | VoxelGrid ownerGrid, |
| | | 574 | | SwiftList<(RectangularDirection Direction, Voxel Voxel)> results) |
| | | 575 | | { |
| | 9 | 576 | | SwiftThrowHelper.ThrowIfNull(results, nameof(results)); |
| | | 577 | | |
| | 9 | 578 | | results.Clear(); |
| | 9 | 579 | | if (!IsValidOwnerGrid(ownerGrid)) |
| | 1 | 580 | | return; |
| | | 581 | | |
| | 8 | 582 | | VoxelNeighborResolver.AddRectangularNeighbors(this, ownerGrid, results); |
| | 8 | 583 | | } |
| | | 584 | | |
| | | 585 | | /// <summary> |
| | | 586 | | /// Clears and fills caller-owned storage with hex-prism neighbors in deterministic direction order. |
| | | 587 | | /// </summary> |
| | | 588 | | /// <param name="ownerGrid">The active grid that owns this voxel.</param> |
| | | 589 | | /// <param name="results">Caller-owned storage cleared and filled with direction-labeled neighbors.</param> |
| | | 590 | | public void GetHexNeighborsInto( |
| | | 591 | | VoxelGrid ownerGrid, |
| | | 592 | | SwiftList<(HexDirection Direction, Voxel Voxel)> results) |
| | | 593 | | { |
| | 4 | 594 | | SwiftThrowHelper.ThrowIfNull(results, nameof(results)); |
| | | 595 | | |
| | 4 | 596 | | results.Clear(); |
| | 4 | 597 | | if (!IsValidOwnerGrid(ownerGrid)) |
| | 1 | 598 | | return; |
| | | 599 | | |
| | 3 | 600 | | VoxelNeighborResolver.AddHexNeighbors(this, ownerGrid, results); |
| | 3 | 601 | | } |
| | | 602 | | |
| | | 603 | | private bool IsValidOwnerGrid(VoxelGrid? ownerGrid) |
| | | 604 | | { |
| | 127 | 605 | | return ownerGrid != null |
| | 127 | 606 | | && ownerGrid.IsActive |
| | 127 | 607 | | && ownerGrid.GridIndex == WorldIndex.GridIndex |
| | 127 | 608 | | && ownerGrid.SpawnToken == WorldIndex.GridSpawnToken |
| | 127 | 609 | | && ownerGrid.World != null |
| | 127 | 610 | | && ownerGrid.World.SpawnToken == WorldIndex.WorldSpawnToken; |
| | | 611 | | } |
| | | 612 | | |
| | | 613 | | #endregion |
| | | 614 | | |
| | | 615 | | #region Utility |
| | | 616 | | |
| | | 617 | | /// <inheritdoc/> |
| | 97453 | 618 | | public override int GetHashCode() => RuntimeHelpers.GetHashCode(this); |
| | | 619 | | |
| | | 620 | | /// <inheritdoc/> |
| | 1 | 621 | | public override string ToString() => WorldIndex.ToString(); |
| | | 622 | | |
| | | 623 | | /// <inheritdoc/> |
| | 81 | 624 | | public bool Equals(Voxel? other) => ReferenceEquals(this, other); |
| | | 625 | | |
| | | 626 | | /// <inheritdoc/> |
| | 2 | 627 | | public override bool Equals(object? obj) => ReferenceEquals(this, obj); |
| | | 628 | | |
| | | 629 | | #endregion |
| | | 630 | | } |