From 471f70b391d0b0eca1ea85a88bcf62f8a5f9b454 Mon Sep 17 00:00:00 2001 From: soul Date: Fri, 27 Nov 2026 01:19:10 +0100 Subject: [PATCH] Commodities default to one cell; an L-shape check refuses a stored commodity with no Shape SimpleCommodityData and CompoundCommodityData construct with a one-cell Shape. EconomyChecks.Shapes names every commodity whose Shape is null or empty, and economy author refuses a batch that introduces one. The Shape initializers the default makes redundant leave Munitions and Ore. Co-Authored-By: Claude Sonnet 5.5 --- Assets/Scripts/ServerShared/EconomyChecks.cs | 8 ++- Assets/Scripts/ServerShared/ItemData.cs | 5 ++ .../CommodityShapeTests.cs | 67 +++++++++++++++++++ tools/AetherDb/Economy/Munitions.cs | 8 +-- tools/AetherDb/Economy/OresAndProvinces.cs | 5 +- tools/AetherDb/EconomyCommand.cs | 2 +- 6 files changed, 86 insertions(+), 9 deletions(-) create mode 100644 tests/Aetheria.Shared.Tests/CommodityShapeTests.cs diff --git a/Assets/Scripts/ServerShared/EconomyChecks.cs b/Assets/Scripts/ServerShared/EconomyChecks.cs index 9f35d26f8..d3feecd90 100644 --- a/Assets/Scripts/ServerShared/EconomyChecks.cs +++ b/Assets/Scripts/ServerShared/EconomyChecks.cs @@ -56,13 +56,14 @@ 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"; + 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"; private const double Margin = 1e-9; public static EconomyReport Check(CultCache catalog, EconomyLoad load = null) { var faults = new List(); faults.AddRange(Recipe(catalog)); + faults.AddRange(Shapes(catalog)); faults.AddRange(Weights(catalog)); faults.AddRange(Lore(catalog)); faults.AddRange(SourceSink(catalog)); @@ -156,6 +157,11 @@ public static class EconomyChecks return faults; } + // Every commodity holds a Shape with at least one cell: a cargo bay reads it to store the good, and a commodity without one cannot be held. + public static List Shapes(CultCache catalog) => + Commodities(catalog).Where(item => item.Shape?.Coordinates == null || item.Shape.Coordinates.Length == 0) + .Select(item => new EconomyFault(LShape, catalog.RefOf(item).Key.Value, $"Commodity '{item.Name}' has no Shape cell: a cargo bay cannot hold it.")).ToList(); + // Every commodity and every offered design (but a Station or Turret hull) has a recipe, and each recipe is well formed. public static List Recipe(CultCache catalog) { diff --git a/Assets/Scripts/ServerShared/ItemData.cs b/Assets/Scripts/ServerShared/ItemData.cs index ab9c9dcfc..7fba0e2b9 100644 --- a/Assets/Scripts/ServerShared/ItemData.cs +++ b/Assets/Scripts/ServerShared/ItemData.cs @@ -327,6 +327,8 @@ public class SimpleCommodityData : ItemData [Inspectable, JsonProperty("maxStackSize"), Key(9)] public int MaxStack = 10; + // A commodity is one cell unless authored otherwise: crate size is a balance knob, and a cargo bay reads Shape to store the good. + public SimpleCommodityData() => Shape = new Shape(); } [JsonObject(MemberSerialization.OptIn)] @@ -357,6 +359,9 @@ public class ItemRole public class CompoundCommodityData : CraftedItemData { // MessagePack keys 10 and 11 belonged to the removed demand weights and classification; do not reuse them. + + // A commodity is one cell unless authored otherwise: crate size is a balance knob, and a cargo bay reads Shape to store the good. + public CompoundCommodityData() => Shape = new Shape(); } [CultDocument("aetheria.consumableitemdata", "1"), Inspectable, MessagePackObject] diff --git a/tests/Aetheria.Shared.Tests/CommodityShapeTests.cs b/tests/Aetheria.Shared.Tests/CommodityShapeTests.cs new file mode 100644 index 000000000..9792a5752 --- /dev/null +++ b/tests/Aetheria.Shared.Tests/CommodityShapeTests.cs @@ -0,0 +1,67 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using CultMath; +using GameCult.Caching; +using Xunit; + +// A commodity holds a Shape (aetheria-living-world, cut commodity-shapes): one cell by the type default, refused as an L-shape fault +// when a stored commodity has none, and held by a cargo bay in the shipped catalog. +public sealed class CommodityShapeTests : IDisposable +{ + private readonly string _root = Path.Combine(Path.GetTempPath(), "aetheria-commodityshape-" + Guid.NewGuid().ToString("N")); + + public CommodityShapeTests() => Directory.CreateDirectory(_root); + public void Dispose() => Directory.Delete(_root, true); + + private sealed class Lambda : EconomyBatch + { + private readonly Action _author; + public Lambda(Action author) => _author = author; + public override void Author(EconomyAuthor author) => _author(author); + } + + // Kills: the default dropped from either commodity type. + [Fact] + public void ANewCommodityIsOneCell() + { + Assert.Single(new SimpleCommodityData().Shape.Coordinates); + Assert.Single(new CompoundCommodityData().Shape.Coordinates); + } + + // Kills: a check that reads only simple commodities, or one that treats a restored shape as a fault. + [Fact] + public void AShapelessCommodityIsAnLShapeFault() + { + using var f = EconomyFixture.Healthy(); + Assert.Empty(EconomyChecks.Shapes(f.Cache)); + var key = new CultRecordKey(EconomyFixture.Key("plate")); + var plate = f.Cache.Get(key); + plate.Shape = null; + f.Cache.Upsert(typeof(CompoundCommodityData), plate, key); + var fault = Assert.Single(EconomyChecks.Shapes(f.Cache)); + Assert.Equal(EconomyChecks.LShape, fault.Invariant); + Assert.Equal(key.Value, fault.Record); + Assert.Single(EconomyChecks.Check(f.Cache).Faults, x => x.Invariant == EconomyChecks.LShape); + plate.Shape = new Shape(); + f.Cache.Upsert(typeof(CompoundCommodityData), plate, key); + Assert.Empty(EconomyChecks.Shapes(f.Cache)); + } + + // Kills: LShape missing from the refusing set in EconomyCommand. + [Fact] + public void ABatchThatWritesAShapelessCommodityIsRefused() + { + using var f = EconomyFixture.Healthy(); + f.Cache.FlushAsync().GetAwaiter().GetResult(); + var bytes = File.ReadAllBytes(f.CatalogPath); + var output = new StringWriter(); + var batch = new Lambda(a => a.Put(new SimpleCommodityData { Name = "void", Shape = null }, new CultRecordKey(EconomyFixture.Key("void")))); + var code = EconomyCommand.Author(f.CatalogPath, new List { batch }, true, output, TestCatalog.Registry(), _root); + Assert.Equal(1, code); + Assert.Contains("The batch is refused", output.ToString()); + Assert.Contains("L-shape commodity:void", output.ToString()); + Assert.Equal(bytes, File.ReadAllBytes(f.CatalogPath)); + } +} diff --git a/tools/AetherDb/Economy/Munitions.cs b/tools/AetherDb/Economy/Munitions.cs index c20165253..8610b522e 100644 --- a/tools/AetherDb/Economy/Munitions.cs +++ b/tools/AetherDb/Economy/Munitions.cs @@ -146,8 +146,8 @@ public sealed class Munitions : EconomyBatch // Missiles: the crate a launcher design reloads from. One crate is one magazine reload (the reload takes one unit of the weapon's // AmmoType), the same convention as Ammo for guns, whatever the magazine holds (2 to 288 rounds across the five designs). A crate is // a warhead and a motor; the guidance package (seeker and fin actuators) waits on the electronics chain, so the recipe does not - // yet draw it. Shape and stack match Ammo's, so a hold takes crates cell for cell. - var missiles = new SimpleCommodityData { Name = "Missiles", Shape = new Shape(), MaxStack = 5, Mass = 8f, Price = 1800, + // yet draw it. Stack matches Ammo's and the type default gives the crate one cell, so a hold takes crates cell for cell. + var missiles = new SimpleCommodityData { Name = "Missiles", MaxStack = 5, Mass = 8f, Price = 1800, Description = "Crated guided munitions: a high-explosive warhead mated to a rocket motor on a launch rail. The crate a launcher reloads from; the rounds are real, only the reactor energy of the launch is not." }; _a.Put(missiles, CommodityKey("missiles")); var missilesRef = new CultRecordRef(CommodityKey("missiles")); @@ -155,8 +155,8 @@ public sealed class Munitions : EconomyBatch "A missile crate is a warhead and a motor in one box, and the box is the arsenal's most counted object: Zhestokost ties each launcher crew's allowance to a serial range, so a cache of loose crates is either a deserter's or a quartermaster's crime.", new string[0], (warhead, 1), (motor, 1)); - // Mines: the crate the mine launcher reloads from, a warhead per mine laid. Shape and stack as Missiles. - var mines = new SimpleCommodityData { Name = "Mines", Shape = new Shape(), MaxStack = 5, Mass = 7f, Price = 1400, + // Mines: the crate the mine launcher reloads from, a warhead per mine laid. Stack as Missiles. + var mines = new SimpleCommodityData { Name = "Mines", MaxStack = 5, Mass = 7f, Price = 1400, Description = "Crated proximity mines: a high-explosive warhead in a steel drum with a fuzed trigger. The crate a mine launcher reloads from." }; _a.Put(mines, CommodityKey("mines")); var minesRef = new CultRecordRef(CommodityKey("mines")); diff --git a/tools/AetherDb/Economy/OresAndProvinces.cs b/tools/AetherDb/Economy/OresAndProvinces.cs index 814531390..9b867a10a 100644 --- a/tools/AetherDb/Economy/OresAndProvinces.cs +++ b/tools/AetherDb/Economy/OresAndProvinces.cs @@ -221,12 +221,11 @@ public sealed class OresAndProvinces : EconomyBatch private static CultRecordKey CommodityKey(string id) => new CultRecordKey("commodity:" + id); - // A new ore: a raw good with an extraction recipe at the mining station that fills its metal roles. Every ore gets a one-cell - // Shape: ItemData.Shape has no default and a cargo bay reads it to store the good. + // A new ore: a raw good with an extraction recipe at the mining station that fills its metal roles. Its Shape is the commodity type's one-cell default. private CultRecordRef Ore(string id, string name, string description, float mass, int price, int workforce, int perCycle, string lore, params string[] fills) { var key = CommodityKey(id); - _a.Put(new SimpleCommodityData { Name = name, Description = description, Mass = mass, Price = price, Shape = new Shape() }, key); + _a.Put(new SimpleCommodityData { Name = name, Description = description, Mass = mass, Price = price }, key); Recipe(key, "mining", 600, workforce, perCycle, lore, fills, new List()); return new CultRecordRef(key); } diff --git a/tools/AetherDb/EconomyCommand.cs b/tools/AetherDb/EconomyCommand.cs index 4bba3fddc..485886e0f 100644 --- a/tools/AetherDb/EconomyCommand.cs +++ b/tools/AetherDb/EconomyCommand.cs @@ -60,7 +60,7 @@ public static class EconomyCommand // The invariants whose faults the batches introduce refuse them. The rest (a missing sink, no consumer demand, a commodity // with no recipe yet) are completeness: reported, and settled by the batch that closes the chain. private static readonly string[] Shape = - { EconomyChecks.LRecipe, EconomyChecks.LLore, EconomyChecks.LProductive, EconomyChecks.LOneKindList, EconomyChecks.LWeights }; + { EconomyChecks.LRecipe, EconomyChecks.LLore, EconomyChecks.LProductive, EconomyChecks.LOneKindList, EconomyChecks.LWeights, EconomyChecks.LShape }; // A batch is compiled code under tools/AetherDb/Economy/ and runs with the operator's authority: reviewed code, never an input. private const string Usage = "economy report | author ... [apply] | " + EconomyPrices.Usage + " | balance\nbalance reads the chain and changes nothing. A batch is reviewed code: it runs on a trial copy and must write only through EconomyAuthor.";