From 72372dc77dc4475d4667a803eb3f4162e8a25425 Mon Sep 17 00:00:00 2001 From: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> Date: Sat, 19 Apr 2025 11:48:41 +1000 Subject: [PATCH] Predict gas volume pumps (#33835) * Predict gas volume pumps * Also this one * Review --- .../Binary/Systems/GasVolumePumpSystem.cs | 29 +++++ .../UI/GasVolumePumpBoundUserInterface.cs | 38 +++--- .../Atmos/UI/GasVolumePumpWindow.xaml | 5 +- .../Atmos/UI/GasVolumePumpWindow.xaml.cs | 3 +- .../Components/GasVolumePumpComponent.cs | 52 -------- .../EntitySystems/GasVolumePumpSystem.cs | 111 +----------------- .../Components/GasVolumePumpComponent.cs | 45 +++++++ .../SharedGasVolumePumpComponent.cs | 17 +-- .../Systems/SharedGasVolumePumpSystem.cs | 91 ++++++++++++++ .../Structures/Piping/Atmospherics/binary.yml | 3 + 10 files changed, 199 insertions(+), 195 deletions(-) create mode 100644 Content.Client/Atmos/Piping/Binary/Systems/GasVolumePumpSystem.cs delete mode 100644 Content.Server/Atmos/Piping/Binary/Components/GasVolumePumpComponent.cs create mode 100644 Content.Shared/Atmos/Piping/Binary/Components/GasVolumePumpComponent.cs create mode 100644 Content.Shared/Atmos/Piping/Binary/Systems/SharedGasVolumePumpSystem.cs diff --git a/Content.Client/Atmos/Piping/Binary/Systems/GasVolumePumpSystem.cs b/Content.Client/Atmos/Piping/Binary/Systems/GasVolumePumpSystem.cs new file mode 100644 index 0000000000..f615d9a892 --- /dev/null +++ b/Content.Client/Atmos/Piping/Binary/Systems/GasVolumePumpSystem.cs @@ -0,0 +1,29 @@ +using Content.Shared.Atmos.Piping.Binary.Components; +using Content.Shared.Atmos.Piping.Binary.Systems; +using Robust.Client.GameObjects; + +namespace Content.Client.Atmos.Piping.Binary.Systems; + +public sealed class GasVolumePumpSystem : SharedGasVolumePumpSystem +{ + [Dependency] private readonly UserInterfaceSystem _ui = default!; + + public override void Initialize() + { + base.Initialize(); + SubscribeLocalEvent(OnPumpState); + } + + protected override void UpdateUi(Entity entity) + { + if (_ui.TryGetOpenUi(entity.Owner, GasVolumePumpUiKey.Key, out var bui)) + { + bui.Update(); + } + } + + private void OnPumpState(Entity ent, ref AfterAutoHandleStateEvent args) + { + UpdateUi(ent); + } +} diff --git a/Content.Client/Atmos/UI/GasVolumePumpBoundUserInterface.cs b/Content.Client/Atmos/UI/GasVolumePumpBoundUserInterface.cs index 642f34c2f9..55682f75ef 100644 --- a/Content.Client/Atmos/UI/GasVolumePumpBoundUserInterface.cs +++ b/Content.Client/Atmos/UI/GasVolumePumpBoundUserInterface.cs @@ -1,8 +1,7 @@ -using Content.Shared.Atmos; -using Content.Shared.Atmos.Piping.Binary.Components; +using Content.Shared.Atmos.Piping.Binary.Components; +using Content.Shared.IdentityManagement; using Content.Shared.Localizations; using JetBrains.Annotations; -using Robust.Client.GameObjects; using Robust.Client.UserInterface; namespace Content.Client.Atmos.UI @@ -14,7 +13,7 @@ namespace Content.Client.Atmos.UI public sealed class GasVolumePumpBoundUserInterface : BoundUserInterface { [ViewVariables] - private const float MaxTransferRate = Atmospherics.MaxTransferRate; + private float _maxTransferRate; [ViewVariables] private GasVolumePumpWindow? _window; @@ -29,38 +28,41 @@ namespace Content.Client.Atmos.UI _window = this.CreateWindow(); + if (EntMan.TryGetComponent(Owner, out GasVolumePumpComponent? pump)) + { + _maxTransferRate = pump.MaxTransferRate; + } + _window.ToggleStatusButtonPressed += OnToggleStatusButtonPressed; _window.PumpTransferRateChanged += OnPumpTransferRatePressed; + Update(); } private void OnToggleStatusButtonPressed() { if (_window is null) return; - SendMessage(new GasVolumePumpToggleStatusMessage(_window.PumpStatus)); + + SendPredictedMessage(new GasVolumePumpToggleStatusMessage(_window.PumpStatus)); } private void OnPumpTransferRatePressed(string value) { var rate = UserInputParser.TryFloat(value, out var parsed) ? parsed : 0f; - if (rate > MaxTransferRate) - rate = MaxTransferRate; + rate = Math.Clamp(rate, 0f, _maxTransferRate); - SendMessage(new GasVolumePumpChangeTransferRateMessage(rate)); + SendPredictedMessage(new GasVolumePumpChangeTransferRateMessage(rate)); } - /// - /// Update the UI state based on server-sent info - /// - /// - protected override void UpdateState(BoundUserInterfaceState state) + public override void Update() { - base.UpdateState(state); - if (_window == null || state is not GasVolumePumpBoundUserInterfaceState cast) + base.Update(); + + if (_window is null || !EntMan.TryGetComponent(Owner, out GasVolumePumpComponent? pump)) return; - _window.Title = cast.PumpLabel; - _window.SetPumpStatus(cast.Enabled); - _window.SetTransferRate(cast.TransferRate); + _window.Title = Identity.Name(Owner, EntMan); + _window.SetPumpStatus(pump.Enabled); + _window.SetTransferRate(pump.TransferRate); } } } diff --git a/Content.Client/Atmos/UI/GasVolumePumpWindow.xaml b/Content.Client/Atmos/UI/GasVolumePumpWindow.xaml index 7275b34b3a..fd547ef01e 100644 --- a/Content.Client/Atmos/UI/GasVolumePumpWindow.xaml +++ b/Content.Client/Atmos/UI/GasVolumePumpWindow.xaml @@ -1,5 +1,6 @@ - @@ -19,4 +20,4 @@