diff --git a/Assets/Scripts/ServerShared/EconomyChecks.cs b/Assets/Scripts/ServerShared/EconomyChecks.cs index 9a265f9fd..3fe389321 100644 --- a/Assets/Scripts/ServerShared/EconomyChecks.cs +++ b/Assets/Scripts/ServerShared/EconomyChecks.cs @@ -56,7 +56,7 @@ public sealed class EconomyReport // The one owner of the catalog's economy invariants. Pure: draws nothing, reads no clock, writes nothing. One method per rule. public static class EconomyChecks { - public const string LRecipe = "L-recipe", LOneKindList = "L-one-kind-list", LLore = "L-lore", LSource = "L-source-sink", LProductive = "L-productive", LDemand = "L-consumer-demand", LWeights = "L-weights", LShape = "L-shape", LPrice = "L-price"; + public const string LRecipe = "L-recipe", LOneKindList = "L-one-kind-list", LLore = "L-lore", LSource = "L-source-sink", LProductive = "L-productive", LDemand = "L-consumer-demand", LWeights = "L-weights", LShape = "L-shape", LPrice = "L-price", LFacility = "L-facility"; private const double Margin = 1e-9; public static EconomyReport Check(CultCache catalog, EconomyLoad load = null) @@ -88,7 +88,7 @@ public static class EconomyChecks : new RecipeData { Output = recipe.Output, OutputCount = recipe.OutputCount, Cycle = recipe.Cycle, Workforce = recipe.Workforce, - StationKind = recipe.StationKind, LoreHook = recipe.LoreHook, + StationKind = recipe.StationKind, Facility = recipe.Facility, LoreHook = recipe.LoreHook, Inputs = (recipe.Inputs ?? new List()).Where(c => c != null).ToList(), Upkeep = (recipe.Upkeep ?? new List()).Where(c => c != null).ToList(), Fills = (recipe.Fills ?? new List()).Where(f => f != null).ToList(), @@ -203,6 +203,9 @@ public static class EconomyChecks else if (market.Rows.Contains(null)) Add(marketKey, "Rows holds a null entry."); } var kindsPlaced = new HashSet(catalog.GetAll().SelectMany(f => StationKinds.KindsOf(catalog, f)).Select(k => k.Key.Value), StringComparer.Ordinal); + var kindsInOrder = catalog.GetAll().Select(kind => (Key: catalog.RefOf(kind).Key.Value, Kind: kind)).OrderBy(pair => pair.Key, StringComparer.Ordinal).ToList(); + var facilitiesListed = new HashSet(kindsInOrder.SelectMany(pair => pair.Kind.Facilities ?? new List>()).Select(f => f.Key.Value), StringComparer.Ordinal); + void NoFacility(string record, string message) => faults.Add(new EconomyFault(LFacility, record, message)); foreach (var (key, stored) in StoredRecipes(catalog)) { var recipe = Sound(stored); @@ -230,6 +233,16 @@ public static class EconomyChecks if (kind == null) Add(key, "StationKind names no station kind this catalog holds."); else if (!kindsPlaced.Contains(recipe.StationKind.Key.Value)) faults.Add(new EconomyFault(LOneKindList, key, $"No faction places station kind '{kind.Name}', so nothing can run this recipe.")); + var facility = catalog.Get(recipe.Facility); + if (!recipe.Facility.IsSet()) NoFacility(key, "Facility is unset; a recipe names the facility gear that runs it."); + else if (facility == null) NoFacility(key, $"Facility '{recipe.Facility.Key.Value}' names no item this catalog holds."); + else if (!StationKinds.IsRecipeFacility(facility)) NoFacility(key, $"Facility '{facility.Name}' is not a recipe facility (gear of hardpoint type Facility holding a FacilityData)."); + else + { + if (!facilitiesListed.Contains(recipe.Facility.Key.Value)) NoFacility(key, $"No station kind lists facility '{facility.Name}' in its Facilities, so no station is fitted to run this recipe."); + if (kind != null && !(kind.Facilities ?? new List>()).Any(listed => listed.Key.Equals(recipe.Facility.Key))) + NoFacility(key, $"Station kind '{kind.Name}' does not list facility '{facility.Name}' in its Facilities; the recipe's kind and facility must agree."); + } var roles = Recipes.RolesOf(output); if (!Recipes.IsCommodity(output)) { @@ -245,6 +258,19 @@ public static class EconomyChecks if (Recipes.Fillers(catalog, role.Name).Length == 0) Add(key, $"Role '{role.Name}' has no commodity whose recipe fills it."); } } + foreach (var (kindKey, kind) in kindsInOrder) + for (var i = 0; i < (kind.Facilities?.Count ?? 0); i++) + { + var listed = kind.Facilities[i]; + var item = catalog.Get(listed); + if (!listed.IsSet()) NoFacility(kindKey, $"Station kind '{kind.Name}' Facilities[{i}] is unset."); + else if (item == null) NoFacility(kindKey, $"Station kind '{kind.Name}' Facilities[{i}] names no item this catalog holds."); + else if (!StationKinds.IsRecipeFacility(item)) NoFacility(kindKey, $"Station kind '{kind.Name}' Facilities[{i}] '{item.Name}' is not a recipe facility."); + } + foreach (var gear in catalog.GetAll().Select(gear => (Key: catalog.RefOf(gear).Key.Value, Gear: gear)).OrderBy(pair => pair.Key, StringComparer.Ordinal)) + if (gear.Gear.Behaviors != null && gear.Gear.Behaviors.Any(behavior => behavior is FacilityData) && gear.Gear.HardpointType != HardpointType.Facility) + NoFacility(gear.Key, $"'{gear.Gear.Name}' holds a FacilityData but is hardpoint type {gear.Gear.HardpointType}; a recipe facility mounts on a Facility row."); + foreach (var line in StationKinds.FacilityRoomFaults(catalog)) NoFacility("station-facilities", line); return faults; } diff --git a/Assets/Scripts/ServerShared/LoadoutGenerator.cs b/Assets/Scripts/ServerShared/LoadoutGenerator.cs index d4e2d5643..9018a11da 100644 --- a/Assets/Scripts/ServerShared/LoadoutGenerator.cs +++ b/Assets/Scripts/ServerShared/LoadoutGenerator.cs @@ -173,7 +173,7 @@ public class LoadoutGenerator } } - private static bool IsHeater(GearData item) => + internal static bool IsHeater(GearData item) => item.HardpointType == HardpointType.Tool && item.Behaviors.Any(b => b is ThermotoggleData { HighPass: false }) && item.Behaviors.Any(b => b is HeatData); @@ -372,6 +372,8 @@ public class LoadoutGenerator var previousProducts = new List<(FactionProductData product, GearData design)>(); foreach (var hardpoint in hullData.Hardpoints.OrderByDescending(h=>h.Shape.Coordinates.Length)) { + // A Facility row is filled by its kind's Facilities (follow-up station-buys-by-installed-gear); the random fill never does. + if (hardpoint.Type == HardpointType.Facility) continue; if (hardpoint.Type == HardpointType.ControlModule) { var (controllerProduct, controllerData) = RandomProduct(hardpoint, 2, diff --git a/Assets/Scripts/ServerShared/StationKinds.cs b/Assets/Scripts/ServerShared/StationKinds.cs index ffb6ffd51..02d5efc2c 100644 --- a/Assets/Scripts/ServerShared/StationKinds.cs +++ b/Assets/Scripts/ServerShared/StationKinds.cs @@ -103,6 +103,48 @@ public static class StationKinds return standIn != null && standIn.Kind.IsSet() && OfferedKinds(catalog).Contains(standIn.Kind.Key.Value) ? standIn.Kind : default; } + // A recipe facility: facility gear a station runs recipes on. The hardpoint type says where it mounts, FacilityData says it runs + // recipes. + public static bool IsRecipeFacility(ItemData item) => + item is GearData gear && gear.HardpointType == HardpointType.Facility && gear.Behaviors != null && gear.Behaviors.Any(behavior => behavior is FacilityData); + + // Every offered station hull takes the facilities of every kind it serves, one station at a time (a station is one kind, so the + // designs of one kind each take a distinct Facility row; two kinds may share a row). A hull of kind H serves H itself and every + // kind some faction places whose hull kind is H (HullKindFor). One line per design that finds no row, naming the hull, the kind and + // the design, ordered by hull name then kind key. Reads the catalog and nothing else. + public static string[] FacilityRoomFaults(CultCache catalog) + { + var served = new Dictionary>(StringComparer.Ordinal); + foreach (var faction in catalog.GetAll()) + foreach (var kind in KindsOf(catalog, faction)) + { + var hullKind = HullKindFor(catalog, faction, kind).Key.Value; + if (!served.TryGetValue(hullKind, out var kinds)) served[hullKind] = kinds = new HashSet(StringComparer.Ordinal); + kinds.Add(kind.Key.Value); + } + var faults = new List<(string Hull, string Kind, string Line)>(); + foreach (var hull in DrawableStationProducts(catalog).Select(entry => entry.hull).Distinct().OrderBy(hull => hull.Name, StringComparer.Ordinal)) + { + var own = hull.Kind.Key.Value; + var kinds = new HashSet(served.TryGetValue(own, out var found) ? found : new HashSet(), StringComparer.Ordinal) { own }; + foreach (var kindKey in kinds.OrderBy(key => key, StringComparer.Ordinal)) + { + var kind = catalog.Get(new CultRecordKey(kindKey)); + if (kind == null) continue; + var rows = hull.Hardpoints.Where(hardpoint => hardpoint.Type == HardpointType.Facility).ToList(); + var designs = kind.Facilities.Select(facility => catalog.Get(facility)).OfType() + .OrderByDescending(design => design.Shape.Coordinates.Length).ThenBy(design => design.Name, StringComparer.Ordinal); + foreach (var design in designs) + { + var row = rows.FirstOrDefault(hardpoint => hardpoint.Takes(design)); + if (row != null) rows.Remove(row); + else faults.Add((hull.Name, kindKey, $"Station hull '{hull.Name}' has no free Facility row that takes {design.Name}, a facility of station kind '{kind.Name}'.")); + } + } + } + return faults.OrderBy(fault => fault.Hull, StringComparer.Ordinal).ThenBy(fault => fault.Kind, StringComparer.Ordinal).Select(fault => fault.Line).ToArray(); + } + // What is wrong with a hull's station facts, or null: a Station hull names a kind the catalog holds, and no other hull names one // or carries a Facility row. // The one rule, applied to the catalog on Open and to each mod package on Compose. @@ -119,7 +161,7 @@ public static class StationKinds { if (hull.Kind.IsSet()) return $"Hull '{hull.Name}' is a {hull.HullType} and names a Kind; only a station hull has one."; - if (hull.Hardpoints.Any(hardpoint => hardpoint.Type == HardpointType.Facility)) + if (hull.Hardpoints != null && hull.Hardpoints.Any(hardpoint => hardpoint.Type == HardpointType.Facility)) return $"Hull '{hull.Name}' is a {hull.HullType} and has a Facility row; only a station hull takes facilities."; } return null; diff --git a/tests/Aetheria.Shared.Tests/EconomyBalanceTests.cs b/tests/Aetheria.Shared.Tests/EconomyBalanceTests.cs index fe5b03dd6..a90cdbd8f 100644 --- a/tests/Aetheria.Shared.Tests/EconomyBalanceTests.cs +++ b/tests/Aetheria.Shared.Tests/EconomyBalanceTests.cs @@ -155,7 +155,7 @@ public sealed class EconomyBalanceTests var first = f.Cache.Get(RecipeData.KeyOf(new CultRecordKey("commodity:bar"))); f.Cache.Upsert(typeof(RecipeData), new RecipeData { - Output = first.Output, OutputCount = 1, Cycle = 1, Workforce = 1, StationKind = first.StationKind, LoreHook = "a good", + Output = first.Output, OutputCount = 1, Cycle = 1, Workforce = 1, StationKind = first.StationKind, Facility = first.Facility, LoreHook = "a good", Inputs = new System.Collections.Generic.List { new CommodityCount { Commodity = EconomyFixture.Item("commodity:ore"), Count = cheapFirst ? 3 : 1 } } }, new CultRecordKey("recipe:commodity:bar-alt")); var report = EconomyBalance.Balance(f.Cache); diff --git a/tests/Aetheria.Shared.Tests/EconomyChecksTests.cs b/tests/Aetheria.Shared.Tests/EconomyChecksTests.cs index fba588e78..ae367846b 100644 --- a/tests/Aetheria.Shared.Tests/EconomyChecksTests.cs +++ b/tests/Aetheria.Shared.Tests/EconomyChecksTests.cs @@ -69,6 +69,26 @@ public sealed class EconomyChecksTests var ghost = f.Station("ghost", "Ghost"); f.EditRecipe(Ore, r => r.StationKind = ghost); }, "recipe:" + Ore, EconomyChecks.LOneKindList); + // L-facility: each clause fails its own case (ruling demand-consumed-inputs-processed-by-facilities). + const string Facility = EconomyChecks.LFacility; + yield return Case("an unset facility", f => f.EditRecipe(Ore, r => r.Facility = default), "recipe:" + Ore, Facility, "Facility is unset"); + yield return Case("a facility naming nothing", f => f.EditRecipe(Ore, r => r.Facility = EconomyFixture.Item("commodity:ghost")), "recipe:" + Ore, Facility, "names no item"); + yield return Case("a facility that is not facility gear", f => f.EditRecipe(Ore, r => r.Facility = EconomyFixture.Item("gear:laser")), "recipe:" + Ore, Facility, "not a recipe facility"); + yield return Case("a facility no kind lists", f => + { + // With no kind set the kind-mirror clause has nothing to say, so only the listing clause speaks. + var orphan = f.FacilityGear("orphan-works"); + f.EditRecipe(Ore, r => { r.Facility = orphan; r.StationKind = default; }); + }, "recipe:" + Ore, Facility, "No station kind lists"); + yield return Case("a kind that does not list its recipe's facility", f => + { + var forge = f.Station("forge", "Forge"); + f.EditItem(forge.Key.Value, kind => kind.Facilities = new List> { f.FacilityGear("forge-works") }); + f.EditRecipe(Ore, r => r.StationKind = forge); + }, "recipe:" + Ore, Facility, "Station kind 'Forge' does not list"); + yield return Case("a kind listing gear that is not a facility", f => f.EditItem("station-kind:mill", kind => kind.Facilities.Add(EconomyFixture.Item("gear:laser"))), "station-kind:mill", Facility, "Facilities[1]"); + yield return Case("facility gear on a Tool hardpoint", f => f.EditItem("gear:laser", gear => gear.Behaviors.Add(new FacilityData())), "gear:laser", Facility, "holds a FacilityData"); + yield return Case("a served kind whose hull has no Facility row", f => f.EditItem("hull:yard", hull => hull.Hardpoints.Clear()), "station-facilities", Facility, "Yard"); } [Theory] diff --git a/tests/Aetheria.Shared.Tests/EconomyCommandTests.cs b/tests/Aetheria.Shared.Tests/EconomyCommandTests.cs index e0f4a0eb5..ec371df8d 100644 --- a/tests/Aetheria.Shared.Tests/EconomyCommandTests.cs +++ b/tests/Aetheria.Shared.Tests/EconomyCommandTests.cs @@ -81,7 +81,7 @@ public sealed class EconomyCommandTests else a.Put(new CompoundCommodityData { Name = id }, key); a.Put(new RecipeData { - Output = EconomyFixture.Item(key.Value), StationKind = kind ?? f.Mill, LoreHook = lore, + Output = EconomyFixture.Item(key.Value), StationKind = kind ?? f.Mill, Facility = f.MillWorks, LoreHook = lore, Inputs = (inputs ?? Array.Empty<(string, int)>()).Select(i => new CommodityCount { Commodity = EconomyFixture.Item(EconomyFixture.Key(i.Id)), Count = i.Count }).ToList(), Fills = (fills ?? Array.Empty()).ToList() }, RecipeData.KeyOf(key)); @@ -611,7 +611,7 @@ public sealed class EconomyCommandTests { var key = new CultRecordKey(EconomyFixture.Key(id)); f.Cache.Upsert(typeof(SimpleCommodityData), new SimpleCommodityData { Name = id }, key); - f.Cache.Upsert(typeof(RecipeData), new RecipeData { Output = EconomyFixture.Item(key.Value), StationKind = f.Mill, LoreHook = "a good", Inputs = new List() }, RecipeData.KeyOf(key)); + f.Cache.Upsert(typeof(RecipeData), new RecipeData { Output = EconomyFixture.Item(key.Value), StationKind = f.Mill, Facility = f.MillWorks, LoreHook = "a good", Inputs = new List() }, RecipeData.KeyOf(key)); } [Fact] diff --git a/tests/Aetheria.Shared.Tests/EconomyFixture.cs b/tests/Aetheria.Shared.Tests/EconomyFixture.cs index 127a79efe..8cb77212d 100644 --- a/tests/Aetheria.Shared.Tests/EconomyFixture.cs +++ b/tests/Aetheria.Shared.Tests/EconomyFixture.cs @@ -13,6 +13,8 @@ public sealed class EconomyFixture : IDisposable public CultCache Cache { get; private set; } public CultRecordRef Maker { get; private set; } public CultRecordRef Mill { get; private set; } + // The facility design Mill's recipes name: facility gear no faction offers (NotOffered), 2x2, on a Facility row. + public CultRecordRef MillWorks { get; private set; } public EconomyFixture() { @@ -22,7 +24,11 @@ public sealed class EconomyFixture : IDisposable Cache.Upsert(new InputLayout { Rows = new InputLayoutRow[] { new InputLayoutRowSpacer { Height = 1 } } }); Maker = new CultRecordRef(Cache.Upsert(new Faction { Name = "Maker", ShortName = "MKR" }).Key); Mill = Station("mill", "Mill"); - Sell(Cache.Upsert(typeof(HullData), new HullData { Name = "Yard", HullType = HullType.Station, Kind = Mill, Shape = new Shape(), Price = 1 }, new CultRecordKey("hull:yard"))); + MillWorks = FacilityGear("mill-works"); + var mill = Cache.Get(Mill.Key); + mill.Facilities = new List> { MillWorks }; + Cache.Upsert(typeof(StationKindData), mill, Mill.Key); + Sell(Cache.Upsert(typeof(HullData), new HullData { Name = "Yard", HullType = HullType.Station, Kind = Mill, Shape = new Shape(), Price = 1, Hardpoints = { FacilityRow() } }, new CultRecordKey("hull:yard"))); } public void Dispose() @@ -42,6 +48,28 @@ public sealed class EconomyFixture : IDisposable public static CultRecordRef Item(string key) => new CultRecordRef(new CultRecordKey(key)); public static string Key(string id) => "commodity:" + id; + private static Shape Solid(int width, int height) + { + var shape = new Shape(width, height); + foreach (var cell in shape.AllCoordinates) shape[cell] = true; + return shape; + } + + // A 2x2 Facility row, which takes any 2x2 facility design. + public static HardpointData FacilityRow() => new HardpointData { Type = HardpointType.Facility, Position = new CultMath.int2(0, 0), Shape = Solid(2, 2), Transform = "Fa.1" }; + + // A recipe facility design: Facility gear, 2x2, sold by the maker NotOffered (a design with no product is an unsold design). + public CultRecordRef FacilityGear(string id) + { + var key = new CultRecordKey("gear:" + id); + Cache.Upsert(typeof(GearData), new GearData + { + Name = id, Hardpoint = HardpointType.Facility, Shape = Solid(2, 2), Price = 1, Behaviors = new List { new FacilityData() } + }, key); + Cache.Upsert(new FactionProductData { Name = key.Value, Design = new CultRecordRef(key), Manufacturer = Maker, NotOffered = true }); + return Item(key.Value); + } + public CultRecordRef Station(string id, string name) { var key = StationKindData.KeyOf(id); @@ -56,7 +84,7 @@ public sealed class EconomyFixture : IDisposable // the roles its recipe takes, any filler will do. public string Good(string id, (string Id, int Count)[] inputs = null, string[] fills = null, int outputCount = 1, float cycle = 1, int workforce = 0, (string Id, int Count)[] upkeep = null, string lore = "a good", (string Role, int Count)[] roles = null, CultRecordRef? kind = null, - (string Role, int Count)[] roleInputs = null) + (string Role, int Count)[] roleInputs = null, CultRecordRef? facility = null) { var key = new CultRecordKey(Key(id)); var roleList = (roles ?? Array.Empty<(string, int)>()).Select(r => new ItemRole { Name = r.Role, Count = r.Count }).ToList(); @@ -71,6 +99,7 @@ public sealed class EconomyFixture : IDisposable Inputs = (inputs ?? Array.Empty<(string, int)>()).Select(i => new CommodityCount { Commodity = Item(Key(i.Id)), Count = i.Count }).ToList(), Upkeep = (upkeep ?? Array.Empty<(string, int)>()).Select(i => new CommodityCount { Commodity = Item(Key(i.Id)), Count = i.Count }).ToList(), StationKind = kind ?? Mill, + Facility = facility ?? MillWorks, LoreHook = lore, Fills = (fills ?? Array.Empty()).ToList(), RoleInputs = (roleInputs ?? Array.Empty<(string, int)>()).Select(r => new ItemRole { Name = r.Role, Count = r.Count }).ToList() @@ -99,6 +128,7 @@ public sealed class EconomyFixture : IDisposable Name = id, HullType = type, Shape = new Shape(), Price = 1, Kind = type == HullType.Station ? Mill : default, Roles = roles.Select(r => new ItemRole { Name = r.Role, Count = r.Count }).ToList() }, key); + if (type == HullType.Station) EditItem(key.Value, hull => hull.Hardpoints.Add(FacilityRow())); Design(key, offered, recipe); return key.Value; } @@ -110,7 +140,7 @@ public sealed class EconomyFixture : IDisposable Name = key.Value, Design = new CultRecordRef(key), Manufacturer = Maker, NotOffered = !offered }); if (recipe) - Cache.Upsert(typeof(RecipeData), new RecipeData { Output = Item(key.Value), StationKind = Mill }, RecipeData.KeyOf(key)); + Cache.Upsert(typeof(RecipeData), new RecipeData { Output = Item(key.Value), StationKind = Mill, Facility = MillWorks }, RecipeData.KeyOf(key)); } public void EditRecipe(string outputKey, Action edit)