GasThermoMachineSystem.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. using Content.Server.Atmos.EntitySystems;
  2. using Content.Server.Atmos.Monitor.Systems;
  3. using Content.Server.Atmos.Piping.Components;
  4. using Content.Server.Atmos.Piping.Unary.Components;
  5. using Content.Server.DeviceNetwork;
  6. using Content.Server.DeviceNetwork.Components;
  7. using Content.Server.DeviceNetwork.Systems;
  8. using Content.Server.NodeContainer;
  9. using Content.Server.NodeContainer.EntitySystems;
  10. using Content.Server.NodeContainer.Nodes;
  11. using Content.Server.Power.Components;
  12. using Content.Shared.Atmos;
  13. using Content.Shared.Atmos.Piping.Unary.Components;
  14. using JetBrains.Annotations;
  15. using Robust.Server.GameObjects;
  16. using Content.Server.Power.EntitySystems;
  17. using Content.Shared.UserInterface;
  18. using Content.Shared.Administration.Logs;
  19. using Content.Shared.Database;
  20. using Content.Shared.DeviceNetwork;
  21. using Content.Shared.Examine;
  22. namespace Content.Server.Atmos.Piping.Unary.EntitySystems
  23. {
  24. [UsedImplicitly]
  25. public sealed class GasThermoMachineSystem : EntitySystem
  26. {
  27. [Dependency] private readonly AtmosphereSystem _atmosphereSystem = default!;
  28. [Dependency] private readonly UserInterfaceSystem _userInterfaceSystem = default!;
  29. [Dependency] private readonly PowerReceiverSystem _power = default!;
  30. [Dependency] private readonly NodeContainerSystem _nodeContainer = default!;
  31. [Dependency] private readonly DeviceNetworkSystem _deviceNetwork = default!;
  32. [Dependency] private readonly ISharedAdminLogManager _adminLogger = default!;
  33. public override void Initialize()
  34. {
  35. base.Initialize();
  36. SubscribeLocalEvent<GasThermoMachineComponent, AtmosDeviceUpdateEvent>(OnThermoMachineUpdated);
  37. SubscribeLocalEvent<GasThermoMachineComponent, ExaminedEvent>(OnExamined);
  38. // UI events
  39. SubscribeLocalEvent<GasThermoMachineComponent, BeforeActivatableUIOpenEvent>(OnBeforeOpened);
  40. SubscribeLocalEvent<GasThermoMachineComponent, GasThermomachineToggleMessage>(OnToggleMessage);
  41. SubscribeLocalEvent<GasThermoMachineComponent, GasThermomachineChangeTemperatureMessage>(OnChangeTemperature);
  42. // Device network
  43. SubscribeLocalEvent<GasThermoMachineComponent, DeviceNetworkPacketEvent>(OnPacketRecv);
  44. }
  45. private void OnBeforeOpened(Entity<GasThermoMachineComponent> ent, ref BeforeActivatableUIOpenEvent args)
  46. {
  47. DirtyUI(ent, ent.Comp);
  48. }
  49. private void OnThermoMachineUpdated(EntityUid uid, GasThermoMachineComponent thermoMachine, ref AtmosDeviceUpdateEvent args)
  50. {
  51. thermoMachine.LastEnergyDelta = 0f;
  52. if (!(_power.IsPowered(uid) && TryComp<ApcPowerReceiverComponent>(uid, out var receiver)))
  53. return;
  54. GetHeatExchangeGasMixture(uid, thermoMachine, out var heatExchangeGasMixture);
  55. if (heatExchangeGasMixture == null)
  56. return;
  57. float sign = Math.Sign(thermoMachine.Cp); // 1 if heater, -1 if freezer
  58. float targetTemp = thermoMachine.TargetTemperature;
  59. float highTemp = targetTemp + sign * thermoMachine.TemperatureTolerance;
  60. float temp = heatExchangeGasMixture.Temperature;
  61. if (sign * temp >= sign * highTemp) // upper bound
  62. thermoMachine.HysteresisState = false; // turn off
  63. else if (sign * temp < sign * targetTemp) // lower bound
  64. thermoMachine.HysteresisState = true; // turn on
  65. if (thermoMachine.HysteresisState)
  66. targetTemp = highTemp; // when on, target upper hysteresis bound
  67. else // Hysteresis is the same as "Should this be on?"
  68. {
  69. // Turn dynamic load back on when power has been adjusted to not cause lights to
  70. // blink every time this heater comes on.
  71. //receiver.Load = 0f;
  72. return;
  73. }
  74. // Multiply power in by coefficient of performance, add that heat to gas
  75. float dQ = thermoMachine.HeatCapacity * thermoMachine.Cp * args.dt;
  76. // Clamps the heat transferred to not overshoot
  77. float Cin = _atmosphereSystem.GetHeatCapacity(heatExchangeGasMixture, true);
  78. float dT = targetTemp - temp;
  79. float dQLim = dT * Cin;
  80. float scale = 1f;
  81. if (Math.Abs(dQ) > Math.Abs(dQLim))
  82. {
  83. scale = dQLim / dQ; // reduce power consumption
  84. thermoMachine.HysteresisState = false; // turn off
  85. }
  86. float dQActual = dQ * scale;
  87. if (thermoMachine.Atmospheric)
  88. {
  89. _atmosphereSystem.AddHeat(heatExchangeGasMixture, dQActual);
  90. thermoMachine.LastEnergyDelta = dQActual;
  91. }
  92. else
  93. {
  94. float dQLeak = dQActual * thermoMachine.EnergyLeakPercentage;
  95. float dQPipe = dQActual - dQLeak;
  96. _atmosphereSystem.AddHeat(heatExchangeGasMixture, dQPipe);
  97. thermoMachine.LastEnergyDelta = dQPipe;
  98. if (dQLeak != 0f && _atmosphereSystem.GetContainingMixture(uid, args.Grid, args.Map, excite: true) is { } containingMixture)
  99. _atmosphereSystem.AddHeat(containingMixture, dQLeak);
  100. }
  101. receiver.Load = thermoMachine.HeatCapacity;// * scale; // we're not ready for dynamic load yet, see note above
  102. }
  103. /// <summary>
  104. /// Returns the gas mixture with which the thermomachine will exchange heat (the local atmosphere if atmospheric or the inlet pipe
  105. /// air if not). Returns null if no gas mixture is found.
  106. /// </summary>
  107. private void GetHeatExchangeGasMixture(EntityUid uid, GasThermoMachineComponent thermoMachine, out GasMixture? heatExchangeGasMixture)
  108. {
  109. heatExchangeGasMixture = null;
  110. if (thermoMachine.Atmospheric)
  111. {
  112. heatExchangeGasMixture = _atmosphereSystem.GetContainingMixture(uid, excite: true);
  113. }
  114. else
  115. {
  116. if (!_nodeContainer.TryGetNode(uid, thermoMachine.InletName, out PipeNode? inlet))
  117. return;
  118. heatExchangeGasMixture = inlet.Air;
  119. }
  120. }
  121. private bool IsHeater(GasThermoMachineComponent comp)
  122. {
  123. return comp.Cp >= 0;
  124. }
  125. private void OnToggleMessage(EntityUid uid, GasThermoMachineComponent thermoMachine, GasThermomachineToggleMessage args)
  126. {
  127. var powerState = _power.TogglePower(uid);
  128. _adminLogger.Add(LogType.AtmosPowerChanged, $"{ToPrettyString(args.Actor)} turned {(powerState ? "On" : "Off")} {ToPrettyString(uid)}");
  129. DirtyUI(uid, thermoMachine);
  130. }
  131. private void OnChangeTemperature(EntityUid uid, GasThermoMachineComponent thermoMachine, GasThermomachineChangeTemperatureMessage args)
  132. {
  133. if (IsHeater(thermoMachine))
  134. thermoMachine.TargetTemperature = MathF.Min(args.Temperature, thermoMachine.MaxTemperature);
  135. else
  136. thermoMachine.TargetTemperature = MathF.Max(args.Temperature, thermoMachine.MinTemperature);
  137. thermoMachine.TargetTemperature = MathF.Max(thermoMachine.TargetTemperature, Atmospherics.TCMB);
  138. _adminLogger.Add(LogType.AtmosTemperatureChanged, $"{ToPrettyString(args.Actor)} set temperature on {ToPrettyString(uid)} to {thermoMachine.TargetTemperature}");
  139. DirtyUI(uid, thermoMachine);
  140. }
  141. private void DirtyUI(EntityUid uid, GasThermoMachineComponent? thermoMachine, UserInterfaceComponent? ui=null)
  142. {
  143. if (!Resolve(uid, ref thermoMachine, ref ui, false))
  144. return;
  145. ApcPowerReceiverComponent? powerReceiver = null;
  146. if (!Resolve(uid, ref powerReceiver))
  147. return;
  148. _userInterfaceSystem.SetUiState(uid, ThermomachineUiKey.Key,
  149. new GasThermomachineBoundUserInterfaceState(thermoMachine.MinTemperature, thermoMachine.MaxTemperature, thermoMachine.TargetTemperature, !powerReceiver.PowerDisabled, IsHeater(thermoMachine)));
  150. }
  151. private void OnExamined(EntityUid uid, GasThermoMachineComponent thermoMachine, ExaminedEvent args)
  152. {
  153. if (!args.IsInDetailsRange)
  154. return;
  155. if (Loc.TryGetString("gas-thermomachine-system-examined", out var str,
  156. ("machineName", !IsHeater(thermoMachine) ? "freezer" : "heater"),
  157. ("tempColor", !IsHeater(thermoMachine) ? "deepskyblue" : "red"),
  158. ("temp", Math.Round(thermoMachine.TargetTemperature,2))
  159. ))
  160. args.PushMarkup(str);
  161. }
  162. private void OnPacketRecv(EntityUid uid, GasThermoMachineComponent component, DeviceNetworkPacketEvent args)
  163. {
  164. if (!TryComp(uid, out DeviceNetworkComponent? netConn)
  165. || !args.Data.TryGetValue(DeviceNetworkConstants.Command, out var cmd))
  166. return;
  167. var payload = new NetworkPayload();
  168. switch (cmd)
  169. {
  170. case AtmosDeviceNetworkSystem.SyncData:
  171. payload.Add(DeviceNetworkConstants.Command, AtmosDeviceNetworkSystem.SyncData);
  172. payload.Add(AtmosDeviceNetworkSystem.SyncData, new GasThermoMachineData(component.LastEnergyDelta));
  173. _deviceNetwork.QueuePacket(uid, args.SenderAddress, payload, device: netConn);
  174. return;
  175. }
  176. }
  177. }
  178. }