| | | 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.Concurrent; |
| | | 10 | | using System.Collections.Generic; |
| | | 11 | | using GridForge.Spatial; |
| | | 12 | | using SwiftCollections; |
| | | 13 | | using SwiftCollections.Pool; |
| | | 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 | | { |
| | 17 | 38 | | add => _onOccupantAdded += value; |
| | 17 | 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 | | { |
| | 17 | 50 | | add => _onOccupantRemoved += value; |
| | 17 | 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; |
| | 670 | 69 | | public readonly SwiftDictionary<WorldVoxelIndex, OccupantTicket> Tickets = new(); |
| | | 70 | | |
| | 670 | 71 | | public OccupancyRecord(IVoxelOccupant occupant) |
| | | 72 | | { |
| | 670 | 73 | | Occupant = occupant; |
| | 670 | 74 | | } |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Synchronizes one world's tracked occupant registrations. |
| | | 79 | | /// </summary> |
| | | 80 | | private sealed class WorldOccupancyRegistry |
| | | 81 | | { |
| | 60 | 82 | | public readonly object SyncRoot = new(); |
| | 60 | 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 OccupantTicket Ticket; |
| | | 93 | | |
| | | 94 | | public TrackedOccupancy(WorldVoxelIndex voxelIndex, OccupantTicket ticket) |
| | | 95 | | { |
| | 172 | 96 | | VoxelIndex = voxelIndex; |
| | 172 | 97 | | Ticket = ticket; |
| | 172 | 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 | | { |
| | 17 | 124 | | TrackedOccupancy[] occupancies = GetTrackedOccupanciesSnapshot(world, occupant); |
| | 17 | 125 | | if (occupancies.Length == 0) |
| | 10 | 126 | | return Array.Empty<WorldVoxelIndex>(); |
| | | 127 | | |
| | 7 | 128 | | WorldVoxelIndex[] indices = new WorldVoxelIndex[occupancies.Length]; |
| | 32 | 129 | | for (int i = 0; i < occupancies.Length; i++) |
| | 9 | 130 | | indices[i] = occupancies[i].VoxelIndex; |
| | | 131 | | |
| | 7 | 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 OccupantTicket ticket) |
| | | 148 | | { |
| | 196 | 149 | | ticket = default; |
| | 196 | 150 | | if (world == null || occupant == null || !TryGetWorldRegistry(world, out WorldOccupancyRegistry? registry)) |
| | 6 | 151 | | return false; |
| | | 152 | | |
| | 190 | 153 | | lock (registry!.SyncRoot) |
| | 190 | 154 | | return TryGetTrackedRecordUnsafe(registry, occupant, out OccupancyRecord? record) |
| | 190 | 155 | | && record!.Tickets.TryGetValue(index, out ticket); |
| | 190 | 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 | | { |
| | 388 | 177 | | return occupant != null |
| | 388 | 178 | | && grid.TryGetVoxel(occupant.Position, out Voxel? voxel) |
| | 388 | 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 | | { |
| | 740 | 203 | | if (!TryGetAddContext(grid, targetVoxel, occupant, out GridWorld? world, out ScanCell? scanCell)) |
| | 1 | 204 | | return false; |
| | | 205 | | |
| | | 206 | | OccupantTicket ticket; |
| | | 207 | | byte occupantCount; |
| | | 208 | | |
| | 739 | 209 | | lock (grid.OccupantSyncRoot) |
| | | 210 | | { |
| | 739 | 211 | | if (!targetVoxel.HasVacancy) |
| | 65 | 212 | | return false; |
| | | 213 | | |
| | 674 | 214 | | ticket = scanCell!.AddOccupant(targetVoxel.WorldIndex, occupant); |
| | 673 | 215 | | if (!TryTrackOccupancy(world!, occupant, targetVoxel.WorldIndex, ticket)) |
| | | 216 | | { |
| | 4 | 217 | | scanCell.TryRemoveOccupant(targetVoxel.WorldIndex, ticket); |
| | 4 | 218 | | return false; |
| | | 219 | | } |
| | | 220 | | |
| | 669 | 221 | | TrackActiveScanCell(grid, targetVoxel.ScanCellKey); |
| | | 222 | | |
| | 669 | 223 | | targetVoxel.OccupantCount++; |
| | 669 | 224 | | occupantCount = targetVoxel.OccupantCount; |
| | 669 | 225 | | } |
| | | 226 | | |
| | 669 | 227 | | NotifyOccupantAdded(targetVoxel, occupant, ticket, occupantCount); |
| | | 228 | | |
| | 669 | 229 | | return true; |
| | 69 | 230 | | } |
| | | 231 | | |
| | | 232 | | /// <summary> |
| | | 233 | | /// Attempts to de-register the occupant from every voxel currently tracked in the supplied world. |
| | | 234 | | /// </summary> |
| | | 235 | | public static bool TryDeregister(GridWorld world, IVoxelOccupant occupant) |
| | | 236 | | { |
| | 6 | 237 | | TrackedOccupancy[] occupancies = GetTrackedOccupanciesSnapshot(world, occupant); |
| | 6 | 238 | | if (occupancies.Length == 0) |
| | 1 | 239 | | return false; |
| | | 240 | | |
| | 5 | 241 | | bool removedAny = false; |
| | | 242 | | |
| | 22 | 243 | | for (int i = 0; i < occupancies.Length; i++) |
| | | 244 | | { |
| | 6 | 245 | | WorldVoxelIndex voxelIndex = occupancies[i].VoxelIndex; |
| | 6 | 246 | | if (!world.TryGetGridAndVoxel(voxelIndex, out VoxelGrid? grid, out Voxel? voxel)) |
| | | 247 | | { |
| | 1 | 248 | | removedAny |= ForgetTrackedOccupancy(world, occupant, voxelIndex); |
| | 1 | 249 | | continue; |
| | | 250 | | } |
| | | 251 | | |
| | 5 | 252 | | removedAny |= grid!.TryRemoveVoxelOccupant(voxel!, occupant); |
| | | 253 | | } |
| | | 254 | | |
| | 5 | 255 | | return removedAny; |
| | | 256 | | } |
| | | 257 | | |
| | | 258 | | /// <summary> |
| | | 259 | | /// Attempts to remove an occupant from the given world-scoped voxel identity in the supplied world. |
| | | 260 | | /// </summary> |
| | | 261 | | public static bool TryRemoveVoxelOccupant( |
| | | 262 | | GridWorld world, |
| | | 263 | | WorldVoxelIndex index, |
| | | 264 | | IVoxelOccupant occupant) |
| | | 265 | | { |
| | 2 | 266 | | return occupant != null |
| | 2 | 267 | | && world != null |
| | 2 | 268 | | && world.TryGetGridAndVoxel(index, out VoxelGrid? grid, out Voxel? voxel) |
| | 2 | 269 | | && grid!.TryRemoveVoxelOccupant(voxel!, occupant); |
| | | 270 | | } |
| | | 271 | | |
| | | 272 | | /// <summary> |
| | | 273 | | /// Attempts to remove an occupant from the given world position. |
| | | 274 | | /// </summary> |
| | | 275 | | public static bool TryRemoveVoxelOccupant( |
| | | 276 | | this VoxelGrid grid, |
| | | 277 | | IVoxelOccupant occupant) |
| | | 278 | | { |
| | 143 | 279 | | if (!TryGetGridOccupancies(grid, occupant, out TrackedOccupancy[] occupancies)) |
| | 4 | 280 | | return false; |
| | | 281 | | |
| | 139 | 282 | | return RemoveTrackedOccupancies(grid, occupant, occupancies); |
| | | 283 | | } |
| | | 284 | | |
| | | 285 | | /// <summary> |
| | | 286 | | /// Attempts to remove an occupant at the specified voxel coordinates. |
| | | 287 | | /// </summary> |
| | | 288 | | public static bool TryRemoveVoxelOccupant( |
| | | 289 | | this VoxelGrid grid, |
| | | 290 | | VoxelIndex voxelIndex, |
| | | 291 | | IVoxelOccupant occupant) |
| | | 292 | | { |
| | 4 | 293 | | return occupant != null |
| | 4 | 294 | | && grid.TryGetVoxel(voxelIndex, out Voxel? targetVoxel) |
| | 4 | 295 | | && grid.TryRemoveVoxelOccupant(targetVoxel!, occupant); |
| | | 296 | | } |
| | | 297 | | |
| | | 298 | | /// <summary> |
| | | 299 | | /// Removes an occupant from this grid. |
| | | 300 | | /// </summary> |
| | | 301 | | public static bool TryRemoveVoxelOccupant( |
| | | 302 | | this VoxelGrid grid, |
| | | 303 | | Voxel targetVoxel, |
| | | 304 | | IVoxelOccupant occupant) |
| | | 305 | | { |
| | 162 | 306 | | if (!TryGetRemovalContext(grid, targetVoxel, occupant, out ScanCell? scanCell, out OccupantTicket ticket)) |
| | 6 | 307 | | return false; |
| | | 308 | | |
| | 156 | 309 | | bool success = false; |
| | 156 | 310 | | byte occupantCount = targetVoxel.OccupantCount; |
| | | 311 | | |
| | 156 | 312 | | lock (grid.OccupantSyncRoot) |
| | | 313 | | { |
| | 156 | 314 | | success = TryRemoveOccupantFromScanCell( |
| | 156 | 315 | | grid, |
| | 156 | 316 | | targetVoxel, |
| | 156 | 317 | | occupant, |
| | 156 | 318 | | scanCell!, |
| | 156 | 319 | | ticket, |
| | 156 | 320 | | out occupantCount); |
| | 156 | 321 | | } |
| | | 322 | | |
| | 156 | 323 | | if (success) |
| | 155 | 324 | | NotifyOccupantRemoved(targetVoxel, occupant, ticket, occupantCount); |
| | | 325 | | |
| | 156 | 326 | | return success; |
| | | 327 | | } |
| | | 328 | | |
| | | 329 | | #endregion |
| | | 330 | | |
| | | 331 | | #region Internal Helpers |
| | | 332 | | |
| | | 333 | | internal static bool ForgetTrackedOccupancy( |
| | | 334 | | GridWorld? world, |
| | | 335 | | IVoxelOccupant occupant, |
| | | 336 | | WorldVoxelIndex index) |
| | | 337 | | { |
| | 677 | 338 | | if (world == null || occupant == null || !TryGetWorldRegistry(world, out WorldOccupancyRegistry? registry)) |
| | 2 | 339 | | return false; |
| | | 340 | | |
| | 675 | 341 | | lock (registry!.SyncRoot) |
| | 675 | 342 | | return ForgetTrackedOccupancyUnsafe(occupant, index, registry); |
| | 675 | 343 | | } |
| | | 344 | | |
| | | 345 | | internal static void ClearTrackedOccupancies(GridWorld world) |
| | | 346 | | { |
| | 542 | 347 | | if (!TryGetWorldRegistry(world, out WorldOccupancyRegistry? registry)) |
| | 481 | 348 | | return; |
| | | 349 | | |
| | 61 | 350 | | lock (registry!.SyncRoot) |
| | 61 | 351 | | registry.Records.Clear(); |
| | 61 | 352 | | } |
| | | 353 | | |
| | | 354 | | internal static void ReleaseTrackedOccupancies(GridWorld world) |
| | | 355 | | { |
| | 533 | 356 | | if (!_occupancyRegistries.TryRemove(world, out WorldOccupancyRegistry? registry)) |
| | 473 | 357 | | return; |
| | | 358 | | |
| | 60 | 359 | | lock (registry.SyncRoot) |
| | 60 | 360 | | registry.Records.Clear(); |
| | 60 | 361 | | } |
| | | 362 | | |
| | | 363 | | #endregion |
| | | 364 | | |
| | | 365 | | #region Private Methods |
| | | 366 | | |
| | | 367 | | private static bool TryGetAddContext( |
| | | 368 | | VoxelGrid grid, |
| | | 369 | | Voxel targetVoxel, |
| | | 370 | | IVoxelOccupant occupant, |
| | | 371 | | out GridWorld? world, |
| | | 372 | | out ScanCell? scanCell) |
| | | 373 | | { |
| | 740 | 374 | | world = grid.World; |
| | 740 | 375 | | scanCell = null; |
| | 740 | 376 | | return occupant != null |
| | 740 | 377 | | && world != null |
| | 740 | 378 | | && grid.TryGetScanCell(targetVoxel.ScanCellKey, out scanCell); |
| | | 379 | | } |
| | | 380 | | |
| | | 381 | | private static void TrackActiveScanCell(VoxelGrid grid, int scanCellKey) |
| | | 382 | | { |
| | 669 | 383 | | grid.ActiveScanCells ??= SwiftHashSetPool<int>.Shared.Rent(); |
| | 669 | 384 | | if (!grid.ActiveScanCells.Contains(scanCellKey)) |
| | 75 | 385 | | grid.ActiveScanCells.Add(scanCellKey); |
| | 669 | 386 | | } |
| | | 387 | | |
| | | 388 | | private static bool TryGetGridOccupancies( |
| | | 389 | | VoxelGrid grid, |
| | | 390 | | IVoxelOccupant occupant, |
| | | 391 | | out TrackedOccupancy[] occupancies) |
| | | 392 | | { |
| | 143 | 393 | | occupancies = Array.Empty<TrackedOccupancy>(); |
| | 143 | 394 | | if (grid.World == null) |
| | 1 | 395 | | return false; |
| | | 396 | | |
| | 142 | 397 | | occupancies = GetTrackedOccupanciesSnapshot(grid.World, occupant, grid.GridIndex); |
| | 142 | 398 | | return occupancies.Length > 0; |
| | | 399 | | } |
| | | 400 | | |
| | | 401 | | private static bool RemoveTrackedOccupancies( |
| | | 402 | | VoxelGrid grid, |
| | | 403 | | IVoxelOccupant occupant, |
| | | 404 | | TrackedOccupancy[] occupancies) |
| | | 405 | | { |
| | 139 | 406 | | bool removedAny = false; |
| | | 407 | | |
| | 556 | 408 | | for (int i = 0; i < occupancies.Length; i++) |
| | | 409 | | { |
| | 139 | 410 | | WorldVoxelIndex voxelIndex = occupancies[i].VoxelIndex; |
| | 139 | 411 | | if (!TryGetCurrentTrackedVoxel(grid, voxelIndex, out Voxel? voxel)) |
| | | 412 | | { |
| | 2 | 413 | | removedAny |= ForgetTrackedOccupancy(grid.World!, occupant, voxelIndex); |
| | 2 | 414 | | continue; |
| | | 415 | | } |
| | | 416 | | |
| | 137 | 417 | | removedAny |= TryRemoveVoxelOccupant(grid, voxel!, occupant); |
| | | 418 | | } |
| | | 419 | | |
| | 139 | 420 | | return removedAny; |
| | | 421 | | } |
| | | 422 | | |
| | | 423 | | private static bool TryGetCurrentTrackedVoxel( |
| | | 424 | | VoxelGrid grid, |
| | | 425 | | WorldVoxelIndex voxelIndex, |
| | | 426 | | out Voxel? voxel) |
| | | 427 | | { |
| | 139 | 428 | | voxel = null; |
| | 139 | 429 | | return grid.World != null |
| | 139 | 430 | | && voxelIndex.WorldSpawnToken == grid.World.SpawnToken |
| | 139 | 431 | | && voxelIndex.GridSpawnToken == grid.SpawnToken |
| | 139 | 432 | | && grid.TryGetVoxel(voxelIndex.VoxelIndex, out voxel); |
| | | 433 | | } |
| | | 434 | | |
| | | 435 | | private static bool TryTrackOccupancy( |
| | | 436 | | GridWorld world, |
| | | 437 | | IVoxelOccupant occupant, |
| | | 438 | | WorldVoxelIndex index, |
| | | 439 | | OccupantTicket ticket) |
| | | 440 | | { |
| | 679 | 441 | | WorldOccupancyRegistry registry = GetWorldRegistry(world); |
| | 679 | 442 | | lock (registry.SyncRoot) |
| | | 443 | | { |
| | 679 | 444 | | if (!TryGetOrAddOccupancyRecord(registry, world, occupant, out OccupancyRecord record)) |
| | 2 | 445 | | return false; |
| | | 446 | | |
| | 677 | 447 | | if (record.Tickets.ContainsKey(index)) |
| | | 448 | | { |
| | 3 | 449 | | GridForgeLogger.Channel.Warn($"Occupant {occupant.GlobalId} is already registered to voxel {index}."); |
| | 3 | 450 | | return false; |
| | | 451 | | } |
| | | 452 | | |
| | 674 | 453 | | record.Tickets[index] = ticket; |
| | 674 | 454 | | return true; |
| | | 455 | | } |
| | 679 | 456 | | } |
| | | 457 | | |
| | | 458 | | private static bool ForgetTrackedOccupancyUnsafe( |
| | | 459 | | IVoxelOccupant occupant, |
| | | 460 | | WorldVoxelIndex index, |
| | | 461 | | WorldOccupancyRegistry registry) |
| | | 462 | | { |
| | 675 | 463 | | if (!TryGetTrackedRecordUnsafe(registry, occupant, out OccupancyRecord? record) |
| | 675 | 464 | | || !TryRemoveTrackedTicket(record!, index)) |
| | | 465 | | { |
| | 2 | 466 | | return false; |
| | | 467 | | } |
| | | 468 | | |
| | 673 | 469 | | if (record!.Tickets.Count == 0) |
| | 669 | 470 | | registry.Records.Remove(occupant.GlobalId); |
| | | 471 | | |
| | 673 | 472 | | return true; |
| | | 473 | | } |
| | | 474 | | |
| | | 475 | | private static bool TryRemoveTrackedTicket(OccupancyRecord record, WorldVoxelIndex index) |
| | | 476 | | { |
| | 674 | 477 | | if (!record.Tickets.ContainsKey(index)) |
| | 1 | 478 | | return false; |
| | | 479 | | |
| | 673 | 480 | | record.Tickets.Remove(index); |
| | 673 | 481 | | return true; |
| | | 482 | | } |
| | | 483 | | |
| | | 484 | | private static bool TryGetTrackedRecordUnsafe( |
| | | 485 | | WorldOccupancyRegistry registry, |
| | | 486 | | IVoxelOccupant occupant, |
| | | 487 | | out OccupancyRecord? record) |
| | | 488 | | { |
| | 1026 | 489 | | record = null; |
| | 1026 | 490 | | if (!registry.Records.TryGetValue(occupant.GlobalId, out record)) |
| | 16 | 491 | | return false; |
| | | 492 | | |
| | 1010 | 493 | | return ReferenceEquals(record.Occupant, occupant); |
| | | 494 | | } |
| | | 495 | | |
| | | 496 | | private static TrackedOccupancy[] GetTrackedOccupanciesSnapshot( |
| | | 497 | | GridWorld world, |
| | | 498 | | IVoxelOccupant occupant, |
| | | 499 | | ushort? gridIndex = null) |
| | | 500 | | { |
| | 165 | 501 | | if (world == null || occupant == null || !TryGetWorldRegistry(world, out WorldOccupancyRegistry? registry)) |
| | 4 | 502 | | return Array.Empty<TrackedOccupancy>(); |
| | | 503 | | |
| | 161 | 504 | | lock (registry!.SyncRoot) |
| | | 505 | | { |
| | 161 | 506 | | if (!TryGetTrackedRecordUnsafe(registry, occupant, out OccupancyRecord? record) || record!.Tickets.Count == |
| | 9 | 507 | | return Array.Empty<TrackedOccupancy>(); |
| | | 508 | | |
| | 152 | 509 | | TrackedOccupancy[] occupancies = new TrackedOccupancy[record.Tickets.Count]; |
| | 152 | 510 | | int count = CopyTrackedOccupancies(record, gridIndex, occupancies); |
| | 152 | 511 | | return FinalizeTrackedOccupancySnapshot(occupancies, count); |
| | | 512 | | } |
| | 161 | 513 | | } |
| | | 514 | | |
| | | 515 | | private static bool TryGetRemovalContext( |
| | | 516 | | VoxelGrid grid, |
| | | 517 | | Voxel targetVoxel, |
| | | 518 | | IVoxelOccupant occupant, |
| | | 519 | | out ScanCell? scanCell, |
| | | 520 | | out OccupantTicket ticket) |
| | | 521 | | { |
| | 162 | 522 | | scanCell = null; |
| | 162 | 523 | | ticket = default; |
| | | 524 | | |
| | 162 | 525 | | if (occupant == null || grid.World == null) |
| | 1 | 526 | | return false; |
| | | 527 | | |
| | 161 | 528 | | if (!TryGetOccupancyTicket(grid.World, occupant, targetVoxel.WorldIndex, out ticket)) |
| | | 529 | | { |
| | 4 | 530 | | GridForgeLogger.Channel.Warn($"Occupant {occupant.GlobalId} is not registered to voxel {targetVoxel.WorldInd |
| | 4 | 531 | | return false; |
| | | 532 | | } |
| | | 533 | | |
| | 157 | 534 | | return targetVoxel.IsOccupied |
| | 157 | 535 | | && grid.TryGetScanCell(targetVoxel.ScanCellKey, out scanCell) == true; |
| | | 536 | | } |
| | | 537 | | |
| | | 538 | | private static bool TryRemoveOccupantFromScanCell( |
| | | 539 | | VoxelGrid grid, |
| | | 540 | | Voxel targetVoxel, |
| | | 541 | | IVoxelOccupant occupant, |
| | | 542 | | ScanCell scanCell, |
| | | 543 | | OccupantTicket ticket, |
| | | 544 | | out byte occupantCount) |
| | | 545 | | { |
| | 156 | 546 | | occupantCount = targetVoxel.OccupantCount; |
| | | 547 | | |
| | 156 | 548 | | if (!scanCell.TryRemoveOccupant(targetVoxel.WorldIndex, ticket)) |
| | 1 | 549 | | return false; |
| | | 550 | | |
| | 155 | 551 | | ForgetTrackedOccupancy(grid.World!, occupant, targetVoxel.WorldIndex); |
| | 155 | 552 | | ReleaseActiveScanCellIfEmpty(grid, targetVoxel, scanCell); |
| | | 553 | | |
| | 155 | 554 | | targetVoxel.OccupantCount--; |
| | 155 | 555 | | occupantCount = targetVoxel.OccupantCount; |
| | 155 | 556 | | return true; |
| | | 557 | | } |
| | | 558 | | |
| | | 559 | | private static void ReleaseActiveScanCellIfEmpty( |
| | | 560 | | VoxelGrid grid, |
| | | 561 | | Voxel targetVoxel, |
| | | 562 | | ScanCell scanCell) |
| | | 563 | | { |
| | 155 | 564 | | if (scanCell.IsOccupied || grid.ActiveScanCells == null) |
| | 134 | 565 | | return; |
| | | 566 | | |
| | 21 | 567 | | grid.ActiveScanCells.Remove(targetVoxel.ScanCellKey); |
| | 21 | 568 | | if (grid.IsOccupied) |
| | 1 | 569 | | return; |
| | | 570 | | |
| | 20 | 571 | | GridForgeLogger.Channel.Info($"Releasing unused active scan cells collection."); |
| | 20 | 572 | | SwiftHashSetPool<int>.Shared.Release(grid.ActiveScanCells); |
| | 20 | 573 | | grid.ActiveScanCells = null; |
| | 20 | 574 | | } |
| | | 575 | | |
| | | 576 | | private static bool TryGetOrAddOccupancyRecord( |
| | | 577 | | WorldOccupancyRegistry registry, |
| | | 578 | | GridWorld world, |
| | | 579 | | IVoxelOccupant occupant, |
| | | 580 | | out OccupancyRecord record) |
| | | 581 | | { |
| | 679 | 582 | | if (registry.Records.TryGetValue(occupant.GlobalId, out record)) |
| | | 583 | | { |
| | 9 | 584 | | if (ReferenceEquals(record.Occupant, occupant)) |
| | 7 | 585 | | return true; |
| | | 586 | | |
| | 2 | 587 | | GridForgeLogger.Channel.Warn($"Occupant id collision detected for {occupant.GlobalId} in world {world.SpawnT |
| | 2 | 588 | | return false; |
| | | 589 | | } |
| | | 590 | | |
| | 670 | 591 | | record = new OccupancyRecord(occupant); |
| | 670 | 592 | | registry.Records[occupant.GlobalId] = record; |
| | 670 | 593 | | return true; |
| | | 594 | | } |
| | | 595 | | |
| | | 596 | | private static int CopyTrackedOccupancies( |
| | | 597 | | OccupancyRecord record, |
| | | 598 | | ushort? gridIndex, |
| | | 599 | | TrackedOccupancy[] occupancies) |
| | | 600 | | { |
| | 152 | 601 | | int count = 0; |
| | | 602 | | |
| | 618 | 603 | | foreach (WorldVoxelIndex index in record.Tickets.Keys) |
| | | 604 | | { |
| | 157 | 605 | | if (gridIndex.HasValue && index.GridIndex != gridIndex.Value) |
| | | 606 | | continue; |
| | | 607 | | |
| | 154 | 608 | | occupancies[count++] = new TrackedOccupancy(index, record.Tickets[index]); |
| | | 609 | | } |
| | | 610 | | |
| | 152 | 611 | | return count; |
| | | 612 | | } |
| | | 613 | | |
| | | 614 | | private static TrackedOccupancy[] FinalizeTrackedOccupancySnapshot( |
| | | 615 | | TrackedOccupancy[] occupancies, |
| | | 616 | | int count) |
| | | 617 | | { |
| | 152 | 618 | | if (count == 0) |
| | 1 | 619 | | return Array.Empty<TrackedOccupancy>(); |
| | | 620 | | |
| | 151 | 621 | | if (count != occupancies.Length) |
| | 1 | 622 | | Array.Resize(ref occupancies, count); |
| | | 623 | | |
| | 151 | 624 | | Array.Sort(occupancies, CompareTrackedOccupancies); |
| | 151 | 625 | | return occupancies; |
| | | 626 | | } |
| | | 627 | | |
| | | 628 | | private static int CompareTrackedOccupancies(TrackedOccupancy left, TrackedOccupancy right) |
| | | 629 | | { |
| | 13 | 630 | | int result = left.VoxelIndex.WorldSpawnToken.CompareTo(right.VoxelIndex.WorldSpawnToken); |
| | 13 | 631 | | if (result != 0) |
| | 2 | 632 | | return result; |
| | | 633 | | |
| | 11 | 634 | | result = left.VoxelIndex.GridIndex.CompareTo(right.VoxelIndex.GridIndex); |
| | 11 | 635 | | if (result != 0) |
| | 3 | 636 | | return result; |
| | | 637 | | |
| | 8 | 638 | | result = left.VoxelIndex.VoxelIndex.x.CompareTo(right.VoxelIndex.VoxelIndex.x); |
| | 8 | 639 | | if (result != 0) |
| | 2 | 640 | | return result; |
| | | 641 | | |
| | 6 | 642 | | result = left.VoxelIndex.VoxelIndex.y.CompareTo(right.VoxelIndex.VoxelIndex.y); |
| | 6 | 643 | | if (result != 0) |
| | 1 | 644 | | return result; |
| | | 645 | | |
| | 5 | 646 | | result = left.VoxelIndex.VoxelIndex.z.CompareTo(right.VoxelIndex.VoxelIndex.z); |
| | 5 | 647 | | if (result != 0) |
| | 1 | 648 | | return result; |
| | | 649 | | |
| | 4 | 650 | | result = left.VoxelIndex.GridSpawnToken.CompareTo(right.VoxelIndex.GridSpawnToken); |
| | 4 | 651 | | if (result != 0) |
| | 1 | 652 | | return result; |
| | | 653 | | |
| | 3 | 654 | | result = left.Ticket.Slot.CompareTo(right.Ticket.Slot); |
| | 3 | 655 | | return result != 0 |
| | 3 | 656 | | ? result |
| | 3 | 657 | | : left.Ticket.Generation.CompareTo(right.Ticket.Generation); |
| | | 658 | | } |
| | | 659 | | |
| | | 660 | | /// <summary> |
| | | 661 | | /// Notifies listeners that an occupant was added. |
| | | 662 | | /// </summary> |
| | | 663 | | private static void NotifyOccupantAdded( |
| | | 664 | | Voxel targetVoxel, |
| | | 665 | | IVoxelOccupant occupant, |
| | | 666 | | OccupantTicket ticket, |
| | | 667 | | byte occupantCount) |
| | | 668 | | { |
| | 669 | 669 | | OccupantEventInfo eventInfo = new(targetVoxel.WorldIndex, occupant, ticket, occupantCount); |
| | 669 | 670 | | Action<OccupantEventInfo>? handlers = _onOccupantAdded; |
| | 669 | 671 | | if (handlers != null) |
| | | 672 | | { |
| | 5 | 673 | | var handlerDelegates = handlers.GetInvocationList(); |
| | 22 | 674 | | for (int i = 0; i < handlerDelegates.Length; i++) |
| | | 675 | | { |
| | | 676 | | try |
| | | 677 | | { |
| | 6 | 678 | | ((Action<OccupantEventInfo>)handlerDelegates[i])(eventInfo); |
| | 5 | 679 | | } |
| | 1 | 680 | | catch (Exception ex) |
| | | 681 | | { |
| | 1 | 682 | | GridForgeLogger.Channel.Error($"[Voxel {targetVoxel.WorldIndex}] Occupant add error: {ex.Message}"); |
| | 1 | 683 | | } |
| | | 684 | | } |
| | | 685 | | } |
| | | 686 | | |
| | 669 | 687 | | targetVoxel.NotifyOccupantAdded(eventInfo); |
| | 669 | 688 | | } |
| | | 689 | | |
| | | 690 | | /// <summary> |
| | | 691 | | /// Notifies listeners that an occupant was removed. |
| | | 692 | | /// </summary> |
| | | 693 | | private static void NotifyOccupantRemoved( |
| | | 694 | | Voxel targetVoxel, |
| | | 695 | | IVoxelOccupant occupant, |
| | | 696 | | OccupantTicket ticket, |
| | | 697 | | byte occupantCount) |
| | | 698 | | { |
| | 155 | 699 | | OccupantEventInfo eventInfo = new(targetVoxel.WorldIndex, occupant, ticket, occupantCount); |
| | 155 | 700 | | Action<OccupantEventInfo>? handlers = _onOccupantRemoved; |
| | 155 | 701 | | if (handlers != null) |
| | | 702 | | { |
| | 3 | 703 | | var handlerDelegates = handlers.GetInvocationList(); |
| | 14 | 704 | | for (int i = 0; i < handlerDelegates.Length; i++) |
| | | 705 | | { |
| | | 706 | | try |
| | | 707 | | { |
| | 4 | 708 | | ((Action<OccupantEventInfo>)handlerDelegates[i])(eventInfo); |
| | 3 | 709 | | } |
| | 1 | 710 | | catch (Exception ex) |
| | | 711 | | { |
| | 1 | 712 | | GridForgeLogger.Channel.Error($"[Voxel {targetVoxel.WorldIndex}] Occupant remove error: {ex.Message} |
| | 1 | 713 | | } |
| | | 714 | | } |
| | | 715 | | } |
| | | 716 | | |
| | 155 | 717 | | targetVoxel.NotifyOccupantRemoved(eventInfo); |
| | 155 | 718 | | } |
| | | 719 | | |
| | | 720 | | private static WorldOccupancyRegistry GetWorldRegistry(GridWorld world) |
| | | 721 | | { |
| | 679 | 722 | | return _occupancyRegistries.GetOrAdd(world, static _ => new WorldOccupancyRegistry()); |
| | | 723 | | } |
| | | 724 | | |
| | | 725 | | private static bool TryGetWorldRegistry(GridWorld world, out WorldOccupancyRegistry? registry) |
| | | 726 | | { |
| | 1574 | 727 | | registry = null; |
| | 1574 | 728 | | return _occupancyRegistries.TryGetValue(world, out registry); |
| | | 729 | | } |
| | | 730 | | |
| | | 731 | | #endregion |
| | | 732 | | } |