Generalize ReagentUnit into FixedPoint2 and use it for damage calculations (#5151)

* Damage units

* sum ext method
This commit is contained in:
mirrorcult
2021-11-03 16:48:03 -07:00
committed by GitHub
parent 8165d8f38c
commit 3ab4a30a0f
100 changed files with 730 additions and 601 deletions

View File

@@ -1,47 +1,48 @@
using System;
using Content.Shared.Chemistry;
using Content.Shared.Chemistry.Reagent;
using Content.Shared.FixedPoint;
using NUnit.Framework;
namespace Content.Tests.Shared.Chemistry
{
[TestFixture, TestOf(typeof(ReagentUnit))]
public class ReagentUnit_Tests
[TestFixture, TestOf(typeof(FixedPoint2))]
public class FixedPoint2_Tests
{
[Test]
[TestCase(1, "1")]
[TestCase(0, "0")]
[TestCase(-1, "-1")]
public void ReagentUnitIntegerTests(int value, string expected)
public void FixedPoint2IntegerTests(int value, string expected)
{
var result = ReagentUnit.New(value);
var result = FixedPoint2.New(value);
Assert.That($"{result}", Is.EqualTo(expected));
}
[Test]
[TestCase(1.001f, "1")]
[TestCase(0.999f, "1")]
public void ReagentUnitFloatTests(float value, string expected)
public void FixedPoint2FloatTests(float value, string expected)
{
var result = ReagentUnit.New(value);
var result = FixedPoint2.New(value);
Assert.That($"{result}", Is.EqualTo(expected));
}
[Test]
[TestCase(1.001d, "1")]
[TestCase(0.999d, "1")]
public void ReagentUnitDoubleTests(double value, string expected)
public void FixedPoint2DoubleTests(double value, string expected)
{
var result = ReagentUnit.New(value);
var result = FixedPoint2.New(value);
Assert.That($"{result}", Is.EqualTo(expected));
}
[Test]
[TestCase("1.005", "1.01")]
[TestCase("0.999", "1")]
public void ReagentUnitStringTests(string value, string expected)
public void FixedPoint2StringTests(string value, string expected)
{
var result = ReagentUnit.New(value);
var result = FixedPoint2.New(value);
Assert.That($"{result}", Is.EqualTo(expected));
}
@@ -52,8 +53,8 @@ namespace Content.Tests.Shared.Chemistry
[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 a = FixedPoint2.New(aFloat);
var b = FixedPoint2.New(bFloat);
var result = a + b;
@@ -66,8 +67,8 @@ namespace Content.Tests.Shared.Chemistry
[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 a = FixedPoint2.New(aFloat);
var b = FixedPoint2.New(bFloat);
var result = a - b;
@@ -80,8 +81,8 @@ namespace Content.Tests.Shared.Chemistry
[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 a = FixedPoint2.New(aFloat);
var b = FixedPoint2.New(bFloat);
var result = a / b;
@@ -93,8 +94,8 @@ namespace Content.Tests.Shared.Chemistry
[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 a = FixedPoint2.New(aFloat);
var b = FixedPoint2.New(bFloat);
var result = a * b;
@@ -112,18 +113,18 @@ namespace Content.Tests.Shared.Chemistry
}
[Test]
public void ReagentUnitMin()
public void FixedPoint2Min()
{
var unorderedList = new[]
{
ReagentUnit.New(5),
ReagentUnit.New(3),
ReagentUnit.New(1),
ReagentUnit.New(2),
ReagentUnit.New(4),
FixedPoint2.New(5),
FixedPoint2.New(3),
FixedPoint2.New(1),
FixedPoint2.New(2),
FixedPoint2.New(4),
};
var min = ReagentUnit.Min(unorderedList);
Assert.That(min, Is.EqualTo(ReagentUnit.New(1)));
var min = FixedPoint2.Min(unorderedList);
Assert.That(min, Is.EqualTo(FixedPoint2.New(1)));
}
[Test]
@@ -133,10 +134,10 @@ namespace Content.Tests.Shared.Chemistry
[TestCase(1, 1, true)]
[TestCase(0, 1, false)]
[TestCase(-1, 1, false)]
public void ReagentUnitEquals(int a, int b, bool expected)
public void FixedPoint2Equals(int a, int b, bool expected)
{
var parameter = ReagentUnit.New(a);
var comparison = ReagentUnit.New(b);
var parameter = FixedPoint2.New(a);
var comparison = FixedPoint2.New(b);
Assert.That(parameter.Equals(comparison), Is.EqualTo(comparison.Equals(parameter)));
Assert.That(comparison.Equals(parameter), Is.EqualTo(expected));
}

View File

@@ -1,5 +1,6 @@
using Content.Shared.Chemistry.Components;
using Content.Shared.Chemistry.Reagent;
using Content.Shared.FixedPoint;
using NUnit.Framework;
namespace Content.Tests.Shared.Chemistry
@@ -11,7 +12,7 @@ namespace Content.Tests.Shared.Chemistry
public void AddReagentAndGetSolution()
{
var solution = new Solution();
solution.AddReagent("water", ReagentUnit.New(1000));
solution.AddReagent("water", FixedPoint2.New(1000));
var quantity = solution.GetReagentQuantity("water");
Assert.That(quantity.Int(), Is.EqualTo(1000));
@@ -20,7 +21,7 @@ namespace Content.Tests.Shared.Chemistry
[Test]
public void ConstructorAddReagent()
{
var solution = new Solution("water", ReagentUnit.New(1000));
var solution = new Solution("water", FixedPoint2.New(1000));
var quantity = solution.GetReagentQuantity("water");
Assert.That(quantity.Int(), Is.EqualTo(1000));
@@ -38,7 +39,7 @@ namespace Content.Tests.Shared.Chemistry
[Test]
public void AddLessThanZeroReagentReturnsZero()
{
var solution = new Solution("water", ReagentUnit.New(-1000));
var solution = new Solution("water", FixedPoint2.New(-1000));
var quantity = solution.GetReagentQuantity("water");
Assert.That(quantity.Int(), Is.EqualTo(0));
@@ -48,8 +49,8 @@ namespace Content.Tests.Shared.Chemistry
public void AddingReagentsSumsProperly()
{
var solution = new Solution();
solution.AddReagent("water", ReagentUnit.New(1000));
solution.AddReagent("water", ReagentUnit.New(2000));
solution.AddReagent("water", FixedPoint2.New(1000));
solution.AddReagent("water", FixedPoint2.New(2000));
var quantity = solution.GetReagentQuantity("water");
Assert.That(quantity.Int(), Is.EqualTo(3000));
@@ -59,8 +60,8 @@ namespace Content.Tests.Shared.Chemistry
public void ReagentQuantitiesStayUnique()
{
var solution = new Solution();
solution.AddReagent("water", ReagentUnit.New(1000));
solution.AddReagent("fire", ReagentUnit.New(2000));
solution.AddReagent("water", FixedPoint2.New(1000));
solution.AddReagent("fire", FixedPoint2.New(2000));
Assert.That(solution.GetReagentQuantity("water").Int(), Is.EqualTo(1000));
Assert.That(solution.GetReagentQuantity("fire").Int(), Is.EqualTo(2000));
@@ -70,8 +71,8 @@ namespace Content.Tests.Shared.Chemistry
public void TotalVolumeIsCorrect()
{
var solution = new Solution();
solution.AddReagent("water", ReagentUnit.New(1000));
solution.AddReagent("fire", ReagentUnit.New(2000));
solution.AddReagent("water", FixedPoint2.New(1000));
solution.AddReagent("fire", FixedPoint2.New(2000));
Assert.That(solution.TotalVolume.Int(), Is.EqualTo(3000));
}
@@ -80,8 +81,8 @@ namespace Content.Tests.Shared.Chemistry
public void CloningSolutionIsCorrect()
{
var solution = new Solution();
solution.AddReagent("water", ReagentUnit.New(1000));
solution.AddReagent("fire", ReagentUnit.New(2000));
solution.AddReagent("water", FixedPoint2.New(1000));
solution.AddReagent("fire", FixedPoint2.New(2000));
var newSolution = solution.Clone();
@@ -94,10 +95,10 @@ namespace Content.Tests.Shared.Chemistry
public void RemoveSolutionRecalculatesProperly()
{
var solution = new Solution();
solution.AddReagent("water", ReagentUnit.New(1000));
solution.AddReagent("fire", ReagentUnit.New(2000));
solution.AddReagent("water", FixedPoint2.New(1000));
solution.AddReagent("fire", FixedPoint2.New(2000));
solution.RemoveReagent("water", ReagentUnit.New(500));
solution.RemoveReagent("water", FixedPoint2.New(500));
Assert.That(solution.GetReagentQuantity("water").Int(), Is.EqualTo(500));
Assert.That(solution.GetReagentQuantity("fire").Int(), Is.EqualTo(2000));
@@ -107,9 +108,9 @@ namespace Content.Tests.Shared.Chemistry
[Test]
public void RemoveLessThanOneQuantityDoesNothing()
{
var solution = new Solution("water", ReagentUnit.New(100));
var solution = new Solution("water", FixedPoint2.New(100));
solution.RemoveReagent("water", ReagentUnit.New(-100));
solution.RemoveReagent("water", FixedPoint2.New(-100));
Assert.That(solution.GetReagentQuantity("water").Int(), Is.EqualTo(100));
Assert.That(solution.TotalVolume.Int(), Is.EqualTo(100));
@@ -118,9 +119,9 @@ namespace Content.Tests.Shared.Chemistry
[Test]
public void RemoveMoreThanTotalRemovesAllReagent()
{
var solution = new Solution("water", ReagentUnit.New(100));
var solution = new Solution("water", FixedPoint2.New(100));
solution.RemoveReagent("water", ReagentUnit.New(1000));
solution.RemoveReagent("water", FixedPoint2.New(1000));
Assert.That(solution.GetReagentQuantity("water").Int(), Is.EqualTo(0));
Assert.That(solution.TotalVolume.Int(), Is.EqualTo(0));
@@ -129,9 +130,9 @@ namespace Content.Tests.Shared.Chemistry
[Test]
public void RemoveNonExistReagentDoesNothing()
{
var solution = new Solution("water", ReagentUnit.New(100));
var solution = new Solution("water", FixedPoint2.New(100));
solution.RemoveReagent("fire", ReagentUnit.New(1000));
solution.RemoveReagent("fire", FixedPoint2.New(1000));
Assert.That(solution.GetReagentQuantity("water").Int(), Is.EqualTo(100));
Assert.That(solution.TotalVolume.Int(), Is.EqualTo(100));
@@ -140,9 +141,9 @@ namespace Content.Tests.Shared.Chemistry
[Test]
public void RemoveSolution()
{
var solution = new Solution("water", ReagentUnit.New(700));
var solution = new Solution("water", FixedPoint2.New(700));
solution.RemoveSolution(ReagentUnit.New(500));
solution.RemoveSolution(FixedPoint2.New(500));
//Check that edited solution is correct
Assert.That(solution.GetReagentQuantity("water").Int(), Is.EqualTo(200));
@@ -152,9 +153,9 @@ namespace Content.Tests.Shared.Chemistry
[Test]
public void RemoveSolutionMoreThanTotalRemovesAll()
{
var solution = new Solution("water", ReagentUnit.New(800));
var solution = new Solution("water", FixedPoint2.New(800));
solution.RemoveSolution(ReagentUnit.New(1000));
solution.RemoveSolution(FixedPoint2.New(1000));
//Check that edited solution is correct
Assert.That(solution.GetReagentQuantity("water").Int(), Is.EqualTo(0));
@@ -165,10 +166,10 @@ namespace Content.Tests.Shared.Chemistry
public void RemoveSolutionRatioPreserved()
{
var solution = new Solution();
solution.AddReagent("water", ReagentUnit.New(1000));
solution.AddReagent("fire", ReagentUnit.New(2000));
solution.AddReagent("water", FixedPoint2.New(1000));
solution.AddReagent("fire", FixedPoint2.New(2000));
solution.RemoveSolution(ReagentUnit.New(1500));
solution.RemoveSolution(FixedPoint2.New(1500));
Assert.That(solution.GetReagentQuantity("water").Int(), Is.EqualTo(500));
Assert.That(solution.GetReagentQuantity("fire").Int(), Is.EqualTo(1000));
@@ -178,9 +179,9 @@ namespace Content.Tests.Shared.Chemistry
[Test]
public void RemoveSolutionLessThanOneDoesNothing()
{
var solution = new Solution("water", ReagentUnit.New(800));
var solution = new Solution("water", FixedPoint2.New(800));
solution.RemoveSolution(ReagentUnit.New(-200));
solution.RemoveSolution(FixedPoint2.New(-200));
Assert.That(solution.GetReagentQuantity("water").Int(), Is.EqualTo(800));
Assert.That(solution.TotalVolume.Int(), Is.EqualTo(800));
@@ -190,10 +191,10 @@ namespace Content.Tests.Shared.Chemistry
public void SplitSolution()
{
var solution = new Solution();
solution.AddReagent("water", ReagentUnit.New(1000));
solution.AddReagent("fire", ReagentUnit.New(2000));
solution.AddReagent("water", FixedPoint2.New(1000));
solution.AddReagent("fire", FixedPoint2.New(2000));
var splitSolution = solution.SplitSolution(ReagentUnit.New(750));
var splitSolution = solution.SplitSolution(FixedPoint2.New(750));
Assert.That(solution.GetReagentQuantity("water").Int(), Is.EqualTo(750));
Assert.That(solution.GetReagentQuantity("fire").Int(), Is.EqualTo(1500));
@@ -208,10 +209,10 @@ namespace Content.Tests.Shared.Chemistry
public void SplitSolutionFractional()
{
var solution = new Solution();
solution.AddReagent("water", ReagentUnit.New(1));
solution.AddReagent("fire", ReagentUnit.New(2));
solution.AddReagent("water", FixedPoint2.New(1));
solution.AddReagent("fire", FixedPoint2.New(2));
var splitSolution = solution.SplitSolution(ReagentUnit.New(1));
var splitSolution = solution.SplitSolution(FixedPoint2.New(1));
Assert.That(solution.GetReagentQuantity("water").Float(), Is.EqualTo(0.67f));
Assert.That(solution.GetReagentQuantity("fire").Float(), Is.EqualTo(1.33f));
@@ -226,10 +227,10 @@ namespace Content.Tests.Shared.Chemistry
public void SplitSolutionFractionalOpposite()
{
var solution = new Solution();
solution.AddReagent("water", ReagentUnit.New(1));
solution.AddReagent("fire", ReagentUnit.New(2));
solution.AddReagent("water", FixedPoint2.New(1));
solution.AddReagent("fire", FixedPoint2.New(2));
var splitSolution = solution.SplitSolution(ReagentUnit.New(2));
var splitSolution = solution.SplitSolution(FixedPoint2.New(2));
Assert.That(solution.GetReagentQuantity("water").Float(), Is.EqualTo(0.33f));
Assert.That(solution.GetReagentQuantity("fire").Float(), Is.EqualTo(0.67f));
@@ -246,9 +247,9 @@ namespace Content.Tests.Shared.Chemistry
public void SplitSolutionTinyFractionalBigSmall(float initial, float reduce, float remainder)
{
var solution = new Solution();
solution.AddReagent("water", ReagentUnit.New(initial));
solution.AddReagent("water", FixedPoint2.New(initial));
var splitSolution = solution.SplitSolution(ReagentUnit.New(reduce));
var splitSolution = solution.SplitSolution(FixedPoint2.New(reduce));
Assert.That(solution.GetReagentQuantity("water").Float(), Is.EqualTo(remainder));
Assert.That(solution.TotalVolume.Float(), Is.EqualTo(remainder));
@@ -265,11 +266,11 @@ namespace Content.Tests.Shared.Chemistry
public void SplitRounding(int amount)
{
var solutionOne = new Solution();
solutionOne.AddReagent("foo", ReagentUnit.New(amount));
solutionOne.AddReagent("bar", ReagentUnit.New(amount));
solutionOne.AddReagent("baz", ReagentUnit.New(amount));
solutionOne.AddReagent("foo", FixedPoint2.New(amount));
solutionOne.AddReagent("bar", FixedPoint2.New(amount));
solutionOne.AddReagent("baz", FixedPoint2.New(amount));
var splitAmount = ReagentUnit.New(5);
var splitAmount = FixedPoint2.New(5);
var split = solutionOne.SplitSolution(splitAmount);
Assert.That(split.TotalVolume, Is.EqualTo(splitAmount));
@@ -278,9 +279,9 @@ namespace Content.Tests.Shared.Chemistry
[Test]
public void SplitSolutionMoreThanTotalRemovesAll()
{
var solution = new Solution("water", ReagentUnit.New(800));
var solution = new Solution("water", FixedPoint2.New(800));
var splitSolution = solution.SplitSolution(ReagentUnit.New(1000));
var splitSolution = solution.SplitSolution(FixedPoint2.New(1000));
Assert.That(solution.GetReagentQuantity("water").Int(), Is.EqualTo(0));
Assert.That(solution.TotalVolume.Int(), Is.EqualTo(0));
@@ -292,9 +293,9 @@ namespace Content.Tests.Shared.Chemistry
[Test]
public void SplitSolutionLessThanOneDoesNothing()
{
var solution = new Solution("water", ReagentUnit.New(800));
var solution = new Solution("water", FixedPoint2.New(800));
var splitSolution = solution.SplitSolution(ReagentUnit.New(-200));
var splitSolution = solution.SplitSolution(FixedPoint2.New(-200));
Assert.That(solution.GetReagentQuantity("water").Int(), Is.EqualTo(800));
Assert.That(solution.TotalVolume.Int(), Is.EqualTo(800));
@@ -307,33 +308,33 @@ namespace Content.Tests.Shared.Chemistry
public void SplitSolutionZero()
{
var solution = new Solution();
solution.AddReagent("Impedrezene", ReagentUnit.New(0.01 + 0.19));
solution.AddReagent("Thermite", ReagentUnit.New(0.01 + 0.39));
solution.AddReagent("Li", ReagentUnit.New(0.01 + 0.17));
solution.AddReagent("F", ReagentUnit.New(0.01 + 0.17));
solution.AddReagent("Na", ReagentUnit.New(0 + 0.13));
solution.AddReagent("Hg", ReagentUnit.New(0.15 + 4.15));
solution.AddReagent("Cu", ReagentUnit.New(0 + 0.13));
solution.AddReagent("U", ReagentUnit.New(0.76 + 20.77));
solution.AddReagent("Fe", ReagentUnit.New(0.01 + 0.36));
solution.AddReagent("SpaceDrugs", ReagentUnit.New(0.02 + 0.41));
solution.AddReagent("Al", ReagentUnit.New(0));
solution.AddReagent("Glucose", ReagentUnit.New(0));
solution.AddReagent("O", ReagentUnit.New(0));
solution.AddReagent("Impedrezene", FixedPoint2.New(0.01 + 0.19));
solution.AddReagent("Thermite", FixedPoint2.New(0.01 + 0.39));
solution.AddReagent("Li", FixedPoint2.New(0.01 + 0.17));
solution.AddReagent("F", FixedPoint2.New(0.01 + 0.17));
solution.AddReagent("Na", FixedPoint2.New(0 + 0.13));
solution.AddReagent("Hg", FixedPoint2.New(0.15 + 4.15));
solution.AddReagent("Cu", FixedPoint2.New(0 + 0.13));
solution.AddReagent("U", FixedPoint2.New(0.76 + 20.77));
solution.AddReagent("Fe", FixedPoint2.New(0.01 + 0.36));
solution.AddReagent("SpaceDrugs", FixedPoint2.New(0.02 + 0.41));
solution.AddReagent("Al", FixedPoint2.New(0));
solution.AddReagent("Glucose", FixedPoint2.New(0));
solution.AddReagent("O", FixedPoint2.New(0));
solution.SplitSolution(ReagentUnit.New(0.98));
solution.SplitSolution(FixedPoint2.New(0.98));
}
[Test]
public void AddSolution()
{
var solutionOne = new Solution();
solutionOne.AddReagent("water", ReagentUnit.New(1000));
solutionOne.AddReagent("fire", ReagentUnit.New(2000));
solutionOne.AddReagent("water", FixedPoint2.New(1000));
solutionOne.AddReagent("fire", FixedPoint2.New(2000));
var solutionTwo = new Solution();
solutionTwo.AddReagent("water", ReagentUnit.New(500));
solutionTwo.AddReagent("earth", ReagentUnit.New(1000));
solutionTwo.AddReagent("water", FixedPoint2.New(500));
solutionTwo.AddReagent("earth", FixedPoint2.New(1000));
solutionOne.AddSolution(solutionTwo);

View File

@@ -5,6 +5,7 @@ using Robust.Shared.IoC;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization.Manager;
using System.Collections.Generic;
using Content.Shared.FixedPoint;
namespace Content.Tests.Shared
{
@@ -21,7 +22,7 @@ namespace Content.Tests.Shared
// "missing" blunt entry
{ "Piercing", -2 },// Turn Piercing into Healing
{ "Slash", 3 },
{ "Radiation", 1.06f }, // Small change, paired with fractional reduction
{ "Radiation", 1.5f },
};
static private Dictionary<string, float> _resistanceReductionDict = new()
@@ -59,75 +60,75 @@ namespace Content.Tests.Shared
DamageSpecifier damageSpec = new(_damageSpec);
// Check that it properly split up the groups into types
int damage;
Assert.That(damageSpec.Total, Is.EqualTo(8));
FixedPoint2 damage;
Assert.That(damageSpec.Total, Is.EqualTo(FixedPoint2.New(8)));
Assert.That(damageSpec.DamageDict.TryGetValue("Blunt", out damage));
Assert.That(damage, Is.EqualTo(2));
Assert.That(damage, Is.EqualTo(FixedPoint2.New(2)));
Assert.That(damageSpec.DamageDict.TryGetValue("Piercing", out damage));
Assert.That(damage, Is.EqualTo(2));
Assert.That(damage, Is.EqualTo(FixedPoint2.New(2)));
Assert.That(damageSpec.DamageDict.TryGetValue("Slash", out damage));
Assert.That(damage, Is.EqualTo(1));
Assert.That(damage, Is.EqualTo(FixedPoint2.New(1)));
Assert.That(damageSpec.DamageDict.TryGetValue("Radiation", out damage));
Assert.That(damage, Is.EqualTo(3));
Assert.That(damage, Is.EqualTo(FixedPoint2.New(3)));
// check that integer multiplication works
damageSpec = damageSpec * 2;
Assert.That(damageSpec.Total, Is.EqualTo(16));
Assert.That(damageSpec.Total, Is.EqualTo(FixedPoint2.New(16)));
Assert.That(damageSpec.DamageDict.TryGetValue("Blunt", out damage));
Assert.That(damage, Is.EqualTo(4));
Assert.That(damage, Is.EqualTo(FixedPoint2.New(4)));
Assert.That(damageSpec.DamageDict.TryGetValue("Piercing", out damage));
Assert.That(damage, Is.EqualTo(4));
Assert.That(damage, Is.EqualTo(FixedPoint2.New(4)));
Assert.That(damageSpec.DamageDict.TryGetValue("Slash", out damage));
Assert.That(damage, Is.EqualTo(2));
Assert.That(damage, Is.EqualTo(FixedPoint2.New(2)));
Assert.That(damageSpec.DamageDict.TryGetValue("Radiation", out damage));
Assert.That(damage, Is.EqualTo(6));
Assert.That(damage, Is.EqualTo(FixedPoint2.New(6)));
// check that float multiplication works
damageSpec = damageSpec * 2.2f;
Assert.That(damageSpec.DamageDict.TryGetValue("Blunt", out damage));
Assert.That(damage, Is.EqualTo(9));
Assert.That(damage, Is.EqualTo(FixedPoint2.New(8.8)));
Assert.That(damageSpec.DamageDict.TryGetValue("Piercing", out damage));
Assert.That(damage, Is.EqualTo(9));
Assert.That(damage, Is.EqualTo(FixedPoint2.New(8.8)));
Assert.That(damageSpec.DamageDict.TryGetValue("Slash", out damage));
Assert.That(damage, Is.EqualTo(4));
Assert.That(damage, Is.EqualTo(FixedPoint2.New(4.4)));
Assert.That(damageSpec.DamageDict.TryGetValue("Radiation", out damage));
Assert.That(damage, Is.EqualTo(13));
Assert.That(damageSpec.Total, Is.EqualTo(9 + 9 + 4 + 13));
Assert.That(damage, Is.EqualTo(FixedPoint2.New(13.2)));
Assert.That(damageSpec.Total, Is.EqualTo(FixedPoint2.New(8.8 + 8.8 + 4.4 + 13.2)));
// check that integer division works
damageSpec = damageSpec / 2;
Assert.That(damageSpec.DamageDict.TryGetValue("Blunt", out damage));
Assert.That(damage, Is.EqualTo(5));
Assert.That(damage, Is.EqualTo(FixedPoint2.New(4.4)));
Assert.That(damageSpec.DamageDict.TryGetValue("Piercing", out damage));
Assert.That(damage, Is.EqualTo(5));
Assert.That(damage, Is.EqualTo(FixedPoint2.New(4.4)));
Assert.That(damageSpec.DamageDict.TryGetValue("Slash", out damage));
Assert.That(damage, Is.EqualTo(2));
Assert.That(damage, Is.EqualTo(FixedPoint2.New(2.2)));
Assert.That(damageSpec.DamageDict.TryGetValue("Radiation", out damage));
Assert.That(damage, Is.EqualTo(7));
Assert.That(damage, Is.EqualTo(FixedPoint2.New(6.6)));
// check that float division works
damageSpec = damageSpec / 2.4f;
damageSpec = damageSpec / 2.2f;
Assert.That(damageSpec.DamageDict.TryGetValue("Blunt", out damage));
Assert.That(damage, Is.EqualTo(2));
Assert.That(damage, Is.EqualTo(FixedPoint2.New(2)));
Assert.That(damageSpec.DamageDict.TryGetValue("Piercing", out damage));
Assert.That(damage, Is.EqualTo(2));
Assert.That(damage, Is.EqualTo(FixedPoint2.New(2)));
Assert.That(damageSpec.DamageDict.TryGetValue("Slash", out damage));
Assert.That(damage, Is.EqualTo(1));
Assert.That(damage, Is.EqualTo(FixedPoint2.New(1)));
Assert.That(damageSpec.DamageDict.TryGetValue("Radiation", out damage));
Assert.That(damage, Is.EqualTo(3));
Assert.That(damage, Is.EqualTo(FixedPoint2.New(3)));
// Lets also test the constructor with damage types and damage groups works properly.
damageSpec = new(_prototypeManager.Index<DamageGroupPrototype>("Brute"), 4);
Assert.That(damageSpec.DamageDict.TryGetValue("Blunt", out damage));
Assert.That(damage, Is.EqualTo(1));
Assert.That(damage, Is.EqualTo(FixedPoint2.New(1.33)));
Assert.That(damageSpec.DamageDict.TryGetValue("Piercing", out damage));
Assert.That(damage, Is.EqualTo(2)); // integer rounding. Piercing is defined as last group member in yaml.
Assert.That(damage, Is.EqualTo(FixedPoint2.New(1.33)));
Assert.That(damageSpec.DamageDict.TryGetValue("Slash", out damage));
Assert.That(damage, Is.EqualTo(1));
Assert.That(damage, Is.EqualTo(FixedPoint2.New(1.34))); // doesn't divide evenly, so the 0.01 goes to the last one
damageSpec = new(_prototypeManager.Index<DamageTypePrototype>("Piercing"), 4);
Assert.That(damageSpec.DamageDict.TryGetValue("Piercing", out damage));
Assert.That(damage, Is.EqualTo(4));
Assert.That(damage, Is.EqualTo(FixedPoint2.New(4)));
}
//Check that DamageSpecifier will be properly adjusted by a resistance set
@@ -146,21 +147,21 @@ namespace Content.Tests.Shared
//damage is initially 20 / 20 / 10 / 30
//Each time we subtract -5 / 0 / 8 / 0.5
//then multiply by 1 / -2 / 3 / 1.06
//then multiply by 1 / -2 / 3 / 1.5
// Apply once
damageSpec = DamageSpecifier.ApplyModifierSet(damageSpec, modifierSet);
Assert.That(damageSpec.DamageDict["Blunt"], Is.EqualTo(25));
Assert.That(damageSpec.DamageDict["Piercing"], Is.EqualTo(-40)); // became healing
Assert.That(damageSpec.DamageDict["Slash"], Is.EqualTo(6));
Assert.That(damageSpec.DamageDict["Radiation"], Is.EqualTo(31)); // would be 32 w/o fraction adjustment
Assert.That(damageSpec.DamageDict["Blunt"], Is.EqualTo(FixedPoint2.New(25)));
Assert.That(damageSpec.DamageDict["Piercing"], Is.EqualTo(FixedPoint2.New(-40))); // became healing
Assert.That(damageSpec.DamageDict["Slash"], Is.EqualTo(FixedPoint2.New(6)));
Assert.That(damageSpec.DamageDict["Radiation"], Is.EqualTo(FixedPoint2.New(44.25)));
// And again, checking for some other behavior
damageSpec = DamageSpecifier.ApplyModifierSet(damageSpec, modifierSet);
Assert.That(damageSpec.DamageDict["Blunt"], Is.EqualTo(30));
Assert.That(damageSpec.DamageDict["Piercing"], Is.EqualTo(-40)); // resistances don't apply to healing
Assert.That(damageSpec.DamageDict["Blunt"], Is.EqualTo(FixedPoint2.New(30)));
Assert.That(damageSpec.DamageDict["Piercing"], Is.EqualTo(FixedPoint2.New(-40))); // resistances don't apply to healing
Assert.That(!damageSpec.DamageDict.ContainsKey("Slash")); // Reduction reduced to 0, and removed from specifier
Assert.That(damageSpec.DamageDict["Radiation"], Is.EqualTo(32));
Assert.That(damageSpec.DamageDict["Radiation"], Is.EqualTo(FixedPoint2.New(65.63)));
}
// Default damage Yaml