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)