| | | 1 | | //======================================================================= |
| | | 2 | | // RetainedPartitionLifecycle.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2026-present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using GridForge.Grids; |
| | | 9 | | using GridForge.Spatial; |
| | | 10 | | using SwiftCollections; |
| | | 11 | | using System; |
| | | 12 | | |
| | | 13 | | namespace Gravitas; |
| | | 14 | | |
| | | 15 | | internal interface IRetainedPhysicsPartition<in TOwner> : IVoxelPartition |
| | | 16 | | { |
| | | 17 | | int RetainedIndex { get; } |
| | | 18 | | |
| | | 19 | | bool IsEmpty { get; } |
| | | 20 | | |
| | | 21 | | bool IsAllocated { get; } |
| | | 22 | | |
| | | 23 | | int EmptySinceFrame { get; } |
| | | 24 | | |
| | | 25 | | bool IsOwnedBy(TOwner owner); |
| | | 26 | | |
| | | 27 | | void SetRetainedIndex(int index); |
| | | 28 | | |
| | | 29 | | void ClearRetainedIndex(); |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | internal static class RetainedPartitionLifecycle |
| | | 33 | | { |
| | | 34 | | internal static void DetachAll<TPartition, TOwner>( |
| | | 35 | | SwiftList<TPartition> retainedPartitions, |
| | | 36 | | GridWorld world, |
| | | 37 | | TOwner owner, |
| | | 38 | | Action<TPartition> releasePartition, |
| | | 39 | | string partitionName, |
| | | 40 | | string detachError) |
| | | 41 | | where TPartition : class, IRetainedPhysicsPartition<TOwner> |
| | | 42 | | { |
| | | 43 | | // Reset is a context boundary; retained GridForge payloads are a runtime cache, not replay state. |
| | 735 | 44 | | while (retainedPartitions.Count > 0) |
| | | 45 | | { |
| | 640 | 46 | | TPartition partition = retainedPartitions[retainedPartitions.Count - 1]; |
| | 640 | 47 | | if (!partition.IsOwnedBy(owner)) |
| | | 48 | | { |
| | 1 | 49 | | int disabledCursor = -1; |
| | 1 | 50 | | Untrack(retainedPartitions, owner, partition, ref disabledCursor); |
| | 1 | 51 | | continue; |
| | | 52 | | } |
| | | 53 | | |
| | 639 | 54 | | if (world.TryGetVoxel(partition.WorldIndex, out Voxel? voxel) |
| | 639 | 55 | | && voxel!.TryGetPartition(out TPartition? attachedPartition) |
| | 639 | 56 | | && ReferenceEquals(attachedPartition, partition)) |
| | | 57 | | { |
| | 638 | 58 | | bool removed = voxel.TryRemovePartition<TPartition>(); |
| | 638 | 59 | | SwiftThrowHelper.ThrowIfTrue(!removed, partitionName, detachError); |
| | | 60 | | |
| | 638 | 61 | | if (partition.IsOwnedBy(owner)) |
| | 1 | 62 | | releasePartition(partition); |
| | | 63 | | |
| | 1 | 64 | | continue; |
| | | 65 | | } |
| | | 66 | | |
| | 1 | 67 | | releasePartition(partition); |
| | | 68 | | } |
| | 95 | 69 | | } |
| | | 70 | | |
| | | 71 | | internal static void Track<TPartition, TOwner>( |
| | | 72 | | SwiftList<TPartition> retainedPartitions, |
| | | 73 | | TOwner owner, |
| | | 74 | | TPartition partition, |
| | | 75 | | string partitionName) |
| | | 76 | | where TPartition : class, IRetainedPhysicsPartition<TOwner> |
| | | 77 | | { |
| | 493576 | 78 | | SwiftThrowHelper.ThrowIfNull(partition, nameof(partition)); |
| | 493576 | 79 | | SwiftThrowHelper.ThrowIfArgument( |
| | 493576 | 80 | | partition.RetainedIndex >= 0, |
| | 493576 | 81 | | nameof(partition), |
| | 493576 | 82 | | $"{partitionName} is already tracked as retained."); |
| | | 83 | | |
| | 493575 | 84 | | partition.SetRetainedIndex(retainedPartitions.Count); |
| | 493575 | 85 | | retainedPartitions.Add(partition); |
| | 493575 | 86 | | } |
| | | 87 | | |
| | | 88 | | internal static void Untrack<TPartition, TOwner>( |
| | | 89 | | SwiftList<TPartition> retainedPartitions, |
| | | 90 | | TOwner owner, |
| | | 91 | | TPartition partition, |
| | | 92 | | ref int retirementCursor) |
| | | 93 | | where TPartition : class, IRetainedPhysicsPartition<TOwner> |
| | | 94 | | { |
| | 493586 | 95 | | int index = FindIndex(retainedPartitions, owner, partition); |
| | 493586 | 96 | | if (index < 0) |
| | | 97 | | { |
| | 22 | 98 | | partition.ClearRetainedIndex(); |
| | 22 | 99 | | return; |
| | | 100 | | } |
| | | 101 | | |
| | 493564 | 102 | | int lastIndex = retainedPartitions.Count - 1; |
| | 493564 | 103 | | if (index != lastIndex) |
| | | 104 | | { |
| | 309998 | 105 | | TPartition movedPartition = retainedPartitions[lastIndex]; |
| | 309998 | 106 | | retainedPartitions[index] = movedPartition; |
| | 309998 | 107 | | movedPartition.SetRetainedIndex(index); |
| | | 108 | | } |
| | | 109 | | |
| | 493564 | 110 | | retainedPartitions.RemoveAt(lastIndex); |
| | 493564 | 111 | | partition.ClearRetainedIndex(); |
| | | 112 | | |
| | 493564 | 113 | | if (retirementCursor < 0) |
| | 2 | 114 | | return; |
| | | 115 | | |
| | 493562 | 116 | | if (retirementCursor > index) |
| | 29215 | 117 | | retirementCursor--; |
| | 493562 | 118 | | if (retirementCursor >= retainedPartitions.Count) |
| | 8176 | 119 | | retirementCursor = 0; |
| | 493562 | 120 | | } |
| | | 121 | | |
| | | 122 | | internal static void RetireExpired<TPartition, TOwner>( |
| | | 123 | | SwiftList<TPartition> retainedPartitions, |
| | | 124 | | GridWorld world, |
| | | 125 | | TOwner owner, |
| | | 126 | | int budget, |
| | | 127 | | int currentFrame, |
| | | 128 | | int timeToKillFrames, |
| | | 129 | | Action<TPartition> releasePartition, |
| | | 130 | | ref int retirementCursor) |
| | | 131 | | where TPartition : class, IRetainedPhysicsPartition<TOwner> |
| | | 132 | | { |
| | 3533 | 133 | | if (budget <= 0 || retainedPartitions.Count == 0) |
| | 38 | 134 | | return; |
| | | 135 | | |
| | 3495 | 136 | | int inspected = 0; |
| | 230326 | 137 | | while (inspected < budget && retainedPartitions.Count > 0) |
| | | 138 | | { |
| | 226831 | 139 | | if (retirementCursor >= retainedPartitions.Count) |
| | 8805 | 140 | | retirementCursor = 0; |
| | | 141 | | |
| | 226831 | 142 | | TPartition partition = retainedPartitions[retirementCursor]; |
| | 226831 | 143 | | inspected++; |
| | | 144 | | |
| | 226831 | 145 | | if (!ShouldRetire(partition, owner, currentFrame, timeToKillFrames)) |
| | | 146 | | { |
| | 226629 | 147 | | retirementCursor++; |
| | 226629 | 148 | | continue; |
| | | 149 | | } |
| | | 150 | | |
| | 202 | 151 | | Retire(world, owner, partition, releasePartition); |
| | | 152 | | } |
| | 3495 | 153 | | } |
| | | 154 | | |
| | | 155 | | internal static bool TryRetireEmptyForReuse<TPartition, TOwner>( |
| | | 156 | | SwiftList<TPartition> retainedPartitions, |
| | | 157 | | SwiftStack<TPartition> inactivePartitionPool, |
| | | 158 | | GridWorld world, |
| | | 159 | | TOwner owner, |
| | | 160 | | Action<TPartition> releasePartition, |
| | | 161 | | ref int retirementCursor) |
| | | 162 | | where TPartition : class, IRetainedPhysicsPartition<TOwner> |
| | | 163 | | { |
| | 493476 | 164 | | int inspected = 0; |
| | 2147483647 | 165 | | while (inspected < retainedPartitions.Count && retainedPartitions.Count > 0) |
| | | 166 | | { |
| | 2147483647 | 167 | | if (retirementCursor >= retainedPartitions.Count) |
| | 434942 | 168 | | retirementCursor = 0; |
| | | 169 | | |
| | 2147483647 | 170 | | TPartition partition = retainedPartitions[retirementCursor]; |
| | 2147483647 | 171 | | inspected++; |
| | | 172 | | |
| | 2147483647 | 173 | | if (!partition.IsOwnedBy(owner) || !partition.IsEmpty || partition.IsAllocated) |
| | | 174 | | { |
| | 2147483647 | 175 | | retirementCursor++; |
| | 2147483647 | 176 | | continue; |
| | | 177 | | } |
| | | 178 | | |
| | 45563 | 179 | | int poolCount = inactivePartitionPool.Count; |
| | 45563 | 180 | | Retire(world, owner, partition, releasePartition); |
| | 45563 | 181 | | if (inactivePartitionPool.Count > poolCount) |
| | 45562 | 182 | | return true; |
| | | 183 | | |
| | 1 | 184 | | retirementCursor++; |
| | | 185 | | } |
| | | 186 | | |
| | 447914 | 187 | | return false; |
| | | 188 | | } |
| | | 189 | | |
| | | 190 | | private static int FindIndex<TPartition, TOwner>( |
| | | 191 | | SwiftList<TPartition> retainedPartitions, |
| | | 192 | | TOwner owner, |
| | | 193 | | TPartition partition) |
| | | 194 | | where TPartition : class, IRetainedPhysicsPartition<TOwner> |
| | | 195 | | { |
| | | 196 | | _ = owner; |
| | 493586 | 197 | | int index = partition.RetainedIndex; |
| | 493586 | 198 | | return (uint)index < (uint)retainedPartitions.Count && ReferenceEquals(retainedPartitions[index], partition) |
| | 493586 | 199 | | ? index |
| | 493586 | 200 | | : -1; |
| | | 201 | | } |
| | | 202 | | |
| | | 203 | | private static bool ShouldRetire<TPartition, TOwner>( |
| | | 204 | | TPartition partition, |
| | | 205 | | TOwner owner, |
| | | 206 | | int currentFrame, |
| | | 207 | | int timeToKillFrames) |
| | | 208 | | where TPartition : class, IRetainedPhysicsPartition<TOwner> |
| | | 209 | | { |
| | 226831 | 210 | | if (!partition.IsOwnedBy(owner) || !partition.IsEmpty || partition.IsAllocated || partition.EmptySinceFrame < 0) |
| | 211327 | 211 | | return false; |
| | | 212 | | |
| | 15504 | 213 | | int idleFrames = currentFrame - partition.EmptySinceFrame; |
| | 15504 | 214 | | return idleFrames >= timeToKillFrames; |
| | | 215 | | } |
| | | 216 | | |
| | | 217 | | private static void Retire<TPartition, TOwner>( |
| | | 218 | | GridWorld world, |
| | | 219 | | TOwner owner, |
| | | 220 | | TPartition partition, |
| | | 221 | | Action<TPartition> releasePartition) |
| | | 222 | | where TPartition : class, IRetainedPhysicsPartition<TOwner> |
| | | 223 | | { |
| | | 224 | | _ = owner; |
| | 45765 | 225 | | if (!world.TryGetVoxel(partition.WorldIndex, out Voxel? voxel)) |
| | | 226 | | { |
| | 3 | 227 | | releasePartition(partition); |
| | 3 | 228 | | return; |
| | | 229 | | } |
| | | 230 | | |
| | 45762 | 231 | | if (!voxel!.TryGetPartition(out TPartition? attachedPartition) |
| | 45762 | 232 | | || !ReferenceEquals(attachedPartition, partition)) |
| | | 233 | | { |
| | 2 | 234 | | releasePartition(partition); |
| | 2 | 235 | | return; |
| | | 236 | | } |
| | | 237 | | |
| | 45760 | 238 | | _ = voxel.TryRemovePartition<TPartition>(); |
| | 45760 | 239 | | } |
| | | 240 | | } |