| | | 1 | | using FixedMathSharp; |
| | | 2 | | using GridForge.Grids; |
| | | 3 | | using GridForge.Spatial; |
| | | 4 | | using GridForge.Utility; |
| | | 5 | | using SwiftCollections; |
| | | 6 | | using System; |
| | | 7 | | |
| | | 8 | | namespace GridForge.Blockers; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Base class for all grid blockers that handles applying and removing obstacles. |
| | | 12 | | /// </summary> |
| | | 13 | | public abstract class Blocker : IBlocker |
| | | 14 | | { |
| | 135 | 15 | | private readonly object _gridWatcherLock = new(); |
| | | 16 | | private bool _isWatchingWorldEvents; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// The world this blocker is bound to. |
| | | 20 | | /// </summary> |
| | 1123 | 21 | | public GridWorld World { get; } |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Unique token representing this blockage instance. |
| | | 25 | | /// </summary> |
| | 986 | 26 | | public BoundsKey BlockageToken { get; protected set; } = default; |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Indicates whether the blocker is currently active. |
| | | 30 | | /// </summary> |
| | 297 | 31 | | public bool IsActive { get; protected set; } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// The cached minimum bounds of the blockage area. |
| | | 35 | | /// </summary> |
| | 445 | 36 | | public Vector3d CacheMin { get; protected set; } |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// The cached maximum bounds of the blockage area. |
| | | 40 | | /// </summary> |
| | 446 | 41 | | public Vector3d CacheMax { get; private set; } |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Tracks whether the blocker is currently blocking voxels. |
| | | 45 | | /// </summary> |
| | 601 | 46 | | public bool IsBlocking { get; protected set; } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Flags whether or not to hold onto a reference of the voxels this blocker covers. |
| | | 50 | | /// </summary> |
| | 953 | 51 | | public bool CacheCoveredVoxels { get; protected set; } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Stable voxel identifiers cached for safe blocker removal when <see cref="CacheCoveredVoxels"/> is true. |
| | | 55 | | /// </summary> |
| | | 56 | | protected SwiftList<WorldVoxelIndex>? _cachedCoveredVoxels; |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Grid indices currently covered by this blocker. |
| | | 60 | | /// </summary> |
| | 135 | 61 | | private readonly SwiftHashSet<ushort> _watchedGridIndices = new(); |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Event triggered when a blocker is applied. |
| | | 65 | | /// </summary> |
| | | 66 | | private static Action<BlockageEventInfo>? _onBlockageApplied; |
| | | 67 | | |
| | | 68 | | /// <inheritdoc cref="_onBlockageApplied"/> |
| | | 69 | | public static event Action<BlockageEventInfo> OnBlockageApplied |
| | | 70 | | { |
| | 2 | 71 | | add => _onBlockageApplied += value; |
| | 2 | 72 | | remove => _onBlockageApplied -= value; |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// Event triggered when a blocker is removed. |
| | | 77 | | /// </summary> |
| | | 78 | | private static Action<BlockageEventInfo>? _onBlockageRemoved; |
| | | 79 | | |
| | | 80 | | /// <inheritdoc cref="_onBlockageRemoved"/> |
| | | 81 | | public static event Action<BlockageEventInfo> OnBlockageRemoved |
| | | 82 | | { |
| | 2 | 83 | | add => _onBlockageRemoved += value; |
| | 2 | 84 | | remove => _onBlockageRemoved -= value; |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Initializes a new blocker instance bound to the supplied world. |
| | | 89 | | /// </summary> |
| | | 90 | | /// <param name="world">The world whose grids this blocker should affect.</param> |
| | | 91 | | /// <param name="active">Flag whether or not this blocker will block on update.</param> |
| | | 92 | | /// <param name="cacheCoveredVoxels">Flag whether or not to cache covered voxels that are blocked.</param> |
| | 135 | 93 | | protected Blocker(GridWorld world, bool active = true, bool cacheCoveredVoxels = false) |
| | | 94 | | { |
| | 135 | 95 | | World = world ?? throw new ArgumentNullException(nameof(world)); |
| | 135 | 96 | | IsActive = active; |
| | 135 | 97 | | CacheCoveredVoxels = cacheCoveredVoxels; |
| | 135 | 98 | | } |
| | | 99 | | |
| | | 100 | | /// <summary> |
| | | 101 | | /// Toggles the blocker from inactive to active or active to inactive state |
| | | 102 | | /// If object is currently blocking, the blocker will be removed. |
| | | 103 | | /// If object is not active and not blocking, the blocker will be applied. |
| | | 104 | | /// </summary> |
| | | 105 | | public virtual void ToggleStatus(bool status) |
| | | 106 | | { |
| | 2 | 107 | | if (!status) |
| | | 108 | | { |
| | 1 | 109 | | RemoveBlockageCore(keepWatching: false); |
| | 1 | 110 | | IsActive = false; |
| | 1 | 111 | | return; |
| | | 112 | | } |
| | | 113 | | |
| | 1 | 114 | | if (!IsBlocking) |
| | | 115 | | { |
| | 1 | 116 | | IsActive = true; |
| | 1 | 117 | | ApplyBlockage(); |
| | | 118 | | } |
| | 1 | 119 | | } |
| | | 120 | | |
| | | 121 | | /// <summary> |
| | | 122 | | /// Applies the blockage by marking voxels as obstacles. |
| | | 123 | | /// </summary> |
| | | 124 | | public virtual void ApplyBlockage() |
| | | 125 | | { |
| | 140 | 126 | | if (!IsActive || IsBlocking || !World.IsActive) |
| | 2 | 127 | | return; |
| | | 128 | | |
| | 138 | 129 | | CacheMin = GetBoundsMin(); |
| | 138 | 130 | | CacheMax = GetBoundsMax(); |
| | 138 | 131 | | BlockageToken = new BoundsKey(CacheMin, CacheMax); |
| | | 132 | | |
| | 138 | 133 | | if (CacheCoveredVoxels) |
| | 10 | 134 | | _cachedCoveredVoxels ??= new SwiftList<WorldVoxelIndex>(); |
| | | 135 | | |
| | 138 | 136 | | RegisterGridWatcher(); |
| | 138 | 137 | | _cachedCoveredVoxels?.Clear(); |
| | 138 | 138 | | _watchedGridIndices.Clear(); |
| | | 139 | | |
| | 138 | 140 | | bool hasCoverage = true; |
| | 138 | 141 | | bool foundCoverage = false; |
| | 552 | 142 | | foreach (GridVoxelSet covered in GridTracer.GetCoveredVoxels(World, CacheMin, CacheMax)) |
| | | 143 | | { |
| | 138 | 144 | | if (covered.Voxels.Count <= 0) |
| | | 145 | | continue; |
| | | 146 | | |
| | 137 | 147 | | foundCoverage = true; |
| | 137 | 148 | | _watchedGridIndices.Add(covered.Grid.GridIndex); |
| | | 149 | | |
| | 1594 | 150 | | foreach (Voxel voxel in covered.Voxels) |
| | | 151 | | { |
| | 660 | 152 | | if (!covered.Grid.TryAddObstacle(voxel, BlockageToken)) |
| | | 153 | | { |
| | 1 | 154 | | hasCoverage = false; |
| | 1 | 155 | | continue; |
| | | 156 | | } |
| | | 157 | | |
| | 659 | 158 | | if (CacheCoveredVoxels) |
| | 16 | 159 | | _cachedCoveredVoxels!.Add(voxel.WorldIndex); |
| | | 160 | | } |
| | | 161 | | } |
| | | 162 | | |
| | 138 | 163 | | IsBlocking = foundCoverage && hasCoverage; |
| | | 164 | | |
| | 138 | 165 | | if (IsBlocking) |
| | | 166 | | { |
| | 134 | 167 | | NotifyBlockageApplied(); |
| | 134 | 168 | | return; |
| | | 169 | | } |
| | | 170 | | |
| | 4 | 171 | | if (!foundCoverage) |
| | 3 | 172 | | BlockageToken = default; |
| | 4 | 173 | | } |
| | | 174 | | |
| | | 175 | | /// <summary> |
| | | 176 | | /// Removes the blockage by clearing obstacle markers from voxels. |
| | | 177 | | /// </summary> |
| | | 178 | | public virtual void RemoveBlockage() |
| | | 179 | | { |
| | 8 | 180 | | RemoveBlockageCore(keepWatching: false); |
| | 8 | 181 | | } |
| | | 182 | | |
| | | 183 | | private void RemoveBlockageCore(bool keepWatching) |
| | | 184 | | { |
| | 14 | 185 | | if (!IsBlocking) |
| | | 186 | | { |
| | 3 | 187 | | _cachedCoveredVoxels?.Clear(); |
| | 3 | 188 | | _watchedGridIndices.Clear(); |
| | 3 | 189 | | if (!keepWatching || !IsActive) |
| | 1 | 190 | | UnregisterGridWatcher(); |
| | 3 | 191 | | return; |
| | | 192 | | } |
| | | 193 | | |
| | 11 | 194 | | BlockageEventInfo removalEventInfo = CreateBlockageEventInfo(); |
| | | 195 | | |
| | 11 | 196 | | if (CacheCoveredVoxels && _cachedCoveredVoxels?.Count > 0) |
| | | 197 | | { |
| | 24 | 198 | | foreach (WorldVoxelIndex voxelIndex in _cachedCoveredVoxels) |
| | 10 | 199 | | GridObstacleManager.TryRemoveObstacle(World, voxelIndex, BlockageToken); |
| | | 200 | | } |
| | | 201 | | else |
| | | 202 | | { |
| | 38 | 203 | | foreach (GridVoxelSet covered in GridTracer.GetCoveredVoxels(World, CacheMin, CacheMax)) |
| | | 204 | | { |
| | 68 | 205 | | foreach (Voxel voxel in covered.Voxels) |
| | 24 | 206 | | covered.Grid.TryRemoveObstacle(voxel, BlockageToken); |
| | | 207 | | } |
| | | 208 | | } |
| | | 209 | | |
| | 11 | 210 | | BlockageToken = default; |
| | 11 | 211 | | IsBlocking = false; |
| | 11 | 212 | | _cachedCoveredVoxels?.Clear(); |
| | 11 | 213 | | _watchedGridIndices.Clear(); |
| | | 214 | | |
| | 11 | 215 | | if (!keepWatching || !IsActive) |
| | 9 | 216 | | UnregisterGridWatcher(); |
| | | 217 | | |
| | 11 | 218 | | NotifyBlockageRemoved(removalEventInfo); |
| | 11 | 219 | | } |
| | | 220 | | |
| | | 221 | | /// <summary> |
| | | 222 | | /// Creates a snapshot describing the current blocker coverage. |
| | | 223 | | /// </summary> |
| | | 224 | | protected BlockageEventInfo CreateBlockageEventInfo() |
| | | 225 | | { |
| | 12 | 226 | | return new BlockageEventInfo(World.SpawnToken, BlockageToken, CacheMin, CacheMax); |
| | | 227 | | } |
| | | 228 | | |
| | | 229 | | /// <summary> |
| | | 230 | | /// Notifies subscribers that blockage has been applied. |
| | | 231 | | /// </summary> |
| | | 232 | | protected virtual void NotifyBlockageApplied() |
| | | 233 | | { |
| | 134 | 234 | | Action<BlockageEventInfo>? handlers = _onBlockageApplied; |
| | 134 | 235 | | if (handlers == null) |
| | 133 | 236 | | return; |
| | | 237 | | |
| | 1 | 238 | | BlockageEventInfo eventInfo = CreateBlockageEventInfo(); |
| | | 239 | | |
| | 1 | 240 | | var handlerDelegates = handlers.GetInvocationList(); |
| | 6 | 241 | | for (int i = 0; i < handlerDelegates.Length; i++) |
| | | 242 | | { |
| | | 243 | | try |
| | | 244 | | { |
| | 2 | 245 | | ((Action<BlockageEventInfo>)handlerDelegates[i])(eventInfo); |
| | 1 | 246 | | } |
| | 1 | 247 | | catch (Exception ex) |
| | | 248 | | { |
| | 1 | 249 | | GridForgeLogger.Channel.Error( |
| | 1 | 250 | | $"Blockage apply notification: {ex.Message} | Bounds: {eventInfo.BoundsMin} -> {eventInfo.BoundsMax} |
| | 1 | 251 | | } |
| | | 252 | | } |
| | 1 | 253 | | } |
| | | 254 | | |
| | | 255 | | /// <summary> |
| | | 256 | | /// Notifies subscribers that blockage has been removed. |
| | | 257 | | /// </summary> |
| | | 258 | | protected virtual void NotifyBlockageRemoved(BlockageEventInfo eventInfo) |
| | | 259 | | { |
| | 11 | 260 | | Action<BlockageEventInfo>? handlers = _onBlockageRemoved; |
| | 11 | 261 | | if (handlers == null) |
| | 10 | 262 | | return; |
| | | 263 | | |
| | 1 | 264 | | var handlerDelegates = handlers.GetInvocationList(); |
| | 6 | 265 | | for (int i = 0; i < handlerDelegates.Length; i++) |
| | | 266 | | { |
| | | 267 | | try |
| | | 268 | | { |
| | 2 | 269 | | ((Action<BlockageEventInfo>)handlerDelegates[i])(eventInfo); |
| | 1 | 270 | | } |
| | 1 | 271 | | catch (Exception ex) |
| | | 272 | | { |
| | 1 | 273 | | GridForgeLogger.Channel.Error( |
| | 1 | 274 | | $"Blockage remove notification: {ex.Message} | Bounds: {eventInfo.BoundsMin} -> {eventInfo.BoundsMax |
| | 1 | 275 | | } |
| | | 276 | | } |
| | 1 | 277 | | } |
| | | 278 | | |
| | | 279 | | /// <summary> |
| | | 280 | | /// Gets the min bounds of the area to block. Must be implemented by subclasses. |
| | | 281 | | /// </summary> |
| | | 282 | | protected abstract Vector3d GetBoundsMin(); |
| | | 283 | | |
| | | 284 | | /// <summary> |
| | | 285 | | /// Gets the max bounds of the area to block. Must be implemented by subclasses. |
| | | 286 | | /// </summary> |
| | | 287 | | protected abstract Vector3d GetBoundsMax(); |
| | | 288 | | |
| | | 289 | | /// <summary> |
| | | 290 | | /// Sets whether or not to cache covered voxels for this blocker. |
| | | 291 | | /// If enabled, the blocker will store references to the voxels it covers when applying blockage, |
| | | 292 | | /// which can improve performance when removing blockage at the cost of increased memory usage. |
| | | 293 | | /// </summary> |
| | | 294 | | public virtual void SetCacheCoveredVoxels(bool cache) |
| | | 295 | | { |
| | 4 | 296 | | if (CacheCoveredVoxels == cache) |
| | 1 | 297 | | return; |
| | | 298 | | |
| | 3 | 299 | | CacheCoveredVoxels = cache; |
| | 3 | 300 | | if (cache && _cachedCoveredVoxels == null) |
| | 2 | 301 | | _cachedCoveredVoxels = new SwiftList<WorldVoxelIndex>(); |
| | 1 | 302 | | else if (!cache) |
| | 1 | 303 | | _cachedCoveredVoxels = null; |
| | 1 | 304 | | } |
| | | 305 | | |
| | | 306 | | /// <summary> |
| | | 307 | | /// Resets the blocker to its default state, removing any active blockage and clearing cached data. |
| | | 308 | | /// </summary> |
| | | 309 | | public virtual void Reset() |
| | | 310 | | { |
| | 1 | 311 | | RemoveBlockageCore(keepWatching: false); |
| | | 312 | | |
| | 1 | 313 | | CacheCoveredVoxels = false; |
| | 1 | 314 | | _cachedCoveredVoxels = null; |
| | 1 | 315 | | _watchedGridIndices.Clear(); |
| | | 316 | | |
| | 1 | 317 | | IsActive = false; |
| | 1 | 318 | | CacheMin = Vector3d.Zero; |
| | 1 | 319 | | CacheMax = Vector3d.Zero; |
| | 1 | 320 | | BlockageToken = default; |
| | 1 | 321 | | } |
| | | 322 | | |
| | | 323 | | private void ReapplyBlockage() |
| | | 324 | | { |
| | 5 | 325 | | if (!IsActive) |
| | 1 | 326 | | return; |
| | | 327 | | |
| | 4 | 328 | | RemoveBlockageCore(keepWatching: true); |
| | 4 | 329 | | ApplyBlockage(); |
| | 4 | 330 | | } |
| | | 331 | | |
| | | 332 | | private void RegisterGridWatcher() |
| | | 333 | | { |
| | 139 | 334 | | lock (_gridWatcherLock) |
| | | 335 | | { |
| | 139 | 336 | | if (_isWatchingWorldEvents) |
| | 4 | 337 | | return; |
| | | 338 | | |
| | 135 | 339 | | World.OnActiveGridAdded += HandleActiveGridAdded; |
| | 135 | 340 | | World.OnActiveGridRemoved += HandleActiveGridRemoved; |
| | 135 | 341 | | World.OnReset += HandleWorldReset; |
| | 135 | 342 | | _isWatchingWorldEvents = true; |
| | 135 | 343 | | } |
| | 139 | 344 | | } |
| | | 345 | | |
| | | 346 | | private void UnregisterGridWatcher() |
| | | 347 | | { |
| | 135 | 348 | | lock (_gridWatcherLock) |
| | | 349 | | { |
| | 135 | 350 | | if (!_isWatchingWorldEvents) |
| | 0 | 351 | | return; |
| | | 352 | | |
| | 135 | 353 | | World.OnActiveGridAdded -= HandleActiveGridAdded; |
| | 135 | 354 | | World.OnActiveGridRemoved -= HandleActiveGridRemoved; |
| | 135 | 355 | | World.OnReset -= HandleWorldReset; |
| | 135 | 356 | | _isWatchingWorldEvents = false; |
| | 135 | 357 | | } |
| | 135 | 358 | | } |
| | | 359 | | |
| | | 360 | | private bool ShouldReactToGridAdded(GridEventInfo eventInfo) |
| | | 361 | | { |
| | 5 | 362 | | if (!IsActive) |
| | 1 | 363 | | return false; |
| | | 364 | | |
| | 4 | 365 | | return CacheMax.x >= eventInfo.BoundsMin.x |
| | 4 | 366 | | && CacheMin.x <= eventInfo.BoundsMax.x |
| | 4 | 367 | | && CacheMax.y >= eventInfo.BoundsMin.y |
| | 4 | 368 | | && CacheMin.y <= eventInfo.BoundsMax.y |
| | 4 | 369 | | && CacheMax.z >= eventInfo.BoundsMin.z |
| | 4 | 370 | | && CacheMin.z <= eventInfo.BoundsMax.z; |
| | | 371 | | } |
| | | 372 | | |
| | | 373 | | private bool ShouldReactToGridRemoved(GridEventInfo eventInfo) |
| | | 374 | | { |
| | 3 | 375 | | return IsActive && _watchedGridIndices.Contains(eventInfo.GridIndex); |
| | | 376 | | } |
| | | 377 | | |
| | | 378 | | private void HandleActiveGridAdded(GridEventInfo eventInfo) |
| | | 379 | | { |
| | 4 | 380 | | if (eventInfo.WorldSpawnToken != World.SpawnToken || !ShouldReactToGridAdded(eventInfo)) |
| | 1 | 381 | | return; |
| | | 382 | | |
| | 3 | 383 | | ReapplyBlockage(); |
| | 3 | 384 | | } |
| | | 385 | | |
| | | 386 | | private void HandleActiveGridRemoved(GridEventInfo eventInfo) |
| | | 387 | | { |
| | 2 | 388 | | if (eventInfo.WorldSpawnToken != World.SpawnToken || !ShouldReactToGridRemoved(eventInfo)) |
| | 1 | 389 | | return; |
| | | 390 | | |
| | 1 | 391 | | ReapplyBlockage(); |
| | 1 | 392 | | } |
| | | 393 | | |
| | | 394 | | private void HandleWorldReset() |
| | | 395 | | { |
| | 125 | 396 | | IsBlocking = false; |
| | 125 | 397 | | BlockageToken = default; |
| | 125 | 398 | | _cachedCoveredVoxels?.Clear(); |
| | 125 | 399 | | _watchedGridIndices.Clear(); |
| | 125 | 400 | | UnregisterGridWatcher(); |
| | 125 | 401 | | } |
| | | 402 | | } |