SolutionTemperatureEffects.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using Content.Shared.Chemistry.Reagent;
  2. using Content.Shared.EntityEffects;
  3. using Robust.Shared.Prototypes;
  4. namespace Content.Server.EntityEffects.Effects;
  5. /// <summary>
  6. /// Sets the temperature of the solution involved with the reaction to a new value.
  7. /// </summary>
  8. [DataDefinition]
  9. public sealed partial class SetSolutionTemperatureEffect : EntityEffect
  10. {
  11. /// <summary>
  12. /// The temperature to set the solution to.
  13. /// </summary>
  14. [DataField("temperature", required: true)] private float _temperature;
  15. protected override string? ReagentEffectGuidebookText(IPrototypeManager prototype, IEntitySystemManager entSys)
  16. => Loc.GetString("reagent-effect-guidebook-set-solution-temperature-effect",
  17. ("chance", Probability), ("temperature", _temperature));
  18. public override void Effect(EntityEffectBaseArgs args)
  19. {
  20. if (args is EntityEffectReagentArgs reagentArgs)
  21. {
  22. var solution = reagentArgs.Source;
  23. if (solution == null)
  24. return;
  25. solution.Temperature = _temperature;
  26. return;
  27. }
  28. // TODO: Someone needs to figure out how to do this for non-reagent effects.
  29. throw new NotImplementedException();
  30. }
  31. }
  32. /// <summary>
  33. /// Adjusts the temperature of the solution involved in the reaction.
  34. /// </summary>
  35. [DataDefinition]
  36. public sealed partial class AdjustSolutionTemperatureEffect : EntityEffect
  37. {
  38. /// <summary>
  39. /// The change in temperature.
  40. /// </summary>
  41. [DataField("delta", required: true)] private float _delta;
  42. /// <summary>
  43. /// The minimum temperature this effect can reach.
  44. /// </summary>
  45. [DataField("minTemp")] private float _minTemp = 0.0f;
  46. /// <summary>
  47. /// The maximum temperature this effect can reach.
  48. /// </summary>
  49. [DataField("maxTemp")] private float _maxTemp = float.PositiveInfinity;
  50. /// <summary>
  51. /// If true, then scale ranges by intensity. If not, the ranges are the same regardless of reactant amount.
  52. /// </summary>
  53. [DataField("scaled")] private bool _scaled;
  54. protected override string? ReagentEffectGuidebookText(IPrototypeManager prototype, IEntitySystemManager entSys)
  55. => Loc.GetString("reagent-effect-guidebook-adjust-solution-temperature-effect",
  56. ("chance", Probability), ("deltasign", MathF.Sign(_delta)), ("mintemp", _minTemp), ("maxtemp", _maxTemp));
  57. public override void Effect(EntityEffectBaseArgs args)
  58. {
  59. if (args is EntityEffectReagentArgs reagentArgs)
  60. {
  61. var solution = reagentArgs.Source;
  62. if (solution == null || solution.Volume == 0)
  63. return;
  64. var deltaT = _scaled ? _delta * (float) reagentArgs.Quantity : _delta;
  65. solution.Temperature = Math.Clamp(solution.Temperature + deltaT, _minTemp, _maxTemp);
  66. return;
  67. }
  68. // TODO: Someone needs to figure out how to do this for non-reagent effects.
  69. throw new NotImplementedException();
  70. }
  71. }
  72. /// <summary>
  73. /// Adjusts the thermal energy of the solution involved in the reaction.
  74. /// </summary>
  75. public sealed partial class AdjustSolutionThermalEnergyEffect : EntityEffect
  76. {
  77. /// <summary>
  78. /// The change in energy.
  79. /// </summary>
  80. [DataField("delta", required: true)] private float _delta;
  81. /// <summary>
  82. /// The minimum temperature this effect can reach.
  83. /// </summary>
  84. [DataField("minTemp")] private float _minTemp = 0.0f;
  85. /// <summary>
  86. /// The maximum temperature this effect can reach.
  87. /// </summary>
  88. [DataField("maxTemp")] private float _maxTemp = float.PositiveInfinity;
  89. /// <summary>
  90. /// If true, then scale ranges by intensity. If not, the ranges are the same regardless of reactant amount.
  91. /// </summary>
  92. [DataField("scaled")] private bool _scaled;
  93. public override void Effect(EntityEffectBaseArgs args)
  94. {
  95. if (args is EntityEffectReagentArgs reagentArgs)
  96. {
  97. var solution = reagentArgs.Source;
  98. if (solution == null || solution.Volume == 0)
  99. return;
  100. if (_delta > 0 && solution.Temperature >= _maxTemp)
  101. return;
  102. if (_delta < 0 && solution.Temperature <= _minTemp)
  103. return;
  104. var heatCap = solution.GetHeatCapacity(null);
  105. var deltaT = _scaled
  106. ? _delta / heatCap * (float) reagentArgs.Quantity
  107. : _delta / heatCap;
  108. solution.Temperature = Math.Clamp(solution.Temperature + deltaT, _minTemp, _maxTemp);
  109. return;
  110. }
  111. // TODO: Someone needs to figure out how to do this for non-reagent effects.
  112. throw new NotImplementedException();
  113. }
  114. protected override string? ReagentEffectGuidebookText(IPrototypeManager prototype, IEntitySystemManager entSys)
  115. => Loc.GetString("reagent-effect-guidebook-adjust-solution-temperature-effect",
  116. ("chance", Probability), ("deltasign", MathF.Sign(_delta)), ("mintemp", _minTemp), ("maxtemp", _maxTemp));
  117. }