Solution rejig (#12428)
This commit is contained in:
@@ -51,13 +51,7 @@ public sealed partial class SolutionContainerSystem : EntitySystem
|
||||
foreach (var (name, solutionHolder) in component.Solutions)
|
||||
{
|
||||
solutionHolder.Name = name;
|
||||
if (solutionHolder.MaxVolume == FixedPoint2.Zero)
|
||||
{
|
||||
solutionHolder.MaxVolume = solutionHolder.TotalVolume > solutionHolder.InitialMaxVolume
|
||||
? solutionHolder.TotalVolume
|
||||
: solutionHolder.InitialMaxVolume;
|
||||
}
|
||||
|
||||
solutionHolder.ValidateSolution();
|
||||
UpdateAppearance(uid, solutionHolder);
|
||||
}
|
||||
}
|
||||
@@ -70,14 +64,14 @@ public sealed partial class SolutionContainerSystem : EntitySystem
|
||||
|| !solutionsManager.Solutions.TryGetValue(examinableComponent.Solution, out var solutionHolder))
|
||||
return;
|
||||
|
||||
if (solutionHolder.Contents.Count == 0)
|
||||
var primaryReagent = solutionHolder.GetPrimaryReagentId();
|
||||
|
||||
if (string.IsNullOrEmpty(primaryReagent))
|
||||
{
|
||||
args.PushText(Loc.GetString("shared-solution-container-component-on-examine-empty-container"));
|
||||
return;
|
||||
}
|
||||
|
||||
var primaryReagent = solutionHolder.GetPrimaryReagentId();
|
||||
|
||||
if (!_prototypeManager.TryIndex(primaryReagent, out ReagentPrototype? proto))
|
||||
{
|
||||
Logger.Error(
|
||||
@@ -85,7 +79,7 @@ public sealed partial class SolutionContainerSystem : EntitySystem
|
||||
return;
|
||||
}
|
||||
|
||||
var colorHex = solutionHolder.Color
|
||||
var colorHex = solutionHolder.GetColor(_prototypeManager)
|
||||
.ToHexNoAlpha(); //TODO: If the chem has a dark color, the examine text becomes black on a black background, which is unreadable.
|
||||
var messageString = "shared-solution-container-component-on-examine-main-text";
|
||||
|
||||
@@ -104,9 +98,9 @@ public sealed partial class SolutionContainerSystem : EntitySystem
|
||||
|| !Resolve(uid, ref appearanceComponent, false))
|
||||
return;
|
||||
|
||||
var filledVolumePercent = Math.Min(1.0f, solution.CurrentVolume.Float() / solution.MaxVolume.Float());
|
||||
var filledVolumePercent = solution.FillFraction * 100;
|
||||
appearanceComponent.SetData(SolutionContainerVisuals.VisualState,
|
||||
new SolutionContainerVisualState(solution.Color, filledVolumePercent));
|
||||
new SolutionContainerVisualState(solution.GetColor(_prototypeManager), filledVolumePercent));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -139,7 +133,7 @@ public sealed partial class SolutionContainerSystem : EntitySystem
|
||||
|
||||
public void RemoveAllSolution(EntityUid uid, Solution solutionHolder)
|
||||
{
|
||||
if (solutionHolder.CurrentVolume == 0)
|
||||
if (solutionHolder.Volume == 0)
|
||||
return;
|
||||
|
||||
solutionHolder.RemoveAllSolution();
|
||||
@@ -169,8 +163,8 @@ public sealed partial class SolutionContainerSystem : EntitySystem
|
||||
return;
|
||||
|
||||
targetSolution.MaxVolume = capacity;
|
||||
if (capacity < targetSolution.CurrentVolume)
|
||||
targetSolution.RemoveSolution(targetSolution.CurrentVolume - capacity);
|
||||
if (capacity < targetSolution.Volume)
|
||||
targetSolution.RemoveSolution(targetSolution.Volume - capacity);
|
||||
|
||||
UpdateChemicals(targetUid, targetSolution);
|
||||
}
|
||||
@@ -188,14 +182,20 @@ public sealed partial class SolutionContainerSystem : EntitySystem
|
||||
out FixedPoint2 acceptedQuantity, float? temperature = null)
|
||||
{
|
||||
acceptedQuantity = targetSolution.AvailableVolume > quantity ? quantity : targetSolution.AvailableVolume;
|
||||
targetSolution.AddReagent(reagentId, acceptedQuantity, temperature);
|
||||
|
||||
if (acceptedQuantity > 0)
|
||||
UpdateChemicals(targetUid, targetSolution, true);
|
||||
if (acceptedQuantity <= 0)
|
||||
return quantity == 0;
|
||||
|
||||
if (temperature == null)
|
||||
targetSolution.AddReagent(reagentId, acceptedQuantity);
|
||||
else
|
||||
targetSolution.AddReagent(_prototypeManager.Index<ReagentPrototype>(reagentId), acceptedQuantity, temperature.Value, _prototypeManager);
|
||||
|
||||
UpdateChemicals(targetUid, targetSolution, true);
|
||||
return acceptedQuantity == quantity;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Removes reagent of an Id to the container.
|
||||
/// </summary>
|
||||
@@ -224,10 +224,10 @@ public sealed partial class SolutionContainerSystem : EntitySystem
|
||||
public bool TryAddSolution(EntityUid targetUid, Solution? targetSolution, Solution addedSolution)
|
||||
{
|
||||
if (targetSolution == null
|
||||
|| !targetSolution.CanAddSolution(addedSolution) || addedSolution.TotalVolume == 0)
|
||||
|| !targetSolution.CanAddSolution(addedSolution) || addedSolution.Volume == 0)
|
||||
return false;
|
||||
|
||||
targetSolution.AddSolution(addedSolution);
|
||||
targetSolution.AddSolution(addedSolution, _prototypeManager);
|
||||
UpdateChemicals(targetUid, targetSolution, true);
|
||||
return true;
|
||||
}
|
||||
@@ -245,13 +245,13 @@ public sealed partial class SolutionContainerSystem : EntitySystem
|
||||
if (quantity < 0)
|
||||
return TryTransferSolution(targetUid, sourceUid, target, source, -quantity);
|
||||
|
||||
quantity = FixedPoint2.Min(quantity, target.AvailableVolume, source.CurrentVolume);
|
||||
quantity = FixedPoint2.Min(quantity, target.AvailableVolume, source.Volume);
|
||||
if (quantity == 0)
|
||||
return false;
|
||||
|
||||
// TODO after #12428 is merged, this should be made into a function that directly transfers reagents.
|
||||
// currently this is quite inefficient.
|
||||
target.AddSolution(source.SplitSolution(quantity));
|
||||
// TODO This should be made into a function that directly transfers reagents. currently this is quite
|
||||
// inefficient.
|
||||
target.AddSolution(source.SplitSolution(quantity), _prototypeManager);
|
||||
|
||||
UpdateChemicals(sourceUid, source, false);
|
||||
UpdateChemicals(targetUid, target, true);
|
||||
@@ -294,16 +294,16 @@ public sealed partial class SolutionContainerSystem : EntitySystem
|
||||
FixedPoint2 overflowThreshold,
|
||||
[NotNullWhen(true)] out Solution? overflowingSolution)
|
||||
{
|
||||
if (addedSolution.TotalVolume == 0 || overflowThreshold > targetSolution.MaxVolume)
|
||||
if (addedSolution.Volume == 0 || overflowThreshold > targetSolution.MaxVolume)
|
||||
{
|
||||
overflowingSolution = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
targetSolution.AddSolution(addedSolution);
|
||||
targetSolution.AddSolution(addedSolution, _prototypeManager);
|
||||
UpdateChemicals(targetUid, targetSolution, true);
|
||||
overflowingSolution = targetSolution.SplitSolution(FixedPoint2.Max(FixedPoint2.Zero,
|
||||
targetSolution.CurrentVolume - overflowThreshold));
|
||||
targetSolution.Volume - overflowThreshold));
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -320,14 +320,16 @@ public sealed partial class SolutionContainerSystem : EntitySystem
|
||||
return solutionsMgr.Solutions.TryGetValue(name, out solution);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Will ensure a solution is added to given entity even if it's missing solutionContainerManager
|
||||
/// </summary>
|
||||
/// <param name="uid">EntityUid to which to add solution</param>
|
||||
/// <param name="name">name for the solution</param>
|
||||
/// <param name="solutionsMgr">solution components used in resolves</param>
|
||||
/// <param name="existed">true if the solution already existed</param>
|
||||
/// <returns>solution</returns>
|
||||
public Solution EnsureSolution(EntityUid uid, string name,
|
||||
public Solution EnsureSolution(EntityUid uid, string name, out bool existed,
|
||||
SolutionContainerManagerComponent? solutionsMgr = null)
|
||||
{
|
||||
if (!Resolve(uid, ref solutionsMgr, false))
|
||||
@@ -335,15 +337,76 @@ public sealed partial class SolutionContainerSystem : EntitySystem
|
||||
solutionsMgr = EntityManager.EnsureComponent<SolutionContainerManagerComponent>(uid);
|
||||
}
|
||||
|
||||
if (!solutionsMgr.Solutions.ContainsKey(name))
|
||||
if (!solutionsMgr.Solutions.TryGetValue(name, out var existing))
|
||||
{
|
||||
var newSolution = new Solution() { Name = name };
|
||||
solutionsMgr.Solutions.Add(name, newSolution);
|
||||
existed = false;
|
||||
return newSolution;
|
||||
}
|
||||
|
||||
return solutionsMgr.Solutions[name];
|
||||
existed = true;
|
||||
return existing;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Will ensure a solution is added to given entity even if it's missing solutionContainerManager
|
||||
/// </summary>
|
||||
/// <param name="uid">EntityUid to which to add solution</param>
|
||||
/// <param name="name">name for the solution</param>
|
||||
/// <param name="solutionsMgr">solution components used in resolves</param>
|
||||
/// <returns>solution</returns>
|
||||
public Solution EnsureSolution(EntityUid uid, string name, SolutionContainerManagerComponent? solutionsMgr = null)
|
||||
=> EnsureSolution(uid, name, out _, solutionsMgr);
|
||||
|
||||
/// <summary>
|
||||
/// Will ensure a solution is added to given entity even if it's missing solutionContainerManager
|
||||
/// </summary>
|
||||
/// <param name="uid">EntityUid to which to add solution</param>
|
||||
/// <param name="name">name for the solution</param>
|
||||
/// <param name="minVol">Ensures that the solution's maximum volume is larger than this value./param>
|
||||
/// <param name="solutionsMgr">solution components used in resolves</param>
|
||||
/// <returns>solution</returns>
|
||||
public Solution EnsureSolution(EntityUid uid, string name, FixedPoint2 minVol, out bool existed,
|
||||
SolutionContainerManagerComponent? solutionsMgr = null)
|
||||
{
|
||||
if (!Resolve(uid, ref solutionsMgr, false))
|
||||
{
|
||||
solutionsMgr = EntityManager.EnsureComponent<SolutionContainerManagerComponent>(uid);
|
||||
}
|
||||
|
||||
if (!solutionsMgr.Solutions.TryGetValue(name, out var existing))
|
||||
{
|
||||
var newSolution = new Solution() { Name = name };
|
||||
solutionsMgr.Solutions.Add(name, newSolution);
|
||||
existed = false;
|
||||
newSolution.MaxVolume = minVol;
|
||||
return newSolution;
|
||||
}
|
||||
|
||||
existed = true;
|
||||
existing.MaxVolume = FixedPoint2.Max(existing.MaxVolume, minVol);
|
||||
return existing;
|
||||
}
|
||||
|
||||
public Solution EnsureSolution(EntityUid uid, string name,
|
||||
IEnumerable<Solution.ReagentQuantity> reagents,
|
||||
bool setMaxVol = true,
|
||||
SolutionContainerManagerComponent? solutionsMgr = null)
|
||||
{
|
||||
if (!Resolve(uid, ref solutionsMgr, false))
|
||||
solutionsMgr = EntityManager.EnsureComponent<SolutionContainerManagerComponent>(uid);
|
||||
|
||||
if (!solutionsMgr.Solutions.TryGetValue(name, out var existing))
|
||||
{
|
||||
var newSolution = new Solution(reagents, setMaxVol);
|
||||
solutionsMgr.Solutions.Add(name, newSolution);
|
||||
return newSolution;
|
||||
}
|
||||
|
||||
existing.SetContents(reagents, setMaxVol);
|
||||
return existing;
|
||||
}
|
||||
/// <summary>
|
||||
/// Removes an amount from all reagents in a solution, adding it to a new solution.
|
||||
/// </summary>
|
||||
@@ -446,10 +509,8 @@ public sealed partial class SolutionContainerSystem : EntitySystem
|
||||
/// <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;
|
||||
var heatCap = solution.GetHeatCapacity(_prototypeManager);
|
||||
solution.Temperature = heatCap == 0 ? 0 : thermalEnergy / heatCap;
|
||||
UpdateChemicals(owner, solution, true);
|
||||
}
|
||||
|
||||
@@ -464,7 +525,8 @@ public sealed partial class SolutionContainerSystem : EntitySystem
|
||||
if (thermalEnergy == 0.0f)
|
||||
return;
|
||||
|
||||
solution.ThermalEnergy += thermalEnergy;
|
||||
var heatCap = solution.GetHeatCapacity(_prototypeManager);
|
||||
solution.Temperature += heatCap == 0 ? 0 : thermalEnergy / heatCap;
|
||||
UpdateChemicals(owner, solution, true);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user