From dc666218042ca474012c15bd987fc43b09cd7ee6 Mon Sep 17 00:00:00 2001 From: PrPleGoo Date: Sat, 14 Mar 2020 14:04:08 +0100 Subject: [PATCH] Replaced static Rounders with an impleneted interface --- Content.Client/ClientContentIoC.cs | 3 ++ .../Chemistry/Metabolism/DefaultFood.cs | 10 +++--- .../Components/Chemistry/SolutionComponent.cs | 26 +++++++------- Content.Server/ServerContentIoC.cs | 5 ++- .../Chemistry/DefaultMetabolizable.cs | 10 +++--- .../Chemistry/RounderForReagents.cs | 14 ++++++++ Content.Shared/Chemistry/Solution.cs | 24 +++++++------ .../Components/Chemistry/SolutionComponent.cs | 36 +++++++++---------- .../Chemistry/IRounderForReagents.cs | 7 ++++ Content.Shared/Maths/Rounders.cs | 12 ------- .../Prototypes/Entities/items/chemistry.yml | 14 ++++---- 11 files changed, 91 insertions(+), 70 deletions(-) create mode 100644 Content.Shared/Chemistry/RounderForReagents.cs create mode 100644 Content.Shared/Interfaces/Chemistry/IRounderForReagents.cs delete mode 100644 Content.Shared/Maths/Rounders.cs diff --git a/Content.Client/ClientContentIoC.cs b/Content.Client/ClientContentIoC.cs index 6a8e9ea10a..d26587104c 100644 --- a/Content.Client/ClientContentIoC.cs +++ b/Content.Client/ClientContentIoC.cs @@ -7,7 +7,9 @@ using Content.Client.Parallax; using Content.Client.Sandbox; using Content.Client.UserInterface; using Content.Client.Utility; +using Content.Shared.Chemistry; using Content.Shared.Interfaces; +using Content.Shared.Interfaces.Chemistry; using Robust.Shared.IoC; namespace Content.Client @@ -27,6 +29,7 @@ namespace Content.Client IoCManager.Register(); IoCManager.Register(); IoCManager.Register(); + IoCManager.Register(); } } } diff --git a/Content.Server/Chemistry/Metabolism/DefaultFood.cs b/Content.Server/Chemistry/Metabolism/DefaultFood.cs index 4de983702a..aca26e2628 100644 --- a/Content.Server/Chemistry/Metabolism/DefaultFood.cs +++ b/Content.Server/Chemistry/Metabolism/DefaultFood.cs @@ -1,9 +1,8 @@ -using System; -using Content.Server.GameObjects.Components.Nutrition; +using Content.Server.GameObjects.Components.Nutrition; using Content.Shared.Interfaces.Chemistry; -using Content.Shared.Maths; using Robust.Shared.Interfaces.GameObjects; using Robust.Shared.Interfaces.Serialization; +using Robust.Shared.IoC; using Robust.Shared.Serialization; namespace Content.Server.Chemistry.Metabolism @@ -14,6 +13,9 @@ namespace Content.Server.Chemistry.Metabolism /// class DefaultFood : IMetabolizable { +#pragma warning disable 649 + [Dependency] private readonly IRounderForReagents _rounder; +#pragma warning restore 649 //Rate of metabolism in units / second private decimal _metabolismRate; public decimal MetabolismRate => _metabolismRate; @@ -31,7 +33,7 @@ namespace Content.Server.Chemistry.Metabolism //Remove reagent at set rate, satiate hunger if a HungerComponent can be found decimal IMetabolizable.Metabolize(IEntity solutionEntity, string reagentId, float tickTime) { - var metabolismAmount = (MetabolismRate * (decimal) tickTime).RoundForReagents(); + var metabolismAmount = _rounder.Round(MetabolismRate * (decimal) tickTime); if (solutionEntity.TryGetComponent(out HungerComponent hunger)) hunger.UpdateFood((float)(metabolismAmount * NutritionFactor)); diff --git a/Content.Server/GameObjects/Components/Chemistry/SolutionComponent.cs b/Content.Server/GameObjects/Components/Chemistry/SolutionComponent.cs index b612c4052d..c5a815752a 100644 --- a/Content.Server/GameObjects/Components/Chemistry/SolutionComponent.cs +++ b/Content.Server/GameObjects/Components/Chemistry/SolutionComponent.cs @@ -1,13 +1,8 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel.Design; -using Content.Server.Chemistry; -using Content.Server.GameObjects.Components.Nutrition; +using Content.Server.Chemistry; using Content.Server.GameObjects.EntitySystems; -using Content.Server.Interfaces; using Content.Shared.Chemistry; using Content.Shared.GameObjects; -using Content.Shared.Maths; +using Content.Shared.Interfaces.Chemistry; using Robust.Server.GameObjects.EntitySystems; using Robust.Shared.GameObjects; using Robust.Shared.Interfaces.GameObjects; @@ -15,6 +10,8 @@ using Robust.Shared.IoC; using Robust.Shared.Localization; using Robust.Shared.Prototypes; using Robust.Shared.Utility; +using System; +using System.Collections.Generic; namespace Content.Server.GameObjects.Components.Chemistry { @@ -25,6 +22,7 @@ namespace Content.Server.GameObjects.Components.Chemistry internal class SolutionComponent : Shared.GameObjects.Components.Chemistry.SolutionComponent, IExamine { #pragma warning disable 649 + [Dependency] private readonly IRounderForReagents _rounder; [Dependency] private readonly IPrototypeManager _prototypeManager; [Dependency] private readonly ILocalizationManager _loc; [Dependency] private readonly IEntitySystemManager _entitySystemManager; @@ -226,8 +224,8 @@ namespace Content.Server.GameObjects.Components.Chemistry public bool TryAddReagent(string reagentId, decimal quantity, out decimal acceptedQuantity, bool skipReactionCheck = false, bool skipColor = false) { - quantity = quantity.RoundForReagents(); - var toAcceptQuantity = (_maxVolume - _containedSolution.TotalVolume).RoundForReagents(); + quantity = _rounder.Round(quantity); + var toAcceptQuantity = _rounder.Round(MaxVolume - ContainedSolution.TotalVolume); if (quantity > toAcceptQuantity) { acceptedQuantity = toAcceptQuantity; @@ -238,7 +236,7 @@ namespace Content.Server.GameObjects.Components.Chemistry acceptedQuantity = quantity; } - _containedSolution.AddReagent(reagentId, acceptedQuantity); + ContainedSolution.AddReagent(reagentId, acceptedQuantity); if (!skipColor) { RecalculateColor(); } @@ -250,10 +248,10 @@ namespace Content.Server.GameObjects.Components.Chemistry public bool TryAddSolution(Solution solution, bool skipReactionCheck = false, bool skipColor = false) { - if (solution.TotalVolume > (_maxVolume - _containedSolution.TotalVolume)) + if (solution.TotalVolume > (MaxVolume - ContainedSolution.TotalVolume)) return false; - _containedSolution.AddSolution(solution); + ContainedSolution.AddSolution(solution); if (!skipColor) { RecalculateColor(); } @@ -279,7 +277,7 @@ namespace Content.Server.GameObjects.Components.Chemistry { return false; } - var currentUnitReactions = (reagentQuantity / reactant.Value.Amount).RoundForReagents(); + var currentUnitReactions = _rounder.Round(reagentQuantity / reactant.Value.Amount); if (currentUnitReactions < unitReactions) { unitReactions = currentUnitReactions; @@ -309,7 +307,7 @@ namespace Content.Server.GameObjects.Components.Chemistry { if (!reactant.Value.Catalyst) { - var amountToRemove = (unitReactions * reactant.Value.Amount).RoundForReagents(); + var amountToRemove = _rounder.Round(unitReactions * reactant.Value.Amount); TryRemoveReagent(reactant.Key, amountToRemove); } } diff --git a/Content.Server/ServerContentIoC.cs b/Content.Server/ServerContentIoC.cs index ec9ff2a876..1c5937c0fe 100644 --- a/Content.Server/ServerContentIoC.cs +++ b/Content.Server/ServerContentIoC.cs @@ -1,4 +1,4 @@ -using Content.Server.Cargo; +using Content.Server.Cargo; using Content.Server.Chat; using Content.Server.GameTicking; using Content.Server.Interfaces; @@ -7,7 +7,9 @@ using Content.Server.Interfaces.GameTicking; using Content.Server.Preferences; using Content.Server.Sandbox; using Content.Server.Utility; +using Content.Shared.Chemistry; using Content.Shared.Interfaces; +using Content.Shared.Interfaces.Chemistry; using Robust.Shared.IoC; namespace Content.Server @@ -26,6 +28,7 @@ namespace Content.Server IoCManager.Register(); IoCManager.Register(); IoCManager.Register(); + IoCManager.Register(); } } } diff --git a/Content.Shared/Chemistry/DefaultMetabolizable.cs b/Content.Shared/Chemistry/DefaultMetabolizable.cs index 7ceb3372c4..31d91d351b 100644 --- a/Content.Shared/Chemistry/DefaultMetabolizable.cs +++ b/Content.Shared/Chemistry/DefaultMetabolizable.cs @@ -1,8 +1,7 @@ -using System; -using Content.Shared.Interfaces.Chemistry; -using Content.Shared.Maths; +using Content.Shared.Interfaces.Chemistry; using Robust.Shared.Interfaces.GameObjects; using Robust.Shared.Interfaces.Serialization; +using Robust.Shared.IoC; using Robust.Shared.Serialization; namespace Content.Shared.Chemistry @@ -10,6 +9,9 @@ namespace Content.Shared.Chemistry //Default metabolism for reagents. Metabolizes the reagent with no effects class DefaultMetabolizable : IMetabolizable { +#pragma warning disable 649 + [Dependency] private readonly IRounderForReagents _rounder; +#pragma warning restore 649 //Rate of metabolism in units / second private decimal _metabolismRate = 1; public decimal MetabolismRate => _metabolismRate; @@ -21,7 +23,7 @@ namespace Content.Shared.Chemistry decimal IMetabolizable.Metabolize(IEntity solutionEntity, string reagentId, float tickTime) { - var metabolismAmount = (MetabolismRate * (decimal)tickTime).RoundForReagents(); + var metabolismAmount = _rounder.Round(MetabolismRate * (decimal)tickTime); return metabolismAmount; } } diff --git a/Content.Shared/Chemistry/RounderForReagents.cs b/Content.Shared/Chemistry/RounderForReagents.cs new file mode 100644 index 0000000000..d7c6ccce11 --- /dev/null +++ b/Content.Shared/Chemistry/RounderForReagents.cs @@ -0,0 +1,14 @@ +using Content.Shared.Interfaces.Chemistry; +using System; + +namespace Content.Shared.Chemistry +{ + + public class RounderForReagents : IRounderForReagents + { + public decimal Round(decimal value) + { + return Math.Round(value, 2); + } + } +} diff --git a/Content.Shared/Chemistry/Solution.cs b/Content.Shared/Chemistry/Solution.cs index e10d99f389..109b780108 100644 --- a/Content.Shared/Chemistry/Solution.cs +++ b/Content.Shared/Chemistry/Solution.cs @@ -1,12 +1,13 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Diagnostics.CodeAnalysis; -using Content.Shared.Maths; +using Content.Shared.Interfaces.Chemistry; using Robust.Shared.Interfaces.Serialization; +using Robust.Shared.IoC; using Robust.Shared.Serialization; using Robust.Shared.Utility; using Robust.Shared.ViewVariables; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; namespace Content.Shared.Chemistry { @@ -15,6 +16,9 @@ namespace Content.Shared.Chemistry /// public class Solution : IExposeData, IEnumerable { +#pragma warning disable 649 + [Dependency] private readonly IRounderForReagents _rounder; +#pragma warning restore 649 // Most objects on the station hold only 1 or 2 reagents [ViewVariables] private List _contents = new List(2); @@ -63,7 +67,7 @@ namespace Content.Shared.Chemistry /// The quantity in milli-units. public void AddReagent(string reagentId, decimal quantity) { - quantity = quantity.RoundForReagents(); + quantity = _rounder.Round(quantity); if (quantity <= 0) return; @@ -111,7 +115,7 @@ namespace Content.Shared.Chemistry var curQuantity = reagent.Quantity; - var newQuantity = (curQuantity - quantity).RoundForReagents(); + var newQuantity = _rounder.Round(curQuantity - quantity); if (newQuantity <= 0) { _contents.RemoveSwap(i); @@ -136,7 +140,7 @@ namespace Content.Shared.Chemistry if(quantity <= 0) return; - var ratio = (TotalVolume - quantity).RoundForReagents() / TotalVolume; + var ratio = _rounder.Round(TotalVolume - quantity) / TotalVolume; if (ratio <= 0) { @@ -151,12 +155,12 @@ namespace Content.Shared.Chemistry // quantity taken is always a little greedy, so fractional quantities get rounded up to the nearest // whole unit. This should prevent little bits of chemical remaining because of float rounding errors. - var newQuantity = (oldQuantity * ratio).RoundForReagents(); + var newQuantity = _rounder.Round(oldQuantity * ratio); _contents[i] = new ReagentQuantity(reagent.ReagentId, newQuantity); } - TotalVolume = (TotalVolume * ratio).RoundForReagents(); + TotalVolume = _rounder.Round(TotalVolume * ratio); } public void RemoveAllSolution() diff --git a/Content.Shared/GameObjects/Components/Chemistry/SolutionComponent.cs b/Content.Shared/GameObjects/Components/Chemistry/SolutionComponent.cs index 71ba3c6c91..8a1f2b8a58 100644 --- a/Content.Shared/GameObjects/Components/Chemistry/SolutionComponent.cs +++ b/Content.Shared/GameObjects/Components/Chemistry/SolutionComponent.cs @@ -1,12 +1,12 @@ -using System; -using System.Collections.Generic; -using Content.Shared.Chemistry; +using Content.Shared.Chemistry; using Robust.Shared.GameObjects; using Robust.Shared.IoC; using Robust.Shared.Maths; using Robust.Shared.Prototypes; using Robust.Shared.Serialization; using Robust.Shared.ViewVariables; +using System; +using System.Collections.Generic; namespace Content.Shared.GameObjects.Components.Chemistry { @@ -17,8 +17,8 @@ namespace Content.Shared.GameObjects.Components.Chemistry #pragma warning restore 649 [ViewVariables] - protected Solution _containedSolution = new Solution(); - protected decimal _maxVolume; + protected Solution ContainedSolution = new Solution(); + private decimal _maxVolume; private SolutionCaps _capabilities; /// @@ -40,7 +40,7 @@ namespace Content.Shared.GameObjects.Components.Chemistry /// The total volume of all the of the reagents in the container. /// [ViewVariables] - public decimal CurrentVolume => _containedSolution.TotalVolume; + public decimal CurrentVolume => ContainedSolution.TotalVolume; /// /// The volume without reagents remaining in the container. @@ -64,7 +64,7 @@ namespace Content.Shared.GameObjects.Components.Chemistry set => _capabilities = value; } - public IReadOnlyList ReagentList => _containedSolution.Contents; + public IReadOnlyList ReagentList => ContainedSolution.Contents; /// /// Shortcut for Capabilities PourIn flag to avoid binary operators. @@ -94,8 +94,8 @@ namespace Content.Shared.GameObjects.Components.Chemistry { base.ExposeData(serializer); - serializer.DataField(ref _maxVolume, "maxVol", 0); - serializer.DataField(ref _containedSolution, "contents", _containedSolution); + serializer.DataField(ref _maxVolume, "maxVol", 0M); + serializer.DataField(ref ContainedSolution, "contents", ContainedSolution); serializer.DataField(ref _capabilities, "caps", SolutionCaps.None); } @@ -112,13 +112,13 @@ namespace Content.Shared.GameObjects.Components.Chemistry { base.Shutdown(); - _containedSolution.RemoveAllSolution(); - _containedSolution = new Solution(); + ContainedSolution.RemoveAllSolution(); + ContainedSolution = new Solution(); } public void RemoveAllSolution() { - _containedSolution.RemoveAllSolution(); + ContainedSolution.RemoveAllSolution(); OnSolutionChanged(); } @@ -126,7 +126,7 @@ namespace Content.Shared.GameObjects.Components.Chemistry { if (!ContainsReagent(reagentId, out var currentQuantity)) return false; - _containedSolution.RemoveReagent(reagentId, quantity); + ContainedSolution.RemoveReagent(reagentId, quantity); OnSolutionChanged(); return true; } @@ -141,27 +141,27 @@ namespace Content.Shared.GameObjects.Components.Chemistry if (CurrentVolume == 0) return false; - _containedSolution.RemoveSolution(quantity); + ContainedSolution.RemoveSolution(quantity); OnSolutionChanged(); return true; } public Solution SplitSolution(decimal quantity) { - var solutionSplit = _containedSolution.SplitSolution(quantity); + var solutionSplit = ContainedSolution.SplitSolution(quantity); OnSolutionChanged(); return solutionSplit; } protected void RecalculateColor() { - if(_containedSolution.TotalVolume == 0) + if(ContainedSolution.TotalVolume == 0) SubstanceColor = Color.White; Color mixColor = default; var runningTotalQuantity = 0M; - foreach (var reagent in _containedSolution) + foreach (var reagent in ContainedSolution) { runningTotalQuantity += reagent.Quantity; @@ -218,7 +218,7 @@ namespace Content.Shared.GameObjects.Components.Chemistry /// Return true if the solution contains the reagent. public bool ContainsReagent(string reagentId, out decimal quantity) { - foreach (var reagent in _containedSolution.Contents) + foreach (var reagent in ContainedSolution.Contents) { if (reagent.ReagentId == reagentId) { diff --git a/Content.Shared/Interfaces/Chemistry/IRounderForReagents.cs b/Content.Shared/Interfaces/Chemistry/IRounderForReagents.cs new file mode 100644 index 0000000000..da8a1871c7 --- /dev/null +++ b/Content.Shared/Interfaces/Chemistry/IRounderForReagents.cs @@ -0,0 +1,7 @@ +namespace Content.Shared.Interfaces.Chemistry +{ + public interface IRounderForReagents + { + decimal Round(decimal value); + } +} diff --git a/Content.Shared/Maths/Rounders.cs b/Content.Shared/Maths/Rounders.cs deleted file mode 100644 index 4b07d84026..0000000000 --- a/Content.Shared/Maths/Rounders.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; - -namespace Content.Shared.Maths -{ - public static class Rounders - { - public static decimal RoundForReagents(this decimal me) - { - return Math.Round(me, 2); - } - } -} diff --git a/Resources/Prototypes/Entities/items/chemistry.yml b/Resources/Prototypes/Entities/items/chemistry.yml index cca0065844..cf5e63916f 100644 --- a/Resources/Prototypes/Entities/items/chemistry.yml +++ b/Resources/Prototypes/Entities/items/chemistry.yml @@ -9,10 +9,10 @@ - type: Icon texture: Objects/Chemistry/chemicals.rsi/beaker.png - type: Solution - maxVol: 50 + maxVol: 50.0 caps: 27 - type: Pourable - transferAmount: 5 + transferAmount: 5.0 - type: entity name: Large Beaker @@ -25,10 +25,10 @@ - type: Icon texture: Objects/Chemistry/chemicals.rsi/beakerlarge.png - type: Solution - maxVol: 100 + maxVol: 100.0 caps: 27 - type: Pourable - transferAmount: 5 + transferAmount: 5.0 - type: entity name: Dropper @@ -41,10 +41,10 @@ - type: Icon texture: Objects/Chemistry/chemicals.rsi/dropper.png - type: Solution - maxVol: 5 + maxVol: 5.0 caps: 19 - type: Pourable - transferAmount: 5 + transferAmount: 5.0 - type: entity name: Syringe @@ -57,7 +57,7 @@ - type: Icon texture: Objects/Chemistry/chemicals.rsi/syringeproj.png - type: Solution - maxVol: 15 + maxVol: 15.0 caps: 19 - type: Injector injectOnly: false