Replace decimal with ReagentUnit

This commit is contained in:
PrPleGoo
2020-04-05 11:36:12 +02:00
parent 539214b1ad
commit 4e0242d47c
27 changed files with 496 additions and 253 deletions

View File

@@ -9,9 +9,6 @@ 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,10 +18,9 @@ namespace Content.Shared.Chemistry
serializer.DataField(ref _metabolismRate, "rate", 1);
}
decimal IMetabolizable.Metabolize(IEntity solutionEntity, string reagentId, float tickTime)
ReagentUnit IMetabolizable.Metabolize(IEntity solutionEntity, string reagentId, float tickTime)
{
var metabolismAmount = _rounder.Round(MetabolismRate * (decimal)tickTime);
return metabolismAmount;
return ReagentUnit.New(MetabolismRate * (decimal)tickTime);
}
}
}

View File

@@ -0,0 +1,162 @@
using System;
using System.Linq;
namespace Content.Shared.Chemistry
{
[Serializable]
public struct ReagentUnit
{
private int _value;
private static readonly int Shift = 2;
public static ReagentUnit MaxValue => new ReagentUnit(int.MaxValue);
private decimal ShiftDown()
{
return _value / (decimal)Math.Pow(10, Shift);
}
private decimal ShiftUp()
{
return _value * (decimal)Math.Pow(10, Shift);
}
private ReagentUnit(int value)
{
_value = value;
}
public static ReagentUnit New(int value)
{
return new ReagentUnit(value * (int) Math.Pow(10, Shift));
}
public static ReagentUnit New(decimal value)
{
return new ReagentUnit((int) Math.Round(value * (decimal) Math.Pow(10, Shift)));
}
public static ReagentUnit New(float value)
{
return new ReagentUnit((int) Math.Round(value * (float) Math.Pow(10, Shift)));
}
public static ReagentUnit New(double value)
{
return new ReagentUnit((int) Math.Round(value * Math.Pow(10, Shift)));
}
public static ReagentUnit operator +(ReagentUnit a) => a;
public static ReagentUnit operator -(ReagentUnit a) => new ReagentUnit(-a._value);
public static ReagentUnit operator +(ReagentUnit a, ReagentUnit b)
=> new ReagentUnit(a._value + b._value);
public static ReagentUnit operator -(ReagentUnit a, ReagentUnit b)
=> a + -b;
public static ReagentUnit operator *(ReagentUnit a, ReagentUnit b)
{
var aD = a.ShiftDown();
var bD = b.ShiftDown();
return New(aD * bD);
}
public static ReagentUnit operator *(ReagentUnit a, float b)
{
var aD = (float) a.ShiftDown();
return New(aD * b);
}
public static ReagentUnit operator *(ReagentUnit a, decimal b)
{
var aD = a.ShiftDown();
return New(aD * b);
}
public static ReagentUnit operator /(ReagentUnit a, ReagentUnit b)
{
if (b._value == 0)
{
throw new DivideByZeroException();
}
var aD = a.ShiftDown();
var bD = b.ShiftDown();
return New(aD / bD);
}
public static bool operator <=(ReagentUnit a, int b)
{
return a.ShiftDown() <= b;
}
public static bool operator >=(ReagentUnit a, int b)
{
return a.ShiftDown() >= b;
}
public static bool operator ==(ReagentUnit a, int b)
{
return a.ShiftDown() == b;
}
public static bool operator !=(ReagentUnit a, int b)
{
return a.ShiftDown() != b;
}
public static bool operator <=(ReagentUnit a, ReagentUnit b)
{
return a._value <= b._value;
}
public static bool operator >=(ReagentUnit a, ReagentUnit b)
{
return a._value >= b._value;
}
public static bool operator <(ReagentUnit a, ReagentUnit b)
{
return a._value < b._value;
}
public static bool operator >(ReagentUnit a, ReagentUnit b)
{
return a._value > b._value;
}
public override string ToString() => $"{ShiftDown()}";
public float Float()
{
return (float) ShiftDown();
}
public decimal Decimal()
{
return (decimal) ShiftDown();
}
public int Int()
{
return (int) ShiftDown();
}
public static ReagentUnit Min(params ReagentUnit[] reagentUnits)
{
return reagentUnits.OrderBy(x => x._value).First();
}
public override bool Equals(object obj)
{
return obj is ReagentUnit unit &&
_value == unit._value;
}
public override int GetHashCode()
{
return HashCode.Combine(_value);
}
}
}

View File

@@ -1,14 +0,0 @@
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

@@ -16,9 +16,6 @@ namespace Content.Shared.Chemistry
/// </summary>
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
[ViewVariables]
private List<ReagentQuantity> _contents = new List<ReagentQuantity>(2);
@@ -28,7 +25,7 @@ namespace Content.Shared.Chemistry
/// The calculated total volume of all reagents in the solution (ex. Total volume of liquid in beaker).
/// </summary>
[ViewVariables]
public decimal TotalVolume { get; private set; }
public ReagentUnit TotalVolume { get; private set; }
/// <summary>
/// Constructs an empty solution (ex. an empty beaker).
@@ -40,7 +37,7 @@ namespace Content.Shared.Chemistry
/// </summary>
/// <param name="reagentId">The prototype ID of the reagent to add.</param>
/// <param name="quantity">The quantity in milli-units.</param>
public Solution(string reagentId, int quantity)
public Solution(string reagentId, ReagentUnit quantity)
{
AddReagent(reagentId, quantity);
}
@@ -52,7 +49,7 @@ namespace Content.Shared.Chemistry
if (serializer.Reading)
{
TotalVolume = 0;
TotalVolume = ReagentUnit.New(0);
foreach (var reagent in _contents)
{
TotalVolume += reagent.Quantity;
@@ -65,9 +62,8 @@ namespace Content.Shared.Chemistry
/// </summary>
/// <param name="reagentId">The prototype ID of the reagent to add.</param>
/// <param name="quantity">The quantity in milli-units.</param>
public void AddReagent(string reagentId, decimal quantity)
public void AddReagent(string reagentId, ReagentUnit quantity)
{
quantity = _rounder.Round(quantity);
if (quantity <= 0)
return;
@@ -91,7 +87,7 @@ namespace Content.Shared.Chemistry
/// </summary>
/// <param name="reagentId">The prototype ID of the reagent to add.</param>
/// <returns>The quantity in milli-units.</returns>
public decimal GetReagentQuantity(string reagentId)
public ReagentUnit GetReagentQuantity(string reagentId)
{
for (var i = 0; i < _contents.Count; i++)
{
@@ -99,10 +95,10 @@ namespace Content.Shared.Chemistry
return _contents[i].Quantity;
}
return 0;
return ReagentUnit.New(0);
}
public void RemoveReagent(string reagentId, decimal quantity)
public void RemoveReagent(string reagentId, ReagentUnit quantity)
{
if(quantity <= 0)
return;
@@ -115,7 +111,7 @@ namespace Content.Shared.Chemistry
var curQuantity = reagent.Quantity;
var newQuantity = _rounder.Round(curQuantity - quantity);
var newQuantity = curQuantity - quantity;
if (newQuantity <= 0)
{
_contents.RemoveSwap(i);
@@ -135,12 +131,12 @@ namespace Content.Shared.Chemistry
/// Remove the specified quantity from this solution.
/// </summary>
/// <param name="quantity">The quantity of this solution to remove</param>
public void RemoveSolution(decimal quantity)
public void RemoveSolution(ReagentUnit quantity)
{
if(quantity <= 0)
return;
var ratio = _rounder.Round(TotalVolume - quantity) / TotalVolume;
var ratio = (TotalVolume - quantity).Decimal() / TotalVolume.Decimal();
if (ratio <= 0)
{
@@ -155,24 +151,24 @@ 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 = _rounder.Round(oldQuantity * ratio);
var newQuantity = oldQuantity * ratio;
_contents[i] = new ReagentQuantity(reagent.ReagentId, newQuantity);
}
TotalVolume = _rounder.Round(TotalVolume * ratio);
TotalVolume = TotalVolume * ratio;
}
public void RemoveAllSolution()
{
_contents.Clear();
TotalVolume = 0;
TotalVolume = ReagentUnit.New(0);
}
public Solution SplitSolution(decimal quantity)
public Solution SplitSolution(ReagentUnit quantity)
{
if (quantity <= 0)
return IoCManager.InjectDependencies(new Solution());
return new Solution();
Solution newSolution;
@@ -183,15 +179,15 @@ namespace Content.Shared.Chemistry
return newSolution;
}
newSolution = IoCManager.InjectDependencies(new Solution());
var newTotalVolume = 0M;
var ratio = (TotalVolume - quantity) / TotalVolume;
newSolution = new Solution();
var newTotalVolume = ReagentUnit.New(0M);
var ratio = (TotalVolume - quantity).Decimal() / TotalVolume.Decimal();
for (var i = 0; i < _contents.Count; i++)
{
var reagent = _contents[i];
var newQuantity = (int)Math.Floor(reagent.Quantity * ratio);
var newQuantity = reagent.Quantity * ratio;
var splitQuantity = reagent.Quantity - newQuantity;
_contents[i] = new ReagentQuantity(reagent.ReagentId, newQuantity);
@@ -199,7 +195,7 @@ namespace Content.Shared.Chemistry
newTotalVolume += splitQuantity;
}
TotalVolume = (int)Math.Floor(TotalVolume * ratio);
TotalVolume = TotalVolume * ratio;
newSolution.TotalVolume = newTotalVolume;
return newSolution;
@@ -234,8 +230,8 @@ namespace Content.Shared.Chemistry
public Solution Clone()
{
var volume = 0M;
var newSolution = IoCManager.InjectDependencies(new Solution());
var volume = ReagentUnit.New(0);
var newSolution = new Solution();
for (var i = 0; i < _contents.Count; i++)
{
@@ -252,9 +248,9 @@ namespace Content.Shared.Chemistry
public readonly struct ReagentQuantity
{
public readonly string ReagentId;
public readonly decimal Quantity;
public readonly ReagentUnit Quantity;
public ReagentQuantity(string reagentId, decimal quantity)
public ReagentQuantity(string reagentId, ReagentUnit quantity)
{
ReagentId = reagentId;
Quantity = quantity;

View File

@@ -1,4 +1,5 @@
using System;
using Content.Shared.Chemistry;
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization;
@@ -18,11 +19,11 @@ namespace Content.Shared.GameObjects.Components.Chemistry
[Serializable, NetSerializable]
protected sealed class InjectorComponentState : ComponentState
{
public decimal CurrentVolume { get; }
public decimal TotalVolume { get; }
public ReagentUnit CurrentVolume { get; }
public ReagentUnit TotalVolume { get; }
public InjectorToggleMode CurrentMode { get; }
public InjectorComponentState(decimal currentVolume, decimal totalVolume, InjectorToggleMode currentMode) : base(ContentNetIDs.REAGENT_INJECTOR)
public InjectorComponentState(ReagentUnit currentVolume, ReagentUnit totalVolume, InjectorToggleMode currentMode) : base(ContentNetIDs.REAGENT_INJECTOR)
{
CurrentVolume = currentVolume;
TotalVolume = totalVolume;

View File

@@ -26,8 +26,8 @@ namespace Content.Shared.GameObjects.Components.Chemistry
public class ReagentDispenserBoundUserInterfaceState : BoundUserInterfaceState
{
public readonly bool HasBeaker;
public readonly decimal BeakerCurrentVolume;
public readonly decimal BeakerMaxVolume;
public readonly ReagentUnit BeakerCurrentVolume;
public readonly ReagentUnit BeakerMaxVolume;
public readonly string ContainerName;
/// <summary>
/// A list of the reagents which this dispenser can dispense.
@@ -38,10 +38,10 @@ namespace Content.Shared.GameObjects.Components.Chemistry
/// </summary>
public readonly List<Solution.ReagentQuantity> ContainerReagents;
public readonly string DispenserName;
public readonly decimal SelectedDispenseAmount;
public readonly ReagentUnit SelectedDispenseAmount;
public ReagentDispenserBoundUserInterfaceState(bool hasBeaker, decimal beakerCurrentVolume, decimal beakerMaxVolume, string containerName,
List<ReagentDispenserInventoryEntry> inventory, string dispenserName, List<Solution.ReagentQuantity> containerReagents, decimal selectedDispenseAmount)
public ReagentDispenserBoundUserInterfaceState(bool hasBeaker, ReagentUnit beakerCurrentVolume, ReagentUnit beakerMaxVolume, string containerName,
List<ReagentDispenserInventoryEntry> inventory, string dispenserName, List<Solution.ReagentQuantity> containerReagents, ReagentUnit selectedDispenseAmount)
{
HasBeaker = hasBeaker;
BeakerCurrentVolume = beakerCurrentVolume;

View File

@@ -18,7 +18,7 @@ namespace Content.Shared.GameObjects.Components.Chemistry
[ViewVariables]
protected Solution ContainedSolution;
private decimal _maxVolume;
private ReagentUnit _maxVolume;
private SolutionCaps _capabilities;
/// <summary>
@@ -30,7 +30,7 @@ namespace Content.Shared.GameObjects.Components.Chemistry
/// The maximum volume of the container.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
public decimal MaxVolume
public ReagentUnit MaxVolume
{
get => _maxVolume;
set => _maxVolume = value; // Note that the contents won't spill out if the capacity is reduced.
@@ -40,13 +40,13 @@ namespace Content.Shared.GameObjects.Components.Chemistry
/// The total volume of all the of the reagents in the container.
/// </summary>
[ViewVariables]
public decimal CurrentVolume => ContainedSolution.TotalVolume;
public ReagentUnit CurrentVolume => ContainedSolution.TotalVolume;
/// <summary>
/// The volume without reagents remaining in the container.
/// </summary>
[ViewVariables]
public decimal EmptyVolume => MaxVolume - CurrentVolume;
public ReagentUnit EmptyVolume => MaxVolume - CurrentVolume;
/// <summary>
/// The current blended color of all the reagents in the container.
@@ -94,14 +94,14 @@ namespace Content.Shared.GameObjects.Components.Chemistry
{
base.ExposeData(serializer);
serializer.DataField(ref _maxVolume, "maxVol", 0M);
serializer.DataField(ref ContainedSolution, "contents", IoCManager.InjectDependencies(new Solution()));
serializer.DataField(ref _maxVolume, "maxVol", ReagentUnit.New(0M));
serializer.DataField(ref ContainedSolution, "contents", new Solution());
serializer.DataField(ref _capabilities, "caps", SolutionCaps.None);
}
public virtual void Init()
{
ContainedSolution = IoCManager.InjectDependencies(new Solution());
ContainedSolution = new Solution();
}
/// <inheritdoc />
@@ -118,7 +118,7 @@ namespace Content.Shared.GameObjects.Components.Chemistry
base.Shutdown();
ContainedSolution.RemoveAllSolution();
ContainedSolution = IoCManager.InjectDependencies(new Solution());
ContainedSolution = new Solution();
}
public void RemoveAllSolution()
@@ -127,7 +127,7 @@ namespace Content.Shared.GameObjects.Components.Chemistry
OnSolutionChanged();
}
public bool TryRemoveReagent(string reagentId, decimal quantity)
public bool TryRemoveReagent(string reagentId, ReagentUnit quantity)
{
if (!ContainsReagent(reagentId, out var currentQuantity)) return false;
@@ -141,7 +141,7 @@ namespace Content.Shared.GameObjects.Components.Chemistry
/// </summary>
/// <param name="quantity">Quantity of this solution to remove</param>
/// <returns>Whether or not the solution was successfully removed</returns>
public bool TryRemoveSolution(int quantity)
public bool TryRemoveSolution(ReagentUnit quantity)
{
if (CurrentVolume == 0)
return false;
@@ -151,7 +151,7 @@ namespace Content.Shared.GameObjects.Components.Chemistry
return true;
}
public Solution SplitSolution(decimal quantity)
public Solution SplitSolution(ReagentUnit quantity)
{
var solutionSplit = ContainedSolution.SplitSolution(quantity);
OnSolutionChanged();
@@ -164,7 +164,7 @@ namespace Content.Shared.GameObjects.Components.Chemistry
SubstanceColor = Color.White;
Color mixColor = default;
var runningTotalQuantity = 0M;
var runningTotalQuantity = ReagentUnit.New(0M);
foreach (var reagent in ContainedSolution)
{
@@ -176,7 +176,7 @@ namespace Content.Shared.GameObjects.Components.Chemistry
if (mixColor == default)
mixColor = proto.SubstanceColor;
mixColor = BlendRGB(mixColor, proto.SubstanceColor, (float) (reagent.Quantity / runningTotalQuantity));
mixColor = BlendRGB(mixColor, proto.SubstanceColor, reagent.Quantity.Float() / runningTotalQuantity.Float());
}
}
@@ -221,7 +221,7 @@ namespace Content.Shared.GameObjects.Components.Chemistry
/// <param name="reagentId">The reagent to check for.</param>
/// <param name="quantity">Output the quantity of the reagent if it is contained, 0 if it isn't.</param>
/// <returns>Return true if the solution contains the reagent.</returns>
public bool ContainsReagent(string reagentId, out decimal quantity)
public bool ContainsReagent(string reagentId, out ReagentUnit quantity)
{
foreach (var reagent in ContainedSolution.Contents)
{
@@ -231,7 +231,7 @@ namespace Content.Shared.GameObjects.Components.Chemistry
return true;
}
}
quantity = 0;
quantity = ReagentUnit.New(0);
return false;
}

View File

@@ -19,6 +19,6 @@ namespace Content.Shared.Interfaces.Chemistry
/// <param name="reagentId">The reagent id</param>
/// <param name="tickTime">The time since the last metabolism tick in seconds.</param>
/// <returns>The amount of reagent to be removed. The metabolizing organ should handle removing the reagent.</returns>
decimal Metabolize(IEntity solutionEntity, string reagentId, float tickTime);
ReagentUnit Metabolize(IEntity solutionEntity, string reagentId, float tickTime);
}
}

View File

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