1
0

GasPowerReceiverSystem.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using Content.Server.Atmos.EntitySystems;
  2. using Content.Server.Atmos.Piping.Components;
  3. using Content.Server.NodeContainer;
  4. using Content.Server.NodeContainer.EntitySystems;
  5. using Content.Server.NodeContainer.Nodes;
  6. using Content.Server.Power.Components;
  7. using Content.Shared.Atmos;
  8. using Content.Shared.Power;
  9. namespace Content.Server.Power.Generator;
  10. /// <summary>
  11. /// This handles gas power receivers, allowing devices to accept power in the form of a gas.
  12. /// </summary>
  13. public sealed class GasPowerReceiverSystem : EntitySystem
  14. {
  15. [Dependency] private readonly AtmosphereSystem _atmosphereSystem = default!;
  16. [Dependency] private readonly NodeContainerSystem _nodeContainer = default!;
  17. /// <inheritdoc/>
  18. public override void Initialize()
  19. {
  20. SubscribeLocalEvent<GasPowerReceiverComponent, AtmosDeviceUpdateEvent>(OnDeviceUpdated);
  21. }
  22. private void OnDeviceUpdated(EntityUid uid, GasPowerReceiverComponent component, ref AtmosDeviceUpdateEvent args)
  23. {
  24. var timeDelta = args.dt;
  25. if (!_nodeContainer.TryGetNode(uid, "pipe", out PipeNode? pipe))
  26. return;
  27. // if we're below the max temperature, then we are simply consuming our target gas
  28. if (pipe.Air.Temperature <= component.MaxTemperature)
  29. {
  30. // we have enough gas, so we consume it and are powered
  31. if (pipe.Air[(int) component.TargetGas] > component.MolesConsumedSec * timeDelta)
  32. {
  33. pipe.Air.AdjustMoles(component.TargetGas, -component.MolesConsumedSec * timeDelta);
  34. SetPowered(uid, component, true);
  35. }
  36. else // we do not have enough gas, so we power off
  37. {
  38. SetPowered(uid, component, false);
  39. }
  40. }
  41. else // we are exceeding the max temp and are now operating in pressure mode
  42. {
  43. var pres = component.PressureConsumedSec * timeDelta;
  44. if (pipe.Air.Pressure >= pres)
  45. {
  46. // remove gas from the pipe
  47. var res = pipe.Air.Remove(pres * 100.0f / (Atmospherics.R * pipe.Air.Temperature));
  48. if (component.OffVentGas)
  49. {
  50. // eject the gas into the atmosphere
  51. var mix = _atmosphereSystem.GetContainingMixture(uid, args.Grid, args.Map, false, true);
  52. if (mix is not null)
  53. _atmosphereSystem.Merge(res, mix);
  54. }
  55. SetPowered(uid, component, true);
  56. }
  57. else // if we do not have high enough pressure to operate, power off
  58. {
  59. SetPowered(uid, component, false);
  60. }
  61. }
  62. }
  63. private void SetPowered(EntityUid uid, GasPowerReceiverComponent comp, bool state)
  64. {
  65. if (state != comp.Powered)
  66. {
  67. comp.Powered = state;
  68. var ev = new PowerChangedEvent(state, 0);
  69. RaiseLocalEvent(uid, ref ev);
  70. }
  71. }
  72. }