GasTankSystem.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. using Content.Server.Atmos.Components;
  2. using Content.Server.Body.Components;
  3. using Content.Server.Body.Systems;
  4. using Content.Server.Cargo.Systems;
  5. using Content.Server.Explosion.EntitySystems;
  6. using Content.Shared.UserInterface;
  7. using Content.Shared.Actions;
  8. using Content.Shared.Atmos;
  9. using Content.Shared.Atmos.Components;
  10. using Content.Shared.Examine;
  11. using Content.Shared.Throwing;
  12. using Content.Shared.Toggleable;
  13. using Content.Shared.Verbs;
  14. using JetBrains.Annotations;
  15. using Robust.Server.GameObjects;
  16. using Robust.Shared.Audio;
  17. using Robust.Shared.Audio.Systems;
  18. using Robust.Shared.Containers;
  19. using Robust.Shared.Random;
  20. using Robust.Shared.Configuration;
  21. using Content.Shared.CCVar;
  22. namespace Content.Server.Atmos.EntitySystems
  23. {
  24. [UsedImplicitly]
  25. public sealed class GasTankSystem : EntitySystem
  26. {
  27. [Dependency] private readonly AtmosphereSystem _atmosphereSystem = default!;
  28. [Dependency] private readonly ExplosionSystem _explosions = default!;
  29. [Dependency] private readonly InternalsSystem _internals = default!;
  30. [Dependency] private readonly SharedAudioSystem _audioSys = default!;
  31. [Dependency] private readonly SharedContainerSystem _containers = default!;
  32. [Dependency] private readonly SharedActionsSystem _actions = default!;
  33. [Dependency] private readonly UserInterfaceSystem _ui = default!;
  34. [Dependency] private readonly IRobustRandom _random = default!;
  35. [Dependency] private readonly ThrowingSystem _throwing = default!;
  36. [Dependency] private readonly IConfigurationManager _cfg = default!;
  37. private const float TimerDelay = 0.5f;
  38. private float _timer = 0f;
  39. private const float MinimumSoundValvePressure = 10.0f;
  40. private float _maxExplosionRange;
  41. public override void Initialize()
  42. {
  43. base.Initialize();
  44. SubscribeLocalEvent<GasTankComponent, ComponentShutdown>(OnGasShutdown);
  45. SubscribeLocalEvent<GasTankComponent, BeforeActivatableUIOpenEvent>(BeforeUiOpen);
  46. SubscribeLocalEvent<GasTankComponent, GetItemActionsEvent>(OnGetActions);
  47. SubscribeLocalEvent<GasTankComponent, ExaminedEvent>(OnExamined);
  48. SubscribeLocalEvent<GasTankComponent, ToggleActionEvent>(OnActionToggle);
  49. SubscribeLocalEvent<GasTankComponent, EntParentChangedMessage>(OnParentChange);
  50. SubscribeLocalEvent<GasTankComponent, GasTankSetPressureMessage>(OnGasTankSetPressure);
  51. SubscribeLocalEvent<GasTankComponent, GasTankToggleInternalsMessage>(OnGasTankToggleInternals);
  52. SubscribeLocalEvent<GasTankComponent, GasAnalyzerScanEvent>(OnAnalyzed);
  53. SubscribeLocalEvent<GasTankComponent, PriceCalculationEvent>(OnGasTankPrice);
  54. SubscribeLocalEvent<GasTankComponent, GetVerbsEvent<AlternativeVerb>>(OnGetAlternativeVerb);
  55. Subs.CVar(_cfg, CCVars.AtmosTankFragment, UpdateMaxRange, true);
  56. }
  57. private void UpdateMaxRange(float value)
  58. {
  59. _maxExplosionRange = value;
  60. }
  61. private void OnGasShutdown(Entity<GasTankComponent> gasTank, ref ComponentShutdown args)
  62. {
  63. DisconnectFromInternals(gasTank);
  64. }
  65. private void OnGasTankToggleInternals(Entity<GasTankComponent> ent, ref GasTankToggleInternalsMessage args)
  66. {
  67. ToggleInternals(ent);
  68. }
  69. private void OnGasTankSetPressure(Entity<GasTankComponent> ent, ref GasTankSetPressureMessage args)
  70. {
  71. var pressure = Math.Clamp(args.Pressure, 0f, ent.Comp.MaxOutputPressure);
  72. ent.Comp.OutputPressure = pressure;
  73. UpdateUserInterface(ent, true);
  74. }
  75. public void UpdateUserInterface(Entity<GasTankComponent> ent, bool initialUpdate = false)
  76. {
  77. var (owner, component) = ent;
  78. _ui.SetUiState(owner, SharedGasTankUiKey.Key,
  79. new GasTankBoundUserInterfaceState
  80. {
  81. TankPressure = component.Air?.Pressure ?? 0,
  82. OutputPressure = initialUpdate ? component.OutputPressure : null,
  83. InternalsConnected = component.IsConnected,
  84. CanConnectInternals = CanConnectToInternals(ent)
  85. });
  86. }
  87. private void BeforeUiOpen(Entity<GasTankComponent> ent, ref BeforeActivatableUIOpenEvent args)
  88. {
  89. // Only initial update includes output pressure information, to avoid overwriting client-input as the updates come in.
  90. UpdateUserInterface(ent, true);
  91. }
  92. private void OnParentChange(EntityUid uid, GasTankComponent component, ref EntParentChangedMessage args)
  93. {
  94. // When an item is moved from hands -> pockets, the container removal briefly dumps the item on the floor.
  95. // So this is a shitty fix, where the parent check is just delayed. But this really needs to get fixed
  96. // properly at some point.
  97. component.CheckUser = true;
  98. }
  99. private void OnGetActions(EntityUid uid, GasTankComponent component, GetItemActionsEvent args)
  100. {
  101. args.AddAction(ref component.ToggleActionEntity, component.ToggleAction);
  102. }
  103. private void OnExamined(EntityUid uid, GasTankComponent component, ExaminedEvent args)
  104. {
  105. using var _ = args.PushGroup(nameof(GasTankComponent));
  106. if (args.IsInDetailsRange)
  107. args.PushMarkup(Loc.GetString("comp-gas-tank-examine", ("pressure", Math.Round(component.Air?.Pressure ?? 0))));
  108. if (component.IsConnected)
  109. args.PushMarkup(Loc.GetString("comp-gas-tank-connected"));
  110. args.PushMarkup(Loc.GetString(component.IsValveOpen ? "comp-gas-tank-examine-open-valve" : "comp-gas-tank-examine-closed-valve"));
  111. }
  112. private void OnActionToggle(Entity<GasTankComponent> gasTank, ref ToggleActionEvent args)
  113. {
  114. if (args.Handled)
  115. return;
  116. ToggleInternals(gasTank);
  117. args.Handled = true;
  118. }
  119. public override void Update(float frameTime)
  120. {
  121. base.Update(frameTime);
  122. _timer += frameTime;
  123. if (_timer < TimerDelay)
  124. return;
  125. _timer -= TimerDelay;
  126. var query = EntityQueryEnumerator<GasTankComponent>();
  127. while (query.MoveNext(out var uid, out var comp))
  128. {
  129. var gasTank = (uid, comp);
  130. if (comp.IsValveOpen && !comp.IsLowPressure && comp.OutputPressure > 0)
  131. {
  132. ReleaseGas(gasTank);
  133. }
  134. if (comp.CheckUser)
  135. {
  136. comp.CheckUser = false;
  137. if (Transform(uid).ParentUid != comp.User)
  138. {
  139. DisconnectFromInternals(gasTank);
  140. continue;
  141. }
  142. }
  143. if (comp.Air != null)
  144. {
  145. _atmosphereSystem.React(comp.Air, comp);
  146. }
  147. CheckStatus(gasTank);
  148. if (_ui.IsUiOpen(uid, SharedGasTankUiKey.Key))
  149. {
  150. UpdateUserInterface(gasTank);
  151. }
  152. }
  153. }
  154. private void ReleaseGas(Entity<GasTankComponent> gasTank)
  155. {
  156. var removed = RemoveAirVolume(gasTank, gasTank.Comp.ValveOutputRate * TimerDelay);
  157. var environment = _atmosphereSystem.GetContainingMixture(gasTank.Owner, false, true);
  158. if (environment != null)
  159. {
  160. _atmosphereSystem.Merge(environment, removed);
  161. }
  162. var strength = removed.TotalMoles * MathF.Sqrt(removed.Temperature);
  163. var dir = _random.NextAngle().ToWorldVec();
  164. _throwing.TryThrow(gasTank, dir * strength, strength);
  165. if (gasTank.Comp.OutputPressure >= MinimumSoundValvePressure)
  166. _audioSys.PlayPvs(gasTank.Comp.RuptureSound, gasTank);
  167. }
  168. private void ToggleInternals(Entity<GasTankComponent> ent)
  169. {
  170. if (ent.Comp.IsConnected)
  171. {
  172. DisconnectFromInternals(ent);
  173. }
  174. else
  175. {
  176. ConnectToInternals(ent);
  177. }
  178. }
  179. public GasMixture? RemoveAir(Entity<GasTankComponent> gasTank, float amount)
  180. {
  181. var gas = gasTank.Comp.Air?.Remove(amount);
  182. CheckStatus(gasTank);
  183. return gas;
  184. }
  185. public GasMixture RemoveAirVolume(Entity<GasTankComponent> gasTank, float volume)
  186. {
  187. var component = gasTank.Comp;
  188. if (component.Air == null)
  189. return new GasMixture(volume);
  190. var molesNeeded = component.OutputPressure * volume / (Atmospherics.R * component.Air.Temperature);
  191. var air = RemoveAir(gasTank, molesNeeded);
  192. if (air != null)
  193. air.Volume = volume;
  194. else
  195. return new GasMixture(volume);
  196. return air;
  197. }
  198. public bool CanConnectToInternals(Entity<GasTankComponent> ent)
  199. {
  200. TryGetInternalsComp(ent, out _, out var internalsComp, ent.Comp.User);
  201. return internalsComp != null && internalsComp.BreathTools.Count != 0 && !ent.Comp.IsValveOpen;
  202. }
  203. public void ConnectToInternals(Entity<GasTankComponent> ent)
  204. {
  205. var (owner, component) = ent;
  206. if (component.IsConnected || !CanConnectToInternals(ent))
  207. return;
  208. TryGetInternalsComp(ent, out var internalsUid, out var internalsComp, ent.Comp.User);
  209. if (internalsUid == null || internalsComp == null)
  210. return;
  211. if (_internals.TryConnectTank((internalsUid.Value, internalsComp), owner))
  212. component.User = internalsUid.Value;
  213. _actions.SetToggled(component.ToggleActionEntity, component.IsConnected);
  214. // Couldn't toggle!
  215. if (!component.IsConnected)
  216. return;
  217. component.ConnectStream = _audioSys.Stop(component.ConnectStream);
  218. component.ConnectStream = _audioSys.PlayPvs(component.ConnectSound, owner)?.Entity;
  219. UpdateUserInterface(ent);
  220. }
  221. public void DisconnectFromInternals(Entity<GasTankComponent> ent)
  222. {
  223. var (owner, component) = ent;
  224. if (component.User == null)
  225. return;
  226. TryGetInternalsComp(ent, out var internalsUid, out var internalsComp, component.User);
  227. component.User = null;
  228. _actions.SetToggled(component.ToggleActionEntity, false);
  229. if (internalsUid != null && internalsComp != null)
  230. _internals.DisconnectTank((internalsUid.Value, internalsComp));
  231. component.DisconnectStream = _audioSys.Stop(component.DisconnectStream);
  232. component.DisconnectStream = _audioSys.PlayPvs(component.DisconnectSound, owner)?.Entity;
  233. UpdateUserInterface(ent);
  234. }
  235. /// <summary>
  236. /// Tries to retrieve the internals component of either the gas tank's user,
  237. /// or the gas tank's... containing container
  238. /// </summary>
  239. /// <param name="user">The user of the gas tank</param>
  240. /// <returns>True if internals comp isn't null, false if it is null</returns>
  241. private bool TryGetInternalsComp(Entity<GasTankComponent> ent, out EntityUid? internalsUid, out InternalsComponent? internalsComp, EntityUid? user = null)
  242. {
  243. internalsUid = default;
  244. internalsComp = default;
  245. // If the gas tank doesn't exist for whatever reason, don't even bother
  246. if (TerminatingOrDeleted(ent.Owner))
  247. return false;
  248. user ??= ent.Comp.User;
  249. // Check if the gas tank's user actually has the component that allows them to use a gas tank and mask
  250. if (TryComp<InternalsComponent>(user, out var userInternalsComp) && userInternalsComp != null)
  251. {
  252. internalsUid = user;
  253. internalsComp = userInternalsComp;
  254. return true;
  255. }
  256. // Yeah I have no clue what this actually does, I appreciate the lack of comments on the original function
  257. if (_containers.TryGetContainingContainer((ent.Owner, Transform(ent.Owner)), out var container) && container != null)
  258. {
  259. if (TryComp<InternalsComponent>(container.Owner, out var containerInternalsComp) && containerInternalsComp != null)
  260. {
  261. internalsUid = container.Owner;
  262. internalsComp = containerInternalsComp;
  263. return true;
  264. }
  265. }
  266. return false;
  267. }
  268. public void AssumeAir(Entity<GasTankComponent> ent, GasMixture giver)
  269. {
  270. _atmosphereSystem.Merge(ent.Comp.Air, giver);
  271. CheckStatus(ent);
  272. }
  273. public void CheckStatus(Entity<GasTankComponent> ent)
  274. {
  275. var (owner, component) = ent;
  276. if (component.Air == null)
  277. return;
  278. var pressure = component.Air.Pressure;
  279. if (pressure > component.TankFragmentPressure && _maxExplosionRange > 0)
  280. {
  281. // Give the gas a chance to build up more pressure.
  282. for (var i = 0; i < 3; i++)
  283. {
  284. _atmosphereSystem.React(component.Air, component);
  285. }
  286. pressure = component.Air.Pressure;
  287. var range = MathF.Sqrt((pressure - component.TankFragmentPressure) / component.TankFragmentScale);
  288. // Let's cap the explosion, yeah?
  289. // !1984
  290. range = Math.Min(Math.Min(range, GasTankComponent.MaxExplosionRange), _maxExplosionRange);
  291. _explosions.TriggerExplosive(owner, radius: range);
  292. return;
  293. }
  294. if (pressure > component.TankRupturePressure)
  295. {
  296. if (component.Integrity <= 0)
  297. {
  298. var environment = _atmosphereSystem.GetContainingMixture(owner, false, true);
  299. if (environment != null)
  300. _atmosphereSystem.Merge(environment, component.Air);
  301. _audioSys.PlayPvs(component.RuptureSound, Transform(owner).Coordinates, AudioParams.Default.WithVariation(0.125f));
  302. QueueDel(owner);
  303. return;
  304. }
  305. component.Integrity--;
  306. return;
  307. }
  308. if (pressure > component.TankLeakPressure)
  309. {
  310. if (component.Integrity <= 0)
  311. {
  312. var environment = _atmosphereSystem.GetContainingMixture(owner, false, true);
  313. if (environment == null)
  314. return;
  315. var leakedGas = component.Air.RemoveRatio(0.25f);
  316. _atmosphereSystem.Merge(environment, leakedGas);
  317. }
  318. else
  319. {
  320. component.Integrity--;
  321. }
  322. return;
  323. }
  324. if (component.Integrity < 3)
  325. component.Integrity++;
  326. }
  327. /// <summary>
  328. /// Returns the gas mixture for the gas analyzer
  329. /// </summary>
  330. private void OnAnalyzed(EntityUid uid, GasTankComponent component, GasAnalyzerScanEvent args)
  331. {
  332. args.GasMixtures ??= new List<(string, GasMixture?)>();
  333. args.GasMixtures.Add((Name(uid), component.Air));
  334. }
  335. private void OnGasTankPrice(EntityUid uid, GasTankComponent component, ref PriceCalculationEvent args)
  336. {
  337. args.Price += _atmosphereSystem.GetPrice(component.Air);
  338. }
  339. private void OnGetAlternativeVerb(EntityUid uid, GasTankComponent component, GetVerbsEvent<AlternativeVerb> args)
  340. {
  341. if (!args.CanAccess || !args.CanInteract || args.Hands == null)
  342. return;
  343. args.Verbs.Add(new AlternativeVerb()
  344. {
  345. Text = component.IsValveOpen ? Loc.GetString("comp-gas-tank-close-valve") : Loc.GetString("comp-gas-tank-open-valve"),
  346. Act = () =>
  347. {
  348. component.IsValveOpen = !component.IsValveOpen;
  349. _audioSys.PlayPvs(component.ValveSound, uid);
  350. },
  351. Disabled = component.IsConnected,
  352. });
  353. }
  354. }
  355. }