From 87793044999446b618d7f35738171708e4aed221 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 18 Oct 2026 17:28:36 +0200 Subject: [PATCH 1/7] Galaxy generation refuses seed 0: delete the clock fallback and the seed defaults GalaxyStage.SeedFrom is the one minter of a run's seed; both generation constructors now take progressCallback and seed without defaults and throw ArgumentOutOfRangeException on zero instead of reading the clock. Co-Authored-By: Claude Sonnet 5.5 --- Assets/Scripts/ServerShared/Galaxy.cs | 12 ++++++------ tests/Aetheria.Shared.Tests/RunStartTests.cs | 10 ++++++++++ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/Assets/Scripts/ServerShared/Galaxy.cs b/Assets/Scripts/ServerShared/Galaxy.cs index 0b2f71387..b2d1e5dd0 100644 --- a/Assets/Scripts/ServerShared/Galaxy.cs +++ b/Assets/Scripts/ServerShared/Galaxy.cs @@ -96,15 +96,15 @@ public class Galaxy SectorBackgroundSettings background, CultCache cache, Action log, - Action progressCallback = null, - uint seed = 0) + Action progressCallback, + uint seed) { _cache = cache; IsPrelude = false; Background = background; Log = log; var factions = cache.GetAll(); - Seed = seed == 0 ? (uint) (DateTime.Now.Ticks % uint.MaxValue) : seed; + Seed = seed != 0 ? seed : throw new ArgumentOutOfRangeException(nameof(seed), "a generated galaxy takes its run's seed from GalaxyStage, never zero"); var random = new Random(Seed); Factions = factions.OrderBy(x => random.NextFloat()).Take(settings.MegaCount).ToArray(); foreach (var f in Factions) FactionRelationships[f] = FactionRelationship.Neutral; @@ -163,15 +163,15 @@ public class Galaxy PlayerSettings playerSettings, DirectoryInfo narrativeDirectory, Action log, - Action progressCallback = null, - uint seed = 0) + Action progressCallback, + uint seed) { _cache = cache; IsPrelude = true; Background = background; Log = log; - Seed = seed == 0 ? (uint) (DateTime.Now.Ticks % uint.MaxValue) : seed; + Seed = seed != 0 ? seed : throw new ArgumentOutOfRangeException(nameof(seed), "a generated galaxy takes its run's seed from GalaxyStage, never zero"); var random = new Random(Seed); var factions = new List(); diff --git a/tests/Aetheria.Shared.Tests/RunStartTests.cs b/tests/Aetheria.Shared.Tests/RunStartTests.cs index efedc20dd..7c22bb911 100644 --- a/tests/Aetheria.Shared.Tests/RunStartTests.cs +++ b/tests/Aetheria.Shared.Tests/RunStartTests.cs @@ -115,6 +115,16 @@ public sealed partial class RunStartTests : IDisposable private const float Freezing = 273.15f; + // One minter: a generated galaxy never reads the clock, so seed 0 is refused by both generation constructors. + [Fact] + public void AGeneratedGalaxyRefusesSeedZero() + { + Assert.Throws(() => + new Galaxy(new SectorGenerationSettings(), new SectorBackgroundSettings(), _cache, _ => { }, null, 0)); + Assert.Throws(() => + new Galaxy(new TutorialGenerationSettings(), new SectorBackgroundSettings(), _cache, new PlayerSettings(), new DirectoryInfo(_root), _ => { }, null, 0)); + } + // A main-sector galaxy: each station's gear must come from manufacturers its faction can reach, the makers its // allegiance names, present in the galaxy or not. private Galaxy MainSectorGalaxy() From 020dc58a812c04d6e8fb983d32d1bf0a1d3095ee Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 18 Oct 2026 17:29:17 +0200 Subject: [PATCH 2/7] The run's seed rides the save: SavedGame key 14, written by Capture, read by Continue Galaxy.Seed on a continued run is the saved seed instead of zero. A save written before run-seed decodes Seed 0. Co-Authored-By: Claude Sonnet 5.5 --- Assets/Scripts/ServerShared/Galaxy.cs | 5 ++- Assets/Scripts/ServerShared/GalaxyStage.cs | 3 +- Assets/Scripts/ServerShared/SavedGame.cs | 8 +++- .../DemoTerminusTests.cs | 2 + tests/Aetheria.Shared.Tests/RunSaveTests.cs | 37 +++++++++++++++++++ 5 files changed, 51 insertions(+), 4 deletions(-) diff --git a/Assets/Scripts/ServerShared/Galaxy.cs b/Assets/Scripts/ServerShared/Galaxy.cs index b2d1e5dd0..e9e75b239 100644 --- a/Assets/Scripts/ServerShared/Galaxy.cs +++ b/Assets/Scripts/ServerShared/Galaxy.cs @@ -28,8 +28,8 @@ public class Galaxy private Action Log { get; } public bool IsPrelude { get; } - // The seed this galaxy was generated at, which also seeds its run's item draws (RunStart); zero for a galaxy - // restored from a save. + // The run's seed, minted once by GalaxyStage.SeedFrom and carried by the save (SavedGame.Seed). It seeds + // generation, the run's item draws (RunStart) and the zone's drop dice (Zone.DropSeed). public uint Seed { get; } private GalaxyZone[] _exitPath; @@ -51,6 +51,7 @@ public class Galaxy _cache = cultCache; Log = log; Background = savedGame.Background; + Seed = savedGame.Seed; Factions = savedGame.Factions.Select(faction => cultCache.Get(faction)).ToArray(); for (var i = 0; i < Factions.Length; i++) diff --git a/Assets/Scripts/ServerShared/GalaxyStage.cs b/Assets/Scripts/ServerShared/GalaxyStage.cs index cd8feefed..a93e25f89 100644 --- a/Assets/Scripts/ServerShared/GalaxyStage.cs +++ b/Assets/Scripts/ServerShared/GalaxyStage.cs @@ -46,7 +46,8 @@ public sealed class GalaxyStage _clock = clock ?? (() => (uint) (DateTime.Now.Ticks % uint.MaxValue)); } - // The seed this generation runs at, never zero: the scenario's, or the clock's when the scenario's is zero. + // The run's one minter: the scenario's seed, or the clock's when the scenario's is zero, never zero. Galaxy carries + // what it mints and SavedGame keeps it, so Continue rolls the same drops. public uint Seed { get; private set; } internal void SeedFrom(Scenario scenario) diff --git a/Assets/Scripts/ServerShared/SavedGame.cs b/Assets/Scripts/ServerShared/SavedGame.cs index ba819e6ec..1758074fa 100644 --- a/Assets/Scripts/ServerShared/SavedGame.cs +++ b/Assets/Scripts/ServerShared/SavedGame.cs @@ -50,6 +50,11 @@ public class SavedGame [Key(13)] public int Credits; + + // The run's seed (Galaxy.Seed), minted once by GalaxyStage.SeedFrom. Written only by RunSave.Capture and read only + // by Galaxy's save constructor; 0 in a save written before run-seed, which then rolls name-only drops. + [Key(14)] + public uint Seed; } // The run's lifecycle over the run store. Only Commit creates SavedGame, SavedZone and ProvenanceLedger records; @@ -79,7 +84,8 @@ public static class RunSave Exit = Array.IndexOf(galaxy.Zones, galaxy.Exit), IsTutorial = galaxy.IsPrelude, ActionBarBindings = actionBar, - Credits = credits + Credits = credits, + Seed = galaxy.Seed }; // A zone generated earlier but not loaded this session has no live Contents; its packed contents carry over. diff --git a/tests/Aetheria.Shared.Tests/DemoTerminusTests.cs b/tests/Aetheria.Shared.Tests/DemoTerminusTests.cs index ae1895e75..54f46a57c 100644 --- a/tests/Aetheria.Shared.Tests/DemoTerminusTests.cs +++ b/tests/Aetheria.Shared.Tests/DemoTerminusTests.cs @@ -51,6 +51,8 @@ public sealed partial class RunStartTests var (game, zones) = RunSave.Capture(_cache, galaxy, arena, staged.Player, new SavedActionBarBinding[0], 0); RunSave.Commit(_cache, game, zones, _items.Lots); var restored = new Galaxy(_cache, _cache.GetGlobal(), _ => { }); + Assert.NotEqual(0u, galaxy.Seed); + Assert.Equal(galaxy.Seed, restored.Seed); Dictionary Standing(Galaxy g) => g.Factions.ToDictionary(faction => faction.Name, faction => g.FactionRelationships[faction]); Assert.Equal(new Dictionary diff --git a/tests/Aetheria.Shared.Tests/RunSaveTests.cs b/tests/Aetheria.Shared.Tests/RunSaveTests.cs index 3bd075724..1320b7ddd 100644 --- a/tests/Aetheria.Shared.Tests/RunSaveTests.cs +++ b/tests/Aetheria.Shared.Tests/RunSaveTests.cs @@ -170,6 +170,43 @@ public sealed class RunSaveTests : IDisposable Assert.Equal(4242, cache.GetGlobal().Credits); } + // The run's seed rides the save: Continue restores it, Capture writes it back, and a save from before run-seed + // restores zero. + [Fact] + public void TheRunSeedRidesTheSave() + { + using (var cache = Open()) + { + var game = Game(cache); + game.Seed = 0xC0FFEEu; + RunSave.Commit(cache, game, Zones(2, StageZoneContents(cache), 0), new ProvenanceLedger()); + } + + using (var cache = Open()) + { + var galaxy = new Galaxy(cache, cache.GetGlobal(), _ => { }); + Assert.Equal(0xC0FFEEu, galaxy.Seed); + var itemManager = new ItemManager(cache, new ProvenanceLedger(), TestSettings(), _ => { }); + galaxy.Zones[0].Contents = new Zone(itemManager, new PlanetSettings(), new ZonePack(), galaxy.Zones[0], galaxy); + var (game, zones) = RunSave.Capture(cache, galaxy, galaxy.Zones[0].Contents, null, new SavedActionBarBinding[0], 0); + Assert.Equal(0xC0FFEEu, game.Seed); + RunSave.Commit(cache, game, zones, RunSave.Lots(cache)); + } + + using (var cache = Open()) + Assert.Equal(0xC0FFEEu, cache.GetGlobal().Seed); + + // A save written before run-seed holds no seed, and continues on name-only drops. + using (var cache = Open()) + RunSave.Commit(cache, Game(cache), Zones(2, StageZoneContents(cache), 0), new ProvenanceLedger()); + + using (var cache = Open()) + { + Assert.Equal(0u, cache.GetGlobal().Seed); + Assert.Equal(0u, new Galaxy(cache, cache.GetGlobal(), _ => { }).Seed); + } + } + [Fact] public void ClearRemovesEveryRunRecord() { From f23d3c654204ccbebed90995883ab8e6976f8351 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 18 Oct 2026 17:30:50 +0200 Subject: [PATCH 3/7] Drops roll on the run's seed: Zone.DropSeed feeds the loot and mine dice DropSeed = CombatSeed ^ Mix(Galaxy.Seed), derived at construction. A zone with no galaxy, or a restored seed of 0, keeps its dice bit for bit; CombatSeed still roots shot, patrol and range-sample dice. RunSeedFixture builds a test zone's galaxy through RunSave.Commit and the Continue constructor. Co-Authored-By: Claude Sonnet 5.5 --- Assets/Scripts/ServerShared/FireControl.cs | 4 +-- Assets/Scripts/ServerShared/Zone.cs | 9 +++-- tests/Aetheria.Shared.Tests/LootTests.cs | 33 +++++++++++++++++-- tests/Aetheria.Shared.Tests/MineTests.cs | 28 +++++++++++++--- tests/Aetheria.Shared.Tests/RunSaveTests.cs | 9 ++++- tests/Aetheria.Shared.Tests/RunSeedFixture.cs | 26 +++++++++++++++ 6 files changed, 98 insertions(+), 11 deletions(-) create mode 100644 tests/Aetheria.Shared.Tests/RunSeedFixture.cs diff --git a/Assets/Scripts/ServerShared/FireControl.cs b/Assets/Scripts/ServerShared/FireControl.cs index 5e0a0dcc5..5ea06f455 100644 --- a/Assets/Scripts/ServerShared/FireControl.cs +++ b/Assets/Scripts/ServerShared/FireControl.cs @@ -775,7 +775,7 @@ public static class FireControl // Lays a mine for a mine layer: freezes the payload as evaluated now (stats, the item's blast radius, the // layer and its faction) and hands the body to Zone.Lay, the one adder. It launches at the shooter's velocity // plus the mount direction turned by a dice angle within +-Spread/2, times Velocity; the dice are the mine's - // own (zone seed, MineStream, body id), so no other roll is perturbed. No target, no PendingShot, no price: + // own (the zone's DropSeed, MineStream, body id), so no other roll is perturbed. No target, no PendingShot, no price: // InstantWeapon has already paid. A layer whose item has no blast radius lays nothing. public static void Lay(Weapon weapon, EquippedItem item, Entity source) { @@ -789,7 +789,7 @@ public static class FireControl var id = zone.NextBodyId(); var half = weapon.Spread / 2f; - var angle = radians(SimulationDice.For(zone.CombatSeed, SimulationDice.MineStream, id.Value).NextFloat(-half, half)); + var angle = radians(SimulationDice.For(zone.DropSeed, SimulationDice.MineStream, id.Value).NextFloat(-half, half)); var s = sin(angle); var c = cos(angle); var mount = MountDirection(item).xz; diff --git a/Assets/Scripts/ServerShared/Zone.cs b/Assets/Scripts/ServerShared/Zone.cs index 4db63be46..719c37e6f 100644 --- a/Assets/Scripts/ServerShared/Zone.cs +++ b/Assets/Scripts/ServerShared/Zone.cs @@ -126,11 +126,15 @@ public class Zone // of touching a shared stream. public uint CombatSeed { get; } + // The zone's drop dice (loot and mine scatter): CombatSeed mixed with its run's seed (ruling loot-seed-in-save), so a + // new game drops differently and a reloaded save repeats. Mix(0) is 0, so a zone with no galaxy rolls on CombatSeed. + public uint DropSeed { get; } + // The one source of floating item and mine ids: zone-scoped, from 1, never reused. Runtime state, not saved. private uint _lastBodyId; public FloatingBodyId NextBodyId() => new FloatingBodyId(++_lastBodyId); - // The death ordinal of the loot dice: the nth death in this zone rolls on SimulationDice.For(CombatSeed, LootStream, n). + // The death ordinal of the loot dice: the nth death in this zone rolls on SimulationDice.For(DropSeed, LootStream, n). private uint _deaths; // The zone-scoped agent ordinal, from 1, never reused: the key of a pilot's own dice. @@ -152,6 +156,7 @@ public class Zone Settings = settings; _orbitPositionAt = OrbitPositionAt; CombatSeed = galaxyZone?.Name.StableHash() ?? 1337u; + DropSeed = CombatSeed ^ SimulationDice.Mix(galaxy?.Seed ?? 0u); var cache = itemManager.ItemData; // Cut 5, 5.6 (docs/fire-control-cut.md, Soul finding 7; operator ruling Q3): death removes the ship, @@ -452,7 +457,7 @@ public class Zone private void DropLoot(Entity dead) { var settings = _itemManager.GameplaySettings; - var random = SimulationDice.For(CombatSeed, SimulationDice.LootStream, ++_deaths); + var random = SimulationDice.For(DropSeed, SimulationDice.LootStream, ++_deaths); var at = new float2(dead.Position.x, dead.Position.z); float2 Heading() { var angle = random.NextFloat() * 2f * PI; return float2(cos(angle), sin(angle)); } foreach (var equipped in dead.Equipment.ToArray()) diff --git a/tests/Aetheria.Shared.Tests/LootTests.cs b/tests/Aetheria.Shared.Tests/LootTests.cs index f0b52f4ce..ae47f6b32 100644 --- a/tests/Aetheria.Shared.Tests/LootTests.cs +++ b/tests/Aetheria.Shared.Tests/LootTests.cs @@ -61,7 +61,7 @@ public sealed class LootTests : IDisposable } private Lab Build(string zoneName = "Loot", float probability = .5f, float lifetime = 30f, float velocity = 25f, - bool well = false, float spread = 0f) + bool well = false, float spread = 0f, uint? runSeed = null) { var settings = new GameplaySettings { @@ -104,6 +104,7 @@ public sealed class LootTests : IDisposable cache.Upsert(new CargoBayData { Name = "Crate", Shape = new Shape(), InteriorShape = Solid(4, 4), Durability = 4 }); cache.Upsert(new SimpleCommodityData { Name = "Ore", Shape = Solid(1, 1), MaxStack = 100 }); cache.FlushAsync().Wait(); + var galaxy = runSeed.HasValue ? RunSeedFixture.Continue(cache, zoneName, runSeed.Value) : null; var ledger = new ProvenanceLedger(); for (var i = 1; i <= 9000; i++) ledger.Lots[i] = new Lot { Origin = new Attributed(), Quality = 1 }; @@ -111,7 +112,9 @@ public sealed class LootTests : IDisposable var planets = new PlanetSettings(); var pack = new ZonePack { Radius = well ? 1000 : 0 }; if (well) WellFixture.Dig(cache, pack, planets, 300, 100); - var zone = new Zone(items, planets, pack, new GalaxyZone { Name = zoneName, Owner = null }, null); + var zone = galaxy != null + ? new Zone(items, planets, pack, galaxy.Zones[0], galaxy) + : new Zone(items, planets, pack, new GalaxyZone { Name = zoneName, Owner = null }, null); return new Lab { Items = items, Zone = zone }; } @@ -209,6 +212,32 @@ public sealed class LootTests : IDisposable Assert.True(Differ(dropsA, Released(first, count))); } + private List<(string Item, float2 Position, float2 Velocity)> DropsAt(string zoneName, uint runSeed) + { + var lab = Build(zoneName, runSeed: runSeed); + Kill(Spawn(lab, float3(31, 0, -17))); + return Released(lab, 0); + } + + // A new game rolls different drops from other games: the same ship, killed in the same zone, at another run seed. + [Fact] + public void ANewGameRollsDifferentDrops() + { + var one = DropsAt("Dice", 1); + Assert.True(one.Count > 2, "the fixture must drop enough for a draw to show"); + Assert.True(Differ(one, DropsAt("Dice", 2))); + } + + // Reloading one save repeats its drops, and the run's other zones still roll on their own names. + [Fact] + public void ReloadingASaveRepeatsItsDrops() + { + var first = DropsAt("Dice", 7); + Assert.True(first.Count > 2, "the fixture must drop enough for a draw to show"); + AssertSameDrops(first, DropsAt("Dice", 7)); + Assert.True(Differ(first, DropsAt("Other dice", 7))); + } + [Fact] public void GearDropsByProbabilityCargoAlways() { diff --git a/tests/Aetheria.Shared.Tests/MineTests.cs b/tests/Aetheria.Shared.Tests/MineTests.cs index a2deeb5cf..24269af1a 100644 --- a/tests/Aetheria.Shared.Tests/MineTests.cs +++ b/tests/Aetheria.Shared.Tests/MineTests.cs @@ -67,7 +67,7 @@ public sealed class MineTests : IDisposable private Lab Build(float blast = 4f, float arming = 2f, float fuse = 2f, float life = 30f, float spread = 0f, float velocity = 0f, float damage = 100f, float range = 1000f, float cooldown = 1000f, Faction layerFaction = null, WeaponFuse? fuseKind = WeaponFuse.Proximity, bool well = false, float3? layerAt = null, int magazine = 100, - float minRange = 0f) + float minRange = 0f, uint? runSeed = null) { var settings = new GameplaySettings { @@ -114,6 +114,7 @@ public sealed class MineTests : IDisposable cache.Upsert(Gear("Shield", new ShieldData { Efficiency = Constant(1), EnergyUsage = Constant(1), Capacity = Constant(1000000), RefillDuration = Constant(.01f), RestoreDuration = Constant(.01f) }, 10)); cache.Upsert(Gear("TinyShield", new ShieldData { Efficiency = Constant(1), EnergyUsage = Constant(1), Capacity = Constant(.001f), RefillDuration = Constant(.01f), RestoreDuration = Constant(.01f) }, 10)); cache.FlushAsync().Wait(); + var galaxy = runSeed.HasValue ? RunSeedFixture.Continue(cache, "Mines", runSeed.Value) : null; var ledger = new ProvenanceLedger(); for (var i = 1; i <= 500; i++) ledger.Lots[i] = new Lot { Origin = new Attributed(), Quality = 1 }; @@ -121,7 +122,9 @@ public sealed class MineTests : IDisposable var planets = new PlanetSettings(); var pack = new ZonePack { Radius = well ? 1000 : 0 }; if (well) WellFixture.Dig(cache, pack, planets, 300, 100); - var zone = new Zone(items, planets, pack, new GalaxyZone { Name = "Mines", Owner = null }, null); + var zone = galaxy != null + ? new Zone(items, planets, pack, galaxy.Zones[0], galaxy) + : new Zone(items, planets, pack, new GalaxyZone { Name = "Mines", Owner = null }, null); var lab = new Lab { Items = items, Zone = zone, Data = data, ItemData = cache.GetByName("Mine Layer") }; var layerRef = items.ItemData.RefOf(items.ItemData.GetByName("Layer")); @@ -683,6 +686,23 @@ public sealed class MineTests : IDisposable Assert.False(FireControl.Refuses(lab.Weapon, lab.Layer)); } + // A new game scatters its mines differently: the same lay, at another run seed, launches at another velocity. + [Fact] + public void ANewGameScattersMinesDifferently() + { + float2 LaunchAt(uint runSeed) + { + var lab = Build(spread: 40f, velocity: 5f, layerFaction: A, runSeed: runSeed); + var atLay = default(KinematicBody); + using var sub = lab.Zone.Mines.ObserveAdd().Subscribe(e => atLay = e.Value.Body); + lab.Layer.Direction = normalize(float2(1, 2)); + LayOne(lab); + return atLay.Velocity; + } + + Assert.NotEqual(LaunchAt(1), LaunchAt(2)); + } + [Fact] public void TheLaunchAngleIsTheMinesOwnDice() { @@ -697,9 +717,9 @@ public sealed class MineTests : IDisposable var mine = LayOne(lab); - // The angle is the first draw of (zone seed, mine stream, body id), in +-Spread/2, turning the mount + // The angle is the first draw of (the zone's DropSeed, mine stream, body id), in +-Spread/2, turning the mount // counterclockwise in the (x, y) plane. - var degreesTurned = SimulationDice.For(lab.Zone.CombatSeed, SimulationDice.MineStream, mine.Id.Value).NextFloat(-20f, 20f); + var degreesTurned = SimulationDice.For(lab.Zone.DropSeed, SimulationDice.MineStream, mine.Id.Value).NextFloat(-20f, 20f); var a = radians(degreesTurned); var m = FireControl.MountDirection(lab.WeaponItem).xz; var turned = float2(m.x * cos(a) - m.y * sin(a), m.x * sin(a) + m.y * cos(a)); diff --git a/tests/Aetheria.Shared.Tests/RunSaveTests.cs b/tests/Aetheria.Shared.Tests/RunSaveTests.cs index 1320b7ddd..7e32c9928 100644 --- a/tests/Aetheria.Shared.Tests/RunSaveTests.cs +++ b/tests/Aetheria.Shared.Tests/RunSaveTests.cs @@ -188,6 +188,8 @@ public sealed class RunSaveTests : IDisposable Assert.Equal(0xC0FFEEu, galaxy.Seed); var itemManager = new ItemManager(cache, new ProvenanceLedger(), TestSettings(), _ => { }); galaxy.Zones[0].Contents = new Zone(itemManager, new PlanetSettings(), new ZonePack(), galaxy.Zones[0], galaxy); + // The zone's drop dice are its name's seed mixed with the run's. + Assert.Equal("Zone 0".StableHash() ^ SimulationDice.Mix(0xC0FFEEu), galaxy.Zones[0].Contents.DropSeed); var (game, zones) = RunSave.Capture(cache, galaxy, galaxy.Zones[0].Contents, null, new SavedActionBarBinding[0], 0); Assert.Equal(0xC0FFEEu, game.Seed); RunSave.Commit(cache, game, zones, RunSave.Lots(cache)); @@ -203,7 +205,12 @@ public sealed class RunSaveTests : IDisposable using (var cache = Open()) { Assert.Equal(0u, cache.GetGlobal().Seed); - Assert.Equal(0u, new Galaxy(cache, cache.GetGlobal(), _ => { }).Seed); + var galaxy = new Galaxy(cache, cache.GetGlobal(), _ => { }); + Assert.Equal(0u, galaxy.Seed); + var itemManager = new ItemManager(cache, new ProvenanceLedger(), TestSettings(), _ => { }); + var zone = new Zone(itemManager, new PlanetSettings(), new ZonePack(), galaxy.Zones[0], galaxy); + Assert.Equal("Zone 0".StableHash(), zone.CombatSeed); + Assert.Equal(zone.CombatSeed, zone.DropSeed); } } diff --git a/tests/Aetheria.Shared.Tests/RunSeedFixture.cs b/tests/Aetheria.Shared.Tests/RunSeedFixture.cs new file mode 100644 index 000000000..0152584b7 --- /dev/null +++ b/tests/Aetheria.Shared.Tests/RunSeedFixture.cs @@ -0,0 +1,26 @@ +using System.Collections.Generic; +using GameCult.Caching; + +// A fixture's run at a fixed seed, restored as Continue restores it: one zone, committed through RunSave.Commit and +// read back by the Galaxy save constructor, so a test's galaxy carries the seed the way a real run's does. +internal static class RunSeedFixture +{ + public static Galaxy Continue(CultCache cache, string zoneName, uint seed) + { + var game = new SavedGame + { + Seed = seed, + Factions = new CultRecordRef[0], + Relationships = new FactionRelationship[0], + HomeZones = new Dictionary(), + BossZones = new Dictionary(), + DiscoveredZones = new[] { 0 }, + ActionBarBindings = new SavedActionBarBinding[0], + Entrance = 0, + Exit = -1 + }; + var zone = new SavedZone { Name = zoneName, AdjacentZones = new int[0], Factions = new int[0], Owner = -1 }; + RunSave.Commit(cache, game, new[] { zone }, new ProvenanceLedger()); + return new Galaxy(cache, cache.GetGlobal(), _ => { }); + } +} From 727c4096595cf589658c59d83a8fcf226b1b1888 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 18 Oct 2026 17:33:11 +0200 Subject: [PATCH 4/7] Pin the dice of a zone without a run seed at their pre-run-seed values A zone with no galaxy and a zone of a save written before run-seed (seed 0) drop the items and scatter the mines they did at eb27ced1; values computed at that base. Co-Authored-By: Claude Sonnet 5.5 --- tests/Aetheria.Shared.Tests/LootTests.cs | 25 ++++++++++++++++++ tests/Aetheria.Shared.Tests/MineTests.cs | 32 ++++++++++++++++-------- 2 files changed, 47 insertions(+), 10 deletions(-) diff --git a/tests/Aetheria.Shared.Tests/LootTests.cs b/tests/Aetheria.Shared.Tests/LootTests.cs index ae47f6b32..e8a5016a7 100644 --- a/tests/Aetheria.Shared.Tests/LootTests.cs +++ b/tests/Aetheria.Shared.Tests/LootTests.cs @@ -238,6 +238,31 @@ public sealed class LootTests : IDisposable Assert.True(Differ(first, DropsAt("Other dice", 7))); } + // Pinned at eb27ced1, before run-seed: a zone with no galaxy, and a zone of a save written before run-seed (seed 0), + // roll on the zone's name alone and drop exactly what they dropped. + [Theory] + [InlineData(false)] + [InlineData(true)] + public void AZoneWithoutARunSeedKeepsItsDrops(bool restoredFromAnOldSave) + { + var drops = restoredFromAnOldSave ? DropsAt("Dice", 0) : Released(KillInNewLab("Dice"), 0); + Assert.Equal(new[] { "Spare", "Orex7", "Spare" }, drops.Select(d => d.Item)); + Assert.All(drops, d => Assert.Equal(new float2(31, -17), d.Position)); + var velocities = new[] { float2(-17.977364f, 17.372805f), float2(-5.908107f, 24.291857f), float2(-24.228783f, -6.1616626f) }; + for (var i = 0; i < velocities.Length; i++) + { + Assert.Equal(velocities[i].x, drops[i].Velocity.x, 3); + Assert.Equal(velocities[i].y, drops[i].Velocity.y, 3); + } + } + + private Lab KillInNewLab(string zoneName) + { + var lab = Build(zoneName); + Kill(Spawn(lab, float3(31, 0, -17))); + return lab; + } + [Fact] public void GearDropsByProbabilityCargoAlways() { diff --git a/tests/Aetheria.Shared.Tests/MineTests.cs b/tests/Aetheria.Shared.Tests/MineTests.cs index 24269af1a..137d3f84a 100644 --- a/tests/Aetheria.Shared.Tests/MineTests.cs +++ b/tests/Aetheria.Shared.Tests/MineTests.cs @@ -690,19 +690,31 @@ public sealed class MineTests : IDisposable [Fact] public void ANewGameScattersMinesDifferently() { - float2 LaunchAt(uint runSeed) - { - var lab = Build(spread: 40f, velocity: 5f, layerFaction: A, runSeed: runSeed); - var atLay = default(KinematicBody); - using var sub = lab.Zone.Mines.ObserveAdd().Subscribe(e => atLay = e.Value.Body); - lab.Layer.Direction = normalize(float2(1, 2)); - LayOne(lab); - return atLay.Velocity; - } - Assert.NotEqual(LaunchAt(1), LaunchAt(2)); } + private float2 LaunchAt(uint? runSeed) + { + var lab = Build(spread: 40f, velocity: 5f, layerFaction: A, runSeed: runSeed); + var atLay = default(KinematicBody); + using var sub = lab.Zone.Mines.ObserveAdd().Subscribe(e => atLay = e.Value.Body); + lab.Layer.Direction = normalize(float2(1, 2)); + LayOne(lab); + return atLay.Velocity; + } + + // Pinned at eb27ced1, before run-seed: a zone with no galaxy, and a zone of a save written before run-seed (seed 0), + // scatter their mines on the zone's name alone. + [Theory] + [InlineData(false)] + [InlineData(true)] + public void AZoneWithoutARunSeedKeepsItsMineScatter(bool restoredFromAnOldSave) + { + var velocity = LaunchAt(restoredFromAnOldSave ? 0u : (uint?) null); + Assert.Equal(3.2546253f, velocity.x, 3); + Assert.Equal(3.795711f, velocity.y, 3); + } + [Fact] public void TheLaunchAngleIsTheMinesOwnDice() { From 47f834ea23b4c8939316972c9033b208e57c4cfb Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 18 Oct 2026 17:39:52 +0200 Subject: [PATCH 5/7] Pin all five pre-run-seed drops of the dice zone (the first pin kept three) Co-Authored-By: Claude Sonnet 5.5 --- tests/Aetheria.Shared.Tests/LootTests.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/Aetheria.Shared.Tests/LootTests.cs b/tests/Aetheria.Shared.Tests/LootTests.cs index e8a5016a7..ecd65f8e1 100644 --- a/tests/Aetheria.Shared.Tests/LootTests.cs +++ b/tests/Aetheria.Shared.Tests/LootTests.cs @@ -246,9 +246,13 @@ public sealed class LootTests : IDisposable public void AZoneWithoutARunSeedKeepsItsDrops(bool restoredFromAnOldSave) { var drops = restoredFromAnOldSave ? DropsAt("Dice", 0) : Released(KillInNewLab("Dice"), 0); - Assert.Equal(new[] { "Spare", "Orex7", "Spare" }, drops.Select(d => d.Item)); + Assert.Equal(new[] { "Spare", "Spare", "Spare", "Orex7", "Spare" }, drops.Select(d => d.Item)); Assert.All(drops, d => Assert.Equal(new float2(31, -17), d.Position)); - var velocities = new[] { float2(-17.977364f, 17.372805f), float2(-5.908107f, 24.291857f), float2(-24.228783f, -6.1616626f) }; + var velocities = new[] + { + float2(-14.870228f, -20.096674f), float2(-4.9265895f, -24.509768f), float2(-17.977364f, 17.372805f), + float2(-5.908107f, 24.291857f), float2(-24.228783f, -6.1616626f) + }; for (var i = 0; i < velocities.Length; i++) { Assert.Equal(velocities[i].x, drops[i].Velocity.x, 3); From 0ed0605aa5a88af20af27ebe200c0814d8a908ca Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 18 Oct 2026 20:13:03 +0200 Subject: [PATCH 6/7] Pin the run seed's edges: AI dice stay on CombatSeed, full-width seed, refusal names its field, refusal test cannot hang - ConsumableAiTests.ThePilotsDiceDoNotMoveWithTheRunSeed: sample offset and patrol circuit equal at different run seeds. - RunSaveTests.TheRunSeedRidesTheSave uses a seed with the top bit set. - RunStartTests.AGeneratedGalaxyRefusesSeedZero builds from authored settings (small ZoneCount) so a constructor that stops refusing fails by assertion, and pins ParamName and no ActualValue. Co-Authored-By: Claude Sonnet 5.5 --- .../ConsumableAiTests.cs | 31 +++++++++++++++++-- tests/Aetheria.Shared.Tests/RunSaveTests.cs | 10 +++--- tests/Aetheria.Shared.Tests/RunStartTests.cs | 22 ++++++++++--- 3 files changed, 50 insertions(+), 13 deletions(-) diff --git a/tests/Aetheria.Shared.Tests/ConsumableAiTests.cs b/tests/Aetheria.Shared.Tests/ConsumableAiTests.cs index 6ad889fda..2f5863958 100644 --- a/tests/Aetheria.Shared.Tests/ConsumableAiTests.cs +++ b/tests/Aetheria.Shared.Tests/ConsumableAiTests.cs @@ -50,6 +50,7 @@ public sealed class ConsumableAiTests : IDisposable public GearData HeavyGun; public CultRecordRef Maker; public float Optimum; + public CultRecordKey[] Circuit; public void Store(ConsumableItemData design) => Assert.True(Hold.TryStore((ItemInstance) Items.CreateInstance(Items.CreateLot(design, Maker, .5f)))); @@ -97,7 +98,7 @@ public sealed class ConsumableAiTests : IDisposable } }; - private Scene Build(bool carryVent = false, bool carryOverdrive = false, bool carryDecoy = false, int stackableVents = 0, float effective = 500f) + private Scene Build(bool carryVent = false, bool carryOverdrive = false, bool carryDecoy = false, int stackableVents = 0, float effective = 500f, uint? runSeed = null, int orbits = 1) { var cache = AetheriaStores.Open(Path.Combine(_root, Guid.NewGuid().ToString("N") + ".cc"), catalogWritable: true); _caches.Add(cache); @@ -150,7 +151,10 @@ public sealed class ConsumableAiTests : IDisposable var maker = cache.RefOf(cache.GetByName("Maker")); EquippableItem Make(string name) where T : CraftedItemData => (EquippableItem) items.CreateInstance(items.CreateLot(cache.GetByName(name), maker, .5f)); - var zone = new Zone(items, new PlanetSettings(), new ZonePack(), new GalaxyZone { Name = "Test Zone", Owner = null }, null); + var galaxy = runSeed.HasValue ? RunSeedFixture.Continue(cache, "Test Zone", runSeed.Value) : null; + var zone = galaxy != null + ? new Zone(items, new PlanetSettings(), new ZonePack(), galaxy.Zones[0], galaxy) + : new Zone(items, new PlanetSettings(), new ZonePack(), new GalaxyZone { Name = "Test Zone", Owner = null }, null); Ship NewShip(string gear) { var ship = new Ship(items, zone, Make("Skiff"), new EntitySettings()) @@ -174,8 +178,10 @@ public sealed class ConsumableAiTests : IDisposable // The agent joins as the zone admits a piloted ship (its Minion and CombatState come from production); the // locker is a gun ship nobody pilots. // One fixed orbit far from the fight, before the agent joins: a pilot that is not engaged patrols it, as production's would. - zone.Orbits.Add(new CultRecordKey("test-orbit"), new Orbit(new OrbitData { FixedPosition = float2(0, 5000) })); + for (var i = 0; i < orbits; i++) + zone.Orbits.Add(new CultRecordKey(orbits == 1 ? "test-orbit" : "test-orbit-" + i), new Orbit(new OrbitData { FixedPosition = float2(0, 5000) })); zone.Admit(scene.Agent, piloted: true); + scene.Circuit = ((PatrolOrbitsTask) zone.Agents.Single().Task).Circuit; zone.Admit(scene.Prey, piloted: false); zone.Admit(scene.Locker, piloted: false); // The flight is the only writer of the agent's Target: the agent senses the prey and holds a grudge on it, so the @@ -356,6 +362,25 @@ public sealed class ConsumableAiTests : IDisposable // The production path for the optimum: a ship the zone admits as piloted derives it from its gun's live stats // before it needs it, and re-derives it when its loadout changes. The gun's effective range is 500 and the // heavy gun's is 800, so the optimum moves with the weapons it has now. + // The run seed feeds drops and mines only (ruling run-seed-drops-only): the same pilot in the same zone samples its + // range with the same offset and draws the same patrol circuit at every run seed. Kills: the combat-sample dice or + // the patrol dice read DropSeed. + [Theory] + [InlineData(1u, 2u)] + [InlineData(0xC0FFEE42u, 0x2468ACE1u)] + public void ThePilotsDiceDoNotMoveWithTheRunSeed(uint first, uint second) + { + var a = Build(runSeed: first, orbits: 8); + var b = Build(runSeed: second, orbits: 8); + a.AssertInCombat(); + b.AssertInCombat(); + Assert.NotEqual(a.Zone.DropSeed, b.Zone.DropSeed); + Assert.Equal(a.Zone.CombatSeed, b.Zone.CombatSeed); + Assert.Equal(a.Optimum, b.Optimum); + Assert.Equal(4, a.Circuit.Length); + Assert.Equal(a.Circuit, b.Circuit); + } + [Fact] public void ASpawnedShipsOptimumFollowsItsWeaponsAndNeverReadsZero() { diff --git a/tests/Aetheria.Shared.Tests/RunSaveTests.cs b/tests/Aetheria.Shared.Tests/RunSaveTests.cs index 7e32c9928..456561d76 100644 --- a/tests/Aetheria.Shared.Tests/RunSaveTests.cs +++ b/tests/Aetheria.Shared.Tests/RunSaveTests.cs @@ -178,25 +178,25 @@ public sealed class RunSaveTests : IDisposable using (var cache = Open()) { var game = Game(cache); - game.Seed = 0xC0FFEEu; + game.Seed = 0xC0FFEE42u; RunSave.Commit(cache, game, Zones(2, StageZoneContents(cache), 0), new ProvenanceLedger()); } using (var cache = Open()) { var galaxy = new Galaxy(cache, cache.GetGlobal(), _ => { }); - Assert.Equal(0xC0FFEEu, galaxy.Seed); + Assert.Equal(0xC0FFEE42u, galaxy.Seed); var itemManager = new ItemManager(cache, new ProvenanceLedger(), TestSettings(), _ => { }); galaxy.Zones[0].Contents = new Zone(itemManager, new PlanetSettings(), new ZonePack(), galaxy.Zones[0], galaxy); // The zone's drop dice are its name's seed mixed with the run's. - Assert.Equal("Zone 0".StableHash() ^ SimulationDice.Mix(0xC0FFEEu), galaxy.Zones[0].Contents.DropSeed); + Assert.Equal("Zone 0".StableHash() ^ SimulationDice.Mix(0xC0FFEE42u), galaxy.Zones[0].Contents.DropSeed); var (game, zones) = RunSave.Capture(cache, galaxy, galaxy.Zones[0].Contents, null, new SavedActionBarBinding[0], 0); - Assert.Equal(0xC0FFEEu, game.Seed); + Assert.Equal(0xC0FFEE42u, game.Seed); RunSave.Commit(cache, game, zones, RunSave.Lots(cache)); } using (var cache = Open()) - Assert.Equal(0xC0FFEEu, cache.GetGlobal().Seed); + Assert.Equal(0xC0FFEE42u, cache.GetGlobal().Seed); // A save written before run-seed holds no seed, and continues on name-only drops. using (var cache = Open()) diff --git a/tests/Aetheria.Shared.Tests/RunStartTests.cs b/tests/Aetheria.Shared.Tests/RunStartTests.cs index 7c22bb911..4b181f67e 100644 --- a/tests/Aetheria.Shared.Tests/RunStartTests.cs +++ b/tests/Aetheria.Shared.Tests/RunStartTests.cs @@ -115,14 +115,26 @@ public sealed partial class RunStartTests : IDisposable private const float Freezing = 273.15f; - // One minter: a generated galaxy never reads the clock, so seed 0 is refused by both generation constructors. + // One minter: a generated galaxy never reads the clock, so seed 0 is refused by both generation constructors, naming + // the field and echoing no input (no ActualValue). The settings are the authored ones: a galaxy that stops refusing must be able to + // finish generating, so the test fails by assertion rather than wedging in the generator. [Fact] public void AGeneratedGalaxyRefusesSeedZero() { - Assert.Throws(() => - new Galaxy(new SectorGenerationSettings(), new SectorBackgroundSettings(), _cache, _ => { }, null, 0)); - Assert.Throws(() => - new Galaxy(new TutorialGenerationSettings(), new SectorBackgroundSettings(), _cache, new PlayerSettings(), new DirectoryInfo(_root), _ => { }, null, 0)); + var authored = AuthoredSettings.Load(FindRepoRoot()); + var background = authored.Read("SectorBackgroundSettings"); + var sector = authored.Read("SectorGenerationSettings"); + sector.ZoneCount = 8; + var tutorial = authored.Read("TutorialGenerationSettings"); + + var main = Assert.Throws(() => new Galaxy(sector, background, _cache, _ => { }, null, 0)); + var prelude = Assert.Throws(() => + new Galaxy(tutorial, background, _cache, new PlayerSettings(), new DirectoryInfo(_root), _ => { }, null, 0)); + foreach (var refusal in new[] { main, prelude }) + { + Assert.Equal("seed", refusal.ParamName); + Assert.Null(refusal.ActualValue); + } } // A main-sector galaxy: each station's gear must come from manufacturers its faction can reach, the makers its From cb1c093e84b90ea634a708a0eb6bf06ba30f0dce Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 18 Oct 2026 20:21:31 +0200 Subject: [PATCH 7/7] ConsumableAiTests: open the run store so a scene can continue a run at a fixed seed Co-Authored-By: Claude Sonnet 5.5 --- tests/Aetheria.Shared.Tests/ConsumableAiTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Aetheria.Shared.Tests/ConsumableAiTests.cs b/tests/Aetheria.Shared.Tests/ConsumableAiTests.cs index 2f5863958..7478b3440 100644 --- a/tests/Aetheria.Shared.Tests/ConsumableAiTests.cs +++ b/tests/Aetheria.Shared.Tests/ConsumableAiTests.cs @@ -100,7 +100,7 @@ public sealed class ConsumableAiTests : IDisposable private Scene Build(bool carryVent = false, bool carryOverdrive = false, bool carryDecoy = false, int stackableVents = 0, float effective = 500f, uint? runSeed = null, int orbits = 1) { - var cache = AetheriaStores.Open(Path.Combine(_root, Guid.NewGuid().ToString("N") + ".cc"), catalogWritable: true); + var cache = AetheriaStores.Open(Path.Combine(_root, Guid.NewGuid().ToString("N") + ".cc"), Path.Combine(_root, Guid.NewGuid().ToString("N") + ".run.cc"), catalogWritable: true); _caches.Add(cache); cache.Upsert(new VerseGrammar { Revision = 1 }); cache.Upsert(new Faction { Name = "Maker", ShortName = "MKR" });