From c042669eaed0694723b85c2139d2af2b115a72e1 Mon Sep 17 00:00:00 2001 From: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> Date: Thu, 15 May 2025 22:52:24 +1000 Subject: [PATCH] Predict sleeping action (#37414) * Predict sleeping action * reviews --- Content.Client/Bed/BedSystem.cs | 8 +++ Content.Server/Bed/BedSystem.cs | 37 ++++---------- .../Bed/Components/HealOnBuckleHealing.cs | 7 --- .../Bed/Components/HealOnBuckleComponent.cs | 16 ++++-- .../Bed/Components/HealOnBuckleHealing.cs | 7 +++ Content.Shared/Bed/SharedBedSystem.cs | 49 +++++++++++++++++++ 6 files changed, 86 insertions(+), 38 deletions(-) create mode 100644 Content.Client/Bed/BedSystem.cs delete mode 100644 Content.Server/Bed/Components/HealOnBuckleHealing.cs rename {Content.Server => Content.Shared}/Bed/Components/HealOnBuckleComponent.cs (53%) create mode 100644 Content.Shared/Bed/Components/HealOnBuckleHealing.cs create mode 100644 Content.Shared/Bed/SharedBedSystem.cs diff --git a/Content.Client/Bed/BedSystem.cs b/Content.Client/Bed/BedSystem.cs new file mode 100644 index 0000000000..9c6f28290f --- /dev/null +++ b/Content.Client/Bed/BedSystem.cs @@ -0,0 +1,8 @@ +using Content.Shared.Bed; + +namespace Content.Client.Bed; + +public sealed class BedSystem : SharedBedSystem +{ + +} diff --git a/Content.Server/Bed/BedSystem.cs b/Content.Server/Bed/BedSystem.cs index b2d6889408..673984c0a7 100644 --- a/Content.Server/Bed/BedSystem.cs +++ b/Content.Server/Bed/BedSystem.cs @@ -1,9 +1,9 @@ using Content.Server.Actions; using Content.Server.Bed.Components; using Content.Server.Body.Systems; -using Content.Server.Power.Components; using Content.Server.Power.EntitySystems; using Content.Shared.Bed; +using Content.Shared.Bed.Components; using Content.Shared.Bed.Sleep; using Content.Shared.Body.Components; using Content.Shared.Buckle.Components; @@ -11,49 +11,30 @@ using Content.Shared.Damage; using Content.Shared.Emag.Systems; using Content.Shared.Mobs.Systems; using Content.Shared.Power; -using Robust.Shared.Timing; -using Robust.Shared.Utility; namespace Content.Server.Bed { - public sealed class BedSystem : EntitySystem + public sealed class BedSystem : SharedBedSystem { [Dependency] private readonly DamageableSystem _damageableSystem = default!; - [Dependency] private readonly ActionsSystem _actionsSystem = default!; [Dependency] private readonly EmagSystem _emag = default!; - [Dependency] private readonly SleepingSystem _sleepingSystem = default!; [Dependency] private readonly SharedAppearanceSystem _appearance = default!; [Dependency] private readonly MobStateSystem _mobStateSystem = default!; - [Dependency] private readonly IGameTiming _timing = default!; + + private EntityQuery _sleepingQuery; public override void Initialize() { base.Initialize(); - SubscribeLocalEvent(OnStrapped); - SubscribeLocalEvent(OnUnstrapped); + + _sleepingQuery = GetEntityQuery(); + SubscribeLocalEvent(OnStasisStrapped); SubscribeLocalEvent(OnStasisUnstrapped); SubscribeLocalEvent(OnPowerChanged); SubscribeLocalEvent(OnEmagged); } - private void OnStrapped(Entity bed, ref StrappedEvent args) - { - EnsureComp(bed); - bed.Comp.NextHealTime = _timing.CurTime + TimeSpan.FromSeconds(bed.Comp.HealTime); - _actionsSystem.AddAction(args.Buckle, ref bed.Comp.SleepAction, SleepingSystem.SleepActionId, bed); - - // Single action entity, cannot strap multiple entities to the same bed. - DebugTools.AssertEqual(args.Strap.Comp.BuckledEntities.Count, 1); - } - - private void OnUnstrapped(Entity bed, ref UnstrappedEvent args) - { - _actionsSystem.RemoveAction(args.Buckle, bed.Comp.SleepAction); - _sleepingSystem.TryWaking(args.Buckle.Owner); - RemComp(bed); - } - public override void Update(float frameTime) { base.Update(frameTime); @@ -61,7 +42,7 @@ namespace Content.Server.Bed var query = EntityQueryEnumerator(); while (query.MoveNext(out var uid, out _, out var bedComponent, out var strapComponent)) { - if (_timing.CurTime < bedComponent.NextHealTime) + if (Timing.CurTime < bedComponent.NextHealTime) continue; bedComponent.NextHealTime += TimeSpan.FromSeconds(bedComponent.HealTime); @@ -76,7 +57,7 @@ namespace Content.Server.Bed var damage = bedComponent.Damage; - if (HasComp(healedEntity)) + if (_sleepingQuery.HasComp(healedEntity)) damage *= bedComponent.SleepMultiplier; _damageableSystem.TryChangeDamage(healedEntity, damage, true, origin: uid); diff --git a/Content.Server/Bed/Components/HealOnBuckleHealing.cs b/Content.Server/Bed/Components/HealOnBuckleHealing.cs deleted file mode 100644 index aaa82c737c..0000000000 --- a/Content.Server/Bed/Components/HealOnBuckleHealing.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace Content.Server.Bed.Components -{ - // TODO rename this component - [RegisterComponent] - public sealed partial class HealOnBuckleHealingComponent : Component - {} -} diff --git a/Content.Server/Bed/Components/HealOnBuckleComponent.cs b/Content.Shared/Bed/Components/HealOnBuckleComponent.cs similarity index 53% rename from Content.Server/Bed/Components/HealOnBuckleComponent.cs rename to Content.Shared/Bed/Components/HealOnBuckleComponent.cs index 3c6f3a4382..87f235cfb6 100644 --- a/Content.Server/Bed/Components/HealOnBuckleComponent.cs +++ b/Content.Shared/Bed/Components/HealOnBuckleComponent.cs @@ -1,8 +1,10 @@ using Content.Shared.Damage; +using Robust.Shared.GameStates; +using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom; -namespace Content.Server.Bed.Components +namespace Content.Shared.Bed.Components { - [RegisterComponent] + [RegisterComponent, NetworkedComponent, AutoGenerateComponentPause, AutoGenerateComponentState] public sealed partial class HealOnBuckleComponent : Component { /// @@ -23,8 +25,16 @@ namespace Content.Server.Bed.Components [DataField] public float SleepMultiplier = 3f; + /// + /// Next time that will be applied. + /// + [DataField(customTypeSerializer: typeof(TimeOffsetSerializer)), AutoPausedField, AutoNetworkedField] public TimeSpan NextHealTime = TimeSpan.Zero; //Next heal - [DataField] public EntityUid? SleepAction; + /// + /// Action for the attached entity to be able to sleep. + /// + [DataField, AutoNetworkedField] + public EntityUid? SleepAction; } } diff --git a/Content.Shared/Bed/Components/HealOnBuckleHealing.cs b/Content.Shared/Bed/Components/HealOnBuckleHealing.cs new file mode 100644 index 0000000000..ad83ed6c84 --- /dev/null +++ b/Content.Shared/Bed/Components/HealOnBuckleHealing.cs @@ -0,0 +1,7 @@ +using Robust.Shared.GameStates; + +namespace Content.Shared.Bed.Components; + +// TODO rename this component +[RegisterComponent, NetworkedComponent] +public sealed partial class HealOnBuckleHealingComponent : Component; diff --git a/Content.Shared/Bed/SharedBedSystem.cs b/Content.Shared/Bed/SharedBedSystem.cs new file mode 100644 index 0000000000..903bfefc20 --- /dev/null +++ b/Content.Shared/Bed/SharedBedSystem.cs @@ -0,0 +1,49 @@ +using Content.Shared.Actions; +using Content.Shared.Bed.Components; +using Content.Shared.Bed.Sleep; +using Content.Shared.Buckle.Components; +using Robust.Shared.Timing; +using Robust.Shared.Utility; + +namespace Content.Shared.Bed; + +public abstract class SharedBedSystem : EntitySystem +{ + [Dependency] protected readonly IGameTiming Timing = default!; + [Dependency] private readonly ActionContainerSystem _actConts = default!; + [Dependency] private readonly SharedActionsSystem _actionsSystem = default!; + [Dependency] private readonly SleepingSystem _sleepingSystem = default!; + + public override void Initialize() + { + base.Initialize(); + + SubscribeLocalEvent(OnHealMapInit); + SubscribeLocalEvent(OnStrapped); + SubscribeLocalEvent(OnUnstrapped); + } + + private void OnHealMapInit(Entity ent, ref MapInitEvent args) + { + _actConts.EnsureAction(ent.Owner, ref ent.Comp.SleepAction, SleepingSystem.SleepActionId); + Dirty(ent); + } + + private void OnStrapped(Entity bed, ref StrappedEvent args) + { + EnsureComp(bed); + bed.Comp.NextHealTime = Timing.CurTime + TimeSpan.FromSeconds(bed.Comp.HealTime); + _actionsSystem.AddAction(args.Buckle, ref bed.Comp.SleepAction, SleepingSystem.SleepActionId, bed); + Dirty(bed); + + // Single action entity, cannot strap multiple entities to the same bed. + DebugTools.AssertEqual(args.Strap.Comp.BuckledEntities.Count, 1); + } + + private void OnUnstrapped(Entity bed, ref UnstrappedEvent args) + { + _actionsSystem.RemoveAction(args.Buckle, bed.Comp.SleepAction); + _sleepingSystem.TryWaking(args.Buckle.Owner); + RemComp(bed); + } +} -- 2.51.2