| | | 1 | | using FixedMathSharp; |
| | | 2 | | using SwiftCollections; |
| | | 3 | | using System; |
| | | 4 | | using System.Runtime.CompilerServices; |
| | | 5 | | |
| | | 6 | | namespace Trailblazer.Navigation.MovementGroups; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Tracks movement-group membership and resolves formation-preserving destinations for one world context. |
| | | 10 | | /// </summary> |
| | | 11 | | internal sealed class MovementGroupCoordinatorState |
| | | 12 | | { |
| | | 13 | | private const int MinMovementGroupSize = 2; |
| | | 14 | | |
| | | 15 | | private const int MovementGroupHistoryFrames = 1; |
| | | 16 | | |
| | | 17 | | private readonly TrailblazerWorldContext _context; |
| | | 18 | | |
| | 961 | 19 | | private readonly SwiftDictionary<int, MovementGroupState> _movementGroups = new(); |
| | | 20 | | |
| | 961 | 21 | | private readonly SwiftDictionary<Guid, MovementGroupMembership> _movementGroupMemberships = new(); |
| | | 22 | | |
| | 961 | 23 | | internal MovementGroupCoordinatorState(TrailblazerWorldContext context) |
| | | 24 | | { |
| | 961 | 25 | | _context = context ?? throw new ArgumentNullException(nameof(context)); |
| | 961 | 26 | | } |
| | | 27 | | |
| | 210 | 28 | | private int FrameCount => _context.FrameCount; |
| | | 29 | | |
| | 28 | 30 | | private Fixed64 VoxelSize => _context.VoxelSize; |
| | | 31 | | |
| | | 32 | | internal void CacheOwner(MovementGroupSession session, Guid ownerId) |
| | | 33 | | { |
| | 60 | 34 | | if (session.HasOwnerId && session.OwnerId == ownerId) |
| | 8 | 35 | | return; |
| | | 36 | | |
| | 52 | 37 | | if (session.HasOwnerId) |
| | 1 | 38 | | _movementGroupMemberships.Remove(session.OwnerId); |
| | | 39 | | |
| | 52 | 40 | | session.OwnerId = ownerId; |
| | 52 | 41 | | session.HasOwnerId = true; |
| | 52 | 42 | | } |
| | | 43 | | |
| | | 44 | | internal void Prewarm( |
| | | 45 | | MovementGroupSession session, |
| | | 46 | | Guid ownerId, |
| | | 47 | | Vector3d requestedDestination, |
| | | 48 | | Vector3d position, |
| | | 49 | | Fixed64 radius) |
| | | 50 | | { |
| | 12 | 51 | | if (session.GroupId < 0) |
| | 1 | 52 | | return; |
| | | 53 | | |
| | 11 | 54 | | CacheOwner(session, ownerId); |
| | 11 | 55 | | UpdateTarget(session, requestedDestination, position, radius); |
| | 11 | 56 | | } |
| | | 57 | | |
| | | 58 | | internal MovementGroupTarget UpdateTarget( |
| | | 59 | | MovementGroupSession session, |
| | | 60 | | Vector3d requestedDestination, |
| | | 61 | | Vector3d position, |
| | | 62 | | Fixed64 radius, |
| | | 63 | | bool resetFormationOffset = false) |
| | | 64 | | { |
| | 105 | 65 | | if (session.GroupId < 0) |
| | 0 | 66 | | return new(MovementGroupTravelMode.None, requestedDestination); |
| | | 67 | | |
| | 105 | 68 | | MovementGroupState group = GetOrCreateGroup(session.GroupId); |
| | 105 | 69 | | MovementGroupMember self = GetOrCreateMember(session, group, ref resetFormationOffset); |
| | | 70 | | |
| | 105 | 71 | | if (self.RequestedDestination != requestedDestination) |
| | 65 | 72 | | resetFormationOffset = true; |
| | | 73 | | |
| | 105 | 74 | | UpdateMemberState(self, requestedDestination, position, radius, resetFormationOffset); |
| | 105 | 75 | | UpdateMembership(session, self, requestedDestination); |
| | | 76 | | |
| | 105 | 77 | | int minFrame = FrameCount - MovementGroupHistoryFrames; |
| | 105 | 78 | | if (!TryGetFormationMetrics(group, requestedDestination, minFrame, out Vector3d groupCenter, out Fixed64 average |
| | 77 | 79 | | return new(MovementGroupTravelMode.Individual, requestedDestination); |
| | | 80 | | |
| | 28 | 81 | | Fixed64 maxSpreadSq = UpdateFormationOffsets(group, requestedDestination, minFrame, groupCenter); |
| | 28 | 82 | | Fixed64 allowedSpreadSq = averageRadius * averageRadius * (Fixed64)(groupCount * 2); |
| | 28 | 83 | | Fixed64 distanceToSharedDestinationSq = (requestedDestination - groupCenter).SqrMagnitude; |
| | 28 | 84 | | if (maxSpreadSq > allowedSpreadSq || distanceToSharedDestinationSq <= maxSpreadSq) |
| | 5 | 85 | | return new(MovementGroupTravelMode.GroupIndividual, requestedDestination); |
| | | 86 | | |
| | 23 | 87 | | return new(MovementGroupTravelMode.Formation, requestedDestination + self.FormationOffset); |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | internal void Remove(MovementGroupSession session) |
| | | 91 | | { |
| | 329 | 92 | | if (session.GroupId < 0) |
| | 318 | 93 | | return; |
| | | 94 | | |
| | 11 | 95 | | if (_movementGroups.TryGetValue(session.GroupId, out MovementGroupState group)) |
| | | 96 | | { |
| | 10 | 97 | | if (group.Members.TryGetValue(session.GroupIndex, out MovementGroupMember member) && member.HasOccupantId) |
| | 1 | 98 | | _movementGroupMemberships.Remove(member.OccupantId); |
| | | 99 | | |
| | 10 | 100 | | group.Members.TryRemoveAt(session.GroupIndex); |
| | 10 | 101 | | if (group.Members.Count == 0) |
| | 4 | 102 | | _movementGroups.Remove(session.GroupId); |
| | | 103 | | } |
| | 1 | 104 | | else if (session.HasOwnerId) |
| | | 105 | | { |
| | 1 | 106 | | _movementGroupMemberships.Remove(session.OwnerId); |
| | | 107 | | } |
| | | 108 | | |
| | 11 | 109 | | session.GroupIndex = -1; |
| | 11 | 110 | | } |
| | | 111 | | |
| | | 112 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 113 | | internal bool IsNeighbor( |
| | | 114 | | MovementGroupSession session, |
| | | 115 | | Guid otherId, |
| | | 116 | | Vector3d requestedDestination, |
| | | 117 | | int currentFrame) |
| | | 118 | | { |
| | 17 | 119 | | if (session.GroupId < 0 || !_movementGroupMemberships.TryGetValue(otherId, out MovementGroupMembership membershi |
| | 7 | 120 | | return false; |
| | | 121 | | |
| | 10 | 122 | | return membership.GroupId == session.GroupId |
| | 10 | 123 | | && membership.RequestedDestination == requestedDestination |
| | 10 | 124 | | && membership.LastSeenFrame >= currentFrame - MovementGroupHistoryFrames; |
| | | 125 | | } |
| | | 126 | | |
| | | 127 | | internal void Reset() |
| | | 128 | | { |
| | 35 | 129 | | _movementGroups.Clear(); |
| | 35 | 130 | | _movementGroupMemberships.Clear(); |
| | 35 | 131 | | } |
| | | 132 | | |
| | | 133 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 134 | | private MovementGroupState GetOrCreateGroup(int groupId) |
| | | 135 | | { |
| | 105 | 136 | | if (_movementGroups.TryGetValue(groupId, out MovementGroupState group)) |
| | 63 | 137 | | return group; |
| | | 138 | | |
| | 42 | 139 | | group = new(); |
| | 42 | 140 | | _movementGroups[groupId] = group; |
| | 42 | 141 | | return group; |
| | | 142 | | } |
| | | 143 | | |
| | | 144 | | private static MovementGroupMember GetOrCreateMember( |
| | | 145 | | MovementGroupSession session, |
| | | 146 | | MovementGroupState group, |
| | | 147 | | ref bool resetFormationOffset) |
| | | 148 | | { |
| | 105 | 149 | | if (group.Members.TryGetValue(session.GroupIndex, out MovementGroupMember self)) |
| | 40 | 150 | | return self; |
| | | 151 | | |
| | 65 | 152 | | self = new(); |
| | 65 | 153 | | session.GroupIndex = group.Members.Add(self); |
| | 65 | 154 | | resetFormationOffset = true; |
| | 65 | 155 | | return self; |
| | | 156 | | } |
| | | 157 | | |
| | | 158 | | private void UpdateMemberState( |
| | | 159 | | MovementGroupMember self, |
| | | 160 | | Vector3d requestedDestination, |
| | | 161 | | Vector3d position, |
| | | 162 | | Fixed64 radius, |
| | | 163 | | bool resetFormationOffset) |
| | | 164 | | { |
| | 105 | 165 | | self.Position = position; |
| | 105 | 166 | | self.Radius = radius; |
| | 105 | 167 | | self.RequestedDestination = requestedDestination; |
| | 105 | 168 | | self.LastSeenFrame = FrameCount; |
| | | 169 | | |
| | 105 | 170 | | if (resetFormationOffset) |
| | 65 | 171 | | self.HasFormationOffset = false; |
| | 105 | 172 | | } |
| | | 173 | | |
| | | 174 | | private void UpdateMembership( |
| | | 175 | | MovementGroupSession session, |
| | | 176 | | MovementGroupMember self, |
| | | 177 | | Vector3d requestedDestination) |
| | | 178 | | { |
| | 105 | 179 | | if (!session.HasOwnerId) |
| | 44 | 180 | | return; |
| | | 181 | | |
| | 61 | 182 | | if (self.HasOccupantId && self.OccupantId != session.OwnerId) |
| | 1 | 183 | | _movementGroupMemberships.Remove(self.OccupantId); |
| | | 184 | | |
| | 61 | 185 | | self.OccupantId = session.OwnerId; |
| | 61 | 186 | | self.HasOccupantId = true; |
| | | 187 | | |
| | 61 | 188 | | _movementGroupMemberships[session.OwnerId] = new MovementGroupMembership |
| | 61 | 189 | | { |
| | 61 | 190 | | GroupId = session.GroupId, |
| | 61 | 191 | | RequestedDestination = requestedDestination, |
| | 61 | 192 | | LastSeenFrame = self.LastSeenFrame |
| | 61 | 193 | | }; |
| | 61 | 194 | | } |
| | | 195 | | |
| | | 196 | | private bool TryGetFormationMetrics( |
| | | 197 | | MovementGroupState group, |
| | | 198 | | Vector3d requestedDestination, |
| | | 199 | | int minFrame, |
| | | 200 | | out Vector3d groupCenter, |
| | | 201 | | out Fixed64 averageRadius, |
| | | 202 | | out int groupCount) |
| | | 203 | | { |
| | 105 | 204 | | groupCenter = Vector3d.Zero; |
| | 105 | 205 | | averageRadius = Fixed64.Zero; |
| | 105 | 206 | | groupCount = 0; |
| | | 207 | | |
| | 500 | 208 | | foreach (MovementGroupMember member in group.Members) |
| | | 209 | | { |
| | 145 | 210 | | if (!IsEligibleGroupMember(member, requestedDestination, minFrame)) |
| | | 211 | | continue; |
| | | 212 | | |
| | 133 | 213 | | groupCenter += member.Position; |
| | 133 | 214 | | averageRadius += member.Radius; |
| | 133 | 215 | | groupCount++; |
| | | 216 | | } |
| | | 217 | | |
| | 105 | 218 | | if (groupCount < MinMovementGroupSize) |
| | 77 | 219 | | return false; |
| | | 220 | | |
| | 28 | 221 | | groupCenter /= groupCount; |
| | 28 | 222 | | averageRadius = (averageRadius / groupCount) + (VoxelSize * Fixed64.Half); |
| | 28 | 223 | | return true; |
| | | 224 | | } |
| | | 225 | | |
| | | 226 | | private static Fixed64 UpdateFormationOffsets( |
| | | 227 | | MovementGroupState group, |
| | | 228 | | Vector3d requestedDestination, |
| | | 229 | | int minFrame, |
| | | 230 | | Vector3d groupCenter) |
| | | 231 | | { |
| | 28 | 232 | | Fixed64 maxSpreadSq = Fixed64.Zero; |
| | | 233 | | |
| | 168 | 234 | | foreach (MovementGroupMember member in group.Members) |
| | | 235 | | { |
| | 56 | 236 | | if (!IsEligibleGroupMember(member, requestedDestination, minFrame)) |
| | | 237 | | continue; |
| | | 238 | | |
| | 56 | 239 | | Vector3d formationOffset = member.Position - groupCenter; |
| | 56 | 240 | | if (!member.HasFormationOffset) |
| | | 241 | | { |
| | 34 | 242 | | member.FormationOffset = formationOffset; |
| | 34 | 243 | | member.HasFormationOffset = true; |
| | | 244 | | } |
| | | 245 | | |
| | 56 | 246 | | Fixed64 spreadSq = formationOffset.SqrMagnitude; |
| | 56 | 247 | | if (spreadSq > maxSpreadSq) |
| | 20 | 248 | | maxSpreadSq = spreadSq; |
| | | 249 | | } |
| | | 250 | | |
| | 28 | 251 | | return maxSpreadSq; |
| | | 252 | | } |
| | | 253 | | |
| | | 254 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 255 | | private static bool IsEligibleGroupMember(MovementGroupMember member, Vector3d requestedDestination, int minFrame) |
| | | 256 | | { |
| | 201 | 257 | | return member.LastSeenFrame >= minFrame |
| | 201 | 258 | | && member.RequestedDestination == requestedDestination; |
| | | 259 | | } |
| | | 260 | | } |