The drain reads the crew: the station block prints what the quarters asked of the hold per sim hour and the spans their step went short in

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
soul 2026-11-29 01:17:42 +01:00
commit 5a4698b2cd
2 changed files with 59 additions and 5 deletions

View file

@ -61,6 +61,24 @@ public sealed class EconomyDrainTests
Assert.Equal(120, sample.Mined.Sum(m => m.Units));
}
// The crew's draw is read apart from the facilities' (finding crew-draw-unmeasured): each station's quarters are stepped through every
// span of the horizon beside its facilities, and the drain prints what they took per sim hour and the spans they went short in. The
// shipped stations hold no Water, Oxygen or Algae to start with, so the crew is short in every span and takes nothing.
[Fact]
public void TheDrainReadsTheCrewApartFromTheFacilities()
{
var sample = EconomyDrain.Run(Root, 1, 30, 2, Seed1.Value);
foreach (var station in sample.Stations)
{
var crew = Assert.Single(station.Crew);
Assert.Equal(("Crew Quarters", 20, 120, 7200.0), (crew.Name, crew.Occupants, crew.Spans, crew.Seconds));
Assert.Equal(new[] { "Algae", "Oxygen", "Water" }, crew.Taken.Keys.OrderBy(name => name, StringComparer.Ordinal));
Assert.All(crew.Taken.Values, taken => Assert.Equal(0, taken));
Assert.True(crew.ShortSpans > crew.Spans / 2, $"short in {crew.ShortSpans} of {crew.Spans} spans");
Assert.Contains("crew Crew Quarters (20 occupants): per sim hour took ", Printed(sample));
}
}
// A drain copy shares no object with the live station: EntitySerializer.Pack alone puts the live stacks in the pack, so a copy
// made from it would move the original's units when it sells or takes (Soul s1, copy-isolation-unpinned).
[Fact]

View file

@ -62,6 +62,18 @@ public sealed class OutputPile
public List<int> Hourly = new List<int>();
}
// What a station's crew quarters asked of the hold over the horizon, by commodity (occupants x rate x time), and the spans their step went
// short in: the crew's draw read apart from the facilities' (finding crew-draw-unmeasured). The hold is shared, so what was taken is
// the asked less what the short spans dropped.
public sealed class CrewDrain
{
public string Name;
public int Occupants;
public double Seconds;
public int Spans, ShortSpans;
public Dictionary<string, double> Asked = new Dictionary<string, double>();
}
public sealed class StationDrain
{
public string Role, Name, Kind;
@ -70,6 +82,7 @@ public sealed class StationDrain
public List<SortieSale> Sorties = new List<SortieSale>();
public bool Filled;
public List<FacilityDrain> Facilities = new List<FacilityDrain>();
public List<CrewDrain> Crew = new List<CrewDrain>();
public List<OreDrain> Ores = new List<OreDrain>();
public List<OutputPile> Outputs = new List<OutputPile>();
// Sim seconds into the horizon at the end of the first span any facility ended stalled Room, or null.
@ -242,6 +255,9 @@ public static class EconomyDrain
var total = facility.WindowSteps.Values.Sum();
output.WriteLine($" window stalls: {(total == 0 ? "no steps" : string.Join(", ", facility.WindowSteps.Select(kv => string.Format(inv, "{0} {1:0.#}%", kv.Key, 100.0 * kv.Value / total))))} | horizon ends {facility.HorizonEnd}");
}
foreach (var crew in station.Crew.Where(c => c.Spans > 0))
output.WriteLine(string.Format(inv, " crew {0} ({1} occupants): per sim hour asked {2} | short in {3} of {4} spans", crew.Name, crew.Occupants,
string.Join(", ", crew.Asked.Select(kv => string.Format(inv, "{0} {1:0.##}", kv.Key, kv.Value * 3600 / crew.Seconds))), crew.ShortSpans, crew.Spans));
output.WriteLine(station.WindowDisagreements.Count == 0
? " window by steps and by spans: units held, run progress and first Room time agree"
: $" window by steps and by spans: DISAGREE: {string.Join("; ", station.WindowDisagreements)}");
@ -369,6 +385,8 @@ public static class EconomyDrain
{
WindowCopy = EconomyDrain.Copy(Demo, Station);
HorizonCopy = EconomyDrain.Copy(Demo, Station);
foreach (var unit in Crews(Station))
Drain.Crew.Add(new CrewDrain { Name = Demo.Items.GetData(unit.EquippableItem).Name, Occupants = unit.Behaviors.Select(b => b.Data).OfType<HabitationData>().First().Capacity });
foreach (var (unit, facility) in Facilities(Station))
{
var design = Demo.Items.GetData(unit.EquippableItem);
@ -383,21 +401,39 @@ public static class EconomyDrain
public static EquippableItem[] Units(Entity entity) =>
entity.Equipment.Where(unit => unit.Behaviors.OfType<Facility>().Any()).Select(unit => unit.EquippableItem).ToArray();
// The habitation units of an entity, in equipment order: the order Drain.Crew is indexed in.
private static EquippedItem[] Crews(Entity entity) => entity.Equipment.Where(unit => unit.Behaviors.OfType<Habitation>().Any()).ToArray();
private static IEnumerable<(EquippedItem unit, Facility facility)> Facilities(Entity entity) =>
entity.Equipment.SelectMany(unit => unit.Behaviors.OfType<Facility>().Select(facility => (unit, facility)));
// The copy's facilities, each advanced through `seconds` of sim time in spans of RegionClock.Span by the off-screen step the
// game ships (RegionClock.AdvanceInstalled), in equipment order. Returns the Stall each ended in; each sees the sim time and
// the stalls after every span.
public Stall[] Spans(Entity copy, double seconds, Action<double, Stall[]> each = null)
// The copy's facilities and crew quarters, each advanced through `seconds` of sim time in spans of RegionClock.Span by the off-screen
// step the game ships (RegionClock.AdvanceInstalled), in equipment order. Returns the Stall each facility ended in; each sees the sim
// time and the stalls after every span. With a tally, each span adds what the crew asked of the hold (occupants x rate x span) and
// whether a quarters' step went short (Habitation.Shortfall), indexed as Drain.Crew.
public Stall[] Spans(Entity copy, double seconds, Action<double, Stall[]> each = null, IReadOnlyList<CrewDrain> tally = null)
{
var facilities = Facilities(copy).Select(f => f.facility).ToArray();
var crews = Crews(copy);
var last = new Stall[facilities.Length];
for (var at = 0.0; at < seconds - 1e-9;)
{
var span = Math.Min(RegionClock.Span, seconds - at);
RegionClock.AdvanceInstalled(copy, span);
for (var i = 0; i < facilities.Length; i++) last[i] = facilities[i].Stall;
for (var c = 0; tally != null && c < crews.Length; c++)
{
var habitation = crews[c].Behaviors.OfType<Habitation>().First();
var data = (HabitationData) habitation.Data;
tally[c].Spans++;
tally[c].Seconds += span;
if (habitation.Shortfall.Count > 0) tally[c].ShortSpans++;
foreach (var row in data.Table)
{
var name = Name(Demo.Cache, row.Commodity.Key.Value);
tally[c].Asked[name] = tally[c].Asked.GetValueOrDefault(name) + data.Capacity * row.PerOccupantHour * span / 3600;
}
}
at += span;
each?.Invoke(at, last);
}
@ -451,7 +487,7 @@ public static class EconomyDrain
for (var i = 0; i < oreKeys.Count; i++)
if (!Drain.Ores[i].EmptyAt.HasValue && hold.Held(oreKeys[i]) == 0) Drain.Ores[i].EmptyAt = at;
if (at >= nextHour - 1e-9) { Sample(); nextHour += 3600; }
});
}, Drain.Crew);
Drain.WantedEnd = StationWants.Of(HorizonCopy).Count;
var saturated = Full().Select(recipe => Name(catalog, recipe.Output.Key.Value)).ToList();
HashSet<CultRecordKey> Draws(EquippedItem unit) => Production.RunList(catalog, unit.EquippableItem.Data.Key).SelectMany(recipe => Production.Draws(catalog, recipe)).ToHashSet();