diff --git a/Assets/Scripts/ServerShared/LoadoutGenerator.cs b/Assets/Scripts/ServerShared/LoadoutGenerator.cs index ed221b00a..3f7ee1110 100644 --- a/Assets/Scripts/ServerShared/LoadoutGenerator.cs +++ b/Assets/Scripts/ServerShared/LoadoutGenerator.cs @@ -148,9 +148,7 @@ public class LoadoutGenerator // The gear stock goes to the Stock bays and stops short of StationStockShare of their cells together, so the rest stays free // for pawned gear (ruling pawn-stock-in-stock-bay-share). All sixteen picks are still drawn, so no random stream moves; // only storing stops. - var stockBays = entity.CargoBays.Where(bay => bay.Data.Role == BayRole.Stock).ToArray(); - var stockCells = (int) (stockBays.Sum(bay => bay.Data.InteriorShape.Coordinates.Length) * StationStockShare); - var stocked = stockBays.Sum(bay => bay.Data.InteriorShape.Coordinates.Count(cell => bay.Occupancy[cell.x, cell.y] != null)); + var stockBays = StationWants.RoleBays(entity, BayRole.Stock).ToArray(); var inventory = RandomProducts(16, 1, data => !(data is HullData hull && hull.HullType != HullType.Ship) && !(data is CargoBayData)) .OrderByDescending(entry => entry.design.Shape.Coordinates.Length); @@ -158,8 +156,8 @@ public class LoadoutGenerator { var instance = ItemManager.CreateInstance(entry.product); var cells = entry.design.Shape.Coordinates.Length; - if (stocked + cells > stockCells) continue; - if (stockBays.Any(bay => bay.TryStore(instance))) stocked += cells; + if (cells > StationServices.StockRoom(entity)) continue; + stockBays.Any(bay => bay.TryStore(instance)); } entity.CanTow = hullData.CanTow; diff --git a/Assets/Scripts/ServerShared/StationServices.cs b/Assets/Scripts/ServerShared/StationServices.cs index 624189bba..f8e81361e 100644 --- a/Assets/Scripts/ServerShared/StationServices.cs +++ b/Assets/Scripts/ServerShared/StationServices.cs @@ -110,11 +110,21 @@ public static class StationServices if (item is ICommodityStack) throw new ArgumentException("A commodity stack sells through TrySellPart, which says how many units sold.", nameof(item)); var price = SellPrice(station.ItemManager, item); + if (station.ItemManager.GetData(item).Shape.Coordinates.Length > StockRoom(station)) return false; if (!station.CargoBays.Any(bay => from.TryTransferItem(bay, item, 1))) return false; credits += price; return true; } + // The cells of gear the Stock bays may still take: StationStockShare of their cells together, less the cells occupied, generated + // and pawned gear alike (ruling pawn-stock-in-stock-bay-share). Role bays refuse gear, so the Stock bays are the only bays gear lands in. + public static int StockRoom(Entity station) + { + var bays = StationWants.RoleBays(station, BayRole.Stock).ToArray(); + var cap = (int) (bays.Sum(bay => bay.Data.InteriorShape.Coordinates.Length) * LoadoutGenerator.StationStockShare); + return cap - bays.Sum(bay => bay.Data.InteriorShape.Coordinates.Count(cell => bay.Occupancy[cell.x, cell.y] != null)); + } + // The sale of a commodity stack: TryStorePart, and the station pays SellPrice for the units its bays took, added to // `credits` (aetheria-living-world:ruling:no-market-absorption: no unit is paid for that no bay took). The rest stays in the // seller's hold with the refusal NoRoom; a good the station does not want moves nothing (NotWanted). diff --git a/Assets/Scripts/ServerShared/StationWants.cs b/Assets/Scripts/ServerShared/StationWants.cs index cf6d2c7b4..e38bc80a3 100644 --- a/Assets/Scripts/ServerShared/StationWants.cs +++ b/Assets/Scripts/ServerShared/StationWants.cs @@ -95,14 +95,24 @@ public static class StationWants // Whether the recipe's product has room for one more cycle's output. public static bool CanStart(Entity station, RecipeData recipe) => Room(station, recipe.Output.Key) >= recipe.OutputCount; - // The run list of each installed recipe facility unit: two units of one design are two lists. + // The run list of each installed recipe facility unit: two units of one design are two lists. A list of raw recipes (a mine's) holds + // only the ores the station's ground holds, the ones Production.Advance can start (IProductionHold.Deposit); a station with no zone + // (a hull being generated) has no ground to read and lists the design's whole book. private static IEnumerable> FacilityRuns(Entity station) { var catalog = station.ItemManager.ItemData; + (CultRecordKey design, float amount)[] ground = null; foreach (var equipped in station.Equipment) { var design = station.ItemManager.GetData(equipped.EquippableItem); - if (StationKinds.IsRecipeFacility(design)) yield return Production.RunList(catalog, catalog.RefOf(design).Key); + if (!StationKinds.IsRecipeFacility(design)) continue; + var run = Production.RunList(catalog, catalog.RefOf(design).Key); + if (station.Zone != null && run.Count > 0 && run.All(recipe => Recipes.IsRaw(catalog, recipe))) + { + ground ??= Deposits.Of(station.Zone, catalog); + run = run.Where(recipe => ground.Any(held => held.design.Equals(recipe.Output.Key) && held.amount > 0f)).ToArray(); + } + yield return run; } } diff --git a/tests/Aetheria.Shared.Tests/EconomyRegionTests.cs b/tests/Aetheria.Shared.Tests/EconomyRegionTests.cs index b596fd208..863a4fe6a 100644 --- a/tests/Aetheria.Shared.Tests/EconomyRegionTests.cs +++ b/tests/Aetheria.Shared.Tests/EconomyRegionTests.cs @@ -91,6 +91,10 @@ public sealed class EconomyRegionTests // The rigs and skimmers of the region are facilities with recipes of their own: with the ground worked, an idle refinery's cause is the // ore only another faction makes or whose leg's hauler is lost, never "no haul leg": legs are seeded and fly. The report says extraction runs. + // A mine lists only the ores its ground holds (StationWants.FacilityRuns), so a refinery is idle for the ore a cross-faction leg or a lost + // hauler keeps from it. A recipe that never ran is never idle for Room: its product bays take at least one cycle of it. Refined goods that + // did run fill their share and stall their refinery on Room afterwards (nothing carries them away: ruling no-market-absorption), which + // reads in the facility lines, not as an idle recipe. [Fact] public void ExtractionRunsAndTheIdleCauseIsACrossFactionLegOrALostHauler() { @@ -102,6 +106,7 @@ public sealed class EconomyRegionTests Assert.NotEmpty(idle); Assert.Contains(idle, message => message.Contains("a cross-faction leg (region-cross-faction-legs)") || message.Contains("leg's hauler is lost")); Assert.All(idle, message => Assert.DoesNotContain("no haul leg", message)); + Assert.All(idle, message => Assert.DoesNotContain("no room for its output", message)); Assert.All(idle, message => Assert.DoesNotContain("no extraction runs", message)); Assert.All(idle, message => Assert.DoesNotContain("moves no cargo", message)); var text = new StringWriter(); diff --git a/tests/Aetheria.Shared.Tests/RegionLegsMemoryTests.cs b/tests/Aetheria.Shared.Tests/RegionLegsMemoryTests.cs index 8285c68af..965d05ad3 100644 --- a/tests/Aetheria.Shared.Tests/RegionLegsMemoryTests.cs +++ b/tests/Aetheria.Shared.Tests/RegionLegsMemoryTests.cs @@ -276,10 +276,9 @@ public sealed partial class RunStartTests // No ping-pong: over 24 sim h on seed 2 a hauler never turns straight back through the wormhole it came by without a call between (the // refusal expiring under its own cargo made it do so 238 times). Counted per hauler from the admissions into zones and its docks. - // The turn count is the mutant's separator: the refusal expiring under its cargo turns 54 haulers back at the role-bay head (238 before it). The - // transfer count is a ceiling on traffic, not a separation (margin rule not met: 4169 wormhole transfers shipped on seed 2 over 24 sim h with - // role bays, 2683 before them, because haulers now find room at both ends; the mutant's count was not re-measured, the turn count fails first). - private const int TransferBound = 5000; + // The turn count is the mutant's separator: the refusal expiring under its cargo turns 54 haulers back at the role-bay head (238 before it), 0 shipped. + // The transfer count has no upper bound: the mutant reads 4313 wormhole transfers on seed 2 over 24 sim h against 4169 shipped (a separation of 1.03, + // below the 1.2 the margin rule needs), so no bound claims it (lodur:ruling:deterministic-bound-margin). Traffic at all is the one thing it pins. [Fact] public void NoHaulerTurnsBackWithoutACall() @@ -304,6 +303,6 @@ public sealed partial class RunStartTests } AdvanceBy(galaxy, null, 0, 24 * 3600); Assert.Equal(0, turns); - Assert.InRange(transfers, 1, TransferBound); + Assert.True(transfers >= 1, "no hauler crossed a wormhole"); } } diff --git a/tests/Aetheria.Shared.Tests/StationServicesTests.cs b/tests/Aetheria.Shared.Tests/StationServicesTests.cs index 40417c2e7..bee471ad1 100644 --- a/tests/Aetheria.Shared.Tests/StationServicesTests.cs +++ b/tests/Aetheria.Shared.Tests/StationServicesTests.cs @@ -178,6 +178,28 @@ public sealed class StationServicesTests : IDisposable Assert.Throws(() => StationServices.TrySell(from, station, Ore(10), ref credits)); } + // Pawned gear sits in the Stock bays within StationStockShare of their cells together with the stock the station was generated with + // (ruling pawn-stock-in-stock-bay-share): the fixture station has one 16-cell Crate, so 8 cells; 3 are generated stock, so 5 pawn. + [Fact] + public void PawnedGearStopsAtTheStockShareWithGeneratedStock() + { + var station = Station(); + var stock = StationWants.RoleBays(station, BayRole.Stock).Single(); + for (var i = 0; i < 3; i++) Assert.True(stock.TryStore(Mint("Gun"))); + var from = Ship().CargoBays.Single(); + var held = new List(); + for (var i = 0; i < 16; i++) { var chip = Mint("Gun"); Assert.True(from.TryStore(chip)); held.Add(chip); } + + var credits = 0; + var pawned = held.TakeWhile(chip => StationServices.TrySell(from, station, chip, ref credits)).Count(); + + Assert.Equal(5, pawned); + Assert.Equal(5 * StationServices.SellPrice(_items, Mint("Gun")), credits); + Assert.Equal(0, StationServices.StockRoom(station)); + Assert.Equal(8, stock.Occupancy.Cast().Count(held => held != null)); + Assert.Contains(held[pawned], from.Cargo.Keys); + } + // A commodity sells by what the station's installed gear draws: the Smelter's recipe takes Ore, so Ore sells whole into room and // Shells, which no facility draws, are NotWanted with nothing moved (ruling installed-gear-decides-what-a-station-buys). [Fact] @@ -226,14 +248,17 @@ public sealed class StationServicesTests : IDisposable Assert.True(from.TryStore(ore)); var station = Station(); Assert.True(station.TryEquip(Mint("Crate"), new int2(2, 2))); + Assert.True(station.TryEquip(Mint("Crate"))); var full = StationWants.RoleBays(station, BayRole.Stock).First(); - var open = StationWants.RoleBays(station, BayRole.Stock).Last(); + var others = StationWants.RoleBays(station, BayRole.Stock).Skip(1).ToArray(); while (full.TryStore(Mint("Gun"))) { } + // Three Crates, 48 cells: the stock share is 24, and the full bay spends 16 of it, so the sale lands in another Crate. + Assert.Equal(8, StationServices.StockRoom(station)); 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(gun, others.SelectMany(bay => bay.Cargo.Keys)); Assert.Contains(ore, Pantry(station).Cargo.Keys); }