From 74bd92251a15a5e13f0c563619241bbd68fc6a1b Mon Sep 17 00:00:00 2001 From: soul Date: Sun, 29 Nov 2026 04:30:39 +0100 Subject: [PATCH] ZoneGenerator: the overflow seat's root spacing stays inside the zone's boundary circle; tests pin containment, isolation and the seat order The boundary circle (Radius x ZoneBoundaryRadius) is a fixed function of the zone's radius, drawn before any content, so spacing must fit inside it: a root takes the station only while moving every root outward by its spacing keeps the outermost root within the boundary; where no root fits beyond its ring group the station orbits a root at its family extent, the least spacing that fits. Unbounded, spacing put a root outside the boundary in 47 of 6400 zones (up to 3.2x the pack radius; 0 at base). OverflowSeatTests sweep nine seeds that exercised the bound: no root beyond the boundary, isolation holds for every spaced root, and an overflow station orbits a root with no massive child wherever the zone has one (reverting to the old first-root-with-children rule fails it). Co-Authored-By: Claude Sonnet 5.5 --- Assets/Scripts/ServerShared/ZoneGenerator.cs | 25 +++-- .../OverflowSeatTests.cs | 94 +++++++++++++++++++ 2 files changed, 112 insertions(+), 7 deletions(-) create mode 100644 tests/Aetheria.Shared.Tests/OverflowSeatTests.cs diff --git a/Assets/Scripts/ServerShared/ZoneGenerator.cs b/Assets/Scripts/ServerShared/ZoneGenerator.cs index 2fd61730a..438b4c280 100644 --- a/Assets/Scripts/ServerShared/ZoneGenerator.cs +++ b/Assets/Scripts/ServerShared/ZoneGenerator.cs @@ -368,24 +368,35 @@ public static class ZoneGenerator ? Math.Max(r.Empty ? 0f : Extent(r), (1 + GravityField.RingReach) * GravityField.Beta * r.Children.Where(Massive).Min(c => c.Distance)) : Extent(r); // How far the roots must move apart for this root to take the station: 1 when it already clears. - float Spacing(GeneratorPlanet r) + float SpacingAt(GeneratorPlanet r, float at) { if (massiveRoots.Count < 2 || !massiveRoots.Contains(r)) return 1f; var nearest = massiveRoots.Where(o => o != r).Min(o => length(o.FixedPosition - r.FixedPosition)); - return Math.Max(1f, GravityField.Beta * GravityField.Beta * SeatDistance(r) / (GravityField.Reach * nearest) * (1 + 1e-3f)); + return Math.Max(1f, GravityField.Beta * GravityField.Beta * at / (GravityField.Reach * nearest) * (1 + 1e-3f)); } - var seats = new[] + float Spacing(GeneratorPlanet r) => SpacingAt(r, SeatDistance(r)); + // The zone's boundary circle is fixed before any content, from its radius (zoneBoundary above), so the spacing must fit inside it: a + // root may only take the station while moving every root outward by its spacing leaves the outermost one within the boundary. + // The first kind with a root that fits seats it; if no root of any kind fits, the least spacing is taken, held at the boundary. + var kinds = new[] { roots.Where(r => !r.Empty && !r.Children.Any(Massive) && (roots.Count > 1 || r.Children.Count > 0)).ToList(), // a lone childless root has nothing to orbit: the zone refuses roots.Where(r => r.Empty && r.Children.Any(Massive)).ToList(), roots.Where(r => !r.Empty && r.Children.Any(Massive)).ToList(), - }.FirstOrDefault(kind => kind.Count > 0); - var rootNode = seats?.OrderBy(Spacing).ThenByDescending(FamilyMass).First(); + }; + var outermost = roots.Max(r => length(r.FixedPosition)); + var fits = outermost <= 0f ? float.PositiveInfinity : Math.Max(1f, pack.Radius * zoneSettings.ZoneBoundaryRadius / outermost); + var rootNode = kinds.Select(kind => kind.Where(r => Spacing(r) <= fits).OrderBy(Spacing).ThenByDescending(FamilyMass).FirstOrDefault()).FirstOrDefault(r => r != null); + // No root fits beyond its ring group: the station then orbits at its root's family extent instead, the closest seat that clears the family. + var tight = rootNode == null; + if (tight) + rootNode = kinds.SelectMany(kind => kind).Where(r => SpacingAt(r, Extent(r)) <= fits).OrderBy(r => SpacingAt(r, Extent(r))).ThenByDescending(FamilyMass).FirstOrDefault() + ?? kinds.SelectMany(kind => kind).OrderBy(r => SpacingAt(r, Extent(r))).ThenByDescending(FamilyMass).FirstOrDefault(); if (rootNode == null) throw new InvalidOperationException( $"Zone {galaxyZone.Name} must seat all {required} of its dealt stations, but it has no candidate orbit at all -- every planet in the zone is a parentless root."); - var distance = SeatDistance(rootNode); - var spacing = Spacing(rootNode); + var distance = tight && !rootNode.Empty ? Extent(rootNode) : SeatDistance(rootNode); + var spacing = Math.Min(SpacingAt(rootNode, distance), fits); if (spacing > 1f) foreach (var root in roots) { diff --git a/tests/Aetheria.Shared.Tests/OverflowSeatTests.cs b/tests/Aetheria.Shared.Tests/OverflowSeatTests.cs new file mode 100644 index 000000000..29f6da9cb --- /dev/null +++ b/tests/Aetheria.Shared.Tests/OverflowSeatTests.cs @@ -0,0 +1,94 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ + +using System; +using System.Collections.Generic; +using System.Linq; +using CultMath; +using Xunit; +using static CultMath.math; + +// The overflow seat (aetheria-living-world, cut region-chain-deck; rulings overflow-station-clears-root-first and root-window-floor): a station +// the rails cannot seat orbits a root, the roots move apart so the root's reach floor stays inside its subzone's isolation, and the zone's +// boundary circle (fixed from its radius before any content) keeps holding every root. +public sealed partial class RunStartTests +{ + // Seeds whose zones put a root outside the boundary under the unbounded spacing (47 of 6400 zones of seeds 1-100 did, at ddf0b4ec5: seeds 2, 6, 8, 11, + // 12, 18, 21 and 28 among them), so the sweep exercises the bound; the full 100-seed sweep takes over half an hour. + private static readonly uint[] SpacedSeeds = { 1, 2, 6, 8, 11, 12, 18, 21, 28 }; + + // Every zone of the demo galaxy of each seed, generated with the demo's orders, with what the overflow seat did. + private IEnumerable<(uint seed, GalaxyZone gz, Zone zone, ZonePack pack, string[] rootStations)> OverflowSweep(IEnumerable seeds) + { + var orders = new DemoTerminus().EntranceStations; + foreach (var seed in seeds) + { + var galaxy = DemoGalaxy(seed); + foreach (var gz in galaxy.Zones) + { + var pack = ZoneGenerator.GenerateZone(_items, _zoneSettings, _planetSettings, galaxy, gz, gz == galaxy.Entrance ? orders : null, false); + var zone = new Zone(_items, _planetSettings, pack, gz, galaxy); + // A station whose orbit circles a root where no body rides that ring was seated by the overflow path. + var bodyRings = pack.Planets.Select(planet => _cache.Get(planet)).Where(body => body != null).Select(body => _cache.Get(body.Orbit)).ToList(); + var rootSeated = pack.Entities.OfType().Where(entity => entity.Kind.IsSet()) + .Select(entity => _cache.Get(entity.Orbit)).Where(orbit => orbit.Parent.IsSet() && !_cache.Get(orbit.Parent).Parent.IsSet() + && !bodyRings.Any(ring => ring.Parent.IsSet() && ring.Parent.Key.Equals(orbit.Parent.Key) && Math.Abs(ring.Distance - orbit.Distance) < .01f)) + .Select(orbit => orbit.Parent.Key.Value).Distinct().ToArray(); + yield return (seed, gz, zone, pack, rootSeated); + } + } + } + + // The boundary circle is a fixed function of the zone's radius (ZoneGenerator: Radius x ZoneBoundaryRadius), so spacing must fit inside it: + // no root lies beyond it, with or without an overflow station. + [Fact] + public void RootSpacingStaysInsideTheZoneBoundary() + { + var zones = 0; + var spaced = 0; + var outside = new List(); + var broken = new List(); + foreach (var (seed, gz, zone, pack, rootStations) in OverflowSweep(SpacedSeeds)) + { + zones++; + var bound = pack.Radius * _zoneSettings.ZoneBoundaryRadius; + var far = zone.Orbits.Values.Where(orbit => !orbit.Data.Parent.IsSet()).Select(orbit => length(orbit.Data.FixedPosition)).DefaultIfEmpty(0f).Max(); + if (far > bound * (1 + 1e-4f)) outside.Add($"seed {seed} {gz.Name}: root at {far} beyond the boundary {bound}"); + if (rootStations.Length == 0) continue; + var roots = zone.Orbits.Keys.Where(k => !zone.Orbits[k].Data.Parent.IsSet() && zone.Wells.Reach(k) > 0f).ToList(); + if (roots.Count < 2) continue; + spaced++; + foreach (var root in roots) + { + var nearest = roots.Where(other => !other.Equals(root)).Min(other => length(zone.Orbits[root].Data.FixedPosition - zone.Orbits[other].Data.FixedPosition)); + if (Math.Abs(zone.Wells.Reach(root) - GravityField.Reach * nearest) > 1e-3f * nearest) broken.Add($"seed {seed} {gz.Name}: reach {zone.Wells.Reach(root)} is not Reach x {nearest}"); + } + } + Assert.True(spaced > 0, $"{zones} zones swept and none was spaced for an overflow station: the sweep does not exercise the spacing"); + Assert.True(outside.Count == 0, $"{outside.Count} of {zones} zones hold a root outside the boundary, e.g. {string.Join("; ", outside.Take(3))}"); + Assert.True(broken.Count == 0, $"{broken.Count} spaced roots lose isolation, e.g. {string.Join("; ", broken.Take(3))}"); + } + + // The ruled seat order: where the zone has a root no massive body orbits, the overflow station orbits one of those, not a root with a family. + [Fact] + public void AnOverflowStationOrbitsARootWithNoMassiveChildWhereTheZoneHasOne() + { + var childless = 0; + var wrong = new List(); + foreach (var (seed, gz, zone, pack, rootStations) in OverflowSweep(SpacedSeeds)) + { + if (rootStations.Length == 0) continue; + bool HasMassiveChild(CultMath.float2 _, GameCult.Caching.CultRecordKey root) => + zone.Orbits.Any(o => o.Value.Data.Parent.IsSet() && o.Value.Data.Parent.Key.Equals(root) && zone.Wells.Reach(o.Key) > 0f); + var roots = zone.Orbits.Keys.Where(k => !zone.Orbits[k].Data.Parent.IsSet()).ToList(); + var free = roots.Where(k => zone.Wells.Reach(k) > 0f && !HasMassiveChild(default, k)).ToList(); + if (free.Count == 0) continue; + childless++; + foreach (var seat in rootStations) + if (!free.Any(k => k.Value == seat)) wrong.Add($"seed {seed} {gz.Name}: seated on a root with a massive child while {free.Count} roots have none"); + } + Assert.True(childless > 0, "no swept zone had both an overflow station and a root with no massive child"); + Assert.True(wrong.Count == 0, string.Join("; ", wrong.Take(3))); + } +}