2021-01-07 00:37:17 -06:00
#nullable enable
using System.Linq ;
2020-09-13 14:23:52 +02:00
using Content.Server.Atmos ;
2020-08-27 09:45:27 -06:00
using Content.Server.GameObjects.Components.NodeContainer ;
using Content.Server.GameObjects.Components.NodeContainer.Nodes ;
2020-09-13 14:23:52 +02:00
using Content.Shared.GameObjects.Components.Atmos ;
2021-02-04 09:47:04 +00:00
using Content.Shared.Interfaces.GameObjects.Components ;
2020-08-31 04:33:05 -06:00
using Robust.Server.GameObjects ;
2020-10-12 05:02:57 -06:00
using Robust.Shared.GameObjects ;
2020-08-27 09:45:27 -06:00
using Robust.Shared.Log ;
2021-03-05 01:08:38 +01:00
using Robust.Shared.Serialization.Manager.Attributes ;
2020-08-27 09:45:27 -06:00
using Robust.Shared.ViewVariables ;
2020-09-13 14:23:52 +02:00
namespace Content.Server.GameObjects.Components.Atmos.Piping.Pumps
2020-08-27 09:45:27 -06:00
{
/// <summary>
/// Transfer gas from one <see cref="PipeNode"/> to another.
/// </summary>
2021-02-04 09:47:04 +00:00
public abstract class BasePumpComponent : Component , IActivate
2020-08-27 09:45:27 -06:00
{
2020-08-31 16:29:53 -06:00
/// <summary>
/// If the pump is currently pumping.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
public bool PumpEnabled
{
get = > _pumpEnabled ;
set
{
_pumpEnabled = value ;
UpdateAppearance ( ) ;
}
}
2021-03-05 01:08:38 +01:00
[DataField("pumpEnabled")]
2020-10-12 05:02:57 -06:00
private bool _pumpEnabled ;
2020-08-31 16:29:53 -06:00
2020-08-27 09:45:27 -06:00
/// <summary>
2021-01-07 00:37:17 -06:00
/// Needs to be same <see cref="PipeDirection"/> as that of a <see cref="PipeNode"/> on this entity.
2020-08-27 09:45:27 -06:00
/// </summary>
[ViewVariables]
2021-03-31 12:41:23 -07:00
[DataField("initialInletDirection", required: true)]
2021-03-05 01:08:38 +01:00
private PipeDirection _initialInletDirection = PipeDirection . None ;
2020-08-27 09:45:27 -06:00
/// <summary>
2021-01-07 00:37:17 -06:00
/// Needs to be same <see cref="PipeDirection"/> as that of a <see cref="PipeNode"/> on this entity.
2020-08-27 09:45:27 -06:00
/// </summary>
[ViewVariables]
2021-03-31 12:41:23 -07:00
[DataField("initialOutletDirection", required: true)]
2021-03-05 01:08:38 +01:00
private PipeDirection _initialOutletDirection = PipeDirection . None ;
2020-08-27 09:45:27 -06:00
[ViewVariables]
2021-01-07 00:37:17 -06:00
private PipeNode ? _inletPipe ;
2020-08-27 09:45:27 -06:00
[ViewVariables]
2021-01-07 00:37:17 -06:00
private PipeNode ? _outletPipe ;
2020-08-27 09:45:27 -06:00
2021-01-07 00:37:17 -06:00
private AppearanceComponent ? _appearance ;
2020-08-31 04:33:05 -06:00
2020-08-27 09:45:27 -06:00
public override void Initialize ( )
{
base . Initialize ( ) ;
2021-01-07 00:37:17 -06:00
Owner . EnsureComponentWarn < PipeNetDeviceComponent > ( ) ;
SetPipes ( ) ;
2020-08-31 04:33:05 -06:00
Owner . TryGetComponent ( out _appearance ) ;
UpdateAppearance ( ) ;
2020-08-27 09:45:27 -06:00
}
2021-01-07 00:37:17 -06:00
public override void HandleMessage ( ComponentMessage message , IComponent ? component )
{
base . HandleMessage ( message , component ) ;
switch ( message )
{
case PipeNetUpdateMessage :
Update ( ) ;
break ;
}
}
public void Update ( )
2020-08-27 09:45:27 -06:00
{
2020-08-31 16:29:53 -06:00
if ( ! PumpEnabled )
return ;
2021-01-07 00:37:17 -06:00
if ( _inletPipe = = null | | _outletPipe = = null )
return ;
2020-08-27 09:45:27 -06:00
PumpGas ( _inletPipe . Air , _outletPipe . Air ) ;
}
protected abstract void PumpGas ( GasMixture inletGas , GasMixture outletGas ) ;
2020-08-31 04:33:05 -06:00
private void UpdateAppearance ( )
{
2020-10-12 05:02:57 -06:00
if ( _inletPipe = = null | | _outletPipe = = null ) return ;
2021-01-08 17:05:29 -06:00
_appearance ? . SetData ( PumpVisuals . VisualState , new PumpVisualState ( _initialInletDirection , _initialOutletDirection , PumpEnabled ) ) ;
2020-08-31 04:33:05 -06:00
}
2020-10-12 05:02:57 -06:00
2021-02-08 22:46:28 +01:00
void IActivate . Activate ( ActivateEventArgs eventArgs )
2021-02-04 09:47:04 +00:00
{
PumpEnabled = ! PumpEnabled ;
}
2021-01-07 00:37:17 -06:00
private void SetPipes ( )
2020-10-12 05:02:57 -06:00
{
_inletPipe = null ;
_outletPipe = null ;
if ( ! Owner . TryGetComponent < NodeContainerComponent > ( out var container ) )
{
2021-03-31 12:41:23 -07:00
Logger . Warning ( $"{nameof(BasePumpComponent)} on {Owner?.Prototype?.ID}, Uid {Owner?.Uid} did not have a {nameof(NodeContainerComponent)}." ) ;
2020-10-12 05:02:57 -06:00
return ;
}
var pipeNodes = container . Nodes . OfType < PipeNode > ( ) ;
2021-01-07 00:37:17 -06:00
_inletPipe = pipeNodes . Where ( pipe = > pipe . PipeDirection = = _initialInletDirection ) . FirstOrDefault ( ) ;
_outletPipe = pipeNodes . Where ( pipe = > pipe . PipeDirection = = _initialOutletDirection ) . FirstOrDefault ( ) ;
if ( _inletPipe = = null | | _outletPipe = = null )
2020-10-12 05:02:57 -06:00
{
2021-03-31 12:41:23 -07:00
Logger . Warning ( $"{nameof(BasePumpComponent)} on {Owner?.Prototype?.ID}, Uid {Owner?.Uid} could not find compatible {nameof(PipeNode)}s on its {nameof(NodeContainerComponent)}." ) ;
2020-10-12 05:02:57 -06:00
return ;
}
}
2020-08-27 09:45:27 -06:00
}
}