1
0

PipeNet.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System.Linq;
  2. using Content.Server.Atmos;
  3. using Content.Server.Atmos.EntitySystems;
  4. using Content.Server.NodeContainer.Nodes;
  5. using Content.Shared.Atmos;
  6. using Robust.Shared.Utility;
  7. namespace Content.Server.NodeContainer.NodeGroups
  8. {
  9. public interface IPipeNet : INodeGroup, IGasMixtureHolder
  10. {
  11. /// <summary>
  12. /// Causes gas in the PipeNet to react.
  13. /// </summary>
  14. void Update();
  15. }
  16. [NodeGroup(NodeGroupID.Pipe)]
  17. public sealed class PipeNet : BaseNodeGroup, IPipeNet
  18. {
  19. [ViewVariables] public GasMixture Air { get; set; } = new() {Temperature = Atmospherics.T20C};
  20. [ViewVariables] private AtmosphereSystem? _atmosphereSystem;
  21. public EntityUid? Grid { get; private set; }
  22. public override void Initialize(Node sourceNode, IEntityManager entMan)
  23. {
  24. base.Initialize(sourceNode, entMan);
  25. Grid = entMan.GetComponent<TransformComponent>(sourceNode.Owner).GridUid;
  26. if (Grid == null)
  27. {
  28. // This is probably due to a cannister or something like that being spawned in space.
  29. return;
  30. }
  31. _atmosphereSystem = entMan.EntitySysManager.GetEntitySystem<AtmosphereSystem>();
  32. _atmosphereSystem.AddPipeNet(Grid.Value, this);
  33. }
  34. public void Update()
  35. {
  36. _atmosphereSystem?.React(Air, this);
  37. }
  38. public override void LoadNodes(List<Node> groupNodes)
  39. {
  40. base.LoadNodes(groupNodes);
  41. foreach (var node in groupNodes)
  42. {
  43. var pipeNode = (PipeNode) node;
  44. Air.Volume += pipeNode.Volume;
  45. }
  46. }
  47. public override void RemoveNode(Node node)
  48. {
  49. base.RemoveNode(node);
  50. // if the node is simply being removed into a separate group, we do nothing, as gas redistribution will be
  51. // handled by AfterRemake(). But if it is being deleted, we actually want to remove the gas stored in this node.
  52. if (!node.Deleting || node is not PipeNode pipe)
  53. return;
  54. Air.Multiply(1f - pipe.Volume / Air.Volume);
  55. Air.Volume -= pipe.Volume;
  56. }
  57. public override void AfterRemake(IEnumerable<IGrouping<INodeGroup?, Node>> newGroups)
  58. {
  59. RemoveFromGridAtmos();
  60. var newAir = new List<GasMixture>(newGroups.Count());
  61. foreach (var newGroup in newGroups)
  62. {
  63. if (newGroup.Key is IPipeNet newPipeNet)
  64. newAir.Add(newPipeNet.Air);
  65. }
  66. _atmosphereSystem?.DivideInto(Air, newAir);
  67. }
  68. private void RemoveFromGridAtmos()
  69. {
  70. if (Grid == null)
  71. return;
  72. _atmosphereSystem?.RemovePipeNet(Grid.Value, this);
  73. }
  74. public override string GetDebugData()
  75. {
  76. return @$"Pressure: { Air.Pressure:G3}
  77. Temperature: {Air.Temperature:G3}
  78. Volume: {Air.Volume:G3}";
  79. }
  80. }
  81. }