Simple commodities carry no lot and merge by design; Extract mints nothing; sell-ore selects by ore kind

Lot moves to CraftedItemInstance; ExtractedLot, its index and CargoGain.Lot die; Extracted becomes the empty load-only RetiredExtracted arm; FieldKinds.IsOre owns the raw-ore kind.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
soul 2026-11-27 16:47:52 +01:00
commit 18dd954f5e
12 changed files with 43 additions and 68 deletions

View file

@ -2495,7 +2495,7 @@ public class EquippedCargoBay : EquippedItem
// For simple commodities, search for existing item stacks to add to
foreach (var cargoItem in Cargo.Keys)
{
if (!item.Data.Key.Equals(cargoItem.Data.Key) || item.Lot != cargoItem.Lot) continue;
if (!item.Data.Key.Equals(cargoItem.Data.Key)) continue;
var cargoCommodity = (SimpleCommodity) cargoItem;
if (cargoCommodity.Quantity >= itemData.MaxStack) continue;
@ -2593,7 +2593,7 @@ public class EquippedCargoBay : EquippedItem
ItemsOfType[item.Data.Key] = new List<ItemInstance>();
ItemsOfType[item.Data.Key].Add(item);
}
else if (Occupancy[cargoCoord.x, cargoCoord.y] is SimpleCommodity cargoCommodity && cargoCommodity.Data.Key.Equals(item.Data.Key) && cargoCommodity.Lot == item.Lot)
else if (Occupancy[cargoCoord.x, cargoCoord.y] is SimpleCommodity cargoCommodity && cargoCommodity.Data.Key.Equals(item.Data.Key))
{
if (cargoCommodity.Quantity + item.Quantity <= itemData.MaxStack)
{
@ -2673,7 +2673,7 @@ public class EquippedCargoBay : EquippedItem
item.Quantity -= quantity;
Mass -= itemData.Mass * quantity;
ThermalMass -= itemData.Mass * itemData.SpecificHeat * quantity;
return new SimpleCommodity{Data = item.Data, Lot = item.Lot, Quantity = quantity, Rotation = item.Rotation};
return new SimpleCommodity{Data = item.Data, Quantity = quantity, Rotation = item.Rotation};
}
public void Remove(CraftedItemInstance item)

View file

@ -60,8 +60,8 @@ public static class EntitySerializer
&& itemManager.GetData(commodity).MaxStack >= 1)
{
var maxStack = itemManager.GetData(commodity).MaxStack;
var first = new SimpleCommodity { Data = commodity.Data, Lot = commodity.Lot, Rotation = commodity.Rotation, Quantity = maxStack };
var rest = new SimpleCommodity { Data = commodity.Data, Lot = commodity.Lot, Rotation = commodity.Rotation, Quantity = commodity.Quantity - maxStack };
var first = new SimpleCommodity { Data = commodity.Data, Rotation = commodity.Rotation, Quantity = maxStack };
var rest = new SimpleCommodity { Data = commodity.Data, Rotation = commodity.Rotation, Quantity = commodity.Quantity - maxStack };
if (!bay.TryStore(first, position)) rest.Quantity += first.Quantity;
spill.Add((bay, rest));
return;
@ -82,7 +82,7 @@ public static class EntitySerializer
var remaining = rest.Quantity;
while (remaining > 0)
{
var chunk = new SimpleCommodity { Data = rest.Data, Lot = rest.Lot, Rotation = rest.Rotation, Quantity = Math.Min(remaining, maxStack) };
var chunk = new SimpleCommodity { Data = rest.Data, Rotation = rest.Rotation, Quantity = Math.Min(remaining, maxStack) };
var placed = home.TryStore(chunk) || bays.Where(b => b != home).Any(b => b.TryStore(chunk));
// A refused store may still have topped up a stack; what is left in the chunk found no room.
remaining -= Math.Min(remaining, maxStack) - (placed ? 0 : chunk.Quantity);

View file

@ -1864,18 +1864,16 @@ public static class FireControl
var at = zone.ChunkPose(chunk.Field, chunk.Index).xy;
var velocity = zone.ChunkVelocity(chunk);
var zoneIndex = zone.Galaxy?.Zones == null ? -1 : Array.IndexOf(zone.Galaxy.Zones, zone.GalaxyZone);
var present = zone.Entities.Contains(shooter);
for (var i = 0; i < ores.Length; i++)
{
var key = ores[i].Commodity.Key;
var commodity = (SimpleCommodityData) catalog.Get(ores[i].Commodity);
var maxStack = Math.Max(1, commodity.MaxStack);
var lot = counts[i] > 0 ? shooter.ItemManager.ExtractedLot(zoneIndex, chunk.Field, commodity) : 0;
var adrift = 0;
for (var left = counts[i]; left > 0; left -= maxStack)
{
var stack = new SimpleCommodity { Data = new CultRecordRef<ItemData>(key), Lot = lot, Quantity = Math.Min(left, maxStack) };
var stack = new SimpleCommodity { Data = new CultRecordRef<ItemData>(key), Quantity = Math.Min(left, maxStack) };
// A partial store leaves the remainder in Quantity and returns false, so the next bay is offered the rest.
var stored = false;
if (present) foreach (var bay in shooter.CargoBays) if (stored = bay.TryStore(stack)) break;
@ -1888,7 +1886,7 @@ public static class FireControl
if (counts[i] > 0)
zone.CargoGained.OnNext(new CargoGain
{
Taker = shooter, Design = new CultRecordRef<ItemData>(key), Lot = lot, Stored = counts[i] - adrift, Adrift = adrift, Kind = GainKind.Mined
Taker = shooter, Design = new CultRecordRef<ItemData>(key), Stored = counts[i] - adrift, Adrift = adrift, Kind = GainKind.Mined
});
}
}

View file

@ -10,7 +10,6 @@ public struct CargoGain
{
public Entity Taker;
public CultRecordRef<ItemData> Design;
public int Lot;
public int Stored;
public int Adrift;
public GainKind Kind;

View file

@ -25,10 +25,6 @@ public abstract class ItemInstance
[JsonProperty("data"), Key(0)] public CultRecordRef<ItemData> Data;
[JsonProperty("rotation"), Key(1)] public ItemRotation Rotation;
// The lot this unit was minted or mined from: crafted units' design, provenance, workmanship and role fills all
// live there; a mined SimpleCommodity names its Extracted lot (zone, belt, ore). Lot 0 is no lot: bought, staged
// and loadout commodities, and every save from before the field reached SimpleCommodity.
[JsonProperty("lot"), Key(11)] public int Lot;
}
[Union(0, typeof(CompoundCommodity)),
@ -38,7 +34,10 @@ public abstract class ItemInstance
public abstract class CraftedItemInstance : ItemInstance
{
// Keys 2 (Quality), 9 (Ingredients) and 10 (Product) belonged to fields moved to Lot; do not reuse them.
// Key 11 (Lot) lives on ItemInstance.
// The lot a crafted unit was minted from: its design, provenance, workmanship and role fills all live there. A
// simple commodity has none (ruling simple-commodities-fungible).
[JsonProperty("lot"), Key(11)] public int Lot;
}
[MessagePackObject, JsonObject(MemberSerialization.OptIn)]
@ -47,6 +46,8 @@ public class CompoundCommodity : CraftedItemInstance { }
[MessagePackObject, JsonObject(MemberSerialization.OptIn)]
public class SimpleCommodity : ItemInstance
{
// Key 11 belonged to a lot before ruling simple-commodities-fungible and is never reused; a pre-ruling stack's
// slot 11 is skipped on read.
[JsonProperty("quantity"), Key(2)] public int Quantity;
}

View file

@ -26,10 +26,6 @@ public class ItemManager
public CultCache ItemData { get; }
public ProvenanceLedger Lots { get; }
// (zone index, belt, commodity) -> the Extracted lot minted for it. Derived from Lots at construction and on mint,
// never persisted: a reload rebuilds it from the ledger, so the same source finds the same lot.
private readonly Dictionary<(int, CultRecordKey, CultRecordKey), int> _extracted =
new Dictionary<(int, CultRecordKey, CultRecordKey), int>();
public GameplaySettings GameplaySettings { get; }
public ItemManager(uint seed, CultCache itemData, ProvenanceLedger lots, GameplaySettings settings, Action<string> logger)
@ -38,9 +34,6 @@ public class ItemManager
Random = new Random(seed);
ItemData = itemData;
Lots = lots;
foreach (var (id, lot) in lots.Lots)
if (lot.Origin is Extracted extracted)
_extracted[(extracted.Zone, extracted.Body.Key, extracted.Commodity.Key)] = id;
GameplaySettings = settings;
_logger = logger;
}
@ -61,7 +54,6 @@ public class ItemManager
Random = mark.Stream;
foreach (var id in Lots.Lots.Keys.Where(id => id >= mark.NextLot).ToList()) Lots.Lots.Remove(id);
Lots.NextLot = mark.NextLot;
foreach (var key in _extracted.Where(entry => entry.Value >= mark.NextLot).Select(entry => entry.Key).ToList()) _extracted.Remove(key);
}
public void Log(string s)
@ -99,9 +91,8 @@ public class ItemManager
};
}
// The one resolution path from an instance to the lot it was minted or mined from. Lot 0 throws: a bought or
// staged commodity has no lot, and the caller asks Lot != 0 first.
public Lot GetLot(ItemInstance item) => Lots[item.Lot];
// The one path from a crafted instance to its lot; a simple commodity has no lot.
public Lot GetLot(CraftedItemInstance item) => Lots[item.Lot];
// Returns stat when not equipped: a null-entity context with no heat, no power and no modifiers. It says so
// in its own name (UnequippedStatContext) rather than being a third quiet dialect of the arithmetic.
@ -208,24 +199,6 @@ public class ItemManager
return Lots.Add(lot);
}
// Mining Cut 5b: the lot of every unit mined from one commodity in one belt of one zone, found or minted. Unbranded
// (Product unset) and unworked (Quality 1, no roles). FireControl.Extract is the only caller.
public int ExtractedLot(int zone, CultRecordKey belt, SimpleCommodityData commodity)
{
var key = (zone, belt, ItemData.RefOf<ItemData>(commodity).Key);
if (_extracted.TryGetValue(key, out var found)) return found;
var id = Lots.Add(new Lot
{
Design = ItemData.RefOf<ItemData>(commodity),
Origin = new Extracted
{
Zone = zone, Body = new CultRecordRef<BodyData>(belt), Commodity = ItemData.RefOf(commodity)
},
Quality = 1
});
return _extracted[key] = id;
}
// The one writer of a crafted instance's Data: copied from the lot's design, never independently set.
public CraftedItemInstance CreateInstance(int lot)
{
@ -261,7 +234,7 @@ public class ItemManager
}
// Who made an item and what it is branded as. The maker derives from its lot's provenance and the product is the
// one its lot records; nothing re-derives it. Null maker (an unset faction, or an Extracted lot with none) means
// one its lot records; nothing re-derives it. Null maker (an unset faction) means
// no brand at all; a set maker whose lot records no product (or one the catalog no longer holds) still shows
// the maker, with no product name or flavour text.
public (Faction Maker, FactionProductData Product) Brand(CraftedItemInstance item)
@ -280,7 +253,7 @@ public class ItemManager
}
// What the player reads an item as: its lot's product name when it records one,
// otherwise the design's own name (unbranded, console-given and extracted lots, and commodities).
// otherwise the design's own name (unbranded and console-given lots, and commodities).
public string Title(ItemInstance item) =>
(item is CraftedItemInstance crafted ? Brand(crafted).Product?.Name : null) ?? GetData(item).Name;

View file

@ -109,11 +109,11 @@ public class RoleFill
[Key(2)] [CultElementId] public string Id = "";
}
// Who built a lot, where, and from what. Terminus mints Attributed placeholders; Produced and Extracted are the
// forward shape for backward generation, next scope.
// Who built a lot, where, and from what. Terminus mints Attributed placeholders; Produced is the forward shape for
// backward generation, next scope.
[Union(0, typeof(Attributed)),
Union(1, typeof(Produced)),
Union(2, typeof(Extracted))]
Union(2, typeof(RetiredExtracted))]
public abstract class Provenance { }
// The Terminus placeholder: only the faction is known. No other level of the crafting pyramid is generated yet.
@ -123,7 +123,8 @@ public class Attributed : Provenance
[Key(0)] public CultRecordRef<Faction> Faction;
}
// Built in place at a station, from a factory item's own lot and a set of input lots.
// Built in place at a station, from a factory item's own lot and a set of input lots. Inputs names the compound input
// lots; a simple input carries no lot and is not named per unit.
[MessagePackObject]
public class Produced : Provenance
{
@ -133,12 +134,8 @@ public class Produced : Provenance
[Key(3)] public int[] Inputs;
}
// A raw commodity pulled from a zone.
// Key 2 held a per-source lot on mined simple commodities until ruling simple-commodities-fungible; nothing constructs or
// reads it. It is declared only because CultUnionGuard refuses an undeclared arm, so a run ledger saved before the ruling
// still loads, and RunSave.Commit drops those lots because no item roots them. Never reuse key 2.
[MessagePackObject]
public class Extracted : Provenance
{
// Zone is the zone's index in galaxy.Zones (SavedGame's convention), -1 with no galaxy; Body is the belt.
[Key(0)] public int Zone;
[Key(1)] public CultRecordRef<SimpleCommodityData> Commodity;
[Key(2)] public CultRecordRef<BodyData> Body;
}
public class RetiredExtracted : Provenance { }

View file

@ -234,11 +234,11 @@ public static class RunSave
.ToArray();
game.Zones = keys.Select(key => new CultRecordRef<SavedZone>(key)).ToArray();
// Every crafted item roots, Lot 0 included: a crafted item with no lot is corruption and Reachable throws on it.
// A SimpleCommodity roots only when it names a lot; Lot 0 is bought, staged or loadout stock.
// A simple commodity has no lot.
var roots = zones
.SelectMany(zone => zone.Contents?.Entities ?? new List<EntityPack>())
.SelectMany(EntitySerializer.Items)
.Where(item => item != null && (item is CraftedItemInstance || item.Lot != 0))
.OfType<CraftedItemInstance>()
.Select(item => item.Lot);
cache.Commit(batch =>
{

View file

@ -114,10 +114,10 @@ public static class StationServices
return true;
}
// The mined stacks of the bays: simple commodities with a lot whose origin is Extracted. Ammunition and consumables are
// simple commodities too, and a bought or staged commodity has Lot 0, which GetLot cannot resolve.
// The ore stacks of the bays are the stacks of any design a field kind yields, mined, bought or looted alike (ruling
// simple-commodities-fungible); ammunition and other simple goods no belt yields are not ore.
private static ItemInstance[] OreStacks(ItemManager items, IEnumerable<EquippedCargoBay> bays) =>
bays.SelectMany(bay => bay.Cargo.Keys.OfType<SimpleCommodity>().Where(stack => stack.Lot != 0 && items.GetLot(stack).Origin is Extracted).Select(stack => (ItemInstance) stack)).ToArray();
bays.SelectMany(bay => bay.Cargo.Keys.OfType<SimpleCommodity>().Where(stack => FieldKinds.IsOre(items.ItemData, items.GetData(stack))).Select(stack => (ItemInstance) stack)).ToArray();
// What the station pays for every mined stack of the bays: the sum of SellPrice over the stacks TrySellOre sells.
public static int OreValue(ItemManager items, IEnumerable<EquippedCargoBay> bays) =>

View file

@ -450,7 +450,7 @@ public class Zone
FloatingItems.Remove(floating);
CargoGained.OnNext(new CargoGain
{
Taker = taker, Design = floating.Item.Data, Lot = floating.Item.Lot, Kind = GainKind.Collected,
Taker = taker, Design = floating.Item.Data, Kind = GainKind.Collected,
Stored = stored
});
return true;

View file

@ -292,6 +292,14 @@ public static class FieldKinds
.Where(yield => yield.Amount > 0f)
.ToArray();
}
// Whether the design is raw ore: some field kind yields it. Ore is what a belt can put in a hold; derived on every
// call, never stored. The one owner of the raw-ore kind.
public static bool IsOre(CultCache catalog, ItemData design)
{
var key = catalog.RefOf(design).Key;
return catalog.GetAll<FieldKindData>().Any(kind => kind.Yields.Any(yield => yield.Commodity.Key.Equals(key)));
}
}
// Ore geography (rulings provinces-within-regions, boss-zone-is-near-side, province-partition-by-size, province-types-dealt-per-region).

View file

@ -223,12 +223,11 @@ public static class EconomyLoop
}
}
// The simple commodities the hold carries that were not mined (a mined stack names an Extracted lot), summed per commodity: ammunition
// and consumables. The sampled stack carries the item the price is read from.
// The simple commodities the hold carries that are not ore, summed per commodity: ammunition and consumables. The sampled stack carries the item the price is read from.
private static Dictionary<SimpleCommodityData, (int Quantity, SimpleCommodity Sample)> Ammunition(ItemManager items, Entity player)
{
var stacks = player.CargoBays.SelectMany(bay => bay.Cargo.Keys.OfType<SimpleCommodity>())
.Where(stack => stack.Lot == 0 || !(items.GetLot(stack).Origin is Extracted));
.Where(stack => !FieldKinds.IsOre(items.ItemData, items.GetData(stack)));
return stacks.GroupBy(stack => items.GetData(stack)).ToDictionary(g => g.Key, g => (g.Sum(stack => stack.Quantity), g.First()));
}