| | | 1 | | //======================================================================= |
| | | 2 | | // GridOccupantManager.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 GridForge.Spatial; |
| | | 9 | | using SwiftCollections; |
| | | 10 | | using SwiftCollections.Pool; |
| | | 11 | | using System; |
| | | 12 | | using System.Collections.Concurrent; |
| | | 13 | | using System.Collections.Generic; |
| | | 14 | | |
| | | 15 | | namespace GridForge.Grids; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Provides utility methods for managing voxel occupants within a grid. |
| | | 19 | | /// Supports adding, removing, and retrieving occupants with thread-safe operations. |
| | | 20 | | /// </summary> |
| | | 21 | | public static class GridOccupantManager |
| | | 22 | | { |
| | | 23 | | #region Constants & Events |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Maximum number of occupants allowed per voxel. |
| | | 27 | | /// </summary> |
| | | 28 | | public const byte MaxOccupantCount = byte.MaxValue; |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Event triggered when an occupant is added. |
| | | 32 | | /// </summary> |
| | | 33 | | private static Action<OccupantEventInfo>? _onOccupantAdded; |
| | | 34 | | |
| | | 35 | | /// <inheritdoc cref="_onOccupantAdded"/> |
| | | 36 | | public static event Action<OccupantEventInfo> OnOccupantAdded |
| | | 37 | | { |
| | 10 | 38 | | add => _onOccupantAdded += value; |
| | 10 | 39 | | remove => _onOccupantAdded -= value; |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Event triggered when an occupant is removed. |
| | | 44 | | /// </summary> |
| | | 45 | | private static Action<OccupantEventInfo>? _onOccupantRemoved; |
| | | 46 | | |
| | | 47 | | /// <inheritdoc cref="_onOccupantRemoved"/> |
| | | 48 | | public static event Action<OccupantEventInfo> OnOccupantRemoved |
| | | 49 | | { |
| | 10 | 50 | | add => _onOccupantRemoved += value; |
| | 10 | 51 | | remove => _onOccupantRemoved -= value; |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | #endregion |
| | | 55 | | |
| | | 56 | | #region Private Fields |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Tracks occupant registrations independently for each world. |
| | | 60 | | /// </summary> |
| | 1 | 61 | | private static readonly ConcurrentDictionary<GridWorld, WorldOccupancyRegistry> _occupancyRegistries = new(); |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Tracks all voxel registrations for a single occupant. |
| | | 65 | | /// </summary> |
| | | 66 | | private sealed class OccupancyRecord |
| | | 67 | | { |
| | | 68 | | public readonly IVoxelOccupant Occupant; |
| | 403 | 69 | | public readonly SwiftDictionary<WorldVoxelIndex, int> Tickets = new(); |
| | | 70 | | |
| | 403 | 71 | | public OccupancyRecord(IVoxelOccupant occupant) |
| | | 72 | | { |
| | 403 | 73 | | Occupant = occupant; |
| | 403 | 74 | | } |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Synchronizes one world's tracked occupant registrations. |
| | | 79 | | /// </summary> |
| | | 80 | | private sealed class WorldOccupancyRegistry |
| | | 81 | | { |
| | 51 | 82 | | public readonly object SyncRoot = new(); |
| | 51 | 83 | | public readonly SwiftDictionary<Guid, OccupancyRecord> Records = new(); |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | /// <summary> |
| | | 87 | | /// Immutable snapshot of one tracked occupancy. |
| | | 88 | | /// </summary> |
| | | 89 | | private readonly struct TrackedOccupancy |
| | | 90 | | { |
| | | 91 | | public readonly WorldVoxelIndex VoxelIndex; |
| | | 92 | | public readonly int Ticket; |
| | | 93 | | |
| | | 94 | | public TrackedOccupancy(WorldVoxelIndex voxelIndex, int ticket) |
| | | 95 | | { |
| | 166 | 96 | | VoxelIndex = voxelIndex; |
| | 166 | 97 | | Ticket = ticket; |
| | 166 | 98 | | } |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | #endregion |
| | | 102 | | |
| | | 103 | | #region Occupant Management |
| | | 104 | | |
| | | 105 | | /// <summary> |
| | | 106 | | /// Attempts to register the occupant with the current voxel it is on in the supplied world. |
| | | 107 | | /// </summary> |
| | | 108 | | public static bool TryRegister(GridWorld world, IVoxelOccupant occupant) |
| | | 109 | | { |
| | 12 | 110 | | return occupant != null |
| | 12 | 111 | | && world != null |
| | 12 | 112 | | && world.TryGetGridAndVoxel(occupant.Position, out VoxelGrid? grid, out Voxel? voxel) |
| | 12 | 113 | | && grid!.TryAddVoxelOccupant(voxel!, occupant); |
| | | 114 | | } |
| | | 115 | | |
| | | 116 | | /// <summary> |
| | | 117 | | /// Returns a snapshot of the voxel indices currently tracked for the occupant in the supplied world. |
| | | 118 | | /// </summary> |
| | | 119 | | /// <param name="world">The world whose occupancy registry should be queried.</param> |
| | | 120 | | /// <param name="occupant">The occupant whose tracked voxel indices should be returned.</param> |
| | | 121 | | /// <returns>A deterministic snapshot of the occupant's currently tracked voxel indices.</returns> |
| | | 122 | | public static IEnumerable<WorldVoxelIndex> GetOccupiedIndices(GridWorld world, IVoxelOccupant occupant) |
| | | 123 | | { |
| | 13 | 124 | | TrackedOccupancy[] occupancies = GetTrackedOccupanciesSnapshot(world, occupant); |
| | 13 | 125 | | if (occupancies.Length == 0) |
| | 8 | 126 | | return Array.Empty<WorldVoxelIndex>(); |
| | | 127 | | |
| | 5 | 128 | | WorldVoxelIndex[] indices = new WorldVoxelIndex[occupancies.Length]; |
| | 24 | 129 | | for (int i = 0; i < occupancies.Length; i++) |
| | 7 | 130 | | indices[i] = occupancies[i].VoxelIndex; |
| | | 131 | | |
| | 5 | 132 | | return indices; |
| | | 133 | | } |
| | | 134 | | |
| | | 135 | | /// <summary> |
| | | 136 | | /// Attempts to retrieve the scan-cell ticket for the occupant at a specific voxel in the supplied world. |
| | | 137 | | /// </summary> |
| | | 138 | | /// <param name="world">The world whose occupancy registry should be queried.</param> |
| | | 139 | | /// <param name="occupant">The occupant whose registration should be queried.</param> |
| | | 140 | | /// <param name="index">The tracked voxel index to look up.</param> |
| | | 141 | | /// <param name="ticket">The tracked scan-cell ticket for the occupant at that voxel.</param> |
| | | 142 | | /// <returns>True if the occupant is registered to the voxel, otherwise false.</returns> |
| | | 143 | | public static bool TryGetOccupancyTicket( |
| | | 144 | | GridWorld world, |
| | | 145 | | IVoxelOccupant occupant, |
| | | 146 | | WorldVoxelIndex index, |
| | | 147 | | out int ticket) |
| | | 148 | | { |
| | 178 | 149 | | ticket = -1; |
| | 178 | 150 | | if (world == null || occupant == null || !TryGetWorldRegistry(world, out WorldOccupancyRegistry? registry)) |
| | 3 | 151 | | return false; |
| | | 152 | | |
| | 175 | 153 | | lock (registry!.SyncRoot) |
| | 175 | 154 | | return TryGetTrackedRecordUnsafe(world, occupant, out OccupancyRecord? record) |
| | 175 | 155 | | && record!.Tickets.TryGetValue(index, out ticket); |
| | 175 | 156 | | } |
| | | 157 | | |
| | | 158 | | /// <summary> |
| | | 159 | | /// Attempts to add an occupant from the given world-scoped voxel identity in the supplied world. |
| | | 160 | | /// </summary> |
| | | 161 | | public static bool TryAddVoxelOccupant( |
| | | 162 | | GridWorld world, |
| | | 163 | | WorldVoxelIndex index, |
| | | 164 | | IVoxelOccupant occupant) |
| | | 165 | | { |
| | 3 | 166 | | return occupant != null |
| | 3 | 167 | | && world != null |
| | 3 | 168 | | && world.TryGetGridAndVoxel(index, out VoxelGrid? grid, out Voxel? voxel) |
| | 3 | 169 | | && grid!.TryAddVoxelOccupant(voxel!, occupant); |
| | | 170 | | } |
| | | 171 | | |
| | | 172 | | /// <summary> |
| | | 173 | | /// Attempts to add an occupant at the given world position. |
| | | 174 | | /// </summary> |
| | | 175 | | public static bool TryAddVoxelOccupant(this VoxelGrid grid, IVoxelOccupant occupant) |
| | | 176 | | { |
| | 375 | 177 | | return occupant != null |
| | 375 | 178 | | && grid.TryGetVoxel(occupant.Position, out Voxel? voxel) |
| | 375 | 179 | | && grid.TryAddVoxelOccupant(voxel!, occupant); |
| | | 180 | | } |
| | | 181 | | |
| | | 182 | | /// <summary> |
| | | 183 | | /// Attempts to add an occupant at the specified voxel index. |
| | | 184 | | /// </summary> |
| | | 185 | | public static bool TryAddVoxelOccupant( |
| | | 186 | | this VoxelGrid grid, |
| | | 187 | | VoxelIndex voxelIndex, |
| | | 188 | | IVoxelOccupant occupant) |
| | | 189 | | { |
| | 2 | 190 | | return occupant != null |
| | 2 | 191 | | && grid.TryGetVoxel(voxelIndex, out Voxel? target) |
| | 2 | 192 | | && grid.TryAddVoxelOccupant(target!, occupant); |
| | | 193 | | } |
| | | 194 | | |
| | | 195 | | /// <summary> |
| | | 196 | | /// Adds an occupant to the grid. |
| | | 197 | | /// </summary> |
| | | 198 | | public static bool TryAddVoxelOccupant( |
| | | 199 | | this VoxelGrid grid, |
| | | 200 | | Voxel targetVoxel, |
| | | 201 | | IVoxelOccupant occupant) |
| | | 202 | | { |
| | 408 | 203 | | if (!TryGetAddContext(grid, targetVoxel, occupant, out GridWorld? world, out ScanCell? scanCell)) |
| | 2 | 204 | | return false; |
| | | 205 | | |
| | | 206 | | int ticket; |
| | | 207 | | byte occupantCount; |
| | | 208 | | |
| | 406 | 209 | | lock (grid.OccupantSyncRoot) |
| | | 210 | | { |
| | 406 | 211 | | ticket = scanCell!.AddOccupant(targetVoxel.WorldIndex, occupant); |
| | 406 | 212 | | if (!TryTrackOccupancy(world!, occupant, targetVoxel.WorldIndex, ticket)) |
| | | 213 | | { |
| | 4 | 214 | | scanCell.TryRemoveOccupant(targetVoxel.WorldIndex, ticket); |
| | 4 | 215 | | return false; |
| | | 216 | | } |
| | | 217 | | |
| | 402 | 218 | | TrackActiveScanCell(grid, targetVoxel.ScanCellKey); |
| | | 219 | | |
| | 402 | 220 | | targetVoxel.OccupantCount++; |
| | 402 | 221 | | occupantCount = targetVoxel.OccupantCount; |
| | 402 | 222 | | } |
| | | 223 | | |
| | 402 | 224 | | NotifyOccupantAdded(targetVoxel, occupant, ticket, occupantCount); |
| | | 225 | | |
| | 402 | 226 | | return true; |
| | 4 | 227 | | } |
| | | 228 | | |
| | | 229 | | /// <summary> |
| | | 230 | | /// Attempts to de-register the occupant from every voxel currently tracked in the supplied world. |
| | | 231 | | /// </summary> |
| | | 232 | | public static bool TryDeregister(GridWorld world, IVoxelOccupant occupant) |
| | | 233 | | { |
| | 6 | 234 | | TrackedOccupancy[] occupancies = GetTrackedOccupanciesSnapshot(world, occupant); |
| | 6 | 235 | | if (occupancies.Length == 0) |
| | 1 | 236 | | return false; |
| | | 237 | | |
| | 5 | 238 | | bool removedAny = false; |
| | | 239 | | |
| | 22 | 240 | | for (int i = 0; i < occupancies.Length; i++) |
| | | 241 | | { |
| | 6 | 242 | | WorldVoxelIndex voxelIndex = occupancies[i].VoxelIndex; |
| | 6 | 243 | | if (!world.TryGetGridAndVoxel(voxelIndex, out VoxelGrid? grid, out Voxel? voxel)) |
| | | 244 | | { |
| | 1 | 245 | | removedAny |= ForgetTrackedOccupancy(world, occupant, voxelIndex); |
| | 1 | 246 | | continue; |
| | | 247 | | } |
| | | 248 | | |
| | 5 | 249 | | removedAny |= grid!.TryRemoveVoxelOccupant(voxel!, occupant); |
| | | 250 | | } |
| | | 251 | | |
| | 5 | 252 | | return removedAny; |
| | | 253 | | } |
| | | 254 | | |
| | | 255 | | /// <summary> |
| | | 256 | | /// Attempts to remove an occupant from the given world-scoped voxel identity in the supplied world. |
| | | 257 | | /// </summary> |
| | | 258 | | public static bool TryRemoveVoxelOccupant( |
| | | 259 | | GridWorld world, |
| | | 260 | | WorldVoxelIndex index, |
| | | 261 | | IVoxelOccupant occupant) |
| | | 262 | | { |
| | 2 | 263 | | return occupant != null |
| | 2 | 264 | | && world != null |
| | 2 | 265 | | && world.TryGetGridAndVoxel(index, out VoxelGrid? grid, out Voxel? voxel) |
| | 2 | 266 | | && grid!.TryRemoveVoxelOccupant(voxel!, occupant); |
| | | 267 | | } |
| | | 268 | | |
| | | 269 | | /// <summary> |
| | | 270 | | /// Attempts to remove an occupant from the given world position. |
| | | 271 | | /// </summary> |
| | | 272 | | public static bool TryRemoveVoxelOccupant( |
| | | 273 | | this VoxelGrid grid, |
| | | 274 | | IVoxelOccupant occupant) |
| | | 275 | | { |
| | 141 | 276 | | if (!TryGetGridOccupancies(grid, occupant, out TrackedOccupancy[] occupancies)) |
| | 4 | 277 | | return false; |
| | | 278 | | |
| | 137 | 279 | | return RemoveTrackedOccupancies(grid, occupant, occupancies); |
| | | 280 | | } |
| | | 281 | | |
| | | 282 | | /// <summary> |
| | | 283 | | /// Attempts to remove an occupant at the specified voxel coordinates. |
| | | 284 | | /// </summary> |
| | | 285 | | public static bool TryRemoveVoxelOccupant( |
| | | 286 | | this VoxelGrid grid, |
| | | 287 | | VoxelIndex voxelIndex, |
| | | 288 | | IVoxelOccupant occupant) |
| | | 289 | | { |
| | 4 | 290 | | return occupant != null |
| | 4 | 291 | | && grid.TryGetVoxel(voxelIndex, out Voxel? targetVoxel) |
| | 4 | 292 | | && grid.TryRemoveVoxelOccupant(targetVoxel!, occupant); |
| | | 293 | | } |
| | | 294 | | |
| | | 295 | | /// <summary> |
| | | 296 | | /// Removes an occupant from this grid. |
| | | 297 | | /// </summary> |
| | | 298 | | public static bool TryRemoveVoxelOccupant( |
| | | 299 | | this VoxelGrid grid, |
| | | 300 | | Voxel targetVoxel, |
| | | 301 | | IVoxelOccupant occupant) |
| | | 302 | | { |
| | 160 | 303 | | if (!TryGetRemovalContext(grid, targetVoxel, occupant, out ScanCell? scanCell, out int ticket)) |
| | 6 | 304 | | return false; |
| | | 305 | | |
| | 154 | 306 | | bool success = false; |
| | 154 | 307 | | byte occupantCount = targetVoxel.OccupantCount; |
| | | 308 | | |
| | 154 | 309 | | lock (grid.OccupantSyncRoot) |
| | | 310 | | { |
| | 154 | 311 | | success = TryRemoveOccupantFromScanCell( |
| | 154 | 312 | | grid, |
| | 154 | 313 | | targetVoxel, |
| | 154 | 314 | | occupant, |
| | 154 | 315 | | scanCell!, |
| | 154 | 316 | | ticket, |
| | 154 | 317 | | out occupantCount); |
| | 154 | 318 | | } |
| | | 319 | | |
| | 154 | 320 | | if (success) |
| | 153 | 321 | | NotifyOccupantRemoved(targetVoxel, occupant, ticket, occupantCount); |
| | | 322 | | |
| | 154 | 323 | | return success; |
| | | 324 | | } |
| | | 325 | | |
| | | 326 | | #endregion |
| | | 327 | | |
| | | 328 | | #region Internal Helpers |
| | | 329 | | |
| | | 330 | | internal static void ForgetTrackedOccupancies( |
| | | 331 | | GridWorld? world, |
| | | 332 | | IEnumerable<IVoxelOccupant> occupants, |
| | | 333 | | WorldVoxelIndex index) |
| | | 334 | | { |
| | 179 | 335 | | if (world == null) |
| | 1 | 336 | | return; |
| | | 337 | | |
| | 854 | 338 | | foreach (IVoxelOccupant occupant in occupants) |
| | 249 | 339 | | ForgetTrackedOccupancy(world, occupant, index); |
| | 178 | 340 | | } |
| | | 341 | | |
| | | 342 | | internal static void ClearTrackedOccupancies(GridWorld world) |
| | | 343 | | { |
| | 427 | 344 | | if (!TryGetWorldRegistry(world, out WorldOccupancyRegistry? registry)) |
| | 376 | 345 | | return; |
| | | 346 | | |
| | 51 | 347 | | lock (registry!.SyncRoot) |
| | 51 | 348 | | registry.Records.Clear(); |
| | 51 | 349 | | } |
| | | 350 | | |
| | | 351 | | internal static void ReleaseTrackedOccupancies(GridWorld world) |
| | | 352 | | { |
| | 423 | 353 | | if (!_occupancyRegistries.TryRemove(world, out WorldOccupancyRegistry? registry)) |
| | 372 | 354 | | return; |
| | | 355 | | |
| | 51 | 356 | | lock (registry.SyncRoot) |
| | 51 | 357 | | registry.Records.Clear(); |
| | 51 | 358 | | } |
| | | 359 | | |
| | | 360 | | #endregion |
| | | 361 | | |
| | | 362 | | #region Private Methods |
| | | 363 | | |
| | | 364 | | private static bool TryGetAddContext( |
| | | 365 | | VoxelGrid grid, |
| | | 366 | | Voxel targetVoxel, |
| | | 367 | | IVoxelOccupant occupant, |
| | | 368 | | out GridWorld? world, |
| | | 369 | | out ScanCell? scanCell) |
| | | 370 | | { |
| | 408 | 371 | | world = grid.World; |
| | 408 | 372 | | scanCell = null; |
| | 408 | 373 | | return occupant != null |
| | 408 | 374 | | && world != null |
| | 408 | 375 | | && targetVoxel.HasVacancy |
| | 408 | 376 | | && grid.TryGetScanCell(targetVoxel.ScanCellKey, out scanCell); |
| | | 377 | | } |
| | | 378 | | |
| | | 379 | | private static void TrackActiveScanCell(VoxelGrid grid, int scanCellKey) |
| | | 380 | | { |
| | 402 | 381 | | grid.ActiveScanCells ??= SwiftHashSetPool<int>.Shared.Rent(); |
| | 402 | 382 | | if (!grid.ActiveScanCells.Contains(scanCellKey)) |
| | 62 | 383 | | grid.ActiveScanCells.Add(scanCellKey); |
| | 402 | 384 | | } |
| | | 385 | | |
| | | 386 | | private static bool TryGetGridOccupancies( |
| | | 387 | | VoxelGrid grid, |
| | | 388 | | IVoxelOccupant occupant, |
| | | 389 | | out TrackedOccupancy[] occupancies) |
| | | 390 | | { |
| | 141 | 391 | | occupancies = Array.Empty<TrackedOccupancy>(); |
| | 141 | 392 | | if (grid.World == null) |
| | 1 | 393 | | return false; |
| | | 394 | | |
| | 140 | 395 | | occupancies = GetTrackedOccupanciesSnapshot(grid.World, occupant, grid.GridIndex); |
| | 140 | 396 | | return occupancies.Length > 0; |
| | | 397 | | } |
| | | 398 | | |
| | | 399 | | private static bool RemoveTrackedOccupancies( |
| | | 400 | | VoxelGrid grid, |
| | | 401 | | IVoxelOccupant occupant, |
| | | 402 | | TrackedOccupancy[] occupancies) |
| | | 403 | | { |
| | 137 | 404 | | bool removedAny = false; |
| | | 405 | | |
| | 548 | 406 | | for (int i = 0; i < occupancies.Length; i++) |
| | | 407 | | { |
| | 137 | 408 | | WorldVoxelIndex voxelIndex = occupancies[i].VoxelIndex; |
| | 137 | 409 | | if (!TryGetCurrentTrackedVoxel(grid, voxelIndex, out Voxel? voxel)) |
| | | 410 | | { |
| | 2 | 411 | | removedAny |= ForgetTrackedOccupancy(grid.World!, occupant, voxelIndex); |
| | 2 | 412 | | continue; |
| | | 413 | | } |
| | | 414 | | |
| | 135 | 415 | | removedAny |= TryRemoveVoxelOccupant(grid, voxel!, occupant); |
| | | 416 | | } |
| | | 417 | | |
| | 137 | 418 | | return removedAny; |
| | | 419 | | } |
| | | 420 | | |
| | | 421 | | private static bool TryGetCurrentTrackedVoxel( |
| | | 422 | | VoxelGrid grid, |
| | | 423 | | WorldVoxelIndex voxelIndex, |
| | | 424 | | out Voxel? voxel) |
| | | 425 | | { |
| | 137 | 426 | | voxel = null; |
| | 137 | 427 | | return grid.World != null |
| | 137 | 428 | | && voxelIndex.WorldSpawnToken == grid.World.SpawnToken |
| | 137 | 429 | | && voxelIndex.GridSpawnToken == grid.SpawnToken |
| | 137 | 430 | | && grid.TryGetVoxel(voxelIndex.VoxelIndex, out voxel); |
| | | 431 | | } |
| | | 432 | | |
| | | 433 | | private static bool TryTrackOccupancy( |
| | | 434 | | GridWorld world, |
| | | 435 | | IVoxelOccupant occupant, |
| | | 436 | | WorldVoxelIndex index, |
| | | 437 | | int ticket) |
| | | 438 | | { |
| | 412 | 439 | | WorldOccupancyRegistry registry = GetWorldRegistry(world); |
| | 412 | 440 | | lock (registry.SyncRoot) |
| | | 441 | | { |
| | 412 | 442 | | if (!TryGetOrAddOccupancyRecord(registry, world, occupant, out OccupancyRecord record)) |
| | 2 | 443 | | return false; |
| | | 444 | | |
| | 410 | 445 | | if (record.Tickets.ContainsKey(index)) |
| | | 446 | | { |
| | 3 | 447 | | GridForgeLogger.Channel.Warn($"Occupant {occupant.GlobalId} is already registered to voxel {index}."); |
| | 3 | 448 | | return false; |
| | | 449 | | } |
| | | 450 | | |
| | 407 | 451 | | record.Tickets[index] = ticket; |
| | 407 | 452 | | return true; |
| | | 453 | | } |
| | 412 | 454 | | } |
| | | 455 | | |
| | | 456 | | private static bool ForgetTrackedOccupancy( |
| | | 457 | | GridWorld world, |
| | | 458 | | IVoxelOccupant occupant, |
| | | 459 | | WorldVoxelIndex index) |
| | | 460 | | { |
| | 410 | 461 | | if (world == null || occupant == null || !TryGetWorldRegistry(world, out WorldOccupancyRegistry? registry)) |
| | 1 | 462 | | return false; |
| | | 463 | | |
| | 409 | 464 | | lock (registry!.SyncRoot) |
| | 409 | 465 | | return ForgetTrackedOccupancyUnsafe(world, occupant, index, registry); |
| | 409 | 466 | | } |
| | | 467 | | |
| | | 468 | | private static bool ForgetTrackedOccupancyUnsafe( |
| | | 469 | | GridWorld world, |
| | | 470 | | IVoxelOccupant occupant, |
| | | 471 | | WorldVoxelIndex index, |
| | | 472 | | WorldOccupancyRegistry registry) |
| | | 473 | | { |
| | 409 | 474 | | if (!TryGetTrackedRecordUnsafe(world, occupant, out OccupancyRecord? record) |
| | 409 | 475 | | || !TryRemoveTrackedTicket(record!, index)) |
| | | 476 | | { |
| | 2 | 477 | | return false; |
| | | 478 | | } |
| | | 479 | | |
| | 407 | 480 | | if (record!.Tickets.Count == 0) |
| | 403 | 481 | | registry.Records.Remove(occupant.GlobalId); |
| | | 482 | | |
| | 407 | 483 | | return true; |
| | | 484 | | } |
| | | 485 | | |
| | | 486 | | private static bool TryRemoveTrackedTicket(OccupancyRecord record, WorldVoxelIndex index) |
| | | 487 | | { |
| | 408 | 488 | | if (!record.Tickets.ContainsKey(index)) |
| | 1 | 489 | | return false; |
| | | 490 | | |
| | 407 | 491 | | record.Tickets.Remove(index); |
| | 407 | 492 | | return true; |
| | | 493 | | } |
| | | 494 | | |
| | | 495 | | private static bool TryGetTrackedRecordUnsafe( |
| | | 496 | | GridWorld world, |
| | | 497 | | IVoxelOccupant occupant, |
| | | 498 | | out OccupancyRecord? record) |
| | | 499 | | { |
| | 742 | 500 | | record = null; |
| | 742 | 501 | | if (world == null |
| | 742 | 502 | | || occupant == null |
| | 742 | 503 | | || !TryGetWorldRegistry(world, out WorldOccupancyRegistry? registry) |
| | 742 | 504 | | || !registry!.Records.TryGetValue(occupant.GlobalId, out record)) |
| | | 505 | | { |
| | 15 | 506 | | return false; |
| | | 507 | | } |
| | | 508 | | |
| | 727 | 509 | | return ReferenceEquals(record.Occupant, occupant); |
| | | 510 | | } |
| | | 511 | | |
| | | 512 | | private static TrackedOccupancy[] GetTrackedOccupanciesSnapshot( |
| | | 513 | | GridWorld world, |
| | | 514 | | IVoxelOccupant occupant, |
| | | 515 | | ushort? gridIndex = null) |
| | | 516 | | { |
| | 159 | 517 | | if (world == null || occupant == null || !TryGetWorldRegistry(world, out WorldOccupancyRegistry? registry)) |
| | 2 | 518 | | return Array.Empty<TrackedOccupancy>(); |
| | | 519 | | |
| | 157 | 520 | | lock (registry!.SyncRoot) |
| | | 521 | | { |
| | 157 | 522 | | if (!TryGetTrackedRecordUnsafe(world, occupant, out OccupancyRecord? record) || record!.Tickets.Count == 0) |
| | 9 | 523 | | return Array.Empty<TrackedOccupancy>(); |
| | | 524 | | |
| | 148 | 525 | | TrackedOccupancy[] occupancies = new TrackedOccupancy[record.Tickets.Count]; |
| | 148 | 526 | | int count = CopyTrackedOccupancies(record, gridIndex, occupancies); |
| | 148 | 527 | | return FinalizeTrackedOccupancySnapshot(occupancies, count); |
| | | 528 | | } |
| | 157 | 529 | | } |
| | | 530 | | |
| | | 531 | | private static bool TryGetRemovalContext( |
| | | 532 | | VoxelGrid grid, |
| | | 533 | | Voxel targetVoxel, |
| | | 534 | | IVoxelOccupant occupant, |
| | | 535 | | out ScanCell? scanCell, |
| | | 536 | | out int ticket) |
| | | 537 | | { |
| | 160 | 538 | | scanCell = null; |
| | 160 | 539 | | ticket = -1; |
| | | 540 | | |
| | 160 | 541 | | if (occupant == null || grid.World == null) |
| | 1 | 542 | | return false; |
| | | 543 | | |
| | 159 | 544 | | if (!TryGetOccupancyTicket(grid.World, occupant, targetVoxel.WorldIndex, out ticket)) |
| | | 545 | | { |
| | 4 | 546 | | GridForgeLogger.Channel.Warn($"Occupant {occupant.GlobalId} is not registered to voxel {targetVoxel.WorldInd |
| | 4 | 547 | | return false; |
| | | 548 | | } |
| | | 549 | | |
| | 155 | 550 | | return targetVoxel.IsOccupied |
| | 155 | 551 | | && grid.TryGetScanCell(targetVoxel.ScanCellKey, out scanCell) == true; |
| | | 552 | | } |
| | | 553 | | |
| | | 554 | | private static bool TryRemoveOccupantFromScanCell( |
| | | 555 | | VoxelGrid grid, |
| | | 556 | | Voxel targetVoxel, |
| | | 557 | | IVoxelOccupant occupant, |
| | | 558 | | ScanCell scanCell, |
| | | 559 | | int ticket, |
| | | 560 | | out byte occupantCount) |
| | | 561 | | { |
| | 154 | 562 | | occupantCount = targetVoxel.OccupantCount; |
| | | 563 | | |
| | 154 | 564 | | if (!scanCell.TryRemoveOccupant(targetVoxel.WorldIndex, ticket)) |
| | 1 | 565 | | return false; |
| | | 566 | | |
| | 153 | 567 | | ForgetTrackedOccupancy(grid.World!, occupant, targetVoxel.WorldIndex); |
| | 153 | 568 | | ReleaseActiveScanCellIfEmpty(grid, targetVoxel, scanCell); |
| | | 569 | | |
| | 153 | 570 | | targetVoxel.OccupantCount--; |
| | 153 | 571 | | occupantCount = targetVoxel.OccupantCount; |
| | 153 | 572 | | return true; |
| | | 573 | | } |
| | | 574 | | |
| | | 575 | | private static void ReleaseActiveScanCellIfEmpty( |
| | | 576 | | VoxelGrid grid, |
| | | 577 | | Voxel targetVoxel, |
| | | 578 | | ScanCell scanCell) |
| | | 579 | | { |
| | 153 | 580 | | if (scanCell.IsOccupied || grid.ActiveScanCells == null) |
| | 134 | 581 | | return; |
| | | 582 | | |
| | 19 | 583 | | grid.ActiveScanCells.Remove(targetVoxel.ScanCellKey); |
| | 19 | 584 | | if (grid.IsOccupied) |
| | 1 | 585 | | return; |
| | | 586 | | |
| | 18 | 587 | | GridForgeLogger.Channel.Info($"Releasing unused active scan cells collection."); |
| | 18 | 588 | | SwiftHashSetPool<int>.Shared.Release(grid.ActiveScanCells); |
| | 18 | 589 | | grid.ActiveScanCells = null; |
| | 18 | 590 | | } |
| | | 591 | | |
| | | 592 | | private static bool TryGetOrAddOccupancyRecord( |
| | | 593 | | WorldOccupancyRegistry registry, |
| | | 594 | | GridWorld world, |
| | | 595 | | IVoxelOccupant occupant, |
| | | 596 | | out OccupancyRecord record) |
| | | 597 | | { |
| | 412 | 598 | | if (registry.Records.TryGetValue(occupant.GlobalId, out record)) |
| | | 599 | | { |
| | 9 | 600 | | if (ReferenceEquals(record.Occupant, occupant)) |
| | 7 | 601 | | return true; |
| | | 602 | | |
| | 2 | 603 | | GridForgeLogger.Channel.Warn($"Occupant id collision detected for {occupant.GlobalId} in world {world.SpawnT |
| | 2 | 604 | | return false; |
| | | 605 | | } |
| | | 606 | | |
| | 403 | 607 | | record = new OccupancyRecord(occupant); |
| | 403 | 608 | | registry.Records[occupant.GlobalId] = record; |
| | 403 | 609 | | return true; |
| | | 610 | | } |
| | | 611 | | |
| | | 612 | | private static int CopyTrackedOccupancies( |
| | | 613 | | OccupancyRecord record, |
| | | 614 | | ushort? gridIndex, |
| | | 615 | | TrackedOccupancy[] occupancies) |
| | | 616 | | { |
| | 148 | 617 | | int count = 0; |
| | | 618 | | |
| | 602 | 619 | | foreach (WorldVoxelIndex index in record.Tickets.Keys) |
| | | 620 | | { |
| | 153 | 621 | | if (gridIndex.HasValue && index.GridIndex != gridIndex.Value) |
| | | 622 | | continue; |
| | | 623 | | |
| | 150 | 624 | | occupancies[count++] = new TrackedOccupancy(index, record.Tickets[index]); |
| | | 625 | | } |
| | | 626 | | |
| | 148 | 627 | | return count; |
| | | 628 | | } |
| | | 629 | | |
| | | 630 | | private static TrackedOccupancy[] FinalizeTrackedOccupancySnapshot( |
| | | 631 | | TrackedOccupancy[] occupancies, |
| | | 632 | | int count) |
| | | 633 | | { |
| | 148 | 634 | | if (count == 0) |
| | 1 | 635 | | return Array.Empty<TrackedOccupancy>(); |
| | | 636 | | |
| | 147 | 637 | | if (count != occupancies.Length) |
| | 1 | 638 | | Array.Resize(ref occupancies, count); |
| | | 639 | | |
| | 147 | 640 | | Array.Sort(occupancies, CompareTrackedOccupancies); |
| | 147 | 641 | | return occupancies; |
| | | 642 | | } |
| | | 643 | | |
| | | 644 | | private static int CompareTrackedOccupancies(TrackedOccupancy left, TrackedOccupancy right) |
| | | 645 | | { |
| | 12 | 646 | | int result = left.VoxelIndex.WorldSpawnToken.CompareTo(right.VoxelIndex.WorldSpawnToken); |
| | 12 | 647 | | if (result != 0) |
| | 2 | 648 | | return result; |
| | | 649 | | |
| | 10 | 650 | | result = left.VoxelIndex.GridIndex.CompareTo(right.VoxelIndex.GridIndex); |
| | 10 | 651 | | if (result != 0) |
| | 3 | 652 | | return result; |
| | | 653 | | |
| | 7 | 654 | | result = left.VoxelIndex.VoxelIndex.x.CompareTo(right.VoxelIndex.VoxelIndex.x); |
| | 7 | 655 | | if (result != 0) |
| | 2 | 656 | | return result; |
| | | 657 | | |
| | 5 | 658 | | result = left.VoxelIndex.VoxelIndex.y.CompareTo(right.VoxelIndex.VoxelIndex.y); |
| | 5 | 659 | | if (result != 0) |
| | 1 | 660 | | return result; |
| | | 661 | | |
| | 4 | 662 | | result = left.VoxelIndex.VoxelIndex.z.CompareTo(right.VoxelIndex.VoxelIndex.z); |
| | 4 | 663 | | if (result != 0) |
| | 1 | 664 | | return result; |
| | | 665 | | |
| | 3 | 666 | | result = left.VoxelIndex.GridSpawnToken.CompareTo(right.VoxelIndex.GridSpawnToken); |
| | 3 | 667 | | if (result != 0) |
| | 1 | 668 | | return result; |
| | | 669 | | |
| | 2 | 670 | | return left.Ticket.CompareTo(right.Ticket); |
| | | 671 | | } |
| | | 672 | | |
| | | 673 | | /// <summary> |
| | | 674 | | /// Notifies listeners that an occupant was added. |
| | | 675 | | /// </summary> |
| | | 676 | | private static void NotifyOccupantAdded( |
| | | 677 | | Voxel targetVoxel, |
| | | 678 | | IVoxelOccupant occupant, |
| | | 679 | | int ticket, |
| | | 680 | | byte occupantCount) |
| | | 681 | | { |
| | 402 | 682 | | OccupantEventInfo eventInfo = new(targetVoxel.WorldIndex, occupant, ticket, occupantCount); |
| | 402 | 683 | | Action<OccupantEventInfo>? handlers = _onOccupantAdded; |
| | 402 | 684 | | if (handlers != null) |
| | | 685 | | { |
| | 5 | 686 | | var handlerDelegates = handlers.GetInvocationList(); |
| | 22 | 687 | | for (int i = 0; i < handlerDelegates.Length; i++) |
| | | 688 | | { |
| | | 689 | | try |
| | | 690 | | { |
| | 6 | 691 | | ((Action<OccupantEventInfo>)handlerDelegates[i])(eventInfo); |
| | 5 | 692 | | } |
| | 1 | 693 | | catch (Exception ex) |
| | | 694 | | { |
| | 1 | 695 | | GridForgeLogger.Channel.Error($"[Voxel {targetVoxel.WorldIndex}] Occupant add error: {ex.Message}"); |
| | 1 | 696 | | } |
| | | 697 | | } |
| | | 698 | | } |
| | | 699 | | |
| | 402 | 700 | | targetVoxel.NotifyOccupantAdded(eventInfo); |
| | 402 | 701 | | } |
| | | 702 | | |
| | | 703 | | /// <summary> |
| | | 704 | | /// Notifies listeners that an occupant was removed. |
| | | 705 | | /// </summary> |
| | | 706 | | private static void NotifyOccupantRemoved( |
| | | 707 | | Voxel targetVoxel, |
| | | 708 | | IVoxelOccupant occupant, |
| | | 709 | | int ticket, |
| | | 710 | | byte occupantCount) |
| | | 711 | | { |
| | 153 | 712 | | OccupantEventInfo eventInfo = new(targetVoxel.WorldIndex, occupant, ticket, occupantCount); |
| | 153 | 713 | | Action<OccupantEventInfo>? handlers = _onOccupantRemoved; |
| | 153 | 714 | | if (handlers != null) |
| | | 715 | | { |
| | 3 | 716 | | var handlerDelegates = handlers.GetInvocationList(); |
| | 14 | 717 | | for (int i = 0; i < handlerDelegates.Length; i++) |
| | | 718 | | { |
| | | 719 | | try |
| | | 720 | | { |
| | 4 | 721 | | ((Action<OccupantEventInfo>)handlerDelegates[i])(eventInfo); |
| | 3 | 722 | | } |
| | 1 | 723 | | catch (Exception ex) |
| | | 724 | | { |
| | 1 | 725 | | GridForgeLogger.Channel.Error($"[Voxel {targetVoxel.WorldIndex}] Occupant remove error: {ex.Message} |
| | 1 | 726 | | } |
| | | 727 | | } |
| | | 728 | | } |
| | | 729 | | |
| | 153 | 730 | | targetVoxel.NotifyOccupantRemoved(eventInfo); |
| | 153 | 731 | | } |
| | | 732 | | |
| | | 733 | | private static WorldOccupancyRegistry GetWorldRegistry(GridWorld world) |
| | | 734 | | { |
| | 412 | 735 | | return _occupancyRegistries.GetOrAdd(world, static _ => new WorldOccupancyRegistry()); |
| | | 736 | | } |
| | | 737 | | |
| | | 738 | | private static bool TryGetWorldRegistry(GridWorld world, out WorldOccupancyRegistry? registry) |
| | | 739 | | { |
| | 1912 | 740 | | registry = null; |
| | 1912 | 741 | | return _occupancyRegistries.TryGetValue(world, out registry); |
| | | 742 | | } |
| | | 743 | | |
| | | 744 | | #endregion |
| | | 745 | | } |