From e9b688de51d6f750c730b147ab1dd16a0c13bc7b Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Fri, 18 Sep 2026 16:19:22 +0200 Subject: [PATCH] Cut 0: collapse the three disagreeing stat evaluate bodies into one ItemManager.Evaluate (unequipped), ConsumableItemEffect.Evaluate and EquippedItem.Evaluate each hand-computed the same quality/durability/heat/ modifier arithmetic with small, undocumented disagreements (docs/stats-and- power-cut.md Sec 0.2). Collapse the arithmetic into one function, PerformanceStat.Evaluate(IStatContext), fed by three IStatContext implementations: EquippedItem (the only one with heat, live durability and modifiers), ConsumableItemEffect (progress substitutes for heat, unconditionally, matching its prior behaviour exactly) and a new UnequippedStatContext in ItemManager (no heat, no modifiers, by name). No behaviour change beyond the two disagreements the cut map names as defects. Each context's original NaN handling is preserved exactly (unequipped throws with diagnostic detail; equipped/consumable fall back to Min). Behavior.Evaluate(PerformanceStat) keeps its name and signature, so the ~81 existing call sites are untouched. Verified: dotnet test tests/Aetheria.Shared.Tests unchanged at 61/61 before new tests are added; a real-catalog probe over GameData/Aetheria.cc found the new unequipped path reproduces the old formula exactly across 3123 (item, quality, durability, stat) cases; Unity batchmode compiles with 0 error CS and the Cut 1/2/3 shield probes still pass; AetherDb census, factions, dangling, station-fit, loadout 1 and hardpoint-fit are byte-identical before and after. Co-Authored-By: Claude Opus 5 --- Assets/Scripts/ServerShared/Entity.cs | 49 +++++++++++++--------- Assets/Scripts/ServerShared/ItemData.cs | 26 ++++++++++++ Assets/Scripts/ServerShared/ItemManager.cs | 38 +++++++++++++---- 3 files changed, 84 insertions(+), 29 deletions(-) diff --git a/Assets/Scripts/ServerShared/Entity.cs b/Assets/Scripts/ServerShared/Entity.cs index 0383c96e1..12e4d420b 100644 --- a/Assets/Scripts/ServerShared/Entity.cs +++ b/Assets/Scripts/ServerShared/Entity.cs @@ -1138,7 +1138,10 @@ public abstract class Entity } } -public class ConsumableItemEffect +// The consumable case: no durability, no modifiers (there is no entity's worth of equipment to modify against), +// and progress-through-duration substitutes for heat rather than being exponentiated by the stat's heat field. +// That substitution is the disagreement the map names, kept exactly as it behaved before the collapse. +public class ConsumableItemEffect : IStatContext { public float RemainingDuration { get; private set; } public Entity Entity { get; } @@ -1176,18 +1179,20 @@ public class ConsumableItemEffect public float Evaluate(PerformanceStat stat) { - var effectiveness = Data.Effectiveness.Evaluate((Data.Duration - RemainingDuration) / Data.Duration); - var quality = pow(Lot.QualityForRole(stat.FromRole), stat.QualityExponent); - - var result = lerp(stat.Min, stat.Max, effectiveness * quality); - - if (float.IsNaN(result)) - return stat.Min; - return result; + var result = stat.Evaluate(this); + return float.IsNaN(result) ? stat.Min : result; } + + public float HeatFactor(PerformanceStat stat) => + Data.Effectiveness.Evaluate((Data.Duration - RemainingDuration) / Data.Duration); + public float DurabilityFactor(PerformanceStat stat) => 1f; + public float ScaleModifier(PerformanceStat stat) => 1f; + public float ConstantModifier(PerformanceStat stat) => 0f; } -public class EquippedItem +// The equipped case: the only one with heat, live durability, and modifiers, because it is the only one with an +// entity and a running simulation behind it. +public class EquippedItem : IStatContext { public int SortPosition; public EquippableItem EquippableItem; @@ -1353,21 +1358,25 @@ public class EquippedItem public float Evaluate(PerformanceStat stat) { - var heat = pow(ThermalPerformance, ThermalExponent * stat.HeatExponentMultiplier); - var durability = pow(DurabilityPerformance, DurabilityExponent * stat.DurabilityExponentMultiplier); - var quality = pow(Lot.QualityForRole(stat.FromRole), stat.QualityExponent); + var result = stat.Evaluate(this); + return float.IsNaN(result) ? stat.Min : result; + } + public float HeatFactor(PerformanceStat stat) => pow(ThermalPerformance, ThermalExponent * stat.HeatExponentMultiplier); + public float DurabilityFactor(PerformanceStat stat) => pow(DurabilityPerformance, DurabilityExponent * stat.DurabilityExponentMultiplier); + + public float ScaleModifier(PerformanceStat stat) + { var scaleModifier = 1.0f; - var scaleModifiers = stat.GetScaleModifiers(Entity).Values; - foreach (var value in scaleModifiers) scaleModifier *= value; + foreach (var value in stat.GetScaleModifiers(Entity).Values) scaleModifier *= value; + return scaleModifier; + } + public float ConstantModifier(PerformanceStat stat) + { float constantModifier = 0; foreach (var value in stat.GetConstantModifiers(Entity).Values) constantModifier += value; - - var result = lerp(stat.Min, stat.Max, durability * quality * heat) * scaleModifier + constantModifier; - if (float.IsNaN(result)) - return stat.Min; - return result; + return constantModifier; } public void AddHeat(float heat, bool ignoreThermalMass = false) diff --git a/Assets/Scripts/ServerShared/ItemData.cs b/Assets/Scripts/ServerShared/ItemData.cs index b4899b7d3..c21115290 100644 --- a/Assets/Scripts/ServerShared/ItemData.cs +++ b/Assets/Scripts/ServerShared/ItemData.cs @@ -572,6 +572,19 @@ public class HardpointData private static Dictionary _tintColors; } +// What a PerformanceStat reads to resolve one number: the lot it prices quality against, and the three +// per-context factors that used to be three separate Evaluate bodies. EquippedItem, ConsumableItemEffect and the +// unequipped case (ItemManager's own private context) are the three implementations; each supplies only the +// sources it has, and says so through what it returns rather than through a second, quietly different formula. +public interface IStatContext +{ + Lot Lot { get; } + float HeatFactor(PerformanceStat stat); + float DurabilityFactor(PerformanceStat stat); + float ScaleModifier(PerformanceStat stat); + float ConstantModifier(PerformanceStat stat); +} + [MessagePackObject, JsonObject(MemberSerialization.OptIn)] public class PerformanceStat { @@ -619,6 +632,19 @@ public class PerformanceStat return ConstantModifiers[entity]; } + + // The one evaluation path. Quality is read from the context's Lot the same way for everyone; heat, + // durability and modifiers are whatever the context has, and a context with none of those returns the + // identity for that term (1 for a multiplied factor, 0 for an added one) instead of a bespoke formula. + // Callers keep their own NaN handling: an unequipped read throws with diagnostic detail, an equipped or + // consumable read falls back to Min. That disagreement is not named as a defect, so it is not touched here. + public float Evaluate(IStatContext context) + { + var quality = pow(context.Lot.QualityForRole(FromRole), QualityExponent); + var durability = context.DurabilityFactor(this); + var heat = context.HeatFactor(this); + return lerp(Min, Max, quality * durability * heat) * context.ScaleModifier(this) + context.ConstantModifier(this); + } } [CultDocument("aetheria.personalityattribute", "1"), Inspectable, MessagePackObject, JsonObject(MemberSerialization.OptIn)] diff --git a/Assets/Scripts/ServerShared/ItemManager.cs b/Assets/Scripts/ServerShared/ItemManager.cs index 3d7037950..38a4b015a 100644 --- a/Assets/Scripts/ServerShared/ItemManager.cs +++ b/Assets/Scripts/ServerShared/ItemManager.cs @@ -71,22 +71,42 @@ public class ItemManager // The one resolution path from a crafted instance to the lot it was minted from. public Lot GetLot(CraftedItemInstance item) => Lots[item.Lot]; - // Returns stat when not equipped + // Returns stat when not equipped: a null-entity context with no heat, no power and no modifiers. It says so + // in its own name (UnequippedStatContext) rather than being a third quiet dialect of the arithmetic. public float Evaluate(PerformanceStat stat, EquippableItem item) { var data = GetData(item); - var lot = GetLot(item); - var quality = pow(lot.QualityForRole(stat.FromRole), stat.QualityExponent); - var durabilityExponent = lerp( - GameplaySettings.DurabilityQualityMin, - GameplaySettings.DurabilityQualityMax, - pow(lot.Quality, GameplaySettings.DurabilityQualityExponent)); - var durability = pow(item.Durability / data.Durability, durabilityExponent * stat.DurabilityExponentMultiplier); - var result = lerp(stat.Min, stat.Max, quality * durability); + var result = stat.Evaluate(new UnequippedStatContext(this, item, data)); if (float.IsNaN(result)) throw new InvalidOperationException($"Performance Stat on {data.Name} evaluating as NaN: input data is invalid! Durability: {item.Durability} / {data.Durability}"); return result; + } + // The unequipped case: quality and durability read live off the item and its lot; heat and modifiers do not + // exist without an entity, so they resolve as the identity for their term rather than a bespoke formula. + private sealed class UnequippedStatContext : IStatContext + { + private readonly EquippableItem _item; + private readonly EquippableItemData _data; + private readonly float _durabilityExponent; + public Lot Lot { get; } + + public UnequippedStatContext(ItemManager items, EquippableItem item, EquippableItemData data) + { + _item = item; + _data = data; + Lot = items.GetLot(item); + _durabilityExponent = lerp( + items.GameplaySettings.DurabilityQualityMin, + items.GameplaySettings.DurabilityQualityMax, + pow(Lot.Quality, items.GameplaySettings.DurabilityQualityExponent)); + } + + public float HeatFactor(PerformanceStat stat) => 1f; + public float DurabilityFactor(PerformanceStat stat) => + pow(_item.Durability / _data.Durability, _durabilityExponent * stat.DurabilityExponentMultiplier); + public float ScaleModifier(PerformanceStat stat) => 1f; + public float ConstantModifier(PerformanceStat stat) => 0f; } public int GetPrice(CraftedItemInstance item)