Replace decimal with ReagentUnit
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
162
Content.Shared/Chemistry/ReagentUnit.cs
Normal file
162
Content.Shared/Chemistry/ReagentUnit.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user