Restore consolidates stack fragments; Reachable drops a retired input from a Produced lot

EquippedCargoBay.Consolidate pours stacks into earlier stacks they StacksWith, called once per bay after SpillOversize
(finding old-fragments-never-merge). ProvenanceLedger.Reachable no longer names a RetiredExtracted input that
RunSave.Commit drops (finding input-keeps-retired-lot).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
soul 2026-11-27 19:00:52 +01:00
commit b04828abd4
3 changed files with 47 additions and 5 deletions

View file

@ -2639,6 +2639,34 @@ public class EquippedCargoBay : EquippedItem
public int Count(CultRecordKey design) =>
ItemsOfType.TryGetValue(design, out var items) ? items.Sum(i => i is ICommodityStack stack ? stack.Quantity : 1) : 0;
// Pours each stack into an earlier stack (InteriorShape.Coordinates order) it StacksWith that has room, through
// Remove and TryStore, so Mass and ThermalMass conserve and the merge rule stays StacksWith's. Emptied stacks leave.
// Run once after a restore: a saved bay may hold fragments that were stored before the bay merged them.
public void Consolidate()
{
var stacks = new List<ItemInstance>();
foreach (var coordinate in Data.InteriorShape.Coordinates)
if (Occupancy[coordinate.x, coordinate.y] is ICommodityStack && !stacks.Contains(Occupancy[coordinate.x, coordinate.y]))
stacks.Add(Occupancy[coordinate.x, coordinate.y]);
for (var later = 1; later < stacks.Count; later++)
{
var from = stacks[later];
for (var earlier = 0; earlier < later && Cargo.ContainsKey(from); earlier++)
{
var into = stacks[earlier];
if (!Cargo.ContainsKey(into) || !((ICommodityStack) into).StacksWith(from)) continue;
var room = ItemManager.MaxStack(into) - ((ICommodityStack) into).Quantity;
if (room < 1) continue;
var origin = Cargo[from];
var part = Remove(from, Math.Min(room, ((ICommodityStack) from).Quantity));
// Whole or nothing: the part fits the room it was sized to, and a refusal puts it back where it was.
if (!TryStore(part, Cargo[into])) TryStore(part, origin);
}
}
}
// The part removed: null, with the bay as it was, when the item is not here or the quantity is below one unit. A
// stack gives up `quantity` units through its own Split (the stack itself, whole, when it holds no more); a unit
// leaves whole for any quantity of one or more.

View file

@ -158,7 +158,10 @@ public static class EntitySerializer
foreach (var (position, item) in pack.DockingBayContents[bayIndex])
RestoreStack(itemManager, entity.DockingBays[bayIndex], position, item, spill);
SpillOversize(itemManager, entity.CargoBays.Cast<EquippedCargoBay>().Concat(entity.DockingBays).ToList(), spill);
var bays = entity.CargoBays.Cast<EquippedCargoBay>().Concat(entity.DockingBays).ToList();
SpillOversize(itemManager, bays, spill);
// Fragments saved before the bay merged them, and the parts a spill placed, fold into the stacks that take them.
foreach (var bay in bays) bay.Consolidate();
entity.Temperature = pack.Temperature;

View file

@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using GameCult.Caching;
using MessagePack;
@ -35,6 +36,8 @@ public class ProvenanceLedger
// A new ledger holding only lots reachable from roots, closed over Produced.Facility and Produced.Inputs. Never
// mutates this: the live ledger keeps unreachable lots, because a floor pickup can still be collected after a save.
// An input whose lot is RetiredExtracted names a lot RunSave.Commit drops (no item roots it), so the reached copy of
// the Produced lot omits it rather than name a lot the saved ledger lacks (finding input-keeps-retired-lot).
public ProvenanceLedger Reachable(IEnumerable<int> roots)
{
var reached = new Dictionary<int, Lot>();
@ -44,14 +47,22 @@ public class ProvenanceLedger
var id = pending.Dequeue();
if (reached.ContainsKey(id)) continue;
var lot = this[id];
reached[id] = lot;
if (lot.Origin is Produced produced)
{
var inputs = produced.Inputs?.Where(input => !(this[input].Origin is RetiredExtracted)).ToArray();
if (inputs != null && inputs.Length != produced.Inputs.Length)
lot = new Lot
{
Design = lot.Design, Quality = lot.Quality, Roles = lot.Roles, Product = lot.Product, Id = lot.Id,
Origin = new Produced { Faction = produced.Faction, Station = produced.Station, Facility = produced.Facility, Inputs = inputs }
};
reached[id] = lot;
pending.Enqueue(produced.Facility);
if (produced.Inputs != null)
foreach (var input in produced.Inputs)
if (inputs != null)
foreach (var input in inputs)
pending.Enqueue(input);
}
else reached[id] = lot;
}
return new ProvenanceLedger { NextLot = NextLot, Lots = reached };
@ -69,7 +80,7 @@ public class Lot
[Key(3)] public List<RoleFill> Roles;
// The product the lot was made as: the one owner of which product names its units. Unset for unbranded lots
// (console give, presets, extracted), which brand their maker only.
// (console give, presets), which brand their maker only.
[Key(4)] public CultRecordRef<FactionProductData> Product;
// The quality a stat reads for one of the design's roles: the part's, capped by the lot's own final assembly