Fix and refactor SolutionContainerSystem.RemoveEachReagent (#7245)
This commit is contained in:
committed by
GitHub
parent
be6431f7aa
commit
46ac70a734
@@ -569,10 +569,10 @@ namespace Content.Server.Botany.Components
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
var amt = FixedPoint2.New(1);
|
var amt = FixedPoint2.New(1);
|
||||||
foreach (var reagent in solutionSystem.RemoveEachReagent(Owner, solution, amt))
|
foreach (var (reagentId, quantity) in solutionSystem.RemoveEachReagent(Owner, solution, amt))
|
||||||
{
|
{
|
||||||
var reagentProto = _prototypeManager.Index<ReagentPrototype>(reagent);
|
var reagentProto = _prototypeManager.Index<ReagentPrototype>(reagentId);
|
||||||
reagentProto.ReactionPlant(Owner, new Solution.ReagentQuantity(reagent, amt), solution);
|
reagentProto.ReactionPlant(Owner, new Solution.ReagentQuantity(reagentId, quantity), solution);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -290,35 +290,33 @@ public sealed partial class SolutionContainerSystem : EntitySystem
|
|||||||
return solutionsMgr.Solutions[name];
|
return solutionsMgr.Solutions[name];
|
||||||
}
|
}
|
||||||
|
|
||||||
public string[] RemoveEachReagent(EntityUid uid, Solution solution, FixedPoint2 quantity)
|
/// <summary>
|
||||||
|
/// Removes an amount from all reagents in a solution, adding it to a new solution.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="uid">The entity containing the solution.</param>
|
||||||
|
/// <param name="solution">The solution to remove reagents from.</param>
|
||||||
|
/// <param name="quantity">The amount to remove from every reagent in the solution.</param>
|
||||||
|
/// <returns>A new solution containing every removed reagent from the original solution.</returns>
|
||||||
|
public Solution RemoveEachReagent(EntityUid uid, Solution solution, FixedPoint2 quantity)
|
||||||
{
|
{
|
||||||
var removedReagent = new string[solution.Contents.Count];
|
|
||||||
if (quantity <= 0)
|
if (quantity <= 0)
|
||||||
return Array.Empty<string>();
|
return new Solution();
|
||||||
|
|
||||||
var pos = 0;
|
var removedSolution = new Solution();
|
||||||
for (var i = 0; i < solution.Contents.Count; i++)
|
|
||||||
|
// RemoveReagent does a RemoveSwap, meaning we don't have to copy the list if we iterate it backwards.
|
||||||
|
for (var i = solution.Contents.Count-1; i >= 0; i--)
|
||||||
{
|
{
|
||||||
var (reagentId, curQuantity) = solution.Contents[i];
|
var (reagentId, _) = solution.Contents[i];
|
||||||
removedReagent[pos++] = reagentId;
|
|
||||||
if (!_prototypeManager.TryIndex(reagentId, out ReagentPrototype? proto))
|
|
||||||
proto = new ReagentPrototype();
|
|
||||||
|
|
||||||
var newQuantity = curQuantity - quantity;
|
var removedQuantity = solution.RemoveReagent(reagentId, quantity);
|
||||||
if (newQuantity <= 0)
|
|
||||||
{
|
if(removedQuantity > 0)
|
||||||
solution.Contents.RemoveSwap(i);
|
removedSolution.AddReagent(reagentId, removedQuantity);
|
||||||
solution.TotalVolume -= curQuantity;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
solution.Contents[i] = new Solution.ReagentQuantity(reagentId, newQuantity);
|
|
||||||
solution.TotalVolume -= quantity;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
UpdateChemicals(uid, solution);
|
UpdateChemicals(uid, solution);
|
||||||
return removedReagent;
|
return removedSolution;
|
||||||
}
|
}
|
||||||
|
|
||||||
public FixedPoint2 GetReagentQuantity(EntityUid owner, string reagentId)
|
public FixedPoint2 GetReagentQuantity(EntityUid owner, string reagentId)
|
||||||
|
|||||||
@@ -167,34 +167,41 @@ namespace Content.Shared.Chemistry.Components
|
|||||||
return FixedPoint2.New(0);
|
return FixedPoint2.New(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RemoveReagent(string reagentId, FixedPoint2 quantity)
|
/// <summary>
|
||||||
|
/// Attempts to remove an amount of reagent from the solution.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="reagentId">The reagent to be removed.</param>
|
||||||
|
/// <param name="quantity">The amount of reagent to remove.</param>
|
||||||
|
/// <returns>How much reagent was actually removed. Zero if the reagent is not present on the solution.</returns>
|
||||||
|
public FixedPoint2 RemoveReagent(string reagentId, FixedPoint2 quantity)
|
||||||
{
|
{
|
||||||
if(quantity <= 0)
|
if(quantity <= 0)
|
||||||
return;
|
return FixedPoint2.Zero;
|
||||||
|
|
||||||
for (var i = 0; i < Contents.Count; i++)
|
for (var i = 0; i < Contents.Count; i++)
|
||||||
{
|
{
|
||||||
var reagent = Contents[i];
|
var reagent = Contents[i];
|
||||||
|
|
||||||
if(reagent.ReagentId != reagentId)
|
if(reagent.ReagentId != reagentId)
|
||||||
continue;
|
continue;
|
||||||
if (!IoCManager.Resolve<IPrototypeManager>().TryIndex(reagentId, out ReagentPrototype? proto))
|
|
||||||
proto = new ReagentPrototype();
|
|
||||||
|
|
||||||
var curQuantity = reagent.Quantity;
|
var curQuantity = reagent.Quantity;
|
||||||
var newQuantity = curQuantity - quantity;
|
var newQuantity = curQuantity - quantity;
|
||||||
|
|
||||||
if (newQuantity <= 0)
|
if (newQuantity <= 0)
|
||||||
{
|
{
|
||||||
Contents.RemoveSwap(i);
|
Contents.RemoveSwap(i);
|
||||||
TotalVolume -= curQuantity;
|
TotalVolume -= curQuantity;
|
||||||
}
|
return curQuantity;
|
||||||
else
|
|
||||||
{
|
|
||||||
Contents[i] = new ReagentQuantity(reagentId, newQuantity);
|
|
||||||
TotalVolume -= quantity;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
Contents[i] = new ReagentQuantity(reagentId, newQuantity);
|
||||||
|
TotalVolume -= quantity;
|
||||||
|
return quantity;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Reagent is not on the solution...
|
||||||
|
return FixedPoint2.Zero;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user