diff --git a/tests/Aetheria.Shared.Tests/CompoundStackTests.cs b/tests/Aetheria.Shared.Tests/CompoundStackTests.cs index c2f38c613..ab1c15d57 100644 --- a/tests/Aetheria.Shared.Tests/CompoundStackTests.cs +++ b/tests/Aetheria.Shared.Tests/CompoundStackTests.cs @@ -45,6 +45,10 @@ public sealed class CompoundStackTests : IDisposable var pocket = new Shape(1, 1); foreach (var cell in pocket.AllCoordinates) pocket[cell] = true; _cache.Upsert(new CargoBayData { Name = "Pocket", Shape = new Shape(), InteriorShape = pocket, Price = 5, Durability = 4 }); + // A station's holds are by role (ruling station-bays-by-category): a Pantry and a Larder take what its Smelter draws, a Store what it makes. + _cache.Upsert(new IngredientBayData { Name = "Pantry", Shape = new Shape(), InteriorShape = bay, Price = 20, Durability = 4 }); + _cache.Upsert(new IngredientBayData { Name = "Larder", Shape = new Shape(), InteriorShape = pocket, Price = 5, Durability = 4 }); + _cache.Upsert(new ProductBayData { Name = "Store", Shape = new Shape(), InteriorShape = bay, Price = 20, Durability = 4 }); _ore = new SimpleCommodityData { Name = "Ore", Shape = new Shape(), Price = 7, MaxStack = 10 }; _cache.Upsert(_ore); _gem = new CompoundCommodityData { Name = "Gem", Shape = new Shape(), Price = 10, Mass = 2f, SpecificHeat = 3f, MaxStack = 4 }; @@ -118,7 +122,8 @@ public sealed class CompoundStackTests : IDisposable private OrbitalEntity Station(bool wants = true) { var station = new OrbitalEntity(_items, _zone, Mint("Hub"), default, new EntitySettings()); - Assert.True(station.TryEquip(Mint("Crate"), new int2(1, 1))); + Assert.True(station.TryEquip(Mint("Pantry"), new int2(1, 1))); + Assert.True(station.TryEquip(Mint("Store"))); if (wants) Assert.True(station.TryEquip(Mint("Smelter"), new int2(0, 0))); return station; } @@ -255,12 +260,12 @@ public sealed class CompoundStackTests : IDisposable Assert.Empty(from.Cargo); Assert.Equal(3, station.CargoBays[0].Count(_cache.RefOf(_gem).Key)); - // Part-full bay: the station's stack of the lot has room for one unit and every other cell holds a gun, so one unit + // Part-full bay: the station's stack of the lot has room for one unit and every other cell holds ore, so one unit // sells, is paid for alone, and two stay in the seller's hold. var second = Gem(_lotA, 3); Assert.True(from.TryStore(second)); var hold = station.CargoBays[0]; - while (hold.TryStore(Mint("Gun"))) { } + while (hold.TryStore(new SimpleCommodity { Data = _cache.RefOf(_ore), Quantity = 10 })) { } credits = 0; Assert.Equal(1, StationServices.TrySellPart(from, station, second, ref credits).Sold); Assert.Equal(Pays(1), credits); @@ -443,14 +448,14 @@ public sealed class CompoundStackTests : IDisposable var key = _cache.RefOf(_gem).Key; var station = Station(); - Assert.True(station.TryEquip(Mint("Pocket"), new int2(2, 2))); + Assert.True(station.TryEquip(Mint("Larder"))); var crate = station.CargoBays[0]; - var pocket = station.CargoBays[1]; + var pocket = station.CargoBays.Last(); var inCrate = Gem(_lotA, 2); var inPocket = Gem(_lotA, 3); Assert.True(crate.TryStore(inCrate)); Assert.True(pocket.TryStore(inPocket)); - while (crate.TryStore(Mint("Gun"))) { } + while (crate.TryStore(new SimpleCommodity { Data = _cache.RefOf(_ore), Quantity = 10 })) { } var from = ShipWith("Crate").CargoBays[0]; var stack = Gem(_lotA, 4); diff --git a/tests/Aetheria.Shared.Tests/EconomyDrainTests.cs b/tests/Aetheria.Shared.Tests/EconomyDrainTests.cs index 810b68a33..60f3eb064 100644 --- a/tests/Aetheria.Shared.Tests/EconomyDrainTests.cs +++ b/tests/Aetheria.Shared.Tests/EconomyDrainTests.cs @@ -51,14 +51,14 @@ public sealed class EconomyDrainTests // The sortie rig after Boot was lifted out of it, on the shipped catalog, read at the commit before the extraction; re-read at the authored starter // (131/73/58, 1295, 131 mined at the generated one): Light Lasers on the Energy mounts instead of the seed-1 draw. What the refinery buys of the - // 120 mined units is its shares of the stock room the gear stock leaves (cut stationwants-ingredient-share): 10 sold and 110 refused for 851 - // credits, against 72 sold for 970 with an unbounded intake; the mined units and the sortie's time are the rig's own and did not move. + // 120 mined units is its shares of its ingredient bays, in whole cells (cut station-role-bays): 23 sold and 97 refused for 895 + // credits (10 and 110 for 851 on the stock room, 72 sold for 970 with an unbounded intake); the mined units and the sortie's time are the rig's own and did not move. [Fact] public void SortieOutputUnchangedByBoot() { var sample = Seed1.Value.Single(); - Assert.Equal((120, 10, 110), (sample.Units, sample.UnitsSold, sample.UnitsRefused)); - Assert.Equal(851, sample.SellValue); + Assert.Equal((120, 23, 97), (sample.Units, sample.UnitsSold, sample.UnitsRefused)); + Assert.Equal(895, sample.SellValue); Assert.Equal(150, sample.SimSeconds, 3); Assert.Equal(120, sample.Mined.Sum(m => m.Units)); } @@ -195,7 +195,7 @@ public sealed class EconomyDrainTests [Fact] public void ASaturatedRefineryIsNamed() { - var supplied = EconomyDrain.Run(Root, 1, 30, 24, Seed1.Value, supplied: true); + var supplied = EconomyDrain.Run(Root, 1, 30, 48, Seed1.Value, supplied: true); var refinery = Station(supplied, "processing"); Assert.NotNull(refinery.FirstSaturated); Assert.True(refinery.WantedEnd < refinery.WantedStart); @@ -265,8 +265,8 @@ public sealed class EconomyDrainTests { var sample = EconomyDrain.Run(Root, 1, 20, 1, Seed1.Value); var zenith = Station(sample, "trade-hub"); - var silo = zenith.Bays.Single(bay => bay.Silo); - var stock = zenith.Bays.Single(bay => !bay.Silo); + var silo = zenith.Bays.Single(bay => bay.Role == BayRole.Products); + var stock = zenith.Bays.Single(bay => bay.Role == BayRole.Stock); Assert.Equal(("Nickel-Iron", 640), silo.Capacity.First(c => c.Ore == "Nickel-Iron")); Assert.Equal(("Nickel-Iron", 80), stock.Capacity.First(c => c.Ore == "Nickel-Iron")); // Onward-haul ore goes to the silo only (ore-haul-run): the stock bay holds room for it but takes none. diff --git a/tests/Aetheria.Shared.Tests/ExtractionTests.cs b/tests/Aetheria.Shared.Tests/ExtractionTests.cs index 2c5b380b7..8267d2e3d 100644 --- a/tests/Aetheria.Shared.Tests/ExtractionTests.cs +++ b/tests/Aetheria.Shared.Tests/ExtractionTests.cs @@ -87,8 +87,11 @@ public sealed class ExtractionTests : IDisposable } }); _cache.Upsert(new CargoBayData { Name = "Crate", Shape = new Shape(), InteriorShape = Solid(4, 4), Price = 20, Durability = 4 }); - _cache.Upsert(new CargoBayData { Name = "Vault", Shape = new Shape(), InteriorShape = Solid(12, 12), Price = 20, Durability = 4 }); - _cache.Upsert(new CargoBayData { Name = "Pocket", Shape = new Shape(), InteriorShape = Solid(1, 1), Price = 5, Durability = 4 }); + // A rig's ore lands in product bays only (ruling station-bays-by-category): the Vault and the Pocket are product bays, the Stockroom is not. + _cache.Upsert(new ProductBayData { Name = "Vault", Shape = new Shape(), InteriorShape = Solid(40, 40), Price = 20, Durability = 4 }); + _cache.Upsert(new ProductBayData { Name = "Closet", Shape = new Shape(), InteriorShape = Solid(6, 6), Price = 20, Durability = 4 }); + _cache.Upsert(new CargoBayData { Name = "Stockroom", Shape = new Shape(), InteriorShape = Solid(12, 12), Price = 20, Durability = 4 }); + _cache.Upsert(new ProductBayData { Name = "Pocket", Shape = new Shape(), InteriorShape = Solid(1, 1), Price = 5, Durability = 4 }); _cache.Upsert(new SimpleCommodityData { Name = "Slag", Shape = new Shape(), Price = 1, MaxStack = 100 }); } @@ -350,10 +353,11 @@ public sealed class ExtractionTests : IDisposable [Fact] public void RigStopsWhenFull() { - var on = Build("Extraction Rig", new[] { 3 }, bay: "Pocket"); - var off = Build("Extraction Rig", new[] { 3 }, bay: "Pocket"); + var on = Build("Extraction Rig", new[] { 3 }, bay: "Closet"); + var off = Build("Extraction Rig", new[] { 3 }, bay: "Closet"); + // The product bay is full of slag: no cell is left for ore, though the ores' shares have room. foreach (var world in new[] { on, off }) - Assert.True(world.Station.CargoBays[0].TryStore(new SimpleCommodity { Data = new CultRecordRef(Good("Slag")), Quantity = 1 })); + while (world.Station.CargoBays[0].TryStore(new SimpleCommodity { Data = new CultRecordRef(Good("Slag")), Quantity = 100 })) { } Step(on, 30); Assert.Equal(Stall.Room, on.Facility.Stall); Assert.Equal(0, Units(on)); @@ -366,13 +370,13 @@ public sealed class ExtractionTests : IDisposable Assert.Equal(2.0, off.Unit.Run.Progress); } - // Where a rig's ore lands is where Advance's put lands it: the silo, up to its share of each good, and never the station's other bays. + // Where a rig's ore lands is where Advance's put lands it: the silo, up to its share of each good, and never the station's Stock bays. [Fact] public void RigLandsInTheSilosShare() { - var world = Build("Extraction Rig", new[] { 20 }, silo: true); + var world = Build("Extraction Rig", new[] { 20 }, bay: "Stockroom", silo: true); var silo = world.Station.CargoBays.Single(bay => bay.Data is SiloData); - var vault = world.Station.CargoBays.Single(bay => !(bay.Data is SiloData)); + var vault = world.Station.CargoBays.Single(bay => bay.Data.Role == BayRole.Stock); var shareA = StationWants.Allocation(world.Station, _oreA).Value; var shareB = StationWants.Allocation(world.Station, _oreB).Value; Step(world, 3000); diff --git a/tests/Aetheria.Shared.Tests/FacilityProductionTests.cs b/tests/Aetheria.Shared.Tests/FacilityProductionTests.cs index 3fcb4fce7..559d42d47 100644 --- a/tests/Aetheria.Shared.Tests/FacilityProductionTests.cs +++ b/tests/Aetheria.Shared.Tests/FacilityProductionTests.cs @@ -61,7 +61,10 @@ public sealed class FacilityProductionTests : IDisposable new HardpointData { Type = HardpointType.Facility, Position = new int2(1, 5), Shape = Solid(2, 2), Transform = "Fa.4" } } }); - cache.Upsert(new CargoBayData { Name = "Crate", Shape = new Shape(), InteriorShape = Solid(4, 4), Price = 20, Durability = 4 }); + // A station's holds are by role (ruling station-bays-by-category): a Crate takes what the facility draws, a Warehouse what it makes. + cache.Upsert(new IngredientBayData { Name = "Crate", Shape = new Shape(), InteriorShape = Solid(4, 4), Price = 20, Durability = 4 }); + cache.Upsert(new ProductBayData { Name = "Warehouse", Shape = new Shape(), InteriorShape = Solid(12, 12), Price = 20, Durability = 4 }); + cache.Upsert(new CargoBayData { Name = "Locker", Shape = new Shape(), InteriorShape = Solid(4, 4), Price = 20, Durability = 4 }); cache.Upsert(new CargoBayData { Name = "Pocket", Shape = new Shape(), InteriorShape = Solid(1, 1), Price = 5, Durability = 4 }); cache.Upsert(new SimpleCommodityData { Name = "Slag", Shape = new Shape(), Price = 1, MaxStack = 100 }); cache.Upsert(new GearData { Name = "Lamp", Shape = Solid(1, 1), Price = 1, Durability = 4, Hardpoint = HardpointType.Tool }); @@ -89,8 +92,8 @@ public sealed class FacilityProductionTests : IDisposable }; } - // A station of Test Yard with the named facility on its row and the named bay, and the shipped silo on its second row when asked. - private World Build(string facility = "Refinery", string bay = "Crate", bool silo = false, bool twin = false) + // A station of Test Yard with the named facility on its row and the named bay, and a product bay: the shipped silo on its second row when asked, else a Warehouse. + private World Build(string facility = "Refinery", string bay = "Crate", bool silo = false, bool twin = false, bool products = true) { var items = new ItemManager(1u, _cache, new ProvenanceLedger(), RunSaveTests.TestSettings(), _ => { }); var zone = new Zone(items, new PlanetSettings(), new ZonePack(), new GalaxyZone { Name = "Test Zone", Owner = null }, null); @@ -98,6 +101,7 @@ public sealed class FacilityProductionTests : IDisposable Assert.True(station.TryEquip(Mint(items, _cache, facility), new int2(0, 0))); Assert.True(station.TryEquip(Mint(items, _cache, bay), new int2(4, 4))); if (silo) Assert.True(station.TryEquip(Mint(items, _cache, "Silo"), new int2(2, 0))); + else if (products) Assert.True(station.TryEquip(Mint(items, _cache, "Warehouse"))); if (twin) Assert.True(station.TryEquip(Mint(items, _cache, facility), new int2(0, 2))); zone.Admit(station, piloted: false); return Wrap(items, zone, station); @@ -158,7 +162,7 @@ public sealed class FacilityProductionTests : IDisposable Assert.Equal(0, Held(world, IronFillers[0])); Assert.Equal(0, world.Unit.Run.Progress); Assert.Equal(Stall.Inputs, world.Facility.Stall); - Assert.Equal(1, world.Station.CargoBays[0].Cargo.Count); + Assert.Equal(1, world.Station.CargoBays[1].Cargo.Count); } [Fact] @@ -186,15 +190,22 @@ public sealed class FacilityProductionTests : IDisposable Assert.Equal(Stall.Inputs, world.Facility.Stall); } + // A product bay with no cell left holds the finished batch: the cycle stays at its end and stalls on Room, then puts when a cell frees. [Fact] - public void FullHoldHoldsTheBatch() + public void FullProductBayHoldsTheBatch() { - var world = Build(bay: "Pocket"); + var world = Build(silo: true); Stock(world, IronFillers[0], 3); Step(world, 1); Assert.Equal(0, Held(world, IronFillers[0])); - var slag = Goods("Slag", 1); - Assert.True(world.Station.CargoBays[0].TryStore(slag)); + var silo = SiloOf(world); + var slag = new List(); + while (true) + { + var stack = Goods("Slag", 100); + if (!silo.TryStore(stack)) break; + slag.Add(stack); + } Step(world, StepsOf(IronCycle) + 100); Assert.Equal(Stall.Room, world.Facility.Stall); @@ -202,7 +213,7 @@ public sealed class FacilityProductionTests : IDisposable Assert.Equal(0, Held(world, Iron)); Assert.Equal(0, Held(world, IronFillers[0])); - world.Station.CargoBays[0].Remove(slag); + silo.Remove(slag[0]); Step(world, 1); Assert.Equal(2, Held(world, Iron)); Assert.Equal(0, world.Unit.Run.Progress); @@ -333,7 +344,7 @@ public sealed class FacilityProductionTests : IDisposable // Cut facility-output-room: a station with a silo keeps what its facilities make there, and each product holds an even share of it. private static EquippedCargoBay SiloOf(World world) => world.Station.CargoBays.Single(bay => bay.Data is SiloData); - private static EquippedCargoBay StockOf(World world) => world.Station.CargoBays.Single(bay => !(bay.Data is SiloData)); + private static EquippedCargoBay StockOf(World world) => world.Station.CargoBays.Single(bay => bay.Data.Role == BayRole.Ingredients); private IReadOnlyList RefineryRuns => Production.RunList(_cache, _cache.RefOf(_cache.GetByName("Refinery")).Key); @@ -474,13 +485,19 @@ public sealed class FacilityProductionTests : IDisposable Assert.Equal(Stall.Inputs, Production.Advance(_cache, runs, null, 1, empty.Hold).stall); } + // Outputs land only in product bays (pin OutputsLandOnlyInProductBays): a station with outputs and no product bay has no room for them, + // whatever its other bays hold, and starts nothing. [Fact] - public void ASiloFreeStationKeepsRoomAtThePut() + public void AStationWithoutAProductBayStartsNothing() { - var world = Build(); - Assert.Null(StationWants.Allocation(world.Station, Iron)); - Assert.Equal(int.MaxValue, world.Hold.Room(Iron)); - Assert.Equal(world.Station.CargoBays, StationWants.OutputBays(world.Station, Iron)); + var world = Build(products: false); + Assert.Equal(0, StationWants.Allocation(world.Station, Iron)); + Assert.Equal(0, world.Hold.Room(Iron)); + Assert.Empty(StationWants.OutputBays(world.Station, Iron)); + Stock(world, IronFillers[0], 3); + Step(world, 3); + Assert.Null(world.Unit.Run); + Assert.Equal(Stall.Room, world.Facility.Stall); } [Fact] @@ -497,7 +514,7 @@ public sealed class FacilityProductionTests : IDisposable Assert.Contains("Facility Pill", error.Message); } - // The shipped catalog as it boots, a shipped station hull with no heater, the shipped Refinery on its Facility row: an hour of sim + // The shipped catalog as it boots, a shipped station hull with no heater, the shipped Refinery on its Facility row and the shipped Silo on another: an hour of sim // time (the station's cells cool toward 0 meanwhile) turns 15 ore into 5 cycles of iron, one cycle fewer than the hour holds. [Fact] public void TheShippedRefineryRunsAnHourOnAHeaterlessShippedStation() @@ -509,6 +526,7 @@ public sealed class FacilityProductionTests : IDisposable Assert.True(station.TryEquip(Mint(items, shipped, "Refinery"))); var bay = shipped.GetAll().Where(data => !(data is DockingBayData)).OrderByDescending(data => data.InteriorShape.Coordinates.Length).ThenBy(data => data.Name, StringComparer.Ordinal).First(); Assert.True(station.TryEquip(Mint(items, shipped, bay.Name))); + Assert.True(station.TryEquip(Mint(items, shipped, "Silo"))); Assert.DoesNotContain(station.Equipment.Select(equipped => equipped.Data), data => data is GearData gear && gear.Behaviors.Any(behavior => behavior is HeatData)); zone.Admit(station, piloted: false); var world = Wrap(items, zone, station); @@ -544,7 +562,7 @@ public sealed class FacilityProductionTests : IDisposable [Fact] public void TheCargoRevisionCountsEveryChangeToWhatTheBaysHold() { - var world = Build(); + var world = Build(bay: "Locker"); var bay = world.Station.CargoBays[0]; var at = world.Station.CargoRevision; Assert.Equal(at, world.Hold.Revision); @@ -566,10 +584,10 @@ public sealed class FacilityProductionTests : IDisposable bay.Remove(unit); Moved(); world.Station.Deactivate(); - var pocket = world.Station.TryEquip(Mint(world.Items, _cache, "Pocket"), new int2(6, 6)); + var pocket = world.Station.TryEquip(Mint(world.Items, _cache, "Pocket")); Assert.True(pocket); Moved(); - Assert.NotNull(world.Station.TryUnequip(world.Station.CargoBays[1])); + Assert.NotNull(world.Station.TryUnequip(world.Station.CargoBays.Last())); Moved(); } @@ -580,14 +598,14 @@ public sealed class FacilityProductionTests : IDisposable { var world = Build(); world.Station.Deactivate(); - Assert.True(world.Station.TryEquip(Mint(world.Items, _cache, "Pocket"), new int2(6, 6))); + Assert.True(world.Station.TryEquip(Mint(world.Items, _cache, "Pocket"))); world.Station.Activate(); Stock(world, IronFillers[0], 2); Step(world, 3); Assert.Equal(Stall.Inputs, world.Facility.Stall); world.Station.Deactivate(); - Assert.NotNull(world.Station.TryUnequip(world.Station.CargoBays[1])); + Assert.NotNull(world.Station.TryUnequip(world.Station.CargoBays.Last())); Stock(world, IronFillers[0], 1); world.Station.Activate(); Step(world, 3); diff --git a/tests/Aetheria.Shared.Tests/HaulRunTests.cs b/tests/Aetheria.Shared.Tests/HaulRunTests.cs index bd0aca07a..13989acbb 100644 --- a/tests/Aetheria.Shared.Tests/HaulRunTests.cs +++ b/tests/Aetheria.Shared.Tests/HaulRunTests.cs @@ -26,8 +26,8 @@ public sealed partial class RunStartTests var refinery = StationOfKind(arena, "processing"); var player = staged.Player; var silo = zenith.CargoBays.Single(bay => bay.Data is SiloData); - var stock = zenith.CargoBays.Single(bay => !(bay.Data is SiloData)); - var good = StationWants.Of(zenith).OrderBy(key => key.Value, StringComparer.Ordinal).First(); + var stock = StationWants.RoleBays(zenith, BayRole.Stock).Single(); + var good = _cache.GetAll().Where(order => order.From.Key.Equals(zenith.OrbitData)).SelectMany(order => order.Goods.Select(held => held.Key)).OrderBy(key => key.Value, StringComparer.Ordinal).First(); Assert.Equal(new[] { silo }, StationWants.Bays(zenith, good)); Assert.Empty(StationWants.Bays(zenith, new CultRecordKey("commodity:not-wanted"))); @@ -45,7 +45,7 @@ public sealed partial class RunStartTests Assert.Equal(0, stock.Count(good)); var drawn = StationWants.Of(refinery).First(key => FieldKinds.IsOre(_cache, _cache.Get(new CultRecordRef(key)))); - Assert.Equal(refinery.CargoBays.Where(bay => !(bay.Data is SiloData)), StationWants.Bays(refinery, drawn)); + Assert.Equal(StationWants.RoleBays(refinery, BayRole.Ingredients), StationWants.Bays(refinery, drawn)); var ore = new SimpleCommodity { Data = new CultRecordRef(drawn), Quantity = 5 }; Assert.True(hold.TryStore(ore)); var taken = StationServices.TrySellPart(hold, refinery, ore, ref credits); diff --git a/tests/Aetheria.Shared.Tests/StationServicesTests.cs b/tests/Aetheria.Shared.Tests/StationServicesTests.cs index fd604935a..40417c2e7 100644 --- a/tests/Aetheria.Shared.Tests/StationServicesTests.cs +++ b/tests/Aetheria.Shared.Tests/StationServicesTests.cs @@ -36,6 +36,9 @@ public sealed class StationServicesTests : IDisposable var bay = new Shape(4, 4); foreach (var cell in bay.AllCoordinates) bay[cell] = true; _cache.Upsert(new CargoBayData { Name = "Crate", Shape = new Shape(), InteriorShape = bay, Price = 20, Durability = 4 }); + // A station's holds are by role (ruling station-bays-by-category): a Pantry takes what its Smelter draws, a Store what it makes. + _cache.Upsert(new IngredientBayData { Name = "Pantry", Shape = new Shape(), InteriorShape = bay, Price = 20, Durability = 4 }); + _cache.Upsert(new ProductBayData { Name = "Store", Shape = new Shape(), InteriorShape = bay, Price = 20, Durability = 4 }); _cache.Upsert(new DockingBayData { Name = "Berth", Shape = new Shape(), InteriorShape = bay, MaxSize = new int2(2, 2), Price = 60, Durability = 6 }); var ore = new SimpleCommodityData { Name = "Ore", Shape = new Shape(), Price = 7, MaxStack = 100 }; _cache.Upsert(ore); @@ -91,11 +94,15 @@ public sealed class StationServicesTests : IDisposable return ship; } - // A station whose one cargo bay can take the goods, or one with no cargo bay at all. + // A station whose ingredient bay can take the goods (and whose product bay holds what its Smelter makes), or one with no cargo bay at all. + private static EquippedCargoBay Pantry(Entity station) => StationWants.RoleBays(station, BayRole.Ingredients).Single(); + private OrbitalEntity Station(bool withBay = true, bool wants = true) { var station = new OrbitalEntity(_items, _zone, Mint(withBay ? "Hub" : "Bare Hub"), default, new EntitySettings()); - if (withBay) Assert.True(station.TryEquip(Mint("Crate"), new int2(1, 1))); + if (withBay) Assert.True(station.TryEquip(Mint("Pantry"), new int2(1, 1))); + if (withBay) Assert.True(station.TryEquip(Mint("Store"))); + if (withBay) Assert.True(station.TryEquip(Mint("Crate"))); if (withBay && wants) Assert.True(station.TryEquip(Mint("Smelter"), new int2(0, 0))); return station; } @@ -155,7 +162,7 @@ public sealed class StationServicesTests : IDisposable Assert.True(StationServices.TrySell(from, station, gun, ref credits)); Assert.Equal(1000 + StationServices.SellPrice(_items, Mint("Gun", 4)), credits); Assert.DoesNotContain(gun, from.Cargo.Keys); - Assert.Contains(gun, station.CargoBays.Single().Cargo.Keys); + Assert.Contains(gun, StationWants.RoleBays(station, BayRole.Stock).Single().Cargo.Keys); // A station with nowhere to put it takes nothing and pays nothing. var other = Mint("Gun"); @@ -189,7 +196,7 @@ public sealed class StationServicesTests : IDisposable Assert.Equal((10, 10, SaleRefusal.None), (sold.Sold, sold.Offered, sold.Refusal)); Assert.Equal(1035, credits); Assert.DoesNotContain(ore, from.Cargo.Keys); - Assert.Equal(10, station.CargoBays.Single().Cargo.Keys.OfType().Sum(c => c.Quantity)); + Assert.Equal(10, Pantry(station).Cargo.Keys.OfType().Sum(c => c.Quantity)); var refused = StationServices.TrySellPart(from, station, shells, ref credits); Assert.Equal((0, 5, SaleRefusal.NotWanted), (refused.Sold, refused.Offered, refused.Refusal)); @@ -219,15 +226,15 @@ public sealed class StationServicesTests : IDisposable Assert.True(from.TryStore(ore)); var station = Station(); Assert.True(station.TryEquip(Mint("Crate"), new int2(2, 2))); - var full = station.CargoBays[0]; - var open = station.CargoBays[1]; + var full = StationWants.RoleBays(station, BayRole.Stock).First(); + var open = StationWants.RoleBays(station, BayRole.Stock).Last(); while (full.TryStore(Mint("Gun"))) { } var credits = 0; Assert.True(StationServices.TrySell(from, station, gun, ref credits)); Assert.Equal(10, StationServices.TrySellPart(from, station, ore, ref credits).Sold); Assert.Contains(gun, open.Cargo.Keys); - Assert.Contains(ore, open.Cargo.Keys); + Assert.Contains(ore, Pantry(station).Cargo.Keys); } // Nothing is absorbed (ruling no-market-absorption): a hold with room for some units takes those and pays for those @@ -237,11 +244,11 @@ public sealed class StationServicesTests : IDisposable { var from = Ship().CargoBays.Single(); var station = Station(); - var bay = station.CargoBays[0]; - // The station's one ore stack holds 60 of its 100 and every other cell holds a gun: 40 units fit. - var shelf = Ore(60); - Assert.True(bay.TryStore(shelf)); - while (bay.TryStore(Mint("Gun"))) { } + var bay = Pantry(station); + // The station holds all but 40 units of Ore's share of its ingredient bay: 40 units fit. + var share = StationWants.Holding(station)[_cache.RefOf(_cache.GetByName("Ore")).Key]; + var shelf = Ore(100); + for (var held = 0; held < share - 40; held += 100) Assert.True(bay.TryStore(shelf = Ore(Math.Min(100, share - 40 - held)))); var big = Ore(100); Assert.True(from.TryStore(big)); var credits = 0; @@ -250,9 +257,8 @@ public sealed class StationServicesTests : IDisposable Assert.Equal(StationServices.SellPrice(_items, Ore(40)), credits); Assert.Contains(big, from.Cargo.Keys); Assert.Equal(60, big.Quantity); - Assert.Equal(100, shelf.Quantity); - // Now the stack is full and no cell is free: nothing sells, nothing is paid, the stack stays. + // Now the share is full: nothing sells, nothing is paid, the stack stays. credits = 0; var refused = StationServices.TrySellPart(from, station, big, ref credits); Assert.Equal((0, 60, SaleRefusal.NoRoom), (refused.Sold, refused.Offered, refused.Refusal)); @@ -260,8 +266,6 @@ public sealed class StationServicesTests : IDisposable Assert.Equal(60, big.Quantity); } - // A commodity the seller does not hold sells for nothing: not one never stored, and not a stale copy of a stack the sale - // already took. The held stack is untouched, and no credit is paid. [Fact] public void TrySell_commodity_not_in_the_hold_pays_nothing() { @@ -273,14 +277,14 @@ public sealed class StationServicesTests : IDisposable Assert.Equal(0, StationServices.TrySellPart(from, station, Ore(10), ref credits).Sold); Assert.Equal(500, credits); - Assert.Empty(station.CargoBays.Single().Cargo.Keys.OfType()); + Assert.Empty(Pantry(station).Cargo.Keys.OfType()); Assert.Equal(10, Assert.Single(from.Cargo.Keys.OfType()).Quantity); Assert.Equal(10, StationServices.TrySellPart(from, station, held, ref credits).Sold); var paid = credits; Assert.Equal(0, StationServices.TrySellPart(from, station, held, ref credits).Sold); Assert.Equal(paid, credits); - Assert.Equal(10, station.CargoBays.Single().Cargo.Keys.OfType().Sum(c => c.Quantity)); + Assert.Equal(10, Pantry(station).Cargo.Keys.OfType().Sum(c => c.Quantity)); } [Fact] @@ -621,7 +625,8 @@ public sealed class StationServicesTests : IDisposable Assert.True(one.CargoBays.Single().TryStore(Ore(30))); Assert.True(two.CargoBays.Single().TryStore(Ore(40))); var full = Station(); - while (full.CargoBays.Single().TryStore(Mint("Gun"))) { } + var oreKey = _cache.RefOf(_cache.GetByName("Ore")).Key; + for (var held = 0; held < StationWants.Holding(full)[oreKey]; held += 100) Assert.True(Pantry(full).TryStore(Ore(100))); credits = 0; var refused = StationServices.TrySellOre(bays, full, ref credits); Assert.Equal((0, 70, SaleRefusal.NoRoom), (refused.Sold, refused.Offered, refused.Refusal)); diff --git a/tests/Aetheria.Shared.Tests/StationWantsTests.cs b/tests/Aetheria.Shared.Tests/StationWantsTests.cs index 26acb8b62..67a67c106 100644 --- a/tests/Aetheria.Shared.Tests/StationWantsTests.cs +++ b/tests/Aetheria.Shared.Tests/StationWantsTests.cs @@ -229,7 +229,7 @@ public sealed partial class RunStartTests Assert.Empty(_cache.GetAll()); } - // Generated gear stock stops at half the stock bay: every demo station keeps at least (1 - StationStockShare) of its cells free. + // Generated gear stock stops at half the Stock bays: every demo station keeps at least (1 - StationStockShare) of their cells free. [Fact] public void GeneratedStockLeavesRoom() { @@ -239,9 +239,9 @@ public sealed partial class RunStartTests Assert.True(failures.Count == 0, $"seed {seed}: " + string.Join("; ", failures)); foreach (var station in arena.Entities.OfType().Where(entity => entity.HullData.HullType == HullType.Station)) { - var bay = station.CargoBays.First(candidate => !(candidate.Data is SiloData)); - var cells = bay.Data.InteriorShape.Coordinates.Length; - var free = bay.Data.InteriorShape.Coordinates.Count(cell => bay.Occupancy[cell.x, cell.y] == null); + var bays = StationWants.RoleBays(station, BayRole.Stock).ToArray(); + var cells = bays.Sum(bay => bay.Data.InteriorShape.Coordinates.Length); + var free = bays.Sum(bay => bay.Data.InteriorShape.Coordinates.Count(cell => bay.Occupancy[cell.x, cell.y] == null)); var floor = cells - (int) (cells * LoadoutGenerator.StationStockShare); Assert.True(free >= floor, $"seed {seed} {station.Name}: {free} of {cells} cells free, at least {floor} wanted"); } @@ -285,23 +285,30 @@ public sealed partial class RunStartTests return (fills.Max(slot => slot.Count), fills.Sum(slot => slot.Rate / slot.Goods.Length)); } - // What a good holds by the rule, worked out here from the catalog: its floor, and its weight's part of what the floors leave of the target. + private int FloorCells(CultRecordKey good, int floor) => Math.Max(1, (int) Math.Ceiling(floor / (double) MaxStack(good))); + + private static int IngredientCells(Entity station) => StationWants.RoleBays(station, BayRole.Ingredients).Sum(bay => bay.Data.InteriorShape.Coordinates.Length); + + // What a good holds by the rule, worked out here from the catalog: the cells of its floor, and its weight's part of the cells the floors + // and the crew's goods leave, times its stack. private int Expected(Entity station, List<(double Rate, int Count, CultRecordKey[] Goods)> slots, CultRecordKey good) { var goods = slots.SelectMany(slot => slot.Goods).Distinct().ToArray(); - var left = StationWants.IngredientShare * StationWants.StockRoom(station) - goods.Sum(other => (double) FloorAndWeight(slots, other).Floor / MaxStack(other)); + var crewOnly = station.Equipment.SelectMany(unit => unit.Behaviors.Select(behavior => behavior.Data).OfType()) + .SelectMany(data => data.Table.Select(row => row.Commodity.Key)).Distinct().Count(crew => !goods.Contains(crew)); + var left = IngredientCells(station) - goods.Sum(other => FloorCells(other, FloorAndWeight(slots, other).Floor)) - crewOnly; var (floor, weight) = FloorAndWeight(slots, good); - return floor + (left > 0 ? Floored(left * MaxStack(good) * weight / slots.Sum(slot => slot.Rate)) : 0); + return (FloorCells(good, floor) + (left > 0 ? Floored(left * weight / slots.Sum(slot => slot.Rate)) : 0)) * MaxStack(good); } - // Puts exactly `units` of the good in the station's stock bays, after taking out what is there. + // Puts exactly `units` of the good in the station's ingredient bays, after taking out what is there. private static void Hold(Entity station, CultRecordKey good, int units) { - foreach (var bay in station.CargoBays.Where(bay => !(bay.Data is SiloData))) + foreach (var bay in StationWants.RoleBays(station, BayRole.Ingredients)) foreach (var stack in bay.Cargo.Keys.Where(stack => stack is SimpleCommodity held && held.Data.Key.Equals(good)).ToArray()) bay.Remove(stack); for (var left = units; left > 0; left -= 10) - Assert.True(station.CargoBays.Where(bay => !(bay.Data is SiloData)).Any(bay => bay.TryStore(new SimpleCommodity { Data = new CultRecordRef(good), Quantity = Math.Min(10, left) }))); + Assert.True(StationWants.RoleBays(station, BayRole.Ingredients).Any(bay => bay.TryStore(new SimpleCommodity { Data = new CultRecordRef(good), Quantity = Math.Min(10, left) }))); Assert.Equal(units, station.CargoBays.Sum(bay => bay.Count(good))); } @@ -348,16 +355,19 @@ public sealed partial class RunStartTests [Fact] public void TheShareFollowsTheRecipesSlots() { - var refinery = DemoStation("processing"); + var shipped = DemoStation("processing"); + // The shipped refinery's ingredient bays are fitted to its floors (Need), so they leave next to nothing to split: the split is read on a copy + // given every ingredient bay its hull has room for. + var refinery = (OrbitalEntity) EntitySerializer.Unpack(_items, shipped.Zone, EntitySerializer.Pack(shipped)); + var bay = _cache.GetAll().First(product => _cache.Get(product.Design) is IngredientBayData); + while (refinery.TryEquip(_items.CreateInstance(bay) as EquippableItem)) { } + Assert.True(IngredientCells(refinery) > IngredientCells(shipped) + 8); var slots = SlotsOf(refinery); - // The generated gear stock leaves the shipped refinery next to no room over its floors, so the split is read with the gear put away. - foreach (var bay in refinery.CargoBays) - foreach (var gear in bay.Cargo.Keys.Where(item => !(item is ICommodityStack)).ToList()) bay.Remove(gear); var holding = StationWants.Holding(refinery); Assert.Equal(slots.SelectMany(slot => slot.Goods).Distinct().OrderBy(good => good.Value, StringComparer.Ordinal), holding.Keys.OrderBy(good => good.Value, StringComparer.Ordinal)); foreach (var good in holding.Keys) Assert.Equal(Expected(refinery, slots, good), holding[good]); - var extra = holding.Keys.ToDictionary(good => good, good => holding[good] - FloorAndWeight(slots, good).Floor); - Assert.True(extra.Values.Max() > 2 * Math.Max(1, extra.Values.Where(units => units > 0).Min()), "the split is not even"); + var extra = holding.Keys.ToDictionary(good => good, good => holding[good] / MaxStack(good) - FloorCells(good, FloorAndWeight(slots, good).Floor)); + Assert.True(extra.Values.Max() > extra.Values.Min(), "the split is even"); } // A role slot is one slot, not one per filler: the fillers of a role that fill nothing else hold the same, each its even part of the slot's rate. @@ -389,42 +399,42 @@ public sealed partial class RunStartTests var holding = StationWants.Holding(refinery); foreach (var good in holding.Keys) Assert.True(holding[good] >= FloorAndWeight(slots, good).Floor, $"{_cache.Get(good).Name}: {holding[good]} held for a slot of {FloorAndWeight(slots, good).Floor}"); - Assert.Contains(holding.Keys, good => holding[good] == FloorAndWeight(slots, good).Floor); + Assert.All(holding.Keys, good => Assert.Equal(0, holding[good] % MaxStack(good))); } - // The shares together never exceed the target, IngredientShare of the stock room the gear stock leaves: in cells, whatever the room (gear - // put away widens it) and the shape of the slots. The first goods to arrive cannot fill a bay the shares overcommit. + // Whole cells (pin SharesAreWholeCells): every drawn good holds at least one cell, each share is whole cells of its stack, and the cells of the + // shares together are at most the ingredient bays' cells: the first goods to arrive cannot fill a bay the shares overcommit. [Fact] - public void SharesNeverSumAboveTheTarget() + public void SharesAreWholeCells() { var refinery = DemoStation("processing"); - double Cells() => StationWants.Holding(refinery).Where(share => SlotsOf(refinery).Any(slot => slot.Goods.Contains(share.Key))).Sum(share => (double) share.Value / MaxStack(share.Key)); - var room = StationWants.StockRoom(refinery); - Assert.True(room > 0); - Assert.True(Cells() <= StationWants.IngredientShare * room + 1e-9, $"{Cells()} cells of shares for a target of {StationWants.IngredientShare * room}"); - Assert.True(Cells() > StationWants.IngredientShare * room - 1.5, "the shares leave most of the target unused"); - foreach (var bay in refinery.CargoBays) - foreach (var gear in bay.Cargo.Keys.Where(item => !(item is ICommodityStack)).ToList()) bay.Remove(gear); - var wider = StationWants.StockRoom(refinery); - Assert.True(wider > room); - Assert.True(Cells() <= StationWants.IngredientShare * wider + 1e-9, $"{Cells()} cells of shares for a target of {StationWants.IngredientShare * wider}"); - Assert.True(Cells() > StationWants.IngredientShare * wider - 1.5); + var holding = StationWants.Holding(refinery); + var slots = SlotsOf(refinery); + foreach (var share in holding) + { + Assert.True(share.Value >= MaxStack(share.Key), $"{_cache.Get(share.Key).Name} holds under a cell"); + Assert.Equal(0, share.Value % MaxStack(share.Key)); + } + var cells = holding.Sum(share => share.Value / MaxStack(share.Key)); + Assert.True(cells <= IngredientCells(refinery), $"{cells} cells of shares in {IngredientCells(refinery)}"); + Assert.True(slots.SelectMany(slot => slot.Goods).Distinct().All(good => holding.ContainsKey(good))); } - // A hull whose one-cycle floors exceed its stock room cannot start a cycle of every slot at once: FloorCells reads the excess, and the shares - // are the floors. The catalog is edited (a refinery recipe gains an input of 400 units a cycle) to make the shipped hull overflow. + // A hull whose one-cycle floors exceed its ingredient cells cannot start a cycle of every slot at once: FloorCells reads the excess, and the + // shares are the floors. The catalog is edited (a refinery recipe gains an input of 400 units a cycle) to make the shipped hull overflow. [Fact] public void FloorsBeyondTheRoomAreAHullFault() { var refinery = DemoStation("processing"); - Assert.True(StationWants.FloorCells(refinery) <= StationWants.StockRoom(refinery), $"the shipped refinery's floors take {StationWants.FloorCells(refinery)} of {StationWants.StockRoom(refinery)} cells"); + Assert.True(StationWants.FloorCells(refinery) <= IngredientCells(refinery), $"the shipped refinery's floors take {StationWants.FloorCells(refinery)} of {IngredientCells(refinery)} cells"); var design = refinery.Equipment.Select(unit => _items.GetData(unit.EquippableItem)).Single(data => StationKinds.IsRecipeFacility(data)); var recipe = Production.RunList(_cache, _cache.RefOf(design).Key).First(candidate => candidate.Inputs.Count > 0); recipe.Inputs.Add(new CommodityCount { Commodity = new CultRecordRef(GoodNamed("Regolith")), Count = 400 }); _cache.Upsert(typeof(RecipeData), recipe, RecipeData.KeyOf(recipe.Output.Key)); - Assert.True(StationWants.FloorCells(refinery) > StationWants.StockRoom(refinery)); + Assert.True(StationWants.FloorCells(refinery) > IngredientCells(refinery)); var slots = SlotsOf(refinery); - Assert.All(StationWants.Holding(refinery), share => Assert.Equal(FloorAndWeight(slots, share.Key).Floor, share.Value)); + Assert.All(StationWants.Holding(refinery).Where(share => slots.Any(slot => slot.Goods.Contains(share.Key))), share => + Assert.Equal(FloorCells(share.Key, FloorAndWeight(slots, share.Key).Floor) * MaxStack(share.Key), share.Value)); } // One cap for every buyer: at its share a good is refused by the player's sale, the ore sale, a hauler's delivery and the hauler's door @@ -544,4 +554,91 @@ public sealed partial class RunStartTests Assert.All(before.Keys.Where(good => !crew.Contains(good)), good => Assert.False(after.ContainsKey(good))); Assert.Equal(before.OrderBy(kv => kv.Key.Value, StringComparer.Ordinal), StationWants.Holding(refinery).OrderBy(kv => kv.Key.Value, StringComparer.Ordinal)); } + + // What a bay is for is the design's (cut station-role-bays, ruling station-bays-by-category): read from the type, never stored. + [Fact] + public void ARoleIsTheDesigns() + { + Assert.Equal(BayRole.Stock, new CargoBayData().Role); + Assert.Equal(BayRole.Stock, new DockingBayData().Role); + Assert.Equal(BayRole.Ingredients, new IngredientBayData().Role); + Assert.Equal(BayRole.Products, new ProductBayData().Role); + Assert.Equal(BayRole.Products, new SiloData().Role); + } + + // An ingredient or product bay holds commodity stacks only; a Stock bay takes gear. + [Fact] + public void RoleBaysHoldBulkOnly() + { + var refinery = DemoStation("processing"); + var gear = _cache.GetAll().Where(product => _cache.Get(product.Design) is GearData data && data.Shape.Coordinates.Length == 1 && !(data is HullData)).OrderBy(product => product.Name, StringComparer.Ordinal).First(); + foreach (var role in new[] { BayRole.Ingredients, BayRole.Products }) + Assert.All(StationWants.RoleBays(refinery, role), bay => Assert.False(bay.TryStore(_items.CreateInstance(gear)), $"a {role} bay took gear")); + Assert.Contains(StationWants.RoleBays(refinery, BayRole.Stock), bay => bay.TryStore(_items.CreateInstance(gear))); + } + + // A sale of a drawn good lands in the ingredient bays and nowhere else. + [Fact] + public void ADrawnGoodLandsOnlyInIngredientBays() + { + var (_, arena, staged, failures) = Launch(new DemoTerminus(), Inputs(() => 1)); + Assert.True(failures.Count == 0, string.Join("; ", failures)); + var refinery = StationOfKind(arena, "processing"); + var regolith = GoodNamed("Regolith"); + Assert.All(StationWants.Bays(refinery, regolith), bay => Assert.Equal(BayRole.Ingredients, bay.Data.Role)); + Assert.NotEmpty(StationWants.Bays(refinery, regolith)); + var sold = SoldFrom(staged.Player.CargoBays[0], refinery, regolith, 30); + Assert.True(sold > 0); + Assert.Equal(sold, StationWants.RoleBays(refinery, BayRole.Ingredients).Sum(bay => bay.Count(regolith))); + Assert.Equal(0, refinery.CargoBays.Where(bay => bay.Data.Role != BayRole.Ingredients).Sum(bay => bay.Count(regolith))); + } + + // Generation fits the role bays to Need for every demo station of the pinned seeds. + [Fact] + public void GenerationMeetsNeed() + { + foreach (var seed in new uint[] { 1, 2, 3, 69, 97 }) + { + var (_, arena, _, failures) = Launch(new DemoTerminus(), Inputs(() => seed)); + Assert.True(failures.Count == 0, $"seed {seed}: " + string.Join("; ", failures)); + foreach (var station in arena.Entities.OfType().Where(entity => entity.HullData.HullType == HullType.Station)) + foreach (var role in new[] { BayRole.Ingredients, BayRole.Products }) + { + var cells = StationWants.RoleBays(station, role).Sum(bay => bay.Data.InteriorShape.Coordinates.Length); + Assert.True(cells >= StationWants.Need(station, role), $"seed {seed} {station.Name}: {cells} {role} cells, needs {StationWants.Need(station, role)}"); + } + } + } + + // A station whose hull cannot take the bays it needs is a hull fault, named: here the refinery recipe's draw outgrows every hull. + [Fact] + public void AHullWithoutRoomFailsByName() + { + var design = _cache.GetAll().Single(data => data.Name == "Refinery"); + var recipe = Production.RunList(_cache, _cache.RefOf(design).Key).First(candidate => candidate.Inputs.Count > 0); + recipe.Inputs.Add(new CommodityCount { Commodity = new CultRecordRef(GoodNamed("Regolith")), Count = 4000 }); + _cache.Upsert(typeof(RecipeData), recipe, RecipeData.KeyOf(recipe.Output.Key)); + var error = Assert.Throws(() => Launch(new DemoTerminus(), Inputs(() => 1))); + Assert.Contains("Ingredients", error.Message); + Assert.Contains("needs", error.Message); + } + + // Generated gear lands only in the Stock bays and stops at StationStockShare of their cells. + [Fact] + public void GearStockOnlyInStockBays() + { + foreach (var seed in new uint[] { 1, 2, 3, 69, 97 }) + { + var (_, arena, _, failures) = Launch(new DemoTerminus(), Inputs(() => seed)); + Assert.True(failures.Count == 0, $"seed {seed}: " + string.Join("; ", failures)); + foreach (var station in arena.Entities.OfType().Where(entity => entity.HullData.HullType == HullType.Station)) + { + Assert.All(station.CargoBays.Where(bay => bay.Data.Role != BayRole.Stock), bay => Assert.Empty(bay.Cargo.Keys.Where(item => !(item is ICommodityStack)))); + var stock = StationWants.RoleBays(station, BayRole.Stock).ToArray(); + var gear = stock.Sum(bay => bay.Cargo.Keys.Where(item => !(item is ICommodityStack)).Sum(item => _items.GetData(item).Shape.Coordinates.Length)); + var cells = stock.Sum(bay => bay.Data.InteriorShape.Coordinates.Length); + Assert.True(gear <= (int) (cells * LoadoutGenerator.StationStockShare) + 1, $"seed {seed} {station.Name}: {gear} of {cells} stock cells hold gear"); + } + } + } }