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