Fix gas pressure pump prediction (#35865)

* ACTUALLY predict gas pumps

* generic fake

* aaaaaaaaaaaaa
This commit is contained in:
Milon
2025-03-28 00:42:16 +01:00
committed by GitHub
parent ea785da1b2
commit 92a3fd99ca
6 changed files with 61 additions and 52 deletions

View File

@@ -1,4 +1,3 @@
using Content.Client.Atmos.UI;
using Content.Shared.Atmos.Components;
using Content.Shared.Atmos.EntitySystems;
using Content.Shared.Atmos.Piping.Binary.Components;
@@ -15,7 +14,12 @@ public sealed class GasPressurePumpSystem : SharedGasPressurePumpSystem
private void OnPumpUpdate(Entity<GasPressurePumpComponent> ent, ref AfterAutoHandleStateEvent args)
{
if (UserInterfaceSystem.TryGetOpenUi<GasPressurePumpBoundUserInterface>(ent.Owner, GasPressurePumpUiKey.Key, out var bui))
UpdateUi(ent);
}
protected override void UpdateUi(Entity<GasPressurePumpComponent> ent)
{
if (UserInterfaceSystem.TryGetOpenUi(ent.Owner, GasPressurePumpUiKey.Key, out var bui))
{
bui.Update();
}

View File

@@ -12,7 +12,7 @@ namespace Content.Client.Atmos.UI;
/// Initializes a <see cref="GasPressurePumpWindow"/> and updates it when new server messages are received.
/// </summary>
[UsedImplicitly]
public sealed class GasPressurePumpBoundUserInterface : BoundUserInterface
public sealed class GasPressurePumpBoundUserInterface(EntityUid owner, Enum uiKey) : BoundUserInterface(owner, uiKey)
{
[ViewVariables]
private const float MaxPressure = Atmospherics.MaxOutputPressure;
@@ -20,10 +20,6 @@ public sealed class GasPressurePumpBoundUserInterface : BoundUserInterface
[ViewVariables]
private GasPressurePumpWindow? _window;
public GasPressurePumpBoundUserInterface(EntityUid owner, Enum uiKey) : base(owner, uiKey)
{
}
protected override void Open()
{
base.Open();
@@ -35,7 +31,7 @@ public sealed class GasPressurePumpBoundUserInterface : BoundUserInterface
Update();
}
public void Update()
public override void Update()
{
if (_window == null)
return;
@@ -52,7 +48,9 @@ public sealed class GasPressurePumpBoundUserInterface : BoundUserInterface
private void OnToggleStatusButtonPressed()
{
if (_window is null) return;
if (_window is null)
return;
SendPredictedMessage(new GasPressurePumpToggleStatusMessage(_window.PumpStatus));
}

View File

@@ -1,7 +1,6 @@
using Content.Client.UserInterface.Controls;
using Content.Shared.Atmos;
using Robust.Client.AutoGenerated;
using Robust.Client.UserInterface.CustomControls;
using Robust.Client.UserInterface.XAML;
namespace Content.Client.Atmos.UI

View File

@@ -3,6 +3,7 @@ using Content.Server.Atmos.Piping.Components;
using Content.Server.NodeContainer.EntitySystems;
using Content.Server.NodeContainer.Nodes;
using Content.Server.Power.Components;
using Content.Server.Power.EntitySystems;
using Content.Shared.Atmos;
using Content.Shared.Atmos.Components;
using Content.Shared.Atmos.EntitySystems;
@@ -17,6 +18,7 @@ public sealed class GasPressurePumpSystem : SharedGasPressurePumpSystem
[Dependency] private readonly AtmosphereSystem _atmosphereSystem = default!;
[Dependency] private readonly SharedAmbientSoundSystem _ambientSoundSystem = default!;
[Dependency] private readonly NodeContainerSystem _nodeContainer = default!;
[Dependency] private readonly PowerReceiverSystem _power = default!;
public override void Initialize()
{
@@ -25,33 +27,33 @@ public sealed class GasPressurePumpSystem : SharedGasPressurePumpSystem
SubscribeLocalEvent<GasPressurePumpComponent, AtmosDeviceUpdateEvent>(OnPumpUpdated);
}
private void OnPumpUpdated(EntityUid uid, GasPressurePumpComponent pump, ref AtmosDeviceUpdateEvent args)
private void OnPumpUpdated(Entity<GasPressurePumpComponent> ent, ref AtmosDeviceUpdateEvent args)
{
if (!pump.Enabled
|| (TryComp<ApcPowerReceiverComponent>(uid, out var power) && !power.Powered)
|| !_nodeContainer.TryGetNodes(uid, pump.InletName, pump.OutletName, out PipeNode? inlet, out PipeNode? outlet))
if (!ent.Comp.Enabled
|| !_power.IsPowered(ent)
|| !_nodeContainer.TryGetNodes(ent.Owner, ent.Comp.InletName, ent.Comp.OutletName, out PipeNode? inlet, out PipeNode? outlet))
{
_ambientSoundSystem.SetAmbience(uid, false);
_ambientSoundSystem.SetAmbience(ent, false);
return;
}
var outputStartingPressure = outlet.Air.Pressure;
if (outputStartingPressure >= pump.TargetPressure)
if (outputStartingPressure >= ent.Comp.TargetPressure)
{
_ambientSoundSystem.SetAmbience(uid, false);
_ambientSoundSystem.SetAmbience(ent, false);
return; // No need to pump gas if target has been reached.
}
if (inlet.Air.TotalMoles > 0 && inlet.Air.Temperature > 0)
{
// We calculate the necessary moles to transfer using our good ol' friend PV=nRT.
var pressureDelta = pump.TargetPressure - outputStartingPressure;
var pressureDelta = ent.Comp.TargetPressure - outputStartingPressure;
var transferMoles = (pressureDelta * outlet.Air.Volume) / (inlet.Air.Temperature * Atmospherics.R);
var removed = inlet.Air.Remove(transferMoles);
_atmosphereSystem.Merge(outlet.Air, removed);
_ambientSoundSystem.SetAmbience(uid, removed.TotalMoles > 0f);
_ambientSoundSystem.SetAmbience(ent, removed.TotalMoles > 0f);
}
}
}

View File

@@ -3,7 +3,7 @@ using Robust.Shared.GameStates;
namespace Content.Shared.Atmos.Components;
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState(true)]
public sealed partial class GasPressurePumpComponent : Component
{
[DataField, AutoNetworkedField]

View File

@@ -5,18 +5,15 @@ using Content.Shared.Atmos.Piping.Binary.Components;
using Content.Shared.Atmos.Piping.Components;
using Content.Shared.Database;
using Content.Shared.Examine;
using Content.Shared.Popups;
using Content.Shared.Power;
using Content.Shared.Power.Components;
using Content.Shared.Power.EntitySystems;
using Content.Shared.UserInterface;
namespace Content.Shared.Atmos.EntitySystems;
public abstract class SharedGasPressurePumpSystem : EntitySystem
{
[Dependency] private readonly ISharedAdminLogManager _adminLogger = default!;
[Dependency] protected readonly SharedAppearanceSystem Appearance = default!;
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
[Dependency] private readonly SharedPowerReceiverSystem _receiver = default!;
[Dependency] protected readonly SharedUserInterfaceSystem UserInterfaceSystem = default!;
@@ -36,62 +33,71 @@ public abstract class SharedGasPressurePumpSystem : EntitySystem
SubscribeLocalEvent<GasPressurePumpComponent, ExaminedEvent>(OnExamined);
}
private void OnExamined(EntityUid uid, GasPressurePumpComponent pump, ExaminedEvent args)
private void OnExamined(Entity<GasPressurePumpComponent> ent, ref ExaminedEvent args)
{
if (!Transform(uid).Anchored)
if (!Transform(ent).Anchored)
return;
if (Loc.TryGetString("gas-pressure-pump-system-examined", out var str,
if (Loc.TryGetString("gas-pressure-pump-system-examined",
out var str,
("statusColor", "lightblue"), // TODO: change with pressure?
("pressure", pump.TargetPressure)
("pressure", ent.Comp.TargetPressure)
))
{
args.PushMarkup(str);
}
}
private void OnInit(EntityUid uid, GasPressurePumpComponent pump, ComponentInit args)
private void OnInit(Entity<GasPressurePumpComponent> ent, ref ComponentInit args)
{
UpdateAppearance(uid, pump);
UpdateAppearance(ent);
}
private void OnPowerChanged(EntityUid uid, GasPressurePumpComponent component, ref PowerChangedEvent args)
private void OnPowerChanged(Entity<GasPressurePumpComponent> ent, ref PowerChangedEvent args)
{
UpdateAppearance(uid, component);
UpdateAppearance(ent);
}
private void UpdateAppearance(EntityUid uid, GasPressurePumpComponent? pump = null, AppearanceComponent? appearance = null)
private void UpdateAppearance(Entity<GasPressurePumpComponent, AppearanceComponent?> ent)
{
if (!Resolve(uid, ref pump, ref appearance, false))
if (!Resolve(ent, ref ent.Comp2, false))
return;
var pumpOn = pump.Enabled && _receiver.IsPowered(uid);
Appearance.SetData(uid, PumpVisuals.Enabled, pumpOn, appearance);
var pumpOn = ent.Comp1.Enabled && _receiver.IsPowered(ent.Owner);
_appearance.SetData(ent, PumpVisuals.Enabled, pumpOn, ent.Comp2);
}
private void OnToggleStatusMessage(EntityUid uid, GasPressurePumpComponent pump, GasPressurePumpToggleStatusMessage args)
private void OnToggleStatusMessage(Entity<GasPressurePumpComponent> ent, ref GasPressurePumpToggleStatusMessage args)
{
pump.Enabled = args.Enabled;
_adminLogger.Add(LogType.AtmosPowerChanged, LogImpact.Medium,
$"{ToPrettyString(args.Actor):player} set the power on {ToPrettyString(uid):device} to {args.Enabled}");
Dirty(uid, pump);
UpdateAppearance(uid, pump);
ent.Comp.Enabled = args.Enabled;
_adminLogger.Add(LogType.AtmosPowerChanged,
LogImpact.Medium,
$"{ToPrettyString(args.Actor):player} set the power on {ToPrettyString(ent):device} to {args.Enabled}");
Dirty(ent);
UpdateAppearance(ent);
UpdateUi(ent);
}
private void OnOutputPressureChangeMessage(EntityUid uid, GasPressurePumpComponent pump, GasPressurePumpChangeOutputPressureMessage args)
private void OnOutputPressureChangeMessage(Entity<GasPressurePumpComponent> ent, ref GasPressurePumpChangeOutputPressureMessage args)
{
pump.TargetPressure = Math.Clamp(args.Pressure, 0f, Atmospherics.MaxOutputPressure);
_adminLogger.Add(LogType.AtmosPressureChanged, LogImpact.Medium,
$"{ToPrettyString(args.Actor):player} set the pressure on {ToPrettyString(uid):device} to {args.Pressure}kPa");
Dirty(uid, pump);
ent.Comp.TargetPressure = Math.Clamp(args.Pressure, 0f, Atmospherics.MaxOutputPressure);
_adminLogger.Add(LogType.AtmosPressureChanged,
LogImpact.Medium,
$"{ToPrettyString(args.Actor):player} set the pressure on {ToPrettyString(ent):device} to {args.Pressure}kPa");
Dirty(ent);
UpdateUi(ent);
}
private void OnPumpLeaveAtmosphere(EntityUid uid, GasPressurePumpComponent pump, ref AtmosDeviceDisabledEvent args)
private void OnPumpLeaveAtmosphere(Entity<GasPressurePumpComponent> ent, ref AtmosDeviceDisabledEvent args)
{
pump.Enabled = false;
Dirty(uid, pump);
UpdateAppearance(uid, pump);
ent.Comp.Enabled = false;
Dirty(ent);
UpdateAppearance(ent);
UserInterfaceSystem.CloseUi(uid, GasPressurePumpUiKey.Key);
UserInterfaceSystem.CloseUi(ent.Owner, GasPressurePumpUiKey.Key);
}
protected virtual void UpdateUi(Entity<GasPressurePumpComponent> ent)
{
}
}