From 7f09b05725f3f8fd6106e1955fe1a09a6c01228e Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sun, 18 Oct 2026 18:04:08 +0200 Subject: [PATCH] ProductWeight: a bin takes its pass fraction of its maker's preference and its line the rest (ruling bin-share-by-floor) Phi comes from CultMath's math.erf. Co-Authored-By: Claude Sonnet 5.5 --- .../Scripts/ServerShared/LoadoutGenerator.cs | 17 ++++++- .../ProductChoiceTests.cs | 51 ++++++++++++++++++- 2 files changed, 65 insertions(+), 3 deletions(-) diff --git a/Assets/Scripts/ServerShared/LoadoutGenerator.cs b/Assets/Scripts/ServerShared/LoadoutGenerator.cs index 55eea27f8..0206935fb 100644 --- a/Assets/Scripts/ServerShared/LoadoutGenerator.cs +++ b/Assets/Scripts/ServerShared/LoadoutGenerator.cs @@ -252,8 +252,21 @@ public class LoadoutGenerator Galaxy == null || Faction == null || ItemManager.ItemData.Get(product.Manufacturer) == Faction || Faction.Allegiance.ContainsKey(product.Manufacturer); - // How much this outfitter favors a product: its maker's preference. The one weight of generation and of ChooseProduct. - public float ProductWeight(FactionProductData product) => ManufacturerPreference(product.Manufacturer); + // How much this outfitter favors a product: its maker's preference times the product's share of it. The one weight of + // generation and of ChooseProduct. A bin takes the share of its maker's stock that would pass its floor and its line takes + // the rest (ruling bin-share-by-floor), so binning never changes a maker's presence for the design. + public float ProductWeight(FactionProductData product) => ManufacturerPreference(product.Manufacturer) * Share(product); + + // 1 - Phi(floor): the fraction of a normal draw that lies at least floor deviations above its mean. + private static float PassFraction(float floor) => .5f * (1 - erf(floor / sqrt(2f))); + + private float Share(FactionProductData product) + { + if (product.Bin != null) return PassFraction(product.Bin.Floor); + var key = ItemManager.ItemData.RefOf(product).Key; + var binned = ItemManager.ItemData.GetAll().Where(other => other.Bin != null && other.Bin.Line.Key.Equals(key)).Sum(bin => PassFraction(bin.Bin.Floor)); + return max(0f, 1 - binned); + } // The product a design named without one is built as (ruling loadout-product-optional-context-chooses): among the // products of the design this outfitter can get, one draw from the run's seeded Random weighted by ProductWeight; diff --git a/tests/Aetheria.Shared.Tests/ProductChoiceTests.cs b/tests/Aetheria.Shared.Tests/ProductChoiceTests.cs index 888634f8e..a11708393 100644 --- a/tests/Aetheria.Shared.Tests/ProductChoiceTests.cs +++ b/tests/Aetheria.Shared.Tests/ProductChoiceTests.cs @@ -63,7 +63,10 @@ public sealed class ProductChoiceTests : IDisposable } } - private Rig Build(params string[] lampOrder) + private Rig Build(params string[] lampOrder) => Build(new float[0], lampOrder); + + // binFloors adds one bin of lamp-a's line per floor, named "lamp-a bin" and "lamp-a bin 2"..., by the same maker. + private Rig Build(float[] binFloors, string[] lampOrder) { var root = Path.Combine(_root, "rig" + _catalogs++); Directory.CreateDirectory(root); @@ -97,6 +100,12 @@ public sealed class ProductChoiceTests : IDisposable // lampOrder holds product names; the record key of the i-th inserted is "k" + i, so keys and insertion order // both follow the permutation. for (var i = 0; i < lampOrder.Length; i++) Sell(lampOrder[i], lamp.Key, makers[lampOrder[i]], "k" + i); + for (var i = 0; i < binFloors.Length; i++) + seed.Commit(batch => batch.Upsert(typeof(FactionProductData), new FactionProductData + { + Name = i == 0 ? "lamp-a bin" : "lamp-a bin " + (i + 1), Design = new CultRecordRef(lamp.Key), Manufacturer = alpha, + Bin = new ProductBin { Line = new CultRecordRef(new CultRecordKey("k" + Array.IndexOf(lampOrder, "lamp-a"))), Floor = binFloors[i] } + }, new CultRecordKey("bin" + i))); seed.FlushAsync().Wait(); } @@ -249,6 +258,46 @@ public sealed class ProductChoiceTests : IDisposable Assert.InRange(MakerDraws(rig, rig.OutEven, 1, "lamp-a", 300), 80, 120); } + // Ruling bin-share-by-floor: a bin of lamp-a's line takes the fraction of lamp-a's stock that would pass its floor, 1 - Phi(Floor), + // of lamp-a's preference, and the line keeps the rest, so the maker's presence for the design is what it was without the bin. + [Fact] + public void ABinTakesItsPassFractionOfItsMakersPreference() + { + var alone = Build("lamp-z", "lamp-a"); + var preference = alone.Outfitter(alone.OutEven, 1, 0).ProductWeight(alone.Product("lamp-a")); + Assert.True(preference > 0); + + var quarter = Build(new[] { .67f }, new[] { "lamp-z", "lamp-a" }); + var generator = quarter.Outfitter(quarter.OutEven, 1, 0); + Assert.Equal(.2514f * preference, generator.ProductWeight(quarter.Product("lamp-a bin")), 3); + Assert.Equal(.7486f * preference, generator.ProductWeight(quarter.Product("lamp-a")), 3); + Assert.Equal(preference, generator.ProductWeight(quarter.Product("lamp-a bin")) + generator.ProductWeight(quarter.Product("lamp-a")), 3); + + var half = Build(new[] { 0f }, new[] { "lamp-z", "lamp-a" }); + generator = half.Outfitter(half.OutEven, 1, 0); + Assert.Equal(.5f * preference, generator.ProductWeight(half.Product("lamp-a bin")), 3); + Assert.Equal(.5f * preference, generator.ProductWeight(half.Product("lamp-a")), 3); + + // The maker keeps its presence: Alpha's line and bin together draw as lamp-a alone did. + var lamp = quarter.Cache.GetByName("Lamp"); + var alpha = Enumerable.Range(1, 300).Count(i => quarter.Cache.Get(quarter.Outfitter(quarter.OutEven, Seed(i), 0).ChooseProduct(lamp).Manufacturer).Name == "Alpha"); + Assert.InRange(alpha, 180, 220); + } + + // A line gives up the sum of its bins' pass fractions and no more: two top-half bins leave it nothing, and a third does not + // take it below nothing. + [Fact] + public void ALineKeepsWhatItsBinsLeaveAndNeverLessThanNothing() + { + var two = Build(new[] { 0f, .67f }, new[] { "lamp-z", "lamp-a" }); + var alone = Build("lamp-z", "lamp-a"); + var preference = alone.Outfitter(alone.OutEven, 1, 0).ProductWeight(alone.Product("lamp-a")); + Assert.Equal((1 - .5f - .2514f) * preference, two.Outfitter(two.OutEven, 1, 0).ProductWeight(two.Product("lamp-a")), 3); + + var three = Build(new[] { 0f, 0f, 0f }, new[] { "lamp-z", "lamp-a" }); + Assert.Equal(0f, three.Outfitter(three.OutEven, 1, 0).ProductWeight(three.Product("lamp-a"))); + } + // A weight that read the allegiance alone would give 1 : .25 here, an 80% draw; the zone and allegiance together // give 1 : .125 for alpha at its own zone, 89%. [Fact]