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