From f23d3c654204ccbebed90995883ab8e6976f8351 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 18 Oct 2026 17:30:50 +0200 Subject: [PATCH] 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(), _ => { }); + } +}