AtmosUnsafeUnanchorSystem.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using Content.Server.Atmos.EntitySystems;
  2. using Content.Server.Atmos.Piping.Components;
  3. using Content.Server.NodeContainer;
  4. using Content.Server.NodeContainer.EntitySystems;
  5. using Content.Server.NodeContainer.Nodes;
  6. using Content.Server.Popups;
  7. using Content.Shared.Atmos;
  8. using Content.Shared.Construction.Components;
  9. using Content.Shared.Destructible;
  10. using Content.Shared.Popups;
  11. using JetBrains.Annotations;
  12. namespace Content.Server.Atmos.Piping.EntitySystems
  13. {
  14. [UsedImplicitly]
  15. public sealed class AtmosUnsafeUnanchorSystem : EntitySystem
  16. {
  17. [Dependency] private readonly AtmosphereSystem _atmosphere = default!;
  18. [Dependency] private readonly NodeGroupSystem _group = default!;
  19. [Dependency] private readonly PopupSystem _popup = default!;
  20. public override void Initialize()
  21. {
  22. SubscribeLocalEvent<AtmosUnsafeUnanchorComponent, UserUnanchoredEvent>(OnUserUnanchored);
  23. SubscribeLocalEvent<AtmosUnsafeUnanchorComponent, UnanchorAttemptEvent>(OnUnanchorAttempt);
  24. SubscribeLocalEvent<AtmosUnsafeUnanchorComponent, BreakageEventArgs>(OnBreak);
  25. }
  26. private void OnUnanchorAttempt(EntityUid uid, AtmosUnsafeUnanchorComponent component, UnanchorAttemptEvent args)
  27. {
  28. if (!component.Enabled || !EntityManager.TryGetComponent(uid, out NodeContainerComponent? nodes))
  29. return;
  30. if (_atmosphere.GetContainingMixture(uid, true) is not {} environment)
  31. return;
  32. foreach (var node in nodes.Nodes.Values)
  33. {
  34. if (node is not PipeNode pipe)
  35. continue;
  36. if (pipe.Air.Pressure - environment.Pressure > 2 * Atmospherics.OneAtmosphere)
  37. {
  38. args.Delay += 2f;
  39. _popup.PopupEntity(Loc.GetString("comp-atmos-unsafe-unanchor-warning"), pipe.Owner,
  40. args.User, PopupType.MediumCaution);
  41. return; // Show the warning only once.
  42. }
  43. }
  44. }
  45. // When unanchoring a pipe, leak the gas that was inside the pipe element.
  46. // At this point the pipe has been scheduled to be removed from the group, but that won't happen until the next Update() call in NodeGroupSystem,
  47. // so we have to force an update.
  48. // This way the gas inside other connected pipes stays unchanged, while the removed pipe is completely emptied.
  49. private void OnUserUnanchored(EntityUid uid, AtmosUnsafeUnanchorComponent component, UserUnanchoredEvent args)
  50. {
  51. if (component.Enabled)
  52. {
  53. _group.ForceUpdate();
  54. LeakGas(uid);
  55. }
  56. }
  57. private void OnBreak(EntityUid uid, AtmosUnsafeUnanchorComponent component, BreakageEventArgs args)
  58. {
  59. LeakGas(uid, false);
  60. // Can't use DoActsBehavior["Destruction"] in the same trigger because that would prevent us
  61. // from leaking. So we make up for this by queueing deletion here.
  62. QueueDel(uid);
  63. }
  64. /// <summary>
  65. /// Leak gas from the uid's NodeContainer into the tile atmosphere.
  66. /// Setting removeFromPipe to false will duplicate the gas inside the pipe intead of moving it.
  67. /// This is needed to properly handle the gas in the pipe getting deleted with the pipe.
  68. /// </summary>
  69. public void LeakGas(EntityUid uid, bool removeFromPipe = true)
  70. {
  71. if (!EntityManager.TryGetComponent(uid, out NodeContainerComponent? nodes))
  72. return;
  73. if (_atmosphere.GetContainingMixture(uid, true, true) is not { } environment)
  74. environment = GasMixture.SpaceGas;
  75. var buffer = new GasMixture();
  76. foreach (var node in nodes.Nodes.Values)
  77. {
  78. if (node is not PipeNode pipe)
  79. continue;
  80. if (removeFromPipe)
  81. _atmosphere.Merge(buffer, pipe.Air.RemoveVolume(pipe.Volume));
  82. else
  83. {
  84. var copy = new GasMixture(pipe.Air); //clone, then remove to keep the original untouched
  85. _atmosphere.Merge(buffer, copy.RemoveVolume(pipe.Volume));
  86. }
  87. }
  88. _atmosphere.Merge(environment, buffer);
  89. }
  90. }
  91. }