From d2ef2a8c45d167eba6e5a5a989de0bcc82a807d3 Mon Sep 17 00:00:00 2001 From: Whatstone Date: Tue, 18 Jun 2024 14:26:20 -0400 Subject: [PATCH] SplitSolutionPerReagent: suggested fixes --- .../Chemistry/Components/Solution.cs | 44 +++++++++++++++---- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/Content.Shared/Chemistry/Components/Solution.cs b/Content.Shared/Chemistry/Components/Solution.cs index bb2fc6254e3..6e2e0305e8e 100644 --- a/Content.Shared/Chemistry/Components/Solution.cs +++ b/Content.Shared/Chemistry/Components/Solution.cs @@ -675,7 +675,7 @@ public Solution SplitSolution(FixedPoint2 toTake) return newSolution; } - // Frontier: cryogenics per-reagent filter function (#1443) + // Frontier: cryogenics per-reagent filter function (#1443, #1533) /// /// Splits a solution, taking the specified amount of each reagent from the solution. /// If any reagent in the solution has less volume than specified, it will all be transferred into the new solution. @@ -704,13 +704,26 @@ public Solution SplitSolutionPerReagent(FixedPoint2 toTakePer) else { Contents.RemoveSwap(i); - newSolution.Contents.Add(new ReagentQuantity(reagent, quantity)); - Volume -= quantity; + //Only add positive quantities to our new solution. + if (quantity > 0) + { + newSolution.Contents.Add(new ReagentQuantity(reagent, quantity)); + Volume -= quantity; + } } } - newSolution.Volume = origVol - Volume; - _heatCapacityDirty = true; + // If old solution is empty, invalidate old solution and transfer all volume to new. + if (Volume <= 0) + { + RemoveAllSolution(); + newSolution.Volume = origVol; + } + else + { + newSolution.Volume = origVol - Volume; + _heatCapacityDirty = true; + } newSolution._heatCapacityDirty = true; ValidateSolution(); @@ -751,13 +764,26 @@ public Solution SplitSolutionPerReagentWithOnly(FixedPoint2 toTakePer, params st else { Contents.RemoveSwap(i); - newSolution.Contents.Add(new ReagentQuantity(reagent, quantity)); - Volume -= quantity; + //Only add positive quantities to our new solution. + if (quantity > 0) + { + newSolution.Contents.Add(new ReagentQuantity(reagent, quantity)); + Volume -= quantity; + } } } - newSolution.Volume = origVol - Volume; - _heatCapacityDirty = true; + // If old solution is empty, invalidate old solution and transfer all volume to new. + if (Volume <= 0) + { + RemoveAllSolution(); + newSolution.Volume = origVol; + } + else + { + newSolution.Volume = origVol - Volume; + _heatCapacityDirty = true; + } newSolution._heatCapacityDirty = true; ValidateSolution();