Drain: fill with the ore the sorties mined, faulted sorties reported and left out; window long enough for Room and compares run time; copy isolation pinned

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
soul 2026-11-28 15:15:14 +01:00
commit 296caffa16
4 changed files with 210 additions and 72 deletions

View file

@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Linq;
using GameCult.Caching;
using Xunit;
// The station-drain stage of `economy balance --drain` (EconomyDrain) on the demo run of seed 1: the shipped refinery stalls Room the
@ -8,7 +9,9 @@ using Xunit;
public sealed class EconomyDrainTests
{
private static string Root => RunStartTests.FindRepoRoot();
private const int SortieUnits = 131;
// The base loop's sorties of seed 1, mined once: the one belt of the seed, capped, 131 units of mixed ore.
private static readonly Lazy<LoopSample[]> Seed1 = new Lazy<LoopSample[]>(() => EconomyLoop.Sorties(RunStartTests.FindRepoRoot(), 1, 150).ToArray());
private static string Printed(DrainSample sample)
{
@ -19,11 +22,18 @@ public sealed class EconomyDrainTests
private static StationDrain Station(DrainSample sample, string role) => sample.Stations.Single(station => station.Role == role);
// Room for outputs: the refinery's starting gear put away once the sale has filled the bay.
private static void PutGearAway(DemoArena demo)
{
foreach (var bay in demo.Processing.CargoBays)
foreach (var gear in bay.Cargo.Keys.Where(item => !(item is ICommodityStack)).ToList()) bay.Remove(gear);
}
[Fact]
public void DrainIsDeterministic()
{
var first = Printed(EconomyDrain.Run(Root, 1, 30, 2, SortieUnits));
var second = Printed(EconomyDrain.Run(Root, 1, 30, 2, SortieUnits));
var first = Printed(EconomyDrain.Run(Root, 1, 30, 2, Seed1.Value));
var second = Printed(EconomyDrain.Run(Root, 1, 30, 2, Seed1.Value));
Assert.Equal(first, second);
Assert.Contains("Station drain, seed 1", first);
}
@ -32,32 +42,77 @@ public sealed class EconomyDrainTests
[Fact]
public void SortieOutputUnchangedByBoot()
{
var sample = EconomyLoop.Sortie(Root, 1, 0, 150);
var sample = Seed1.Value.Single();
Assert.Equal((131, 73, 58), (sample.Units, sample.UnitsSold, sample.UnitsRefused));
Assert.Equal(1295, sample.SellValue);
Assert.Equal(150, sample.SimSeconds, 3);
Assert.Equal(131, sample.Mined.Sum(m => m.Units));
}
// 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]
public void ACopyNeverSharesObjectsWithTheLiveStation()
{
using var demo = EconomyLoop.Boot(Root, 1);
var ore = demo.Cache.GetAll<SimpleCommodityData>().First(design => FieldKinds.IsOre(demo.Cache, design));
Assert.True(demo.Zenith.CargoBays.Any(bay => bay.TryStore(new SimpleCommodity { Data = new CultRecordRef<ItemData>(demo.Cache.RefOf(ore).Key), Quantity = 5 })));
var live = demo.Zenith.CargoBays.SelectMany(bay => bay.Cargo.Keys).ToList();
Assert.Contains(live, item => item is ICommodityStack);
var copy = EconomyDrain.Copy(demo, demo.Zenith);
var copied = copy.CargoBays.SelectMany(bay => bay.Cargo.Keys).ToList();
Assert.Equal(live.Count, copied.Count);
Assert.DoesNotContain(copied, item => live.Any(held => ReferenceEquals(held, item)));
long Units(Entity entity) => entity.CargoBays.SelectMany(bay => bay.Cargo.Keys.OfType<ICommodityStack>()).Sum(stack => (long) stack.Quantity);
var before = Units(demo.Zenith);
foreach (var bay in copy.CargoBays)
foreach (var stack in bay.Cargo.Keys.OfType<ICommodityStack>().ToList()) bay.Remove((ItemInstance) stack);
Assert.Equal(before, Units(demo.Zenith));
Assert.Equal(0, Units(copy));
}
[Fact]
public void WindowAgreesWithSpans()
{
var sample = EconomyDrain.Run(Root, 1, 120, 1, SortieUnits);
// Long enough for the first cycle to end: Room is inside the window, not only the horizon (Soul s1, window-stall-vacuous).
var sample = EconomyDrain.Run(Root, 1, 700, 1, Seed1.Value);
Assert.All(sample.Stations, station => Assert.Empty(station.WindowDisagreements));
Assert.DoesNotContain(EconomyDrain.Report(new[] { sample }), fault => fault.Invariant == EconomyDrain.HSim);
Assert.True(Station(sample, "processing").Facilities.Single().WindowSteps.TryGetValue(Stall.Room, out var roomSteps) && roomSteps > 0);
// A facility that does not run on screen while the spans run it: the held counts part.
var mutant = EconomyDrain.Run(Root, 1, 120, 1, SortieUnits, beforeWindow: demo =>
// A facility that does not run on screen while the spans run it: the held counts and the run part.
var off = EconomyDrain.Run(Root, 1, 120, 1, Seed1.Value, beforeWindow: demo =>
{
foreach (var unit in demo.Processing.Equipment.Where(item => item.Behaviors.OfType<Facility>().Any())) unit.Enabled.Value = false;
});
Assert.NotEmpty(Station(mutant, "processing").WindowDisagreements);
Assert.Contains(EconomyDrain.Report(new[] { mutant }), fault => fault.Invariant == EconomyDrain.HSim && fault.Record == "seed 1 processing");
Assert.NotEmpty(Station(off, "processing").WindowDisagreements);
Assert.Contains(EconomyDrain.Report(new[] { off }), fault => fault.Invariant == EconomyDrain.HSim && fault.Record == "seed 1 processing");
}
// The screen one step ahead of the span path, which no held count sees: a facility with room has the same units held and a run
// 0.02 s further on (Soul s1, hsim-blind-to-time-skew).
[Fact]
public void WindowSeesATimeSkew()
{
var even = EconomyDrain.Run(Root, 1, 120, 1, Seed1.Value, beforeWindow: PutGearAway);
Assert.Empty(Station(even, "processing").WindowDisagreements);
var skewed = EconomyDrain.Run(Root, 1, 120, 1, Seed1.Value, beforeWindow: PutGearAway, onStep: (demo, step) =>
{
if (step == 0) demo.Arena.Update(EconomyLoop.Step);
});
var parted = Station(skewed, "processing").WindowDisagreements;
Assert.Single(parted);
Assert.Contains("run", parted[0]);
Assert.Contains(EconomyDrain.Report(new[] { skewed }), fault => fault.Invariant == EconomyDrain.HSim);
}
[Fact]
public void AFullRefineryIsAStallFault()
{
var shipped = EconomyDrain.Run(Root, 1, 30, 4, SortieUnits);
var shipped = EconomyDrain.Run(Root, 1, 30, 4, Seed1.Value);
var refinery = Station(shipped, "processing");
Assert.True(refinery.Filled);
Assert.Equal(Stall.Room, refinery.Facilities.Single().HorizonEnd);
@ -66,12 +121,9 @@ public sealed class EconomyDrainTests
Assert.Contains("Room", fault.Message);
// The sale fills every cell: a bay emptied before the fill is filled again by the next sortie. With the starting gear put
// away after the fill (its gear, eight cells) there is room for outputs: the ore drains to nothing, the block reports how long that takes, and the stall is Inputs, not a fault.
var made = EconomyDrain.Run(Root, 1, 30, 24, SortieUnits, beforeWindow: demo =>
{
foreach (var bay in demo.Processing.CargoBays)
foreach (var gear in bay.Cargo.Keys.Where(item => !(item is ICommodityStack)).ToList()) bay.Remove(gear);
});
// away after the fill (eight cells) there is room for outputs: the ore drains to nothing, the block reports how long that
// takes, and the stall is Inputs, not a fault.
var made = EconomyDrain.Run(Root, 1, 30, 24, Seed1.Value, beforeWindow: PutGearAway);
var roomy = Station(made, "processing");
Assert.Contains(roomy.Outputs, pile => pile.Hourly.Any(units => units > 0));
Assert.NotNull(roomy.Ores[0].EmptyAt);
@ -79,13 +131,52 @@ public sealed class EconomyDrainTests
Assert.Equal(Stall.Inputs, roomy.Facilities.Single().HorizonEnd);
Assert.DoesNotContain(EconomyDrain.Report(new[] { made }), f => f.Invariant == EconomyDrain.HStall);
}
[Fact] public void TheSortieHoldIsTheMedianOfWhatSortiesMined() { LoopSample Mined(int units) => new LoopSample { Units = units, Outcome = LoopOutcome.AmmoOut }; Assert.Equal(15, EconomyDrain.SortieUnits(69, new[] { Mined(6), Mined(18), Mined(2), Mined(15), Mined(0) })); Assert.Equal(8, EconomyDrain.SortieUnits(3, new[] { Mined(25), Mined(8), Mined(3) })); Assert.Throws<InvalidOperationException>(() => EconomyDrain.SortieUnits(2, new[] { Mined(0) })); }
[Fact] public void DrainRefusesLoopAndStrayOptions() { foreach (var args in new[] { new[] { "balance", "--drain", "--loop" }, new[] { "balance", "--loop", "1", "--window", "5" }, new[] { "balance", "--drain", "--cap", "5" }, new[] { "balance", "--drain", "--horizon", "0" }, new[] { "balance", "--drain", "--window" } }) { var output = new StringWriter(); Assert.Equal(1, EconomyCommand.Run(args, output)); Assert.Contains("balance --drain [<seed>...]", output.ToString()); } }
// A sortie the base loop faults is a fault of its own and no sortie of the fill count (Soul s1, fill-count-artifact).
[Fact]
public void AFaultedSortieIsReportedAndLeftOutOfTheFill()
{
var mined = Seed1.Value.Single().Mined;
var ammoOut = new LoopSample { Seed = 1, Belt = 0, Belts = 2, BeltKind = "Carbonaceous", Outcome = LoopOutcome.AmmoOut, SimSeconds = 12.4, Units = 18, Mined = mined };
var rig = new LoopSample { Seed = 1, Belt = 1, Belts = 2, BeltKind = "Metallic", Outcome = LoopOutcome.RigFault, Note = "no rock of the belt could be targeted" };
var none = EconomyDrain.Run(Root, 1, 20, 1, new[] { ammoOut, rig });
Assert.Equal(0, none.Usable);
Assert.Equal(2, none.Excluded.Count);
Assert.All(none.Stations, station => Assert.Empty(station.Sorties));
Assert.Equal(2, EconomyDrain.Report(new[] { none }).Count(fault => fault.Invariant == EconomyDrain.HSortie));
Assert.Contains("sorties usable 0 of 2", Printed(none));
var some = EconomyDrain.Run(Root, 1, 20, 1, new[] { ammoOut }.Concat(Seed1.Value).ToArray());
Assert.Equal(1, some.Usable);
Assert.Single(EconomyDrain.Report(new[] { some }), fault => fault.Invariant == EconomyDrain.HSortie);
Assert.Equal(EconomyDrain.Run(Root, 1, 20, 1, Seed1.Value).Stations[1].Sorties.Count, some.Stations[1].Sorties.Count);
}
[Fact]
public void DrainRefusesLoopAndStrayOptions()
{
var refused = new[]
{
new[] { "balance", "--drain", "--loop" },
new[] { "balance", "--loop", "1", "--window", "5" },
new[] { "balance", "--drain", "--cap", "5" },
new[] { "balance", "--drain", "--horizon", "0" },
new[] { "balance", "--drain", "--window" },
new[] { "balance", "--loop", "1", "--cap", "1e39" }
};
foreach (var args in refused)
{
var output = new StringWriter();
Assert.Equal(1, EconomyCommand.Run(args, output));
Assert.Contains("balance --drain [<seed>...]", output.ToString());
}
}
[Fact]
public void IntakeSplitsSiloAndStock()
{
var sample = EconomyDrain.Run(Root, 1, 20, 1, SortieUnits);
var sample = EconomyDrain.Run(Root, 1, 20, 1, Seed1.Value);
var zenith = Station(sample, "trade-hub");
var silo = zenith.Bays.Single(bay => bay.Silo);
var stock = zenith.Bays.Single(bay => !bay.Silo);

View file

@ -104,6 +104,12 @@ public static class EconomyCommand
output.WriteLine(Usage);
return 1;
}
// A cap the sortie takes as a float: a finite double too big for one is as bad as a word.
if (capped && !float.IsFinite((float) cap))
{
output.WriteLine(Usage);
return 1;
}
if (!capped) cap = EconomyLoop.DefaultCap;
if (!windowed) window = EconomyDrain.DefaultWindowSeconds;
if (!horizoned) horizon = EconomyDrain.DefaultHorizonHours;
@ -129,7 +135,7 @@ public static class EconomyCommand
loop = EconomyLoop.Report(db.Cache, settings, seedList.SelectMany(seed => EconomyLoop.Sorties(db.Root, seed, (float) cap)).ToList());
List<DrainSample> drain = null;
if (drainAt >= 0)
drain = seedList.Select(seed => EconomyDrain.Run(db.Root, seed, window, horizon, EconomyDrain.SortieUnits(seed, EconomyLoop.Sorties(db.Root, seed, EconomyLoop.DefaultCap)))).ToList();
drain = seedList.Select(seed => EconomyDrain.Run(db.Root, seed, window, horizon, EconomyLoop.Sorties(db.Root, seed, EconomyLoop.DefaultCap))).ToList();
return Balance(db.Cache, settings.EconomyWageSpread, output, loop, drain);
}
if (args.Length > 0 && args[0] == "author")

View file

@ -9,7 +9,7 @@ using MessagePack;
// What a station's intake and drain look like in the demo run of one seed (ruling station-intake-few-sorties: a station takes ore
// for a few sorties, then fills, and installed facilities work it down). Measurement only: it reads a booted copy of the catalog,
// fills the two demo stations with the seed's median sortie hold through the shipped sale (StationServices.TrySellOre), then reads
// fills the two demo stations with the holds the base loop's sorties mined (a faulted sortie is reported and left out) through the shipped sale (StationServices.TrySellOre), then reads
// the drain twice. On screen for a bounded window, stepping the arena; and by Production.Advance spans on inactive copies for the
// window (which must agree with the steps: H-drain-sim) and for the long horizon (the same rule, FacilityProductionTests.
// OneSpanEqualsManySteps). Writes no catalog, changes no setting: every record it touches is in the booted scratch.
@ -28,7 +28,8 @@ public sealed class BayIntake
// One sortie's hold offered to the station through the shipped sale.
public sealed class SortieSale
{
public string Ore;
// The ores of the hold as mined, with units; Held is what the station wants of them (the offer), Sold what its bays took.
public string Hold;
public int Held, Sold;
public int Refused => Held - Sold;
}
@ -73,47 +74,44 @@ public sealed class StationDrain
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.
public double? FirstRoomStall;
// Units held of every design after the window by steps and by spans, where they differ.
public List<(string Design, int Steps, int Spans)> WindowDisagreements = new List<(string, int, int)>();
// Where the window by steps and by spans part: units held of a design, a facility's run progress, the sim time of its first Room stall.
public List<string> WindowDisagreements = new List<string>();
}
public sealed class DrainSample
{
public uint Seed;
public int SortieUnits;
// The base loop's sorties of this seed: the usable ones fill the stations, the rest are faults and left out of the count.
public int Usable;
public List<LoopSample> Excluded = new List<LoopSample>();
public double WindowSeconds, HorizonHours;
public List<StationDrain> Stations = new List<StationDrain>();
}
public static class EconomyDrain
{
public const string HStall = "H-drain-stall", HSim = "H-drain-sim";
public const double DefaultWindowSeconds = 600, DefaultHorizonHours = 24;
public const string HStall = "H-drain-stall", HSim = "H-drain-sim", HSortie = "H-drain-sortie";
public const double DefaultWindowSeconds = 1200, DefaultHorizonHours = 24;
// One span of the off-screen path, in sim seconds: the resolution of every time the horizon reports.
public const double Span = 60;
private const int MaxSorties = 500;
// Stepped and spanned run progress differ by float rounding only; a skew of one step (0.02 s) is far outside this. A cycle that ends on a
// span boundary is met by the steps one or two steps later, as their sum falls just short of it.
private const double ProgressTolerance = 1e-3;
// The seed's median sortie hold, in units: the middle of the holds of the sorties that mined something (the upper one of an
// even count), whatever stopped them. Most seeds' sorties stop on a starting stock long before the hold fills.
public static int SortieUnits(uint seed, IReadOnlyList<LoopSample> samples)
{
var units = samples.Where(s => s.Units > 0).Select(s => s.Units).OrderBy(u => u).ToList();
if (units.Count == 0) throw new InvalidOperationException($"seed {seed}: no sortie mined anything, so there is no hold to sell");
return units[units.Count / 2];
}
// beforeFill sees the booted arena before any sale (a test empties a bay there); beforeWindow sees it after the fill and before
// the window is read by steps or by spans (a test switches a facility off there).
public static DrainSample Run(string root, uint seed, double windowSeconds, double horizonHours, int sortieUnits,
Action<DemoArena> beforeFill = null, Action<DemoArena> beforeWindow = null)
// sorties are the base loop's samples of this seed (EconomyLoop.Sorties). A measured one that mined something fills the
// stations with the ore it carried as mined; any other (ammunition out, the rig measured nothing) is reported as a fault by
// Report and is not a sortie of the fill count. beforeWindow sees the arena after the fill and before the window is read by steps
// or by spans (a test makes room or switches a facility off there); onStep sees it before each on-screen step (a test skews time).
public static DrainSample Run(string root, uint seed, double windowSeconds, double horizonHours, IReadOnlyList<LoopSample> sorties,
Action<DemoArena> beforeWindow = null, Action<DemoArena, int> onStep = null)
{
if (!double.IsFinite(windowSeconds) || windowSeconds <= 0) throw new ArgumentOutOfRangeException(nameof(windowSeconds), "the window must be a positive, finite number of sim seconds");
if (!double.IsFinite(horizonHours) || horizonHours <= 0) throw new ArgumentOutOfRangeException(nameof(horizonHours), "the horizon must be a positive, finite number of sim hours");
if (sortieUnits < 1) throw new ArgumentOutOfRangeException(nameof(sortieUnits), "a sortie holds at least one unit");
using var demo = EconomyLoop.Boot(root, seed);
beforeFill?.Invoke(demo);
var catalog = demo.Cache;
var sample = new DrainSample { Seed = seed, SortieUnits = sortieUnits, WindowSeconds = windowSeconds, HorizonHours = horizonHours };
var usable = sorties.Where(s => s.Measured && s.Mined.Count > 0).ToList();
var sample = new DrainSample { Seed = seed, Usable = usable.Count, Excluded = sorties.Except(usable).ToList(), WindowSeconds = windowSeconds, HorizonHours = horizonHours };
var stations = new[] { ("processing", demo.Processing), ("trade-hub", demo.Zenith) };
var runs = new List<StationRun>();
foreach (var (role, station) in stations)
@ -121,7 +119,7 @@ public static class EconomyDrain
var drain = new StationDrain { Role = role, Name = station.Name, Kind = station.Kind.Key.Value };
var ores = WantedOres(demo, station);
foreach (var bay in station.CargoBays) drain.Bays.Add(Intake(demo, station, bay, ores));
Fill(demo, station, ores, sortieUnits, drain);
Fill(demo, station, usable, drain);
for (var at = 0; at < drain.Bays.Count; at++)
drain.Bays[at].Taken = station.CargoBays.ElementAt(at).Cargo.Keys.OfType<ICommodityStack>().Where(stack => ores.Contains(((ItemInstance) stack).Data.Key)).Sum(stack => stack.Quantity);
sample.Stations.Add(drain);
@ -130,11 +128,16 @@ public static class EconomyDrain
beforeWindow?.Invoke(demo);
foreach (var run in runs) run.Copy();
// The window on screen: the arena steps, every facility's Stall is read after each step.
// The window on screen: the arena steps, every facility's Stall is read after each step, and the first Room is timed.
var steps = (int) Math.Round(windowSeconds / EconomyLoop.Step);
foreach (var run in runs) run.Live = run.Station.Equipment.SelectMany(unit => unit.Behaviors.OfType<Facility>().Select(facility => (unit, facility))).ToArray();
foreach (var run in runs)
{
run.Live = run.Station.Equipment.SelectMany(unit => unit.Behaviors.OfType<Facility>().Select(facility => (unit, facility))).ToArray();
run.LiveRoom = new double?[run.Live.Length];
}
for (var step = 0; step < steps; step++)
{
onStep?.Invoke(demo, step);
demo.Arena.Update(EconomyLoop.Step);
foreach (var run in runs)
for (var at = 0; at < run.Live.Length; at++)
@ -142,18 +145,39 @@ public static class EconomyDrain
var tally = run.Drain.Facilities[at].WindowSteps;
var stall = run.Live[at].facility.Stall;
tally[stall] = tally.TryGetValue(stall, out var n) ? n + 1 : 1;
if (stall == Stall.Room && run.LiveRoom[at] == null) run.LiveRoom[at] = (step + 1) * (double) EconomyLoop.Step;
}
}
// The same window by spans on the copy: the units held, each facility's run (recipe and progress) and the time of its first Room
// must read as the steps did, to the span for the time. Held counts alone are blind to a facility that stalls Room.
foreach (var run in runs)
{
var spanRoom = new double?[run.Live.Length];
run.Spans(run.WindowCopy, steps * (double) EconomyLoop.Step, (at, stalls) =>
{
for (var i = 0; i < stalls.Length; i++)
if (stalls[i] == Stall.Room && spanRoom[i] == null) spanRoom[i] = at;
});
var onScreen = Held(run.Station);
run.Spans(run.WindowCopy, steps * (double) EconomyLoop.Step);
var spanned = Held(run.WindowCopy);
foreach (var design in onScreen.Keys.Union(spanned.Keys).OrderBy(k => k, StringComparer.Ordinal))
{
onScreen.TryGetValue(design, out var a);
spanned.TryGetValue(design, out var b);
if (a != b) run.Drain.WindowDisagreements.Add((Name(catalog, design), a, b));
if (a != b) run.Drain.WindowDisagreements.Add($"{Name(catalog, design)} held {a} by steps against {b} by spans");
}
var copied = StationRun.Units(run.WindowCopy);
for (var i = 0; i < run.Live.Length; i++)
{
var name = run.Drain.Facilities[i].Name;
var (live, copy) = (run.Live[i].unit.EquippableItem.Run, copied[i].Run);
var (liveRecipe, copyRecipe) = (live?.Recipe.Key.Value, copy?.Recipe.Key.Value);
var (liveProgress, copyProgress) = (live?.Progress ?? 0, copy?.Progress ?? 0);
if (liveRecipe != copyRecipe || Math.Abs(liveProgress - copyProgress) > ProgressTolerance)
run.Drain.WindowDisagreements.Add($"{name} run {liveRecipe ?? "idle"} at {liveProgress:0.###} s by steps against {copyRecipe ?? "idle"} at {copyProgress:0.###} s by spans");
var (steppedRoom, spannedRoom) = (run.LiveRoom[i], spanRoom[i]);
if (steppedRoom.HasValue != spannedRoom.HasValue || (steppedRoom.HasValue && !(steppedRoom.Value > spannedRoom.Value - Span - ProgressTolerance && steppedRoom.Value <= spannedRoom.Value + 2 * EconomyLoop.Step)))
run.Drain.WindowDisagreements.Add($"{name} first Room at {(steppedRoom.HasValue ? steppedRoom.Value.ToString("0.##", CultureInfo.InvariantCulture) : "never")} s by steps against {(spannedRoom.HasValue ? spannedRoom.Value.ToString("0.##", CultureInfo.InvariantCulture) : "never")} s by spans");
}
}
@ -168,29 +192,34 @@ public static class EconomyDrain
{
var faults = new List<EconomyFault>();
foreach (var sample in samples)
{
foreach (var excluded in sample.Excluded)
faults.Add(new EconomyFault(HSortie, $"seed {sample.Seed} belt {excluded.Belt + 1} of {excluded.Belts}", $"the base sortie is {excluded.Outcome} after {excluded.SimSeconds:0.#} s ({excluded.Units} units mined{(string.IsNullOrEmpty(excluded.Note) ? "" : ", " + excluded.Note)}): it is left out of the fill count."));
foreach (var station in sample.Stations)
{
var where = $"seed {sample.Seed} {station.Role}";
foreach (var facility in station.Facilities.Where(f => f.OresLeft.Count > 0 && (f.HorizonEnd == Stall.Room || f.HorizonEnd == Stall.Inputs)))
faults.Add(new EconomyFault(HStall, where, $"{facility.Name} ends the {sample.HorizonHours:0.##} h horizon stalled {facility.HorizonEnd} with {string.Join(", ", facility.OresLeft)} still held: the station does not drain what it took."));
if (station.WindowDisagreements.Count > 0)
faults.Add(new EconomyFault(HSim, where, $"the {sample.WindowSeconds:0.##} s window by steps and by spans disagree on {string.Join(", ", station.WindowDisagreements.Select(d => $"{d.Design} {d.Steps} against {d.Spans}"))}."));
faults.Add(new EconomyFault(HSim, where, $"the {sample.WindowSeconds:0.##} s window by steps and by spans disagree: {string.Join("; ", station.WindowDisagreements)}."));
}
}
return faults;
}
public static void Print(DrainSample sample, TextWriter output)
{
var inv = CultureInfo.InvariantCulture;
output.WriteLine(string.Format(inv, "\nStation drain, seed {0} (a sortie holds {1} units; window {2:0.##} s on screen and by spans; horizon {3:0.##} h by spans of {4:0} s)", sample.Seed, sample.SortieUnits, sample.WindowSeconds, sample.HorizonHours, Span));
output.WriteLine(string.Format(inv, "\nStation drain, seed {0} (window {1:0.##} s on screen and by spans; horizon {2:0.##} h by spans of {3:0} s)", sample.Seed, sample.WindowSeconds, sample.HorizonHours, Span));
output.WriteLine($" sorties usable {sample.Usable} of {sample.Usable + sample.Excluded.Count}" + string.Concat(sample.Excluded.Select(x => $"; excluded belt {x.Belt + 1}/{x.Belts} {x.BeltKind} {x.Outcome} at {x.SimSeconds:0.#} s")));
foreach (var station in sample.Stations)
{
output.WriteLine($" {station.Role} {station.Name} ({station.Kind})");
foreach (var bay in station.Bays)
output.WriteLine($" bay {bay.Name} | {(bay.Silo ? "silo" : "stock")} | cells {bay.Cells} | free {bay.Free} | ore units at MaxStack: {(bay.Capacity.Count == 0 ? "none wanted" : string.Join("; ", bay.Capacity.GroupBy(c => c.Units).Select(g => $"{g.Key} each of {string.Join(", ", g.Select(c => c.Ore))}")))} | wanted ore held after the fill: {bay.Taken}");
output.WriteLine(station.Sorties.Count == 0
? " sorties to fill: none (the station wants no ore)"
: $" sorties to fill: {station.Sorties.Count}{(station.Filled ? "" : "+ (the cap on sorties ended it)")} | per sortie (ore held sold refused): {string.Join("; ", station.Sorties.Select(s => $"{s.Ore} {s.Held} {s.Sold} {s.Refused}"))}");
? " sorties to fill: none (no usable sortie)"
: $" sorties to fill: {station.Sorties.Count}{(station.Filled ? "" : "+ (the station took the whole hold of the last one or the cap on sorties ended it)")} | per sortie (hold as mined | wanted sold refused): {string.Join("; ", station.Sorties.Select(s => $"{s.Hold} | {s.Held} {s.Sold} {s.Refused}"))}");
foreach (var facility in station.Facilities)
{
output.WriteLine($" facility {facility.Name}: {facility.RunList.Count} recipes");
@ -199,8 +228,8 @@ public static class EconomyDrain
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}");
}
output.WriteLine(station.WindowDisagreements.Count == 0
? " window by steps and by spans: held counts agree"
: $" window by steps and by spans: DISAGREE on {string.Join(", ", station.WindowDisagreements.Select(d => $"{d.Design} {d.Steps} against {d.Spans}"))}");
? " 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)}");
foreach (var ore in station.Ores)
{
var drained = ore.Hourly[0] - ore.Hourly[ore.Hourly.Count - 1];
@ -248,38 +277,40 @@ public static class EconomyDrain
return intake;
}
// Sorties to fill: the seed's median sortie hold (one ore a sortie, the wanted ores in turn) sold through the shipped sale until
// a sortie sells less than it brought. The hold is the player's own; what the sale leaves aboard is put away.
private static void Fill(DemoArena demo, Entity station, List<CultRecordKey> ores, int sortieUnits, StationDrain drain)
// Sorties to fill: the usable sorties of the seed in turn, each hold as it was mined, sold through the shipped sale until a sortie
// sells less than the station wanted of it. The hold is the player's own; what the sale leaves aboard is put away.
private static void Fill(DemoArena demo, Entity station, List<LoopSample> usable, StationDrain drain)
{
if (ores.Count == 0) return;
if (usable.Count == 0) return;
var player = demo.Player;
for (var sortie = 0; sortie < MaxSorties; sortie++)
{
var ore = ores[sortie % ores.Count];
var maxStack = demo.Cache.Get<SimpleCommodityData>(ore).MaxStack;
var held = 0;
for (var left = sortieUnits; left > 0;)
var mined = usable[sortie % usable.Count];
foreach (var (ore, units) in mined.Mined)
{
var quantity = Math.Min(left, maxStack);
if (!player.CargoBays.Any(bay => bay.TryStore(new SimpleCommodity { Data = new CultRecordRef<ItemData>(ore), Quantity = quantity }))) break;
held += quantity;
left -= quantity;
var maxStack = demo.Cache.Get<SimpleCommodityData>(ore).MaxStack;
for (var left = units; left > 0;)
{
var quantity = Math.Min(left, maxStack);
if (!player.CargoBays.Any(bay => bay.TryStore(new SimpleCommodity { Data = new CultRecordRef<ItemData>(ore), Quantity = quantity })))
throw new InvalidOperationException($"seed {drain.Name}: the player's hold cannot take back the {units} units of {ore.Value} its sortie mined");
left -= quantity;
}
}
if (held == 0) throw new InvalidOperationException($"seed {drain.Name}: the player's hold takes no {ore.Value}");
var credits = 0;
var result = StationServices.TrySellOre(player.CargoBays, station, ref credits);
drain.Sorties.Add(new SortieSale { Ore = demo.Cache.Get<ItemData>(ore).Name, Held = held, Sold = result.Sold });
drain.Sorties.Add(new SortieSale { Hold = string.Join(", ", mined.Mined.Select(m => $"{m.Units} {demo.Cache.Get<ItemData>(m.Ore).Name}")), Held = result.Offered, Sold = result.Sold });
foreach (var bay in player.CargoBays)
foreach (var stack in bay.Cargo.Keys.OfType<SimpleCommodity>().Where(s => s.Data.Key.Equals(ore)).ToList())
foreach (var stack in bay.Cargo.Keys.OfType<SimpleCommodity>().Where(s => mined.Mined.Any(m => m.Ore.Equals(s.Data.Key))).ToList())
bay.Remove(stack);
if (result.Sold < held) { drain.Filled = true; return; }
if (result.Offered == 0) return;
if (result.Sold < result.Offered) { drain.Filled = true; return; }
}
}
// An inactive copy of a station as it stands, through the bytes a save would hold: Pack alone shares the stack objects with the
// live station, so a copy's sale or take would move the original's units.
internal static Entity Copy(DemoArena demo, Entity station)
public static Entity Copy(DemoArena demo, Entity station)
{
var options = CultDocumentMessagePackSerialization.OptionsFor(typeof(HullData).Assembly);
var bytes = MessagePackSerializer.Serialize(EntitySerializer.Pack(station), options);
@ -307,6 +338,8 @@ public static class EconomyDrain
public readonly List<CultRecordKey> Ores;
public Entity WindowCopy, HorizonCopy;
public (EquippedItem unit, Facility facility)[] Live = Array.Empty<(EquippedItem, Facility)>();
// The sim second each live facility first stalled Room on screen, null while it has not.
public double?[] LiveRoom = Array.Empty<double?>();
public StationRun(DemoArena demo, Entity station, StationDrain drain, List<CultRecordKey> ores)
{
@ -330,6 +363,10 @@ public static class EconomyDrain
}
}
// The facility gear of an entity, in equipment order: the order every per-facility list here is indexed in.
public static EquippableItem[] Units(Entity entity) =>
entity.Equipment.Where(unit => unit.Behaviors.OfType<Facility>().Any()).Select(unit => unit.EquippableItem).ToArray();
private static IEnumerable<(EquippedItem unit, Facility facility)> Facilities(Entity entity) =>
entity.Equipment.SelectMany(unit => unit.Behaviors.OfType<Facility>().Select(facility => (unit, facility)));
@ -339,7 +376,7 @@ public static class EconomyDrain
{
var catalog = Demo.Cache;
var hold = new EntityHold(copy);
var units = copy.Equipment.Where(unit => unit.Behaviors.OfType<Facility>().Any()).Select(unit => unit.EquippableItem).ToArray();
var units = Units(copy);
var last = new Stall[units.Length];
for (var at = 0.0; at < seconds - 1e-9;)
{

View file

@ -36,6 +36,8 @@ public sealed class LoopSample
public int Rocks;
public double SimSeconds;
public int Units;
// The ore the hold carried when the sortie ended, as mined, in key order: what a station is offered. Not printed.
public List<(CultRecordKey Ore, int Units)> Mined = new List<(CultRecordKey, int)>();
// The sim second the first unit floated, i.e. the hold was full; null if it never filled.
public double? HoldFullAt;
// What the arena's processing station paid for the hold at sortie end (TrySellOre), and what it would have paid for every ore
@ -246,6 +248,8 @@ public static class EconomyLoop
else if (targeted == 0) { sample.Outcome = LoopOutcome.RigFault; sample.Note = "no rock of the belt could be targeted"; }
else if (ticks >= capTicks) sample.Outcome = LoopOutcome.Capped;
else { sample.Outcome = LoopOutcome.RigFault; sample.Note = "ran out of rocks before the cap"; }
sample.Mined = player.CargoBays.SelectMany(bay => bay.Cargo.Keys.OfType<SimpleCommodity>()).Where(stack => FieldKinds.IsOre(cache, items.GetData(stack)))
.GroupBy(stack => stack.Data.Key).OrderBy(g => g.Key.Value, StringComparer.Ordinal).Select(g => (g.Key, g.Sum(stack => stack.Quantity))).ToList();
atEnd?.Invoke(player, items, processing);
// The hold sells to the processing station: it pays for the units its bays take, and the rest stays aboard.
var credits = 0;