diff --git a/Aetheria.State.Verify/Program.cs b/Aetheria.State.Verify/Program.cs index 625d6c2b2..4d8bc67d3 100644 --- a/Aetheria.State.Verify/Program.cs +++ b/Aetheria.State.Verify/Program.cs @@ -1914,9 +1914,18 @@ static void RequireDaemonRenderQueryAuthority(string root) "ZoneRenderer frame updates must read asteroid belt pose and radius from daemon belt poses instead of mirrored Unity asteroid belts."); } + if (zoneRenderer.Contains("_zoneSubscriptions", StringComparison.Ordinal) || + zoneRenderer.Contains("zone.Entities.ObserveAdd()", StringComparison.Ordinal) || + zoneRenderer.Contains("zone.Entities.ObserveRemove()", StringComparison.Ordinal)) + { + throw new InvalidOperationException( + "ZoneRenderer must reconcile rendered entities from daemon frame snapshots instead of subscribing to mirrored Unity entity collection mutations."); + } + var requiredZoneRendererSymbols = new[] { "private AetheriaRuntimeZoneSnapshotCommit _daemonZoneSnapshot;", + "private readonly Dictionary _entityInstancesByDaemonKey", "private readonly List _daemonBodyPoses", "private readonly Dictionary _daemonBodyPosesByBodyKey", "private readonly List _daemonAsteroidBeltPoses", @@ -1927,6 +1936,9 @@ static void RequireDaemonRenderQueryAuthority(string root) "foreach (var pose in _daemonBodyPoses)", "zone.PlanetInstances.TryGetValue(pose.BodyKey, out var planet)", "zone.AsteroidBelts.TryGetValue(pose.BodyKey, out var belt)", + "foreach (var entitySnapshot in daemonZone?.Entities ?? Array.Empty())", + "entitiesByDaemonIndex.TryGetValue(entitySnapshot.EntityIndex, out var entity)", + "foreach (var entity in EntityInstances.Keys.ToArray())", "AetheriaRuntimeDaemonRenderQueries.QueryBodyPoses(_daemonZoneSnapshot, _daemonBodyPoses);", "AetheriaRuntimeDaemonRenderQueries.QueryAsteroidBeltPoses(_daemonZoneSnapshot, _daemonAsteroidBeltPoses);", "_daemonBodyPosesByBodyKey.TryGetValue(planet.Key, out var pose)", diff --git a/Assets/Scripts/Gameplay/ActionGameManager.cs b/Assets/Scripts/Gameplay/ActionGameManager.cs index 75a19b5ad..8ddff5a11 100644 --- a/Assets/Scripts/Gameplay/ActionGameManager.cs +++ b/Assets/Scripts/Gameplay/ActionGameManager.cs @@ -2693,6 +2693,8 @@ public class ActionGameManager : MonoBehaviour if (!_authoritativeDaemonEntities.TryGetValue(entitySnapshot.RecordKey, out var entity)) continue; + entity.DaemonRecordKey = entitySnapshot.RecordKey ?? ""; + entity.DaemonEntityIndex = EntityIndexFromRecordKey(entitySnapshot.RecordKey); entity.Name = entitySnapshot.Name ?? ""; entity.CultPosition = new CultMath.float3((float)entitySnapshot.PositionX, (float)entitySnapshot.PositionY, (float)entitySnapshot.PositionZ); entity.CultDirection = new CultMath.float2((float)entitySnapshot.DirectionX, (float)entitySnapshot.DirectionY); @@ -2766,6 +2768,8 @@ public class ActionGameManager : MonoBehaviour entity.OverrideShutdown = entitySnapshot.OverrideShutdown; entity.TractorPower = (float)entitySnapshot.TractorPower; entity.RestoreActiveState(entitySnapshot.IsActive); + entity.DaemonRecordKey = entitySnapshot.RecordKey ?? ""; + entity.DaemonEntityIndex = EntityIndexFromRecordKey(entitySnapshot.RecordKey); entity.Zone = Zone; Zone.Entities.Add(entity); restoredEntities[entitySnapshot.RecordKey] = entity; diff --git a/Assets/Scripts/ServerShared/Entity.cs b/Assets/Scripts/ServerShared/Entity.cs index e5e0ea629..2fcf45e14 100644 --- a/Assets/Scripts/ServerShared/Entity.cs +++ b/Assets/Scripts/ServerShared/Entity.cs @@ -43,6 +43,8 @@ public abstract class Entity public Faction Faction; public EquippableItem Hull; public EquippedItem EquippedHull; + public string DaemonRecordKey = ""; + public int DaemonEntityIndex = -1; public float3 Position; public float2 Direction = new float2(0,1); diff --git a/Assets/Scripts/Zone Display/ZoneRenderer.cs b/Assets/Scripts/Zone Display/ZoneRenderer.cs index afd4dc68b..5329f75ec 100644 --- a/Assets/Scripts/Zone Display/ZoneRenderer.cs +++ b/Assets/Scripts/Zone Display/ZoneRenderer.cs @@ -68,6 +68,8 @@ public class ZoneRenderer : MonoBehaviour [HideInInspector] public Dictionary EntityInstances = new Dictionary(); [HideInInspector] public Dictionary Planets = new Dictionary(StringComparer.Ordinal); + private readonly Dictionary _entityInstancesByDaemonKey = + new Dictionary(StringComparer.Ordinal); private Dictionary _beltObjects = new Dictionary(StringComparer.Ordinal); private Dictionary _beltMeshes = new Dictionary(StringComparer.Ordinal); private Dictionary _beltMatrices = new Dictionary(StringComparer.Ordinal); @@ -82,7 +84,6 @@ public class ZoneRenderer : MonoBehaviour private bool _rootFound; private Entity _perspectiveEntity; private IDisposable[] _perspectiveSubscriptions = new IDisposable[2]; - private List _zoneSubscriptions = new List(); private PlanetObject[] _suns; private bool _showAsteroidUI; private AetheriaRuntimeZoneSnapshotCommit _daemonZoneSnapshot; @@ -212,22 +213,21 @@ public class ZoneRenderer : MonoBehaviour _suns = Planets.Values.Where(p => p is SunObject).ToArray(); + var entitiesByDaemonIndex = new Dictionary(); foreach (var entity in zone.Entities) { - Debug.Log($"Loading entity {entity.Name} from existing zone entity collection"); - LoadEntity(entity); + if (entity != null && entity.DaemonEntityIndex >= 0) + entitiesByDaemonIndex[entity.DaemonEntityIndex] = entity; } - - _zoneSubscriptions.Add(zone.Entities.ObserveAdd().Subscribe(e => + foreach (var entitySnapshot in daemonZone?.Entities ?? Array.Empty()) { - //Debug.Log($"Loading entity {e.Value.Name} from zone add event"); - LoadEntity(e.Value); - })); - _zoneSubscriptions.Add(zone.Entities.ObserveRemove().Subscribe(e => - { - //Debug.Log($"Unloading entity {e.Value.Name} from zone remove event"); - UnloadEntity(e.Value); - })); + if (entitySnapshot != null && + entitiesByDaemonIndex.TryGetValue(entitySnapshot.EntityIndex, out var entity)) + { + Debug.Log($"Loading entity {entity.Name} from daemon entity snapshot {entitySnapshot.EntityIndex}"); + LoadEntity(entity); + } + } if (zone.GalaxyZone != null) { @@ -262,17 +262,13 @@ public class ZoneRenderer : MonoBehaviour } WormholeInstances.Clear(); - foreach (var subscription in _zoneSubscriptions) - subscription.Dispose(); - _zoneSubscriptions.Clear(); - foreach(var gridObject in _loot) if(gridObject) Destroy(gridObject.gameObject); _loot.Clear(); - foreach (var entity in Zone.Entities) + foreach (var entity in EntityInstances.Keys.ToArray()) { - Debug.Log($"Unloading entity {entity.Name} from entities remaining during clear!"); + Debug.Log($"Unloading entity {entity.Name} from rendered entity instances during clear."); UnloadEntity(entity); } @@ -333,6 +329,8 @@ public class ZoneRenderer : MonoBehaviour instance.SetEntity(this, entity); EntityInstances.Add(entity, instance); + if (!string.IsNullOrWhiteSpace(entity.DaemonRecordKey)) + _entityInstancesByDaemonKey[entity.DaemonRecordKey] = instance; } public void UnloadEntity(Entity entity) @@ -351,6 +349,8 @@ public class ZoneRenderer : MonoBehaviour Destroy(instance.gameObject); EntityInstances.Remove(entity); + if (!string.IsNullOrWhiteSpace(entity.DaemonRecordKey)) + _entityInstancesByDaemonKey.Remove(entity.DaemonRecordKey); } void LoadAsteroidBelt(AsteroidBelt runtimeBelt)