1
0

PipeNode.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. using Content.Server.Atmos;
  2. using Content.Server.NodeContainer.EntitySystems;
  3. using Content.Server.NodeContainer.NodeGroups;
  4. using Content.Shared.Atmos;
  5. using Robust.Shared.Map;
  6. using Robust.Shared.Map.Components;
  7. using Robust.Shared.Utility;
  8. namespace Content.Server.NodeContainer.Nodes
  9. {
  10. /// <summary>
  11. /// Connects with other <see cref="PipeNode"/>s whose <see cref="PipeDirection"/>
  12. /// correctly correspond.
  13. /// </summary>
  14. [DataDefinition]
  15. [Virtual]
  16. public partial class PipeNode : Node, IGasMixtureHolder, IRotatableNode
  17. {
  18. /// <summary>
  19. /// The directions in which this pipe can connect to other pipes around it.
  20. /// </summary>
  21. [DataField("pipeDirection")]
  22. public PipeDirection OriginalPipeDirection;
  23. /// <summary>
  24. /// The *current* pipe directions (accounting for rotation)
  25. /// Used to check if this pipe can connect to another pipe in a given direction.
  26. /// </summary>
  27. public PipeDirection CurrentPipeDirection { get; private set; }
  28. private HashSet<PipeNode>? _alwaysReachable;
  29. public void AddAlwaysReachable(PipeNode pipeNode)
  30. {
  31. if (pipeNode.NodeGroupID != NodeGroupID) return;
  32. _alwaysReachable ??= new();
  33. _alwaysReachable.Add(pipeNode);
  34. if (NodeGroup != null)
  35. IoCManager.Resolve<IEntityManager>().System<NodeGroupSystem>().QueueRemakeGroup((BaseNodeGroup) NodeGroup);
  36. }
  37. public void RemoveAlwaysReachable(PipeNode pipeNode)
  38. {
  39. if (_alwaysReachable == null) return;
  40. _alwaysReachable.Remove(pipeNode);
  41. if (NodeGroup != null)
  42. IoCManager.Resolve<IEntityManager>().System<NodeGroupSystem>().QueueRemakeGroup((BaseNodeGroup) NodeGroup);
  43. }
  44. /// <summary>
  45. /// Whether this node can connect to others or not.
  46. /// </summary>
  47. [ViewVariables(VVAccess.ReadWrite)]
  48. public bool ConnectionsEnabled
  49. {
  50. get => _connectionsEnabled;
  51. set
  52. {
  53. _connectionsEnabled = value;
  54. if (NodeGroup != null)
  55. IoCManager.Resolve<IEntityManager>().System<NodeGroupSystem>().QueueRemakeGroup((BaseNodeGroup) NodeGroup);
  56. }
  57. }
  58. [DataField("connectionsEnabled")]
  59. private bool _connectionsEnabled = true;
  60. public override bool Connectable(IEntityManager entMan, TransformComponent? xform = null)
  61. {
  62. return _connectionsEnabled && base.Connectable(entMan, xform);
  63. }
  64. [DataField("rotationsEnabled")]
  65. public bool RotationsEnabled { get; set; } = true;
  66. /// <summary>
  67. /// The <see cref="IPipeNet"/> this pipe is a part of.
  68. /// </summary>
  69. [ViewVariables]
  70. private IPipeNet? PipeNet => (IPipeNet?) NodeGroup;
  71. /// <summary>
  72. /// The gases in this pipe.
  73. /// </summary>
  74. [ViewVariables]
  75. public GasMixture Air
  76. {
  77. get => PipeNet?.Air ?? GasMixture.SpaceGas;
  78. set
  79. {
  80. DebugTools.Assert(PipeNet != null);
  81. PipeNet!.Air = value;
  82. }
  83. }
  84. [DataField("volume")]
  85. public float Volume { get; set; } = DefaultVolume;
  86. private const float DefaultVolume = 200f;
  87. public override void Initialize(EntityUid owner, IEntityManager entMan)
  88. {
  89. base.Initialize(owner, entMan);
  90. if (!RotationsEnabled)
  91. return;
  92. var xform = entMan.GetComponent<TransformComponent>(owner);
  93. CurrentPipeDirection = OriginalPipeDirection.RotatePipeDirection(xform.LocalRotation);
  94. }
  95. bool IRotatableNode.RotateNode(in MoveEvent ev)
  96. {
  97. if (OriginalPipeDirection == PipeDirection.Fourway)
  98. return false;
  99. // update valid pipe direction
  100. if (!RotationsEnabled)
  101. {
  102. if (CurrentPipeDirection == OriginalPipeDirection)
  103. return false;
  104. CurrentPipeDirection = OriginalPipeDirection;
  105. return true;
  106. }
  107. var oldDirection = CurrentPipeDirection;
  108. CurrentPipeDirection = OriginalPipeDirection.RotatePipeDirection(ev.NewRotation);
  109. return oldDirection != CurrentPipeDirection;
  110. }
  111. public override void OnAnchorStateChanged(IEntityManager entityManager, bool anchored)
  112. {
  113. if (!anchored)
  114. return;
  115. // update valid pipe directions
  116. if (!RotationsEnabled)
  117. {
  118. CurrentPipeDirection = OriginalPipeDirection;
  119. return;
  120. }
  121. var xform = entityManager.GetComponent<TransformComponent>(Owner);
  122. CurrentPipeDirection = OriginalPipeDirection.RotatePipeDirection(xform.LocalRotation);
  123. }
  124. public override IEnumerable<Node> GetReachableNodes(TransformComponent xform,
  125. EntityQuery<NodeContainerComponent> nodeQuery,
  126. EntityQuery<TransformComponent> xformQuery,
  127. MapGridComponent? grid,
  128. IEntityManager entMan)
  129. {
  130. if (_alwaysReachable != null)
  131. {
  132. var remQ = new RemQueue<PipeNode>();
  133. foreach (var pipe in _alwaysReachable)
  134. {
  135. if (pipe.Deleting)
  136. {
  137. remQ.Add(pipe);
  138. }
  139. yield return pipe;
  140. }
  141. foreach (var pipe in remQ)
  142. {
  143. _alwaysReachable.Remove(pipe);
  144. }
  145. }
  146. if (!xform.Anchored || grid == null)
  147. yield break;
  148. var pos = grid.TileIndicesFor(xform.Coordinates);
  149. for (var i = 0; i < PipeDirectionHelpers.PipeDirections; i++)
  150. {
  151. var pipeDir = (PipeDirection) (1 << i);
  152. if (!CurrentPipeDirection.HasDirection(pipeDir))
  153. continue;
  154. foreach (var pipe in LinkableNodesInDirection(pos, pipeDir, grid, nodeQuery))
  155. {
  156. yield return pipe;
  157. }
  158. }
  159. }
  160. /// <summary>
  161. /// Gets the pipes that can connect to us from entities on the tile or adjacent in a direction.
  162. /// </summary>
  163. private IEnumerable<PipeNode> LinkableNodesInDirection(Vector2i pos, PipeDirection pipeDir, MapGridComponent grid,
  164. EntityQuery<NodeContainerComponent> nodeQuery)
  165. {
  166. foreach (var pipe in PipesInDirection(pos, pipeDir, grid, nodeQuery))
  167. {
  168. if (pipe.NodeGroupID == NodeGroupID
  169. && pipe.CurrentPipeDirection.HasDirection(pipeDir.GetOpposite()))
  170. {
  171. yield return pipe;
  172. }
  173. }
  174. }
  175. /// <summary>
  176. /// Gets the pipes from entities on the tile adjacent in a direction.
  177. /// </summary>
  178. protected IEnumerable<PipeNode> PipesInDirection(Vector2i pos, PipeDirection pipeDir, MapGridComponent grid,
  179. EntityQuery<NodeContainerComponent> nodeQuery)
  180. {
  181. var offsetPos = pos.Offset(pipeDir.ToDirection());
  182. foreach (var entity in grid.GetAnchoredEntities(offsetPos))
  183. {
  184. if (!nodeQuery.TryGetComponent(entity, out var container))
  185. continue;
  186. foreach (var node in container.Nodes.Values)
  187. {
  188. if (node is PipeNode pipe)
  189. yield return pipe;
  190. }
  191. }
  192. }
  193. }
  194. }