RadiationCollectorSystem.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. using System.Diagnostics.CodeAnalysis;
  2. using System.Linq;
  3. using Content.Server.Atmos.EntitySystems;
  4. using Content.Server.Atmos.Components;
  5. using Content.Server.Popups;
  6. using Content.Server.Power.Components;
  7. using Content.Server.Power.EntitySystems;
  8. using Content.Server.Singularity.Components;
  9. using Content.Shared.Atmos;
  10. using Content.Shared.Examine;
  11. using Content.Shared.Interaction;
  12. using Content.Shared.Radiation.Events;
  13. using Content.Shared.Singularity.Components;
  14. using Content.Shared.Timing;
  15. using Robust.Shared.Containers;
  16. using Robust.Shared.Timing;
  17. namespace Content.Server.Singularity.EntitySystems;
  18. public sealed class RadiationCollectorSystem : EntitySystem
  19. {
  20. [Dependency] private readonly IGameTiming _gameTiming = default!;
  21. [Dependency] private readonly PopupSystem _popupSystem = default!;
  22. [Dependency] private readonly SharedAppearanceSystem _appearance = default!;
  23. [Dependency] private readonly SharedContainerSystem _containerSystem = default!;
  24. [Dependency] private readonly UseDelaySystem _useDelay = default!;
  25. private const string GasTankContainer = "gas_tank";
  26. public override void Initialize()
  27. {
  28. base.Initialize();
  29. SubscribeLocalEvent<RadiationCollectorComponent, ActivateInWorldEvent>(OnActivate);
  30. SubscribeLocalEvent<RadiationCollectorComponent, OnIrradiatedEvent>(OnRadiation);
  31. SubscribeLocalEvent<RadiationCollectorComponent, ExaminedEvent>(OnExamined);
  32. SubscribeLocalEvent<RadiationCollectorComponent, GasAnalyzerScanEvent>(OnAnalyzed);
  33. SubscribeLocalEvent<RadiationCollectorComponent, MapInitEvent>(OnMapInit);
  34. SubscribeLocalEvent<RadiationCollectorComponent, EntInsertedIntoContainerMessage>(OnTankChanged);
  35. SubscribeLocalEvent<RadiationCollectorComponent, EntRemovedFromContainerMessage>(OnTankChanged);
  36. SubscribeLocalEvent<NetworkBatteryPostSync>(PostSync);
  37. }
  38. private bool TryGetLoadedGasTank(EntityUid uid, [NotNullWhen(true)] out GasTankComponent? gasTankComponent)
  39. {
  40. gasTankComponent = null;
  41. if (!_containerSystem.TryGetContainer(uid, GasTankContainer, out var container) || container.ContainedEntities.Count == 0)
  42. return false;
  43. if (!EntityManager.TryGetComponent(container.ContainedEntities.First(), out gasTankComponent))
  44. return false;
  45. return true;
  46. }
  47. private void OnMapInit(EntityUid uid, RadiationCollectorComponent component, MapInitEvent args)
  48. {
  49. TryGetLoadedGasTank(uid, out var gasTank);
  50. UpdateTankAppearance(uid, component, gasTank);
  51. }
  52. private void OnTankChanged(EntityUid uid, RadiationCollectorComponent component, ContainerModifiedMessage args)
  53. {
  54. TryGetLoadedGasTank(uid, out var gasTank);
  55. UpdateTankAppearance(uid, component, gasTank);
  56. }
  57. private void OnActivate(EntityUid uid, RadiationCollectorComponent component, ActivateInWorldEvent args)
  58. {
  59. if (TryComp(uid, out UseDelayComponent? useDelay) && !_useDelay.TryResetDelay((uid, useDelay), true))
  60. return;
  61. ToggleCollector(uid, args.User, component);
  62. }
  63. private void OnRadiation(EntityUid uid, RadiationCollectorComponent component, OnIrradiatedEvent args)
  64. {
  65. if (!component.Enabled || component.RadiationReactiveGases == null)
  66. return;
  67. if (!TryGetLoadedGasTank(uid, out var gasTankComponent))
  68. return;
  69. var charge = 0f;
  70. foreach (var gas in component.RadiationReactiveGases)
  71. {
  72. float reactantMol = gasTankComponent.Air.GetMoles(gas.ReactantPrototype);
  73. float delta = args.TotalRads * reactantMol * gas.ReactantBreakdownRate;
  74. // We need to offset the huge power gains possible when using very cold gases
  75. // (they allow you to have a much higher molar concentrations of gas in the tank).
  76. // Hence power output is modified using the Michaelis-Menten equation,
  77. // it will heavily penalise the power output of low temperature reactions:
  78. // 300K = 100% power output, 73K = 49% power output, 1K = 1% power output
  79. float temperatureMod = 1.5f * gasTankComponent.Air.Temperature / (150f + gasTankComponent.Air.Temperature);
  80. charge += args.TotalRads * reactantMol * component.ChargeModifier * gas.PowerGenerationEfficiency * temperatureMod;
  81. if (delta > 0)
  82. {
  83. gasTankComponent.Air.AdjustMoles(gas.ReactantPrototype, -Math.Min(delta, reactantMol));
  84. }
  85. if (gas.Byproduct != null)
  86. {
  87. gasTankComponent.Air.AdjustMoles((int)gas.Byproduct, delta * gas.MolarRatio);
  88. }
  89. }
  90. if (TryComp<PowerSupplierComponent>(uid, out var comp))
  91. {
  92. int powerHoldoverTicks = _gameTiming.TickRate * 2; // number of ticks to hold radiation
  93. component.PowerTicksLeft = powerHoldoverTicks;
  94. comp.MaxSupply = component.Enabled ? charge : 0;
  95. }
  96. // Update appearance
  97. UpdatePressureIndicatorAppearance(uid, component, gasTankComponent);
  98. }
  99. private void PostSync(NetworkBatteryPostSync ev)
  100. {
  101. // This is run every power tick. Used to decrement the PowerTicksLeft counter.
  102. var query = EntityQueryEnumerator<RadiationCollectorComponent>();
  103. while (query.MoveNext(out var uid, out var component))
  104. {
  105. if (component.PowerTicksLeft > 0)
  106. {
  107. component.PowerTicksLeft -= 1;
  108. }
  109. else if (TryComp<PowerSupplierComponent>(uid, out var comp))
  110. {
  111. comp.MaxSupply = 0;
  112. }
  113. }
  114. }
  115. private void OnExamined(EntityUid uid, RadiationCollectorComponent component, ExaminedEvent args)
  116. {
  117. using (args.PushGroup(nameof(RadiationCollectorComponent)))
  118. {
  119. args.PushMarkup(Loc.GetString("power-radiation-collector-enabled", ("state", component.Enabled)));
  120. if (!TryGetLoadedGasTank(uid, out var gasTank))
  121. {
  122. args.PushMarkup(Loc.GetString("power-radiation-collector-gas-tank-missing"));
  123. }
  124. else
  125. {
  126. _appearance.TryGetData<int>(uid, RadiationCollectorVisuals.PressureState, out var state);
  127. args.PushMarkup(Loc.GetString("power-radiation-collector-gas-tank-present",
  128. ("fullness", state)));
  129. }
  130. }
  131. }
  132. private void OnAnalyzed(EntityUid uid, RadiationCollectorComponent component, GasAnalyzerScanEvent args)
  133. {
  134. if (!TryGetLoadedGasTank(uid, out var gasTankComponent))
  135. return;
  136. args.GasMixtures ??= new List<(string, GasMixture?)>();
  137. args.GasMixtures.Add((Name(uid), gasTankComponent.Air));
  138. }
  139. public void ToggleCollector(EntityUid uid, EntityUid? user = null, RadiationCollectorComponent? component = null)
  140. {
  141. if (!Resolve(uid, ref component))
  142. return;
  143. SetCollectorEnabled(uid, !component.Enabled, user, component);
  144. }
  145. public void SetCollectorEnabled(EntityUid uid, bool enabled, EntityUid? user = null, RadiationCollectorComponent? component = null)
  146. {
  147. if (!Resolve(uid, ref component, false))
  148. return;
  149. component.Enabled = enabled;
  150. // Show message to the player
  151. if (user != null)
  152. {
  153. var msg = component.Enabled ? "radiation-collector-component-use-on" : "radiation-collector-component-use-off";
  154. _popupSystem.PopupEntity(Loc.GetString(msg), uid);
  155. }
  156. // Update appearance
  157. UpdateMachineAppearance(uid, component);
  158. }
  159. private void UpdateMachineAppearance(EntityUid uid, RadiationCollectorComponent component, AppearanceComponent? appearance = null)
  160. {
  161. if (!Resolve(uid, ref appearance))
  162. return;
  163. var state = component.Enabled ? RadiationCollectorVisualState.Active : RadiationCollectorVisualState.Deactive;
  164. _appearance.SetData(uid, RadiationCollectorVisuals.VisualState, state, appearance);
  165. }
  166. private void UpdatePressureIndicatorAppearance(EntityUid uid, RadiationCollectorComponent component, GasTankComponent? gasTank = null, AppearanceComponent? appearance = null)
  167. {
  168. if (!Resolve(uid, ref appearance, false))
  169. return;
  170. // gas canisters can fill tanks up to 10 atm, so we set the warning level thresholds 1/3 and 2/3 of that
  171. if (gasTank == null || gasTank.Air.Pressure < 10)
  172. _appearance.SetData(uid, RadiationCollectorVisuals.PressureState, 0, appearance);
  173. else if (gasTank.Air.Pressure < 3.33f * Atmospherics.OneAtmosphere)
  174. _appearance.SetData(uid, RadiationCollectorVisuals.PressureState, 1, appearance);
  175. else if (gasTank.Air.Pressure < 6.66f * Atmospherics.OneAtmosphere)
  176. _appearance.SetData(uid, RadiationCollectorVisuals.PressureState, 2, appearance);
  177. else
  178. _appearance.SetData(uid, RadiationCollectorVisuals.PressureState, 3, appearance);
  179. }
  180. private void UpdateTankAppearance(EntityUid uid, RadiationCollectorComponent component, GasTankComponent? gasTank = null, AppearanceComponent? appearance = null)
  181. {
  182. if (!Resolve(uid, ref appearance, false))
  183. return;
  184. _appearance.SetData(uid, RadiationCollectorVisuals.TankInserted, gasTank != null, appearance);
  185. UpdatePressureIndicatorAppearance(uid, component, gasTank, appearance);
  186. }
  187. }