| | | 1 | | //======================================================================= |
| | | 2 | | // GridObstacleManager.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 FixedMathSharp; |
| | | 10 | | using GridForge.Spatial; |
| | | 11 | | using SwiftCollections.Pool; |
| | | 12 | | |
| | | 13 | | namespace GridForge.Grids; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Handles the addition, removal, and tracking of obstacles within a grid. |
| | | 17 | | /// Ensures thread safety and proper event notifications when obstacles change. |
| | | 18 | | /// </summary> |
| | | 19 | | public static class GridObstacleManager |
| | | 20 | | { |
| | | 21 | | #region Constants & Events |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Maximum number of obstacles that can exist on a single voxel. |
| | | 25 | | /// </summary> |
| | | 26 | | public const byte MaxObstacleCount = byte.MaxValue; |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Event triggered when an obstacle is added. |
| | | 30 | | /// </summary> |
| | | 31 | | private static Action<ObstacleEventInfo>? _onObstacleAdded; |
| | | 32 | | |
| | | 33 | | /// <inheritdoc cref="_onObstacleAdded"/> |
| | | 34 | | public static event Action<ObstacleEventInfo> OnObstacleAdded |
| | | 35 | | { |
| | 20 | 36 | | add => _onObstacleAdded += value; |
| | 20 | 37 | | remove => _onObstacleAdded -= value; |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Event triggered when an obstacle is removed. |
| | | 42 | | /// </summary> |
| | | 43 | | private static Action<ObstacleEventInfo>? _onObstacleRemoved; |
| | | 44 | | |
| | | 45 | | /// <inheritdoc cref="_onObstacleRemoved"/> |
| | | 46 | | public static event Action<ObstacleEventInfo> OnObstacleRemoved |
| | | 47 | | { |
| | 17 | 48 | | add => _onObstacleRemoved += value; |
| | 17 | 49 | | remove => _onObstacleRemoved -= value; |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Event triggered when all obstacles on a voxel are cleared at once. |
| | | 54 | | /// </summary> |
| | | 55 | | private static Action<ObstacleClearEventInfo>? _onObstaclesCleared; |
| | | 56 | | |
| | | 57 | | /// <inheritdoc cref="_onObstaclesCleared"/> |
| | | 58 | | public static event Action<ObstacleClearEventInfo> OnObstaclesCleared |
| | | 59 | | { |
| | 18 | 60 | | add => _onObstaclesCleared += value; |
| | 18 | 61 | | remove => _onObstaclesCleared -= value; |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | #endregion |
| | | 65 | | |
| | | 66 | | #region Public Methods |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// Attempts to add an obstacle at the given world-scoped voxel identity in the supplied world. |
| | | 70 | | /// </summary> |
| | | 71 | | public static bool TryAddObstacle( |
| | | 72 | | GridWorld world, |
| | | 73 | | WorldVoxelIndex index, |
| | | 74 | | ObstacleToken obstacleToken) |
| | | 75 | | { |
| | 3 | 76 | | return world != null |
| | 3 | 77 | | && world.TryGetGridAndVoxel(index, out VoxelGrid? grid, out Voxel? voxel) |
| | 3 | 78 | | && grid!.TryAddObstacle(voxel!, obstacleToken) == true; |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | /// <summary> |
| | | 82 | | /// Attempts to add an obstacle at the given world position. |
| | | 83 | | /// </summary> |
| | | 84 | | public static bool TryAddObstacle(this VoxelGrid grid, Vector3d position, ObstacleToken obstacleToken) |
| | | 85 | | { |
| | 6 | 86 | | return grid.TryGetVoxel(position, out Voxel? voxel) |
| | 6 | 87 | | && grid.TryAddObstacle(voxel!, obstacleToken); |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | /// <summary> |
| | | 91 | | /// Attempts to add an obstacle at the given XZ-plane world position on the default world Y layer. |
| | | 92 | | /// </summary> |
| | | 93 | | /// <param name="grid">The grid to mutate.</param> |
| | | 94 | | /// <param name="position">The 2D position whose X component maps to world X and Y component maps to world Z.</param |
| | | 95 | | /// <param name="obstacleToken">The obstacle token to attach to the resolved voxel.</param> |
| | | 96 | | /// <returns>True if an obstacle was added to the resolved voxel; otherwise false.</returns> |
| | | 97 | | public static bool TryAddObstacle(this VoxelGrid grid, Vector2d position, ObstacleToken obstacleToken) |
| | | 98 | | { |
| | 2 | 99 | | return grid.TryAddObstacle(position, default, obstacleToken); |
| | | 100 | | } |
| | | 101 | | |
| | | 102 | | /// <summary> |
| | | 103 | | /// Attempts to add an obstacle at the given XZ-plane world position on the supplied world Y layer. |
| | | 104 | | /// </summary> |
| | | 105 | | /// <param name="grid">The grid to mutate.</param> |
| | | 106 | | /// <param name="position">The 2D position whose X component maps to world X and Y component maps to world Z.</param |
| | | 107 | | /// <param name="layerY">The world Y layer to resolve.</param> |
| | | 108 | | /// <param name="obstacleToken">The obstacle token to attach to the resolved voxel.</param> |
| | | 109 | | /// <returns>True if an obstacle was added to the resolved voxel; otherwise false.</returns> |
| | | 110 | | public static bool TryAddObstacle(this VoxelGrid grid, Vector2d position, Fixed64 layerY, ObstacleToken obstacleToke |
| | | 111 | | { |
| | 3 | 112 | | return grid.TryAddObstacle(GridPlane2d.ToWorld(position, layerY), obstacleToken); |
| | | 113 | | } |
| | | 114 | | |
| | | 115 | | /// <summary> |
| | | 116 | | /// Adds an obstacle to this voxel. |
| | | 117 | | /// </summary> |
| | | 118 | | /// <param name="grid"></param> |
| | | 119 | | /// <param name="targetVoxel"></param> |
| | | 120 | | /// <param name="obstacleToken">The process-unique obstacle registration token.</param> |
| | | 121 | | /// <exception cref="Exception"></exception> |
| | | 122 | | public static bool TryAddObstacle(this VoxelGrid grid, Voxel targetVoxel, ObstacleToken obstacleToken) |
| | | 123 | | { |
| | 1031 | 124 | | if (!obstacleToken.IsValid || !targetVoxel.IsBlockable) |
| | 4 | 125 | | return false; |
| | | 126 | | |
| | | 127 | | byte obstacleCount; |
| | | 128 | | uint gridVersion; |
| | | 129 | | bool drainCommittedChanges; |
| | 1027 | 130 | | GridWorld? world = grid.World; |
| | 1027 | 131 | | if (world == null) |
| | 1 | 132 | | return false; |
| | | 133 | | |
| | 1026 | 134 | | world.EnterReadLock(); |
| | | 135 | | try |
| | | 136 | | { |
| | 1026 | 137 | | lock (grid.ObstacleSyncRoot) |
| | | 138 | | { |
| | 1026 | 139 | | lock (world.ChangeSyncRoot) |
| | | 140 | | { |
| | 1026 | 141 | | if (targetVoxel.ObstacleCount >= MaxObstacleCount) |
| | 1 | 142 | | return false; |
| | | 143 | | |
| | 1025 | 144 | | targetVoxel.ObstacleTracker ??= SwiftHashSetPool<ObstacleToken>.Shared.Rent(); |
| | 1025 | 145 | | if (!targetVoxel.ObstacleTracker.Add(obstacleToken)) |
| | 1 | 146 | | return false; |
| | 1024 | 147 | | targetVoxel.ObstacleCount++; |
| | | 148 | | |
| | 1024 | 149 | | grid.ObstacleCount++; |
| | 1024 | 150 | | gridVersion = grid.IncrementVersion(); |
| | 1024 | 151 | | obstacleCount = targetVoxel.ObstacleCount; |
| | | 152 | | |
| | 1024 | 153 | | CreateObstacleCommittedChange( |
| | 1024 | 154 | | world, |
| | 1024 | 155 | | grid, |
| | 1024 | 156 | | targetVoxel, |
| | 1024 | 157 | | GridEventKind.ObstacleAdded, |
| | 1024 | 158 | | GridExactChangeKind.ObstacleAdded, |
| | 1024 | 159 | | obstacleToken, |
| | 1024 | 160 | | obstacleCount, |
| | 1024 | 161 | | gridVersion, |
| | 1024 | 162 | | out _, |
| | 1024 | 163 | | out drainCommittedChanges); |
| | 1024 | 164 | | } |
| | | 165 | | } |
| | | 166 | | } |
| | | 167 | | finally |
| | | 168 | | { |
| | 1026 | 169 | | world.ExitReadLock(); |
| | 1026 | 170 | | } |
| | | 171 | | |
| | 1024 | 172 | | if (drainCommittedChanges) |
| | 650 | 173 | | world.DrainCommittedChanges(); |
| | | 174 | | |
| | 1024 | 175 | | return true; |
| | 2 | 176 | | } |
| | | 177 | | |
| | | 178 | | /// <summary> |
| | | 179 | | /// Attempts to remove an obstacle at the given world-scoped voxel identity in the supplied world. |
| | | 180 | | /// </summary> |
| | | 181 | | public static bool TryRemoveObstacle( |
| | | 182 | | GridWorld world, |
| | | 183 | | WorldVoxelIndex index, |
| | | 184 | | ObstacleToken obstacleToken) |
| | | 185 | | { |
| | 33 | 186 | | return world != null |
| | 33 | 187 | | && world.TryGetGridAndVoxel(index, out VoxelGrid? grid, out Voxel? voxel) |
| | 33 | 188 | | && grid!.TryRemoveObstacle(voxel!, obstacleToken); |
| | | 189 | | } |
| | | 190 | | |
| | | 191 | | /// <summary> |
| | | 192 | | /// Attempts to remove an obstacle from the specified world position. |
| | | 193 | | /// </summary> |
| | | 194 | | public static bool TryRemoveObstacle(this VoxelGrid grid, Vector3d position, ObstacleToken obstacleToken) |
| | | 195 | | { |
| | 6 | 196 | | return grid.TryGetVoxel(position, out Voxel? voxel) |
| | 6 | 197 | | && grid.TryRemoveObstacle(voxel!, obstacleToken); |
| | | 198 | | } |
| | | 199 | | |
| | | 200 | | /// <summary> |
| | | 201 | | /// Attempts to remove an obstacle from the given XZ-plane world position on the default world Y layer. |
| | | 202 | | /// </summary> |
| | | 203 | | /// <param name="grid">The grid to mutate.</param> |
| | | 204 | | /// <param name="position">The 2D position whose X component maps to world X and Y component maps to world Z.</param |
| | | 205 | | /// <param name="obstacleToken">The obstacle token to remove from the resolved voxel.</param> |
| | | 206 | | /// <returns>True if the obstacle was removed from the resolved voxel; otherwise false.</returns> |
| | | 207 | | public static bool TryRemoveObstacle(this VoxelGrid grid, Vector2d position, ObstacleToken obstacleToken) |
| | | 208 | | { |
| | 1 | 209 | | return grid.TryRemoveObstacle(position, default, obstacleToken); |
| | | 210 | | } |
| | | 211 | | |
| | | 212 | | /// <summary> |
| | | 213 | | /// Attempts to remove an obstacle from the given XZ-plane world position on the supplied world Y layer. |
| | | 214 | | /// </summary> |
| | | 215 | | /// <param name="grid">The grid to mutate.</param> |
| | | 216 | | /// <param name="position">The 2D position whose X component maps to world X and Y component maps to world Z.</param |
| | | 217 | | /// <param name="layerY">The world Y layer to resolve.</param> |
| | | 218 | | /// <param name="obstacleToken">The obstacle token to remove from the resolved voxel.</param> |
| | | 219 | | /// <returns>True if the obstacle was removed from the resolved voxel; otherwise false.</returns> |
| | | 220 | | public static bool TryRemoveObstacle(this VoxelGrid grid, Vector2d position, Fixed64 layerY, ObstacleToken obstacleT |
| | | 221 | | { |
| | 3 | 222 | | return grid.TryRemoveObstacle(GridPlane2d.ToWorld(position, layerY), obstacleToken); |
| | | 223 | | } |
| | | 224 | | |
| | | 225 | | /// <summary> |
| | | 226 | | /// Removes an obstacle from a given voxel. |
| | | 227 | | /// </summary> |
| | | 228 | | public static bool TryRemoveObstacle(this VoxelGrid grid, Voxel targetVoxel, ObstacleToken obstacleToken) |
| | | 229 | | { |
| | 98 | 230 | | if (!obstacleToken.IsValid) |
| | 1 | 231 | | return false; |
| | | 232 | | |
| | 97 | 233 | | if (targetVoxel.ObstacleCount == 0) |
| | | 234 | | { |
| | 5 | 235 | | GridForgeLogger.Channel.Warn($"No obstacle to remove on voxel ({targetVoxel.WorldIndex})!"); |
| | 5 | 236 | | return false; |
| | | 237 | | } |
| | | 238 | | |
| | | 239 | | byte obstacleCount; |
| | | 240 | | uint gridVersion; |
| | | 241 | | bool drainCommittedChanges; |
| | 92 | 242 | | GridWorld? world = grid.World; |
| | 92 | 243 | | if (world == null) |
| | 1 | 244 | | return false; |
| | | 245 | | |
| | 91 | 246 | | world.EnterReadLock(); |
| | | 247 | | try |
| | | 248 | | { |
| | 91 | 249 | | lock (grid.ObstacleSyncRoot) |
| | | 250 | | { |
| | 91 | 251 | | lock (world.ChangeSyncRoot) |
| | | 252 | | { |
| | 91 | 253 | | if (!targetVoxel.ObstacleTracker!.Remove(obstacleToken)) |
| | 2 | 254 | | return false; |
| | | 255 | | |
| | 89 | 256 | | if (--targetVoxel.ObstacleCount <= 0) |
| | | 257 | | { |
| | 79 | 258 | | SwiftHashSetPool<ObstacleToken>.Shared.Release(targetVoxel.ObstacleTracker); |
| | 79 | 259 | | targetVoxel.ObstacleTracker = null; |
| | 79 | 260 | | targetVoxel.ObstacleCount = 0; |
| | | 261 | | } |
| | | 262 | | |
| | 89 | 263 | | grid.ObstacleCount--; |
| | 89 | 264 | | gridVersion = grid.IncrementVersion(); |
| | 89 | 265 | | obstacleCount = targetVoxel.ObstacleCount; |
| | | 266 | | |
| | 89 | 267 | | CreateObstacleCommittedChange( |
| | 89 | 268 | | world, |
| | 89 | 269 | | grid, |
| | 89 | 270 | | targetVoxel, |
| | 89 | 271 | | GridEventKind.ObstacleRemoved, |
| | 89 | 272 | | GridExactChangeKind.ObstacleRemoved, |
| | 89 | 273 | | obstacleToken, |
| | 89 | 274 | | obstacleCount, |
| | 89 | 275 | | gridVersion, |
| | 89 | 276 | | out _, |
| | 89 | 277 | | out drainCommittedChanges); |
| | 89 | 278 | | } |
| | | 279 | | } |
| | | 280 | | } |
| | | 281 | | finally |
| | | 282 | | { |
| | 91 | 283 | | world.ExitReadLock(); |
| | 91 | 284 | | } |
| | | 285 | | |
| | 89 | 286 | | if (drainCommittedChanges) |
| | 87 | 287 | | world.DrainCommittedChanges(); |
| | | 288 | | |
| | 89 | 289 | | return true; |
| | 2 | 290 | | } |
| | | 291 | | |
| | | 292 | | /// <summary> |
| | | 293 | | /// Clears all obstacles from the specified voxel. |
| | | 294 | | /// </summary> |
| | | 295 | | /// <param name="grid"></param> |
| | | 296 | | /// <param name="targetVoxel"></param> |
| | | 297 | | public static void ClearObstacles(this VoxelGrid grid, Voxel targetVoxel) |
| | | 298 | | { |
| | 567 | 299 | | if (targetVoxel.ObstacleCount == 0) |
| | 1 | 300 | | return; |
| | | 301 | | |
| | | 302 | | byte clearedObstacleCount; |
| | | 303 | | uint gridVersion; |
| | | 304 | | bool drainCommittedChanges; |
| | 566 | 305 | | GridWorld? world = grid.World; |
| | 566 | 306 | | if (world == null) |
| | 1 | 307 | | return; |
| | | 308 | | |
| | 565 | 309 | | bool enteredReadLock = !world.IsWriteLockHeld; |
| | 565 | 310 | | if (enteredReadLock) |
| | 10 | 311 | | world.EnterReadLock(); |
| | | 312 | | try |
| | | 313 | | { |
| | 565 | 314 | | lock (grid.ObstacleSyncRoot) |
| | | 315 | | { |
| | 565 | 316 | | lock (world.ChangeSyncRoot) |
| | | 317 | | { |
| | 565 | 318 | | clearedObstacleCount = targetVoxel.ObstacleCount; |
| | 565 | 319 | | if (targetVoxel.ObstacleTracker != null) |
| | | 320 | | { |
| | 565 | 321 | | SwiftHashSetPool<ObstacleToken>.Shared.Release(targetVoxel.ObstacleTracker); |
| | 565 | 322 | | targetVoxel.ObstacleTracker = null; |
| | | 323 | | } |
| | | 324 | | |
| | 565 | 325 | | grid.ObstacleCount -= targetVoxel.ObstacleCount; |
| | 565 | 326 | | targetVoxel.ObstacleCount = 0; |
| | 565 | 327 | | gridVersion = grid.IncrementVersion(); |
| | | 328 | | |
| | 565 | 329 | | CreateObstacleClearCommittedChange( |
| | 565 | 330 | | world, |
| | 565 | 331 | | grid, |
| | 565 | 332 | | targetVoxel, |
| | 565 | 333 | | clearedObstacleCount, |
| | 565 | 334 | | gridVersion, |
| | 565 | 335 | | out _, |
| | 565 | 336 | | out drainCommittedChanges); |
| | 565 | 337 | | } |
| | | 338 | | } |
| | | 339 | | } |
| | | 340 | | finally |
| | | 341 | | { |
| | 565 | 342 | | if (enteredReadLock) |
| | 10 | 343 | | world.ExitReadLock(); |
| | 565 | 344 | | } |
| | | 345 | | |
| | 565 | 346 | | if (drainCommittedChanges && enteredReadLock) |
| | 4 | 347 | | world.DrainCommittedChanges(); |
| | 565 | 348 | | } |
| | | 349 | | |
| | | 350 | | #endregion |
| | | 351 | | |
| | | 352 | | #region Private Methods |
| | | 353 | | |
| | | 354 | | private static void CreateObstacleCommittedChange( |
| | | 355 | | GridWorld world, |
| | | 356 | | VoxelGrid grid, |
| | | 357 | | Voxel targetVoxel, |
| | | 358 | | GridEventKind gridEventKind, |
| | | 359 | | GridExactChangeKind exactChangeKind, |
| | | 360 | | ObstacleToken obstacleToken, |
| | | 361 | | byte obstacleCount, |
| | | 362 | | uint gridVersion, |
| | | 363 | | out GridCommittedChange committedChange, |
| | | 364 | | out bool drainCommittedChanges) |
| | | 365 | | { |
| | 1113 | 366 | | GridChangeStamp changeStamp = world.AllocateChangeStamp(); |
| | 1113 | 367 | | ObstacleEventInfo obstacleEvent = new ObstacleEventInfo( |
| | 1113 | 368 | | targetVoxel.WorldIndex, |
| | 1113 | 369 | | obstacleToken, |
| | 1113 | 370 | | obstacleCount, |
| | 1113 | 371 | | gridVersion, |
| | 1113 | 372 | | changeStamp); |
| | 1113 | 373 | | GridEventInfo gridEvent = world.CreateGridEventInfo( |
| | 1113 | 374 | | grid, |
| | 1113 | 375 | | gridEventKind, |
| | 1113 | 376 | | targetVoxel.Index, |
| | 1113 | 377 | | targetVoxel.WorldPosition, |
| | 1113 | 378 | | targetVoxel.WorldPosition, |
| | 1113 | 379 | | changeStamp, |
| | 1113 | 380 | | hasVoxelState: true, |
| | 1113 | 381 | | isVoxelPresent: true, |
| | 1113 | 382 | | obstacleCount); |
| | 1113 | 383 | | committedChange = new GridCommittedChange( |
| | 1113 | 384 | | gridEvent, |
| | 1113 | 385 | | exactChangeKind, |
| | 1113 | 386 | | obstacleEvent, |
| | 1113 | 387 | | targetVoxel); |
| | 1113 | 388 | | targetVoxel.CachedGridVersion = gridVersion; |
| | 1113 | 389 | | drainCommittedChanges = world.EnqueueCommittedChange(committedChange); |
| | 1113 | 390 | | } |
| | | 391 | | |
| | | 392 | | private static void CreateObstacleClearCommittedChange( |
| | | 393 | | GridWorld world, |
| | | 394 | | VoxelGrid grid, |
| | | 395 | | Voxel targetVoxel, |
| | | 396 | | byte clearedObstacleCount, |
| | | 397 | | uint gridVersion, |
| | | 398 | | out GridCommittedChange committedChange, |
| | | 399 | | out bool drainCommittedChanges) |
| | | 400 | | { |
| | 565 | 401 | | GridChangeStamp changeStamp = world.AllocateChangeStamp(); |
| | 565 | 402 | | ObstacleClearEventInfo clearEvent = new ObstacleClearEventInfo( |
| | 565 | 403 | | targetVoxel.WorldIndex, |
| | 565 | 404 | | clearedObstacleCount, |
| | 565 | 405 | | gridVersion, |
| | 565 | 406 | | changeStamp); |
| | 565 | 407 | | GridEventInfo gridEvent = world.CreateGridEventInfo( |
| | 565 | 408 | | grid, |
| | 565 | 409 | | GridEventKind.ObstaclesCleared, |
| | 565 | 410 | | targetVoxel.Index, |
| | 565 | 411 | | targetVoxel.WorldPosition, |
| | 565 | 412 | | targetVoxel.WorldPosition, |
| | 565 | 413 | | changeStamp, |
| | 565 | 414 | | hasVoxelState: true, |
| | 565 | 415 | | isVoxelPresent: true, |
| | 565 | 416 | | obstacleCount: 0); |
| | 565 | 417 | | committedChange = new GridCommittedChange(gridEvent, clearEvent, targetVoxel); |
| | 565 | 418 | | targetVoxel.CachedGridVersion = gridVersion; |
| | 565 | 419 | | drainCommittedChanges = world.EnqueueCommittedChange(committedChange); |
| | 565 | 420 | | } |
| | | 421 | | |
| | | 422 | | /// <summary> |
| | | 423 | | /// Notifies listeners that an obstacle was added. |
| | | 424 | | /// </summary> |
| | | 425 | | private static void NotifyObstacleAdded(ObstacleEventInfo eventInfo) |
| | | 426 | | { |
| | 1025 | 427 | | Action<ObstacleEventInfo>? handlers = _onObstacleAdded; |
| | 1025 | 428 | | if (handlers != null) |
| | | 429 | | { |
| | 11 | 430 | | var handlerDelegates = handlers.GetInvocationList(); |
| | 50 | 431 | | for (int i = 0; i < handlerDelegates.Length; i++) |
| | | 432 | | { |
| | | 433 | | try |
| | | 434 | | { |
| | 14 | 435 | | ((Action<ObstacleEventInfo>)handlerDelegates[i])(eventInfo); |
| | 12 | 436 | | } |
| | 2 | 437 | | catch (Exception ex) |
| | | 438 | | { |
| | 2 | 439 | | GridForgeLogger.Channel.Error($"[Voxel {eventInfo.VoxelIndex}] Obstacle add error: {ex.Message}"); |
| | 2 | 440 | | } |
| | | 441 | | } |
| | | 442 | | } |
| | | 443 | | |
| | | 444 | | // Voxel-local delivery is handled by NotifyCommittedExact after exact identity validation. |
| | 1025 | 445 | | } |
| | | 446 | | |
| | | 447 | | /// <summary> |
| | | 448 | | /// Notifies listeners that an obstacle was removed. |
| | | 449 | | /// </summary> |
| | | 450 | | private static void NotifyObstacleRemoved(ObstacleEventInfo eventInfo) |
| | | 451 | | { |
| | 90 | 452 | | Action<ObstacleEventInfo>? handlers = _onObstacleRemoved; |
| | 90 | 453 | | if (handlers != null) |
| | | 454 | | { |
| | 4 | 455 | | var handlerDelegates = handlers.GetInvocationList(); |
| | 18 | 456 | | for (int i = 0; i < handlerDelegates.Length; i++) |
| | | 457 | | { |
| | | 458 | | try |
| | | 459 | | { |
| | 5 | 460 | | ((Action<ObstacleEventInfo>)handlerDelegates[i])(eventInfo); |
| | 4 | 461 | | } |
| | 1 | 462 | | catch (Exception ex) |
| | | 463 | | { |
| | 1 | 464 | | GridForgeLogger.Channel.Error($"[Voxel {eventInfo.VoxelIndex}] Obstacle remove error: {ex.Message}") |
| | 1 | 465 | | } |
| | | 466 | | } |
| | | 467 | | } |
| | | 468 | | |
| | | 469 | | // Voxel-local delivery is handled by NotifyCommittedExact after exact identity validation. |
| | 90 | 470 | | } |
| | | 471 | | |
| | | 472 | | /// <summary> |
| | | 473 | | /// Notifies listeners that all obstacles on a voxel were cleared. |
| | | 474 | | /// </summary> |
| | | 475 | | private static void NotifyObstaclesCleared(ObstacleClearEventInfo eventInfo) |
| | | 476 | | { |
| | 565 | 477 | | Action<ObstacleClearEventInfo>? handlers = _onObstaclesCleared; |
| | 565 | 478 | | if (handlers != null) |
| | | 479 | | { |
| | 5 | 480 | | var handlerDelegates = handlers.GetInvocationList(); |
| | 22 | 481 | | for (int i = 0; i < handlerDelegates.Length; i++) |
| | | 482 | | { |
| | | 483 | | try |
| | | 484 | | { |
| | 6 | 485 | | ((Action<ObstacleClearEventInfo>)handlerDelegates[i])(eventInfo); |
| | 5 | 486 | | } |
| | 1 | 487 | | catch (Exception ex) |
| | | 488 | | { |
| | 1 | 489 | | GridForgeLogger.Channel.Error($"[Voxel {eventInfo.VoxelIndex}] Obstacle clear error: {ex.Message}"); |
| | 1 | 490 | | } |
| | | 491 | | } |
| | | 492 | | } |
| | | 493 | | |
| | | 494 | | // Voxel-local delivery is handled by NotifyCommittedExact after exact identity validation. |
| | 565 | 495 | | } |
| | | 496 | | |
| | | 497 | | internal static void NotifyCommittedExact(GridCommittedChange change) |
| | | 498 | | { |
| | 3495 | 499 | | switch (change.ExactKind) |
| | | 500 | | { |
| | | 501 | | case GridExactChangeKind.ObstacleAdded: |
| | 1025 | 502 | | NotifyObstacleAdded(change.ObstacleEvent); |
| | 1025 | 503 | | if (IsSameVoxel(change.TargetVoxel, change.ObstacleEvent.VoxelIndex)) |
| | 1024 | 504 | | change.TargetVoxel!.NotifyObstacleAdded(change.ObstacleEvent); |
| | 1024 | 505 | | break; |
| | | 506 | | case GridExactChangeKind.ObstacleRemoved: |
| | 90 | 507 | | NotifyObstacleRemoved(change.ObstacleEvent); |
| | 90 | 508 | | if (IsSameVoxel(change.TargetVoxel, change.ObstacleEvent.VoxelIndex)) |
| | 89 | 509 | | change.TargetVoxel!.NotifyObstacleRemoved(change.ObstacleEvent); |
| | 89 | 510 | | break; |
| | | 511 | | case GridExactChangeKind.ObstaclesCleared: |
| | 565 | 512 | | NotifyObstaclesCleared(change.ObstacleClearEvent); |
| | 565 | 513 | | if (IsSameVoxel(change.TargetVoxel, change.ObstacleClearEvent.VoxelIndex)) |
| | 4 | 514 | | change.TargetVoxel!.NotifyObstaclesCleared(change.ObstacleClearEvent); |
| | | 515 | | break; |
| | | 516 | | } |
| | 567 | 517 | | } |
| | | 518 | | |
| | | 519 | | private static bool IsSameVoxel(Voxel? voxel, WorldVoxelIndex index) => |
| | 1680 | 520 | | voxel != null && voxel.IsAllocated && voxel.WorldIndex == index; |
| | | 521 | | |
| | | 522 | | #endregion |
| | | 523 | | } |