2021-11-04 19:41:56 -05:00
using System ;
2021-12-10 12:23:18 -06:00
using Content.Server.Administration.Logs ;
2021-06-19 13:25:05 +02:00
using Content.Server.Atmos.Piping.Binary.Components ;
using Content.Server.Atmos.Piping.Components ;
using Content.Server.NodeContainer ;
2021-07-04 18:11:52 +02:00
using Content.Server.NodeContainer.Nodes ;
2021-06-19 13:25:05 +02:00
using Content.Shared.Atmos ;
using Content.Shared.Atmos.Piping ;
2021-11-04 19:41:56 -05:00
using Content.Shared.Atmos.Piping.Binary.Components ;
2021-12-10 12:23:18 -06:00
using Content.Shared.Database ;
2021-10-19 21:46:31 +00:00
using Content.Shared.Examine ;
2021-11-04 19:41:56 -05:00
using Content.Shared.Interaction ;
2021-11-11 16:10:21 -06:00
using Content.Shared.Popups ;
2021-06-19 13:25:05 +02:00
using JetBrains.Annotations ;
using Robust.Server.GameObjects ;
using Robust.Shared.GameObjects ;
2021-11-04 19:41:56 -05:00
using Robust.Shared.IoC ;
2021-10-19 21:46:31 +00:00
using Robust.Shared.Localization ;
2021-06-19 13:25:05 +02:00
using Robust.Shared.Maths ;
namespace Content.Server.Atmos.Piping.Binary.EntitySystems
{
[UsedImplicitly]
public class GasPressurePumpSystem : EntitySystem
{
2021-11-04 19:41:56 -05:00
[Dependency] private UserInterfaceSystem _userInterfaceSystem = default ! ;
2021-12-10 12:23:18 -06:00
[Dependency] private AdminLogSystem _adminLogSystem = default ! ;
2021-11-04 19:41:56 -05:00
2021-06-19 13:25:05 +02:00
public override void Initialize ( )
{
base . Initialize ( ) ;
SubscribeLocalEvent < GasPressurePumpComponent , AtmosDeviceUpdateEvent > ( OnPumpUpdated ) ;
SubscribeLocalEvent < GasPressurePumpComponent , AtmosDeviceDisabledEvent > ( OnPumpLeaveAtmosphere ) ;
2021-10-19 21:46:31 +00:00
SubscribeLocalEvent < GasPressurePumpComponent , ExaminedEvent > ( OnExamined ) ;
2021-11-04 19:41:56 -05:00
SubscribeLocalEvent < GasPressurePumpComponent , InteractHandEvent > ( OnPumpInteractHand ) ;
// Bound UI subscriptions
SubscribeLocalEvent < GasPressurePumpComponent , GasPressurePumpChangeOutputPressureMessage > ( OnOutputPressureChangeMessage ) ;
SubscribeLocalEvent < GasPressurePumpComponent , GasPressurePumpToggleStatusMessage > ( OnToggleStatusMessage ) ;
2021-10-19 21:46:31 +00:00
}
private void OnExamined ( EntityUid uid , GasPressurePumpComponent pump , ExaminedEvent args )
{
2021-12-08 13:00:43 +01:00
if ( ! EntityManager . GetComponent < TransformComponent > ( pump . Owner ) . Anchored | | ! args . IsInDetailsRange ) // Not anchored? Out of range? No status.
2021-10-19 21:46:31 +00:00
return ;
if ( Loc . TryGetString ( "gas-pressure-pump-system-examined" , out var str ,
( "statusColor" , "lightblue" ) , // TODO: change with pressure?
( "pressure" , pump . TargetPressure )
) )
args . PushMarkup ( str ) ;
2021-06-19 13:25:05 +02:00
}
private void OnPumpUpdated ( EntityUid uid , GasPressurePumpComponent pump , AtmosDeviceUpdateEvent args )
{
2021-12-08 13:00:43 +01:00
var appearance = EntityManager . GetComponentOrNull < AppearanceComponent > ( pump . Owner ) ;
2021-06-19 13:25:05 +02:00
2021-07-30 14:00:14 +02:00
if ( ! pump . Enabled
2021-09-28 13:35:29 +02:00
| | ! EntityManager . TryGetComponent ( uid , out NodeContainerComponent ? nodeContainer )
2021-07-30 14:00:14 +02:00
| | ! nodeContainer . TryGetNode ( pump . InletName , out PipeNode ? inlet )
2021-06-19 13:25:05 +02:00
| | ! nodeContainer . TryGetNode ( pump . OutletName , out PipeNode ? outlet ) )
2021-07-30 14:00:14 +02:00
{
appearance ? . SetData ( PressurePumpVisuals . Enabled , false ) ;
2021-06-19 13:25:05 +02:00
return ;
2021-07-30 14:00:14 +02:00
}
2021-06-19 13:25:05 +02:00
var outputStartingPressure = outlet . Air . Pressure ;
2021-09-29 20:07:01 +10:00
if ( MathHelper . CloseToPercent ( pump . TargetPressure , outputStartingPressure ) )
2021-07-30 14:00:14 +02:00
{
appearance ? . SetData ( PressurePumpVisuals . Enabled , false ) ;
2021-06-19 13:25:05 +02:00
return ; // No need to pump gas if target has been reached.
2021-07-30 14:00:14 +02:00
}
2021-06-19 13:25:05 +02:00
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 )
{
2021-09-28 13:35:29 +02:00
if ( EntityManager . TryGetComponent ( uid , out AppearanceComponent ? appearance ) )
2021-06-19 13:25:05 +02:00
{
appearance . SetData ( PressurePumpVisuals . Enabled , false ) ;
}
}
2021-11-04 19:41:56 -05:00
private void OnPumpInteractHand ( EntityUid uid , GasPressurePumpComponent component , InteractHandEvent args )
{
2021-12-08 13:00:43 +01:00
if ( ! EntityManager . TryGetComponent ( args . User , out ActorComponent ? actor ) )
2021-11-04 19:41:56 -05:00
return ;
2021-12-08 13:00:43 +01:00
if ( EntityManager . GetComponent < TransformComponent > ( component . Owner ) . Anchored )
2021-11-11 16:10:21 -06:00
{
_userInterfaceSystem . TryOpen ( uid , GasPressurePumpUiKey . Key , actor . PlayerSession ) ;
DirtyUI ( uid , component ) ;
}
else
{
args . User . PopupMessageCursor ( Loc . GetString ( "comp-gas-pump-ui-needs-anchor" ) ) ;
}
2021-11-04 19:41:56 -05:00
args . Handled = true ;
}
private void OnToggleStatusMessage ( EntityUid uid , GasPressurePumpComponent pump , GasPressurePumpToggleStatusMessage args )
{
pump . Enabled = args . Enabled ;
2021-12-10 12:23:18 -06:00
_adminLogSystem . Add ( LogType . AtmosPowerChanged , LogImpact . Medium ,
$"{EntityManager.ToPrettyString(args.Session.AttachedEntity!.Value):player} set the power on {EntityManager.ToPrettyString(uid):device} to {args.Enabled}" ) ;
2021-11-04 19:41:56 -05:00
DirtyUI ( uid , pump ) ;
}
private void OnOutputPressureChangeMessage ( EntityUid uid , GasPressurePumpComponent pump , GasPressurePumpChangeOutputPressureMessage args )
{
pump . TargetPressure = Math . Clamp ( args . Pressure , 0f , Atmospherics . MaxOutputPressure ) ;
2021-12-10 12:23:18 -06:00
_adminLogSystem . Add ( LogType . AtmosPressureChanged , LogImpact . Medium ,
$"{EntityManager.ToPrettyString(args.Session.AttachedEntity!.Value):player} set the pressure on {EntityManager.ToPrettyString(uid):device} to {args.Pressure}kPa" ) ;
2021-11-04 19:41:56 -05:00
DirtyUI ( uid , pump ) ;
}
private void DirtyUI ( EntityUid uid , GasPressurePumpComponent ? pump )
{
if ( ! Resolve ( uid , ref pump ) )
return ;
_userInterfaceSystem . TrySetUiState ( uid , GasPressurePumpUiKey . Key ,
2021-12-08 13:00:43 +01:00
new GasPressurePumpBoundUserInterfaceState ( EntityManager . GetComponent < MetaDataComponent > ( pump . Owner ) . EntityName , pump . TargetPressure , pump . Enabled ) ) ;
2021-11-04 19:41:56 -05:00
}
2021-06-19 13:25:05 +02:00
}
}