Files
tbd-station-14/Content.Server/Atmos/Piping/Binary/EntitySystems/GasPressurePumpSystem.cs
Pieter-Jan Briers 103bc19508 Pow3r: stage 1 (#4208)
Co-authored-by: 20kdc <asdd2808@gmail.com>
2021-07-04 18:11:52 +02:00

68 lines
2.5 KiB
C#

using Content.Server.Atmos.Piping.Binary.Components;
using Content.Server.Atmos.Piping.Components;
using Content.Server.NodeContainer;
using Content.Server.NodeContainer.Nodes;
using Content.Shared.Atmos;
using Content.Shared.Atmos.Piping;
using JetBrains.Annotations;
using Robust.Server.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.Maths;
namespace Content.Server.Atmos.Piping.Binary.EntitySystems
{
[UsedImplicitly]
public class GasPressurePumpSystem : EntitySystem
{
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<GasPressurePumpComponent, AtmosDeviceUpdateEvent>(OnPumpUpdated);
SubscribeLocalEvent<GasPressurePumpComponent, AtmosDeviceDisabledEvent>(OnPumpLeaveAtmosphere);
}
private void OnPumpUpdated(EntityUid uid, GasPressurePumpComponent pump, AtmosDeviceUpdateEvent args)
{
var appearance = pump.Owner.GetComponentOrNull<AppearanceComponent>();
appearance?.SetData(PressurePumpVisuals.Enabled, false);
if (!pump.Enabled)
return;
if (!ComponentManager.TryGetComponent(uid, out NodeContainerComponent? nodeContainer))
return;
if (!nodeContainer.TryGetNode(pump.InletName, out PipeNode? inlet)
|| !nodeContainer.TryGetNode(pump.OutletName, out PipeNode? outlet))
return;
var outputStartingPressure = outlet.Air.Pressure;
if (MathHelper.CloseTo(pump.TargetPressure, outputStartingPressure))
return; // No need to pump gas if target has been reached.
if (inlet.Air.TotalMoles > 0 && inlet.Air.Temperature > 0)
{
appearance?.SetData(PressurePumpVisuals.Enabled, true);
// We calculate the necessary moles to transfer using our good ol' friend PV=nRT.
var pressureDelta = pump.TargetPressure - outputStartingPressure;
var transferMoles = pressureDelta * outlet.Air.Volume / inlet.Air.Temperature * Atmospherics.R;
var removed = inlet.Air.Remove(transferMoles);
outlet.AssumeAir(removed);
}
}
private void OnPumpLeaveAtmosphere(EntityUid uid, GasPressurePumpComponent component, AtmosDeviceDisabledEvent args)
{
if (ComponentManager.TryGetComponent(uid, out AppearanceComponent? appearance))
{
appearance.SetData(PressurePumpVisuals.Enabled, false);
}
}
}
}