Place: a sole root's ring also folds when it cannot keep its heavy member outside the Roche margin; the entrance station falls back to the heaviest root of childless roots; tests follow

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-10-17 05:21:11 +02:00
commit ed9f9930dd
4 changed files with 43 additions and 20 deletions

View file

@ -258,14 +258,14 @@ public static class ZoneGenerator
{
// The entrance gets a station whatever its bodies: it orbits a root node beyond its ring group (or, for a body, its
// whole family), never at a ring member's distance, and sits there, not at a Lagrange point.
var rootNode = planets.FirstOrDefault(p => p.Parent == null && p.Children.Count > 0);
var roots = planets.Where(p => p.Parent == null).ToList();
var rootNode = roots.FirstOrDefault(p => p.Children.Count > 0) ?? (roots.Count > 1 ? roots.OrderByDescending(p => p.Mass).First() : null);
if (rootNode == null)
throw new InvalidOperationException(
"The tutorial entrance zone must always get a station, but it has no candidate orbit at all -- every planet in the zone is a parentless root.");
var rootKey = cache.RefOf(orbitMap[rootNode]).Key;
var distance = rootNode.Empty
? (1 + GravityField.RingReach) * GravityField.Beta * rootNode.Children.Where(Massive).Min(c => c.Distance)
: wells.Support(rootKey);
: Extent(rootNode);
var direct = new OrbitData { Parent = cache.RefOf(orbitMap[rootNode]), Distance = distance, Phase = random.NextFloat() };
pack.Orbits.Add(cache.Upsert(direct));
sources = new[] { (orbit: direct, extent: float.NegativeInfinity, direct: true) };
@ -655,10 +655,8 @@ public static class ZoneGenerator
var ring = node.Children.Where(c => c.Ring && WellMass(c) > 0).ToList();
if (ring.Count == 0) return;
var heavy = ring.OrderByDescending(c => c.Mass).First();
var soleRoot = isRoot && float.IsInfinity(reach);
var ringDistance = soleRoot ? .5f * cap
: Math.Min(.5f * cap, share * reach / (GravityField.Beta * GravityField.Beta * (1 + GravityField.RingReach)));
if (!soleRoot && ringDistance < RocheMargin * Roche(heavy.Mass))
var ringDistance = Math.Min(.5f * cap, share * reach / (GravityField.Beta * GravityField.Beta * (1 + GravityField.RingReach)));
if (ringDistance < RocheMargin * Roche(heavy.Mass))
{
// The pair cannot keep its heavy member outside its Roche margin inside its node's reach: the node is one body.
node.Empty = false;

View file

@ -59,7 +59,9 @@ public sealed class ChildrenWithinReachTests : IClassFixture<ChildrenWithinReach
_out = output;
}
private const float Tolerance = 1e-3f;
private const float Tolerance = 1e-3f;
// A child's own family folded into it after it was placed raises its mass, and the cusp bound grows as mass^.05.
private const float CuspTolerance = 1e-2f;
private const float RocheMargin = 1.5f, CuspMargin = 1.5f; // the rulings tail-taming-law-r3-band-loose-cusp and c4-proceed-and-fly
private static float Roche(Zone zone, float mass) => zone.Settings.RocheLimit.Evaluate(mass);
@ -168,7 +170,7 @@ public sealed class ChildrenWithinReachTests : IClassFixture<ChildrenWithinReach
{
checkedChildren++;
var cusp = 2 * CuspMargin * Roche(zone, mass[child]) * Strength(zone, mass[parent]) / Strength(zone, mass[child]);
Assert.True(DistanceOf(zone, child) >= cusp * (1 - Tolerance), $"seed {seed}: a massive child sits at {DistanceOf(zone, child)}, inside its cusp bound {cusp}");
Assert.True(DistanceOf(zone, child) >= cusp * (1 - CuspTolerance), $"seed {seed}: a massive child sits at {DistanceOf(zone, child)}, inside its cusp bound {cusp}");
}
}
Assert.True(checkedChildren > 50, $"only {checkedChildren} massive children checked");
@ -221,20 +223,31 @@ public sealed class ChildrenWithinReachTests : IClassFixture<ChildrenWithinReach
Assert.True(pairs > 100, $"only {pairs} sibling pairs checked");
}
// The mass split is renormalised so a family's masses sum to its root's; folding moves a family's mass into its parent body.
// A tree with a rosette or binary holds a bodyless node whose own mass is renormalised with the rest, so it is left out.
[Fact]
public void FoldingConservesMass()
{
var zones = 0;
foreach (var (seed, zone) in _zones.All)
var authored = AuthoredSettings.Load(RunStartTests.FindRepoRoot());
var zoneSettings = authored.Read<ZoneGenerationSettings>("ZoneSettings");
var planetSettings = authored.Read<PlanetSettings>("PlanetSettings");
const float mass = 300000f;
var trees = 0;
var folded = 0;
for (uint seed = 1; seed <= 200; seed++)
{
var placed = Placed(zone);
// A zone with a rosette or binary holds bodyless nodes, whose own (renormalised) mass is not a body's.
if (placed.Any(k => !zone.Planets.Values.Any(b => b.Orbit.Key.Equals(k)))) continue;
zones++;
var sum = zone.Planets.Values.Sum(b => b.Mass);
Assert.True(Math.Abs(sum - zone.Pack.Mass) <= 1e-3f * zone.Pack.Mass, $"seed {seed}: the bodies sum to {sum}, not the pack's {zone.Pack.Mass}");
// The same random stream makes the same tree; only the window differs, so a smaller one folds more of it.
var tight = new CultMath.Random(seed);
var tightTree = ZoneGenerator.GenerateEntities(zoneSettings, planetSettings, ref tight, mass, 1500f, float2.zero, float.PositiveInfinity);
var wide = new CultMath.Random(seed);
var wideTree = ZoneGenerator.GenerateEntities(zoneSettings, planetSettings, ref wide, mass, 1e7f, float2.zero, float.PositiveInfinity);
if (tightTree.Any(p => p.Empty || p.Ring) || wideTree.Any(p => p.Empty || p.Ring)) continue;
trees++;
if (tightTree.Length < wideTree.Length) folded++;
Assert.True(Math.Abs(tightTree.Sum(p => p.Mass) - mass) <= 1e-3f * mass, $"seed {seed}: a folded tree's bodies sum to {tightTree.Sum(p => p.Mass)}, not {mass}");
}
Assert.True(zones > 10, $"only {zones} zones without a bodyless node");
_out.WriteLine($"folding: {trees} trees, {folded} with a fold");
Assert.True(trees > 20 && folded > 10, $"{trees} trees, {folded} with a fold");
}
[Fact]

View file

@ -750,7 +750,11 @@ public sealed class RestoredHullsTests
// Confirms this fixture still exercises the widened path (a regression here would mean the fixture no
// longer probes what this test claims to probe).
Assert.True(IsRosetteMember(cache, pack, stationOrbit), "the entrance station no longer sits on a rosette-only orbit -- this fixture no longer tests the widened candidate path.");
// The zone has no body alone on its ring, so the station orbits the root node beyond its ring group.
var rootOrbit = cache.Get(stationOrbit.Parent);
Assert.False(rootOrbit.Parent.IsSet(), "the entrance station no longer orbits a root node -- this fixture no longer tests the no-candidate path.");
var ring = pack.Planets.Select(p => cache.Get(cache.Get(p).Orbit)).Where(o => o.Parent.IsSet() && o.Parent.Key.Equals(stationOrbit.Parent.Key)).Select(o => o.Distance).DefaultIfEmpty(0f).Min();
Assert.True(stationOrbit.Distance >= (1 + GravityField.RingReach) * GravityField.Beta * ring * .999f, $"the entrance station at {stationOrbit.Distance} sits inside its node's ring group of radius {ring}.");
var turrets = pack.Entities.OfType<OrbitalEntityPack>()
.Where(e => (cache.Get(e.Hull.Data) as HullData)?.HullType != HullType.Station).ToArray();

View file

@ -93,7 +93,7 @@ public sealed class StationPlacementTests : IDisposable
var rootKey = orbit.Data.Parent.Key;
Assert.False(zone.Orbits[rootKey].Data.Parent.IsSet(), $"seed {built.Seed}: a station with no source orbit does not orbit a root node");
var innermost = zone.Orbits.Where(o => o.Value.Data.Parent.IsSet() && o.Value.Data.Parent.Key.Equals(rootKey) && zone.Wells.Reach(o.Key) > 0f && !o.Key.Equals(station.OrbitData))
.Min(o => o.Value.Data.Distance);
.DefaultIfEmpty(0f).Min(o => o.Value.Data.Distance);
Assert.True(orbit.Data.Distance >= zone.Wells.Support(rootKey) * .999f || orbit.Data.Distance >= (1 + GravityField.RingReach) * GravityField.Beta * innermost * .999f,
$"seed {built.Seed}: a root-node station at {orbit.Data.Distance} sits inside its node's family");
rootStations++;
@ -148,6 +148,7 @@ public sealed class StationPlacementTests : IDisposable
{
var bodyOrbit = zone.Orbits[body.Orbit.Key];
if (SameRing(bodyOrbit, orbit)) continue; // its base
if (IsAncestorBody(zone, orbit, body.Orbit.Key)) continue; // the body it orbits round: it keeps its distance by its own Roche margin
var margin = 1.5f * zone.Settings.RocheLimit.Evaluate(body.Mass) + Contacts.Radius(entity);
for (var k = 0; k < 64; k++)
{
@ -161,6 +162,13 @@ public sealed class StationPlacementTests : IDisposable
Assert.True(checkedEntities > 0, "the seeds generated no station");
}
private static bool IsAncestorBody(Zone zone, Orbit orbit, CultRecordKey bodyOrbit)
{
for (var parent = orbit.Data.Parent; parent.IsSet(); parent = zone.Orbits[parent.Key].Data.Parent)
if (parent.Key.Equals(bodyOrbit)) return true;
return false;
}
private static List<OrbitalEntity> Orbitals(Built built) => built.Zone.Entities.OfType<OrbitalEntity>().ToList();
// Every turret belongs to one station on its ring. Each side of a station grows outward from it: a turret's centre sits