From 7b73e513463c5fe2cfb279c1831e09050386f43d Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 10 Oct 2026 05:32:50 +0200 Subject: [PATCH 01/11] Consumable supply: scarce stock at stations and on ships through the one availability rule LoadoutGenerator.AvailableProducts and RandomProducts now range over CraftedItemData; only equippable gear needs a hull home. StockConsumables is the last step of GenerateShipLoadout and GenerateStationLoadout: it draws nothing when no consumable is on offer. Unbuilt/untested at this commit. Co-Authored-By: Claude Opus 5.5 --- .../Scripts/ServerShared/LoadoutGenerator.cs | 38 ++- .../ConsumableSupplyTests.cs | 230 ++++++++++++++++++ 2 files changed, 264 insertions(+), 4 deletions(-) create mode 100644 tests/Aetheria.Shared.Tests/ConsumableSupplyTests.cs diff --git a/Assets/Scripts/ServerShared/LoadoutGenerator.cs b/Assets/Scripts/ServerShared/LoadoutGenerator.cs index d251a2d45..9566b19c3 100644 --- a/Assets/Scripts/ServerShared/LoadoutGenerator.cs +++ b/Assets/Scripts/ServerShared/LoadoutGenerator.cs @@ -16,6 +16,14 @@ public class LoadoutGenerator public Faction Faction { get; } public float PriceExponent { get; } + // Consumables are scarce (operator, 2026-10-09): a quarter of stations stock one product, one ship in ten carries + // one unit, and a carried consumable becomes loot when its ship dies. The units are the most a roll can stock; a + // station draws 1 to the maximum. + public const float StationConsumableChance = .25f; + public const int StationConsumableUnits = 2; + public const float ShipConsumableChance = .1f; + public const int ShipConsumableUnits = 1; + public LoadoutGenerator( ref Random random, ItemManager itemManager, @@ -46,6 +54,7 @@ public class LoadoutGenerator var entity = new Ship(ItemManager, null, hull, ItemManager.GameplaySettings.DefaultEntitySettings); entity.Faction = Faction; OutfitEntity(entity); + StockConsumables(entity, ShipConsumableChance, ShipConsumableUnits); return EntitySerializer.Pack(entity); } @@ -117,11 +126,32 @@ public class LoadoutGenerator cargo.TryStore(instance); } + StockConsumables(entity, StationConsumableChance, StationConsumableUnits); + entity.CanTow = hullData.CanTow; return EntitySerializer.Pack(entity) as OrbitalEntityPack; } + // The last thing a loadout does. With no consumable on offer, or no hold to put one in, nothing is drawn, so a + // catalog without consumables generates exactly what it did before them. Otherwise one roll decides whether the + // entity has any, and a hit stocks 1 to maxUnits instances of one product, stopping when no hold takes another. + private void StockConsumables(Entity entity, float chance, int maxUnits) + { + var (available, _) = AvailableProducts(null, false); + if (available.Length == 0 || !entity.CargoBays.Any()) return; + if (Random.NextFloat() >= chance) return; + + var (product, design) = RandomProducts(1, 0).FirstOrDefault(); + if (design == null) return; + var units = Random.NextInt(1, maxUnits + 1); + for (var i = 0; i < units; i++) + { + var instance = ItemManager.CreateInstance(product); + if (!entity.CargoBays.Any(bay => bay.TryStore(instance))) break; + } + } + private static bool IsHeater(GearData item) => item.HardpointType == HardpointType.Tool && item.Behaviors.Any(b => b is ThermotoggleData { HighPass: false }) && @@ -139,7 +169,7 @@ public class LoadoutGenerator // Products normally come only from manufacturers present in the galaxy and known to the zone's faction; a // required item falls back to any manufacturer when none of those make one, which means the product table // lacks variety, so it is logged rather than hidden. - public (FactionProductData product, T design)[] RandomProducts(int count, float sizeExponent, Predicate filter = null, bool required = false) where T : EquippableItemData + public (FactionProductData product, T design)[] RandomProducts(int count, float sizeExponent, Predicate filter = null, bool required = false) where T : CraftedItemData { var (available, preferManufacturers) = AvailableProducts(filter, required); return available.WeightedRandomElements(ref Random, entry => @@ -149,9 +179,9 @@ public class LoadoutGenerator count); } - // The products this faction can be offered for a design kind, and whether manufacturer preference applies (it does + // The products this faction can be offered for a design kind (only gear needs a home), and whether manufacturer preference applies (it does // not once a required item has fallen back to any manufacturer). The one availability rule for every selection. - private ((FactionProductData product, T design)[] available, bool preferManufacturers) AvailableProducts(Predicate filter, bool required) where T : EquippableItemData + private ((FactionProductData product, T design)[] available, bool preferManufacturers) AvailableProducts(Predicate filter, bool required) where T : CraftedItemData { var hulls = ItemManager.ItemData.GetAll().ToArray(); var candidates = ItemManager.ItemData.GetAll() @@ -160,7 +190,7 @@ public class LoadoutGenerator entry.design != null && entry.design.Price > 0 && entry.product.Manufacturer.IsSet() && - HasHome(entry.design, hulls) && + (!(entry.design is EquippableItemData equippable) || HasHome(equippable, hulls)) && (filter?.Invoke(entry.design) ?? true)) .ToArray(); var available = candidates.Where(entry => IsAvailable(entry.product)).ToArray(); diff --git a/tests/Aetheria.Shared.Tests/ConsumableSupplyTests.cs b/tests/Aetheria.Shared.Tests/ConsumableSupplyTests.cs new file mode 100644 index 000000000..362b0e283 --- /dev/null +++ b/tests/Aetheria.Shared.Tests/ConsumableSupplyTests.cs @@ -0,0 +1,230 @@ +/* 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.IO; +using System.Linq; +using CultMath; +using GameCult.Caching; +using Xunit; +using Random = CultMath.Random; + +// Consumables are scarce (ruling consumables-scarce): a quarter of stations stock one product, one ship in ten carries +// one unit, and the one availability rule (LoadoutGenerator.AvailableProducts) decides which products a faction is +// offered. Every count here is measured on packs the production generator returns, from fixed seeds, so the runs are +// deterministic; the 3-sigma band is the binomial one, sigma = sqrt(n p (1 - p)) with n = 400 seeds, and a rule that +// breaks (chance 1, chance 0, an unconditional roll) lands far outside it. +public sealed class ConsumableSupplyTests : IDisposable +{ + private const int Seeds = 400; + + private readonly string _root = Path.Combine(Path.GetTempPath(), "aetheria-consumablesupply-" + Guid.NewGuid().ToString("N")); + private readonly List _caches = new List(); + private int _rigs; + + public ConsumableSupplyTests() => Directory.CreateDirectory(_root); + + public void Dispose() + { + foreach (var cache in _caches) cache.Dispose(); + Directory.Delete(_root, true); + } + + private enum Offer { None, Reachable, Stranger } + + // One catalog: a ship hull, a station hull and the station's required fittings, all made by Maker; and, when + // offered, one consumable design with a product from each of Maker, its ally and (unless the offer is Stranger + // only) a stranger Maker's allegiance does not name. + private sealed class Rig + { + public CultCache Cache; + public ItemManager Items; + public Galaxy Galaxy; + public Faction Maker; + public LoadoutGenerator Generator(ref Random random) => new LoadoutGenerator(ref random, Items, Galaxy, null, Maker, .5f); + } + + private static Shape Solid(int w, int h) + { + var shape = new Shape(w, h); + foreach (var cell in shape.AllCoordinates) shape[cell] = true; + return shape; + } + + private Rig Build(Offer offer, bool holdless = false) + { + var root = Path.Combine(_root, "rig" + _rigs++); + Directory.CreateDirectory(root); + var catalog = Path.Combine(root, "Aetheria.cc"); + CultRecordRef maker; + using (var seed = AetheriaStores.Open(catalog, catalogWritable: true)) + { + seed.Upsert(new TestCatalogGlobal { Name = "Temperament" }); + var ally = seed.Upsert(new Faction { Name = "Ally", ShortName = "ALY" }); + var stranger = seed.Upsert(new Faction { Name = "Stranger", ShortName = "STR" }); + maker = seed.Upsert(new Faction { Name = "Maker", ShortName = "MKR", Allegiance = { { ally, 1f } } }); + + var skiff = seed.Upsert(new HullData { Name = "Skiff", HullType = HullType.Ship, Shape = Solid(5, 5), Price = 100 }); + var platform = seed.Upsert(new HullData { Name = "Platform", HullType = HullType.Station, Shape = Solid(5, 5), Price = 100 }); + // A hold with no cell cannot take a consumable; the others take up to eight. + var crate = seed.Upsert(new CargoBayData + { + Name = "Crate", Shape = new Shape(), Price = 5, InteriorShape = holdless ? new Shape(1, 1) : Solid(4, 2) + }); + var dock = seed.Upsert(new DockingBayData { Name = "Dock", Shape = new Shape(), Price = 5, InteriorShape = Solid(1, 1) }); + var capacitor = seed.Upsert(new GearData { Name = "Cap", Hardpoint = HardpointType.Tool, Shape = new Shape(), Price = 1, Behaviors = { new CapacitorData() } }); + var heater = seed.Upsert(new GearData + { + Name = "Heater", Hardpoint = HardpointType.Tool, Shape = new Shape(), Price = 1, + Behaviors = { new ThermotoggleData { HighPass = false }, new HeatData() } + }); + // Hardpoint gear no hull in this catalog has a hardpoint for: it has no home. + var orphan = seed.Upsert(new GearData { Name = "Orphan", Hardpoint = HardpointType.Sensors, Shape = new Shape(), Price = 1 }); + + void Sell(string name, CultRecordKey design, CultRecordRef by) => + seed.Upsert(new FactionProductData { Name = name, Design = new CultRecordRef(design), Manufacturer = by }); + foreach (var (name, design) in new[] + { + ("Skiff", skiff.Key), ("Platform", platform.Key), ("Crate", crate.Key), ("Dock", dock.Key), + ("Cap", capacitor.Key), ("Heater", heater.Key), ("Orphan", orphan.Key) + }) + Sell(name + " by Maker", design, maker); + + if (offer != Offer.None) + { + var overdrive = seed.Upsert(new ConsumableItemData { Name = "Overdrive", Duration = 1f, Shape = new Shape(), Price = 50 }); + if (offer == Offer.Reachable) + { + Sell("Overdrive by Maker", overdrive.Key, maker); + Sell("Overdrive by Ally", overdrive.Key, ally); + } + Sell("Overdrive by Stranger", overdrive.Key, stranger); + } + seed.FlushAsync().Wait(); + } + + var cache = AetheriaStores.Open(catalog, Path.Combine(root, "run.cc"), Path.Combine(root, "player.cc")); + _caches.Add(cache); + var game = new SavedGame + { + Factions = new[] { maker }, + Relationships = new[] { FactionRelationship.Neutral }, + HomeZones = new Dictionary { { 0, 0 } }, + BossZones = new Dictionary(), + DiscoveredZones = new[] { 0 }, + ActionBarBindings = new SavedActionBarBinding[0], + Exit = -1 + }; + var zones = new[] + { + new SavedZone { Name = "Zone 0", Position = new float2(0, 0), AdjacentZones = Array.Empty(), Factions = new[] { 0 }, Owner = 0, Contents = null } + }; + RunSave.Commit(cache, game, zones, new ProvenanceLedger()); + return new Rig + { + Cache = cache, + Items = new ItemManager(cache, new ProvenanceLedger(), RunSaveTests.TestSettings(), _ => { }), + Galaxy = new Galaxy(cache, cache.GetGlobal(), _ => { }), + Maker = cache.GetByName("Maker") + }; + } + + private static ConsumableItem[] Consumables(EntityPack pack) => + pack.CargoContents.SelectMany(bay => bay).Select(entry => entry.item).OfType().ToArray(); + + // Distinct, fixed seeds: the same 400 generators on every run. + private static uint SeedOf(int i) => (uint) (i * 7919 + 13); + + private static void AssertWithinThreeSigma(int observed, float chance, string what) + { + var expected = Seeds * chance; + var sigma = MathF.Sqrt(Seeds * chance * (1 - chance)); + Assert.True(MathF.Abs(observed - expected) <= 3 * sigma, + $"{what}: {observed} of {Seeds}, expected {expected} +/- {3 * sigma}"); + } + + [Fact] + public void AFewStationsStockConsumables() + { + var rig = Build(Offer.Reachable); + var stocked = 0; + var makers = new HashSet(); + for (var i = 0; i < Seeds; i++) + { + var random = new Random(SeedOf(i)); + var pack = rig.Generator(ref random).GenerateStationLoadout(); + Assert.NotEmpty(pack.CargoContents.SelectMany(bay => bay)); // the gear stock is read from the same pack + var held = Consumables(pack); + if (held.Length == 0) continue; + stocked++; + Assert.InRange(held.Length, 1, LoadoutGenerator.StationConsumableUnits); + var brands = held.Select(item => rig.Items.Brand(item)).ToArray(); + var product = Assert.Single(brands.Select(brand => brand.Product.Name).Distinct()); // exactly one product + Assert.Contains(product, new[] { "Overdrive by Maker", "Overdrive by Ally" }); // sold by a maker on offer + makers.Add(product); + } + AssertWithinThreeSigma(stocked, LoadoutGenerator.StationConsumableChance, "stations stocking a consumable"); + Assert.Equal(2, makers.Count); // the pick is among the products on offer, not always the first + } + + [Fact] + public void FewShipsCarryOne() + { + var rig = Build(Offer.Reachable); + var carrying = 0; + for (var i = 0; i < Seeds; i++) + { + var random = new Random(SeedOf(i)); + var held = Consumables(rig.Generator(ref random).GenerateShipLoadout()); + if (held.Length == 0) continue; + carrying++; + Assert.Equal(LoadoutGenerator.ShipConsumableUnits, held.Length); + } + AssertWithinThreeSigma(carrying, LoadoutGenerator.ShipConsumableChance, "ships carrying a consumable"); + + // A ship whose only hold has no free cell takes none, and generating it does not throw. + var holdless = Build(Offer.Reachable, holdless: true); + for (var i = 0; i < 100; i++) + { + var random = new Random(SeedOf(i)); + Assert.Empty(Consumables(holdless.Generator(ref random).GenerateShipLoadout())); + } + } + + [Fact] + public void ConsumablesNeedNoHome() + { + var rig = Build(Offer.Reachable); + var random = new Random(1); + var generator = rig.Generator(ref random); + Assert.NotEmpty(generator.RandomProducts(8, 0)); + Assert.Empty(generator.RandomProducts(8, 0, design => design.Name == "Orphan")); + Assert.NotEmpty(generator.RandomProducts(8, 0, design => design.Name == "Cap")); // a gear with a home still is + } + + [Fact] + public void AvailabilityGatesTheDraw() + { + // The same catalog with and without a consumable design whose every maker the faction cannot reach. + var gated = Build(Offer.Stranger); + var plain = Build(Offer.None); + var control = Build(Offer.Reachable); // positive control: the consumable on offer does move the generator + + float[] AfterGeneration(Rig rig) + { + rig.Items.Random = new Random(99); + var random = new Random(5); + var generator = rig.Generator(ref random); + generator.GenerateStationLoadout(); + for (var i = 0; i < 3; i++) generator.GenerateShipLoadout(); + return Enumerable.Range(0, 4).Select(_ => generator.Random.NextFloat()) + .Concat(Enumerable.Range(0, 4).Select(_ => rig.Items.Random.NextFloat())).ToArray(); + } + + var baseline = AfterGeneration(plain); + Assert.Equal(baseline, AfterGeneration(gated)); + Assert.NotEqual(baseline, AfterGeneration(control)); + } +} From c2e46a4637516de276d1bb528fe31b89cfc112a1 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 10 Oct 2026 05:34:47 +0200 Subject: [PATCH 02/11] ConsumableSupplyTests: count consumables in docking-bay holds too (a station's first bay is its dock) Co-Authored-By: Claude Opus 5.5 --- tests/Aetheria.Shared.Tests/ConsumableSupplyTests.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/Aetheria.Shared.Tests/ConsumableSupplyTests.cs b/tests/Aetheria.Shared.Tests/ConsumableSupplyTests.cs index 362b0e283..545392834 100644 --- a/tests/Aetheria.Shared.Tests/ConsumableSupplyTests.cs +++ b/tests/Aetheria.Shared.Tests/ConsumableSupplyTests.cs @@ -132,7 +132,8 @@ public sealed class ConsumableSupplyTests : IDisposable } private static ConsumableItem[] Consumables(EntityPack pack) => - pack.CargoContents.SelectMany(bay => bay).Select(entry => entry.item).OfType().ToArray(); + pack.CargoContents.Concat(pack.DockingBayContents ?? Array.Empty<(int2 position, ItemInstance item)[]>()) + .SelectMany(bay => bay).Select(entry => entry.item).OfType().ToArray(); // Distinct, fixed seeds: the same 400 generators on every run. private static uint SeedOf(int i) => (uint) (i * 7919 + 13); From 54d200ec6051d3a6691d7eacf7405f709adc11fb Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 10 Oct 2026 05:37:21 +0200 Subject: [PATCH 03/11] probe x --- tests/Aetheria.Shared.Tests/ConsumableSupplyTests.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/Aetheria.Shared.Tests/ConsumableSupplyTests.cs b/tests/Aetheria.Shared.Tests/ConsumableSupplyTests.cs index 545392834..73c83080c 100644 --- a/tests/Aetheria.Shared.Tests/ConsumableSupplyTests.cs +++ b/tests/Aetheria.Shared.Tests/ConsumableSupplyTests.cs @@ -152,10 +152,14 @@ public sealed class ConsumableSupplyTests : IDisposable var rig = Build(Offer.Reachable); var stocked = 0; var makers = new HashSet(); + var hist = new int[4]; int dockN = 0, crateN = 0; for (var i = 0; i < Seeds; i++) { var random = new Random(SeedOf(i)); var pack = rig.Generator(ref random).GenerateStationLoadout(); + hist[Math.Min(3, Consumables(pack).Length)]++; + dockN += (pack.DockingBayContents ?? new (int2, ItemInstance)[0][]).SelectMany(b => b).Count(e => e.item is ConsumableItem); + crateN += pack.CargoContents.SelectMany(b => b).Count(e => e.item is ConsumableItem); Assert.NotEmpty(pack.CargoContents.SelectMany(bay => bay)); // the gear stock is read from the same pack var held = Consumables(pack); if (held.Length == 0) continue; @@ -166,6 +170,7 @@ public sealed class ConsumableSupplyTests : IDisposable Assert.Contains(product, new[] { "Overdrive by Maker", "Overdrive by Ally" }); // sold by a maker on offer makers.Add(product); } + Assert.True(false, $"PROBE stocked={stocked} hist={string.Join(",", hist)} dock={dockN} crate={crateN} none={Enumerable.Range(0, 0).Count()}"); AssertWithinThreeSigma(stocked, LoadoutGenerator.StationConsumableChance, "stations stocking a consumable"); Assert.Equal(2, makers.Count); // the pick is among the products on offer, not always the first } From a5aac29055a0d833d77144babb4b6730640b1163 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 10 Oct 2026 05:47:11 +0200 Subject: [PATCH 04/11] ConsumableSupplyTests: roomy holds (gear stock draws 16 with replacement), drop the probe, pin the station maximum Commit 54d200ec before this is a diagnostic probe (builds, test deliberately red). Co-Authored-By: Claude Opus 5.5 --- .../ConsumableSupplyTests.cs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/tests/Aetheria.Shared.Tests/ConsumableSupplyTests.cs b/tests/Aetheria.Shared.Tests/ConsumableSupplyTests.cs index 73c83080c..a90418f46 100644 --- a/tests/Aetheria.Shared.Tests/ConsumableSupplyTests.cs +++ b/tests/Aetheria.Shared.Tests/ConsumableSupplyTests.cs @@ -66,12 +66,13 @@ public sealed class ConsumableSupplyTests : IDisposable var stranger = seed.Upsert(new Faction { Name = "Stranger", ShortName = "STR" }); maker = seed.Upsert(new Faction { Name = "Maker", ShortName = "MKR", Allegiance = { { ally, 1f } } }); - var skiff = seed.Upsert(new HullData { Name = "Skiff", HullType = HullType.Ship, Shape = Solid(5, 5), Price = 100 }); + var skiff = seed.Upsert(new HullData { Name = "Skiff", HullType = HullType.Ship, Shape = Solid(3, 3), Price = 100 }); var platform = seed.Upsert(new HullData { Name = "Platform", HullType = HullType.Station, Shape = Solid(5, 5), Price = 100 }); - // A hold with no cell cannot take a consumable; the others take up to eight. + // A hold with no cell cannot take a consumable. The others are large: a station stocks up to sixteen + // picks of gear (drawn with replacement) before its consumable, and a full hold would refuse it. var crate = seed.Upsert(new CargoBayData { - Name = "Crate", Shape = new Shape(), Price = 5, InteriorShape = holdless ? new Shape(1, 1) : Solid(4, 2) + Name = "Crate", Shape = new Shape(), Price = 5, InteriorShape = holdless ? new Shape(1, 1) : Solid(16, 12) }); var dock = seed.Upsert(new DockingBayData { Name = "Dock", Shape = new Shape(), Price = 5, InteriorShape = Solid(1, 1) }); var capacitor = seed.Upsert(new GearData { Name = "Cap", Hardpoint = HardpointType.Tool, Shape = new Shape(), Price = 1, Behaviors = { new CapacitorData() } }); @@ -152,26 +153,24 @@ public sealed class ConsumableSupplyTests : IDisposable var rig = Build(Offer.Reachable); var stocked = 0; var makers = new HashSet(); - var hist = new int[4]; int dockN = 0, crateN = 0; + var fullest = 0; for (var i = 0; i < Seeds; i++) { var random = new Random(SeedOf(i)); var pack = rig.Generator(ref random).GenerateStationLoadout(); - hist[Math.Min(3, Consumables(pack).Length)]++; - dockN += (pack.DockingBayContents ?? new (int2, ItemInstance)[0][]).SelectMany(b => b).Count(e => e.item is ConsumableItem); - crateN += pack.CargoContents.SelectMany(b => b).Count(e => e.item is ConsumableItem); Assert.NotEmpty(pack.CargoContents.SelectMany(bay => bay)); // the gear stock is read from the same pack var held = Consumables(pack); if (held.Length == 0) continue; stocked++; + fullest = Math.Max(fullest, held.Length); Assert.InRange(held.Length, 1, LoadoutGenerator.StationConsumableUnits); var brands = held.Select(item => rig.Items.Brand(item)).ToArray(); var product = Assert.Single(brands.Select(brand => brand.Product.Name).Distinct()); // exactly one product Assert.Contains(product, new[] { "Overdrive by Maker", "Overdrive by Ally" }); // sold by a maker on offer makers.Add(product); } - Assert.True(false, $"PROBE stocked={stocked} hist={string.Join(",", hist)} dock={dockN} crate={crateN} none={Enumerable.Range(0, 0).Count()}"); AssertWithinThreeSigma(stocked, LoadoutGenerator.StationConsumableChance, "stations stocking a consumable"); + Assert.Equal(LoadoutGenerator.StationConsumableUnits, fullest); // a roll can stock up to the maximum Assert.Equal(2, makers.Count); // the pick is among the products on offer, not always the first } From 87528dbda4f528f90c0b014d18878dea57676d9b Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 10 Oct 2026 05:47:37 +0200 Subject: [PATCH 05/11] ConsumableSupplyTests: a station roll can also stock a single unit Co-Authored-By: Claude Opus 5.5 --- tests/Aetheria.Shared.Tests/ConsumableSupplyTests.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/Aetheria.Shared.Tests/ConsumableSupplyTests.cs b/tests/Aetheria.Shared.Tests/ConsumableSupplyTests.cs index a90418f46..5afe34245 100644 --- a/tests/Aetheria.Shared.Tests/ConsumableSupplyTests.cs +++ b/tests/Aetheria.Shared.Tests/ConsumableSupplyTests.cs @@ -153,7 +153,7 @@ public sealed class ConsumableSupplyTests : IDisposable var rig = Build(Offer.Reachable); var stocked = 0; var makers = new HashSet(); - var fullest = 0; + int fullest = 0, thinnest = int.MaxValue; for (var i = 0; i < Seeds; i++) { var random = new Random(SeedOf(i)); @@ -163,6 +163,7 @@ public sealed class ConsumableSupplyTests : IDisposable if (held.Length == 0) continue; stocked++; fullest = Math.Max(fullest, held.Length); + thinnest = Math.Min(thinnest, held.Length); Assert.InRange(held.Length, 1, LoadoutGenerator.StationConsumableUnits); var brands = held.Select(item => rig.Items.Brand(item)).ToArray(); var product = Assert.Single(brands.Select(brand => brand.Product.Name).Distinct()); // exactly one product @@ -171,6 +172,7 @@ public sealed class ConsumableSupplyTests : IDisposable } AssertWithinThreeSigma(stocked, LoadoutGenerator.StationConsumableChance, "stations stocking a consumable"); Assert.Equal(LoadoutGenerator.StationConsumableUnits, fullest); // a roll can stock up to the maximum + Assert.Equal(1, thinnest); // and as few as one Assert.Equal(2, makers.Count); // the pick is among the products on offer, not always the first } From e8d42df2580ca8995bc2e4ffbfef25adfa70275e Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 10 Oct 2026 05:56:45 +0200 Subject: [PATCH 06/11] ConsumableSupplyTests: ship hull with two interior cells (cargo bay and capacitor) Co-Authored-By: Claude Opus 5.5 --- tests/Aetheria.Shared.Tests/ConsumableSupplyTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Aetheria.Shared.Tests/ConsumableSupplyTests.cs b/tests/Aetheria.Shared.Tests/ConsumableSupplyTests.cs index 5afe34245..5642da55c 100644 --- a/tests/Aetheria.Shared.Tests/ConsumableSupplyTests.cs +++ b/tests/Aetheria.Shared.Tests/ConsumableSupplyTests.cs @@ -66,7 +66,7 @@ public sealed class ConsumableSupplyTests : IDisposable var stranger = seed.Upsert(new Faction { Name = "Stranger", ShortName = "STR" }); maker = seed.Upsert(new Faction { Name = "Maker", ShortName = "MKR", Allegiance = { { ally, 1f } } }); - var skiff = seed.Upsert(new HullData { Name = "Skiff", HullType = HullType.Ship, Shape = Solid(3, 3), Price = 100 }); + var skiff = seed.Upsert(new HullData { Name = "Skiff", HullType = HullType.Ship, Shape = Solid(4, 3), Price = 100 }); var platform = seed.Upsert(new HullData { Name = "Platform", HullType = HullType.Station, Shape = Solid(5, 5), Price = 100 }); // A hold with no cell cannot take a consumable. The others are large: a station stocks up to sixteen // picks of gear (drawn with replacement) before its consumable, and a full hold would refuse it. From 293747c5f97e7f6f05e2d593dbd352717fcb2049 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 10 Oct 2026 07:57:32 +0200 Subject: [PATCH 07/11] ConsumableSupplyTests: pin the generator's Random after generation to the pre-consumable tree The gated-versus-plain comparison alone cannot see an unconditional draw (both catalogs draw). Values measured on origin/master d4a27def with the same fixture. The roll-before-availability mutant survived the full suite without this pin. Co-Authored-By: Claude Opus 5.5 --- .../Aetheria.Shared.Tests/ConsumableSupplyTests.cs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/tests/Aetheria.Shared.Tests/ConsumableSupplyTests.cs b/tests/Aetheria.Shared.Tests/ConsumableSupplyTests.cs index 5642da55c..7d3325df5 100644 --- a/tests/Aetheria.Shared.Tests/ConsumableSupplyTests.cs +++ b/tests/Aetheria.Shared.Tests/ConsumableSupplyTests.cs @@ -230,8 +230,16 @@ public sealed class ConsumableSupplyTests : IDisposable .Concat(Enumerable.Range(0, 4).Select(_ => rig.Items.Random.NextFloat())).ToArray(); } - var baseline = AfterGeneration(plain); - Assert.Equal(baseline, AfterGeneration(gated)); - Assert.NotEqual(baseline, AfterGeneration(control)); + // What the plain catalog leaves in both generators' Random, measured on the tree before consumables existed + // (origin/master d4a27def, the same fixture and seeds). The gated and plain catalogs agreeing is not enough: + // a step that drew unconditionally would draw in both. This pins the value itself. + var beforeConsumables = new[] + { + 0.85624266f, 0.037778974f, 0.7931649f, 0.3596493f, 0.17511952f, 0.6642336f, 0.31897342f, 0.5977364f + }; + var plainAfter = AfterGeneration(plain); + Assert.Equal(beforeConsumables, plainAfter); + Assert.Equal(plainAfter, AfterGeneration(gated)); + Assert.NotEqual(plainAfter, AfterGeneration(control)); } } From eff21e05130eb5b5a25baf58908dc1f02b21c7d3 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 10 Oct 2026 08:02:48 +0200 Subject: [PATCH 08/11] ConsumableSupplyTests: spell the ruling's chances and units out instead of reading the constants A mutant that changed a constant moved the test's expectation with it and survived (station chance 1, ship chance 0 and 1, station units 3). Co-Authored-By: Claude Opus 5.5 --- .../ConsumableSupplyTests.cs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/tests/Aetheria.Shared.Tests/ConsumableSupplyTests.cs b/tests/Aetheria.Shared.Tests/ConsumableSupplyTests.cs index 7d3325df5..157f0fa09 100644 --- a/tests/Aetheria.Shared.Tests/ConsumableSupplyTests.cs +++ b/tests/Aetheria.Shared.Tests/ConsumableSupplyTests.cs @@ -20,6 +20,11 @@ public sealed class ConsumableSupplyTests : IDisposable { private const int Seeds = 400; + // The ruling's numbers, spelled out: a quarter of stations stock one product of 1-2 units, one ship in ten + // carries one unit. The tests read these, never LoadoutGenerator's constants, so a changed constant fails them. + private const float StationChance = .25f, ShipChance = .1f; + private const int StationUnits = 2, ShipUnits = 1; + private readonly string _root = Path.Combine(Path.GetTempPath(), "aetheria-consumablesupply-" + Guid.NewGuid().ToString("N")); private readonly List _caches = new List(); private int _rigs; @@ -164,14 +169,14 @@ public sealed class ConsumableSupplyTests : IDisposable stocked++; fullest = Math.Max(fullest, held.Length); thinnest = Math.Min(thinnest, held.Length); - Assert.InRange(held.Length, 1, LoadoutGenerator.StationConsumableUnits); + Assert.InRange(held.Length, 1, StationUnits); var brands = held.Select(item => rig.Items.Brand(item)).ToArray(); var product = Assert.Single(brands.Select(brand => brand.Product.Name).Distinct()); // exactly one product Assert.Contains(product, new[] { "Overdrive by Maker", "Overdrive by Ally" }); // sold by a maker on offer makers.Add(product); } - AssertWithinThreeSigma(stocked, LoadoutGenerator.StationConsumableChance, "stations stocking a consumable"); - Assert.Equal(LoadoutGenerator.StationConsumableUnits, fullest); // a roll can stock up to the maximum + AssertWithinThreeSigma(stocked, StationChance, "stations stocking a consumable"); + Assert.Equal(StationUnits, fullest); // a roll can stock up to the maximum Assert.Equal(1, thinnest); // and as few as one Assert.Equal(2, makers.Count); // the pick is among the products on offer, not always the first } @@ -187,9 +192,9 @@ public sealed class ConsumableSupplyTests : IDisposable var held = Consumables(rig.Generator(ref random).GenerateShipLoadout()); if (held.Length == 0) continue; carrying++; - Assert.Equal(LoadoutGenerator.ShipConsumableUnits, held.Length); + Assert.Equal(ShipUnits, held.Length); } - AssertWithinThreeSigma(carrying, LoadoutGenerator.ShipConsumableChance, "ships carrying a consumable"); + AssertWithinThreeSigma(carrying, ShipChance, "ships carrying a consumable"); // A ship whose only hold has no free cell takes none, and generating it does not throw. var holdless = Build(Offer.Reachable, holdless: true); From 1a81d07e3aa2c90970e648f32c65bca2aa542993 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 10 Oct 2026 11:02:27 +0200 Subject: [PATCH 09/11] ConsumableSupplyTests: shipped hulls, tight holds, two hulls per kind, 3000 ship seeds Station and ship rates are measured per hull on two station hulls and two ship hulls with holds that sixteen gear picks fill, on a station whose first bay refuses, and on the shipped catalog's own hulls. The ship band is 3000 seeds so chances of .12, .15 and .05 fall outside it. These tests fail on the generator as it stands (consumable stocked after the gear); the next commit fixes the generator. Co-Authored-By: Claude Opus 5.5 --- .../ConsumableSupplyTests.cs | 130 +++++++++++++++--- 1 file changed, 113 insertions(+), 17 deletions(-) diff --git a/tests/Aetheria.Shared.Tests/ConsumableSupplyTests.cs b/tests/Aetheria.Shared.Tests/ConsumableSupplyTests.cs index 157f0fa09..325a55a0f 100644 --- a/tests/Aetheria.Shared.Tests/ConsumableSupplyTests.cs +++ b/tests/Aetheria.Shared.Tests/ConsumableSupplyTests.cs @@ -6,8 +6,10 @@ using System; using System.Collections.Generic; using System.IO; using System.Linq; +using System.Reflection; using CultMath; using GameCult.Caching; +using GameCult.Caching.MessagePack; using Xunit; using Random = CultMath.Random; @@ -20,6 +22,10 @@ public sealed class ConsumableSupplyTests : IDisposable { private const int Seeds = 400; + // Ships are cheap to generate and the ruling's one in ten is a narrow target: at 3000 seeds the 3-sigma band is + // +/- 49 around 300, which chances of .12, .15 and .05 all fall outside. + private const int ShipSeeds = 3000; + // The ruling's numbers, spelled out: a quarter of stations stock one product of 1-2 units, one ship in ten // carries one unit. The tests read these, never LoadoutGenerator's constants, so a changed constant fails them. private const float StationChance = .25f, ShipChance = .1f; @@ -39,6 +45,12 @@ public sealed class ConsumableSupplyTests : IDisposable private enum Offer { None, Reachable, Stranger } + // Pinned is the original one-hull-each catalog with a roomy hold, which AvailabilityGatesTheDraw's pinned values + // were measured on. Tight adds a second ship hull, a second station hull and a 2x2 gear whose sixteen picks fill + // a 12-cell hold, as the shipped holds do. Holdless gives every hold no free cell. DockRefuses is Tight with a + // docking bay that has no free cell, so a station's first bay refuses and only a later bay can take a consumable. + private enum Holds { Pinned, Tight, Holdless, DockRefuses } + // One catalog: a ship hull, a station hull and the station's required fittings, all made by Maker; and, when // offered, one consumable design with a product from each of Maker, its ally and (unless the offer is Stranger // only) a stranger Maker's allegiance does not name. @@ -58,7 +70,7 @@ public sealed class ConsumableSupplyTests : IDisposable return shape; } - private Rig Build(Offer offer, bool holdless = false) + private Rig Build(Offer offer, Holds holds = Holds.Pinned) { var root = Path.Combine(_root, "rig" + _rigs++); Directory.CreateDirectory(root); @@ -73,13 +85,13 @@ public sealed class ConsumableSupplyTests : IDisposable var skiff = seed.Upsert(new HullData { Name = "Skiff", HullType = HullType.Ship, Shape = Solid(4, 3), Price = 100 }); var platform = seed.Upsert(new HullData { Name = "Platform", HullType = HullType.Station, Shape = Solid(5, 5), Price = 100 }); - // A hold with no cell cannot take a consumable. The others are large: a station stocks up to sixteen - // picks of gear (drawn with replacement) before its consumable, and a full hold would refuse it. + // A hold with no cell cannot take a consumable. var crate = seed.Upsert(new CargoBayData { - Name = "Crate", Shape = new Shape(), Price = 5, InteriorShape = holdless ? new Shape(1, 1) : Solid(16, 12) + Name = "Crate", Shape = new Shape(), Price = 5, + InteriorShape = holds == Holds.Holdless ? new Shape(1, 1) : holds == Holds.Pinned ? Solid(16, 12) : Solid(4, 3) }); - var dock = seed.Upsert(new DockingBayData { Name = "Dock", Shape = new Shape(), Price = 5, InteriorShape = Solid(1, 1) }); + var dock = seed.Upsert(new DockingBayData { Name = "Dock", Shape = new Shape(), Price = 5, InteriorShape = holds == Holds.DockRefuses ? new Shape(1, 1) : Solid(1, 1) }); var capacitor = seed.Upsert(new GearData { Name = "Cap", Hardpoint = HardpointType.Tool, Shape = new Shape(), Price = 1, Behaviors = { new CapacitorData() } }); var heater = seed.Upsert(new GearData { @@ -89,13 +101,21 @@ public sealed class ConsumableSupplyTests : IDisposable // Hardpoint gear no hull in this catalog has a hardpoint for: it has no home. var orphan = seed.Upsert(new GearData { Name = "Orphan", Hardpoint = HardpointType.Sensors, Shape = new Shape(), Price = 1 }); + var sold = new List<(string, CultRecordKey)>(); + if (holds != Holds.Pinned) + { + sold.Add(("Cutter", seed.Upsert(new HullData { Name = "Cutter", HullType = HullType.Ship, Shape = Solid(3, 3), Price = 100 }).Key)); + sold.Add(("Outpost", seed.Upsert(new HullData { Name = "Outpost", HullType = HullType.Station, Shape = Solid(4, 4), Price = 100 }).Key)); + sold.Add(("Plate", seed.Upsert(new GearData { Name = "Plate", Hardpoint = HardpointType.Tool, Shape = Solid(2, 2), Price = 1 }).Key)); + } + void Sell(string name, CultRecordKey design, CultRecordRef by) => seed.Upsert(new FactionProductData { Name = name, Design = new CultRecordRef(design), Manufacturer = by }); foreach (var (name, design) in new[] { ("Skiff", skiff.Key), ("Platform", platform.Key), ("Crate", crate.Key), ("Dock", dock.Key), ("Cap", capacitor.Key), ("Heater", heater.Key), ("Orphan", orphan.Key) - }) + }.Concat(sold)) Sell(name + " by Maker", design, maker); if (offer != Offer.None) @@ -144,19 +164,27 @@ public sealed class ConsumableSupplyTests : IDisposable // Distinct, fixed seeds: the same 400 generators on every run. private static uint SeedOf(int i) => (uint) (i * 7919 + 13); - private static void AssertWithinThreeSigma(int observed, float chance, string what) + private static void AssertWithinThreeSigma(int observed, int of, float chance, string what) { - var expected = Seeds * chance; - var sigma = MathF.Sqrt(Seeds * chance * (1 - chance)); + var expected = of * chance; + var sigma = MathF.Sqrt(of * chance * (1 - chance)); Assert.True(MathF.Abs(observed - expected) <= 3 * sigma, - $"{what}: {observed} of {Seeds}, expected {expected} +/- {3 * sigma}"); + $"{what}: {observed} of {of}, expected {expected} +/- {3 * sigma}"); + } + + // Hits and trials per hull name, so a chance that scales with hull size is measured on each hull it scales. + private static void AssertEachHullWithinThreeSigma(Dictionary byHull, float chance, string what) + { + Assert.Equal(2, byHull.Count); // both hulls are drawn + foreach (var (hull, (hits, of)) in byHull) AssertWithinThreeSigma(hits, of, chance, $"{what} on {hull}"); } [Fact] public void AFewStationsStockConsumables() { - var rig = Build(Offer.Reachable); + var rig = Build(Offer.Reachable, Holds.Tight); var stocked = 0; + var byHull = new Dictionary(); var makers = new HashSet(); int fullest = 0, thinnest = int.MaxValue; for (var i = 0; i < Seeds; i++) @@ -165,6 +193,9 @@ public sealed class ConsumableSupplyTests : IDisposable var pack = rig.Generator(ref random).GenerateStationLoadout(); Assert.NotEmpty(pack.CargoContents.SelectMany(bay => bay)); // the gear stock is read from the same pack var held = Consumables(pack); + var hull = rig.Items.GetData(pack.Hull).Name; + byHull.TryGetValue(hull, out var seen); + byHull[hull] = (seen.hits + (held.Length > 0 ? 1 : 0), seen.of + 1); if (held.Length == 0) continue; stocked++; fullest = Math.Max(fullest, held.Length); @@ -175,7 +206,8 @@ public sealed class ConsumableSupplyTests : IDisposable Assert.Contains(product, new[] { "Overdrive by Maker", "Overdrive by Ally" }); // sold by a maker on offer makers.Add(product); } - AssertWithinThreeSigma(stocked, StationChance, "stations stocking a consumable"); + AssertWithinThreeSigma(stocked, Seeds, StationChance, "stations stocking a consumable"); + AssertEachHullWithinThreeSigma(byHull, StationChance, "stations stocking a consumable"); Assert.Equal(StationUnits, fullest); // a roll can stock up to the maximum Assert.Equal(1, thinnest); // and as few as one Assert.Equal(2, makers.Count); // the pick is among the products on offer, not always the first @@ -184,20 +216,26 @@ public sealed class ConsumableSupplyTests : IDisposable [Fact] public void FewShipsCarryOne() { - var rig = Build(Offer.Reachable); + var rig = Build(Offer.Reachable, Holds.Tight); var carrying = 0; - for (var i = 0; i < Seeds; i++) + var byHull = new Dictionary(); + for (var i = 0; i < ShipSeeds; i++) { var random = new Random(SeedOf(i)); - var held = Consumables(rig.Generator(ref random).GenerateShipLoadout()); + var pack = rig.Generator(ref random).GenerateShipLoadout(); + var held = Consumables(pack); + var hull = rig.Items.GetData(pack.Hull).Name; + byHull.TryGetValue(hull, out var seen); + byHull[hull] = (seen.hits + (held.Length > 0 ? 1 : 0), seen.of + 1); if (held.Length == 0) continue; carrying++; Assert.Equal(ShipUnits, held.Length); } - AssertWithinThreeSigma(carrying, ShipChance, "ships carrying a consumable"); + AssertWithinThreeSigma(carrying, ShipSeeds, ShipChance, "ships carrying a consumable"); + AssertEachHullWithinThreeSigma(byHull, ShipChance, "ships carrying a consumable"); // A ship whose only hold has no free cell takes none, and generating it does not throw. - var holdless = Build(Offer.Reachable, holdless: true); + var holdless = Build(Offer.Reachable, Holds.Holdless); for (var i = 0; i < 100; i++) { var random = new Random(SeedOf(i)); @@ -205,6 +243,64 @@ public sealed class ConsumableSupplyTests : IDisposable } } + // The first bay may refuse (a docking bay with no free cell); a later bay that takes the unit still holds it. + [Fact] + public void AStationWhoseFirstBayRefusesStocksInAnotherBay() + { + var rig = Build(Offer.Reachable, Holds.DockRefuses); + var stocked = 0; + for (var i = 0; i < Seeds; i++) + { + var random = new Random(SeedOf(i)); + if (Consumables(rig.Generator(ref random).GenerateStationLoadout()).Length > 0) stocked++; + } + AssertWithinThreeSigma(stocked, Seeds, StationChance, "stations stocking a consumable past a refusing first bay"); + } + + // The ruled rates hold on the shipped catalog's own station and ship hulls and holds, with one consumable on offer + // (the shipped catalog has none yet): a station rolled to stock one must end holding one, however full gear fills + // its hold. + [Fact] + public void TheShippedHullsKeepTheRuledRates() + { + var repo = Repo(); + var catalog = Path.Combine(_root, "shipped.cc"); + File.Copy(Path.Combine(repo, "GameData", "Aetheria.cc"), catalog); + var cache = new CultCache(CultDocumentRegistry.ForTypes(typeof(ItemData).Assembly.GetTypes() + .Where(t => t is { IsAbstract: false, IsInterface: false }).Where(t => t.GetCustomAttribute() != null))); + cache.AddBackingStore(new SingleFileMessagePackBackingStore(catalog), AetheriaStores.CatalogTypes); + cache.AddBackingStore(new SingleFileMessagePackBackingStore(Path.Combine(_root, "shipped-run.cc")), AetheriaStores.RunTypes); + _caches.Add(cache); + var maker = cache.RefOf(cache.GetAll().First()); + var overdrive = cache.Upsert(new ConsumableItemData { Name = "Overdrive", Duration = 1f, Shape = new Shape(), Price = 50 }); + cache.Upsert(new FactionProductData { Name = "Overdrive by Maker", Design = new CultRecordRef(overdrive.Key), Manufacturer = maker }); + + var authored = AuthoredSettings.Load(repo); + var galaxy = new Galaxy(authored.Read("TutorialGenerationSettings"), + authored.Read("TutorialBackgroundSettings"), authored.Read("NameGeneratorSettings"), + cache, new PlayerSettings(), Directory.CreateDirectory(Path.Combine(_root, "Narrative")), _ => { }, null, 1); + var items = new ItemManager(cache, new ProvenanceLedger(), authored.Read("GameplaySettings"), _ => { }); + + int stations = 0, ships = 0; + for (var i = 0; i < Seeds; i++) + { + items.Random = new Random(SeedOf(i)); + var random = new Random(SeedOf(i)); + var generator = new LoadoutGenerator(ref random, items, galaxy, galaxy.Entrance, null, .5f); + if (Consumables(generator.GenerateStationLoadout()).Length > 0) stations++; + if (Consumables(generator.GenerateShipLoadout()).Length > 0) ships++; + } + AssertWithinThreeSigma(stations, Seeds, StationChance, "shipped stations stocking a consumable"); + AssertWithinThreeSigma(ships, Seeds, ShipChance, "shipped ships carrying a consumable"); + } + + private static string Repo() + { + for (var dir = new DirectoryInfo(AppContext.BaseDirectory); dir != null; dir = dir.Parent) + if (File.Exists(Path.Combine(dir.FullName, "Aetheria.Shared", "Aetheria.Shared.csproj"))) return dir.FullName; + throw new DirectoryNotFoundException(); + } + [Fact] public void ConsumablesNeedNoHome() { From a00054aa7e01414253a62d37b30c4b27267011de Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 10 Oct 2026 11:09:59 +0200 Subject: [PATCH 10/11] LoadoutGenerator: a station stocks its consumable before the gear Sixteen gear picks drawn with replacement fill a shipped station hold, so a station whose roll hit usually stored nothing (29 of 400 held one against the ruled quarter). The consumable step now runs before the gear fill; with no consumable on offer it still draws nothing, so generation is unchanged. Ships keep their step after OutfitEntity. Co-Authored-By: Claude Opus 5.5 --- Assets/Scripts/ServerShared/LoadoutGenerator.cs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Assets/Scripts/ServerShared/LoadoutGenerator.cs b/Assets/Scripts/ServerShared/LoadoutGenerator.cs index 9566b19c3..ca89ac58d 100644 --- a/Assets/Scripts/ServerShared/LoadoutGenerator.cs +++ b/Assets/Scripts/ServerShared/LoadoutGenerator.cs @@ -116,6 +116,11 @@ public class LoadoutGenerator FillInterior(entity); + // The consumable goes in before the gear stock: sixteen gear picks drawn with replacement can fill a real hold, + // and a roll that hit must leave the entity holding one (the ruled rate is the rule). With no consumable on + // offer nothing is drawn here, so the gear draws are exactly what they were. + StockConsumables(entity, StationConsumableChance, StationConsumableUnits); + var cargo = entity.CargoBays.First(); var inventory = RandomProducts(16, 1, data => !(data is HullData hull && hull.HullType != HullType.Ship) && !(data is CargoBayData)) @@ -126,14 +131,12 @@ public class LoadoutGenerator cargo.TryStore(instance); } - StockConsumables(entity, StationConsumableChance, StationConsumableUnits); - entity.CanTow = hullData.CanTow; return EntitySerializer.Pack(entity) as OrbitalEntityPack; } - // The last thing a loadout does. With no consumable on offer, or no hold to put one in, nothing is drawn, so a + // A loadout's one consumable step. With no consumable on offer, or no hold to put one in, nothing is drawn, so a // catalog without consumables generates exactly what it did before them. Otherwise one roll decides whether the // entity has any, and a hit stocks 1 to maxUnits instances of one product, stopping when no hold takes another. private void StockConsumables(Entity entity, float chance, int maxUnits) From 863d6c8fad94ad6702b556dda963864a3065d188 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 10 Oct 2026 11:30:46 +0200 Subject: [PATCH 11/11] ConsumableSupplyTests: 1500 station seeds, a ship hull with three interior cells, units reach the maximum on each station hull Co-Authored-By: Claude Opus 5.5 --- .../ConsumableSupplyTests.cs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/tests/Aetheria.Shared.Tests/ConsumableSupplyTests.cs b/tests/Aetheria.Shared.Tests/ConsumableSupplyTests.cs index 325a55a0f..775d80ec4 100644 --- a/tests/Aetheria.Shared.Tests/ConsumableSupplyTests.cs +++ b/tests/Aetheria.Shared.Tests/ConsumableSupplyTests.cs @@ -16,11 +16,11 @@ using Random = CultMath.Random; // Consumables are scarce (ruling consumables-scarce): a quarter of stations stock one product, one ship in ten carries // one unit, and the one availability rule (LoadoutGenerator.AvailableProducts) decides which products a faction is // offered. Every count here is measured on packs the production generator returns, from fixed seeds, so the runs are -// deterministic; the 3-sigma band is the binomial one, sigma = sqrt(n p (1 - p)) with n = 400 seeds, and a rule that +// deterministic; the 3-sigma band is the binomial one, sigma = sqrt(n p (1 - p)) with n the seed count, and a rule that // breaks (chance 1, chance 0, an unconditional roll) lands far outside it. public sealed class ConsumableSupplyTests : IDisposable { - private const int Seeds = 400; + private const int Seeds = 1500; // Ships are cheap to generate and the ruling's one in ten is a narrow target: at 3000 seeds the 3-sigma band is // +/- 49 around 300, which chances of .12, .15 and .05 all fall outside. @@ -104,7 +104,7 @@ public sealed class ConsumableSupplyTests : IDisposable var sold = new List<(string, CultRecordKey)>(); if (holds != Holds.Pinned) { - sold.Add(("Cutter", seed.Upsert(new HullData { Name = "Cutter", HullType = HullType.Ship, Shape = Solid(3, 3), Price = 100 }).Key)); + sold.Add(("Cutter", seed.Upsert(new HullData { Name = "Cutter", HullType = HullType.Ship, Shape = Solid(5, 3), Price = 100 }).Key)); sold.Add(("Outpost", seed.Upsert(new HullData { Name = "Outpost", HullType = HullType.Station, Shape = Solid(4, 4), Price = 100 }).Key)); sold.Add(("Plate", seed.Upsert(new GearData { Name = "Plate", Hardpoint = HardpointType.Tool, Shape = Solid(2, 2), Price = 1 }).Key)); } @@ -161,7 +161,7 @@ public sealed class ConsumableSupplyTests : IDisposable pack.CargoContents.Concat(pack.DockingBayContents ?? Array.Empty<(int2 position, ItemInstance item)[]>()) .SelectMany(bay => bay).Select(entry => entry.item).OfType().ToArray(); - // Distinct, fixed seeds: the same 400 generators on every run. + // Distinct, fixed seeds: the same generators on every run. private static uint SeedOf(int i) => (uint) (i * 7919 + 13); private static void AssertWithinThreeSigma(int observed, int of, float chance, string what) @@ -186,7 +186,8 @@ public sealed class ConsumableSupplyTests : IDisposable var stocked = 0; var byHull = new Dictionary(); var makers = new HashSet(); - int fullest = 0, thinnest = int.MaxValue; + int thinnest = int.MaxValue; + var fullestByHull = new Dictionary(); for (var i = 0; i < Seeds; i++) { var random = new Random(SeedOf(i)); @@ -198,7 +199,7 @@ public sealed class ConsumableSupplyTests : IDisposable byHull[hull] = (seen.hits + (held.Length > 0 ? 1 : 0), seen.of + 1); if (held.Length == 0) continue; stocked++; - fullest = Math.Max(fullest, held.Length); + fullestByHull[hull] = Math.Max(fullestByHull.GetValueOrDefault(hull), held.Length); thinnest = Math.Min(thinnest, held.Length); Assert.InRange(held.Length, 1, StationUnits); var brands = held.Select(item => rig.Items.Brand(item)).ToArray(); @@ -208,7 +209,7 @@ public sealed class ConsumableSupplyTests : IDisposable } AssertWithinThreeSigma(stocked, Seeds, StationChance, "stations stocking a consumable"); AssertEachHullWithinThreeSigma(byHull, StationChance, "stations stocking a consumable"); - Assert.Equal(StationUnits, fullest); // a roll can stock up to the maximum + Assert.Equal(new[] { StationUnits, StationUnits }, fullestByHull.Values); // a roll on either hull can stock up to the maximum Assert.Equal(1, thinnest); // and as few as one Assert.Equal(2, makers.Count); // the pick is among the products on offer, not always the first }