diff --git a/Assets/Scripts/ServerShared/Entity.cs b/Assets/Scripts/ServerShared/Entity.cs index 1da6e68d8..9c4929058 100644 --- a/Assets/Scripts/ServerShared/Entity.cs +++ b/Assets/Scripts/ServerShared/Entity.cs @@ -860,7 +860,13 @@ public abstract class Entity public void ActivateConsumable(ConsumableItem item) { - _activeConsumables.Add(new ConsumableItemEffect(item, this)); + var effect = new ConsumableItemEffect(item, this); + // The same hook Activate runs over Equipment: a behaviour that computes its targets there (StatModifier) + // would otherwise reach its second tick uninitialised. + foreach (var behavior in effect.Behaviors) + if (behavior is IInitializableBehavior initializable) + initializable.Initialize(); + _activeConsumables.Add(effect); } public ConsumableItemEffect FindActiveConsumable(ConsumableItemData data) @@ -1453,6 +1459,11 @@ public abstract class Entity // Cut 2 Gate 1 fix (docs/stats-and-power-cut.md): an expired consumable dropped out of this // list without ever telling the resolver, leaving its generation/cache/modifier entries // reachable (keyed by this ConsumableItemEffect instance) for the rest of the process. + // A StatModifier's attachments are keyed by its target items, not by the effect, so Forget alone + // leaves a boost attached to its targets: dispose each behaviour first. + foreach (var behavior in _activeConsumables[i].Behaviors) + if (behavior is IDisposable disposable) + disposable.Dispose(); Resolver.Forget(_activeConsumables[i]); _activeConsumables.RemoveAt(i--); } diff --git a/Assets/Scripts/ServerShared/ItemManager.cs b/Assets/Scripts/ServerShared/ItemManager.cs index d9c42d02a..884179e4e 100644 --- a/Assets/Scripts/ServerShared/ItemManager.cs +++ b/Assets/Scripts/ServerShared/ItemManager.cs @@ -183,6 +183,9 @@ public class ItemManager }; } + if (design is ConsumableItemData) + return new ConsumableItem { Data = l.Design, Lot = lot }; + return new CompoundCommodity { Data = l.Design, Lot = lot diff --git a/Assets/Scripts/ServerShared/StatResolver.cs b/Assets/Scripts/ServerShared/StatResolver.cs index bbd3ea542..7062fbd9e 100644 --- a/Assets/Scripts/ServerShared/StatResolver.cs +++ b/Assets/Scripts/ServerShared/StatResolver.cs @@ -141,6 +141,9 @@ public class StatResolver { set.Scale.Remove(modifierKey); set.Constant.Remove(modifierKey); + // An entry with nothing attached is no entry: a modifier that is gone leaves no trace in the resolver. + if (set.Scale.Count == 0 && set.Constant.Count == 0) + _modifiers.Remove(entry); } _cache.Remove(entry); } diff --git a/tests/Aetheria.Shared.Tests/ConsumableTests.cs b/tests/Aetheria.Shared.Tests/ConsumableTests.cs index 3fdd6aad3..885e4e111 100644 --- a/tests/Aetheria.Shared.Tests/ConsumableTests.cs +++ b/tests/Aetheria.Shared.Tests/ConsumableTests.cs @@ -113,7 +113,7 @@ public sealed class ConsumableTests : IDisposable Assert.Equal(20f, rig.Engine.Evaluate(rig.Thrust), 3); Assert.True(resolver.ModifierEntryCount > baseline); - Tick(rig, 3, .3f); // 0.9s elapsed in all: expired on the third of these ticks + Tick(rig, 3, .3f); // 1.5s elapsed in all: the effect ran out on the fourth tick Assert.Equal(10f, rig.Engine.Evaluate(rig.Thrust), 3); Assert.Equal(baseline, resolver.ModifierEntryCount); } diff --git a/tests/Aetheria.Shared.Tests/LoadoutTests.cs b/tests/Aetheria.Shared.Tests/LoadoutTests.cs index 1f2c9c842..303ca8f06 100644 --- a/tests/Aetheria.Shared.Tests/LoadoutTests.cs +++ b/tests/Aetheria.Shared.Tests/LoadoutTests.cs @@ -760,11 +760,7 @@ public sealed class LoadoutTests : IDisposable var items = new ItemManager(cache, new ProvenanceLedger(), RunSaveTests.TestSettings(), _ => { }); var hullItem = (EquippableItem) items.CreateInstance(items.CreateLot(hullData, maker, .5f)); var ship = new Ship(items, null, hullItem, new EntitySettings()); - // ItemManager.CreateInstance has no ConsumableItemData branch (it only special-cases EquippableItemData, - // falling back to CompoundCommodity otherwise); build the instance directly, as that method does for - // EquippableItem. - var consumableLot = items.CreateLot(consumableData, maker, .5f); - var consumableItem = new ConsumableItem { Data = cache.RefOf(consumableData), Lot = consumableLot }; + var consumableItem = (ConsumableItem) items.CreateInstance(items.CreateLot(consumableData, maker, .5f)); var effect = new ConsumableItemEffect(consumableItem, ship); effect.Update(5f); // half the duration elapsed -> progress .5 -> effectiveness .5 on a linear ramp