Changed all int and some float things in Reagent code to Decimals
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using Content.Shared.Interfaces.Chemistry;
|
||||
using Content.Shared.Maths;
|
||||
using Robust.Shared.Interfaces.GameObjects;
|
||||
using Robust.Shared.Interfaces.Serialization;
|
||||
using Robust.Shared.Serialization;
|
||||
@@ -10,17 +11,17 @@ namespace Content.Shared.Chemistry
|
||||
class DefaultMetabolizable : IMetabolizable
|
||||
{
|
||||
//Rate of metabolism in units / second
|
||||
private int _metabolismRate = 1;
|
||||
public int MetabolismRate => _metabolismRate;
|
||||
private decimal _metabolismRate = 1;
|
||||
public decimal MetabolismRate => _metabolismRate;
|
||||
|
||||
void IExposeData.ExposeData(ObjectSerializer serializer)
|
||||
{
|
||||
serializer.DataField(ref _metabolismRate, "rate", 1);
|
||||
}
|
||||
|
||||
int IMetabolizable.Metabolize(IEntity solutionEntity, string reagentId, float tickTime)
|
||||
decimal IMetabolizable.Metabolize(IEntity solutionEntity, string reagentId, float tickTime)
|
||||
{
|
||||
int metabolismAmount = (int)Math.Round(MetabolismRate * tickTime);
|
||||
var metabolismAmount = (MetabolismRate * (decimal)tickTime).RoundForReagents();
|
||||
return metabolismAmount;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Content.Shared.Maths;
|
||||
using Robust.Shared.Interfaces.Serialization;
|
||||
using Robust.Shared.Serialization;
|
||||
using Robust.Shared.Utility;
|
||||
@@ -23,7 +24,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 int TotalVolume { get; private set; }
|
||||
public decimal TotalVolume { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Constructs an empty solution (ex. an empty beaker).
|
||||
@@ -60,9 +61,10 @@ 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, int quantity)
|
||||
public void AddReagent(string reagentId, decimal quantity)
|
||||
{
|
||||
if(quantity <= 0)
|
||||
quantity = quantity.RoundForReagents();
|
||||
if (quantity <= 0)
|
||||
return;
|
||||
|
||||
for (var i = 0; i < _contents.Count; i++)
|
||||
@@ -85,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 int GetReagentQuantity(string reagentId)
|
||||
public decimal GetReagentQuantity(string reagentId)
|
||||
{
|
||||
for (var i = 0; i < _contents.Count; i++)
|
||||
{
|
||||
@@ -96,7 +98,7 @@ namespace Content.Shared.Chemistry
|
||||
return 0;
|
||||
}
|
||||
|
||||
public void RemoveReagent(string reagentId, int quantity)
|
||||
public void RemoveReagent(string reagentId, decimal quantity)
|
||||
{
|
||||
if(quantity <= 0)
|
||||
return;
|
||||
@@ -109,7 +111,7 @@ namespace Content.Shared.Chemistry
|
||||
|
||||
var curQuantity = reagent.Quantity;
|
||||
|
||||
var newQuantity = curQuantity - quantity;
|
||||
var newQuantity = (curQuantity - quantity).RoundForReagents();
|
||||
if (newQuantity <= 0)
|
||||
{
|
||||
_contents.RemoveSwap(i);
|
||||
@@ -129,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(int quantity)
|
||||
public void RemoveSolution(decimal quantity)
|
||||
{
|
||||
if(quantity <= 0)
|
||||
return;
|
||||
|
||||
var ratio = (float)(TotalVolume - quantity) / TotalVolume;
|
||||
var ratio = (TotalVolume - quantity).RoundForReagents() / TotalVolume;
|
||||
|
||||
if (ratio <= 0)
|
||||
{
|
||||
@@ -149,12 +151,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 = (int)Math.Floor(oldQuantity * ratio);
|
||||
var newQuantity = (oldQuantity * ratio).RoundForReagents();
|
||||
|
||||
_contents[i] = new ReagentQuantity(reagent.ReagentId, newQuantity);
|
||||
}
|
||||
|
||||
TotalVolume = (int)Math.Floor(TotalVolume * ratio);
|
||||
TotalVolume = (TotalVolume * ratio).RoundForReagents();
|
||||
}
|
||||
|
||||
public void RemoveAllSolution()
|
||||
@@ -163,7 +165,7 @@ namespace Content.Shared.Chemistry
|
||||
TotalVolume = 0;
|
||||
}
|
||||
|
||||
public Solution SplitSolution(int quantity)
|
||||
public Solution SplitSolution(decimal quantity)
|
||||
{
|
||||
if (quantity <= 0)
|
||||
return new Solution();
|
||||
@@ -178,8 +180,8 @@ namespace Content.Shared.Chemistry
|
||||
}
|
||||
|
||||
newSolution = new Solution();
|
||||
var newTotalVolume = 0;
|
||||
var ratio = (float)(TotalVolume - quantity) / TotalVolume;
|
||||
var newTotalVolume = 0M;
|
||||
var ratio = (TotalVolume - quantity) / TotalVolume;
|
||||
|
||||
for (var i = 0; i < _contents.Count; i++)
|
||||
{
|
||||
@@ -228,7 +230,7 @@ namespace Content.Shared.Chemistry
|
||||
|
||||
public Solution Clone()
|
||||
{
|
||||
var volume = 0;
|
||||
var volume = 0M;
|
||||
var newSolution = new Solution();
|
||||
|
||||
for (var i = 0; i < _contents.Count; i++)
|
||||
@@ -246,9 +248,9 @@ namespace Content.Shared.Chemistry
|
||||
public readonly struct ReagentQuantity
|
||||
{
|
||||
public readonly string ReagentId;
|
||||
public readonly int Quantity;
|
||||
public readonly decimal Quantity;
|
||||
|
||||
public ReagentQuantity(string reagentId, int quantity)
|
||||
public ReagentQuantity(string reagentId, decimal quantity)
|
||||
{
|
||||
ReagentId = reagentId;
|
||||
Quantity = quantity;
|
||||
|
||||
@@ -18,11 +18,11 @@ namespace Content.Shared.GameObjects.Components.Chemistry
|
||||
[Serializable, NetSerializable]
|
||||
protected sealed class InjectorComponentState : ComponentState
|
||||
{
|
||||
public int CurrentVolume { get; }
|
||||
public int TotalVolume { get; }
|
||||
public decimal CurrentVolume { get; }
|
||||
public decimal TotalVolume { get; }
|
||||
public InjectorToggleMode CurrentMode { get; }
|
||||
|
||||
public InjectorComponentState(int currentVolume, int totalVolume, InjectorToggleMode currentMode) : base(ContentNetIDs.REAGENT_INJECTOR)
|
||||
public InjectorComponentState(decimal currentVolume, decimal totalVolume, InjectorToggleMode currentMode) : base(ContentNetIDs.REAGENT_INJECTOR)
|
||||
{
|
||||
CurrentVolume = currentVolume;
|
||||
TotalVolume = totalVolume;
|
||||
|
||||
@@ -26,8 +26,8 @@ namespace Content.Shared.GameObjects.Components.Chemistry
|
||||
public class ReagentDispenserBoundUserInterfaceState : BoundUserInterfaceState
|
||||
{
|
||||
public readonly bool HasBeaker;
|
||||
public readonly int BeakerCurrentVolume;
|
||||
public readonly int BeakerMaxVolume;
|
||||
public readonly decimal BeakerCurrentVolume;
|
||||
public readonly decimal 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 int SelectedDispenseAmount;
|
||||
public readonly decimal SelectedDispenseAmount;
|
||||
|
||||
public ReagentDispenserBoundUserInterfaceState(bool hasBeaker, int beakerCurrentVolume, int beakerMaxVolume, string containerName,
|
||||
List<ReagentDispenserInventoryEntry> inventory, string dispenserName, List<Solution.ReagentQuantity> containerReagents, int selectedDispenseAmount)
|
||||
public ReagentDispenserBoundUserInterfaceState(bool hasBeaker, decimal beakerCurrentVolume, decimal beakerMaxVolume, string containerName,
|
||||
List<ReagentDispenserInventoryEntry> inventory, string dispenserName, List<Solution.ReagentQuantity> containerReagents, decimal selectedDispenseAmount)
|
||||
{
|
||||
HasBeaker = hasBeaker;
|
||||
BeakerCurrentVolume = beakerCurrentVolume;
|
||||
|
||||
@@ -18,7 +18,7 @@ namespace Content.Shared.GameObjects.Components.Chemistry
|
||||
|
||||
[ViewVariables]
|
||||
protected Solution _containedSolution = new Solution();
|
||||
protected int _maxVolume;
|
||||
protected decimal _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 int MaxVolume
|
||||
public decimal 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 int CurrentVolume => _containedSolution.TotalVolume;
|
||||
public decimal CurrentVolume => _containedSolution.TotalVolume;
|
||||
|
||||
/// <summary>
|
||||
/// The volume without reagents remaining in the container.
|
||||
/// </summary>
|
||||
[ViewVariables]
|
||||
public int EmptyVolume => MaxVolume - CurrentVolume;
|
||||
public decimal EmptyVolume => MaxVolume - CurrentVolume;
|
||||
|
||||
/// <summary>
|
||||
/// The current blended color of all the reagents in the container.
|
||||
@@ -122,7 +122,7 @@ namespace Content.Shared.GameObjects.Components.Chemistry
|
||||
OnSolutionChanged();
|
||||
}
|
||||
|
||||
public bool TryRemoveReagent(string reagentId, int quantity)
|
||||
public bool TryRemoveReagent(string reagentId, decimal quantity)
|
||||
{
|
||||
if (!ContainsReagent(reagentId, out var currentQuantity)) return false;
|
||||
|
||||
@@ -146,7 +146,7 @@ namespace Content.Shared.GameObjects.Components.Chemistry
|
||||
return true;
|
||||
}
|
||||
|
||||
public Solution SplitSolution(int quantity)
|
||||
public Solution SplitSolution(decimal quantity)
|
||||
{
|
||||
var solutionSplit = _containedSolution.SplitSolution(quantity);
|
||||
OnSolutionChanged();
|
||||
@@ -159,7 +159,7 @@ namespace Content.Shared.GameObjects.Components.Chemistry
|
||||
SubstanceColor = Color.White;
|
||||
|
||||
Color mixColor = default;
|
||||
float runningTotalQuantity = 0;
|
||||
var runningTotalQuantity = 0M;
|
||||
|
||||
foreach (var reagent in _containedSolution)
|
||||
{
|
||||
@@ -171,7 +171,7 @@ namespace Content.Shared.GameObjects.Components.Chemistry
|
||||
if (mixColor == default)
|
||||
mixColor = proto.SubstanceColor;
|
||||
|
||||
mixColor = BlendRGB(mixColor, proto.SubstanceColor, reagent.Quantity / runningTotalQuantity);
|
||||
mixColor = BlendRGB(mixColor, proto.SubstanceColor, (float) (reagent.Quantity / runningTotalQuantity));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -216,7 +216,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 int quantity)
|
||||
public bool ContainsReagent(string reagentId, out decimal quantity)
|
||||
{
|
||||
foreach (var reagent in _containedSolution.Contents)
|
||||
{
|
||||
|
||||
@@ -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>
|
||||
int Metabolize(IEntity solutionEntity, string reagentId, float tickTime);
|
||||
decimal Metabolize(IEntity solutionEntity, string reagentId, float tickTime);
|
||||
}
|
||||
}
|
||||
|
||||
12
Content.Shared/Maths/Rounders.cs
Normal file
12
Content.Shared/Maths/Rounders.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
|
||||
namespace Content.Shared.Maths
|
||||
{
|
||||
public static class Rounders
|
||||
{
|
||||
public static decimal RoundForReagents(this decimal me)
|
||||
{
|
||||
return Math.Round(me, 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user