diff --git a/Assets/Scripts/ServerShared/EconomyChecks.cs b/Assets/Scripts/ServerShared/EconomyChecks.cs index 3c8121b72..9a63d1688 100644 --- a/Assets/Scripts/ServerShared/EconomyChecks.cs +++ b/Assets/Scripts/ServerShared/EconomyChecks.cs @@ -170,12 +170,21 @@ public static class EconomyChecks new HashSet(rows.Where(r => r.Buys).Select(r => r.Commodity.Key.Value), StringComparer.Ordinal)); } + // The commodities an offered design's weapon draws as ammunition. One reader, shared by the sink and the consumer-demand rules. + private static HashSet Drawn(CultCache catalog) => + new HashSet(catalog.GetAll().Where(p => LoadoutGenerator.IsOffered(catalog, p)) + .Select(p => catalog.Get(p.Design) as EquippableItemData).Where(design => design?.Behaviors != null) + .SelectMany(design => design.Behaviors.OfType()).Where(weapon => weapon.AmmoType.IsSet()) + .Select(weapon => weapon.AmmoType.Key.Value), StringComparer.Ordinal); + // Every commodity has a source (a grounded chain, or a gate that sells it) and a sink (a recipe uses it, a role some design - // names, or a gate that buys it). A consumer good's sink is judged once needs tables exist. + // names, a weapon some offered design carries draws it, or a gate that buys it). A consumer good's sink is judged once needs + // tables exist. public static List SourceSink(CultCache catalog) { var faults = new List(); var (sells, buys) = Gate(catalog); + var drawn = Drawn(catalog); var grounded = Depths(catalog, sells); var recipes = AllRecipes(catalog).ToList(); var used = new HashSet(recipes.SelectMany(p => p.Recipe.Inputs.Concat(p.Recipe.Upkeep)).Select(c => c.Commodity.Key.Value), StringComparer.Ordinal); @@ -189,8 +198,8 @@ public static class EconomyChecks if (!grounded.TryGetValue(outputKey, out var depth) || depth == null) faults.Add(new EconomyFault(LSource, key, $"'{output.Name}' has no source: no chain of recipes reaches raw goods and no gate sells it.")); if (Recipes.IsConsumerGood(catalog, recipe)) continue; - if (!used.Contains(outputKey) && !recipe.Fills.Any(rolesNamed.Contains) && !buys.Contains(outputKey)) - faults.Add(new EconomyFault(LSource, key, $"'{output.Name}' has no sink: no recipe uses it, no design names a role it fills, no gate buys it.")); + if (!used.Contains(outputKey) && !recipe.Fills.Any(rolesNamed.Contains) && !drawn.Contains(outputKey) && !buys.Contains(outputKey)) + faults.Add(new EconomyFault(LSource, key, $"'{output.Name}' has no sink: no recipe uses it, no design names a role it fills, no weapon draws it, no gate buys it.")); } return faults; } @@ -399,36 +408,45 @@ public static class EconomyChecks return order; } - // Every processed commodity two or three steps below a gear or hull (its role fillers are one step, the gear-specific final - // assemblies, exempt) fills a role some consumer good names, so the shared layer is shared. Raw goods and the gate do not count. + // A processed good owes a consumer use when it is shared (rulings consumer-demand-shared-only, draw-is-one-use). Its uses are + // the gear or hull roles, and the rounds some weapon draws, that the goods downstream of it (itself included) fill or are; a + // good used as upkeep is shared too. Shared means two or more such uses, or upkeep. A shared good must lead, through the + // chain, into a consumer good or a role one names. A good whose every use is one gear role is exempt like its final assembly. + // Derived from the recipe graph on every check; depth and distance play no part. Raw goods and the gate count for nothing. public static List ConsumerDemand(CultCache catalog) { var recipes = AllRecipes(catalog).Where(p => catalog.Get(p.Recipe.Output) != null).ToDictionary(p => p.Recipe.Output.Key.Value, p => p.Recipe, StringComparer.Ordinal); + var drawn = Drawn(catalog); + bool IsCommodityKey(string key) => recipes.TryGetValue(key, out var r) && Recipes.IsCommodity(catalog.Get(r.Output)); var consumerRoles = new HashSet(recipes.Values.Where(r => Recipes.IsConsumerGood(catalog, r)) .SelectMany(r => Recipes.RolesOf(catalog.Get(r.Output))).Select(role => role.Name), StringComparer.Ordinal); - HashSet Below(IEnumerable keys) + var gearRoles = new HashSet(recipes.Values.Where(r => !Recipes.IsCommodity(catalog.Get(r.Output)) && Recipes.IsRoleRecipe(catalog, r)) + .SelectMany(r => Recipes.RolesOf(catalog.Get(r.Output))).Select(role => role.Name), StringComparer.Ordinal); + var upkeep = new HashSet(recipes.Values.SelectMany(r => r.Upkeep).Select(c => c.Commodity.Key.Value), StringComparer.Ordinal); + var takenBy = recipes.Values.Where(r => IsCommodityKey(r.Output.Key.Value)) + .SelectMany(r => r.Inputs.Select(input => (Input: input.Commodity.Key.Value, Output: r.Output.Key.Value))) + .ToLookup(edge => edge.Input, edge => edge.Output, StringComparer.Ordinal); + var faults = new List(); + foreach (var key in recipes.Keys.OrderBy(k => k, StringComparer.Ordinal)) { - var next = new HashSet(StringComparer.Ordinal); - foreach (var key in keys) - { - if (!recipes.TryGetValue(key, out var recipe)) continue; - foreach (var input in recipe.Inputs) next.Add(input.Commodity.Key.Value); - foreach (var role in Recipes.RolesOf(catalog.Get(recipe.Output))) - foreach (var filler in Recipes.Fillers(catalog, role.Name)) next.Add(filler.Value); - } - return next; + var recipe = recipes[key]; + if (!IsCommodityKey(key) || Recipes.IsRaw(catalog, recipe) || Recipes.IsConsumerGood(catalog, recipe)) continue; + var reached = new HashSet(StringComparer.Ordinal) { key }; + var open = new Stack(reached); + while (open.Count > 0) + foreach (var next in takenBy[open.Pop()]) + if (reached.Add(next)) open.Push(next); + var members = reached.Select(k => recipes[k]).ToList(); + if (members.Any(r => Recipes.IsConsumerGood(catalog, r) || r.Fills.Any(consumerRoles.Contains))) continue; + var uses = members.SelectMany(r => r.Fills).Where(gearRoles.Contains).Distinct(StringComparer.Ordinal).OrderBy(role => role, StringComparer.Ordinal) + .Concat(reached.Where(drawn.Contains).OrderBy(k => k, StringComparer.Ordinal).Select(k => catalog.Get(recipes[k].Output).Name)).ToList(); + var asUpkeep = reached.Any(upkeep.Contains); + if (asUpkeep) uses.Add("upkeep"); + if (uses.Count < 2 && !asUpkeep) continue; + var list = uses.Count == 1 ? uses[0] : string.Join(", ", uses.Take(uses.Count - 1)) + " and " + uses[uses.Count - 1]; + faults.Add(new EconomyFault(LDemand, RecipeData.KeyOf(new CultRecordKey(key)).Value, + $"'{catalog.Get(recipe.Output).Name}' leads into {list} but no use leads into a consumer good.")); } - var deep = new HashSet(StringComparer.Ordinal); - foreach (var key in recipes.Where(p => !Recipes.IsCommodity(catalog.Get(p.Value.Output)) && Recipes.IsRoleRecipe(catalog, p.Value)).Select(p => p.Key)) - { - var distance2 = Below(Below(new[] { key })); - deep.UnionWith(distance2); - deep.UnionWith(Below(distance2)); - } - return deep.OrderBy(key => key, StringComparer.Ordinal) - .Where(key => recipes.TryGetValue(key, out var recipe) && Recipes.IsCommodity(catalog.Get(recipe.Output)) && !Recipes.IsRaw(catalog, recipe) && !recipe.Fills.Any(consumerRoles.Contains)) - .Select(key => new EconomyFault(LDemand, RecipeData.KeyOf(new CultRecordKey(key)).Value, - $"'{catalog.Get(recipes[key].Output).Name}' is a processed good below a gear or hull that fills no role a consumer good names.")) - .ToList(); + return faults; } } diff --git a/tests/Aetheria.Shared.Tests/EconomyChecksFurtherTests.cs b/tests/Aetheria.Shared.Tests/EconomyChecksFurtherTests.cs index 151b47b88..e5b029010 100644 --- a/tests/Aetheria.Shared.Tests/EconomyChecksFurtherTests.cs +++ b/tests/Aetheria.Shared.Tests/EconomyChecksFurtherTests.cs @@ -24,7 +24,9 @@ public sealed class EconomyChecksFurtherTests f.Good("ingot", inputs: new[] { ("slag", 1) }); f.Good("steel", inputs: new[] { ("ingot", 1) }); f.Good("barrel", inputs: new[] { ("steel", 1) }, fills: new[] { "Barrel" }); + f.Good("plate", inputs: new[] { ("steel", 1) }, fills: new[] { "Plate" }); f.Gear("cannon", new[] { ("Barrel", 1) }); + f.Hull("tug", HullType.Ship, new[] { ("Plate", 1) }); f.Gear("bare", new[] { ("Barrel", 1) }, recipe: false); f.Good("tar", lore: " "); f.Good("waste"); @@ -93,7 +95,9 @@ public sealed class EconomyChecksFurtherTests f.Good("ore"); f.Good("steel", inputs: new[] { ("ore", 1), ("ghost", 1) }); f.Good("barrel", inputs: new[] { ("steel", 1), ("ghost", 1) }, fills: new[] { "Barrel" }); + f.Good("plate", inputs: new[] { ("steel", 1) }, fills: new[] { "Plate" }); f.Gear("cannon", new[] { ("Barrel", 1) }); + f.Hull("tug", HullType.Ship, new[] { ("Plate", 1) }); var faults = EconomyChecks.ConsumerDemand(f.Cache); Assert.Equal(new[] { "recipe:commodity:steel" }, faults.Select(x => x.Record)); Assert.Contains("steel", faults[0].Message); diff --git a/tests/Aetheria.Shared.Tests/EconomyChecksTests.cs b/tests/Aetheria.Shared.Tests/EconomyChecksTests.cs index 9d9f92f22..4878d1f2a 100644 --- a/tests/Aetheria.Shared.Tests/EconomyChecksTests.cs +++ b/tests/Aetheria.Shared.Tests/EconomyChecksTests.cs @@ -304,47 +304,156 @@ public sealed class EconomyChecksTests Assert.Empty(report.Faults); } - private static EconomyFixture Cannon() + private static string[] DemandRecords(EconomyFixture f) => EconomyChecks.ConsumerDemand(f.Cache).Select(x => x.Record).ToArray(); + + // A gear whose weapon draws the given commodity as ammunition (nothing when draws is null). + private static void Rifle(EconomyFixture f, string id, string draws, bool offered = true) { - var f = new EconomyFixture(); - f.Good("ore"); - f.Good("slag", inputs: new[] { ("ore", 1) }); - f.Good("ingot", inputs: new[] { ("slag", 1) }); - f.Good("steel", inputs: new[] { ("ingot", 1) }); - f.Good("barrel", inputs: new[] { ("steel", 1), ("ore", 1) }, fills: new[] { "Barrel" }); - f.Gear("cannon", new[] { ("Barrel", 1) }); - return f; + var key = f.Gear(id, new[] { ("Frame", 1) }, offered); + f.EditItem(key, g => g.Behaviors.Add(new InstantWeaponData + { + AmmoType = draws == null ? default : new CultRecordRef(new CultRecordKey(EconomyFixture.Key(draws))) + })); } [Fact] - public void ConsumerDemandCoversTheSharedLayer() - { - using var f = Cannon(); - // barrel is distance 1 (exempt); steel is 2 and ingot 3 (faults); slag is 4 and ore is raw (neither is). - var faults = EconomyChecks.ConsumerDemand(f.Cache); - Assert.Equal(new[] { "recipe:commodity:ingot", "recipe:commodity:steel" }, faults.Select(x => x.Record)); - Assert.All(faults, x => Assert.Equal(EconomyChecks.LDemand, x.Invariant)); - - f.Good("pot", roles: new[] { ("Casing", 1) }); - f.EditRecipe("commodity:steel", r => r.Fills.Add("Casing")); - Assert.Equal(new[] { "recipe:commodity:ingot" }, EconomyChecks.ConsumerDemand(f.Cache).Select(x => x.Record)); - f.EditRecipe("commodity:ingot", r => r.Fills.Add("Casing")); - Assert.Empty(EconomyChecks.ConsumerDemand(f.Cache)); - - f.Gate(("commodity:slag", true, false), ("commodity:steel", true, false)); - f.EditRecipe("commodity:steel", r => r.Fills.Clear()); - Assert.Equal(new[] { "recipe:commodity:steel" }, EconomyChecks.ConsumerDemand(f.Cache).Select(x => x.Record)); - } - - [Fact] - public void ConsumerDemandReadsHullsAsWellAsGear() + public void ConsumerDemandExemptsAGoodWhoseUsesLeadIntoOneGearRole() { using var f = new EconomyFixture(); f.Good("ore"); f.Good("ingot", inputs: new[] { ("ore", 1) }); + f.Good("steel", inputs: new[] { ("ingot", 1) }); + f.Good("barrel", inputs: new[] { ("steel", 1) }, fills: new[] { "Barrel" }); + f.Gear("cannon", new[] { ("Barrel", 1) }); + f.Gear("mortar", new[] { ("Barrel", 2) }); + Assert.Empty(EconomyChecks.ConsumerDemand(f.Cache)); + } + + [Fact] + public void ConsumerDemandCoversAGoodThatLeadsIntoTwoGearOrHullRoles() + { + using var f = new EconomyFixture(); + f.Good("ore"); + f.Good("slag", inputs: new[] { ("ore", 1) }); + f.Good("ingot", inputs: new[] { ("slag", 1) }); + f.Good("barrel", inputs: new[] { ("ingot", 1) }, fills: new[] { "Barrel" }); f.Good("plate", inputs: new[] { ("ingot", 1) }, fills: new[] { "Plate" }); + f.Gear("cannon", new[] { ("Barrel", 1) }); f.Hull("tug", HullType.Ship, new[] { ("Plate", 1) }); - Assert.Equal(new[] { "recipe:commodity:ingot" }, EconomyChecks.ConsumerDemand(f.Cache).Select(x => x.Record)); + var faults = EconomyChecks.ConsumerDemand(f.Cache); + Assert.Equal(new[] { "recipe:commodity:ingot", "recipe:commodity:slag" }, faults.Select(x => x.Record)); + Assert.All(faults, x => Assert.Equal(EconomyChecks.LDemand, x.Invariant)); + Assert.Contains("Barrel and Plate", faults[0].Message); + + // A consumer good taking a role that a good downstream of ingot fills clears the chain behind it. + f.Good("pot", roles: new[] { ("Casing", 1) }); + f.EditRecipe("commodity:barrel", r => r.Fills.Add("Casing")); + Assert.Empty(EconomyChecks.ConsumerDemand(f.Cache)); + } + + [Fact] + public void ConsumerDemandIgnoresTheStepWindow() + { + using var near = new EconomyFixture(); + near.Good("ore"); + near.Good("ingot", inputs: new[] { ("ore", 1) }, fills: new[] { "Barrel", "Plate" }); + near.Gear("cannon", new[] { ("Barrel", 1) }); + near.Hull("tug", HullType.Ship, new[] { ("Plate", 1) }); + Assert.Equal(new[] { "recipe:commodity:ingot" }, DemandRecords(near)); + + using var far = new EconomyFixture(); + far.Good("ore"); + far.Good("c1", inputs: new[] { ("ore", 1) }); + for (var i = 2; i <= 5; i++) far.Good("c" + i, inputs: new[] { ("c" + (i - 1), 1) }); + far.Good("c6", inputs: new[] { ("c5", 1) }, fills: new[] { "Barrel", "Plate" }); + far.Gear("cannon", new[] { ("Barrel", 1) }); + far.Hull("tug", HullType.Ship, new[] { ("Plate", 1) }); + Assert.Equal(new[] { "c1", "c2", "c3", "c4", "c5", "c6" }.Select(id => "recipe:commodity:" + id), DemandRecords(far)); + } + + [Fact] + public void ConsumerDemandCoversAGoodThatLeadsIntoUpkeep() + { + using var f = new EconomyFixture(); + f.Good("ore"); + f.Good("coke", inputs: new[] { ("ore", 1) }); + f.Good("plate", inputs: new[] { ("ore", 1) }, fills: new[] { "Plate" }, upkeep: new[] { ("coke", 1) }); + f.Hull("tug", HullType.Ship, new[] { ("Plate", 1) }); + var fault = Assert.Single(EconomyChecks.ConsumerDemand(f.Cache)); + Assert.Equal("recipe:commodity:coke", fault.Record); + Assert.Contains("upkeep", fault.Message); + + f.Good("pot", roles: new[] { ("Casing", 1) }); + f.EditRecipe("commodity:coke", r => r.Fills.Add("Casing")); + Assert.Empty(EconomyChecks.ConsumerDemand(f.Cache)); + } + + [Fact] + public void ConsumerDemandCountsADrawnRoundAsOneUse() + { + using var f = new EconomyFixture(); + f.Good("ore"); + f.Good("casing", inputs: new[] { ("ore", 1) }); + f.Good("round", inputs: new[] { ("casing", 1) }); + Rifle(f, "rifle", "round"); + Assert.Empty(EconomyChecks.ConsumerDemand(f.Cache)); + + // Acid also feeds a warhead role: two uses, so it is shared; the casing still feeds only the round. + f.Good("acid", inputs: new[] { ("ore", 1) }); + f.Good("warhead", inputs: new[] { ("acid", 1) }, fills: new[] { "Warhead" }); + f.Gear("mine", new[] { ("Warhead", 1) }); + f.EditRecipe("commodity:round", r => r.Inputs.Add(new CommodityCount { Commodity = EconomyFixture.Item(EconomyFixture.Key("acid")), Count = 1 })); + var fault = Assert.Single(EconomyChecks.ConsumerDemand(f.Cache)); + Assert.Equal("recipe:commodity:acid", fault.Record); + Assert.Contains("Warhead and round", fault.Message); + + // Without the weapon nothing is drawn, and acid has one use again. + f.EditItem("gear:rifle", g => g.Behaviors.Clear()); + Assert.Empty(EconomyChecks.ConsumerDemand(f.Cache)); + } + + [Fact] + public void ConsumerDemandTreatsTheGateAsNoUse() + { + using var f = new EconomyFixture(); + f.Good("ore"); + f.Good("ingot", inputs: new[] { ("ore", 1) }, fills: new[] { "Barrel", "Plate" }); + f.Good("slug", inputs: new[] { ("ore", 1) }, fills: new[] { "Slug" }); + f.Gear("cannon", new[] { ("Barrel", 1) }); + f.Gear("sling", new[] { ("Slug", 1) }); + f.Hull("tug", HullType.Ship, new[] { ("Plate", 1) }); + f.Gate(("commodity:ingot", true, true), ("commodity:slug", true, true)); + Assert.Equal(new[] { "recipe:commodity:ingot" }, DemandRecords(f)); + } + + [Fact] + public void SourceSinkCountsAWeaponsDrawAsASink() + { + static EconomyFault NoSink(EconomyFixture f) => + EconomyChecks.SourceSink(f.Cache).FirstOrDefault(x => x.Record == "recipe:commodity:round" && x.Message.Contains("no sink")); + static EconomyFixture Catalog(string draws, bool offered) + { + var f = new EconomyFixture(); + f.Good("ore"); + f.Good("round", inputs: new[] { ("ore", 1) }); + Rifle(f, "rifle", draws, offered); + return f; + } + using (var drawn = Catalog("round", true)) Assert.Null(NoSink(drawn)); + using (var unoffered = Catalog("round", false)) Assert.Contains("no weapon draws it", NoSink(unoffered).Message); + using (var unset = Catalog(null, true)) Assert.NotNull(NoSink(unset)); + } + + [Fact] + public void ProductiveMembersAreSortedWhileTheMessageKeepsCycleOrder() + { + using var f = new EconomyFixture(); + f.Good("loopa", inputs: new[] { ("loopc", 1) }); + f.Good("loopc", inputs: new[] { ("loopb", 1) }); + f.Good("loopb", inputs: new[] { ("loopa", 2) }); + var fault = Assert.Single(EconomyChecks.Productive(f.Cache)); + Assert.Equal(new[] { "commodity:loopa", "commodity:loopb", "commodity:loopc" }, fault.Members); + Assert.Contains("commodity:loopa -> commodity:loopc -> commodity:loopb", fault.Message); } [Fact]