using System.Collections.Generic;
using Content.Server.Atmos;
using Content.Server.Atmos.EntitySystems;
using Content.Server.NodeContainer.EntitySystems;
using Content.Server.NodeContainer.NodeGroups;
using Content.Shared.Atmos;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Utility;
using Robust.Shared.ViewVariables;
namespace Content.Server.NodeContainer.Nodes
{
///
/// Connects with other s whose
/// correctly correspond.
///
[DataDefinition]
public class PipeNode : Node, IGasMixtureHolder, IRotatableNode
{
///
/// The directions in which this pipe can connect to other pipes around it.
///
[ViewVariables]
[DataField("pipeDirection")]
private PipeDirection _originalPipeDirection;
///
/// The *current* pipe directions (accounting for rotation)
/// Used to check if this pipe can connect to another pipe in a given direction.
///
public PipeDirection CurrentPipeDirection { get; private set; }
private HashSet? _alwaysReachable;
public void AddAlwaysReachable(PipeNode pipeNode)
{
if (NodeGroup == null) return;
if (pipeNode.NodeGroupID != NodeGroupID) return;
_alwaysReachable ??= new();
_alwaysReachable.Add(pipeNode);
EntitySystem.Get().QueueRemakeGroup((BaseNodeGroup) NodeGroup);
}
public void RemoveAlwaysReachable(PipeNode pipeNode)
{
if (_alwaysReachable == null) return;
if (NodeGroup == null) return;
if (pipeNode.NodeGroupID != NodeGroupID) return;
_alwaysReachable.Remove(pipeNode);
EntitySystem.Get().QueueRemakeGroup((BaseNodeGroup) NodeGroup);
}
///
/// Whether this node can connect to others or not.
///
[ViewVariables(VVAccess.ReadWrite)]
public bool ConnectionsEnabled
{
get => _connectionsEnabled;
set
{
_connectionsEnabled = value;
if (NodeGroup != null)
EntitySystem.Get().QueueRemakeGroup((BaseNodeGroup) NodeGroup);
}
}
[DataField("connectionsEnabled")]
private bool _connectionsEnabled = true;
public override bool Connectable(IEntityManager entMan, TransformComponent? xform = null)
{
return _connectionsEnabled && base.Connectable(entMan, xform);
}
[DataField("rotationsEnabled")]
public bool RotationsEnabled { get; set; } = true;
///
/// The this pipe is a part of.
///
[ViewVariables]
private IPipeNet? PipeNet => (IPipeNet?) NodeGroup;
///
/// The gases in this pipe.
///
[ViewVariables]
public GasMixture Air
{
get => PipeNet?.Air ?? GasMixture.SpaceGas;
set
{
DebugTools.Assert(PipeNet != null);
PipeNet!.Air = value;
}
}
[ViewVariables]
[DataField("volume")]
public float Volume { get; set; } = DefaultVolume;
private const float DefaultVolume = 200f;
public override void Initialize(EntityUid owner, IEntityManager entMan)
{
base.Initialize(owner, entMan);
if (!RotationsEnabled)
return;
var xform = entMan.GetComponent(owner);
CurrentPipeDirection = _originalPipeDirection.RotatePipeDirection(xform.LocalRotation);
}
bool IRotatableNode.RotateEvent(ref RotateEvent ev)
{
if (_originalPipeDirection == PipeDirection.Fourway)
return false;
// update valid pipe direction
if (!RotationsEnabled)
{
if (CurrentPipeDirection == _originalPipeDirection)
return false;
CurrentPipeDirection = _originalPipeDirection;
}
else
{
CurrentPipeDirection = _originalPipeDirection.RotatePipeDirection(ev.NewRotation);
}
// node connections need to be updated
return true;
}
public override IEnumerable GetReachableNodes(TransformComponent xform,
EntityQuery nodeQuery,
EntityQuery xformQuery,
IMapGrid? grid,
IEntityManager entMan)
{
if (_alwaysReachable != null)
{
var remQ = new RemQueue();
foreach (var pipe in _alwaysReachable)
{
if (pipe.Deleting)
{
remQ.Add(pipe);
}
yield return pipe;
}
foreach (var pipe in remQ)
{
_alwaysReachable.Remove(pipe);
}
}
if (!xform.Anchored || grid == null)
yield break;
var pos = grid.TileIndicesFor(xform.Coordinates);
for (var i = 0; i < PipeDirectionHelpers.PipeDirections; i++)
{
var pipeDir = (PipeDirection) (1 << i);
if (!CurrentPipeDirection.HasDirection(pipeDir))
continue;
foreach (var pipe in LinkableNodesInDirection(pos, pipeDir, grid, nodeQuery))
{
yield return pipe;
}
}
}
///
/// Gets the pipes that can connect to us from entities on the tile or adjacent in a direction.
///
private IEnumerable LinkableNodesInDirection(Vector2i pos, PipeDirection pipeDir, IMapGrid grid,
EntityQuery nodeQuery)
{
foreach (var pipe in PipesInDirection(pos, pipeDir, grid, nodeQuery))
{
if (pipe.NodeGroupID == NodeGroupID
&& pipe.CurrentPipeDirection.HasDirection(pipeDir.GetOpposite()))
{
yield return pipe;
}
}
}
///
/// Gets the pipes from entities on the tile adjacent in a direction.
///
protected IEnumerable PipesInDirection(Vector2i pos, PipeDirection pipeDir, IMapGrid grid,
EntityQuery nodeQuery)
{
var offsetPos = pos.Offset(pipeDir.ToDirection());
foreach (var entity in grid.GetAnchoredEntities(offsetPos))
{
if (!nodeQuery.TryGetComponent(entity, out var container))
continue;
foreach (var node in container.Nodes.Values)
{
if (node is PipeNode pipe)
yield return pipe;
}
}
}
}
}