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