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