Adds temperature to solutions (#5834)
This commit is contained in:
@@ -153,6 +153,24 @@ namespace Content.Server.Chemistry.EntitySystems
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the capacity (maximum volume) of a solution to a new value.
|
||||
/// </summary>
|
||||
/// <param name="targetUid">The entity containing the solution.</param>
|
||||
/// <param name="targetSolution">The solution to set the capacity of.</param>
|
||||
/// <param name="capacity">The value to set the capacity of the solution to.</param>
|
||||
public void SetCapacity(EntityUid targetUid, Solution targetSolution, FixedPoint2 capacity)
|
||||
{
|
||||
if (targetSolution.MaxVolume == capacity)
|
||||
return;
|
||||
|
||||
targetSolution.MaxVolume = capacity;
|
||||
if (capacity < targetSolution.CurrentVolume)
|
||||
targetSolution.RemoveSolution(targetSolution.CurrentVolume - capacity);
|
||||
|
||||
UpdateChemicals(targetUid, targetSolution);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds reagent of an Id to the container.
|
||||
/// </summary>
|
||||
@@ -163,10 +181,10 @@ namespace Content.Server.Chemistry.EntitySystems
|
||||
/// <param name="acceptedQuantity">The amount of reagent successfully added.</param>
|
||||
/// <returns>If all the reagent could be added.</returns>
|
||||
public bool TryAddReagent(EntityUid targetUid, Solution targetSolution, string reagentId, FixedPoint2 quantity,
|
||||
out FixedPoint2 acceptedQuantity)
|
||||
out FixedPoint2 acceptedQuantity, float? temperature = null)
|
||||
{
|
||||
acceptedQuantity = targetSolution.AvailableVolume > quantity ? quantity : targetSolution.AvailableVolume;
|
||||
targetSolution.AddReagent(reagentId, acceptedQuantity);
|
||||
targetSolution.AddReagent(reagentId, acceptedQuantity, temperature);
|
||||
|
||||
if (acceptedQuantity > 0)
|
||||
UpdateChemicals(targetUid, targetSolution, true);
|
||||
@@ -257,6 +275,8 @@ namespace Content.Server.Chemistry.EntitySystems
|
||||
{
|
||||
var (reagentId, curQuantity) = solution.Contents[i];
|
||||
removedReagent[pos++] = reagentId;
|
||||
if (!_prototypeManager.TryIndex(reagentId, out ReagentPrototype? proto))
|
||||
proto = new ReagentPrototype();
|
||||
|
||||
var newQuantity = curQuantity - quantity;
|
||||
if (newQuantity <= 0)
|
||||
@@ -302,5 +322,56 @@ namespace Content.Server.Chemistry.EntitySystems
|
||||
|
||||
return reagentQuantity;
|
||||
}
|
||||
|
||||
|
||||
// Thermal energy and temperature management.
|
||||
#region Thermal Energy and Temperature
|
||||
|
||||
/// <summary>
|
||||
/// Sets the temperature of a solution to a new value and then checks for reaction processing.
|
||||
/// </summary>
|
||||
/// <param name="owner">The entity in which the solution is located.</param>
|
||||
/// <param name="solution">The solution to set the temperature of.</param>
|
||||
/// <param name="temperature">The new value to set the temperature to.</param>
|
||||
public void SetTemperature(EntityUid owner, Solution solution, float temperature)
|
||||
{
|
||||
if (temperature == solution.Temperature)
|
||||
return;
|
||||
|
||||
solution.Temperature = temperature;
|
||||
UpdateChemicals(owner, solution, true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the thermal energy of a solution to a new value and then checks for reaction processing.
|
||||
/// </summary>
|
||||
/// <param name="owner">The entity in which the solution is located.</param>
|
||||
/// <param name="solution">The solution to set the thermal energy of.</param>
|
||||
/// <param name="thermalEnergy">The new value to set the thermal energy to.</param>
|
||||
public void SetThermalEnergy(EntityUid owner, Solution solution, float thermalEnergy)
|
||||
{
|
||||
if (thermalEnergy == solution.ThermalEnergy)
|
||||
return;
|
||||
|
||||
solution.ThermalEnergy = thermalEnergy;
|
||||
UpdateChemicals(owner, solution, true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds some thermal energy to a solution and then checks for reaction processing.
|
||||
/// </summary>
|
||||
/// <param name="owner">The entity in which the solution is located.</param>
|
||||
/// <param name="solution">The solution to set the thermal energy of.</param>
|
||||
/// <param name="thermalEnergy">The new value to set the thermal energy to.</param>
|
||||
public void AddThermalEnergy(EntityUid owner, Solution solution, float thermalEnergy)
|
||||
{
|
||||
if (thermalEnergy == 0.0f)
|
||||
return;
|
||||
|
||||
solution.ThermalEnergy += thermalEnergy;
|
||||
UpdateChemicals(owner, solution, true);
|
||||
}
|
||||
|
||||
#endregion Thermal Energy and Temperature
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
using System;
|
||||
using Content.Shared.Chemistry.Components;
|
||||
using Content.Shared.Chemistry.Reagent;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
|
||||
namespace Content.Server.Chemistry.ReactionEffects
|
||||
{
|
||||
/// <summary>
|
||||
/// Sets the temperature of the solution involved with the reaction to a new value.
|
||||
/// </summary>
|
||||
[DataDefinition]
|
||||
public class SetSolutionTemperatureEffect : ReagentEffect
|
||||
{
|
||||
/// <summary>
|
||||
/// The temperature to set the solution to.
|
||||
/// </summary>
|
||||
[DataField("temperature", required: true)] private float _temperature;
|
||||
|
||||
public override void Effect(ReagentEffectArgs args)
|
||||
{
|
||||
var solution = args.Source;
|
||||
if (solution == null)
|
||||
return;
|
||||
|
||||
solution.Temperature = _temperature;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adjusts the temperature of the solution involved in the reaction.
|
||||
/// </summary>
|
||||
[DataDefinition]
|
||||
public class AdjustSolutionTemperatureEffect : ReagentEffect
|
||||
{
|
||||
/// <summary>
|
||||
/// The total change in the thermal energy of the solution.
|
||||
/// </summary>
|
||||
[DataField("delta", required: true)] protected float Delta;
|
||||
|
||||
/// <summary>
|
||||
/// The minimum temperature this effect can reach.
|
||||
/// </summary>
|
||||
[DataField("minTemp")] private float _minTemp = 0.0f;
|
||||
|
||||
/// <summary>
|
||||
/// The maximum temperature this effect can reach.
|
||||
/// </summary>
|
||||
[DataField("maxTemp")] private float _maxTemp = float.PositiveInfinity;
|
||||
|
||||
/// <summary>
|
||||
/// If true, then scale ranges by intensity. If not, the ranges are the same regardless of reactant amount.
|
||||
/// </summary>
|
||||
[DataField("scaled")] private bool _scaled;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="solution"></param>
|
||||
/// <returns></returns>
|
||||
protected virtual float GetDeltaT(Solution solution) => Delta;
|
||||
|
||||
public override void Effect(ReagentEffectArgs args)
|
||||
{
|
||||
var solution = args.Source;
|
||||
if (solution == null)
|
||||
return;
|
||||
|
||||
var deltaT = GetDeltaT(solution);
|
||||
if (_scaled)
|
||||
deltaT = deltaT * (float) args.Quantity;
|
||||
|
||||
if (deltaT == 0.0d)
|
||||
return;
|
||||
if (deltaT > 0.0d && solution.Temperature >= _maxTemp)
|
||||
return;
|
||||
if (deltaT < 0.0d && solution.Temperature <= _minTemp)
|
||||
return;
|
||||
|
||||
solution.Temperature = MathF.Max(MathF.Min(solution.Temperature + deltaT, _minTemp), _maxTemp);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adjusts the thermal energy of the solution involved in the reaction.
|
||||
/// </summary>
|
||||
public class AdjustSolutionThermalEnergyEffect : AdjustSolutionTemperatureEffect
|
||||
{
|
||||
protected override float GetDeltaT(Solution solution)
|
||||
{
|
||||
var heatCapacity = solution.HeatCapacity;
|
||||
if (heatCapacity == 0.0f)
|
||||
return 0.0f;
|
||||
return Delta / heatCapacity;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
using Content.Shared.Chemistry.Components;
|
||||
using Content.Shared.Chemistry.Reagent;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
|
||||
namespace Content.Server.Chemistry.ReagentEffectConditions
|
||||
{
|
||||
/// <summary>
|
||||
/// Requires the solution to be above or below a certain temperature.
|
||||
/// Used for things like explosives.
|
||||
/// </summary>
|
||||
public class SolutionTemperature : ReagentEffectCondition
|
||||
{
|
||||
[DataField("min")]
|
||||
public float Min = 0.0f;
|
||||
|
||||
[DataField("max")]
|
||||
public float Max = float.PositiveInfinity;
|
||||
public override bool Condition(ReagentEffectArgs args)
|
||||
{
|
||||
if (args.Source == null)
|
||||
return false;
|
||||
if (args.Source.Temperature < Min)
|
||||
return false;
|
||||
if (args.Source.Temperature > Max)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
using Content.Shared.Chemistry.Components;
|
||||
using Content.Shared.Chemistry.Reagent;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
|
||||
namespace Content.Server.Chemistry.ReagentEffectConditions
|
||||
{
|
||||
/// <summary>
|
||||
/// Requires the solution to be above or below a certain thermal energy.
|
||||
/// Used for things like explosives.
|
||||
/// </summary>
|
||||
public class SolutionThermalEnergy : ReagentEffectCondition
|
||||
{
|
||||
[DataField("min")]
|
||||
public float Min = 0.0f;
|
||||
|
||||
[DataField("max")]
|
||||
public float Max = float.PositiveInfinity;
|
||||
public override bool Condition(ReagentEffectArgs args)
|
||||
{
|
||||
if (args.Source == null)
|
||||
return false;
|
||||
if (args.Source.ThermalEnergy < Min)
|
||||
return false;
|
||||
if (args.Source.ThermalEnergy > Max)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user