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 ;
using Content.Server.GameObjects.EntitySystems ;
2020-09-21 17:51:07 +02:00
using Content.Shared.GameObjects.Components.Atmos ;
2020-09-12 06:26:50 -06:00
using Robust.Server.GameObjects ;
2021-01-07 00:37:17 -06:00
using Robust.Shared.GameObjects ;
2020-09-13 14:23:52 +02:00
using Robust.Shared.Log ;
using Robust.Shared.ViewVariables ;
2020-08-27 09:45:27 -06:00
2020-09-13 14:23:52 +02:00
namespace Content.Server.GameObjects.Components.Atmos.Piping.Scrubbers
2020-08-27 09:45:27 -06:00
{
/// <summary>
/// Transfers gas from the tile it is on to a <see cref="PipeNode"/>.
/// </summary>
2021-01-07 00:37:17 -06:00
public abstract class BaseSiphonComponent : Component
2020-08-27 09:45:27 -06:00
{
2020-09-06 16:11:53 +02:00
2020-08-27 09:45:27 -06:00
[ViewVariables]
2021-01-07 00:37:17 -06:00
private PipeNode ? _scrubberOutlet ;
2020-08-27 09:45:27 -06:00
2021-01-07 00:37:17 -06:00
private AtmosphereSystem ? _atmosSystem ;
2020-08-27 09:45:27 -06:00
2020-09-12 06:26:50 -06:00
[ViewVariables(VVAccess.ReadWrite)]
public bool SiphonEnabled
{
get = > _siphonEnabled ;
set
{
_siphonEnabled = value ;
UpdateAppearance ( ) ;
}
}
private bool _siphonEnabled = true ;
2021-01-07 00:37:17 -06:00
private AppearanceComponent ? _appearance ;
2020-09-12 06:26:50 -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 > ( ) ;
2020-08-27 09:45:27 -06:00
_atmosSystem = EntitySystem . Get < AtmosphereSystem > ( ) ;
2021-01-07 00:37:17 -06:00
SetOutlet ( ) ;
2020-09-12 06:26:50 -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-09-12 06:26:50 -06:00
if ( ! SiphonEnabled )
return ;
2020-11-18 15:45:53 +01:00
var tileAtmos = Owner . Transform . Coordinates . GetTileAtmosphere ( Owner . EntityManager ) ;
2021-01-07 00:37:17 -06:00
if ( _scrubberOutlet = = null | | tileAtmos = = null | | tileAtmos . Air = = null )
2020-08-27 09:45:27 -06:00
return ;
2021-01-07 00:37:17 -06:00
2020-08-27 09:45:27 -06:00
ScrubGas ( tileAtmos . Air , _scrubberOutlet . Air ) ;
2020-12-08 17:36:59 +01:00
tileAtmos . Invalidate ( ) ;
2020-08-27 09:45:27 -06:00
}
protected abstract void ScrubGas ( GasMixture inletGas , GasMixture outletGas ) ;
2020-09-12 06:26:50 -06:00
2021-01-07 00:37:17 -06:00
private void SetOutlet ( )
{
if ( ! Owner . TryGetComponent < NodeContainerComponent > ( out var container ) )
{
2021-01-24 02:42:33 -06:00
Logger . Error ( $"{nameof(BaseSiphonComponent)} on {Owner?.Prototype?.ID}, Uid {Owner?.Uid} did not have a {nameof(NodeContainerComponent)}." ) ;
2021-01-07 00:37:17 -06:00
return ;
}
_scrubberOutlet = container . Nodes . OfType < PipeNode > ( ) . FirstOrDefault ( ) ;
if ( _scrubberOutlet = = null )
{
2021-01-24 02:42:33 -06:00
Logger . Error ( $"{nameof(BaseSiphonComponent)} on {Owner?.Prototype?.ID}, Uid {Owner?.Uid} could not find compatible {nameof(PipeNode)}s on its {nameof(NodeContainerComponent)}." ) ;
2021-01-07 00:37:17 -06:00
return ;
}
}
2020-09-12 06:26:50 -06:00
private void UpdateAppearance ( )
{
_appearance ? . SetData ( SiphonVisuals . VisualState , new SiphonVisualState ( SiphonEnabled ) ) ;
}
2020-08-27 09:45:27 -06:00
}
}