diff --git a/Assets/Scripts/ServerShared/Production.cs b/Assets/Scripts/ServerShared/Production.cs index 189f5ceb2..b1c8b6b1c 100644 --- a/Assets/Scripts/ServerShared/Production.cs +++ b/Assets/Scripts/ServerShared/Production.cs @@ -25,6 +25,8 @@ public interface IProductionHold int Held(CultRecordKey design); bool TryTake(IReadOnlyList<(CultRecordKey design, int units)> units); bool TryPut(CultRecordKey design, int units); + // The units of the design a put may still store; int.MaxValue when the hold sets no bound. + int Room(CultRecordKey design); } // The one rule for a facility cycle: which recipe starts, which units it takes, how far it has run, what it stores. On screen its @@ -110,6 +112,7 @@ public static class Production if (seconds <= 0) return (run, Stall.None); var cursor = run == null ? -1 : IndexOf(runList, run.Recipe.Key); RecipeData start = null; + var skippedForRoom = false; for (var step = 1; step <= runList.Count && start == null; step++) { // The list may be older than the catalog: a candidate starts only as the catalog now holds it and runs it, so a @@ -117,9 +120,11 @@ public static class Production var candidate = Live(catalog, new CultRecordRef(RecipeData.KeyOf(runList[(cursor + step + runList.Count) % runList.Count].Output.Key))); if (candidate == null) continue; var units = Take(catalog, candidate, hold); - if (units != null && hold.TryTake(units)) start = candidate; + if (units == null) continue; + if (hold.Room(candidate.Output.Key) < candidate.OutputCount) { skippedForRoom = true; continue; } + if (hold.TryTake(units)) start = candidate; } - if (start == null) return (run, Stall.Inputs); + if (start == null) return (run, skippedForRoom ? Stall.Room : Stall.Inputs); run = new FacilityRun { Recipe = new CultRecordRef(RecipeData.KeyOf(start.Output.Key)), Progress = 0 }; } @@ -190,6 +195,13 @@ public sealed class EntityHold : IProductionHold return true; } + // The units the output bays may still take of the design: the station's allocation less what they hold, unbounded without one. + public int Room(CultRecordKey design) + { + var allocation = StationWants.Allocation(_entity, design); + return allocation == null ? int.MaxValue : Math.Max(0, allocation.Value - StationWants.OutputBays(_entity, design).Sum(bay => bay.Count(design))); + } + public bool TryPut(CultRecordKey design, int units) { var data = _entity.ItemManager.ItemData.Get(design); @@ -198,7 +210,7 @@ public sealed class EntityHold : IProductionHold { var chunk = Math.Min(left, data.MaxStack); var part = new SimpleCommodity { Data = new CultRecordRef(design), Quantity = chunk }; - if (!_entity.CargoBays.Any(bay => bay.TryStore(part))) + if (!StationWants.OutputBays(_entity, design).Any(bay => bay.TryStore(part))) { if (stored > 0) TryTake(new[] { (design, stored) }); return false; diff --git a/Assets/Scripts/ServerShared/StationWants.cs b/Assets/Scripts/ServerShared/StationWants.cs index 68eab133e..521ab061f 100644 --- a/Assets/Scripts/ServerShared/StationWants.cs +++ b/Assets/Scripts/ServerShared/StationWants.cs @@ -8,6 +8,13 @@ using Newtonsoft.Json; // recipe facilities draw, and, when it carries a silo, the goods of the haul orders that start at its orbit // (ruling transit-depots-buy-for-onward-haul). Derived on every call from the station's gear and the catalog, stored nowhere. // StationServices.TrySellPart is gated by it and limited by the bays' room. +// +// Where goods land and how much of a product a station holds (cut facility-output-room). A station with a silo keeps what its +// facilities make there, and each product holds an even share of the silo: Allocation, floor(silo cells / products) x the +// product's MaxStack. A recipe starts only when its product has room (CanStart), and a station draws an ore only while some recipe +// that takes it can start, so a full product stops its ores being bought while the station keeps running its other recipes. Prior +// art: X4's per-ware storage allocation (a full ware stops that production, the station keeps running the rest) and Factorio's +// per-product output slot. A station with no silo has no bound: outputs land in any bay and Room happens at the put. public static class StationWants { public static IReadOnlyCollection Of(Entity station) @@ -17,27 +24,61 @@ public static class StationWants return wants; } - // The bays a bought or delivered good may land in: every bay when an installed recipe facility draws it, only the silo bays - // when the station wants it only through a haul order (a transit depot holds goods in its silo, never in its stock bay). The - // one owner of where a good lands; none when the station does not want it. + // The bays a bought or delivered good may land in: the non-silo bays when an installed recipe facility draws it (a drawn good + // is stock, never silo content), the silo bays when the station wants it through a haul order (a transit depot holds goods in + // its silo, never in its stock bay). The one owner of where a good lands; none when the station does not want it. public static IEnumerable Bays(Entity station, CultRecordKey good) { - if (Drawn(station).Contains(good)) return station.CargoBays; - return Ordered(station).Contains(good) ? station.CargoBays.Where(bay => bay.Data is SiloData) : Enumerable.Empty(); + var bays = Enumerable.Empty(); + if (Drawn(station).Contains(good)) bays = bays.Concat(station.CargoBays.Where(bay => !(bay.Data is SiloData))); + if (Ordered(station).Contains(good)) bays = bays.Concat(station.CargoBays.Where(bay => bay.Data is SiloData)); + return bays; + } + + // The bays a facility's output may land in: the silo bays when the station has one, else every bay. The one owner of where + // a facility output lands. + public static IEnumerable OutputBays(Entity station, CultRecordKey good) => + station.CargoBays.Any(bay => bay.Data is SiloData) ? station.CargoBays.Where(bay => bay.Data is SiloData) : station.CargoBays; + + // The units of a product the station may hold in its output bays: the silo's cells divided evenly among the distinct outputs of + // its recipe facilities, each share worth that many full stacks of the product. Null (no bound) without a silo or a recipe + // facility. Derived on every call, stored nowhere. + public static int? Allocation(Entity station, CultRecordKey product) + { + var silos = station.CargoBays.Where(bay => bay.Data is SiloData).ToArray(); + if (silos.Length == 0) return null; + var products = FacilityRecipes(station).Select(recipe => recipe.Output.Key).Distinct().Count(); + if (products == 0) return null; + var cells = silos.Sum(bay => bay.Data.InteriorShape.Coordinates.Length); + var stack = station.ItemManager.ItemData.Get(product)?.MaxStack ?? 0; + return cells / products * stack; + } + + // Whether the recipe's product has room for one more cycle's output. + public static bool CanStart(Entity station, RecipeData recipe) + { + var allocation = Allocation(station, recipe.Output.Key); + return allocation == null || OutputBays(station, recipe.Output.Key).Sum(bay => bay.Count(recipe.Output.Key)) + recipe.OutputCount <= allocation; + } + + private static IEnumerable FacilityRecipes(Entity station) + { + var catalog = station.ItemManager.ItemData; + foreach (var equipped in station.Equipment) + { + var design = station.ItemManager.GetData(equipped.EquippableItem); + if (!StationKinds.IsRecipeFacility(design)) continue; + foreach (var recipe in Production.RunList(catalog, catalog.RefOf(design).Key)) yield return recipe; + } } private static HashSet Drawn(Entity station) { var catalog = station.ItemManager.ItemData; var wants = new HashSet(); - foreach (var equipped in station.Equipment) - { - var design = station.ItemManager.GetData(equipped.EquippableItem); - if (!StationKinds.IsRecipeFacility(design)) continue; - foreach (var recipe in Production.RunList(catalog, catalog.RefOf(design).Key)) - foreach (var draw in Production.Draws(catalog, recipe)) - wants.Add(draw); - } + foreach (var recipe in FacilityRecipes(station).Where(recipe => CanStart(station, recipe))) + foreach (var draw in Production.Draws(catalog, recipe)) + wants.Add(draw); return wants; } diff --git a/tests/Aetheria.Shared.Tests/StationWantsTests.cs b/tests/Aetheria.Shared.Tests/StationWantsTests.cs index b07210b8d..6810159b4 100644 --- a/tests/Aetheria.Shared.Tests/StationWantsTests.cs +++ b/tests/Aetheria.Shared.Tests/StationWantsTests.cs @@ -22,6 +22,7 @@ public sealed partial class RunStartTests public int Held(CultRecordKey design) => _without.Contains(design) ? 0 : 1000; public bool TryTake(IReadOnlyList<(CultRecordKey design, int units)> units) => true; public bool TryPut(CultRecordKey design, int units) => true; + public int Room(CultRecordKey design) => int.MaxValue; } private IEnumerable ShippedFacilityDesigns() =>