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