Replaced static Rounders with an impleneted interface

This commit is contained in:
PrPleGoo
2020-03-14 14:04:08 +01:00
parent f05fdfb5fc
commit dc66621804
11 changed files with 91 additions and 70 deletions

View File

@@ -7,7 +7,9 @@ using Content.Client.Parallax;
using Content.Client.Sandbox; using Content.Client.Sandbox;
using Content.Client.UserInterface; using Content.Client.UserInterface;
using Content.Client.Utility; using Content.Client.Utility;
using Content.Shared.Chemistry;
using Content.Shared.Interfaces; using Content.Shared.Interfaces;
using Content.Shared.Interfaces.Chemistry;
using Robust.Shared.IoC; using Robust.Shared.IoC;
namespace Content.Client namespace Content.Client
@@ -27,6 +29,7 @@ namespace Content.Client
IoCManager.Register<IModuleManager, ClientModuleManager>(); IoCManager.Register<IModuleManager, ClientModuleManager>();
IoCManager.Register<IClientPreferencesManager, ClientPreferencesManager>(); IoCManager.Register<IClientPreferencesManager, ClientPreferencesManager>();
IoCManager.Register<IItemSlotManager, ItemSlotManager>(); IoCManager.Register<IItemSlotManager, ItemSlotManager>();
IoCManager.Register<IRounderForReagents, RounderForReagents>();
} }
} }
} }

View File

@@ -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.Interfaces.Chemistry;
using Content.Shared.Maths;
using Robust.Shared.Interfaces.GameObjects; using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Serialization; using Robust.Shared.Interfaces.Serialization;
using Robust.Shared.IoC;
using Robust.Shared.Serialization; using Robust.Shared.Serialization;
namespace Content.Server.Chemistry.Metabolism namespace Content.Server.Chemistry.Metabolism
@@ -14,6 +13,9 @@ namespace Content.Server.Chemistry.Metabolism
/// </summary> /// </summary>
class DefaultFood : IMetabolizable class DefaultFood : IMetabolizable
{ {
#pragma warning disable 649
[Dependency] private readonly IRounderForReagents _rounder;
#pragma warning restore 649
//Rate of metabolism in units / second //Rate of metabolism in units / second
private decimal _metabolismRate; private decimal _metabolismRate;
public decimal MetabolismRate => _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 //Remove reagent at set rate, satiate hunger if a HungerComponent can be found
decimal IMetabolizable.Metabolize(IEntity solutionEntity, string reagentId, float tickTime) 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)) if (solutionEntity.TryGetComponent(out HungerComponent hunger))
hunger.UpdateFood((float)(metabolismAmount * NutritionFactor)); hunger.UpdateFood((float)(metabolismAmount * NutritionFactor));

View File

@@ -1,13 +1,8 @@
using System; using Content.Server.Chemistry;
using System.Collections.Generic;
using System.ComponentModel.Design;
using Content.Server.Chemistry;
using Content.Server.GameObjects.Components.Nutrition;
using Content.Server.GameObjects.EntitySystems; using Content.Server.GameObjects.EntitySystems;
using Content.Server.Interfaces;
using Content.Shared.Chemistry; using Content.Shared.Chemistry;
using Content.Shared.GameObjects; using Content.Shared.GameObjects;
using Content.Shared.Maths; using Content.Shared.Interfaces.Chemistry;
using Robust.Server.GameObjects.EntitySystems; using Robust.Server.GameObjects.EntitySystems;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects; using Robust.Shared.Interfaces.GameObjects;
@@ -15,6 +10,8 @@ using Robust.Shared.IoC;
using Robust.Shared.Localization; using Robust.Shared.Localization;
using Robust.Shared.Prototypes; using Robust.Shared.Prototypes;
using Robust.Shared.Utility; using Robust.Shared.Utility;
using System;
using System.Collections.Generic;
namespace Content.Server.GameObjects.Components.Chemistry 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 internal class SolutionComponent : Shared.GameObjects.Components.Chemistry.SolutionComponent, IExamine
{ {
#pragma warning disable 649 #pragma warning disable 649
[Dependency] private readonly IRounderForReagents _rounder;
[Dependency] private readonly IPrototypeManager _prototypeManager; [Dependency] private readonly IPrototypeManager _prototypeManager;
[Dependency] private readonly ILocalizationManager _loc; [Dependency] private readonly ILocalizationManager _loc;
[Dependency] private readonly IEntitySystemManager _entitySystemManager; [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) public bool TryAddReagent(string reagentId, decimal quantity, out decimal acceptedQuantity, bool skipReactionCheck = false, bool skipColor = false)
{ {
quantity = quantity.RoundForReagents(); quantity = _rounder.Round(quantity);
var toAcceptQuantity = (_maxVolume - _containedSolution.TotalVolume).RoundForReagents(); var toAcceptQuantity = _rounder.Round(MaxVolume - ContainedSolution.TotalVolume);
if (quantity > toAcceptQuantity) if (quantity > toAcceptQuantity)
{ {
acceptedQuantity = toAcceptQuantity; acceptedQuantity = toAcceptQuantity;
@@ -238,7 +236,7 @@ namespace Content.Server.GameObjects.Components.Chemistry
acceptedQuantity = quantity; acceptedQuantity = quantity;
} }
_containedSolution.AddReagent(reagentId, acceptedQuantity); ContainedSolution.AddReagent(reagentId, acceptedQuantity);
if (!skipColor) { if (!skipColor) {
RecalculateColor(); RecalculateColor();
} }
@@ -250,10 +248,10 @@ namespace Content.Server.GameObjects.Components.Chemistry
public bool TryAddSolution(Solution solution, bool skipReactionCheck = false, bool skipColor = false) 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; return false;
_containedSolution.AddSolution(solution); ContainedSolution.AddSolution(solution);
if (!skipColor) { if (!skipColor) {
RecalculateColor(); RecalculateColor();
} }
@@ -279,7 +277,7 @@ namespace Content.Server.GameObjects.Components.Chemistry
{ {
return false; return false;
} }
var currentUnitReactions = (reagentQuantity / reactant.Value.Amount).RoundForReagents(); var currentUnitReactions = _rounder.Round(reagentQuantity / reactant.Value.Amount);
if (currentUnitReactions < unitReactions) if (currentUnitReactions < unitReactions)
{ {
unitReactions = currentUnitReactions; unitReactions = currentUnitReactions;
@@ -309,7 +307,7 @@ namespace Content.Server.GameObjects.Components.Chemistry
{ {
if (!reactant.Value.Catalyst) if (!reactant.Value.Catalyst)
{ {
var amountToRemove = (unitReactions * reactant.Value.Amount).RoundForReagents(); var amountToRemove = _rounder.Round(unitReactions * reactant.Value.Amount);
TryRemoveReagent(reactant.Key, amountToRemove); TryRemoveReagent(reactant.Key, amountToRemove);
} }
} }

View File

@@ -1,4 +1,4 @@
using Content.Server.Cargo; using Content.Server.Cargo;
using Content.Server.Chat; using Content.Server.Chat;
using Content.Server.GameTicking; using Content.Server.GameTicking;
using Content.Server.Interfaces; using Content.Server.Interfaces;
@@ -7,7 +7,9 @@ using Content.Server.Interfaces.GameTicking;
using Content.Server.Preferences; using Content.Server.Preferences;
using Content.Server.Sandbox; using Content.Server.Sandbox;
using Content.Server.Utility; using Content.Server.Utility;
using Content.Shared.Chemistry;
using Content.Shared.Interfaces; using Content.Shared.Interfaces;
using Content.Shared.Interfaces.Chemistry;
using Robust.Shared.IoC; using Robust.Shared.IoC;
namespace Content.Server namespace Content.Server
@@ -26,6 +28,7 @@ namespace Content.Server
IoCManager.Register<ICargoOrderDataManager, CargoOrderDataManager>(); IoCManager.Register<ICargoOrderDataManager, CargoOrderDataManager>();
IoCManager.Register<IModuleManager, ServerModuleManager>(); IoCManager.Register<IModuleManager, ServerModuleManager>();
IoCManager.Register<IServerPreferencesManager, ServerPreferencesManager>(); IoCManager.Register<IServerPreferencesManager, ServerPreferencesManager>();
IoCManager.Register<IRounderForReagents, RounderForReagents>();
} }
} }
} }

View File

@@ -1,8 +1,7 @@
using System; using Content.Shared.Interfaces.Chemistry;
using Content.Shared.Interfaces.Chemistry;
using Content.Shared.Maths;
using Robust.Shared.Interfaces.GameObjects; using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Serialization; using Robust.Shared.Interfaces.Serialization;
using Robust.Shared.IoC;
using Robust.Shared.Serialization; using Robust.Shared.Serialization;
namespace Content.Shared.Chemistry namespace Content.Shared.Chemistry
@@ -10,6 +9,9 @@ namespace Content.Shared.Chemistry
//Default metabolism for reagents. Metabolizes the reagent with no effects //Default metabolism for reagents. Metabolizes the reagent with no effects
class DefaultMetabolizable : IMetabolizable class DefaultMetabolizable : IMetabolizable
{ {
#pragma warning disable 649
[Dependency] private readonly IRounderForReagents _rounder;
#pragma warning restore 649
//Rate of metabolism in units / second //Rate of metabolism in units / second
private decimal _metabolismRate = 1; private decimal _metabolismRate = 1;
public decimal MetabolismRate => _metabolismRate; public decimal MetabolismRate => _metabolismRate;
@@ -21,7 +23,7 @@ namespace Content.Shared.Chemistry
decimal IMetabolizable.Metabolize(IEntity solutionEntity, string reagentId, float tickTime) decimal IMetabolizable.Metabolize(IEntity solutionEntity, string reagentId, float tickTime)
{ {
var metabolismAmount = (MetabolismRate * (decimal)tickTime).RoundForReagents(); var metabolismAmount = _rounder.Round(MetabolismRate * (decimal)tickTime);
return metabolismAmount; return metabolismAmount;
} }
} }

View File

@@ -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);
}
}
}

View File

@@ -1,12 +1,13 @@
using System; using Content.Shared.Interfaces.Chemistry;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using Content.Shared.Maths;
using Robust.Shared.Interfaces.Serialization; using Robust.Shared.Interfaces.Serialization;
using Robust.Shared.IoC;
using Robust.Shared.Serialization; using Robust.Shared.Serialization;
using Robust.Shared.Utility; using Robust.Shared.Utility;
using Robust.Shared.ViewVariables; using Robust.Shared.ViewVariables;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
namespace Content.Shared.Chemistry namespace Content.Shared.Chemistry
{ {
@@ -15,6 +16,9 @@ namespace Content.Shared.Chemistry
/// </summary> /// </summary>
public class Solution : IExposeData, IEnumerable<Solution.ReagentQuantity> public class Solution : IExposeData, IEnumerable<Solution.ReagentQuantity>
{ {
#pragma warning disable 649
[Dependency] private readonly IRounderForReagents _rounder;
#pragma warning restore 649
// Most objects on the station hold only 1 or 2 reagents // Most objects on the station hold only 1 or 2 reagents
[ViewVariables] [ViewVariables]
private List<ReagentQuantity> _contents = new List<ReagentQuantity>(2); private List<ReagentQuantity> _contents = new List<ReagentQuantity>(2);
@@ -63,7 +67,7 @@ namespace Content.Shared.Chemistry
/// <param name="quantity">The quantity in milli-units.</param> /// <param name="quantity">The quantity in milli-units.</param>
public void AddReagent(string reagentId, decimal quantity) public void AddReagent(string reagentId, decimal quantity)
{ {
quantity = quantity.RoundForReagents(); quantity = _rounder.Round(quantity);
if (quantity <= 0) if (quantity <= 0)
return; return;
@@ -111,7 +115,7 @@ namespace Content.Shared.Chemistry
var curQuantity = reagent.Quantity; var curQuantity = reagent.Quantity;
var newQuantity = (curQuantity - quantity).RoundForReagents(); var newQuantity = _rounder.Round(curQuantity - quantity);
if (newQuantity <= 0) if (newQuantity <= 0)
{ {
_contents.RemoveSwap(i); _contents.RemoveSwap(i);
@@ -136,7 +140,7 @@ namespace Content.Shared.Chemistry
if(quantity <= 0) if(quantity <= 0)
return; return;
var ratio = (TotalVolume - quantity).RoundForReagents() / TotalVolume; var ratio = _rounder.Round(TotalVolume - quantity) / TotalVolume;
if (ratio <= 0) 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 // 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. // 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); _contents[i] = new ReagentQuantity(reagent.ReagentId, newQuantity);
} }
TotalVolume = (TotalVolume * ratio).RoundForReagents(); TotalVolume = _rounder.Round(TotalVolume * ratio);
} }
public void RemoveAllSolution() public void RemoveAllSolution()

View File

@@ -1,12 +1,12 @@
using System; using Content.Shared.Chemistry;
using System.Collections.Generic;
using Content.Shared.Chemistry;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.IoC; using Robust.Shared.IoC;
using Robust.Shared.Maths; using Robust.Shared.Maths;
using Robust.Shared.Prototypes; using Robust.Shared.Prototypes;
using Robust.Shared.Serialization; using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables; using Robust.Shared.ViewVariables;
using System;
using System.Collections.Generic;
namespace Content.Shared.GameObjects.Components.Chemistry namespace Content.Shared.GameObjects.Components.Chemistry
{ {
@@ -17,8 +17,8 @@ namespace Content.Shared.GameObjects.Components.Chemistry
#pragma warning restore 649 #pragma warning restore 649
[ViewVariables] [ViewVariables]
protected Solution _containedSolution = new Solution(); protected Solution ContainedSolution = new Solution();
protected decimal _maxVolume; private decimal _maxVolume;
private SolutionCaps _capabilities; private SolutionCaps _capabilities;
/// <summary> /// <summary>
@@ -40,7 +40,7 @@ namespace Content.Shared.GameObjects.Components.Chemistry
/// The total volume of all the of the reagents in the container. /// The total volume of all the of the reagents in the container.
/// </summary> /// </summary>
[ViewVariables] [ViewVariables]
public decimal CurrentVolume => _containedSolution.TotalVolume; public decimal CurrentVolume => ContainedSolution.TotalVolume;
/// <summary> /// <summary>
/// The volume without reagents remaining in the container. /// The volume without reagents remaining in the container.
@@ -64,7 +64,7 @@ namespace Content.Shared.GameObjects.Components.Chemistry
set => _capabilities = value; set => _capabilities = value;
} }
public IReadOnlyList<Solution.ReagentQuantity> ReagentList => _containedSolution.Contents; public IReadOnlyList<Solution.ReagentQuantity> ReagentList => ContainedSolution.Contents;
/// <summary> /// <summary>
/// Shortcut for Capabilities PourIn flag to avoid binary operators. /// Shortcut for Capabilities PourIn flag to avoid binary operators.
@@ -94,8 +94,8 @@ namespace Content.Shared.GameObjects.Components.Chemistry
{ {
base.ExposeData(serializer); base.ExposeData(serializer);
serializer.DataField(ref _maxVolume, "maxVol", 0); serializer.DataField(ref _maxVolume, "maxVol", 0M);
serializer.DataField(ref _containedSolution, "contents", _containedSolution); serializer.DataField(ref ContainedSolution, "contents", ContainedSolution);
serializer.DataField(ref _capabilities, "caps", SolutionCaps.None); serializer.DataField(ref _capabilities, "caps", SolutionCaps.None);
} }
@@ -112,13 +112,13 @@ namespace Content.Shared.GameObjects.Components.Chemistry
{ {
base.Shutdown(); base.Shutdown();
_containedSolution.RemoveAllSolution(); ContainedSolution.RemoveAllSolution();
_containedSolution = new Solution(); ContainedSolution = new Solution();
} }
public void RemoveAllSolution() public void RemoveAllSolution()
{ {
_containedSolution.RemoveAllSolution(); ContainedSolution.RemoveAllSolution();
OnSolutionChanged(); OnSolutionChanged();
} }
@@ -126,7 +126,7 @@ namespace Content.Shared.GameObjects.Components.Chemistry
{ {
if (!ContainsReagent(reagentId, out var currentQuantity)) return false; if (!ContainsReagent(reagentId, out var currentQuantity)) return false;
_containedSolution.RemoveReagent(reagentId, quantity); ContainedSolution.RemoveReagent(reagentId, quantity);
OnSolutionChanged(); OnSolutionChanged();
return true; return true;
} }
@@ -141,27 +141,27 @@ namespace Content.Shared.GameObjects.Components.Chemistry
if (CurrentVolume == 0) if (CurrentVolume == 0)
return false; return false;
_containedSolution.RemoveSolution(quantity); ContainedSolution.RemoveSolution(quantity);
OnSolutionChanged(); OnSolutionChanged();
return true; return true;
} }
public Solution SplitSolution(decimal quantity) public Solution SplitSolution(decimal quantity)
{ {
var solutionSplit = _containedSolution.SplitSolution(quantity); var solutionSplit = ContainedSolution.SplitSolution(quantity);
OnSolutionChanged(); OnSolutionChanged();
return solutionSplit; return solutionSplit;
} }
protected void RecalculateColor() protected void RecalculateColor()
{ {
if(_containedSolution.TotalVolume == 0) if(ContainedSolution.TotalVolume == 0)
SubstanceColor = Color.White; SubstanceColor = Color.White;
Color mixColor = default; Color mixColor = default;
var runningTotalQuantity = 0M; var runningTotalQuantity = 0M;
foreach (var reagent in _containedSolution) foreach (var reagent in ContainedSolution)
{ {
runningTotalQuantity += reagent.Quantity; runningTotalQuantity += reagent.Quantity;
@@ -218,7 +218,7 @@ namespace Content.Shared.GameObjects.Components.Chemistry
/// <returns>Return true if the solution contains the reagent.</returns> /// <returns>Return true if the solution contains the reagent.</returns>
public bool ContainsReagent(string reagentId, out decimal quantity) public bool ContainsReagent(string reagentId, out decimal quantity)
{ {
foreach (var reagent in _containedSolution.Contents) foreach (var reagent in ContainedSolution.Contents)
{ {
if (reagent.ReagentId == reagentId) if (reagent.ReagentId == reagentId)
{ {

View File

@@ -0,0 +1,7 @@
namespace Content.Shared.Interfaces.Chemistry
{
public interface IRounderForReagents
{
decimal Round(decimal value);
}
}

View File

@@ -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);
}
}
}

View File

@@ -9,10 +9,10 @@
- type: Icon - type: Icon
texture: Objects/Chemistry/chemicals.rsi/beaker.png texture: Objects/Chemistry/chemicals.rsi/beaker.png
- type: Solution - type: Solution
maxVol: 50 maxVol: 50.0
caps: 27 caps: 27
- type: Pourable - type: Pourable
transferAmount: 5 transferAmount: 5.0
- type: entity - type: entity
name: Large Beaker name: Large Beaker
@@ -25,10 +25,10 @@
- type: Icon - type: Icon
texture: Objects/Chemistry/chemicals.rsi/beakerlarge.png texture: Objects/Chemistry/chemicals.rsi/beakerlarge.png
- type: Solution - type: Solution
maxVol: 100 maxVol: 100.0
caps: 27 caps: 27
- type: Pourable - type: Pourable
transferAmount: 5 transferAmount: 5.0
- type: entity - type: entity
name: Dropper name: Dropper
@@ -41,10 +41,10 @@
- type: Icon - type: Icon
texture: Objects/Chemistry/chemicals.rsi/dropper.png texture: Objects/Chemistry/chemicals.rsi/dropper.png
- type: Solution - type: Solution
maxVol: 5 maxVol: 5.0
caps: 19 caps: 19
- type: Pourable - type: Pourable
transferAmount: 5 transferAmount: 5.0
- type: entity - type: entity
name: Syringe name: Syringe
@@ -57,7 +57,7 @@
- type: Icon - type: Icon
texture: Objects/Chemistry/chemicals.rsi/syringeproj.png texture: Objects/Chemistry/chemicals.rsi/syringeproj.png
- type: Solution - type: Solution
maxVol: 15 maxVol: 15.0
caps: 19 caps: 19
- type: Injector - type: Injector
injectOnly: false injectOnly: false