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.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 { private PipeDirection _connectedDirections; /// /// 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; } /// /// The directions in which this node is connected to other nodes. /// Used by . /// [ViewVariables(VVAccess.ReadWrite)] public PipeDirection ConnectedDirections { get => _connectedDirections; private set { _connectedDirections = value; UpdateAppearance(); } } /// /// 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; [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; } } public void AssumeAir(GasMixture giver) { if(PipeNet != null) EntitySystem.Get().Merge(PipeNet.Air, giver); } [ViewVariables] [DataField("volume")] public float Volume { get; set; } = DefaultVolume; private const float DefaultVolume = 200f; public override void OnContainerStartup() { base.OnContainerStartup(); OnConnectedDirectionsNeedsUpdating(); } public override void OnContainerShutdown() { base.OnContainerShutdown(); UpdateAdjacentConnectedDirections(); } public void JoinPipeNet(IPipeNet pipeNet) { OnConnectedDirectionsNeedsUpdating(); } /// /// Rotates the when the entity is rotated, and re-calculates the . /// void IRotatableNode.RotateEvent(ref RotateEvent ev) { OnConnectedDirectionsNeedsUpdating(); UpdateAppearance(); } public override IEnumerable GetReachableNodes() { for (var i = 0; i < PipeDirectionHelpers.AllPipeDirections; i++) { var pipeDir = (PipeDirection) (1 << i); if (!CurrentPipeDirection.HasDirection(pipeDir)) continue; foreach (var pipe in LinkableNodesInDirection(pipeDir)) { yield return pipe; } } } /// /// Gets the pipes that can connect to us from entities on the tile or adjacent in a direction. /// private IEnumerable LinkableNodesInDirection(PipeDirection pipeDir) { if (!Anchored) yield break; foreach (var pipe in PipesInDirection(pipeDir)) { if (pipe.ConnectionsEnabled && pipe.CurrentPipeDirection.HasDirection(pipeDir.GetOpposite())) yield return pipe; } } /// /// Gets the pipes from entities on the tile adjacent in a direction. /// protected IEnumerable PipesInDirection(PipeDirection pipeDir) { if (!IoCManager.Resolve().GetComponent(Owner.Uid).Anchored) yield break; var grid = IoCManager.Resolve().GetGrid(IoCManager.Resolve().GetComponent(Owner.Uid).GridID); var position = IoCManager.Resolve().GetComponent(Owner.Uid).Coordinates; foreach (var entity in grid.GetInDir(position, pipeDir.ToDirection())) { if (!IoCManager.Resolve().TryGetComponent(entity, out var container)) continue; foreach (var node in container.Nodes.Values) { if (node is PipeNode pipe) yield return pipe; } } } /// /// Gets the pipes from entities on the same tile. /// protected IEnumerable PipesInTile() { if (!IoCManager.Resolve().GetComponent(Owner.Uid).Anchored) yield break; var grid = IoCManager.Resolve().GetGrid(IoCManager.Resolve().GetComponent(Owner.Uid).GridID); var position = IoCManager.Resolve().GetComponent(Owner.Uid).Coordinates; foreach (var entity in grid.GetLocal(position)) { if (!IoCManager.Resolve().TryGetComponent(entity, out var container)) continue; foreach (var node in container.Nodes.Values) { if (node is PipeNode pipe) yield return pipe; } } } /// /// Updates the of this and all sorrounding pipes. /// Also updates CurrentPipeDirection. /// private void OnConnectedDirectionsNeedsUpdating() { if (RotationsEnabled) { CurrentPipeDirection = _originalPipeDirection.RotatePipeDirection(IoCManager.Resolve().GetComponent(Owner.Uid).LocalRotation); } else { CurrentPipeDirection = _originalPipeDirection; } UpdateConnectedDirections(); UpdateAdjacentConnectedDirections(); UpdateAppearance(); } /// /// Checks what directions there are connectable pipes in, to update . /// private void UpdateConnectedDirections() { ConnectedDirections = PipeDirection.None; for (var i = 0; i < PipeDirectionHelpers.AllPipeDirections; i++) { var pipeDir = (PipeDirection) (1 << i); if (!CurrentPipeDirection.HasDirection(pipeDir)) continue; foreach (var pipe in LinkableNodesInDirection(pipeDir)) { if (pipe.Connectable && pipe.NodeGroupID == NodeGroupID) { ConnectedDirections |= pipeDir; break; } } } } /// /// Calls on all adjacent pipes, /// to update their when this pipe is changed. /// private void UpdateAdjacentConnectedDirections() { for (var i = 0; i < PipeDirectionHelpers.PipeDirections; i++) { var pipeDir = (PipeDirection) (1 << i); foreach (var pipe in LinkableNodesInDirection(pipeDir)) { pipe.UpdateConnectedDirections(); pipe.UpdateAppearance(); } } } /// /// Updates the . /// Gets the combined of every pipe on this entity, so the visualizer on this entity can draw the pipe connections. /// private void UpdateAppearance() { if (!IoCManager.Resolve().TryGetComponent(Owner.Uid, out AppearanceComponent? appearance) || !IoCManager.Resolve().TryGetComponent(Owner.Uid, out NodeContainerComponent? container)) return; var netConnectedDirections = PipeDirection.None; foreach (var node in container.Nodes.Values) { if (node is PipeNode pipe) { netConnectedDirections |= pipe.ConnectedDirections; } } appearance.SetData(PipeVisuals.VisualState, new PipeVisualState(netConnectedDirections)); } } }