Changed all int and some float things in Reagent code to Decimals

This commit is contained in:
PrPleGoo
2020-03-14 12:55:07 +01:00
parent b9f9eb6651
commit f05fdfb5fc
19 changed files with 101 additions and 81 deletions

View File

@@ -37,7 +37,7 @@ namespace Content.Server.GameObjects.Components.Chemistry
/// attempt to inject it's entire contents upon use.
/// </summary>
[ViewVariables]
private int _transferAmount;
private decimal _transferAmount;
/// <summary>
/// Initial storage volume of the injector
@@ -165,7 +165,7 @@ namespace Content.Server.GameObjects.Components.Chemistry
}
//Get transfer amount. May be smaller than _transferAmount if not enough room
int realTransferAmount = Math.Min(_transferAmount, targetBloodstream.EmptyVolume);
decimal realTransferAmount = Math.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
int realTransferAmount = Math.Min(_transferAmount, targetSolution.EmptyVolume);
decimal realTransferAmount = Math.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
int realTransferAmount = Math.Min(_transferAmount, targetSolution.CurrentVolume);
decimal realTransferAmount = Math.Min(_transferAmount, targetSolution.CurrentVolume);
if (realTransferAmount <= 0)
{
_notifyManager.PopupMessage(Owner.Transform.GridPosition, user,

View File

@@ -69,7 +69,7 @@ namespace Content.Server.GameObjects.Components.Chemistry
return false;
//Get transfer amount. May be smaller than _transferAmount if not enough room
int realTransferAmount = Math.Min(attackPourable.TransferAmount, targetSolution.EmptyVolume);
decimal realTransferAmount = Math.Min(attackPourable.TransferAmount, targetSolution.EmptyVolume);
if (realTransferAmount <= 0) //Special message if container is full
{
_notifyManager.PopupMessage(Owner.Transform.GridPosition, eventArgs.User,

View File

@@ -7,6 +7,7 @@ using Content.Server.GameObjects.EntitySystems;
using Content.Server.Interfaces;
using Content.Shared.Chemistry;
using Content.Shared.GameObjects;
using Content.Shared.Maths;
using Robust.Server.GameObjects.EntitySystems;
using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
@@ -205,7 +206,7 @@ namespace Content.Server.GameObjects.Components.Chemistry
//Check the solution for every reaction
foreach (var reaction in _reactions)
{
if (SolutionValidReaction(reaction, out int unitReactions))
if (SolutionValidReaction(reaction, out var unitReactions))
{
PerformReaction(reaction, unitReactions);
checkForNewReaction = true;
@@ -223,11 +224,13 @@ namespace Content.Server.GameObjects.Components.Chemistry
}
}
public bool TryAddReagent(string reagentId, int quantity, out int acceptedQuantity, bool skipReactionCheck = false, bool skipColor = false)
public bool TryAddReagent(string reagentId, decimal quantity, out decimal acceptedQuantity, bool skipReactionCheck = false, bool skipColor = false)
{
if (quantity > _maxVolume - _containedSolution.TotalVolume)
quantity = quantity.RoundForReagents();
var toAcceptQuantity = (_maxVolume - _containedSolution.TotalVolume).RoundForReagents();
if (quantity > toAcceptQuantity)
{
acceptedQuantity = _maxVolume - _containedSolution.TotalVolume;
acceptedQuantity = toAcceptQuantity;
if (acceptedQuantity == 0) return false;
}
else
@@ -267,16 +270,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 int unitReactions)
private bool SolutionValidReaction(ReactionPrototype reaction, out decimal unitReactions)
{
unitReactions = int.MaxValue; //Set to some impossibly large number initially
unitReactions = decimal.MaxValue; //Set to some impossibly large number initially
foreach (var reactant in reaction.Reactants)
{
if (!ContainsReagent(reactant.Key, out int reagentQuantity))
if (!ContainsReagent(reactant.Key, out decimal reagentQuantity))
{
return false;
}
int currentUnitReactions = reagentQuantity / reactant.Value.Amount;
var currentUnitReactions = (reagentQuantity / reactant.Value.Amount).RoundForReagents();
if (currentUnitReactions < unitReactions)
{
unitReactions = currentUnitReactions;
@@ -299,21 +302,21 @@ 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, int unitReactions)
private void PerformReaction(ReactionPrototype reaction, decimal unitReactions)
{
//Remove non-catalysts
foreach (var reactant in reaction.Reactants)
{
if (!reactant.Value.Catalyst)
{
int amountToRemove = unitReactions * reactant.Value.Amount;
var amountToRemove = (unitReactions * reactant.Value.Amount).RoundForReagents();
TryRemoveReagent(reactant.Key, amountToRemove);
}
}
//Add products
foreach (var product in reaction.Products)
{
TryAddReagent(product.Key, (int)(unitReactions * product.Value), out int acceptedQuantity, true);
TryAddReagent(product.Key, (int)(unitReactions * product.Value), out var acceptedQuantity, true);
}
//Trigger reaction effects
foreach (var effect in reaction.Effects)

View File

@@ -39,7 +39,7 @@ namespace Content.Server.GameObjects.Components.Metabolism
/// <summary>
/// Empty volume of internal solution
/// </summary>
public int EmptyVolume => _internalSolution.EmptyVolume;
public decimal EmptyVolume => _internalSolution.EmptyVolume;
public override void ExposeData(ObjectSerializer serializer)
{
@@ -98,7 +98,7 @@ namespace Content.Server.GameObjects.Components.Metabolism
//Run metabolism code for each reagent
foreach (var metabolizable in proto.Metabolism)
{
int reagentDelta = metabolizable.Metabolize(Owner, reagent.ReagentId, tickTime);
var reagentDelta = metabolizable.Metabolize(Owner, reagent.ReagentId, tickTime);
_internalSolution.TryRemoveReagent(reagent.ReagentId, reagentDelta);
}
}

View File

@@ -5,6 +5,7 @@ using Content.Server.GameObjects.EntitySystems;
using Content.Shared.Chemistry;
using Content.Shared.GameObjects.Components.Nutrition;
using Content.Shared.Interfaces;
using Content.Shared.Maths;
using Robust.Server.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
@@ -32,11 +33,11 @@ namespace Content.Server.GameObjects.Components.Nutrition
[ViewVariables]
private string _finishPrototype;
public int TransferAmount => _transferAmount;
public decimal TransferAmount => _transferAmount;
[ViewVariables]
private int _transferAmount = 2;
private decimal _transferAmount = 2;
public int MaxVolume
public decimal MaxVolume
{
get => _contents.MaxVolume;
set => _contents.MaxVolume = value;
@@ -56,7 +57,7 @@ namespace Content.Server.GameObjects.Components.Nutrition
{
return 0;
}
return Math.Max(1, _contents.CurrentVolume / _transferAmount);
return Math.Max(1, (int)Math.Ceiling(_contents.CurrentVolume / _transferAmount));
}

View File

@@ -99,7 +99,7 @@ namespace Content.Server.GameObjects.Components.Nutrition
{
return 0;
}
return Math.Max(1, _contents.CurrentVolume / _transferAmount);
return Math.Max(1, (int)Math.Ceiling(_contents.CurrentVolume / _transferAmount));
}
bool IUse.UseEntity(UseEntityEventArgs eventArgs)

View File

@@ -27,7 +27,7 @@ namespace Content.Server.GameObjects.Components.Nutrition
/// <summary>
/// Max volume of internal solution storage
/// </summary>
public int MaxVolume
public decimal MaxVolume
{
get => _stomachContents.MaxVolume;
set => _stomachContents.MaxVolume = value;
@@ -141,10 +141,10 @@ namespace Content.Server.GameObjects.Components.Nutrition
private class ReagentDelta
{
public readonly string ReagentId;
public readonly int Quantity;
public readonly decimal Quantity;
public float Lifetime { get; private set; }
public ReagentDelta(string reagentId, int quantity)
public ReagentDelta(string reagentId, decimal quantity)
{
ReagentId = reagentId;
Quantity = quantity;