| | | 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 FixedMathSharp; |
| | | 9 | | using GridForge.Spatial; |
| | | 10 | | using SwiftCollections.Pool; |
| | | 11 | | using System; |
| | | 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 | | { |
| | 10 | 36 | | add => _onObstacleAdded += value; |
| | 10 | 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 | | { |
| | 9 | 48 | | add => _onObstacleRemoved += value; |
| | 9 | 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 | | { |
| | 10 | 60 | | add => _onObstaclesCleared += value; |
| | 10 | 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 | | BoundsKey obstacleSpawnToken) |
| | | 75 | | { |
| | 2 | 76 | | return world != null |
| | 2 | 77 | | && world.TryGetGridAndVoxel(index, out VoxelGrid? grid, out Voxel? voxel) |
| | 2 | 78 | | && grid!.TryAddObstacle(voxel!, obstacleSpawnToken) == 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, BoundsKey obstacleSpawnToken) |
| | | 85 | | { |
| | 6 | 86 | | return grid.TryGetVoxel(position, out Voxel? voxel) |
| | 6 | 87 | | && grid.TryAddObstacle(voxel!, obstacleSpawnToken); |
| | | 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="obstacleSpawnToken">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, BoundsKey obstacleSpawnToken) |
| | | 98 | | { |
| | 2 | 99 | | return grid.TryAddObstacle(position, default, obstacleSpawnToken); |
| | | 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="obstacleSpawnToken">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, BoundsKey obstacleSpawnTok |
| | | 111 | | { |
| | 3 | 112 | | return grid.TryAddObstacle(GridPlane2d.ToWorld(position, layerY), obstacleSpawnToken); |
| | | 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="obstacleSpawnToken"></param> |
| | | 121 | | /// <exception cref="Exception"></exception> |
| | | 122 | | public static bool TryAddObstacle(this VoxelGrid grid, Voxel targetVoxel, BoundsKey obstacleSpawnToken) |
| | | 123 | | { |
| | 750 | 124 | | if (!targetVoxel.IsBlockable) |
| | 3 | 125 | | return false; |
| | | 126 | | |
| | | 127 | | byte obstacleCount; |
| | | 128 | | uint gridVersion; |
| | | 129 | | |
| | 747 | 130 | | lock (grid.ObstacleSyncRoot) |
| | | 131 | | { |
| | 747 | 132 | | targetVoxel.ObstacleTracker ??= SwiftHashSetPool<BoundsKey>.Shared.Rent(); |
| | 747 | 133 | | if (!targetVoxel.ObstacleTracker.Add(obstacleSpawnToken)) |
| | 1 | 134 | | return false; |
| | 746 | 135 | | targetVoxel.ObstacleCount++; |
| | | 136 | | |
| | 746 | 137 | | grid.ObstacleCount++; |
| | 746 | 138 | | gridVersion = grid.IncrementVersion(); |
| | 746 | 139 | | obstacleCount = targetVoxel.ObstacleCount; |
| | 746 | 140 | | } |
| | | 141 | | |
| | 746 | 142 | | NotifyObstacleAdded(grid, targetVoxel, obstacleSpawnToken, obstacleCount, gridVersion); |
| | | 143 | | |
| | 746 | 144 | | return true; |
| | 1 | 145 | | } |
| | | 146 | | |
| | | 147 | | /// <summary> |
| | | 148 | | /// Attempts to remove an obstacle at the given world-scoped voxel identity in the supplied world. |
| | | 149 | | /// </summary> |
| | | 150 | | public static bool TryRemoveObstacle( |
| | | 151 | | GridWorld world, |
| | | 152 | | WorldVoxelIndex index, |
| | | 153 | | BoundsKey obstacleSpawnToken) |
| | | 154 | | { |
| | 34 | 155 | | return world != null |
| | 34 | 156 | | && world.TryGetGridAndVoxel(index, out VoxelGrid? grid, out Voxel? voxel) |
| | 34 | 157 | | && grid!.TryRemoveObstacle(voxel!, obstacleSpawnToken); |
| | | 158 | | } |
| | | 159 | | |
| | | 160 | | /// <summary> |
| | | 161 | | /// Attempts to remove an obstacle from the specified world position. |
| | | 162 | | /// </summary> |
| | | 163 | | public static bool TryRemoveObstacle(this VoxelGrid grid, Vector3d position, BoundsKey obstacleSpawnToken) |
| | | 164 | | { |
| | 6 | 165 | | return grid.TryGetVoxel(position, out Voxel? voxel) |
| | 6 | 166 | | && grid.TryRemoveObstacle(voxel!, obstacleSpawnToken); |
| | | 167 | | } |
| | | 168 | | |
| | | 169 | | /// <summary> |
| | | 170 | | /// Attempts to remove an obstacle from the given XZ-plane world position on the default world Y layer. |
| | | 171 | | /// </summary> |
| | | 172 | | /// <param name="grid">The grid to mutate.</param> |
| | | 173 | | /// <param name="position">The 2D position whose X component maps to world X and Y component maps to world Z.</param |
| | | 174 | | /// <param name="obstacleSpawnToken">The obstacle token to remove from the resolved voxel.</param> |
| | | 175 | | /// <returns>True if the obstacle was removed from the resolved voxel; otherwise false.</returns> |
| | | 176 | | public static bool TryRemoveObstacle(this VoxelGrid grid, Vector2d position, BoundsKey obstacleSpawnToken) |
| | | 177 | | { |
| | 1 | 178 | | return grid.TryRemoveObstacle(position, default, obstacleSpawnToken); |
| | | 179 | | } |
| | | 180 | | |
| | | 181 | | /// <summary> |
| | | 182 | | /// Attempts to remove an obstacle from the given XZ-plane world position on the supplied world Y layer. |
| | | 183 | | /// </summary> |
| | | 184 | | /// <param name="grid">The grid to mutate.</param> |
| | | 185 | | /// <param name="position">The 2D position whose X component maps to world X and Y component maps to world Z.</param |
| | | 186 | | /// <param name="layerY">The world Y layer to resolve.</param> |
| | | 187 | | /// <param name="obstacleSpawnToken">The obstacle token to remove from the resolved voxel.</param> |
| | | 188 | | /// <returns>True if the obstacle was removed from the resolved voxel; otherwise false.</returns> |
| | | 189 | | public static bool TryRemoveObstacle(this VoxelGrid grid, Vector2d position, Fixed64 layerY, BoundsKey obstacleSpawn |
| | | 190 | | { |
| | 3 | 191 | | return grid.TryRemoveObstacle(GridPlane2d.ToWorld(position, layerY), obstacleSpawnToken); |
| | | 192 | | } |
| | | 193 | | |
| | | 194 | | /// <summary> |
| | | 195 | | /// Removes an obstacle from a given voxel. |
| | | 196 | | /// </summary> |
| | | 197 | | public static bool TryRemoveObstacle(this VoxelGrid grid, Voxel targetVoxel, BoundsKey obstacleSpawnToken) |
| | | 198 | | { |
| | 77 | 199 | | if (targetVoxel.ObstacleCount == 0) |
| | | 200 | | { |
| | 5 | 201 | | GridForgeLogger.Channel.Warn($"No obstacle to remove on voxel ({targetVoxel.WorldIndex})!"); |
| | 5 | 202 | | return false; |
| | | 203 | | } |
| | | 204 | | |
| | | 205 | | byte obstacleCount; |
| | | 206 | | uint gridVersion; |
| | | 207 | | |
| | 72 | 208 | | lock (grid.ObstacleSyncRoot) |
| | | 209 | | { |
| | 72 | 210 | | if (!targetVoxel.ObstacleTracker!.Remove(obstacleSpawnToken)) |
| | 1 | 211 | | return false; |
| | | 212 | | |
| | 71 | 213 | | if (--targetVoxel.ObstacleCount <= 0) |
| | | 214 | | { |
| | 65 | 215 | | SwiftHashSetPool<BoundsKey>.Shared.Release(targetVoxel.ObstacleTracker); |
| | 65 | 216 | | targetVoxel.ObstacleTracker = null; |
| | 65 | 217 | | targetVoxel.ObstacleCount = 0; |
| | | 218 | | } |
| | | 219 | | |
| | 71 | 220 | | grid.ObstacleCount--; |
| | 71 | 221 | | gridVersion = grid.IncrementVersion(); |
| | 71 | 222 | | obstacleCount = targetVoxel.ObstacleCount; |
| | 71 | 223 | | } |
| | | 224 | | |
| | 71 | 225 | | NotifyObstacleRemoved(grid, targetVoxel, obstacleSpawnToken, obstacleCount, gridVersion); |
| | | 226 | | |
| | 71 | 227 | | return true; |
| | 1 | 228 | | } |
| | | 229 | | |
| | | 230 | | /// <summary> |
| | | 231 | | /// Clears all obstacles from the specified voxel. |
| | | 232 | | /// </summary> |
| | | 233 | | /// <param name="grid"></param> |
| | | 234 | | /// <param name="targetVoxel"></param> |
| | | 235 | | public static void ClearObstacles(this VoxelGrid grid, Voxel targetVoxel) |
| | | 236 | | { |
| | 561 | 237 | | if (targetVoxel.ObstacleCount == 0) |
| | 1 | 238 | | return; |
| | | 239 | | |
| | | 240 | | byte clearedObstacleCount; |
| | | 241 | | uint gridVersion; |
| | | 242 | | |
| | 560 | 243 | | lock (grid.ObstacleSyncRoot) |
| | | 244 | | { |
| | 560 | 245 | | clearedObstacleCount = targetVoxel.ObstacleCount; |
| | 560 | 246 | | if (targetVoxel.ObstacleTracker != null) |
| | | 247 | | { |
| | 560 | 248 | | SwiftHashSetPool<BoundsKey>.Shared.Release(targetVoxel.ObstacleTracker); |
| | 560 | 249 | | targetVoxel.ObstacleTracker = null; |
| | | 250 | | } |
| | | 251 | | |
| | 560 | 252 | | grid.ObstacleCount -= targetVoxel.ObstacleCount; |
| | 560 | 253 | | targetVoxel.ObstacleCount = 0; |
| | 560 | 254 | | gridVersion = grid.IncrementVersion(); |
| | 560 | 255 | | } |
| | | 256 | | |
| | 560 | 257 | | NotifyObstaclesCleared(grid, targetVoxel, clearedObstacleCount, gridVersion); |
| | 560 | 258 | | } |
| | | 259 | | |
| | | 260 | | #endregion |
| | | 261 | | |
| | | 262 | | #region Private Methods |
| | | 263 | | |
| | | 264 | | /// <summary> |
| | | 265 | | /// Notifies listeners that an obstacle was added. |
| | | 266 | | /// </summary> |
| | | 267 | | private static void NotifyObstacleAdded( |
| | | 268 | | VoxelGrid grid, |
| | | 269 | | Voxel targetVoxel, |
| | | 270 | | BoundsKey obstacleSpawnToken, |
| | | 271 | | byte obstacleCount, |
| | | 272 | | uint gridVersion) |
| | | 273 | | { |
| | 746 | 274 | | ObstacleEventInfo eventInfo = new(targetVoxel.WorldIndex, obstacleSpawnToken, obstacleCount, gridVersion); |
| | 746 | 275 | | Action<ObstacleEventInfo>? handlers = _onObstacleAdded; |
| | 746 | 276 | | if (handlers != null) |
| | | 277 | | { |
| | 7 | 278 | | var handlerDelegates = handlers.GetInvocationList(); |
| | 32 | 279 | | for (int i = 0; i < handlerDelegates.Length; i++) |
| | | 280 | | { |
| | | 281 | | try |
| | | 282 | | { |
| | 9 | 283 | | ((Action<ObstacleEventInfo>)handlerDelegates[i])(eventInfo); |
| | 7 | 284 | | } |
| | 2 | 285 | | catch (Exception ex) |
| | | 286 | | { |
| | 2 | 287 | | GridForgeLogger.Channel.Error($"[Voxel {targetVoxel.WorldIndex}] Obstacle add error: {ex.Message}"); |
| | 2 | 288 | | } |
| | | 289 | | } |
| | | 290 | | } |
| | | 291 | | |
| | 746 | 292 | | targetVoxel.NotifyObstacleAdded(eventInfo); |
| | | 293 | | |
| | 746 | 294 | | targetVoxel.CachedGridVersion = gridVersion; |
| | 746 | 295 | | grid.World!.NotifyActiveGridChange(grid); |
| | 746 | 296 | | } |
| | | 297 | | |
| | | 298 | | /// <summary> |
| | | 299 | | /// Notifies listeners that an obstacle was removed. |
| | | 300 | | /// </summary> |
| | | 301 | | private static void NotifyObstacleRemoved( |
| | | 302 | | VoxelGrid grid, |
| | | 303 | | Voxel targetVoxel, |
| | | 304 | | BoundsKey obstacleSpawnToken, |
| | | 305 | | byte obstacleCount, |
| | | 306 | | uint gridVersion) |
| | | 307 | | { |
| | 71 | 308 | | ObstacleEventInfo eventInfo = new(targetVoxel.WorldIndex, obstacleSpawnToken, obstacleCount, gridVersion); |
| | 71 | 309 | | Action<ObstacleEventInfo>? handlers = _onObstacleRemoved; |
| | 71 | 310 | | if (handlers != null) |
| | | 311 | | { |
| | 3 | 312 | | var handlerDelegates = handlers.GetInvocationList(); |
| | 14 | 313 | | for (int i = 0; i < handlerDelegates.Length; i++) |
| | | 314 | | { |
| | | 315 | | try |
| | | 316 | | { |
| | 4 | 317 | | ((Action<ObstacleEventInfo>)handlerDelegates[i])(eventInfo); |
| | 3 | 318 | | } |
| | 1 | 319 | | catch (Exception ex) |
| | | 320 | | { |
| | 1 | 321 | | GridForgeLogger.Channel.Error($"[Voxel {targetVoxel.WorldIndex}] Obstacle remove error: {ex.Message} |
| | 1 | 322 | | } |
| | | 323 | | } |
| | | 324 | | } |
| | | 325 | | |
| | 71 | 326 | | targetVoxel.NotifyObstacleRemoved(eventInfo); |
| | | 327 | | |
| | 71 | 328 | | targetVoxel.CachedGridVersion = gridVersion; |
| | 71 | 329 | | grid.World!.NotifyActiveGridChange(grid); |
| | 71 | 330 | | } |
| | | 331 | | |
| | | 332 | | /// <summary> |
| | | 333 | | /// Notifies listeners that all obstacles on a voxel were cleared. |
| | | 334 | | /// </summary> |
| | | 335 | | private static void NotifyObstaclesCleared( |
| | | 336 | | VoxelGrid grid, |
| | | 337 | | Voxel targetVoxel, |
| | | 338 | | byte clearedObstacleCount, |
| | | 339 | | uint gridVersion) |
| | | 340 | | { |
| | 560 | 341 | | ObstacleClearEventInfo eventInfo = new(targetVoxel.WorldIndex, clearedObstacleCount, gridVersion); |
| | 560 | 342 | | Action<ObstacleClearEventInfo>? handlers = _onObstaclesCleared; |
| | 560 | 343 | | if (handlers != null) |
| | | 344 | | { |
| | 3 | 345 | | var handlerDelegates = handlers.GetInvocationList(); |
| | 14 | 346 | | for (int i = 0; i < handlerDelegates.Length; i++) |
| | | 347 | | { |
| | | 348 | | try |
| | | 349 | | { |
| | 4 | 350 | | ((Action<ObstacleClearEventInfo>)handlerDelegates[i])(eventInfo); |
| | 3 | 351 | | } |
| | 1 | 352 | | catch (Exception ex) |
| | | 353 | | { |
| | 1 | 354 | | GridForgeLogger.Channel.Error($"[Voxel {targetVoxel.WorldIndex}] Obstacle clear error: {ex.Message}" |
| | 1 | 355 | | } |
| | | 356 | | } |
| | | 357 | | } |
| | | 358 | | |
| | 560 | 359 | | targetVoxel.NotifyObstaclesCleared(eventInfo); |
| | | 360 | | |
| | 560 | 361 | | targetVoxel.CachedGridVersion = gridVersion; |
| | 560 | 362 | | grid.World!.NotifyActiveGridChange(grid); |
| | 560 | 363 | | } |
| | | 364 | | |
| | | 365 | | #endregion |
| | | 366 | | } |