diff --git a/Assets/Scripts/ServerShared/Zone.cs b/Assets/Scripts/ServerShared/Zone.cs index a54aa8ced..d6981f30d 100644 --- a/Assets/Scripts/ServerShared/Zone.cs +++ b/Assets/Scripts/ServerShared/Zone.cs @@ -540,10 +540,13 @@ public class Zone // The one owner of a rail's period (Orbit.Period, AsteroidBelt.Periods): 2 pi sqrt(d / a), a being the pull the // field gives at distance d from the node being orbited. Round a body, that is the body's own well through - // GravityField. Round a bodyless node it is the ring law: the strength of the node's orbits inside d, plus half - // that of the ones on d itself (a companion on the ring pulls half, as a binary does), taken as a point at the - // node. `self` is the orbit being placed (excluded from its own pull); a strengthless self takes no ring share. - // No pull gives an infinite period. + // GravityField. Round a bodyless node it is the ring law: the strength of the node's orbits inside d, plus the + // ring term for the orbits on d itself (within RingWindow of it), taken as a point at the node. A bodied self + // feels half the strength of its ring partners (as a binary does); a strengthless self (a station, a turret) + // takes the period its bodied ring partners have, the mean of what each of them feels. No pull gives an + // infinite period. + private const float RingWindow = 0.1f; + private float PeriodAround(CultRecordKey parentOrbit, float distance, CultRecordKey self) { float a; @@ -552,16 +555,28 @@ public class Zone else { var inside = 0f; - var onRing = 0f; + var ring = new List<(CultRecordKey Orbit, float Strength)>(); if (_childOrbits.TryGetValue(parentOrbit, out var children)) foreach (var child in children) { - if (child.Equals(self)) continue; var childDistance = Orbits[child].Data.Distance; - if (childDistance < distance - 0.1f) inside += Strength(child); - else if (childDistance <= distance + 0.1f) onRing += Strength(child); + var childStrength = Strength(child); + if (childDistance < distance - RingWindow) inside += childStrength; + else if (childDistance <= distance + RingWindow && childStrength > 0) ring.Add((child, childStrength)); } - var strength = inside + (Strength(self) > 0 ? onRing / 2 : 0f); + // Whose pull self feels: its own if it has strength, else each bodied member of the ring. + var ringSum = 0f; + foreach (var member in ring) ringSum += member.Strength; + var selfBodied = Strength(self) > 0; + var feelings = 0f; + var feelers = 0; + foreach (var member in ring) + if (!selfBodied || member.Orbit.Equals(self)) + { + feelings += (ringSum - member.Strength) / 2; + feelers++; + } + var strength = inside + (feelers > 0 ? feelings / feelers : 0f); a = length(GravityField.Acceleration(strength, 0f, float2(distance, 0))); } return a > 0 && distance > 0 ? 2 * PI * sqrt(distance / a) : float.PositiveInfinity; diff --git a/tests/Aetheria.Shared.Tests/OrbitsFromFieldTests.cs b/tests/Aetheria.Shared.Tests/OrbitsFromFieldTests.cs index 2bd68b7bf..10de2c62e 100644 --- a/tests/Aetheria.Shared.Tests/OrbitsFromFieldTests.cs +++ b/tests/Aetheria.Shared.Tests/OrbitsFromFieldTests.cs @@ -195,6 +195,101 @@ public sealed class OrbitsFromFieldTests : IDisposable Assert.Equal(zone.Orbits[planet2].Period, zone.Orbits[companion2].Period); } + // A station on a binary's ring, with nothing inside, feels what the pair's members feel (the mean of what each does), + // and so does one a hair off the ring's distance but within its window. + [Theory] + [InlineData(0f)] + [InlineData(.05f)] + [InlineData(-.05f)] + public void ACompanionOnABinarysRingTakesTheMembersPeriod(float offset) + { + var w = NewWorld(); + var node = w.Orbit(default, 0f); + var light = w.Orbit(node, 400f, 0f); + var heavy = w.Orbit(node, 400f, .5f); + var lightBody = w.Body(light, 100f); + var heavyBody = w.Body(heavy, 2000f); + var companion = w.Orbit(node, 400f + offset, .25f); + var zone = w.Build(); + + Assert.True(C(zone, heavyBody) > 1.5f * C(zone, lightBody), "fixture: unequal members"); + AssertPeriod(Outside(400f + offset, (C(zone, lightBody) + C(zone, heavyBody)) / 4), zone.Orbits[companion].Period, + "the companion, between its two members"); + + // An equal pair: the companion has exactly the members' period. + w = NewWorld(); + node = w.Orbit(default, 0f); + var a = w.Orbit(node, 400f, 0f); + var b = w.Orbit(node, 400f, .5f); + w.Body(a, 300f); + w.Body(b, 300f); + companion = w.Orbit(node, 400f, .25f); + zone = w.Build(); + Assert.True(float.IsFinite(zone.Orbits[a].Period)); + Assert.Equal(zone.Orbits[a].Period, zone.Orbits[companion].Period); + Assert.Equal(zone.Orbits[b].Period, zone.Orbits[companion].Period); + } + + // The ring is the 0.1 u window about a rail's distance: a member just inside it is a ring partner (pulls half), one just + // outside is inside or beyond the rail (pulls whole, or not at all). + [Theory] + [InlineData(-.09f, true)] + [InlineData(.09f, true)] + [InlineData(-.11f, false)] + public void TheRingWindowIsATenthOfAUnit(float partnerOffset, bool onRing) + { + var w = NewWorld(); + var node = w.Orbit(default, 0f); + var rail = w.Orbit(node, 400f, 0f); + var partner = w.Orbit(node, 400f + partnerOffset, .5f); + w.Body(rail, 100f); + var partnerBody = w.Body(partner, 2000f); + var zone = w.Build(); + + var c = C(zone, partnerBody); + AssertPeriod(Outside(400f, onRing ? c / 2 : c), zone.Orbits[rail].Period, "the rail, with a partner at " + partnerOffset); + } + + [Fact] + public void AMemberJustBeyondTheWindowIsOutsideTheRail() + { + var w = NewWorld(); + var node = w.Orbit(default, 0f); + var rail = w.Orbit(node, 400f, 0f); + var beyond = w.Orbit(node, 400.11f, .5f); + w.Body(rail, 100f); + w.Body(beyond, 2000f); + var zone = w.Build(); + + Assert.True(float.IsPositiveInfinity(zone.Orbits[rail].Period), "a body beyond the rail does not pull it"); + } + + // A bodyless node's children read only that node's ring: the well of the body the node itself orbits does not enter. + [Fact] + public void ABodylessNodesChildrenNeverReadItsParentsWell() + { + var w = NewWorld(); + var sunOrbit = w.Orbit(default, 0f); + w.Body(sunOrbit, 5000f); + var node = w.Orbit(sunOrbit, 3000f); + var alone = w.Orbit(node, 200f, 0f); + w.Body(alone, 100f); + var zone = w.Build(); + Assert.True(float.IsPositiveInfinity(zone.Orbits[alone].Period), "a lone body on a bodyless node has no ring partner and no field"); + + w = NewWorld(); + sunOrbit = w.Orbit(default, 0f); + w.Body(sunOrbit, 5000f); + node = w.Orbit(sunOrbit, 3000f); + var a = w.Orbit(node, 200f, 0f); + var b = w.Orbit(node, 200f, .5f); + var bodyA = w.Body(a, 100f); + var bodyB = w.Body(b, 300f); + zone = w.Build(); + AssertPeriod(Outside(200f, C(zone, bodyB) / 2), zone.Orbits[a].Period, "the first member, under its partner alone"); + AssertPeriod(Outside(200f, C(zone, bodyA) / 2), zone.Orbits[b].Period, "the second member, under its partner alone"); + } + [Fact] public void AnOrbitWithNoFieldStandsStill() {