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 <noreply@anthropic.com>
This commit is contained in:
soul 2026-11-27 01:19:10 +01:00
commit 471f70b391
6 changed files with 86 additions and 9 deletions

View file

@ -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<EconomyFault>();
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<EconomyFault> 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<EconomyFault> Recipe(CultCache catalog)
{

View file

@ -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]

View file

@ -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<EconomyAuthor> _author;
public Lambda(Action<EconomyAuthor> 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<CompoundCommodityData>(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<EconomyBatch> { 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));
}
}

View file

@ -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<ItemData>(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<ItemData>(CommodityKey("mines"));

View file

@ -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<SimpleCommodityData> 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<ItemRole>());
return new CultRecordRef<SimpleCommodityData>(key);
}

View file

@ -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 <batch>... [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.";