1
0

LungSystem.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using Content.Server.Atmos.Components;
  2. using Content.Server.Atmos.EntitySystems;
  3. using Content.Server.Body.Components;
  4. using Content.Shared.Chemistry.EntitySystems;
  5. using Content.Shared.Atmos;
  6. using Content.Shared.Chemistry.Components;
  7. using Content.Shared.Clothing;
  8. using Content.Shared.Inventory.Events;
  9. namespace Content.Server.Body.Systems;
  10. public sealed class LungSystem : EntitySystem
  11. {
  12. [Dependency] private readonly AtmosphereSystem _atmos = default!;
  13. [Dependency] private readonly InternalsSystem _internals = default!;
  14. [Dependency] private readonly SharedSolutionContainerSystem _solutionContainerSystem = default!;
  15. public static string LungSolutionName = "Lung";
  16. public override void Initialize()
  17. {
  18. base.Initialize();
  19. SubscribeLocalEvent<LungComponent, ComponentInit>(OnComponentInit);
  20. SubscribeLocalEvent<BreathToolComponent, GotEquippedEvent>(OnGotEquipped);
  21. SubscribeLocalEvent<BreathToolComponent, GotUnequippedEvent>(OnGotUnequipped);
  22. SubscribeLocalEvent<BreathToolComponent, ItemMaskToggledEvent>(OnMaskToggled);
  23. }
  24. private void OnGotUnequipped(Entity<BreathToolComponent> ent, ref GotUnequippedEvent args)
  25. {
  26. _atmos.DisconnectInternals(ent);
  27. }
  28. private void OnGotEquipped(Entity<BreathToolComponent> ent, ref GotEquippedEvent args)
  29. {
  30. if ((args.SlotFlags & ent.Comp.AllowedSlots) == 0)
  31. {
  32. return;
  33. }
  34. ent.Comp.IsFunctional = true;
  35. if (TryComp(args.Equipee, out InternalsComponent? internals))
  36. {
  37. ent.Comp.ConnectedInternalsEntity = args.Equipee;
  38. _internals.ConnectBreathTool((args.Equipee, internals), ent);
  39. }
  40. }
  41. private void OnComponentInit(Entity<LungComponent> entity, ref ComponentInit args)
  42. {
  43. if (_solutionContainerSystem.EnsureSolution(entity.Owner, entity.Comp.SolutionName, out var solution))
  44. {
  45. solution.MaxVolume = 100.0f;
  46. solution.CanReact = false; // No dexalin lungs
  47. }
  48. }
  49. private void OnMaskToggled(Entity<BreathToolComponent> ent, ref ItemMaskToggledEvent args)
  50. {
  51. if (args.Mask.Comp.IsToggled)
  52. {
  53. _atmos.DisconnectInternals(ent);
  54. }
  55. else
  56. {
  57. ent.Comp.IsFunctional = true;
  58. if (TryComp(args.Wearer, out InternalsComponent? internals))
  59. {
  60. ent.Comp.ConnectedInternalsEntity = args.Wearer;
  61. _internals.ConnectBreathTool((args.Wearer.Value, internals), ent);
  62. }
  63. }
  64. }
  65. public void GasToReagent(EntityUid uid, LungComponent lung)
  66. {
  67. if (!_solutionContainerSystem.ResolveSolution(uid, lung.SolutionName, ref lung.Solution, out var solution))
  68. return;
  69. GasToReagent(lung.Air, solution);
  70. _solutionContainerSystem.UpdateChemicals(lung.Solution.Value);
  71. }
  72. private void GasToReagent(GasMixture gas, Solution solution)
  73. {
  74. foreach (var gasId in Enum.GetValues<Gas>())
  75. {
  76. var i = (int) gasId;
  77. var moles = gas[i];
  78. if (moles <= 0)
  79. continue;
  80. var reagent = _atmos.GasReagents[i];
  81. if (reagent is null)
  82. continue;
  83. var amount = moles * Atmospherics.BreathMolesToReagentMultiplier;
  84. solution.AddReagent(reagent, amount);
  85. }
  86. }
  87. public Solution GasToReagent(GasMixture gas)
  88. {
  89. var solution = new Solution();
  90. GasToReagent(gas, solution);
  91. return solution;
  92. }
  93. }