GasOutletInjectorSystem.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using Content.Server.Atmos.EntitySystems;
  2. using Content.Server.Atmos.Piping.Components;
  3. using Content.Server.Atmos.Piping.Unary.Components;
  4. using Content.Server.NodeContainer;
  5. using Content.Server.NodeContainer.EntitySystems;
  6. using Content.Server.NodeContainer.Nodes;
  7. using Content.Shared.Atmos.Piping;
  8. using Content.Shared.Interaction;
  9. using JetBrains.Annotations;
  10. namespace Content.Server.Atmos.Piping.Unary.EntitySystems
  11. {
  12. [UsedImplicitly]
  13. public sealed class GasOutletInjectorSystem : EntitySystem
  14. {
  15. [Dependency] private readonly AtmosphereSystem _atmosphereSystem = default!;
  16. [Dependency] private readonly SharedAppearanceSystem _appearance = default!;
  17. [Dependency] private readonly NodeContainerSystem _nodeContainer = default!;
  18. public override void Initialize()
  19. {
  20. base.Initialize();
  21. SubscribeLocalEvent<GasOutletInjectorComponent, AtmosDeviceUpdateEvent>(OnOutletInjectorUpdated);
  22. SubscribeLocalEvent<GasOutletInjectorComponent, ActivateInWorldEvent>(OnActivate);
  23. SubscribeLocalEvent<GasOutletInjectorComponent, MapInitEvent>(OnMapInit);
  24. }
  25. private void OnMapInit(EntityUid uid, GasOutletInjectorComponent component, MapInitEvent args)
  26. {
  27. UpdateAppearance(uid, component);
  28. }
  29. private void OnActivate(EntityUid uid, GasOutletInjectorComponent component, ActivateInWorldEvent args)
  30. {
  31. if (args.Handled || !args.Complex)
  32. return;
  33. component.Enabled = !component.Enabled;
  34. UpdateAppearance(uid, component);
  35. args.Handled = true;
  36. }
  37. public void UpdateAppearance(EntityUid uid, GasOutletInjectorComponent component, AppearanceComponent? appearance = null)
  38. {
  39. if (!Resolve(uid, ref appearance, false))
  40. return;
  41. _appearance.SetData(uid, OutletInjectorVisuals.Enabled, component.Enabled, appearance);
  42. }
  43. private void OnOutletInjectorUpdated(EntityUid uid, GasOutletInjectorComponent injector, ref AtmosDeviceUpdateEvent args)
  44. {
  45. if (!injector.Enabled)
  46. return;
  47. if (!_nodeContainer.TryGetNode(uid, injector.InletName, out PipeNode? inlet))
  48. return;
  49. var environment = _atmosphereSystem.GetContainingMixture(uid, args.Grid, args.Map, true, true);
  50. if (environment == null)
  51. return;
  52. if (inlet.Air.Temperature < 0)
  53. return;
  54. if (environment.Pressure > injector.MaxPressure)
  55. return;
  56. var timeDelta = args.dt;
  57. // TODO adjust ratio so that environment does not go above MaxPressure?
  58. var ratio = MathF.Min(1f, timeDelta * injector.TransferRate * _atmosphereSystem.PumpSpeedup() / inlet.Air.Volume);
  59. var removed = inlet.Air.RemoveRatio(ratio);
  60. _atmosphereSystem.Merge(environment, removed);
  61. }
  62. }
  63. }