diff --git a/Content.Shared/Mobs/Components/MobThresholdsComponent.cs b/Content.Shared/Mobs/Components/MobThresholdsComponent.cs index c7cfb51ebe..a3c503f2ea 100644 --- a/Content.Shared/Mobs/Components/MobThresholdsComponent.cs +++ b/Content.Shared/Mobs/Components/MobThresholdsComponent.cs @@ -1,6 +1,7 @@ using Content.Shared.FixedPoint; using Content.Shared.Mobs.Systems; using Robust.Shared.GameStates; +using Robust.Shared.Serialization; namespace Content.Shared.Mobs.Components; @@ -8,18 +9,38 @@ namespace Content.Shared.Mobs.Components; [Access(typeof(MobThresholdSystem))] public sealed class MobThresholdsComponent : Component { - [DataField("thresholds", required:true), AutoNetworkedField(true)] + [DataField("thresholds", required:true)] public SortedDictionary Thresholds = new(); - [DataField("triggersAlerts"), AutoNetworkedField] + [DataField("triggersAlerts")] public bool TriggersAlerts = true; - [DataField("currentThresholdState"), AutoNetworkedField] + [DataField("currentThresholdState")] public MobState CurrentThresholdState; /// /// Whether or not this entity can be revived out of a dead state. /// - [DataField("allowRevives"), AutoNetworkedField] + [DataField("allowRevives")] public bool AllowRevives; } + +[Serializable, NetSerializable] +public sealed class MobThresholdsComponentState : ComponentState +{ + public Dictionary UnsortedThresholds; + + public bool TriggersAlerts; + + public MobState CurrentThresholdState; + + public bool AllowRevives; + + public MobThresholdsComponentState(Dictionary unsortedThresholds, bool triggersAlerts, MobState currentThresholdState, bool allowRevives) + { + UnsortedThresholds = unsortedThresholds; + TriggersAlerts = triggersAlerts; + CurrentThresholdState = currentThresholdState; + AllowRevives = allowRevives; + } +} diff --git a/Content.Shared/Mobs/Systems/MobThresholdSystem.cs b/Content.Shared/Mobs/Systems/MobThresholdSystem.cs index 08848808b9..d421be8847 100644 --- a/Content.Shared/Mobs/Systems/MobThresholdSystem.cs +++ b/Content.Shared/Mobs/Systems/MobThresholdSystem.cs @@ -4,6 +4,7 @@ using Content.Shared.Alert; using Content.Shared.Damage; using Content.Shared.FixedPoint; using Content.Shared.Mobs.Components; +using Robust.Shared.GameStates; namespace Content.Shared.Mobs.Systems; @@ -14,12 +15,38 @@ public sealed class MobThresholdSystem : EntitySystem public override void Initialize() { + SubscribeLocalEvent(OnGetState); + SubscribeLocalEvent(OnHandleState); + SubscribeLocalEvent(MobThresholdShutdown); SubscribeLocalEvent(MobThresholdStartup); SubscribeLocalEvent(OnDamaged); SubscribeLocalEvent(OnUpdateMobState); } + private void OnGetState(EntityUid uid, MobThresholdsComponent component, ref ComponentGetState args) + { + var thresholds = new Dictionary(); + foreach (var (key, value) in component.Thresholds) + { + thresholds.Add(key, value); + } + args.State = new MobThresholdsComponentState(thresholds, + component.TriggersAlerts, + component.CurrentThresholdState, + component.AllowRevives); + } + + private void OnHandleState(EntityUid uid, MobThresholdsComponent component, ref ComponentHandleState args) + { + if (args.Current is not MobThresholdsComponentState state) + return; + component.Thresholds = new SortedDictionary(state.UnsortedThresholds); + component.TriggersAlerts = state.TriggersAlerts; + component.CurrentThresholdState = state.CurrentThresholdState; + component.AllowRevives = state.AllowRevives; + } + #region Public API /// @@ -222,7 +249,16 @@ public sealed class MobThresholdSystem : EntitySystem if (!Resolve(target, ref threshold)) return; + // create a duplicate dictionary so we don't modify while enumerating. + var thresholds = new Dictionary(threshold.Thresholds); + foreach (var (damageThreshold, state) in thresholds) + { + if (state != mobState) + continue; + threshold.Thresholds.Remove(damageThreshold); + } threshold.Thresholds[damage] = mobState; + Dirty(threshold); VerifyThresholds(target, threshold); }