diff --git a/Assets/Scripts/ServerShared/Contacts.cs b/Assets/Scripts/ServerShared/Contacts.cs index f680faa5a..d9f854a1a 100644 --- a/Assets/Scripts/ServerShared/Contacts.cs +++ b/Assets/Scripts/ServerShared/Contacts.cs @@ -33,19 +33,7 @@ public readonly struct ContactEvent public static class Contacts { - // The farthest corner of any occupied hull cell from the centre of mass the schematic frame uses, in world - // units: a true bound on how far the hull reaches from Position (the half-diagonal of the bounding box is not, - // as FireControl.Detonate's comment records). A cell at (x, y) spans x +/- .5 and y +/- .5. - public static float Radius(Entity e) - { - var shape = e.HullData.Shape; - var com = shape.CenterOfMass; - var far = 0f; - foreach (var c in shape.Coordinates) - foreach (var (dx, dy) in new[] { (-.5f, -.5f), (.5f, -.5f), (-.5f, .5f), (.5f, .5f) }) - far = max(far, length(float2(c.x + dx, c.y + dy) - com)); - return far * e.ItemManager.GameplaySettings.SchematicCellSize; - } + public static float Radius(Entity e) => e.HullData.Reach(e.ItemManager.GameplaySettings.SchematicCellSize); // For each unordered pair of the zone's entities, in list order: if their discs touch and are closing along // the normal, the impulse moves each one's velocity along the normal only (the tangent components are diff --git a/Assets/Scripts/ServerShared/ItemData.cs b/Assets/Scripts/ServerShared/ItemData.cs index 9b9c057e7..4e946f245 100644 --- a/Assets/Scripts/ServerShared/ItemData.cs +++ b/Assets/Scripts/ServerShared/ItemData.cs @@ -536,6 +536,19 @@ public class HullData : EquippableItemData [Inspectable, JsonProperty("visual"), Key(32)] public CultRecordRef Visual; + // The farthest corner of any occupied hull cell from the centre of mass the schematic frame uses, in world + // units: a true bound on how far the hull reaches from its position (the half-diagonal of the bounding box is not, + // as FireControl.Detonate's comment records). A cell at (x, y) spans x +/- .5 and y +/- .5. + public float Reach(float cellSize) + { + var com = Shape.CenterOfMass; + var far = 0f; + foreach (var c in Shape.Coordinates) + foreach (var (dx, dy) in new[] { (-.5f, -.5f), (.5f, -.5f), (-.5f, .5f), (.5f, .5f) }) + far = max(far, length(float2(c.x + dx, c.y + dy) - com)); + return far * cellSize; + } + [IgnoreMember] public Shape InteriorCells { diff --git a/Assets/Scripts/ServerShared/ZoneGenerator.cs b/Assets/Scripts/ServerShared/ZoneGenerator.cs index 56778a619..76c742b07 100644 --- a/Assets/Scripts/ServerShared/ZoneGenerator.cs +++ b/Assets/Scripts/ServerShared/ZoneGenerator.cs @@ -256,45 +256,51 @@ public static class ZoneGenerator return loadoutGenerators[faction]; } + // L4 or L5: 60 degrees (one sixth of a turn) ahead of or behind the planet on its orbit. Phase is in turns. OrbitData CreateLagrangeOrbit(OrbitData baseOrbit) { var lagrangeOrbit = new OrbitData { Parent = baseOrbit.Parent, Distance = baseOrbit.Distance, - Phase = baseOrbit.Phase + PI / 3 * sign(random.NextFloat() - .5f), + Phase = baseOrbit.Phase + sign(random.NextFloat() - .5f) / 6f, }; pack.Orbits.Add(cache.Upsert(lagrangeOrbit)); return lagrangeOrbit; } - void PlaceTurret(OrbitData orbit, LoadoutGenerator loadoutGenerator, int distanceMultiplier) + // A turret co-orbits with its station, k arcs along the orbit (k is signed: behind the station when negative). + // One arc is the station's reach plus two of the turret's: the first turret's disc clears the station's by one + // turret radius, and turrets step by the same arc, which also clears each other (stationReach + 2 turretReach + // is more than 2 turretReach). Phase is in turns, so the arc length divides by the orbit's circumference. + void PlaceTurret(OrbitData orbit, LoadoutGenerator loadoutGenerator, int k, float stationReach) { - var phase = 20f * distanceMultiplier / orbit.Distance; + var turret = loadoutGenerator.GenerateTurretLoadout(); + if (turret == null) return; + var turretReach = (itemManager.GetData(turret.Hull) as HullData).Reach(itemManager.GameplaySettings.SchematicCellSize); + var arc = k * (stationReach + 2 * turretReach); var turretOrbit = new OrbitData { Parent = orbit.Parent, Distance = orbit.Distance, - Phase = orbit.Phase + phase + Phase = orbit.Phase + arc / (2 * PI * orbit.Distance) }; pack.Orbits.Add(cache.Upsert(turretOrbit)); - var turret = loadoutGenerator.GenerateTurretLoadout(); - if (turret == null) return; turret.Orbit = cache.RefOf(turretOrbit); pack.Entities.Add(turret); } - void PlaceTurrets(OrbitData orbit, LoadoutGenerator loadoutGenerator, int count) + // Turrets flank the station alternately: k runs -1, +1, -2, +2, ... + void PlaceTurrets(OrbitData orbit, LoadoutGenerator loadoutGenerator, int count, float stationReach) { for(int t=0; t + (itemManager.GetData(station.Hull) as HullData).Reach(itemManager.GameplaySettings.SchematicCellSize); + for (var i = 0; i < storyStations.Length; i++) { var story = storyStations[i]; @@ -312,7 +318,7 @@ public static class ZoneGenerator station.Story = i; pack.Entities.Add(station); - if (ambient) PlaceTurrets(lagrangeOrbit, GetLoadoutGenerator(story.Faction), story.Turrets); + if (ambient) PlaceTurrets(lagrangeOrbit, GetLoadoutGenerator(story.Faction), story.Turrets, StationReach(station)); } for (var i = storyStations.Length; i < selectedStationOrbits.Length; i++) @@ -334,7 +340,7 @@ public static class ZoneGenerator station.SecurityRadius = pack.Radius; pack.Entities.Add(station); - if (ambient) PlaceTurrets(lagrangeOrbit, GetLoadoutGenerator(nearestFaction), 2); + if (ambient) PlaceTurrets(lagrangeOrbit, GetLoadoutGenerator(nearestFaction), 2, StationReach(station)); } // A boss zone gets its faction's boss whether or not the zone is ambient: the boss is the run's goal, not diff --git a/tests/Aetheria.Shared.Tests/StationPlacementTests.cs b/tests/Aetheria.Shared.Tests/StationPlacementTests.cs new file mode 100644 index 000000000..0beddc038 --- /dev/null +++ b/tests/Aetheria.Shared.Tests/StationPlacementTests.cs @@ -0,0 +1,179 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using CultMath; +using GameCult.Caching; +using Xunit; +using static CultMath.math; +using float2 = CultMath.float2; + +// Aetheria gravity, turret placement: a station sits at its planet's L4 or L5 (a sixth of a turn along the ring) and +// its turrets flank it along the same ring, spaced by hull reach so that no two bodies overlap. The zones here are +// the production generator's, ambient on, over several galaxies; every expectation is read back from the generated +// zone (its orbits, its hulls) and spelled from the rule, never from the generator's own helpers. +public sealed class StationPlacementTests : IDisposable +{ + private readonly string _root = Path.Combine(Path.GetTempPath(), "aetheria-stationplacement-" + Guid.NewGuid().ToString("N")); + private readonly List _caches = new List(); + + public StationPlacementTests() => Directory.CreateDirectory(_root); + + public void Dispose() + { + foreach (var cache in _caches) cache.Dispose(); + Directory.Delete(_root, true); + } + + private sealed record Built(uint Seed, Zone Zone, ItemManager Items); + + private IEnumerable GeneratedZones() + { + for (uint seed = 1; seed <= 6; seed++) + { + var dir = Path.Combine(_root, $"galaxy{seed}"); + Directory.CreateDirectory(dir); + var catalog = Path.Combine(dir, "Aetheria.cc"); + File.Copy(TestCatalog.Repo, catalog); + var cache = new CultCache(TestCatalog.Registry()); + _caches.Add(cache); + cache.AddBackingStore(new GameCult.Caching.MessagePack.SingleFileMessagePackBackingStore(catalog), AetheriaStores.CatalogTypes); + cache.AddBackingStore(new GameCult.Caching.MessagePack.SingleFileMessagePackBackingStore(Path.Combine(dir, "run.cc")), AetheriaStores.RunTypes); + var authored = AuthoredSettings.Load(RunStartTests.FindRepoRoot()); + var settings = authored.Read("GameplaySettings"); + var galaxy = new Galaxy(authored.Read("TutorialGenerationSettings"), + authored.Read("TutorialBackgroundSettings"), + authored.Read("NameGeneratorSettings"), cache, new PlayerSettings(), + Directory.CreateDirectory(Path.Combine(dir, "Narrative")), _ => { }, null, seed); + var items = new ItemManager(cache, new ProvenanceLedger(), settings, _ => { }); + foreach (var galaxyZone in galaxy.Zones) + { + var pack = ZoneGenerator.GenerateZone(items, authored.Read("ZoneSettings"), galaxy, galaxyZone, + galaxyZone == galaxy.Entrance && galaxy.IsPrelude, true); + var zone = new Zone(items, authored.Read("PlanetSettings"), pack, galaxyZone, galaxy); + zone.Update(.02f); + yield return new Built(seed, zone, items); + } + } + } + + private static HullType TypeOf(ItemManager items, OrbitalEntity e) => (items.GetData(e.Hull) as HullData).HullType; + + private static bool SameRing(Orbit a, Orbit b) => + a.Data.Parent.IsSet() && b.Data.Parent.IsSet() && a.Data.Parent.Key.Equals(b.Data.Parent.Key) && + Math.Abs(a.Data.Distance - b.Data.Distance) < .01f; + + private static float Turns(float turns) => turns - (float) Math.Floor(turns); + + [Fact] + public void AStationSitsAtItsPlanetsL4OrL5() + { + var checkedStations = 0; + var ahead = 0; + var behind = 0; + foreach (var built in GeneratedZones()) + { + var zone = built.Zone; + var hosting = zone.Planets.Values.Select(body => body.Orbit.Key).ToHashSet(); + foreach (var station in zone.Entities.OfType().Where(e => TypeOf(built.Items, e) == HullType.Station)) + { + var orbit = zone.Orbits[station.OrbitData]; + var sources = zone.Orbits.Where(o => hosting.Contains(o.Key) && !o.Key.Equals(station.OrbitData) && SameRing(o.Value, orbit)).ToList(); + Assert.True(sources.Count == 1, $"seed {built.Seed}: a station shares its ring with {sources.Count} planets, not one"); + var planet = sources[0].Value; + + var offset = Turns(orbit.Data.Phase - planet.Data.Phase); + var isAhead = Math.Abs(offset - 1f / 6f) < 1e-5f; + var isBehind = Math.Abs(offset - 5f / 6f) < 1e-5f; + Assert.True(isAhead || isBehind, $"seed {built.Seed}: a station sits {offset} turns from its planet, not 1/6 or 5/6"); + if (isAhead) ahead++; else behind++; + + var parent = zone.Orbits[orbit.Data.Parent.Key].Position; + var toPlanet = planet.Position - parent; + var toStation = station.Position.xz - parent; + var angle = Math.Acos(Math.Clamp(dot(toPlanet, toStation) / (length(toPlanet) * length(toStation)), -1f, 1f)) * 180 / Math.PI; + Assert.True(Math.Abs(angle - 60) < .01, $"seed {built.Seed}: planet-parent-station is {angle} degrees, not 60"); + checkedStations++; + } + } + Assert.True(checkedStations > 0, "the seeds generated no station"); + Assert.True(ahead > 0 && behind > 0, $"the seeds put stations on one side only (ahead {ahead}, behind {behind})"); + } + + [Fact] + public void TurretsFlankTheirStation() + { + var flanked = 0; + var pairsChecked = 0; + foreach (var built in GeneratedZones()) + { + var zone = built.Zone; + var orbitals = zone.Entities.OfType().ToList(); + var turrets = orbitals.Where(e => TypeOf(built.Items, e) == HullType.Turret).ToList(); + foreach (var station in orbitals.Where(e => TypeOf(built.Items, e) == HullType.Station)) + { + var orbit = zone.Orbits[station.OrbitData]; + var mine = turrets.Where(t => SameRing(zone.Orbits[t.OrbitData], orbit)).ToList(); + if (mine.Count == 0) continue; + flanked++; + var stationReach = Contacts.Radius(station); + var circumference = 2 * PI * orbit.Data.Distance; + // The arc is float phase arithmetic: its absolute error grows with the ring, so the tolerance does too. + var slack = circumference * 2e-7f; + + var ks = new List(); + foreach (var turret in mine) + { + var arcUnit = stationReach + 2 * Contacts.Radius(turret); + var phase = Turns(zone.Orbits[turret.OrbitData].Data.Phase - orbit.Data.Phase); + if (phase > .5f) phase -= 1; + var signedArc = phase * circumference; + var k = (int) Math.Round(signedArc / arcUnit); + Assert.True(k != 0, $"seed {built.Seed}: a turret sits on its station"); + Assert.True(Math.Abs(signedArc - k * arcUnit) <= 1e-3f * arcUnit + slack, + $"seed {built.Seed}: a turret's arc {signedArc} is not a whole multiple ({k}) of {arcUnit}"); + ks.Add(k); + } + var expected = Enumerable.Range(0, mine.Count).Select(t => (t / 2 + 1) * (t % 2 == 0 ? -1 : 1)).OrderBy(k => k); + Assert.Equal(expected, ks.OrderBy(k => k)); + + var bodies = new List { station }; + bodies.AddRange(mine); + for (var a = 0; a < bodies.Count; a++) + for (var b = a + 1; b < bodies.Count; b++) + { + var apart = length(bodies[a].Position.xz - bodies[b].Position.xz); + Assert.True(apart > Contacts.Radius(bodies[a]) + Contacts.Radius(bodies[b]), + $"seed {built.Seed}: two bodies on one ring overlap ({apart} apart)"); + pairsChecked++; + } + } + } + Assert.True(flanked > 0, "the seeds generated no station with turrets"); + Assert.True(pairsChecked > 0, "no pair was checked"); + } + + // The reach every spacing is built from: the farthest corner of any occupied cell from the centre of mass, scaled by + // the cell size. Spelled here from that definition over the shipped station and turret hulls. + [Fact] + public void AHullsReachIsItsFarthestCorner() + { + var seen = new HashSet(); + foreach (var built in GeneratedZones().Take(6)) + foreach (var entity in built.Zone.Entities.OfType()) + { + var hull = built.Items.GetData(entity.Hull) as HullData; + if (!seen.Add(hull.Name)) continue; + var com = hull.Shape.CenterOfMass; + var farthest = 0f; + foreach (var c in hull.Shape.Coordinates) + foreach (var dx in new[] { -.5f, .5f }) + foreach (var dy in new[] { -.5f, .5f }) + farthest = Math.Max(farthest, (float) Math.Sqrt(Math.Pow(c.x + dx - com.x, 2) + Math.Pow(c.y + dy - com.y, 2))); + var cell = built.Items.GameplaySettings.SchematicCellSize; + Assert.True(Math.Abs(hull.Reach(cell) - farthest * cell) <= 1e-4f * farthest * cell, $"hull {hull.Name}: reach {hull.Reach(cell)} is not {farthest * cell}"); + Assert.True(hull.Reach(cell) > 0f); + } + Assert.True(seen.Count >= 2, "the zones held fewer than a station hull and a turret hull"); + } +}