2021-07-04 18:11:52 +02:00
using System.Linq ;
2020-09-13 14:23:52 +02:00
using Content.Server.Atmos ;
2021-06-19 13:25:05 +02:00
using Content.Server.Atmos.EntitySystems ;
2021-06-09 22:19:39 +02:00
using Content.Server.NodeContainer.Nodes ;
2021-06-19 13:25:05 +02:00
using Content.Shared.Atmos ;
2021-07-23 11:09:01 +02:00
using Robust.Shared.Utility ;
2020-08-27 09:45:27 -06:00
2021-06-09 22:19:39 +02:00
namespace Content.Server.NodeContainer.NodeGroups
2020-08-27 09:45:27 -06:00
{
2021-06-19 13:25:05 +02:00
public interface IPipeNet : INodeGroup , IGasMixtureHolder
2020-08-27 09:45:27 -06:00
{
/// <summary>
/// Causes gas in the PipeNet to react.
/// </summary>
void Update ( ) ;
}
[NodeGroup(NodeGroupID.Pipe)]
2022-02-15 23:19:32 +13:00
public sealed class PipeNet : BaseNodeGroup , IPipeNet
2020-08-27 09:45:27 -06:00
{
2021-07-04 18:11:52 +02:00
[ViewVariables] public GasMixture Air { get ; set ; } = new ( ) { Temperature = Atmospherics . T20C } ;
2020-08-27 09:45:27 -06:00
2021-01-28 20:21:08 -06:00
[ViewVariables] private AtmosphereSystem ? _atmosphereSystem ;
2020-08-30 12:00:47 +02:00
2022-06-24 00:21:44 +12:00
public EntityUid ? Grid { get ; private set ; }
2020-08-27 09:45:27 -06:00
2022-06-20 12:14:35 +12:00
public override void Initialize ( Node sourceNode , IEntityManager ? entMan = null )
2020-08-27 09:45:27 -06:00
{
2022-06-24 00:21:44 +12:00
IoCManager . Resolve ( ref entMan ) ;
2022-06-20 12:14:35 +12:00
base . Initialize ( sourceNode , entMan ) ;
2020-08-30 12:00:47 +02:00
2022-06-24 00:21:44 +12:00
Grid = entMan . GetComponent < TransformComponent > ( sourceNode . Owner ) . GridUid ;
if ( Grid = = null )
Logger . Error ( $"Created a pipe network without an associated grid. Pipe networks currently need to be tied to a grid for amtos to work. Source entity: {entMan.ToPrettyString(sourceNode.Owner)}" ) ;
_atmosphereSystem = entMan . EntitySysManager . GetEntitySystem < AtmosphereSystem > ( ) ;
2021-07-23 11:09:01 +02:00
_atmosphereSystem . AddPipeNet ( this ) ;
2020-08-27 09:45:27 -06:00
}
public void Update ( )
{
2021-06-26 11:56:11 +02:00
_atmosphereSystem ? . React ( Air , this ) ;
2020-08-27 09:45:27 -06:00
}
2021-07-04 18:11:52 +02:00
public override void LoadNodes ( List < Node > groupNodes )
2020-08-27 09:45:27 -06:00
{
2021-07-04 18:11:52 +02:00
base . LoadNodes ( groupNodes ) ;
2021-06-19 13:25:05 +02:00
2021-07-04 18:11:52 +02:00
foreach ( var node in groupNodes )
{
var pipeNode = ( PipeNode ) node ;
Air . Volume + = pipeNode . Volume ;
}
2020-08-27 09:45:27 -06:00
}
2021-07-04 18:11:52 +02:00
public override void RemoveNode ( Node node )
2020-08-27 09:45:27 -06:00
{
2021-07-04 18:11:52 +02:00
base . RemoveNode ( node ) ;
2021-06-19 13:25:05 +02:00
2022-06-03 22:09:51 +12:00
// if the node is simply being removed into a separate group, we do nothing, as gas redistribution will be
// handled by AfterRemake(). But if it is being deleted, we actually want to remove the gas stored in this node.
if ( ! node . Deleting | | node is not PipeNode pipe )
return ;
Air . Multiply ( 1f - pipe . Volume / Air . Volume ) ;
Air . Volume - = pipe . Volume ;
2020-08-27 09:45:27 -06:00
}
2021-07-04 18:11:52 +02:00
public override void AfterRemake ( IEnumerable < IGrouping < INodeGroup ? , Node > > newGroups )
2020-08-27 09:45:27 -06:00
{
2021-06-19 13:25:05 +02:00
RemoveFromGridAtmos ( ) ;
2022-06-03 22:09:51 +12:00
var newAir = new List < GasMixture > ( newGroups . Count ( ) ) ;
2020-08-27 09:45:27 -06:00
foreach ( var newGroup in newGroups )
{
2022-06-03 22:09:51 +12:00
if ( newGroup . Key is IPipeNet newPipeNet )
newAir . Add ( newPipeNet . Air ) ;
2020-08-27 09:45:27 -06:00
}
2022-06-03 22:09:51 +12:00
_atmosphereSystem ! . DivideInto ( Air , newAir ) ;
2020-08-27 09:45:27 -06:00
}
private void RemoveFromGridAtmos ( )
{
2021-07-23 11:09:01 +02:00
DebugTools . AssertNotNull ( _atmosphereSystem ) ;
_atmosphereSystem ? . RemovePipeNet ( this ) ;
2020-08-27 09:45:27 -06:00
}
2022-02-15 23:19:32 +13:00
public override string GetDebugData ( )
{
return @ $ "Pressure: { Air.Pressure:G3}
Temperature : { Air . Temperature : G3 }
Volume : { Air . Volume : G3 } ";
}
2020-08-27 09:45:27 -06:00
}
}