GasCanisterSystem.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. using Content.Server.Administration.Logs;
  2. using Content.Server.Atmos.Components;
  3. using Content.Server.Atmos.EntitySystems;
  4. using Content.Server.Atmos.Piping.Components;
  5. using Content.Server.Atmos.Piping.Unary.Components;
  6. using Content.Server.Cargo.Systems;
  7. using Content.Server.NodeContainer;
  8. using Content.Server.NodeContainer.EntitySystems;
  9. using Content.Server.NodeContainer.NodeGroups;
  10. using Content.Server.NodeContainer.Nodes;
  11. using Content.Server.Popups;
  12. using Content.Shared.Atmos;
  13. using Content.Shared.Atmos.Piping.Binary.Components;
  14. using Content.Shared.Containers.ItemSlots;
  15. using Content.Shared.Database;
  16. using Content.Shared.Interaction;
  17. using Content.Shared.Lock;
  18. using Robust.Server.GameObjects;
  19. using Robust.Shared.Audio.Systems;
  20. using Robust.Shared.Containers;
  21. using Robust.Shared.Player;
  22. namespace Content.Server.Atmos.Piping.Unary.EntitySystems;
  23. public sealed class GasCanisterSystem : EntitySystem
  24. {
  25. [Dependency] private readonly AtmosphereSystem _atmos = default!;
  26. [Dependency] private readonly IAdminLogManager _adminLogger = default!;
  27. [Dependency] private readonly SharedAppearanceSystem _appearance = default!;
  28. [Dependency] private readonly SharedAudioSystem _audio = default!;
  29. [Dependency] private readonly PopupSystem _popup = default!;
  30. [Dependency] private readonly UserInterfaceSystem _ui = default!;
  31. [Dependency] private readonly NodeContainerSystem _nodeContainer = default!;
  32. [Dependency] private readonly ItemSlotsSystem _slots = default!;
  33. public override void Initialize()
  34. {
  35. base.Initialize();
  36. SubscribeLocalEvent<GasCanisterComponent, ComponentStartup>(OnCanisterStartup);
  37. SubscribeLocalEvent<GasCanisterComponent, AtmosDeviceUpdateEvent>(OnCanisterUpdated);
  38. SubscribeLocalEvent<GasCanisterComponent, ActivateInWorldEvent>(OnCanisterActivate, after: new[] { typeof(LockSystem) });
  39. SubscribeLocalEvent<GasCanisterComponent, InteractHandEvent>(OnCanisterInteractHand);
  40. SubscribeLocalEvent<GasCanisterComponent, ItemSlotInsertAttemptEvent>(OnCanisterInsertAttempt);
  41. SubscribeLocalEvent<GasCanisterComponent, EntInsertedIntoContainerMessage>(OnCanisterContainerInserted);
  42. SubscribeLocalEvent<GasCanisterComponent, EntRemovedFromContainerMessage>(OnCanisterContainerRemoved);
  43. SubscribeLocalEvent<GasCanisterComponent, PriceCalculationEvent>(CalculateCanisterPrice);
  44. SubscribeLocalEvent<GasCanisterComponent, GasAnalyzerScanEvent>(OnAnalyzed);
  45. // Bound UI subscriptions
  46. SubscribeLocalEvent<GasCanisterComponent, GasCanisterHoldingTankEjectMessage>(OnHoldingTankEjectMessage);
  47. SubscribeLocalEvent<GasCanisterComponent, GasCanisterChangeReleasePressureMessage>(OnCanisterChangeReleasePressure);
  48. SubscribeLocalEvent<GasCanisterComponent, GasCanisterChangeReleaseValveMessage>(OnCanisterChangeReleaseValve);
  49. }
  50. /// <summary>
  51. /// Completely dumps the content of the canister into the world.
  52. /// </summary>
  53. public void PurgeContents(EntityUid uid, GasCanisterComponent? canister = null, TransformComponent? transform = null)
  54. {
  55. if (!Resolve(uid, ref canister, ref transform))
  56. return;
  57. var environment = _atmos.GetContainingMixture((uid, transform), false, true);
  58. if (environment is not null)
  59. _atmos.Merge(environment, canister.Air);
  60. _adminLogger.Add(LogType.CanisterPurged, LogImpact.Medium, $"Canister {ToPrettyString(uid):canister} purged its contents of {canister.Air:gas} into the environment.");
  61. canister.Air.Clear();
  62. }
  63. private void OnCanisterStartup(EntityUid uid, GasCanisterComponent comp, ComponentStartup args)
  64. {
  65. // Ensure container
  66. _slots.AddItemSlot(uid, comp.ContainerName, comp.GasTankSlot);
  67. }
  68. private void DirtyUI(EntityUid uid,
  69. GasCanisterComponent? canister = null, NodeContainerComponent? nodeContainer = null)
  70. {
  71. if (!Resolve(uid, ref canister, ref nodeContainer))
  72. return;
  73. var portStatus = false;
  74. string? tankLabel = null;
  75. var tankPressure = 0f;
  76. if (_nodeContainer.TryGetNode(nodeContainer, canister.PortName, out PipeNode? portNode) && portNode.NodeGroup?.Nodes.Count > 1)
  77. portStatus = true;
  78. if (canister.GasTankSlot.Item != null)
  79. {
  80. var tank = canister.GasTankSlot.Item.Value;
  81. var tankComponent = Comp<GasTankComponent>(tank);
  82. tankLabel = Name(tank);
  83. tankPressure = tankComponent.Air.Pressure;
  84. }
  85. _ui.SetUiState(uid, GasCanisterUiKey.Key,
  86. new GasCanisterBoundUserInterfaceState(Name(uid),
  87. canister.Air.Pressure, portStatus, tankLabel, tankPressure, canister.ReleasePressure,
  88. canister.ReleaseValve, canister.MinReleasePressure, canister.MaxReleasePressure));
  89. }
  90. private void OnHoldingTankEjectMessage(EntityUid uid, GasCanisterComponent canister, GasCanisterHoldingTankEjectMessage args)
  91. {
  92. if (canister.GasTankSlot.Item == null)
  93. return;
  94. var item = canister.GasTankSlot.Item;
  95. _slots.TryEjectToHands(uid, canister.GasTankSlot, args.Actor);
  96. if (canister.ReleaseValve)
  97. {
  98. _adminLogger.Add(LogType.CanisterTankEjected, LogImpact.High, $"Player {ToPrettyString(args.Actor):player} ejected tank {ToPrettyString(item):tank} from {ToPrettyString(uid):canister} while the valve was open, releasing [{GetContainedGasesString((uid, canister))}] to atmosphere");
  99. }
  100. else
  101. {
  102. _adminLogger.Add(LogType.CanisterTankEjected, LogImpact.Medium, $"Player {ToPrettyString(args.Actor):player} ejected tank {ToPrettyString(item):tank} from {ToPrettyString(uid):canister}");
  103. }
  104. }
  105. private void OnCanisterChangeReleasePressure(EntityUid uid, GasCanisterComponent canister, GasCanisterChangeReleasePressureMessage args)
  106. {
  107. var pressure = Math.Clamp(args.Pressure, canister.MinReleasePressure, canister.MaxReleasePressure);
  108. _adminLogger.Add(LogType.CanisterPressure, LogImpact.Medium, $"{ToPrettyString(args.Actor):player} set the release pressure on {ToPrettyString(uid):canister} to {args.Pressure}");
  109. canister.ReleasePressure = pressure;
  110. DirtyUI(uid, canister);
  111. }
  112. private void OnCanisterChangeReleaseValve(EntityUid uid, GasCanisterComponent canister, GasCanisterChangeReleaseValveMessage args)
  113. {
  114. // filling a jetpack with plasma is less important than filling a room with it
  115. var hasItem = canister.GasTankSlot.HasItem;
  116. var impact = hasItem ? LogImpact.Medium : LogImpact.High;
  117. _adminLogger.Add(
  118. LogType.CanisterValve,
  119. impact,
  120. $"{ToPrettyString(args.Actor):player} {(args.Valve ? "opened" : "closed")} the valve on {ToPrettyString(uid):canister} to {(hasItem ? "inserted tank" : "environment")} while it contained [{GetContainedGasesString((uid, canister))}]");
  121. canister.ReleaseValve = args.Valve;
  122. DirtyUI(uid, canister);
  123. }
  124. private static string GetContainedGasesString(Entity<GasCanisterComponent> canister)
  125. {
  126. return string.Join(", ", canister.Comp.Air);
  127. }
  128. private void OnCanisterUpdated(EntityUid uid, GasCanisterComponent canister, ref AtmosDeviceUpdateEvent args)
  129. {
  130. _atmos.React(canister.Air, canister);
  131. if (!TryComp<NodeContainerComponent>(uid, out var nodeContainer)
  132. || !TryComp<AppearanceComponent>(uid, out var appearance))
  133. return;
  134. if (!_nodeContainer.TryGetNode(nodeContainer, canister.PortName, out PortablePipeNode? portNode))
  135. return;
  136. if (portNode.NodeGroup is PipeNet {NodeCount: > 1} net)
  137. {
  138. MixContainerWithPipeNet(canister.Air, net.Air);
  139. }
  140. // Release valve is open, release gas.
  141. if (canister.ReleaseValve)
  142. {
  143. if (canister.GasTankSlot.Item != null)
  144. {
  145. var gasTank = Comp<GasTankComponent>(canister.GasTankSlot.Item.Value);
  146. _atmos.ReleaseGasTo(canister.Air, gasTank.Air, canister.ReleasePressure);
  147. }
  148. else
  149. {
  150. var environment = _atmos.GetContainingMixture(uid, args.Grid, args.Map, false, true);
  151. _atmos.ReleaseGasTo(canister.Air, environment, canister.ReleasePressure);
  152. }
  153. }
  154. // If last pressure is very close to the current pressure, do nothing.
  155. if (MathHelper.CloseToPercent(canister.Air.Pressure, canister.LastPressure))
  156. return;
  157. DirtyUI(uid, canister, nodeContainer);
  158. canister.LastPressure = canister.Air.Pressure;
  159. if (canister.Air.Pressure < 10)
  160. {
  161. _appearance.SetData(uid, GasCanisterVisuals.PressureState, 0, appearance);
  162. }
  163. else if (canister.Air.Pressure < Atmospherics.OneAtmosphere)
  164. {
  165. _appearance.SetData(uid, GasCanisterVisuals.PressureState, 1, appearance);
  166. }
  167. else if (canister.Air.Pressure < (15 * Atmospherics.OneAtmosphere))
  168. {
  169. _appearance.SetData(uid, GasCanisterVisuals.PressureState, 2, appearance);
  170. }
  171. else
  172. {
  173. _appearance.SetData(uid, GasCanisterVisuals.PressureState, 3, appearance);
  174. }
  175. }
  176. private void OnCanisterActivate(EntityUid uid, GasCanisterComponent component, ActivateInWorldEvent args)
  177. {
  178. if (!args.Complex)
  179. return;
  180. if (!TryComp<ActorComponent>(args.User, out var actor))
  181. return;
  182. if (CheckLocked(uid, component, args.User))
  183. return;
  184. // Needs to be here so the locked check still happens if the canister
  185. // is locked and you don't have permissions
  186. if (args.Handled)
  187. return;
  188. _ui.OpenUi(uid, GasCanisterUiKey.Key, actor.PlayerSession);
  189. args.Handled = true;
  190. }
  191. private void OnCanisterInteractHand(EntityUid uid, GasCanisterComponent component, InteractHandEvent args)
  192. {
  193. if (!TryComp<ActorComponent>(args.User, out var actor))
  194. return;
  195. if (CheckLocked(uid, component, args.User))
  196. return;
  197. _ui.OpenUi(uid, GasCanisterUiKey.Key, actor.PlayerSession);
  198. args.Handled = true;
  199. }
  200. private void OnCanisterInsertAttempt(EntityUid uid, GasCanisterComponent component, ref ItemSlotInsertAttemptEvent args)
  201. {
  202. if (args.Slot.ID != component.ContainerName || args.User == null)
  203. return;
  204. if (!TryComp<GasTankComponent>(args.Item, out var gasTank) || gasTank.IsValveOpen)
  205. {
  206. args.Cancelled = true;
  207. return;
  208. }
  209. // Preventing inserting a tank since if its locked you cant remove it.
  210. if (!CheckLocked(uid, component, args.User.Value))
  211. return;
  212. args.Cancelled = true;
  213. }
  214. private void OnCanisterContainerInserted(EntityUid uid, GasCanisterComponent component, EntInsertedIntoContainerMessage args)
  215. {
  216. if (args.Container.ID != component.ContainerName)
  217. return;
  218. DirtyUI(uid, component);
  219. _appearance.SetData(uid, GasCanisterVisuals.TankInserted, true);
  220. }
  221. private void OnCanisterContainerRemoved(EntityUid uid, GasCanisterComponent component, EntRemovedFromContainerMessage args)
  222. {
  223. if (args.Container.ID != component.ContainerName)
  224. return;
  225. DirtyUI(uid, component);
  226. _appearance.SetData(uid, GasCanisterVisuals.TankInserted, false);
  227. }
  228. /// <summary>
  229. /// Mix air from a gas container into a pipe net.
  230. /// Useful for anything that uses connector ports.
  231. /// </summary>
  232. public void MixContainerWithPipeNet(GasMixture containerAir, GasMixture pipeNetAir)
  233. {
  234. var buffer = new GasMixture(pipeNetAir.Volume + containerAir.Volume);
  235. _atmos.Merge(buffer, pipeNetAir);
  236. _atmos.Merge(buffer, containerAir);
  237. pipeNetAir.Clear();
  238. _atmos.Merge(pipeNetAir, buffer);
  239. pipeNetAir.Multiply(pipeNetAir.Volume / buffer.Volume);
  240. containerAir.Clear();
  241. _atmos.Merge(containerAir, buffer);
  242. containerAir.Multiply(containerAir.Volume / buffer.Volume);
  243. }
  244. private void CalculateCanisterPrice(EntityUid uid, GasCanisterComponent component, ref PriceCalculationEvent args)
  245. {
  246. args.Price += _atmos.GetPrice(component.Air);
  247. }
  248. /// <summary>
  249. /// Returns the gas mixture for the gas analyzer
  250. /// </summary>
  251. private void OnAnalyzed(EntityUid uid, GasCanisterComponent canisterComponent, GasAnalyzerScanEvent args)
  252. {
  253. args.GasMixtures ??= new List<(string, GasMixture?)>();
  254. args.GasMixtures.Add((Name(uid), canisterComponent.Air));
  255. // if a tank is inserted show it on the analyzer as well
  256. if (canisterComponent.GasTankSlot.Item != null)
  257. {
  258. var tank = canisterComponent.GasTankSlot.Item.Value;
  259. var tankComponent = Comp<GasTankComponent>(tank);
  260. args.GasMixtures.Add((Name(tank), tankComponent.Air));
  261. }
  262. }
  263. /// <summary>
  264. /// Check if the canister is locked, playing its sound and popup if so.
  265. /// </summary>
  266. /// <returns>
  267. /// True if locked, false otherwise.
  268. /// </returns>
  269. private bool CheckLocked(EntityUid uid, GasCanisterComponent comp, EntityUid user)
  270. {
  271. if (TryComp<LockComponent>(uid, out var lockComp) && lockComp.Locked)
  272. {
  273. _popup.PopupEntity(Loc.GetString("gas-canister-popup-denied"), uid, user);
  274. _audio.PlayPvs(comp.AccessDeniedSound, uid);
  275. return true;
  276. }
  277. return false;
  278. }
  279. }