| | | 1 | | //======================================================================= |
| | | 2 | | // Blocker.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.Grids; |
| | | 11 | | using GridForge.Spatial; |
| | | 12 | | using GridForge.Utility; |
| | | 13 | | using SwiftCollections; |
| | | 14 | | using SwiftCollections.Pool; |
| | | 15 | | |
| | | 16 | | namespace GridForge.Blockers; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Base class for all grid blockers that handles applying and removing obstacles. |
| | | 20 | | /// </summary> |
| | | 21 | | public abstract class Blocker : IBlocker |
| | | 22 | | { |
| | 153 | 23 | | private readonly object _gridWatcherLock = new(); |
| | | 24 | | private bool _isWatchingWorldEvents; |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// The world this blocker is bound to. |
| | | 28 | | /// </summary> |
| | | 29 | | public GridWorld World { get; } |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Unique token representing this blockage instance. |
| | | 33 | | /// </summary> |
| | | 34 | | public ObstacleToken BlockageToken { get; private set; } |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Indicates whether the blocker is currently active. |
| | | 38 | | /// </summary> |
| | | 39 | | public bool IsActive { get; protected set; } |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// The cached minimum bounds of the blockage area. |
| | | 43 | | /// </summary> |
| | | 44 | | public Vector3d CacheMin { get; protected set; } |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// The cached maximum bounds of the blockage area. |
| | | 48 | | /// </summary> |
| | | 49 | | public Vector3d CacheMax { get; private set; } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Tracks whether the blocker is currently blocking voxels. |
| | | 53 | | /// </summary> |
| | | 54 | | public bool IsBlocking { get; protected set; } |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Flags whether or not to hold onto a reference of the voxels this blocker covers. |
| | | 58 | | /// </summary> |
| | | 59 | | public bool CacheCoveredVoxels { get; protected set; } |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Stable voxel identifiers cached for safe blocker removal when <see cref="CacheCoveredVoxels"/> is true. |
| | | 63 | | /// </summary> |
| | | 64 | | protected SwiftList<WorldVoxelIndex>? _cachedCoveredVoxels; |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Grid indices currently covered by this blocker. |
| | | 68 | | /// </summary> |
| | 153 | 69 | | private readonly SwiftHashSet<ushort> _watchedGridIndices = new(); |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Event triggered when a blocker is applied. |
| | | 73 | | /// </summary> |
| | | 74 | | private static Action<BlockageEventInfo>? _onBlockageApplied; |
| | | 75 | | |
| | | 76 | | /// <inheritdoc cref="_onBlockageApplied"/> |
| | | 77 | | public static event Action<BlockageEventInfo> OnBlockageApplied |
| | | 78 | | { |
| | 3 | 79 | | add => _onBlockageApplied += value; |
| | 3 | 80 | | remove => _onBlockageApplied -= value; |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Event triggered when a blocker is removed. |
| | | 85 | | /// </summary> |
| | | 86 | | private static Action<BlockageEventInfo>? _onBlockageRemoved; |
| | | 87 | | |
| | | 88 | | /// <inheritdoc cref="_onBlockageRemoved"/> |
| | | 89 | | public static event Action<BlockageEventInfo> OnBlockageRemoved |
| | | 90 | | { |
| | 2 | 91 | | add => _onBlockageRemoved += value; |
| | 2 | 92 | | remove => _onBlockageRemoved -= value; |
| | | 93 | | } |
| | | 94 | | |
| | | 95 | | /// <summary> |
| | | 96 | | /// Initializes a new blocker instance bound to the supplied world. |
| | | 97 | | /// </summary> |
| | | 98 | | /// <param name="world">The world whose grids this blocker should affect.</param> |
| | | 99 | | /// <param name="active">Flag whether or not this blocker will block on update.</param> |
| | | 100 | | /// <param name="cacheCoveredVoxels">Flag whether or not to cache covered voxels that are blocked.</param> |
| | 153 | 101 | | protected Blocker(GridWorld world, bool active = true, bool cacheCoveredVoxels = false) |
| | | 102 | | { |
| | 153 | 103 | | SwiftThrowHelper.ThrowIfNull(world, nameof(world)); |
| | 153 | 104 | | World = world; |
| | 153 | 105 | | IsActive = active; |
| | 153 | 106 | | CacheCoveredVoxels = cacheCoveredVoxels; |
| | 153 | 107 | | } |
| | | 108 | | |
| | | 109 | | /// <summary> |
| | | 110 | | /// Toggles the blocker from inactive to active or active to inactive state |
| | | 111 | | /// If object is currently blocking, the blocker will be removed. |
| | | 112 | | /// If object is not active and not blocking, the blocker will be applied. |
| | | 113 | | /// </summary> |
| | | 114 | | public virtual void ToggleStatus(bool status) |
| | | 115 | | { |
| | 3 | 116 | | if (!status) |
| | | 117 | | { |
| | 1 | 118 | | RemoveBlockageCore(keepWatching: false, preserveToken: false); |
| | 1 | 119 | | IsActive = false; |
| | 1 | 120 | | return; |
| | | 121 | | } |
| | | 122 | | |
| | 2 | 123 | | if (!IsBlocking) |
| | | 124 | | { |
| | 1 | 125 | | IsActive = true; |
| | 1 | 126 | | ApplyBlockage(); |
| | | 127 | | } |
| | 2 | 128 | | } |
| | | 129 | | |
| | | 130 | | /// <summary> |
| | | 131 | | /// Applies the blockage by marking voxels as obstacles. |
| | | 132 | | /// </summary> |
| | | 133 | | public virtual void ApplyBlockage() |
| | | 134 | | { |
| | 155 | 135 | | ApplyBlockageCore(preserveTokenWhenEmpty: false); |
| | 155 | 136 | | } |
| | | 137 | | |
| | | 138 | | private void ApplyBlockageCore(bool preserveTokenWhenEmpty) |
| | | 139 | | { |
| | 165 | 140 | | if (!IsActive || IsBlocking || !World.IsActive) |
| | 3 | 141 | | return; |
| | | 142 | | |
| | 162 | 143 | | PrepareBlockageApplication(); |
| | 162 | 144 | | ObstacleToken blockageToken = BlockageToken; |
| | 162 | 145 | | bool foundCoverage = ApplyBlockageToCoveredVoxels(blockageToken, out bool hasCoverage); |
| | | 146 | | |
| | 162 | 147 | | IsBlocking = foundCoverage && hasCoverage; |
| | | 148 | | |
| | 162 | 149 | | if (IsBlocking) |
| | | 150 | | { |
| | 154 | 151 | | NotifyBlockageApplied(); |
| | 154 | 152 | | return; |
| | | 153 | | } |
| | | 154 | | |
| | 8 | 155 | | if (!preserveTokenWhenEmpty || !hasCoverage) |
| | 5 | 156 | | BlockageToken = default; |
| | 8 | 157 | | } |
| | | 158 | | |
| | | 159 | | /// <summary> |
| | | 160 | | /// Removes the blockage by clearing obstacle markers from voxels. |
| | | 161 | | /// </summary> |
| | | 162 | | public virtual void RemoveBlockage() |
| | | 163 | | { |
| | 27 | 164 | | RemoveBlockageCore(keepWatching: false, preserveToken: false); |
| | 27 | 165 | | } |
| | | 166 | | |
| | | 167 | | private void RemoveBlockageCore(bool keepWatching, bool preserveToken) |
| | | 168 | | { |
| | 39 | 169 | | if (!IsBlocking) |
| | | 170 | | { |
| | 7 | 171 | | if (!preserveToken) |
| | 2 | 172 | | BlockageToken = default; |
| | | 173 | | |
| | 7 | 174 | | ClearCoverageTracking(); |
| | 7 | 175 | | UnregisterGridWatcherIfNeeded(keepWatching); |
| | 7 | 176 | | return; |
| | | 177 | | } |
| | | 178 | | |
| | 32 | 179 | | ObstacleToken blockageToken = BlockageToken; |
| | 32 | 180 | | BlockageEventInfo removalEventInfo = CreateBlockageEventInfo(blockageToken); |
| | | 181 | | |
| | 32 | 182 | | RemoveAppliedBlockage(blockageToken); |
| | | 183 | | |
| | 32 | 184 | | if (!preserveToken) |
| | 27 | 185 | | BlockageToken = default; |
| | 32 | 186 | | IsBlocking = false; |
| | 32 | 187 | | ClearCoverageTracking(); |
| | 32 | 188 | | UnregisterGridWatcherIfNeeded(keepWatching); |
| | | 189 | | |
| | 32 | 190 | | NotifyBlockageRemoved(removalEventInfo); |
| | 32 | 191 | | } |
| | | 192 | | |
| | | 193 | | private void PrepareBlockageApplication() |
| | | 194 | | { |
| | 162 | 195 | | CacheMin = GetBoundsMin(); |
| | 162 | 196 | | CacheMax = GetBoundsMax(); |
| | 162 | 197 | | if (!BlockageToken.IsValid) |
| | 154 | 198 | | BlockageToken = World.AllocateObstacleToken(); |
| | | 199 | | |
| | 162 | 200 | | if (CacheCoveredVoxels) |
| | 29 | 201 | | _cachedCoveredVoxels ??= new SwiftList<WorldVoxelIndex>(); |
| | | 202 | | |
| | 162 | 203 | | RegisterGridWatcher(); |
| | 162 | 204 | | ClearCoverageTracking(); |
| | 162 | 205 | | } |
| | | 206 | | |
| | | 207 | | private bool ApplyBlockageToCoveredVoxels(ObstacleToken blockageToken, out bool hasCoverage) |
| | | 208 | | { |
| | 162 | 209 | | hasCoverage = true; |
| | 162 | 210 | | bool foundCoverage = false; |
| | 162 | 211 | | SwiftList<WorldVoxelIndex>? appliedVoxels = CacheCoveredVoxels |
| | 162 | 212 | | ? _cachedCoveredVoxels |
| | 162 | 213 | | : SwiftListPool<WorldVoxelIndex>.Shared.Rent(); |
| | | 214 | | |
| | | 215 | | try |
| | | 216 | | { |
| | 642 | 217 | | foreach (GridVoxelSet covered in GridTracer.GetCoveredVoxels(World, CacheMin, CacheMax)) |
| | | 218 | | { |
| | 159 | 219 | | foundCoverage = true; |
| | 159 | 220 | | _watchedGridIndices.Add(covered.Grid.GridIndex); |
| | 159 | 221 | | ApplyBlockageToVoxels(covered, appliedVoxels!, blockageToken, ref hasCoverage); |
| | | 222 | | } |
| | | 223 | | |
| | 162 | 224 | | if (!hasCoverage) |
| | 2 | 225 | | RollbackAppliedBlockage(appliedVoxels!, blockageToken); |
| | | 226 | | |
| | 162 | 227 | | return foundCoverage; |
| | | 228 | | } |
| | | 229 | | finally |
| | | 230 | | { |
| | 162 | 231 | | if (!CacheCoveredVoxels) |
| | 133 | 232 | | SwiftListPool<WorldVoxelIndex>.Shared.Release(appliedVoxels!); |
| | 162 | 233 | | } |
| | 162 | 234 | | } |
| | | 235 | | |
| | | 236 | | private void ApplyBlockageToVoxels( |
| | | 237 | | GridVoxelSet covered, |
| | | 238 | | SwiftList<WorldVoxelIndex> appliedVoxels, |
| | | 239 | | ObstacleToken blockageToken, |
| | | 240 | | ref bool hasCoverage) |
| | | 241 | | { |
| | 1776 | 242 | | foreach (Voxel voxel in covered.Voxels) |
| | | 243 | | { |
| | 729 | 244 | | if (!covered.Grid.TryAddObstacle(voxel, blockageToken)) |
| | | 245 | | { |
| | 2 | 246 | | hasCoverage = false; |
| | 2 | 247 | | continue; |
| | | 248 | | } |
| | | 249 | | |
| | 727 | 250 | | appliedVoxels.Add(voxel.WorldIndex); |
| | | 251 | | } |
| | 159 | 252 | | } |
| | | 253 | | |
| | | 254 | | private void RollbackAppliedBlockage( |
| | | 255 | | SwiftList<WorldVoxelIndex> appliedVoxels, |
| | | 256 | | ObstacleToken blockageToken) |
| | | 257 | | { |
| | 6 | 258 | | foreach (WorldVoxelIndex voxelIndex in appliedVoxels) |
| | 1 | 259 | | GridObstacleManager.TryRemoveObstacle(World, voxelIndex, blockageToken); |
| | | 260 | | |
| | 2 | 261 | | appliedVoxels.Clear(); |
| | 2 | 262 | | } |
| | | 263 | | |
| | | 264 | | private void RemoveAppliedBlockage(ObstacleToken blockageToken) |
| | | 265 | | { |
| | 32 | 266 | | if (CacheCoveredVoxels && _cachedCoveredVoxels?.Count > 0) |
| | | 267 | | { |
| | 17 | 268 | | RemoveCachedBlockage(blockageToken); |
| | 17 | 269 | | return; |
| | | 270 | | } |
| | | 271 | | |
| | 15 | 272 | | RemoveTracedBlockage(blockageToken); |
| | 15 | 273 | | } |
| | | 274 | | |
| | | 275 | | private void RemoveCachedBlockage(ObstacleToken blockageToken) |
| | | 276 | | { |
| | 96 | 277 | | foreach (WorldVoxelIndex voxelIndex in _cachedCoveredVoxels!) |
| | 31 | 278 | | GridObstacleManager.TryRemoveObstacle(World, voxelIndex, blockageToken); |
| | 17 | 279 | | } |
| | | 280 | | |
| | | 281 | | private void RemoveTracedBlockage(ObstacleToken blockageToken) |
| | | 282 | | { |
| | 62 | 283 | | foreach (GridVoxelSet covered in GridTracer.GetCoveredVoxels(World, CacheMin, CacheMax)) |
| | | 284 | | { |
| | 124 | 285 | | foreach (Voxel voxel in covered.Voxels) |
| | 46 | 286 | | covered.Grid.TryRemoveObstacle(voxel, blockageToken); |
| | | 287 | | } |
| | 15 | 288 | | } |
| | | 289 | | |
| | | 290 | | private void ClearCoverageTracking() |
| | | 291 | | { |
| | 201 | 292 | | _cachedCoveredVoxels?.Clear(); |
| | 201 | 293 | | _watchedGridIndices.Clear(); |
| | 201 | 294 | | } |
| | | 295 | | |
| | | 296 | | private void UnregisterGridWatcherIfNeeded(bool keepWatching) |
| | | 297 | | { |
| | 39 | 298 | | if (!keepWatching || !IsActive) |
| | 29 | 299 | | UnregisterGridWatcher(); |
| | 39 | 300 | | } |
| | | 301 | | |
| | | 302 | | /// <summary> |
| | | 303 | | /// Creates a snapshot describing the current blocker coverage. |
| | | 304 | | /// </summary> |
| | | 305 | | protected BlockageEventInfo CreateBlockageEventInfo() |
| | | 306 | | { |
| | 2 | 307 | | return CreateBlockageEventInfo(BlockageToken); |
| | | 308 | | } |
| | | 309 | | |
| | | 310 | | private BlockageEventInfo CreateBlockageEventInfo(ObstacleToken blockageToken) |
| | | 311 | | { |
| | 34 | 312 | | return new BlockageEventInfo(World.SpawnToken, blockageToken, CacheMin, CacheMax); |
| | | 313 | | } |
| | | 314 | | |
| | | 315 | | /// <summary> |
| | | 316 | | /// Notifies subscribers that blockage has been applied. |
| | | 317 | | /// </summary> |
| | | 318 | | protected virtual void NotifyBlockageApplied() |
| | | 319 | | { |
| | 154 | 320 | | Action<BlockageEventInfo>? handlers = _onBlockageApplied; |
| | 154 | 321 | | if (handlers == null) |
| | 152 | 322 | | return; |
| | | 323 | | |
| | 2 | 324 | | BlockageEventInfo eventInfo = CreateBlockageEventInfo(); |
| | | 325 | | |
| | 2 | 326 | | var handlerDelegates = handlers.GetInvocationList(); |
| | 10 | 327 | | for (int i = 0; i < handlerDelegates.Length; i++) |
| | | 328 | | { |
| | | 329 | | try |
| | | 330 | | { |
| | 3 | 331 | | ((Action<BlockageEventInfo>)handlerDelegates[i])(eventInfo); |
| | 2 | 332 | | } |
| | 1 | 333 | | catch (Exception ex) |
| | | 334 | | { |
| | 1 | 335 | | GridForgeLogger.Channel.Error( |
| | 1 | 336 | | $"Blockage apply notification: {ex.Message} | Bounds: {eventInfo.BoundsMin} -> {eventInfo.BoundsMax} |
| | 1 | 337 | | } |
| | | 338 | | } |
| | 2 | 339 | | } |
| | | 340 | | |
| | | 341 | | /// <summary> |
| | | 342 | | /// Notifies subscribers that blockage has been removed. |
| | | 343 | | /// </summary> |
| | | 344 | | protected virtual void NotifyBlockageRemoved(BlockageEventInfo eventInfo) |
| | | 345 | | { |
| | 32 | 346 | | Action<BlockageEventInfo>? handlers = _onBlockageRemoved; |
| | 32 | 347 | | if (handlers == null) |
| | 31 | 348 | | return; |
| | | 349 | | |
| | 1 | 350 | | var handlerDelegates = handlers.GetInvocationList(); |
| | 6 | 351 | | for (int i = 0; i < handlerDelegates.Length; i++) |
| | | 352 | | { |
| | | 353 | | try |
| | | 354 | | { |
| | 2 | 355 | | ((Action<BlockageEventInfo>)handlerDelegates[i])(eventInfo); |
| | 1 | 356 | | } |
| | 1 | 357 | | catch (Exception ex) |
| | | 358 | | { |
| | 1 | 359 | | GridForgeLogger.Channel.Error( |
| | 1 | 360 | | $"Blockage remove notification: {ex.Message} | Bounds: {eventInfo.BoundsMin} -> {eventInfo.BoundsMax |
| | 1 | 361 | | } |
| | | 362 | | } |
| | 1 | 363 | | } |
| | | 364 | | |
| | | 365 | | /// <summary> |
| | | 366 | | /// Gets the min bounds of the area to block. Must be implemented by subclasses. |
| | | 367 | | /// </summary> |
| | | 368 | | protected abstract Vector3d GetBoundsMin(); |
| | | 369 | | |
| | | 370 | | /// <summary> |
| | | 371 | | /// Gets the max bounds of the area to block. Must be implemented by subclasses. |
| | | 372 | | /// </summary> |
| | | 373 | | protected abstract Vector3d GetBoundsMax(); |
| | | 374 | | |
| | | 375 | | /// <summary> |
| | | 376 | | /// Sets whether or not to cache covered voxels for this blocker. |
| | | 377 | | /// If enabled, the blocker will store references to the voxels it covers when applying blockage, |
| | | 378 | | /// which can improve performance when removing blockage at the cost of increased memory usage. |
| | | 379 | | /// </summary> |
| | | 380 | | public virtual void SetCacheCoveredVoxels(bool cache) |
| | | 381 | | { |
| | 5 | 382 | | if (CacheCoveredVoxels == cache) |
| | 2 | 383 | | return; |
| | | 384 | | |
| | 3 | 385 | | CacheCoveredVoxels = cache; |
| | 3 | 386 | | if (cache) |
| | | 387 | | { |
| | 2 | 388 | | _cachedCoveredVoxels = new SwiftList<WorldVoxelIndex>(); |
| | 2 | 389 | | return; |
| | | 390 | | } |
| | | 391 | | |
| | 1 | 392 | | _cachedCoveredVoxels = null; |
| | 1 | 393 | | } |
| | | 394 | | |
| | | 395 | | /// <summary> |
| | | 396 | | /// Resets the blocker to its default state, removing any active blockage and clearing cached data. |
| | | 397 | | /// </summary> |
| | | 398 | | public virtual void Reset() |
| | | 399 | | { |
| | 1 | 400 | | RemoveBlockageCore(keepWatching: false, preserveToken: false); |
| | | 401 | | |
| | 1 | 402 | | CacheCoveredVoxels = false; |
| | 1 | 403 | | _cachedCoveredVoxels = null; |
| | 1 | 404 | | _watchedGridIndices.Clear(); |
| | | 405 | | |
| | 1 | 406 | | IsActive = false; |
| | 1 | 407 | | CacheMin = Vector3d.Zero; |
| | 1 | 408 | | CacheMax = Vector3d.Zero; |
| | 1 | 409 | | BlockageToken = default; |
| | 1 | 410 | | } |
| | | 411 | | |
| | | 412 | | private void ReapplyBlockage() |
| | | 413 | | { |
| | 11 | 414 | | if (!IsActive) |
| | 1 | 415 | | return; |
| | | 416 | | |
| | 10 | 417 | | RemoveBlockageCore(keepWatching: true, preserveToken: true); |
| | 10 | 418 | | ApplyBlockageCore(preserveTokenWhenEmpty: true); |
| | 10 | 419 | | } |
| | | 420 | | |
| | | 421 | | private void RegisterGridWatcher() |
| | | 422 | | { |
| | 163 | 423 | | lock (_gridWatcherLock) |
| | | 424 | | { |
| | 163 | 425 | | if (_isWatchingWorldEvents) |
| | 10 | 426 | | return; |
| | | 427 | | |
| | 153 | 428 | | World.OnActiveGridAdded += HandleActiveGridAdded; |
| | 153 | 429 | | World.OnActiveGridRemoved += HandleActiveGridRemoved; |
| | 153 | 430 | | World.OnActiveGridChange += HandleActiveGridChanged; |
| | 153 | 431 | | World.OnReset += HandleWorldReset; |
| | 153 | 432 | | _isWatchingWorldEvents = true; |
| | 153 | 433 | | } |
| | 163 | 434 | | } |
| | | 435 | | |
| | | 436 | | private void UnregisterGridWatcher() |
| | | 437 | | { |
| | 154 | 438 | | lock (_gridWatcherLock) |
| | | 439 | | { |
| | 154 | 440 | | if (!_isWatchingWorldEvents) |
| | 1 | 441 | | return; |
| | | 442 | | |
| | 153 | 443 | | World.OnActiveGridAdded -= HandleActiveGridAdded; |
| | 153 | 444 | | World.OnActiveGridRemoved -= HandleActiveGridRemoved; |
| | 153 | 445 | | World.OnActiveGridChange -= HandleActiveGridChanged; |
| | 153 | 446 | | World.OnReset -= HandleWorldReset; |
| | 153 | 447 | | _isWatchingWorldEvents = false; |
| | 153 | 448 | | } |
| | 154 | 449 | | } |
| | | 450 | | |
| | | 451 | | private bool ShouldReactToGridAdded(GridEventInfo eventInfo) |
| | | 452 | | { |
| | 7 | 453 | | return IsActive && BoundsOverlap(CacheMin, CacheMax, eventInfo.BoundsMin, eventInfo.BoundsMax); |
| | | 454 | | } |
| | | 455 | | |
| | | 456 | | private static bool BoundsOverlap( |
| | | 457 | | Vector3d firstMin, |
| | | 458 | | Vector3d firstMax, |
| | | 459 | | Vector3d secondMin, |
| | | 460 | | Vector3d secondMax) |
| | | 461 | | { |
| | 8 | 462 | | return AxisOverlaps(firstMin.X, firstMax.X, secondMin.X, secondMax.X) |
| | 8 | 463 | | && AxisOverlaps(firstMin.Y, firstMax.Y, secondMin.Y, secondMax.Y) |
| | 8 | 464 | | && AxisOverlaps(firstMin.Z, firstMax.Z, secondMin.Z, secondMax.Z); |
| | | 465 | | } |
| | | 466 | | |
| | | 467 | | private static bool AxisOverlaps(Fixed64 firstMin, Fixed64 firstMax, Fixed64 secondMin, Fixed64 secondMax) |
| | | 468 | | { |
| | 22 | 469 | | return firstMax >= secondMin && firstMin <= secondMax; |
| | | 470 | | } |
| | | 471 | | |
| | | 472 | | private bool ShouldReactToGridRemoved(GridEventInfo eventInfo) |
| | | 473 | | { |
| | 5 | 474 | | return IsActive && _watchedGridIndices.Contains(eventInfo.GridIndex); |
| | | 475 | | } |
| | | 476 | | |
| | | 477 | | private bool ShouldReactToGridChanged(GridEventInfo eventInfo) |
| | | 478 | | { |
| | 21009 | 479 | | return IsActive |
| | 21009 | 480 | | && IsSparseVoxelMutation(eventInfo.ChangeKind) |
| | 21009 | 481 | | && BoundsOverlap(CacheMin, CacheMax, eventInfo.AffectedBoundsMin, eventInfo.AffectedBoundsMax); |
| | | 482 | | } |
| | | 483 | | |
| | | 484 | | private static bool IsSparseVoxelMutation(GridEventKind changeKind) => |
| | 21009 | 485 | | changeKind == GridEventKind.SparseVoxelAdded |
| | 21009 | 486 | | || changeKind == GridEventKind.SparseVoxelRemoved; |
| | | 487 | | |
| | | 488 | | private void HandleActiveGridAdded(GridEventInfo eventInfo) |
| | | 489 | | { |
| | 7 | 490 | | if (eventInfo.WorldSpawnToken != World.SpawnToken || !ShouldReactToGridAdded(eventInfo)) |
| | 2 | 491 | | return; |
| | | 492 | | |
| | 5 | 493 | | ReapplyBlockage(); |
| | 5 | 494 | | } |
| | | 495 | | |
| | | 496 | | private void HandleActiveGridRemoved(GridEventInfo eventInfo) |
| | | 497 | | { |
| | 5 | 498 | | if (eventInfo.WorldSpawnToken != World.SpawnToken || !ShouldReactToGridRemoved(eventInfo)) |
| | 2 | 499 | | return; |
| | | 500 | | |
| | 3 | 501 | | ReapplyBlockage(); |
| | 3 | 502 | | } |
| | | 503 | | |
| | | 504 | | private void HandleActiveGridChanged(GridEventInfo eventInfo) |
| | | 505 | | { |
| | 21010 | 506 | | if (eventInfo.WorldSpawnToken != World.SpawnToken || !ShouldReactToGridChanged(eventInfo)) |
| | 21008 | 507 | | return; |
| | | 508 | | |
| | 2 | 509 | | ReapplyBlockage(); |
| | 2 | 510 | | } |
| | | 511 | | |
| | | 512 | | private void HandleWorldReset() |
| | | 513 | | { |
| | 125 | 514 | | IsBlocking = false; |
| | 125 | 515 | | BlockageToken = default; |
| | 125 | 516 | | _cachedCoveredVoxels?.Clear(); |
| | 125 | 517 | | _watchedGridIndices.Clear(); |
| | 125 | 518 | | UnregisterGridWatcher(); |
| | 125 | 519 | | } |
| | | 520 | | } |