GasVentScrubberSystem.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. using Content.Server.Atmos.EntitySystems;
  2. using Content.Server.Atmos.Monitor.Components;
  3. using Content.Server.Atmos.Monitor.Systems;
  4. using Content.Server.Atmos.Piping.Components;
  5. using Content.Server.Atmos.Piping.Unary.Components;
  6. using Content.Server.DeviceNetwork;
  7. using Content.Server.DeviceNetwork.Components;
  8. using Content.Server.DeviceNetwork.Systems;
  9. using Content.Server.NodeContainer;
  10. using Content.Server.NodeContainer.EntitySystems;
  11. using Content.Server.NodeContainer.Nodes;
  12. using Content.Server.Power.Components;
  13. using Content.Server.Power.EntitySystems;
  14. using Content.Shared.Administration.Logs;
  15. using Content.Shared.Atmos;
  16. using Content.Shared.Atmos.Piping.Unary.Visuals;
  17. using Content.Shared.Atmos.Monitor;
  18. using Content.Shared.Atmos.Piping.Components;
  19. using Content.Shared.Atmos.Piping.Unary.Components;
  20. using Content.Shared.Audio;
  21. using Content.Shared.Database;
  22. using Content.Shared.DeviceNetwork;
  23. using Content.Shared.Power;
  24. using Content.Shared.Tools.Systems;
  25. using JetBrains.Annotations;
  26. using Robust.Server.GameObjects;
  27. namespace Content.Server.Atmos.Piping.Unary.EntitySystems
  28. {
  29. [UsedImplicitly]
  30. public sealed class GasVentScrubberSystem : EntitySystem
  31. {
  32. [Dependency] private readonly ISharedAdminLogManager _adminLogger = default!;
  33. [Dependency] private readonly AtmosphereSystem _atmosphereSystem = default!;
  34. [Dependency] private readonly DeviceNetworkSystem _deviceNetSystem = default!;
  35. [Dependency] private readonly NodeContainerSystem _nodeContainer = default!;
  36. [Dependency] private readonly SharedAmbientSoundSystem _ambientSoundSystem = default!;
  37. [Dependency] private readonly TransformSystem _transformSystem = default!;
  38. [Dependency] private readonly SharedAppearanceSystem _appearance = default!;
  39. [Dependency] private readonly WeldableSystem _weldable = default!;
  40. [Dependency] private readonly PowerReceiverSystem _powerReceiverSystem = default!;
  41. public override void Initialize()
  42. {
  43. base.Initialize();
  44. SubscribeLocalEvent<GasVentScrubberComponent, AtmosDeviceUpdateEvent>(OnVentScrubberUpdated);
  45. SubscribeLocalEvent<GasVentScrubberComponent, AtmosDeviceEnabledEvent>(OnVentScrubberEnterAtmosphere);
  46. SubscribeLocalEvent<GasVentScrubberComponent, AtmosDeviceDisabledEvent>(OnVentScrubberLeaveAtmosphere);
  47. SubscribeLocalEvent<GasVentScrubberComponent, AtmosAlarmEvent>(OnAtmosAlarm);
  48. SubscribeLocalEvent<GasVentScrubberComponent, PowerChangedEvent>(OnPowerChanged);
  49. SubscribeLocalEvent<GasVentScrubberComponent, DeviceNetworkPacketEvent>(OnPacketRecv);
  50. SubscribeLocalEvent<GasVentScrubberComponent, WeldableChangedEvent>(OnWeldChanged);
  51. }
  52. private void OnVentScrubberUpdated(EntityUid uid, GasVentScrubberComponent scrubber, ref AtmosDeviceUpdateEvent args)
  53. {
  54. if (_weldable.IsWelded(uid))
  55. return;
  56. var timeDelta = args.dt;
  57. if (!_powerReceiverSystem.IsPowered(uid))
  58. return;
  59. if (!scrubber.Enabled || !_nodeContainer.TryGetNode(uid, scrubber.OutletName, out PipeNode? outlet))
  60. return;
  61. if (args.Grid is not {} grid)
  62. return;
  63. var position = _transformSystem.GetGridTilePositionOrDefault(uid);
  64. var environment = _atmosphereSystem.GetTileMixture(grid, args.Map, position, true);
  65. Scrub(timeDelta, scrubber, environment, outlet);
  66. if (!scrubber.WideNet)
  67. return;
  68. // Scrub adjacent tiles too.
  69. var enumerator = _atmosphereSystem.GetAdjacentTileMixtures(grid, position, false, true);
  70. while (enumerator.MoveNext(out var adjacent))
  71. {
  72. Scrub(timeDelta, scrubber, adjacent, outlet);
  73. }
  74. }
  75. private void OnVentScrubberLeaveAtmosphere(EntityUid uid, GasVentScrubberComponent component,
  76. AtmosDeviceDisabledEvent args) => UpdateState(uid, component);
  77. private void OnVentScrubberEnterAtmosphere(EntityUid uid, GasVentScrubberComponent component,
  78. AtmosDeviceEnabledEvent args) => UpdateState(uid, component);
  79. private void Scrub(float timeDelta, GasVentScrubberComponent scrubber, GasMixture? tile, PipeNode outlet)
  80. {
  81. Scrub(timeDelta, scrubber.TransferRate*_atmosphereSystem.PumpSpeedup(), scrubber.PumpDirection, scrubber.FilterGases, tile, outlet.Air);
  82. }
  83. /// <summary>
  84. /// True if we were able to scrub, false if we were not.
  85. /// </summary>
  86. public bool Scrub(float timeDelta, float transferRate, ScrubberPumpDirection mode, HashSet<Gas> filterGases, GasMixture? tile, GasMixture destination)
  87. {
  88. // Cannot scrub if tile is null or air-blocked.
  89. if (tile == null
  90. || destination.Pressure >= 50 * Atmospherics.OneAtmosphere) // Cannot scrub if pressure too high.
  91. {
  92. return false;
  93. }
  94. // Take a gas sample.
  95. var ratio = MathF.Min(1f, timeDelta * transferRate / tile.Volume);
  96. var removed = tile.RemoveRatio(ratio);
  97. // Nothing left to remove from the tile.
  98. if (MathHelper.CloseToPercent(removed.TotalMoles, 0f))
  99. return false;
  100. if (mode == ScrubberPumpDirection.Scrubbing)
  101. {
  102. _atmosphereSystem.ScrubInto(removed, destination, filterGases);
  103. // Remix the gases.
  104. _atmosphereSystem.Merge(tile, removed);
  105. }
  106. else if (mode == ScrubberPumpDirection.Siphoning)
  107. {
  108. _atmosphereSystem.Merge(destination, removed);
  109. }
  110. return true;
  111. }
  112. private void OnAtmosAlarm(EntityUid uid, GasVentScrubberComponent component, AtmosAlarmEvent args)
  113. {
  114. if (args.AlarmType == AtmosAlarmType.Danger)
  115. {
  116. component.Enabled = false;
  117. }
  118. else if (args.AlarmType == AtmosAlarmType.Normal)
  119. {
  120. component.Enabled = true;
  121. }
  122. UpdateState(uid, component);
  123. }
  124. private void OnPowerChanged(EntityUid uid, GasVentScrubberComponent component, ref PowerChangedEvent args)
  125. {
  126. UpdateState(uid, component);
  127. }
  128. private void OnPacketRecv(EntityUid uid, GasVentScrubberComponent component, DeviceNetworkPacketEvent args)
  129. {
  130. if (!EntityManager.TryGetComponent(uid, out DeviceNetworkComponent? netConn)
  131. || !args.Data.TryGetValue(DeviceNetworkConstants.Command, out var cmd))
  132. return;
  133. var payload = new NetworkPayload();
  134. switch (cmd)
  135. {
  136. case AtmosDeviceNetworkSystem.SyncData:
  137. payload.Add(DeviceNetworkConstants.Command, AtmosDeviceNetworkSystem.SyncData);
  138. payload.Add(AtmosDeviceNetworkSystem.SyncData, component.ToAirAlarmData());
  139. _deviceNetSystem.QueuePacket(uid, args.SenderAddress, payload, device: netConn);
  140. return;
  141. case DeviceNetworkConstants.CmdSetState:
  142. if (!args.Data.TryGetValue(DeviceNetworkConstants.CmdSetState, out GasVentScrubberData? setData))
  143. break;
  144. var previous = component.ToAirAlarmData();
  145. if (previous.Enabled != setData.Enabled)
  146. {
  147. string enabled = setData.Enabled ? "enabled" : "disabled" ;
  148. _adminLogger.Add(LogType.AtmosDeviceSetting, LogImpact.Medium, $"{ToPrettyString(uid)} {enabled}");
  149. }
  150. // TODO: IgnoreAlarms?
  151. if (previous.PumpDirection != setData.PumpDirection)
  152. _adminLogger.Add(LogType.AtmosDeviceSetting, LogImpact.Medium, $"{ToPrettyString(uid)} direction changed to {setData.PumpDirection}");
  153. // TODO: This is iterating through both sets, it could probably be faster but they're both really small sets anyways
  154. foreach (Gas gas in previous.FilterGases)
  155. if (!setData.FilterGases.Contains(gas))
  156. _adminLogger.Add(LogType.AtmosDeviceSetting, LogImpact.Medium, $"{ToPrettyString(uid)} {gas} filtering disabled");
  157. foreach (Gas gas in setData.FilterGases)
  158. if (!previous.FilterGases.Contains(gas))
  159. _adminLogger.Add(LogType.AtmosDeviceSetting, LogImpact.Medium, $"{ToPrettyString(uid)} {gas} filtering enabled");
  160. if (previous.VolumeRate != setData.VolumeRate)
  161. {
  162. _adminLogger.Add(
  163. LogType.AtmosDeviceSetting,
  164. LogImpact.Medium,
  165. $"{ToPrettyString(uid)} volume rate changed from {previous.VolumeRate} L to {setData.VolumeRate} L"
  166. );
  167. }
  168. if (previous.WideNet != setData.WideNet)
  169. {
  170. string enabled = setData.WideNet ? "enabled" : "disabled" ;
  171. _adminLogger.Add(LogType.AtmosDeviceSetting, LogImpact.Medium, $"{ToPrettyString(uid)} WideNet {enabled}");
  172. }
  173. component.FromAirAlarmData(setData);
  174. UpdateState(uid, component);
  175. return;
  176. }
  177. }
  178. /// <summary>
  179. /// Updates a scrubber's appearance and ambience state.
  180. /// </summary>
  181. private void UpdateState(EntityUid uid, GasVentScrubberComponent scrubber,
  182. AppearanceComponent? appearance = null)
  183. {
  184. if (!Resolve(uid, ref appearance, false))
  185. return;
  186. _ambientSoundSystem.SetAmbience(uid, true);
  187. if (_weldable.IsWelded(uid))
  188. {
  189. _ambientSoundSystem.SetAmbience(uid, false);
  190. _appearance.SetData(uid, ScrubberVisuals.State, ScrubberState.Welded, appearance);
  191. }
  192. else if (!_powerReceiverSystem.IsPowered(uid) || !scrubber.Enabled)
  193. {
  194. _ambientSoundSystem.SetAmbience(uid, false);
  195. _appearance.SetData(uid, ScrubberVisuals.State, ScrubberState.Off, appearance);
  196. }
  197. else if (scrubber.PumpDirection == ScrubberPumpDirection.Scrubbing)
  198. {
  199. _appearance.SetData(uid, ScrubberVisuals.State, scrubber.WideNet ? ScrubberState.WideScrub : ScrubberState.Scrub, appearance);
  200. }
  201. else if (scrubber.PumpDirection == ScrubberPumpDirection.Siphoning)
  202. {
  203. _appearance.SetData(uid, ScrubberVisuals.State, ScrubberState.Siphon, appearance);
  204. }
  205. }
  206. private void OnWeldChanged(EntityUid uid, GasVentScrubberComponent component, ref WeldableChangedEvent args)
  207. {
  208. UpdateState(uid, component);
  209. }
  210. }
  211. }