diff --git a/Assets/Scripts/ServerShared/Settings.cs b/Assets/Scripts/ServerShared/Settings.cs index f603c1942..450f07048 100644 --- a/Assets/Scripts/ServerShared/Settings.cs +++ b/Assets/Scripts/ServerShared/Settings.cs @@ -194,9 +194,12 @@ public class GameplaySettings public int StartingCredits = 4000; // A tuning tolerance, not a price: `economy balance` flags a chain link whose implied wage falls outside [median / k, median x k]. public float EconomyWageSpread = 2; - // Tuning tolerances like it: the band `economy balance --loop` checks the width of an offered gear ladder against (dearest over - // cheapest offered design in a category). 0 means unset: the widths are reported and no ladder is a fault. - public float EconomyLadderMin = 0, EconomyLadderMax = 0; + // Tuning targets like it, not gates: the band `economy balance --loop` checks the width of an offered Weapon or Gear ladder against + // (dearest over cheapest offered design in a category; ruling ladder-band-weapons-and-gear, 10-30x), and the band of sorties at the + // median net it takes to afford the dearest design of such a category (rulings top-tier-per-item and top-gear-within-one-region-run, + // about 10-20 sorties). 0 means unset: the widths and sorties are reported and nothing is a fault. + public float EconomyLadderMin = 10, EconomyLadderMax = 30; + public float EconomySortiesMin = 10, EconomySortiesMax = 20; public float DurabilityQualityExponent = 2; public float DurabilityQualityMin = 2; public float DurabilityQualityMax = .25f; diff --git a/tests/Aetheria.Shared.Tests/EconomyLoopTests.cs b/tests/Aetheria.Shared.Tests/EconomyLoopTests.cs index 5766630b9..5bfafdd10 100644 --- a/tests/Aetheria.Shared.Tests/EconomyLoopTests.cs +++ b/tests/Aetheria.Shared.Tests/EconomyLoopTests.cs @@ -11,8 +11,8 @@ public sealed class EconomyLoopTests private static LoopSample Sample(uint seed, int sell, int ammo, int repair, double seconds, LoopOutcome outcome = LoopOutcome.Capped) => new LoopSample { Seed = seed, Belts = 1, BeltKind = "belt", Weapons = "gun", SimSeconds = seconds, SellValue = sell, AmmoSpent = ammo, Repair = repair, Outcome = outcome, Note = "why" }; - private static GameplaySettings Settings(float min = 0, float max = 0, int purse = 100000) => - new GameplaySettings { StartingCredits = purse, EconomyLadderMin = min, EconomyLadderMax = max }; + private static GameplaySettings Settings(float min = 0, float max = 0, int purse = 100000, float sortiesMin = 0, float sortiesMax = 0) => + new GameplaySettings { StartingCredits = purse, EconomyLadderMin = min, EconomyLadderMax = max, EconomySortiesMin = sortiesMin, EconomySortiesMax = sortiesMax }; // A gear in `hardpoint` offered at `price`, with a recipe so it is an offered design. private static void Offer(EconomyFixture f, string id, HardpointType hardpoint, int price, int plates = 1) @@ -64,6 +64,53 @@ public sealed class EconomyLoopTests Assert.DoesNotContain(EconomyLoop.Report(f.Cache, Settings(0, 200), samples).Faults, x => x.Record == "Gear/Reactor"); } + // Ruling ladder-band-weapons-and-gear: hulls, bays, consumables and control modules carry no tier band. + [Fact] + public void TheLadderBandCoversWeaponsAndGearOnly() + { + using var f = EconomyFixture.Healthy(); + Offer(f, "cheap", HardpointType.Reactor, 10); + Offer(f, "dear", HardpointType.Reactor, 1000); + var hull = f.Hull("big", HullType.Ship, new[] { ("Plate", 1) }); + f.EditItem(hull, h => h.Price = 100000); + var report = EconomyLoop.Report(f.Cache, Settings(10, 30), new[] { Sample(1, 1000, 0, 0, 60) }); + Assert.True(Ladder(report, "Hull/Ship").Width > 30); + Assert.Equal(new[] { "Gear/Reactor" }, report.Faults.Where(x => x.Invariant == EconomyLoop.HLadder).Select(x => x.Record).ToArray()); + } + + // Rulings top-tier-per-item and top-gear-within-one-region-run: about 10-20 sorties at the median net buys the dearest item. + [Fact] + public void TheAffordBandFaultsADearestDesignOutsideTenToTwentySorties() + { + using var f = EconomyFixture.Healthy(); + Offer(f, "cheap", HardpointType.Reactor, 10); + Offer(f, "top", HardpointType.Reactor, 1500); + // One sortie nets 100: the top design takes 15 sorties. + var samples = new[] { Sample(1, 100, 0, 0, 60) }; + var report = EconomyLoop.Report(f.Cache, Settings(sortiesMin: 10, sortiesMax: 20), samples); + Assert.Equal(15, Ladder(report, "Gear/Reactor").TopSorties.Value, 9); + Assert.DoesNotContain(report.Faults, x => x.Invariant == EconomyLoop.HAfford); + Assert.Contains(EconomyLoop.Report(f.Cache, Settings(sortiesMin: 16, sortiesMax: 20), samples).Faults, x => x.Invariant == EconomyLoop.HAfford && x.Record == "Gear/Reactor"); + var low = Assert.Single(EconomyLoop.Report(f.Cache, Settings(sortiesMin: 10, sortiesMax: 14), samples).Faults, x => x.Invariant == EconomyLoop.HAfford); + Assert.Contains("1 measured sortie", low.Message); + Assert.DoesNotContain(EconomyLoop.Report(f.Cache, Settings(), samples).Faults, x => x.Invariant == EconomyLoop.HAfford); + } + + // The shipped bands on the shipped catalog: the ballistic ladder and its top gun sit inside them, on the one measured sortie + // (seed 1, Metallic belt: sell 1734, repair 183, 150 s) the harness has. Kills a bill trimmed out of the ruled band. + [Fact] + public void TheBallisticLadderAndItsTopGunSitInsideTheShippedBands() + { + using var db = AetherDb.Open(); + var settings = new GameplaySettings(); + Assert.Equal((10f, 30f, 10f, 20f), (settings.EconomyLadderMin, settings.EconomyLadderMax, settings.EconomySortiesMin, settings.EconomySortiesMax)); + var report = EconomyLoop.Report(db.Cache, settings, new[] { Sample(1, 1734, 0, 183, 150) }); + var ladder = Ladder(report, "Weapon/ExplosivelyPropelled"); + Assert.InRange(ladder.Width.Value, 10, 30); + Assert.InRange(ladder.TopSorties.Value, 10, 20); + Assert.DoesNotContain(report.Faults, x => x.Record == "Weapon/ExplosivelyPropelled"); + } + [Fact] public void UnsetLadderBandRaisesNoLadderFault() { @@ -71,7 +118,7 @@ public sealed class EconomyLoopTests Offer(f, "cheap", HardpointType.Reactor, 10); Offer(f, "dear", HardpointType.Reactor, 1000); // A settings object nobody tuned: its band is what the field defaults to. - var report = EconomyLoop.Report(f.Cache, new GameplaySettings(), new[] { Sample(1, 1000, 0, 0, 60) }); + var report = EconomyLoop.Report(f.Cache, Settings(), new[] { Sample(1, 1000, 0, 0, 60) }); Assert.Equal(100, Ladder(report, "Gear/Reactor").Width.Value, 9); Assert.DoesNotContain(report.Faults, x => x.Invariant == EconomyLoop.HLadder); } diff --git a/tools/AetherDb/EconomyLoop.cs b/tools/AetherDb/EconomyLoop.cs index 5866c12a1..8c0585b0d 100644 --- a/tools/AetherDb/EconomyLoop.cs +++ b/tools/AetherDb/EconomyLoop.cs @@ -66,6 +66,8 @@ public sealed class LoopLadder public List Designs; // Dearest over cheapest, for a category of two or more priced designs; null otherwise. public double? Width; + // Sorties at the median net it takes to afford the dearest design here; null for a category of one design or with no measured sortie. + public double? TopSorties; } public sealed class LoopReport @@ -74,6 +76,9 @@ public sealed class LoopReport // Over the measured samples only. public double Median; public double Best; + // The median net of a measured sortie, and how many sorties it rests on. + public double MedianNet; + public int MeasuredSorties; public int Purse; public List Ladders; public List Faults; @@ -81,7 +86,9 @@ public sealed class LoopReport public static class EconomyLoop { - public const string HLoop = "H-loop", HLadder = "H-ladder", HAmmo = "H-ammo", HRig = "H-rig"; + public const string HLoop = "H-loop", HLadder = "H-ladder", HAfford = "H-afford", HAmmo = "H-ammo", HRig = "H-rig"; + // The ladder band and the afford band are rulings about weapons and gear only (ladder-band-weapons-and-gear). + private static bool Banded(string category) => category.StartsWith("Weapon/", StringComparison.Ordinal) || category.StartsWith("Gear/", StringComparison.Ordinal); public static readonly uint[] DefaultSeeds = { 1, 2, 3, 69, 97 }; // Every belt of every default seed is a sample (11 of them), so the cap is what bounds wall time. public const float DefaultCap = 150f; @@ -246,6 +253,8 @@ public static class EconomyLoop var rates = samples.Where(s => s.Measured).Select(s => s.RatePerMinute).OrderBy(r => r).ToList(); var median = rates.Count == 0 ? 0 : rates.Count % 2 == 1 ? rates[rates.Count / 2] : (rates[rates.Count / 2 - 1] + rates[rates.Count / 2]) / 2; var best = rates.Count == 0 ? 0 : rates[rates.Count - 1]; + var nets = samples.Where(s => s.Measured).Select(s => (double) s.Net).OrderBy(n => n).ToList(); + var medianNet = nets.Count == 0 ? 0 : nets.Count % 2 == 1 ? nets[nets.Count / 2] : (nets[nets.Count / 2 - 1] + nets[nets.Count / 2]) / 2; var derived = EconomyBalance.Prices(catalog, out _); double? Minutes(int price, double rate) => rate > 0 ? price / rate : (double?) null; double? Width(IEnumerable prices) @@ -268,7 +277,8 @@ public static class EconomyLoop return new LoopLadder { Category = g.Key, Designs = designs, - Width = Width(designs.Select(d => (double) d.Price)) + Width = Width(designs.Select(d => (double) d.Price)), + TopSorties = designs.Count >= 2 && medianNet > 0 ? designs.Max(d => d.Price) / medianNet : (double?) null }; }).ToList(); @@ -285,9 +295,17 @@ public static class EconomyLoop } var (min, max) = (settings.EconomyLadderMin, settings.EconomyLadderMax); if (min > 0 || max > 0) - foreach (var ladder in ladders) + foreach (var ladder in ladders.Where(l => Banded(l.Category))) if (ladder.Width.HasValue && ((min > 0 && ladder.Width.Value < min) || (max > 0 && ladder.Width.Value > max))) faults.Add(new EconomyFault(HLadder, ladder.Category, $"the ladder is {ladder.Width.Value:0.##}x wide, outside the band {min:0.##} to {max:0.##}.")); - return new LoopReport { Samples = samples, Median = median, Best = best, Purse = settings.StartingCredits, Ladders = ladders, Faults = faults }; + var (sortiesMin, sortiesMax) = (settings.EconomySortiesMin, settings.EconomySortiesMax); + if (sortiesMin > 0 || sortiesMax > 0) + foreach (var ladder in ladders.Where(l => Banded(l.Category) && l.TopSorties.HasValue)) + { + var sorties = ladder.TopSorties.Value; + if ((sortiesMin > 0 && sorties < sortiesMin) || (sortiesMax > 0 && sorties > sortiesMax)) + faults.Add(new EconomyFault(HAfford, ladder.Category, $"the dearest design takes {sorties:0.#} sorties at the median net of {medianNet:0.#}, outside the band {sortiesMin:0.##} to {sortiesMax:0.##}; this rests on {nets.Count} measured sortie(s).")); + } + return new LoopReport { Samples = samples, Median = median, Best = best, MedianNet = medianNet, MeasuredSorties = nets.Count, Purse = settings.StartingCredits, Ladders = ladders, Faults = faults }; } }