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

@@ -29,7 +29,6 @@ namespace Content.Client
IoCManager.Register<IModuleManager, ClientModuleManager>();
IoCManager.Register<IClientPreferencesManager, ClientPreferencesManager>();
IoCManager.Register<IItemSlotManager, ItemSlotManager>();
IoCManager.Register<IRounderForReagents, RounderForReagents>();
}
}
}

View File

@@ -7,6 +7,7 @@ using Robust.Client.UserInterface.Controls;
using Robust.Shared.GameObjects;
using Robust.Shared.Localization;
using Robust.Shared.ViewVariables;
using Content.Shared.Chemistry;
namespace Content.Client.GameObjects.Components.Chemistry
{
@@ -16,8 +17,8 @@ namespace Content.Client.GameObjects.Components.Chemistry
[RegisterComponent]
public class InjectorComponent : SharedInjectorComponent, IItemStatus
{
[ViewVariables] private decimal CurrentVolume { get; set; }
[ViewVariables] private decimal TotalVolume { get; set; }
[ViewVariables] private ReagentUnit CurrentVolume { get; set; }
[ViewVariables] private ReagentUnit TotalVolume { get; set; }
[ViewVariables] private InjectorToggleMode CurrentMode { get; set; }
[ViewVariables(VVAccess.ReadWrite)] private bool _uiUpdateNeeded;

View File

@@ -170,7 +170,7 @@ namespace Content.Client.GameObjects.Components.Chemistry
Title = castState.DispenserName;
UpdateContainerInfo(castState);
switch (castState.SelectedDispenseAmount)
switch (castState.SelectedDispenseAmount.Int())
{
case 1:
DispenseButton1.Pressed = true;

View File

@@ -1,5 +1,6 @@
using System;
using Content.Server.GameObjects.Components.Nutrition;
using Content.Shared.Chemistry;
using Content.Shared.Interfaces.Chemistry;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Serialization;
@@ -14,8 +15,8 @@ namespace Content.Server.Chemistry.Metabolism
class DefaultDrink : IMetabolizable
{
//Rate of metabolism in units / second
private int _metabolismRate;
public int MetabolismRate => _metabolismRate;
private ReagentUnit _metabolismRate;
public ReagentUnit MetabolismRate => _metabolismRate;
//How much thirst is satiated when 1u of the reagent is metabolized
private float _hydrationFactor;
@@ -23,16 +24,16 @@ namespace Content.Server.Chemistry.Metabolism
void IExposeData.ExposeData(ObjectSerializer serializer)
{
serializer.DataField(ref _metabolismRate, "rate", 1);
serializer.DataField(ref _metabolismRate, "rate", ReagentUnit.New(1));
serializer.DataField(ref _hydrationFactor, "nutrimentFactor", 30.0f);
}
//Remove reagent at set rate, satiate thirst if a ThirstComponent can be found
decimal IMetabolizable.Metabolize(IEntity solutionEntity, string reagentId, float tickTime)
ReagentUnit IMetabolizable.Metabolize(IEntity solutionEntity, string reagentId, float tickTime)
{
int metabolismAmount = (int)Math.Round(MetabolismRate * tickTime);
var metabolismAmount = MetabolismRate * tickTime;
if (solutionEntity.TryGetComponent(out ThirstComponent thirst))
thirst.UpdateThirst(metabolismAmount * HydrationFactor);
thirst.UpdateThirst(metabolismAmount.Float() * HydrationFactor);
//Return amount of reagent to be removed, remove reagent regardless of ThirstComponent presence
return metabolismAmount;

View File

@@ -1,4 +1,5 @@
using Content.Server.GameObjects.Components.Nutrition;
using Content.Shared.Chemistry;
using Content.Shared.Interfaces.Chemistry;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Serialization;
@@ -13,29 +14,26 @@ namespace Content.Server.Chemistry.Metabolism
/// </summary>
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;
private ReagentUnit _metabolismRate;
public ReagentUnit MetabolismRate => _metabolismRate;
//How much hunger is satiated when 1u of the reagent is metabolized
private decimal _nutritionFactor;
public decimal NutritionFactor => _nutritionFactor;
private float _nutritionFactor;
public float NutritionFactor => _nutritionFactor;
void IExposeData.ExposeData(ObjectSerializer serializer)
{
serializer.DataField(ref _metabolismRate, "rate", 1M);
serializer.DataField(ref _nutritionFactor, "nutrimentFactor", 30.0M);
serializer.DataField(ref _metabolismRate, "rate", ReagentUnit.New(1M));
serializer.DataField(ref _nutritionFactor, "nutrimentFactor", 30.0f);
}
//Remove reagent at set rate, satiate hunger if a HungerComponent can be found
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);
var metabolismAmount = MetabolismRate * tickTime;
if (solutionEntity.TryGetComponent(out HungerComponent hunger))
hunger.UpdateFood((float)(metabolismAmount * NutritionFactor));
hunger.UpdateFood(metabolismAmount.Float() * NutritionFactor);
//Return amount of reagent to be removed, remove reagent regardless of HungerComponent presence
return metabolismAmount;

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using Content.Shared.Chemistry;
using Content.Shared.Interfaces;
using Robust.Shared.Interfaces.Serialization;
using Robust.Shared.Prototypes;
@@ -16,7 +17,7 @@ namespace Content.Server.Chemistry
private string _id;
private string _name;
private Dictionary<string, ReactantPrototype> _reactants;
private Dictionary<string, uint> _products;
private Dictionary<string, ReagentUnit> _products;
private List<IReactionEffect> _effects;
public string ID => _id;
@@ -28,7 +29,7 @@ namespace Content.Server.Chemistry
/// <summary>
/// Reagents created when the reaction occurs.
/// </summary>
public IReadOnlyDictionary<string, uint> Products => _products;
public IReadOnlyDictionary<string, ReagentUnit> Products => _products;
/// <summary>
/// Effects to be triggered when the reaction occurs.
/// </summary>
@@ -41,7 +42,7 @@ namespace Content.Server.Chemistry
serializer.DataField(ref _id, "id", string.Empty);
serializer.DataField(ref _name, "name", string.Empty);
serializer.DataField(ref _reactants, "reactants", new Dictionary<string, ReactantPrototype>());
serializer.DataField(ref _products, "products", new Dictionary<string, uint>());
serializer.DataField(ref _products, "products", new Dictionary<string, ReagentUnit>());
serializer.DataField(ref _effects, "effects", new List<IReactionEffect>());
}
}
@@ -51,13 +52,13 @@ namespace Content.Server.Chemistry
/// </summary>
public class ReactantPrototype : IExposeData
{
private int _amount;
private ReagentUnit _amount;
private bool _catalyst;
/// <summary>
/// Minimum amount of the reactant needed for the reaction to occur.
/// </summary>
public int Amount => _amount;
public ReagentUnit Amount => _amount;
/// <summary>
/// Whether or not the reactant is a catalyst. Catalysts aren't removed when a reaction occurs.
/// </summary>
@@ -65,7 +66,7 @@ namespace Content.Server.Chemistry
public void ExposeData(ObjectSerializer serializer)
{
serializer.DataField(ref _amount, "amount", 1);
serializer.DataField(ref _amount, "amount", ReagentUnit.New(1));
serializer.DataField(ref _catalyst, "catalyst", false);
}
}

View File

@@ -37,13 +37,13 @@ namespace Content.Server.GameObjects.Components.Chemistry
/// attempt to inject it's entire contents upon use.
/// </summary>
[ViewVariables]
private decimal _transferAmount;
private ReagentUnit _transferAmount;
/// <summary>
/// Initial storage volume of the injector
/// </summary>
[ViewVariables]
private int _initialMaxVolume;
private ReagentUnit _initialMaxVolume;
/// <summary>
/// The state of the injector. Determines it's attack behavior. Containers must have the
@@ -62,8 +62,8 @@ namespace Content.Server.GameObjects.Components.Chemistry
{
base.ExposeData(serializer);
serializer.DataField(ref _injectOnly, "injectOnly", false);
serializer.DataField(ref _initialMaxVolume, "initialMaxVolume", 15);
serializer.DataField(ref _transferAmount, "transferAmount", 5);
serializer.DataField(ref _initialMaxVolume, "initialMaxVolume", ReagentUnit.New(15));
serializer.DataField(ref _transferAmount, "transferAmount", ReagentUnit.New(5));
}
public override void Initialize()
@@ -165,7 +165,7 @@ namespace Content.Server.GameObjects.Components.Chemistry
}
//Get transfer amount. May be smaller than _transferAmount if not enough room
decimal realTransferAmount = Math.Min(_transferAmount, targetBloodstream.EmptyVolume);
var realTransferAmount = ReagentUnit.Min(_transferAmount, targetBloodstream.EmptyVolume);
if (realTransferAmount <= 0)
{
_notifyManager.PopupMessage(Owner.Transform.GridPosition, user,
@@ -193,7 +193,7 @@ namespace Content.Server.GameObjects.Components.Chemistry
}
//Get transfer amount. May be smaller than _transferAmount if not enough room
decimal realTransferAmount = Math.Min(_transferAmount, targetSolution.EmptyVolume);
var realTransferAmount = ReagentUnit.Min(_transferAmount, targetSolution.EmptyVolume);
if (realTransferAmount <= 0)
{
_notifyManager.PopupMessage(Owner.Transform.GridPosition, user,
@@ -221,7 +221,7 @@ namespace Content.Server.GameObjects.Components.Chemistry
}
//Get transfer amount. May be smaller than _transferAmount if not enough room
decimal realTransferAmount = Math.Min(_transferAmount, targetSolution.CurrentVolume);
var realTransferAmount = ReagentUnit.Min(_transferAmount, targetSolution.CurrentVolume);
if (realTransferAmount <= 0)
{
_notifyManager.PopupMessage(Owner.Transform.GridPosition, user,

View File

@@ -4,6 +4,7 @@ using System.Text;
using Content.Server.GameObjects.Components.Nutrition;
using Content.Server.GameObjects.EntitySystems;
using Content.Server.Interfaces;
using Content.Shared.Chemistry;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
@@ -28,13 +29,13 @@ namespace Content.Server.GameObjects.Components.Chemistry
public override string Name => "Pourable";
private decimal _transferAmount;
private ReagentUnit _transferAmount;
/// <summary>
/// The amount of solution to be transferred from this solution when clicking on other solutions with it.
/// </summary>
[ViewVariables]
public decimal TransferAmount
public ReagentUnit TransferAmount
{
get => _transferAmount;
set => _transferAmount = value;
@@ -43,7 +44,7 @@ namespace Content.Server.GameObjects.Components.Chemistry
public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);
serializer.DataField(ref _transferAmount, "transferAmount", 5.0M);
serializer.DataField(ref _transferAmount, "transferAmount", ReagentUnit.New(5.0M));
}
/// <summary>
@@ -69,7 +70,7 @@ namespace Content.Server.GameObjects.Components.Chemistry
return false;
//Get transfer amount. May be smaller than _transferAmount if not enough room
decimal realTransferAmount = Math.Min(attackPourable.TransferAmount, targetSolution.EmptyVolume);
var realTransferAmount = ReagentUnit.Min(attackPourable.TransferAmount, targetSolution.EmptyVolume);
if (realTransferAmount <= 0) //Special message if container is full
{
_notifyManager.PopupMessage(Owner.Transform.GridPosition, eventArgs.User,

View File

@@ -41,7 +41,7 @@ namespace Content.Server.GameObjects.Components.Chemistry
[ViewVariables] private string _packPrototypeId;
[ViewVariables] private bool HasBeaker => _beakerContainer.ContainedEntity != null;
[ViewVariables] private decimal _dispenseAmount = 10;
[ViewVariables] private ReagentUnit _dispenseAmount = ReagentUnit.New(10);
[ViewVariables]
private SolutionComponent Solution => _beakerContainer.ContainedEntity.GetComponent<SolutionComponent>();
@@ -115,22 +115,22 @@ namespace Content.Server.GameObjects.Components.Chemistry
TryClear();
break;
case UiButton.SetDispenseAmount1:
_dispenseAmount = 1;
_dispenseAmount = ReagentUnit.New(1);
break;
case UiButton.SetDispenseAmount5:
_dispenseAmount = 5;
_dispenseAmount = ReagentUnit.New(5);
break;
case UiButton.SetDispenseAmount10:
_dispenseAmount = 10;
_dispenseAmount = ReagentUnit.New(10);
break;
case UiButton.SetDispenseAmount25:
_dispenseAmount = 25;
_dispenseAmount = ReagentUnit.New(25);
break;
case UiButton.SetDispenseAmount50:
_dispenseAmount = 50;
_dispenseAmount = ReagentUnit.New(50);
break;
case UiButton.SetDispenseAmount100:
_dispenseAmount = 100;
_dispenseAmount = ReagentUnit.New(100);
break;
case UiButton.Dispense:
if (HasBeaker)
@@ -172,7 +172,7 @@ namespace Content.Server.GameObjects.Components.Chemistry
var beaker = _beakerContainer.ContainedEntity;
if (beaker == null)
{
return new ReagentDispenserBoundUserInterfaceState(false, 0, 0,
return new ReagentDispenserBoundUserInterfaceState(false, ReagentUnit.New(0), ReagentUnit.New(0),
"", Inventory, Owner.Name, null, _dispenseAmount);
}

View File

@@ -22,7 +22,6 @@ 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;
@@ -105,8 +104,7 @@ namespace Content.Server.GameObjects.Components.Chemistry
if ((handSolutionComp.Capabilities & SolutionCaps.PourOut) == 0 || (component.Capabilities & SolutionCaps.PourIn) == 0)
return;
var transferQuantity = Math.Min(component.MaxVolume - component.CurrentVolume, handSolutionComp.CurrentVolume);
transferQuantity = Math.Min(transferQuantity, 10);
var transferQuantity = ReagentUnit.Min(component.MaxVolume - component.CurrentVolume, handSolutionComp.CurrentVolume, ReagentUnit.New(10));
// nothing to transfer
if (transferQuantity <= 0)
@@ -185,8 +183,7 @@ namespace Content.Server.GameObjects.Components.Chemistry
if ((handSolutionComp.Capabilities & SolutionCaps.PourIn) == 0 || (component.Capabilities & SolutionCaps.PourOut) == 0)
return;
var transferQuantity = Math.Min(handSolutionComp.MaxVolume - handSolutionComp.CurrentVolume, component.CurrentVolume);
transferQuantity = Math.Min(transferQuantity, 10);
var transferQuantity = ReagentUnit.Min(handSolutionComp.MaxVolume - handSolutionComp.CurrentVolume, component.CurrentVolume, ReagentUnit.New(10));
// pulling from an empty container, pointless to continue
if (transferQuantity <= 0)
@@ -223,10 +220,9 @@ 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, ReagentUnit quantity, out ReagentUnit acceptedQuantity, bool skipReactionCheck = false, bool skipColor = false)
{
quantity = _rounder.Round(quantity);
var toAcceptQuantity = _rounder.Round(MaxVolume - ContainedSolution.TotalVolume);
var toAcceptQuantity = MaxVolume - ContainedSolution.TotalVolume;
if (quantity > toAcceptQuantity)
{
acceptedQuantity = toAcceptQuantity;
@@ -269,16 +265,16 @@ namespace Content.Server.GameObjects.Components.Chemistry
/// <param name="reaction">The reaction whose reactants will be checked for in the solution.</param>
/// <param name="unitReactions">The number of times the reaction can occur with the given solution.</param>
/// <returns></returns>
private bool SolutionValidReaction(ReactionPrototype reaction, out decimal unitReactions)
private bool SolutionValidReaction(ReactionPrototype reaction, out ReagentUnit unitReactions)
{
unitReactions = decimal.MaxValue; //Set to some impossibly large number initially
unitReactions = ReagentUnit.MaxValue; //Set to some impossibly large number initially
foreach (var reactant in reaction.Reactants)
{
if (!ContainsReagent(reactant.Key, out decimal reagentQuantity))
if (!ContainsReagent(reactant.Key, out ReagentUnit reagentQuantity))
{
return false;
}
var currentUnitReactions = _rounder.Round(reagentQuantity / reactant.Value.Amount);
var currentUnitReactions = reagentQuantity / reactant.Value.Amount;
if (currentUnitReactions < unitReactions)
{
unitReactions = currentUnitReactions;
@@ -301,26 +297,26 @@ namespace Content.Server.GameObjects.Components.Chemistry
/// <param name="solution">Solution to be reacted.</param>
/// <param name="reaction">Reaction to occur.</param>
/// <param name="unitReactions">The number of times to cause this reaction.</param>
private void PerformReaction(ReactionPrototype reaction, decimal unitReactions)
private void PerformReaction(ReactionPrototype reaction, ReagentUnit unitReactions)
{
//Remove non-catalysts
foreach (var reactant in reaction.Reactants)
{
if (!reactant.Value.Catalyst)
{
var amountToRemove = _rounder.Round(unitReactions * reactant.Value.Amount);
var amountToRemove = unitReactions * reactant.Value.Amount;
TryRemoveReagent(reactant.Key, amountToRemove);
}
}
//Add products
foreach (var product in reaction.Products)
{
TryAddReagent(product.Key, (int)(unitReactions * product.Value), out var acceptedQuantity, true);
TryAddReagent(product.Key, product.Value * unitReactions, out var acceptedQuantity, true);
}
//Trigger reaction effects
foreach (var effect in reaction.Effects)
{
effect.React(Owner, unitReactions);
effect.React(Owner, unitReactions.Decimal());
}
//Play reaction sound client-side

View File

@@ -34,17 +34,17 @@ namespace Content.Server.GameObjects.Components.Metabolism
/// Max volume of internal solution storage
/// </summary>
[ViewVariables]
private int _initialMaxVolume;
private ReagentUnit _initialMaxVolume;
/// <summary>
/// Empty volume of internal solution
/// </summary>
public decimal EmptyVolume => _internalSolution.EmptyVolume;
public ReagentUnit EmptyVolume => _internalSolution.EmptyVolume;
public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);
serializer.DataField(ref _initialMaxVolume, "maxVolume", 250);
serializer.DataField(ref _initialMaxVolume, "maxVolume", ReagentUnit.New(250));
}
public override void Initialize()

View File

@@ -33,18 +33,18 @@ namespace Content.Server.GameObjects.Components.Nutrition
[ViewVariables]
private string _finishPrototype;
public decimal TransferAmount => _transferAmount;
public ReagentUnit TransferAmount => _transferAmount;
[ViewVariables]
private decimal _transferAmount = 2;
private ReagentUnit _transferAmount = ReagentUnit.New(2);
public decimal MaxVolume
public ReagentUnit MaxVolume
{
get => _contents.MaxVolume;
set => _contents.MaxVolume = value;
}
private Solution _initialContents; // This is just for loading from yaml
private int _maxVolume;
private ReagentUnit _maxVolume;
private bool _despawnOnFinish;
@@ -57,7 +57,7 @@ namespace Content.Server.GameObjects.Components.Nutrition
{
return 0;
}
return Math.Max(1, (int)Math.Ceiling(_contents.CurrentVolume / _transferAmount));
return Math.Max(1, (int)Math.Ceiling((_contents.CurrentVolume / _transferAmount).Float()));
}
@@ -65,7 +65,7 @@ namespace Content.Server.GameObjects.Components.Nutrition
{
base.ExposeData(serializer);
serializer.DataField(ref _initialContents, "contents", null);
serializer.DataField(ref _maxVolume, "max_volume", 0);
serializer.DataField(ref _maxVolume, "max_volume", ReagentUnit.New(0));
serializer.DataField(ref _useSound, "use_sound", "/Audio/items/drink.ogg");
// E.g. cola can when done or clear bottle, whatever
// Currently this will enforce it has the same volume but this may change.
@@ -91,7 +91,7 @@ namespace Content.Server.GameObjects.Components.Nutrition
| SolutionCaps.Injectable;
var pourable = Owner.AddComponent<PourableComponent>();
pourable.TransferAmount = 5;
pourable.TransferAmount = ReagentUnit.New(5);
}
}
@@ -150,7 +150,7 @@ namespace Content.Server.GameObjects.Components.Nutrition
if (user.TryGetComponent(out StomachComponent stomachComponent))
{
_drinking = true;
var transferAmount = Math.Min(_transferAmount, _contents.CurrentVolume);
var transferAmount = ReagentUnit.Min(_transferAmount, _contents.CurrentVolume);
var split = _contents.SplitSolution(transferAmount);
if (stomachComponent.TryTransferSolution(split))
{

View File

@@ -33,7 +33,7 @@ namespace Content.Server.GameObjects.Components.Nutrition
[ViewVariables]
private SolutionComponent _contents;
[ViewVariables]
private int _transferAmount;
private ReagentUnit _transferAmount;
private Solution _initialContents; // This is just for loading from yaml
@@ -44,7 +44,7 @@ namespace Content.Server.GameObjects.Components.Nutrition
serializer.DataField(ref _initialContents, "contents", null);
serializer.DataField(ref _useSound, "use_sound", "/Audio/items/eatfood.ogg");
// Default is transfer 30 units
serializer.DataField(ref _transferAmount, "transfer_amount", 5);
serializer.DataField(ref _transferAmount, "transfer_amount", ReagentUnit.New(5));
// E.g. empty chip packet when done
serializer.DataField(ref _finishPrototype, "spawn_on_finish", null);
}
@@ -78,7 +78,7 @@ namespace Content.Server.GameObjects.Components.Nutrition
_initialContents = null;
if (_contents.CurrentVolume == 0)
{
_contents.TryAddReagent("chem.Nutriment", 5, out _);
_contents.TryAddReagent("chem.Nutriment", ReagentUnit.New(5), out _);
}
Owner.TryGetComponent(out AppearanceComponent appearance);
_appearanceComponent = appearance;
@@ -99,7 +99,7 @@ namespace Content.Server.GameObjects.Components.Nutrition
{
return 0;
}
return Math.Max(1, (int)Math.Ceiling(_contents.CurrentVolume / _transferAmount));
return Math.Max(1, (int)Math.Ceiling((_contents.CurrentVolume / _transferAmount).Float()));
}
bool IUse.UseEntity(UseEntityEventArgs eventArgs)
@@ -130,7 +130,7 @@ namespace Content.Server.GameObjects.Components.Nutrition
// TODO: Add putting food back in boxes here?
if (user.TryGetComponent(out StomachComponent stomachComponent))
{
var transferAmount = Math.Min(_transferAmount, _contents.CurrentVolume);
var transferAmount = ReagentUnit.Min(_transferAmount, _contents.CurrentVolume);
var split = _contents.SplitSolution(transferAmount);
if (stomachComponent.TryTransferSolution(split))
{

View File

@@ -27,7 +27,7 @@ namespace Content.Server.GameObjects.Components.Nutrition
/// <summary>
/// Max volume of internal solution storage
/// </summary>
public decimal MaxVolume
public ReagentUnit MaxVolume
{
get => _stomachContents.MaxVolume;
set => _stomachContents.MaxVolume = value;
@@ -43,7 +43,7 @@ namespace Content.Server.GameObjects.Components.Nutrition
/// Initial internal solution storage volume
/// </summary>
[ViewVariables]
private int _initialMaxVolume;
private ReagentUnit _initialMaxVolume;
/// <summary>
/// Time in seconds between reagents being ingested and them being transferred to <see cref="BloodstreamComponent"/>
@@ -64,7 +64,7 @@ namespace Content.Server.GameObjects.Components.Nutrition
public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);
serializer.DataField(ref _initialMaxVolume, "maxVolume", 100);
serializer.DataField(ref _initialMaxVolume, "maxVolume", ReagentUnit.New(100));
serializer.DataField(ref _digestionDelay, "digestionDelay", 20);
}
@@ -119,7 +119,7 @@ namespace Content.Server.GameObjects.Components.Nutrition
}
//Add reagents ready for transfer to bloodstream to transferSolution
var transferSolution = IoCManager.InjectDependencies(new Solution());
var transferSolution = new Solution();
foreach (var delta in _reagentDeltas.ToList()) //Use ToList here to remove entries while iterating
{
//Increment lifetime of reagents
@@ -141,10 +141,10 @@ namespace Content.Server.GameObjects.Components.Nutrition
private class ReagentDelta
{
public readonly string ReagentId;
public readonly decimal Quantity;
public readonly ReagentUnit Quantity;
public float Lifetime { get; private set; }
public ReagentDelta(string reagentId, decimal quantity)
public ReagentDelta(string reagentId, ReagentUnit quantity)
{
ReagentId = reagentId;
Quantity = quantity;

View File

@@ -28,7 +28,6 @@ namespace Content.Server
IoCManager.Register<ICargoOrderDataManager, CargoOrderDataManager>();
IoCManager.Register<IModuleManager, ServerModuleManager>();
IoCManager.Register<IServerPreferencesManager, ServerPreferencesManager>();
IoCManager.Register<IRounderForReagents, RounderForReagents>();
}
}
}

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

View File

@@ -0,0 +1,113 @@
using Content.Shared.Chemistry;
using NUnit.Framework;
using System;
namespace Content.Tests.Shared.Chemistry
{
[TestFixture, TestOf(typeof(ReagentUnit))]
public class ReagentUnit_Tests
{
[Test]
[TestCase(1, "1")]
[TestCase(0, "0")]
[TestCase(-1, "-1")]
public void ReagentUnitIntegerTests(int value, string expected)
{
var result = ReagentUnit.New(value);
Assert.AreEqual(expected, $"{result}");
}
[Test]
[TestCase(1.001f, "1")]
[TestCase(0.999f, "1")]
public void ReagentUnitFloatTests(float value, string expected)
{
var result = ReagentUnit.New(value);
Assert.AreEqual(expected, $"{result}");
}
[Test]
[TestCase(1.001d, "1")]
[TestCase(0.999d, "1")]
public void ReagentUnitDoubleTests(double value, string expected)
{
var result = ReagentUnit.New(value);
Assert.AreEqual(expected, $"{result}");
}
[Test]
[TestCase("1.001", "1")]
[TestCase("0.999", "1")]
public void ReagentUnitDecimalTests(string valueAsString, string expected)
{
var value = decimal.Parse(valueAsString);
var result = ReagentUnit.New(value);
Assert.AreEqual(expected, $"{result}");
}
[Test]
[TestCase(1.001f, 1.001f, "2")]
[TestCase(1.001f, 1.004f, "2")]
[TestCase(1f, 2.005f, "3.01")]
public void CalculusPlus(float aFloat, float bFloat, string expected)
{
var a = ReagentUnit.New(aFloat);
var b = ReagentUnit.New(bFloat);
var result = a + b;
Assert.AreEqual(expected, $"{result}");
}
[Test]
[TestCase(1.001f, 1.001f, "0")]
[TestCase(1.001f, 1.004f, "0")]
[TestCase(1f, 2.005f, "-1.01")]
public void CalculusMinus(float aFloat, float bFloat, string expected)
{
var a = ReagentUnit.New(aFloat);
var b = ReagentUnit.New(bFloat);
var result = a - b;
Assert.AreEqual(expected, $"{result}");
}
[Test]
[TestCase(1.001f, 3f, "0.33")]
[TestCase(0.999f, 3f, "0.33")]
[TestCase(2.1f, 3f, "0.7")]
public void CalculusDivision(float aFloat, float bFloat, string expected)
{
var a = ReagentUnit.New(aFloat);
var b = ReagentUnit.New(bFloat);
var result = a / b;
Assert.AreEqual(expected, $"{result}");
}
[Test]
[TestCase(1.001f, 0.999f, "1")]
[TestCase(0.999f, 3f, "3")]
public void CalculusMultiplication(float aFloat, float bFloat, string expected)
{
var a = ReagentUnit.New(aFloat);
var b = ReagentUnit.New(bFloat);
var result = a * b;
Assert.AreEqual(expected, $"{result}");
}
[Test]
[TestCase(0.995f, 100)]
[TestCase(1.005f, 100)]
[TestCase(2.005f, 201)]
public void FloatRoundingTest(float a, int expected)
{
var result = (int) Math.Round(a * (float) Math.Pow(10, 2));
Assert.AreEqual(expected, result);
}
}
}

View File

@@ -10,19 +10,19 @@ namespace Content.Tests.Shared.Chemistry
public void AddReagentAndGetSolution()
{
var solution = new Solution();
solution.AddReagent("water", 1000);
solution.AddReagent("water", ReagentUnit.New(1000));
var quantity = solution.GetReagentQuantity("water");
Assert.That(quantity, Is.EqualTo(1000));
Assert.That(quantity.Int(), Is.EqualTo(1000));
}
[Test]
public void ConstructorAddReagent()
{
var solution = new Solution("water", 1000);
var solution = new Solution("water", ReagentUnit.New(1000));
var quantity = solution.GetReagentQuantity("water");
Assert.That(quantity, Is.EqualTo(1000));
Assert.That(quantity.Int(), Is.EqualTo(1000));
}
[Test]
@@ -31,223 +31,223 @@ namespace Content.Tests.Shared.Chemistry
var solution = new Solution();
var quantity = solution.GetReagentQuantity("water");
Assert.That(quantity, Is.EqualTo(0));
Assert.That(quantity.Int(), Is.EqualTo(0));
}
[Test]
public void AddLessThanZeroReagentReturnsZero()
{
var solution = new Solution("water", -1000);
var solution = new Solution("water", ReagentUnit.New(-1000));
var quantity = solution.GetReagentQuantity("water");
Assert.That(quantity, Is.EqualTo(0));
Assert.That(quantity.Int(), Is.EqualTo(0));
}
[Test]
public void AddingReagentsSumsProperly()
{
var solution = new Solution();
solution.AddReagent("water", 1000);
solution.AddReagent("water", 2000);
solution.AddReagent("water", ReagentUnit.New(1000));
solution.AddReagent("water", ReagentUnit.New(2000));
var quantity = solution.GetReagentQuantity("water");
Assert.That(quantity, Is.EqualTo(3000));
Assert.That(quantity.Int(), Is.EqualTo(3000));
}
[Test]
public void ReagentQuantitiesStayUnique()
{
var solution = new Solution();
solution.AddReagent("water", 1000);
solution.AddReagent("fire", 2000);
solution.AddReagent("water", ReagentUnit.New(1000));
solution.AddReagent("fire", ReagentUnit.New(2000));
Assert.That(solution.GetReagentQuantity("water"), Is.EqualTo(1000));
Assert.That(solution.GetReagentQuantity("fire"), Is.EqualTo(2000));
Assert.That(solution.GetReagentQuantity("water").Int(), Is.EqualTo(1000));
Assert.That(solution.GetReagentQuantity("fire").Int(), Is.EqualTo(2000));
}
[Test]
public void TotalVolumeIsCorrect()
{
var solution = new Solution();
solution.AddReagent("water", 1000);
solution.AddReagent("fire", 2000);
solution.AddReagent("water", ReagentUnit.New(1000));
solution.AddReagent("fire", ReagentUnit.New(2000));
Assert.That(solution.TotalVolume, Is.EqualTo(3000));
Assert.That(solution.TotalVolume.Int(), Is.EqualTo(3000));
}
[Test]
public void CloningSolutionIsCorrect()
{
var solution = new Solution();
solution.AddReagent("water", 1000);
solution.AddReagent("fire", 2000);
solution.AddReagent("water", ReagentUnit.New(1000));
solution.AddReagent("fire", ReagentUnit.New(2000));
var newSolution = solution.Clone();
Assert.That(newSolution.GetReagentQuantity("water"), Is.EqualTo(1000));
Assert.That(newSolution.GetReagentQuantity("fire"), Is.EqualTo(2000));
Assert.That(newSolution.TotalVolume, Is.EqualTo(3000));
Assert.That(newSolution.GetReagentQuantity("water").Int(), Is.EqualTo(1000));
Assert.That(newSolution.GetReagentQuantity("fire").Int(), Is.EqualTo(2000));
Assert.That(newSolution.TotalVolume.Int(), Is.EqualTo(3000));
}
[Test]
public void RemoveSolutionRecalculatesProperly()
{
var solution = new Solution();
solution.AddReagent("water", 1000);
solution.AddReagent("fire", 2000);
solution.AddReagent("water", ReagentUnit.New(1000));
solution.AddReagent("fire", ReagentUnit.New(2000));
solution.RemoveReagent("water", 500);
solution.RemoveReagent("water", ReagentUnit.New(500));
Assert.That(solution.GetReagentQuantity("water"), Is.EqualTo(500));
Assert.That(solution.GetReagentQuantity("fire"), Is.EqualTo(2000));
Assert.That(solution.TotalVolume, Is.EqualTo(2500));
Assert.That(solution.GetReagentQuantity("water").Int(), Is.EqualTo(500));
Assert.That(solution.GetReagentQuantity("fire").Int(), Is.EqualTo(2000));
Assert.That(solution.TotalVolume.Int(), Is.EqualTo(2500));
}
[Test]
public void RemoveLessThanOneQuantityDoesNothing()
{
var solution = new Solution("water", 100);
var solution = new Solution("water", ReagentUnit.New(100));
solution.RemoveReagent("water", -100);
solution.RemoveReagent("water", ReagentUnit.New(-100));
Assert.That(solution.GetReagentQuantity("water"), Is.EqualTo(100));
Assert.That(solution.TotalVolume, Is.EqualTo(100));
Assert.That(solution.GetReagentQuantity("water").Int(), Is.EqualTo(100));
Assert.That(solution.TotalVolume.Int(), Is.EqualTo(100));
}
[Test]
public void RemoveMoreThanTotalRemovesAllReagent()
{
var solution = new Solution("water", 100);
var solution = new Solution("water", ReagentUnit.New(100));
solution.RemoveReagent("water", 1000);
solution.RemoveReagent("water", ReagentUnit.New(1000));
Assert.That(solution.GetReagentQuantity("water"), Is.EqualTo(0));
Assert.That(solution.TotalVolume, Is.EqualTo(0));
Assert.That(solution.GetReagentQuantity("water").Int(), Is.EqualTo(0));
Assert.That(solution.TotalVolume.Int(), Is.EqualTo(0));
}
[Test]
public void RemoveNonExistReagentDoesNothing()
{
var solution = new Solution("water", 100);
var solution = new Solution("water", ReagentUnit.New(100));
solution.RemoveReagent("fire", 1000);
solution.RemoveReagent("fire", ReagentUnit.New(1000));
Assert.That(solution.GetReagentQuantity("water"), Is.EqualTo(100));
Assert.That(solution.TotalVolume, Is.EqualTo(100));
Assert.That(solution.GetReagentQuantity("water").Int(), Is.EqualTo(100));
Assert.That(solution.TotalVolume.Int(), Is.EqualTo(100));
}
[Test]
public void RemoveSolution()
{
var solution = new Solution("water", 700);
var solution = new Solution("water", ReagentUnit.New(700));
solution.RemoveSolution(500);
solution.RemoveSolution(ReagentUnit.New(500));
//Check that edited solution is correct
Assert.That(solution.GetReagentQuantity("water"), Is.EqualTo(200));
Assert.That(solution.TotalVolume, Is.EqualTo(200));
Assert.That(solution.GetReagentQuantity("water").Int(), Is.EqualTo(200));
Assert.That(solution.TotalVolume.Int(), Is.EqualTo(200));
}
[Test]
public void RemoveSolutionMoreThanTotalRemovesAll()
{
var solution = new Solution("water", 800);
var solution = new Solution("water", ReagentUnit.New(800));
solution.RemoveSolution(1000);
solution.RemoveSolution(ReagentUnit.New(1000));
//Check that edited solution is correct
Assert.That(solution.GetReagentQuantity("water"), Is.EqualTo(0));
Assert.That(solution.TotalVolume, Is.EqualTo(0));
Assert.That(solution.GetReagentQuantity("water").Int(), Is.EqualTo(0));
Assert.That(solution.TotalVolume.Int(), Is.EqualTo(0));
}
[Test]
public void RemoveSolutionRatioPreserved()
{
var solution = new Solution();
solution.AddReagent("water", 1000);
solution.AddReagent("fire", 2000);
solution.AddReagent("water", ReagentUnit.New(1000));
solution.AddReagent("fire", ReagentUnit.New(2000));
solution.RemoveSolution(1500);
solution.RemoveSolution(ReagentUnit.New(1500));
Assert.That(solution.GetReagentQuantity("water"), Is.EqualTo(500));
Assert.That(solution.GetReagentQuantity("fire"), Is.EqualTo(1000));
Assert.That(solution.TotalVolume, Is.EqualTo(1500));
Assert.That(solution.GetReagentQuantity("water").Int(), Is.EqualTo(500));
Assert.That(solution.GetReagentQuantity("fire").Int(), Is.EqualTo(1000));
Assert.That(solution.TotalVolume.Int(), Is.EqualTo(1500));
}
[Test]
public void RemoveSolutionLessThanOneDoesNothing()
{
var solution = new Solution("water", 800);
var solution = new Solution("water", ReagentUnit.New(800));
solution.RemoveSolution(-200);
solution.RemoveSolution(ReagentUnit.New(-200));
Assert.That(solution.GetReagentQuantity("water"), Is.EqualTo(800));
Assert.That(solution.TotalVolume, Is.EqualTo(800));
Assert.That(solution.GetReagentQuantity("water").Int(), Is.EqualTo(800));
Assert.That(solution.TotalVolume.Int(), Is.EqualTo(800));
}
[Test]
public void SplitSolution()
{
var solution = new Solution();
solution.AddReagent("water", 1000);
solution.AddReagent("fire", 2000);
solution.AddReagent("water", ReagentUnit.New(1000));
solution.AddReagent("fire", ReagentUnit.New(2000));
var splitSolution = solution.SplitSolution(750);
var splitSolution = solution.SplitSolution(ReagentUnit.New(750));
Assert.That(solution.GetReagentQuantity("water"), Is.EqualTo(750));
Assert.That(solution.GetReagentQuantity("fire"), Is.EqualTo(1500));
Assert.That(solution.TotalVolume, Is.EqualTo(2250));
Assert.That(solution.GetReagentQuantity("water").Int(), Is.EqualTo(750));
Assert.That(solution.GetReagentQuantity("fire").Int(), Is.EqualTo(1500));
Assert.That(solution.TotalVolume.Int(), Is.EqualTo(2250));
Assert.That(splitSolution.GetReagentQuantity("water"), Is.EqualTo(250));
Assert.That(splitSolution.GetReagentQuantity("fire"), Is.EqualTo(500));
Assert.That(splitSolution.TotalVolume, Is.EqualTo(750));
Assert.That(splitSolution.GetReagentQuantity("water").Int(), Is.EqualTo(250));
Assert.That(splitSolution.GetReagentQuantity("fire").Int(), Is.EqualTo(500));
Assert.That(splitSolution.TotalVolume.Int(), Is.EqualTo(750));
}
[Test]
public void SplitSolutionMoreThanTotalRemovesAll()
{
var solution = new Solution("water", 800);
var solution = new Solution("water", ReagentUnit.New(800));
var splitSolution = solution.SplitSolution(1000);
var splitSolution = solution.SplitSolution(ReagentUnit.New(1000));
Assert.That(solution.GetReagentQuantity("water"), Is.EqualTo(0));
Assert.That(solution.TotalVolume, Is.EqualTo(0));
Assert.That(solution.GetReagentQuantity("water").Int(), Is.EqualTo(0));
Assert.That(solution.TotalVolume.Int(), Is.EqualTo(0));
Assert.That(splitSolution.GetReagentQuantity("water"), Is.EqualTo(800));
Assert.That(splitSolution.TotalVolume, Is.EqualTo(800));
Assert.That(splitSolution.GetReagentQuantity("water").Int(), Is.EqualTo(800));
Assert.That(splitSolution.TotalVolume.Int(), Is.EqualTo(800));
}
[Test]
public void SplitSolutionLessThanOneDoesNothing()
{
var solution = new Solution("water", 800);
var solution = new Solution("water", ReagentUnit.New(800));
var splitSolution = solution.SplitSolution(-200);
var splitSolution = solution.SplitSolution(ReagentUnit.New(-200));
Assert.That(solution.GetReagentQuantity("water"), Is.EqualTo(800));
Assert.That(solution.TotalVolume, Is.EqualTo(800));
Assert.That(solution.GetReagentQuantity("water").Int(), Is.EqualTo(800));
Assert.That(solution.TotalVolume.Int(), Is.EqualTo(800));
Assert.That(splitSolution.GetReagentQuantity("water"), Is.EqualTo(0));
Assert.That(splitSolution.TotalVolume, Is.EqualTo(0));
Assert.That(splitSolution.GetReagentQuantity("water").Int(), Is.EqualTo(0));
Assert.That(splitSolution.TotalVolume.Int(), Is.EqualTo(0));
}
[Test]
public void AddSolution()
{
var solutionOne = new Solution();
solutionOne.AddReagent("water", 1000);
solutionOne.AddReagent("fire", 2000);
solutionOne.AddReagent("water", ReagentUnit.New(1000));
solutionOne.AddReagent("fire", ReagentUnit.New(2000));
var solutionTwo = new Solution();
solutionTwo.AddReagent("water", 500);
solutionTwo.AddReagent("earth", 1000);
solutionTwo.AddReagent("water", ReagentUnit.New(500));
solutionTwo.AddReagent("earth", ReagentUnit.New(1000));
solutionOne.AddSolution(solutionTwo);
Assert.That(solutionOne.GetReagentQuantity("water"), Is.EqualTo(1500));
Assert.That(solutionOne.GetReagentQuantity("fire"), Is.EqualTo(2000));
Assert.That(solutionOne.GetReagentQuantity("earth"), Is.EqualTo(1000));
Assert.That(solutionOne.TotalVolume, Is.EqualTo(4500));
Assert.That(solutionOne.GetReagentQuantity("water").Int(), Is.EqualTo(1500));
Assert.That(solutionOne.GetReagentQuantity("fire").Int(), Is.EqualTo(2000));
Assert.That(solutionOne.GetReagentQuantity("earth").Int(), Is.EqualTo(1000));
Assert.That(solutionOne.TotalVolume.Int(), Is.EqualTo(4500));
}
}
}