TegNodeGroup.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. using System.Linq;
  2. using Content.Server.NodeContainer;
  3. using Content.Server.NodeContainer.NodeGroups;
  4. using Content.Server.NodeContainer.Nodes;
  5. using Robust.Shared.Map.Components;
  6. using Robust.Shared.Utility;
  7. namespace Content.Server.Power.Generation.Teg;
  8. /// <summary>
  9. /// Node group that connects the central TEG with its two circulators.
  10. /// </summary>
  11. /// <seealso cref="TegNodeGenerator"/>
  12. /// <seealso cref="TegNodeCirculator"/>
  13. /// <seealso cref="TegSystem"/>
  14. [NodeGroup(NodeGroupID.Teg)]
  15. public sealed class TegNodeGroup : BaseNodeGroup
  16. {
  17. /// <summary>
  18. /// If true, this TEG is fully built and has all its parts properly connected.
  19. /// </summary>
  20. [ViewVariables(VVAccess.ReadWrite)]
  21. public bool IsFullyBuilt { get; private set; }
  22. /// <summary>
  23. /// The central generator component.
  24. /// </summary>
  25. /// <seealso cref="TegGeneratorComponent"/>
  26. [ViewVariables(VVAccess.ReadWrite)]
  27. public TegNodeGenerator? Generator { get; private set; }
  28. // Illustration for how the TEG A/B circulators are laid out.
  29. // Circulator B Generator Circulator A
  30. // ^ -> |
  31. // | V
  32. // They have rotations like the arrows point out.
  33. /// <summary>
  34. /// The A-side circulator. This is the circulator that is in the direction FACING the center component's rotation.
  35. /// </summary>
  36. /// <remarks>
  37. /// Not filled in if there is no center piece to deduce relative rotation from.
  38. /// </remarks>
  39. /// <seealso cref="TegCirculatorComponent"/>
  40. [ViewVariables(VVAccess.ReadWrite)]
  41. public TegNodeCirculator? CirculatorA { get; private set; }
  42. /// <summary>
  43. /// The B-side circulator. This circulator is opposite <see cref="CirculatorA"/>.
  44. /// </summary>
  45. /// <remarks>
  46. /// Not filled in if there is no center piece to deduce relative rotation from.
  47. /// </remarks>
  48. /// <seealso cref="TegCirculatorComponent"/>
  49. [ViewVariables(VVAccess.ReadWrite)]
  50. public TegNodeCirculator? CirculatorB { get; private set; }
  51. private IEntityManager? _entityManager;
  52. public override void Initialize(Node sourceNode, IEntityManager entMan)
  53. {
  54. base.Initialize(sourceNode, entMan);
  55. _entityManager = entMan;
  56. }
  57. public override void LoadNodes(List<Node> groupNodes)
  58. {
  59. DebugTools.Assert(_entityManager != null);
  60. base.LoadNodes(groupNodes);
  61. if (groupNodes.Count > 3)
  62. {
  63. // Somehow got more TEG parts. Probably shenanigans. Bail.
  64. return;
  65. }
  66. Generator = groupNodes.OfType<TegNodeGenerator>().SingleOrDefault();
  67. if (Generator != null)
  68. {
  69. // If we have a generator, we can assign CirculatorA and CirculatorB based on relative rotation.
  70. var xformGenerator = _entityManager.GetComponent<TransformComponent>(Generator.Owner);
  71. var genDir = xformGenerator.LocalRotation.GetDir();
  72. foreach (var node in groupNodes)
  73. {
  74. if (node is not TegNodeCirculator circulator)
  75. continue;
  76. var xform = _entityManager.GetComponent<TransformComponent>(node.Owner);
  77. var dir = xform.LocalRotation.GetDir();
  78. if (genDir.GetClockwise90Degrees() == dir)
  79. {
  80. CirculatorA = circulator;
  81. }
  82. else
  83. {
  84. CirculatorB = circulator;
  85. }
  86. }
  87. }
  88. IsFullyBuilt = Generator != null && CirculatorA != null && CirculatorB != null;
  89. var tegSystem = _entityManager.EntitySysManager.GetEntitySystem<TegSystem>();
  90. foreach (var node in groupNodes)
  91. {
  92. if (node is TegNodeGenerator generator)
  93. tegSystem.UpdateGeneratorConnectivity(generator.Owner, this);
  94. if (node is TegNodeCirculator circulator)
  95. tegSystem.UpdateCirculatorConnectivity(circulator.Owner, this);
  96. }
  97. }
  98. }
  99. /// <summary>
  100. /// Node used by the central TEG generator component.
  101. /// </summary>
  102. /// <seealso cref="TegNodeGroup"/>
  103. /// <seealso cref="TegGeneratorComponent"/>
  104. [DataDefinition]
  105. public sealed partial class TegNodeGenerator : Node
  106. {
  107. public override IEnumerable<Node> GetReachableNodes(
  108. TransformComponent xform,
  109. EntityQuery<NodeContainerComponent> nodeQuery,
  110. EntityQuery<TransformComponent> xformQuery,
  111. MapGridComponent? grid,
  112. IEntityManager entMan)
  113. {
  114. if (!xform.Anchored || grid == null)
  115. yield break;
  116. var gridIndex = grid.TileIndicesFor(xform.Coordinates);
  117. var dir = xform.LocalRotation.GetDir();
  118. var a = FindCirculator(dir);
  119. var b = FindCirculator(dir.GetOpposite());
  120. if (a != null)
  121. yield return a;
  122. if (b != null)
  123. yield return b;
  124. TegNodeCirculator? FindCirculator(Direction searchDir)
  125. {
  126. var targetIdx = gridIndex.Offset(searchDir);
  127. foreach (var node in NodeHelpers.GetNodesInTile(nodeQuery, grid, targetIdx))
  128. {
  129. if (node is not TegNodeCirculator circulator)
  130. continue;
  131. var entity = node.Owner;
  132. var entityXform = xformQuery.GetComponent(entity);
  133. var entityDir = entityXform.LocalRotation.GetDir();
  134. if (entityDir == searchDir.GetClockwise90Degrees())
  135. return circulator;
  136. }
  137. return null;
  138. }
  139. }
  140. }
  141. /// <summary>
  142. /// Node used by the central TEG circulator entities.
  143. /// </summary>
  144. /// <seealso cref="TegNodeGroup"/>
  145. /// <seealso cref="TegCirculatorComponent"/>
  146. [DataDefinition]
  147. public sealed partial class TegNodeCirculator : Node
  148. {
  149. public override IEnumerable<Node> GetReachableNodes(
  150. TransformComponent xform,
  151. EntityQuery<NodeContainerComponent> nodeQuery,
  152. EntityQuery<TransformComponent> xformQuery,
  153. MapGridComponent? grid,
  154. IEntityManager entMan)
  155. {
  156. if (!xform.Anchored || grid == null)
  157. yield break;
  158. var gridIndex = grid.TileIndicesFor(xform.Coordinates);
  159. var dir = xform.LocalRotation.GetDir();
  160. var searchDir = dir.GetClockwise90Degrees();
  161. var targetIdx = gridIndex.Offset(searchDir);
  162. foreach (var node in NodeHelpers.GetNodesInTile(nodeQuery, grid, targetIdx))
  163. {
  164. if (node is not TegNodeGenerator generator)
  165. continue;
  166. var entity = node.Owner;
  167. var entityXform = xformQuery.GetComponent(entity);
  168. var entityDir = entityXform.LocalRotation.GetDir();
  169. if (entityDir == searchDir || entityDir == searchDir.GetOpposite())
  170. {
  171. yield return generator;
  172. break;
  173. }
  174. }
  175. }
  176. }