diff --git a/tests/Aetheria.Shared.Tests/EconomyDrainTests.cs b/tests/Aetheria.Shared.Tests/EconomyDrainTests.cs index 32f184fd1..7ce39343d 100644 --- a/tests/Aetheria.Shared.Tests/EconomyDrainTests.cs +++ b/tests/Aetheria.Shared.Tests/EconomyDrainTests.cs @@ -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] diff --git a/tools/AetherDb/EconomyDrain.cs b/tools/AetherDb/EconomyDrain.cs index f4082171e..e21d16f85 100644 --- a/tools/AetherDb/EconomyDrain.cs +++ b/tools/AetherDb/EconomyDrain.cs @@ -62,6 +62,18 @@ public sealed class OutputPile public List Hourly = new List(); } +// 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 Asked = new Dictionary(); +} + public sealed class StationDrain { public string Role, Name, Kind; @@ -70,6 +82,7 @@ public sealed class StationDrain public List Sorties = new List(); public bool Filled; public List Facilities = new List(); + public List Crew = new List(); public List Ores = new List(); public List Outputs = new List(); // 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().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().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().Any()).ToArray(); + private static IEnumerable<(EquippedItem unit, Facility facility)> Facilities(Entity entity) => entity.Equipment.SelectMany(unit => unit.Behaviors.OfType().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 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 each = null, IReadOnlyList 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().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 Draws(EquippedItem unit) => Production.RunList(catalog, unit.EquippableItem.Data.Key).SelectMany(recipe => Production.Draws(catalog, recipe)).ToHashSet();