From b04828abd4936fff9e24e8189094e51f13d12a9f Mon Sep 17 00:00:00 2001 From: soul Date: Fri, 27 Nov 2026 19:00:52 +0100 Subject: [PATCH] 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 --- Assets/Scripts/ServerShared/Entity.cs | 28 +++++++++++++++++++ .../Scripts/ServerShared/EntitySerializer.cs | 5 +++- Assets/Scripts/ServerShared/Provenance.cs | 19 ++++++++++--- 3 files changed, 47 insertions(+), 5 deletions(-) diff --git a/Assets/Scripts/ServerShared/Entity.cs b/Assets/Scripts/ServerShared/Entity.cs index 6584edc3b..7f9e9c35c 100644 --- a/Assets/Scripts/ServerShared/Entity.cs +++ b/Assets/Scripts/ServerShared/Entity.cs @@ -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(); + 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. diff --git a/Assets/Scripts/ServerShared/EntitySerializer.cs b/Assets/Scripts/ServerShared/EntitySerializer.cs index 5dcae2462..b1d5d7c35 100644 --- a/Assets/Scripts/ServerShared/EntitySerializer.cs +++ b/Assets/Scripts/ServerShared/EntitySerializer.cs @@ -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().Concat(entity.DockingBays).ToList(), spill); + var bays = entity.CargoBays.Cast().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; diff --git a/Assets/Scripts/ServerShared/Provenance.cs b/Assets/Scripts/ServerShared/Provenance.cs index e45a05ac1..be28ef2e6 100644 --- a/Assets/Scripts/ServerShared/Provenance.cs +++ b/Assets/Scripts/ServerShared/Provenance.cs @@ -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 roots) { var reached = new Dictionary(); @@ -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 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 Product; // The quality a stat reads for one of the design's roles: the part's, capped by the lot's own final assembly