GasVolumePumpSystem.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. using Content.Server.Administration.Logs;
  2. using Content.Server.Atmos.EntitySystems;
  3. using Content.Server.Atmos.Monitor.Systems;
  4. using Content.Server.Atmos.Piping.Binary.Components;
  5. using Content.Server.Atmos.Piping.Components;
  6. using Content.Server.DeviceNetwork;
  7. using Content.Server.DeviceNetwork.Components;
  8. using Content.Server.DeviceNetwork.Systems;
  9. using Content.Server.NodeContainer.EntitySystems;
  10. using Content.Server.NodeContainer.Nodes;
  11. using Content.Server.Power.Components;
  12. using Content.Shared.Atmos.Piping.Binary.Components;
  13. using Content.Shared.Atmos.Piping.Components;
  14. using Content.Shared.Atmos.Visuals;
  15. using Content.Shared.Audio;
  16. using Content.Shared.Database;
  17. using Content.Shared.DeviceNetwork;
  18. using Content.Shared.Examine;
  19. using Content.Shared.Interaction;
  20. using Content.Shared.Popups;
  21. using Content.Shared.Power;
  22. using JetBrains.Annotations;
  23. using Robust.Server.GameObjects;
  24. using Robust.Shared.Player;
  25. namespace Content.Server.Atmos.Piping.Binary.EntitySystems
  26. {
  27. [UsedImplicitly]
  28. public sealed class GasVolumePumpSystem : EntitySystem
  29. {
  30. [Dependency] private readonly IAdminLogManager _adminLogger = default!;
  31. [Dependency] private readonly AtmosphereSystem _atmosphereSystem = default!;
  32. [Dependency] private readonly UserInterfaceSystem _userInterfaceSystem = default!;
  33. [Dependency] private readonly SharedAmbientSoundSystem _ambientSoundSystem = default!;
  34. [Dependency] private readonly SharedAppearanceSystem _appearance = default!;
  35. [Dependency] private readonly NodeContainerSystem _nodeContainer = default!;
  36. [Dependency] private readonly DeviceNetworkSystem _deviceNetwork = default!;
  37. [Dependency] private readonly SharedPopupSystem _popup = default!;
  38. public override void Initialize()
  39. {
  40. base.Initialize();
  41. SubscribeLocalEvent<GasVolumePumpComponent, ComponentInit>(OnInit);
  42. SubscribeLocalEvent<GasVolumePumpComponent, AtmosDeviceUpdateEvent>(OnVolumePumpUpdated);
  43. SubscribeLocalEvent<GasVolumePumpComponent, AtmosDeviceDisabledEvent>(OnVolumePumpLeaveAtmosphere);
  44. SubscribeLocalEvent<GasVolumePumpComponent, ExaminedEvent>(OnExamined);
  45. SubscribeLocalEvent<GasVolumePumpComponent, ActivateInWorldEvent>(OnPumpActivate);
  46. SubscribeLocalEvent<GasVolumePumpComponent, PowerChangedEvent>(OnPowerChanged);
  47. // Bound UI subscriptions
  48. SubscribeLocalEvent<GasVolumePumpComponent, GasVolumePumpChangeTransferRateMessage>(OnTransferRateChangeMessage);
  49. SubscribeLocalEvent<GasVolumePumpComponent, GasVolumePumpToggleStatusMessage>(OnToggleStatusMessage);
  50. SubscribeLocalEvent<GasVolumePumpComponent, DeviceNetworkPacketEvent>(OnPacketRecv);
  51. }
  52. private void OnInit(EntityUid uid, GasVolumePumpComponent pump, ComponentInit args)
  53. {
  54. UpdateAppearance(uid, pump);
  55. }
  56. private void OnExamined(EntityUid uid, GasVolumePumpComponent pump, ExaminedEvent args)
  57. {
  58. if (!EntityManager.GetComponent<TransformComponent>(uid).Anchored || !args.IsInDetailsRange) // Not anchored? Out of range? No status.
  59. return;
  60. if (Loc.TryGetString("gas-volume-pump-system-examined", out var str,
  61. ("statusColor", "lightblue"), // TODO: change with volume?
  62. ("rate", pump.TransferRate)
  63. ))
  64. args.PushMarkup(str);
  65. }
  66. private void OnPowerChanged(EntityUid uid, GasVolumePumpComponent component, ref PowerChangedEvent args)
  67. {
  68. UpdateAppearance(uid, component);
  69. }
  70. private void OnVolumePumpUpdated(EntityUid uid, GasVolumePumpComponent pump, ref AtmosDeviceUpdateEvent args)
  71. {
  72. if (!pump.Enabled ||
  73. (TryComp<ApcPowerReceiverComponent>(uid, out var power) && !power.Powered) ||
  74. !_nodeContainer.TryGetNodes(uid, pump.InletName, pump.OutletName, out PipeNode? inlet, out PipeNode? outlet))
  75. {
  76. _ambientSoundSystem.SetAmbience(uid, false);
  77. return;
  78. }
  79. var inputStartingPressure = inlet.Air.Pressure;
  80. var outputStartingPressure = outlet.Air.Pressure;
  81. var previouslyBlocked = pump.Blocked;
  82. pump.Blocked = false;
  83. // Pump mechanism won't do anything if the pressure is too high/too low unless you overclock it.
  84. if ((inputStartingPressure < pump.LowerThreshold) || (outputStartingPressure > pump.HigherThreshold) && !pump.Overclocked)
  85. {
  86. pump.Blocked = true;
  87. }
  88. // Overclocked pumps can only force gas a certain amount.
  89. if ((outputStartingPressure - inputStartingPressure > pump.OverclockThreshold) && pump.Overclocked)
  90. {
  91. pump.Blocked = true;
  92. }
  93. if (previouslyBlocked != pump.Blocked)
  94. UpdateAppearance(uid, pump);
  95. if (pump.Blocked)
  96. return;
  97. // We multiply the transfer rate in L/s by the seconds passed since the last process to get the liters.
  98. var removed = inlet.Air.RemoveVolume(pump.TransferRate * _atmosphereSystem.PumpSpeedup() * args.dt);
  99. // Some of the gas from the mixture leaks when overclocked.
  100. if (pump.Overclocked)
  101. {
  102. var tile = _atmosphereSystem.GetTileMixture(uid, excite: true);
  103. if (tile != null)
  104. {
  105. var leaked = removed.RemoveRatio(pump.LeakRatio);
  106. _atmosphereSystem.Merge(tile, leaked);
  107. }
  108. }
  109. pump.LastMolesTransferred = removed.TotalMoles;
  110. _atmosphereSystem.Merge(outlet.Air, removed);
  111. _ambientSoundSystem.SetAmbience(uid, removed.TotalMoles > 0f);
  112. }
  113. private void OnVolumePumpLeaveAtmosphere(EntityUid uid, GasVolumePumpComponent pump, ref AtmosDeviceDisabledEvent args)
  114. {
  115. pump.Enabled = false;
  116. UpdateAppearance(uid, pump);
  117. DirtyUI(uid, pump);
  118. _userInterfaceSystem.CloseUi(uid, GasVolumePumpUiKey.Key);
  119. }
  120. private void OnPumpActivate(EntityUid uid, GasVolumePumpComponent pump, ActivateInWorldEvent args)
  121. {
  122. if (args.Handled || !args.Complex)
  123. return;
  124. if (!EntityManager.TryGetComponent(args.User, out ActorComponent? actor))
  125. return;
  126. if (Transform(uid).Anchored)
  127. {
  128. _userInterfaceSystem.OpenUi(uid, GasVolumePumpUiKey.Key, actor.PlayerSession);
  129. DirtyUI(uid, pump);
  130. }
  131. else
  132. {
  133. _popup.PopupCursor(Loc.GetString("comp-gas-pump-ui-needs-anchor"), args.User);
  134. }
  135. args.Handled = true;
  136. }
  137. private void OnToggleStatusMessage(EntityUid uid, GasVolumePumpComponent pump, GasVolumePumpToggleStatusMessage args)
  138. {
  139. pump.Enabled = args.Enabled;
  140. _adminLogger.Add(LogType.AtmosPowerChanged, LogImpact.Medium,
  141. $"{ToPrettyString(args.Actor):player} set the power on {ToPrettyString(uid):device} to {args.Enabled}");
  142. DirtyUI(uid, pump);
  143. UpdateAppearance(uid, pump);
  144. }
  145. private void OnTransferRateChangeMessage(EntityUid uid, GasVolumePumpComponent pump, GasVolumePumpChangeTransferRateMessage args)
  146. {
  147. pump.TransferRate = Math.Clamp(args.TransferRate, 0f, pump.MaxTransferRate);
  148. _adminLogger.Add(LogType.AtmosVolumeChanged, LogImpact.Medium,
  149. $"{ToPrettyString(args.Actor):player} set the transfer rate on {ToPrettyString(uid):device} to {args.TransferRate}");
  150. DirtyUI(uid, pump);
  151. }
  152. private void DirtyUI(EntityUid uid, GasVolumePumpComponent? pump)
  153. {
  154. if (!Resolve(uid, ref pump))
  155. return;
  156. _userInterfaceSystem.SetUiState(uid, GasVolumePumpUiKey.Key,
  157. new GasVolumePumpBoundUserInterfaceState(Name(uid), pump.TransferRate, pump.Enabled));
  158. }
  159. private void UpdateAppearance(EntityUid uid, GasVolumePumpComponent? pump = null, AppearanceComponent? appearance = null)
  160. {
  161. if (!Resolve(uid, ref pump, ref appearance, false))
  162. return;
  163. bool pumpOn = pump.Enabled && (TryComp<ApcPowerReceiverComponent>(uid, out var power) && power.Powered);
  164. if (!pumpOn)
  165. _appearance.SetData(uid, GasVolumePumpVisuals.State, GasVolumePumpState.Off, appearance);
  166. else if (pump.Blocked)
  167. _appearance.SetData(uid, GasVolumePumpVisuals.State, GasVolumePumpState.Blocked, appearance);
  168. else
  169. _appearance.SetData(uid, GasVolumePumpVisuals.State, GasVolumePumpState.On, appearance);
  170. }
  171. private void OnPacketRecv(EntityUid uid, GasVolumePumpComponent component, DeviceNetworkPacketEvent args)
  172. {
  173. if (!TryComp(uid, out DeviceNetworkComponent? netConn)
  174. || !args.Data.TryGetValue(DeviceNetworkConstants.Command, out var cmd))
  175. return;
  176. var payload = new NetworkPayload();
  177. switch (cmd)
  178. {
  179. case AtmosDeviceNetworkSystem.SyncData:
  180. payload.Add(DeviceNetworkConstants.Command, AtmosDeviceNetworkSystem.SyncData);
  181. payload.Add(AtmosDeviceNetworkSystem.SyncData, new GasVolumePumpData(component.LastMolesTransferred));
  182. _deviceNetwork.QueuePacket(uid, args.SenderAddress, payload, device: netConn);
  183. return;
  184. }
  185. }
  186. }
  187. }