diff --git a/Assets/Scripts/ServerShared/Zone.cs b/Assets/Scripts/ServerShared/Zone.cs index 0be6b82b2..87dd238ad 100644 --- a/Assets/Scripts/ServerShared/Zone.cs +++ b/Assets/Scripts/ServerShared/Zone.cs @@ -739,28 +739,34 @@ public class Zone public float WellReference(Planet body) => GravityField.Potential(body.WellStrength, body.RocheLimit, Pack.Radius); // The extent of the populated area, from the zone's origin: owned here; presenters read it, nothing authors it. A static - // bound on where any body can be, so it holds at every time and reads nothing the pack's Radius decides: for each body, - // the root orbit's offset from the origin, plus every orbit Distance along its chain to the root, plus its Roche limit. + // bound on where anything can be, so it holds at every time and reads nothing the pack's Radius decides: for each body, + // its orbit chain's reach plus its Roche limit; for each belt, the chain's reach to the orbit its asteroids circle (the + // belt orbit's parent) plus the belt's own Radius, its largest asteroid Distance. public float PopulatedRadius { get; } private float ComputePopulatedRadius() { var radius = 0f; foreach (var body in PlanetInstances.Values) - { - var reach = body.RocheLimit; - var key = body.BodyData.Orbit.Key; - while (key.IsSet() && Orbits.TryGetValue(key, out var orbit)) - { - if (orbit.Data.Parent.IsSet()) reach += orbit.Data.Distance; - else reach += length(orbit.Data.FixedPosition); - key = orbit.Data.Parent.Key; - } - radius = max(radius, reach); - } + radius = max(radius, ChainReach(body.BodyData.Orbit.Key) + body.RocheLimit); + foreach (var belt in AsteroidBelts) + radius = max(radius, ChainReach(Orbits[Planets[belt.Key].Orbit.Key].Data.Parent.Key) + belt.Value.Radius); return radius; } + // The most an orbit can lie from the origin: the root's offset plus every orbit Distance down the chain to it. + private float ChainReach(CultRecordKey key) + { + var reach = 0f; + while (key.IsSet() && Orbits.TryGetValue(key, out var orbit)) + { + if (orbit.Data.Parent.IsSet()) reach += orbit.Data.Distance; + else reach += length(orbit.Data.FixedPosition); + key = orbit.Data.Parent.Key; + } + return reach; + } + public float GetHeight(float2 position) => GetHeight(position, _time); // The field at a time: each well sits where its body's rail puts it then. The sim reads it at _time, a presenter at diff --git a/tests/Aetheria.Shared.Tests/PopulatedRadiusTests.cs b/tests/Aetheria.Shared.Tests/PopulatedRadiusTests.cs index dc1f5e865..af645a68c 100644 --- a/tests/Aetheria.Shared.Tests/PopulatedRadiusTests.cs +++ b/tests/Aetheria.Shared.Tests/PopulatedRadiusTests.cs @@ -10,13 +10,16 @@ using float3 = CultMath.float3; // Zone.PopulatedRadius is the extent of the populated area, owned by the zone and read by the ambient tint's presenter // (ruling aetheria-gravity:ruling:dark-beyond-populated-area). The zone here is the production constructor reading a pack -// of a root off the origin, a planet, a moon and a moon's moon, each with a well of its own mass. +// of a root off the origin, a planet, a moon and a moon's moon, each with a well of its own mass, and a last body +// inserted after the outermost whose reach is the smallest, so the largest decides whatever the order. public sealed class PopulatedRadiusTests : IDisposable { private static readonly float2 RootAt = new float2(300f, -200f); private const float PlanetDistance = 400f; private const float MoonDistance = 100f; private const float MoonsMoonDistance = 40f; + private const float BeltOrbitDistance = 77f; + private const float FarthestAsteroid = 1000f; private readonly string _root = Path.Combine(Path.GetTempPath(), "aetheria-populated-" + Guid.NewGuid().ToString("N")); private readonly List _caches = new List(); @@ -46,7 +49,9 @@ public sealed class PopulatedRadiusTests : IDisposable public CultRecordKey[] Orbits; } - private Scene Build(float radius, bool moonsMoon = true) + // With belt, a belt circles the planet's orbit (asteroids at FarthestAsteroid and 700, the farther first) and its own orbit + // is BeltOrbitDistance out: the asteroids' positions do not depend on that distance, only on the planet's orbit. + private Scene Build(float radius, bool moonsMoon = true, bool belt = false) { var dir = Path.Combine(_root, $"zone{_caches.Count}"); Directory.CreateDirectory(dir); @@ -67,12 +72,24 @@ public sealed class PopulatedRadiusTests : IDisposable var pack = new ZonePack { Radius = radius, Mass = 10000f, Time = 0 }; foreach (var orbit in new[] { rootOrbit, planetOrbit, moonOrbit, moonsMoonOrbit }) pack.Orbits.Add(new CultRecordRef(orbit)); + if (belt) + { + var beltOrbit = cache.Upsert(new OrbitData { Parent = new CultRecordRef(planetOrbit), Distance = BeltOrbitDistance, Phase = .8f }).Key; + pack.Orbits.Add(new CultRecordRef(beltOrbit)); + var asteroids = new[] + { + new Asteroid { Distance = FarthestAsteroid, Phase = .2f, Size = .5f }, + new Asteroid { Distance = 700f, Phase = .6f, Size = .5f } + }; + pack.Planets.Add(new CultRecordRef(cache.Upsert(new AsteroidBeltData { Orbit = new CultRecordRef(beltOrbit), Asteroids = asteroids }).Key)); + } void Body(CultRecordKey orbit, float mass) => pack.Planets.Add(new CultRecordRef(cache.Upsert(new PlanetData { Orbit = new CultRecordRef(orbit), Mass = mass }).Key)); Body(rootOrbit, 5f); Body(planetOrbit, 1f); Body(moonOrbit, .5f); if (moonsMoon) Body(moonsMoonOrbit, .01f); + Body(rootOrbit, .001f); var zone = new Zone(items, Settings(), pack, new GalaxyZone { Name = "Populated", Owner = null }, null); return new Scene { Zone = zone, Orbits = new[] { rootOrbit, planetOrbit, moonOrbit, moonsMoonOrbit } }; } @@ -100,6 +117,16 @@ public sealed class PopulatedRadiusTests : IDisposable Assert.True(worst > 0f); } + [Fact] + public void ItReachesTheFarthestAsteroidOfABeltBeyondTheOutermostBody() + { + // The belt's asteroids circle the planet's orbit, so the chain is the root's offset and the planet's distance; the belt + // orbit's own distance does not enter, and neither do the nearer asteroid or any body (all inside this). + var s = Build(5000f, belt: true); + + Assert.Equal(length(RootAt) + PlanetDistance + FarthestAsteroid, s.Zone.PopulatedRadius, 2); + } + [Fact] public void ItBoundsTheRootOffTheOriginAndEachLevelOfTheChain() {