Move body and orbit key ownership into zone runtime

This commit is contained in:
Meta Vens 2026-06-16 14:22:52 +01:00
commit 0471cb690f
7 changed files with 105 additions and 32 deletions

View file

@ -354,9 +354,8 @@ public class ActionGameManager : MonoBehaviour
if (string.IsNullOrWhiteSpace(bodyKey))
return Guid.Empty;
const string bodyPrefix = "aetheria.body:legacy:";
var runtimeBodyId = bodyKey.StartsWith(bodyPrefix, StringComparison.OrdinalIgnoreCase)
? bodyKey.Substring(bodyPrefix.Length)
var runtimeBodyId = bodyKey.StartsWith(Zone.BodyKeyPrefix, StringComparison.OrdinalIgnoreCase)
? bodyKey.Substring(Zone.BodyKeyPrefix.Length)
: bodyKey;
return Guid.TryParse(runtimeBodyId, out var id) ? id : Guid.Empty;
}
@ -620,7 +619,7 @@ public class ActionGameManager : MonoBehaviour
EntranceZoneIndex = ZoneIndex(CurrentGalaxy?.Entrance),
ExitZoneIndex = ZoneIndex(CurrentGalaxy?.Exit),
CurrentZoneIndex = ZoneIndex(Zone?.GalaxyZone),
CurrentZoneEntityIndex = entityGraph.IndexOf(CurrentEntity),
CurrentZoneEntityIndex = IndexOfEntity(entityGraph, CurrentEntity),
GenerationSeed = CurrentGalaxy?.GenerationSeed ?? 0,
DiscoveredZoneIndices = CurrentGalaxy?.DiscoveredZones
.Select(ZoneIndex)
@ -750,8 +749,8 @@ public class ActionGameManager : MonoBehaviour
.OrderBy(orbit => orbit.ID)
.Select(orbit => new AetheriaRuntimeOrbitSnapshotCommit
{
OrbitKey = OrbitKey(orbit.ID),
ParentOrbitKey = OrbitKey(orbit.Parent),
OrbitKey = orbit.OrbitKey,
ParentOrbitKey = orbit.ParentOrbitKey,
Distance = orbit.Distance,
Phase = orbit.Phase,
FixedPositionX = orbit.FixedPosition.x,
@ -776,10 +775,10 @@ public class ActionGameManager : MonoBehaviour
{
return new AetheriaRuntimeBodySnapshotCommit
{
BodyKey = BodyKey(planet.ID),
BodyKey = planet.BodyKey,
Kind = BodyKind(planet),
Name = planet.Name,
OrbitKey = OrbitKey(planet.OrbitId),
OrbitKey = planet.OrbitKey,
Mass = planet.Mass,
Resources = planet.Resources?
.OrderBy(pair => pair.Key)
@ -807,10 +806,10 @@ public class ActionGameManager : MonoBehaviour
{
return new AetheriaRuntimeBodySnapshotCommit
{
BodyKey = BodyKey(belt.ID),
BodyKey = belt.BodyKey,
Kind = "asteroid_belt",
Name = belt.Name,
OrbitKey = OrbitKey(belt.Orbit),
OrbitKey = belt.OrbitKey,
Mass = belt.Mass,
Resources = belt.Resources?
.OrderBy(pair => pair.Key)
@ -944,7 +943,7 @@ public class ActionGameManager : MonoBehaviour
DirectionY = entity.Direction.y,
VelocityX = entity.Velocity.x,
VelocityY = entity.Velocity.y,
TargetEntityIndex = entity.Target?.Value == null ? -1 : entityGraph.IndexOf(entity.Target.Value),
TargetEntityIndex = entity.Target?.Value == null ? -1 : IndexOfEntity(entityGraph, entity.Target.Value),
IsActive = entity.Active,
HeatsinksEnabled = entity.HeatsinksEnabled,
OverrideShutdown = entity.OverrideShutdown,
@ -965,7 +964,7 @@ public class ActionGameManager : MonoBehaviour
VisibilitySourceCount = entity.VisibilitySources.Count,
Contacts = ProjectEntityContacts(entityGraph, entity),
ChildEntityIndices = entity.Children?
.Select(child => entityGraph.IndexOf(child))
.Select(child => IndexOfEntity(entityGraph, child))
.Where(index => index >= 0)
.ToArray() ?? Array.Empty<int>(),
WeaponGroups = entity.WeaponGroups?
@ -984,7 +983,7 @@ public class ActionGameManager : MonoBehaviour
return entity.EntityInfoGathered
.Select(contact =>
{
var targetIndex = entityGraph.IndexOf(contact.Key);
var targetIndex = IndexOfEntity(entityGraph, contact.Key);
entity.EntityHostility.TryGetValue(contact.Key, out var hostile);
return new AetheriaRuntimeEntityContactCommit
{
@ -998,6 +997,20 @@ public class ActionGameManager : MonoBehaviour
.ToArray();
}
private static int IndexOfEntity(IReadOnlyList<Entity> entityGraph, Entity entity)
{
if (entityGraph == null || entity == null)
return -1;
for (var index = 0; index < entityGraph.Count; index++)
{
if (ReferenceEquals(entityGraph[index], entity))
return index;
}
return -1;
}
private static AetheriaRuntimeCargoBayLoadoutCommit[] ProjectRuntimeCargoBays<TBay>(IEnumerable<TBay> bays)
where TBay : EquippedCargoBay
{
@ -1019,18 +1032,20 @@ public class ActionGameManager : MonoBehaviour
private static AetheriaRuntimeBehaviorStateCommit[] ProjectBehaviorStates(Entity entity)
{
var states = new List<AetheriaRuntimeBehaviorStateCommit>();
var zone = entity.Zone;
for (var ownerIndex = 0; ownerIndex < entity.Equipment.Count; ownerIndex++)
states.AddRange(ProjectBehaviorStates("equipment", ownerIndex, entity.Equipment[ownerIndex].Behaviors));
states.AddRange(ProjectBehaviorStates(zone, "equipment", ownerIndex, entity.Equipment[ownerIndex].Behaviors));
var activeConsumables = entity.ActiveConsumables;
for (var ownerIndex = 0; ownerIndex < activeConsumables.Count; ownerIndex++)
states.AddRange(ProjectBehaviorStates("active_consumable", ownerIndex, activeConsumables[ownerIndex].Behaviors));
states.AddRange(ProjectBehaviorStates(zone, "active_consumable", ownerIndex, activeConsumables[ownerIndex].Behaviors));
return states.ToArray();
}
private static IEnumerable<AetheriaRuntimeBehaviorStateCommit> ProjectBehaviorStates(
Zone zone,
string ownerKind,
int ownerIndex,
IReadOnlyList<Behavior> behaviors)
@ -1104,7 +1119,7 @@ public class ActionGameManager : MonoBehaviour
{
state = new AetheriaRuntimeBehaviorStateCommit
{
ResourceScannerTargetBodyKey = BodyKey(resourceScanner.ScanTarget),
ResourceScannerTargetBodyKey = zone?.GetBodyKey(resourceScanner.ScanTarget) ?? "",
ResourceScannerAsteroidIndex = resourceScanner.Asteroid,
ResourceScannerScanTime = resourceScanner.ScanTime,
ResourceScannerRange = resourceScanner.Range,
@ -1116,7 +1131,7 @@ public class ActionGameManager : MonoBehaviour
{
state = new AetheriaRuntimeBehaviorStateCommit
{
MiningToolAsteroidBeltKey = BodyKey(miningTool.AsteroidBelt),
MiningToolAsteroidBeltKey = zone?.GetBodyKey(miningTool.AsteroidBelt) ?? "",
MiningToolAsteroidIndex = miningTool.Asteroid,
MiningToolRange = miningTool.Range
};
@ -1260,7 +1275,7 @@ public class ActionGameManager : MonoBehaviour
if (weapon is LockWeapon lockWeapon)
{
state.LockProgress = lockWeapon.LockProgress;
state.LockTargetEntityIndex = lockWeapon.LockTarget == null ? -1 : entityGraph.IndexOf(lockWeapon.LockTarget);
state.LockTargetEntityIndex = lockWeapon.LockTarget == null ? -1 : IndexOfEntity(entityGraph, lockWeapon.LockTarget);
}
yield return state;
@ -1389,16 +1404,6 @@ public class ActionGameManager : MonoBehaviour
.ToArray() ?? Array.Empty<AetheriaRuntimeLoadoutItemSlotCommit>();
}
private static string OrbitKey(Guid id)
{
return id == Guid.Empty ? "" : $"aetheria.orbit:legacy:{id:D}";
}
private static string BodyKey(Guid id)
{
return id == Guid.Empty ? "" : $"aetheria.body:legacy:{id:D}";
}
private static int ZoneIndex(GalaxyZone zone)
{
return CurrentGalaxy?.Zones == null || zone == null ? -1 : Array.IndexOf(CurrentGalaxy.Zones, zone);

View file

@ -16,6 +16,9 @@ using Random = Unity.Mathematics.Random;
public class Zone
{
public const string OrbitKeyPrefix = "aetheria.orbit:legacy:";
public const string BodyKeyPrefix = "aetheria.body:legacy:";
public Action<string> Log;
public ReactiveCollection<Entity> Entities = new ReactiveCollection<Entity>();
public Dictionary<Guid, Planet> PlanetInstances = new Dictionary<Guid, Planet>();
@ -102,6 +105,27 @@ public class Zone
return agent;
}
public static string OrbitKey(Guid id)
{
return id == Guid.Empty ? "" : $"{OrbitKeyPrefix}{id:D}";
}
public static string BodyKey(Guid id)
{
return id == Guid.Empty ? "" : $"{BodyKeyPrefix}{id:D}";
}
public string GetBodyKey(Guid bodyId)
{
if (bodyId == Guid.Empty)
return "";
if (PlanetInstances.TryGetValue(bodyId, out var planet))
return planet.BodyKey;
if (AsteroidBelts.TryGetValue(bodyId, out var belt))
return belt.BodyKey;
return BodyKey(bodyId);
}
public void Update(float deltaTime)
{
_time += deltaTime;
@ -367,8 +391,10 @@ public class Planet
public Orbit Orbit;
protected readonly PlanetSettings Settings;
public Guid ID { get; }
public string BodyKey { get; }
public string Name { get; }
public Guid OrbitId { get; }
public string OrbitKey { get; }
public float Mass { get; }
public IReadOnlyDictionary<string, float> Resources { get; }
public float BodyRadiusMultiplier { get; }
@ -384,8 +410,10 @@ public class Planet
Settings = settings;
Orbit = orbit;
ID = data.ID;
BodyKey = Zone.BodyKey(data.ID);
Name = data.Name ?? "";
OrbitId = data.Orbit;
OrbitKey = Zone.OrbitKey(data.Orbit);
Mass = data.Mass;
Resources = data.Resources;
BodyRadiusMultiplier = data.BodyRadiusMultiplier;
@ -472,8 +500,10 @@ public class AsteroidBelt
{
private readonly Asteroid[] _asteroids;
public Guid ID { get; }
public string BodyKey { get; }
public string Name { get; }
public Guid Orbit { get; }
public string OrbitKey { get; }
public float Mass { get; }
public IReadOnlyDictionary<string, float> Resources { get; }
public float BodyRadiusMultiplier { get; }
@ -493,8 +523,10 @@ public class AsteroidBelt
{
_asteroids = data.Asteroids;
ID = data.ID;
BodyKey = Zone.BodyKey(data.ID);
Name = data.Name ?? "";
Orbit = data.Orbit;
OrbitKey = Zone.OrbitKey(data.Orbit);
Mass = data.Mass;
Resources = data.Resources;
BodyRadiusMultiplier = data.BodyRadiusMultiplier;
@ -517,7 +549,9 @@ public class AsteroidBelt
public class Orbit
{
public Guid ID { get; }
public string OrbitKey { get; }
public Guid Parent { get; }
public string ParentOrbitKey { get; }
public float Distance { get; }
public float Phase { get; }
public float2 FixedPosition { get; }
@ -535,7 +569,9 @@ public class Orbit
public Orbit(PlanetSettings settings, OrbitConstructionData data)
{
ID = data.ID;
OrbitKey = Zone.OrbitKey(data.ID);
Parent = data.Parent;
ParentOrbitKey = Zone.OrbitKey(data.Parent);
Distance = data.Distance;
Phase = data.Phase;
FixedPosition = data.FixedPosition;

View file

@ -2,6 +2,7 @@ using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using GameCult.Aetheria.State.Unity;
@ -209,7 +210,6 @@ public class MainMenu : MonoBehaviour
ActionGameManager.IsTutorial = run.IsTutorial;
ActionGameManager.ContinueRunState = run;
var generationSeed = run.GenerationSeed == 0 ? NextGenerationSeed() : run.GenerationSeed;
var sectorSettings = run.IsTutorial ? Settings.TutorialGenerationSettings : Settings.SectorGenerationSettings;
var backgroundSettings = run.IsTutorial ? Settings.TutorialBackgroundSettings : Settings.SectorBackgroundSettings;
backgroundSettings.NoisePosition = generationSeed;
@ -227,7 +227,7 @@ public class MainMenu : MonoBehaviour
setState,
generationSeed)
: new Galaxy(
sectorSettings,
Settings.SectorGenerationSettings,
backgroundSettings,
Settings.NameGeneratorSettings,
ActionGameManager.RuntimeCatalog,