Solution precision fixes (#25199)

* Add test for two chemistry issues

1. rounding issue with reaction processing when making chloral hydrate
2. reliable assert trip due to the ValidateSolution() heat capacity issue.

* Fix FixedPoint2 arithmetic

Fix internal floating point arithmetic in places where it could be avoided.

Fix incorrect rounding mode used in other places (it should always floor, like regular int arithmetic).

I had to add an explicit epsilon value for float -> FixedPoint2 because something like 1.05 is actually like 1.04999 and that'd cause it to be rounded down to 1.04.

This fixes reaction reagent processing in cases where the reagent inputs can't cleanly divide. Previously, when making 30u chloral hydrate by adding the chlorine in 10u increments you'd end up with 0.04 chlorine left over. This was caused by division in the reaction code rounding up in some cases. Changing division here to always round down fixes it.

* Attempt to fix heat capacity precision assert issues.

Fixes #22126

First, we just increase the tolerance of the assert. It was way too low.

Second, actually put a cap on float drift from one-off _heatCapacity changes.

* Fix float -> FixedPoint2 epsilon for negative number, fix tests.

* Fix DamageableTest

* Oh yeah I need to call CleanReturnAsync
This commit is contained in:
Pieter-Jan Briers
2024-02-17 00:54:27 +01:00
committed by GitHub
parent c7870882f6
commit 33611b7094
6 changed files with 232 additions and 49 deletions

View File

@@ -0,0 +1,122 @@
using Content.Server.Chemistry.Containers.EntitySystems;
using Content.Shared.Chemistry.Components;
using Content.Shared.Chemistry.Reaction;
using Content.Shared.Chemistry.Reagent;
using Content.Shared.FixedPoint;
using Robust.Shared.GameObjects;
namespace Content.IntegrationTests.Tests.Chemistry;
[TestFixture]
[TestOf(typeof(ChemicalReactionSystem))]
public sealed class SolutionRoundingTest
{
// This test tests two things:
// * A rounding error in reaction code while I was making chloral hydrate
// * An assert with solution heat capacity calculations that I found a repro for while testing the above.
[TestPrototypes]
private const string Prototypes = @"
- type: entity
id: SolutionRoundingTestContainer
components:
- type: SolutionContainerManager
solutions:
beaker:
maxVol: 100
# This is the Chloral Hydrate recipe fyi.
- type: reagent
id: SolutionRoundingTestReagentA
name: reagent-name-nothing
desc: reagent-desc-nothing
physicalDesc: reagent-physical-desc-nothing
- type: reagent
id: SolutionRoundingTestReagentB
name: reagent-name-nothing
desc: reagent-desc-nothing
physicalDesc: reagent-physical-desc-nothing
- type: reagent
id: SolutionRoundingTestReagentC
name: reagent-name-nothing
desc: reagent-desc-nothing
physicalDesc: reagent-physical-desc-nothing
- type: reagent
id: SolutionRoundingTestReagentD
name: reagent-name-nothing
desc: reagent-desc-nothing
physicalDesc: reagent-physical-desc-nothing
- type: reaction
id: SolutionRoundingTestReaction
impact: Medium
reactants:
SolutionRoundingTestReagentA:
amount: 3
SolutionRoundingTestReagentB:
amount: 1
SolutionRoundingTestReagentC:
amount: 1
products:
SolutionRoundingTestReagentD: 1
";
[Test]
public async Task Test()
{
await using var pair = await PoolManager.GetServerClient();
var server = pair.Server;
var testMap = await pair.CreateTestMap();
Solution solution = default;
Entity<SolutionComponent> solutionEnt = default;
await server.WaitPost(() =>
{
var system = server.System<SolutionContainerSystem>();
var beaker = server.EntMan.SpawnEntity("SolutionRoundingTestContainer", testMap.GridCoords);
system.TryGetSolution(beaker, "beaker", out var newSolutionEnt, out var newSolution);
solutionEnt = newSolutionEnt!.Value;
solution = newSolution!;
system.TryAddSolution(solutionEnt, new Solution("SolutionRoundingTestReagentC", 50));
system.TryAddSolution(solutionEnt, new Solution("SolutionRoundingTestReagentB", 30));
for (var i = 0; i < 9; i++)
{
system.TryAddSolution(solutionEnt, new Solution("SolutionRoundingTestReagentA", 10));
}
});
await server.WaitAssertion(() =>
{
Assert.Multiple(() =>
{
Assert.That(
solution.ContainsReagent("SolutionRoundingTestReagentA", null),
Is.False,
"Solution should not contain reagent A");
Assert.That(
solution.ContainsReagent("SolutionRoundingTestReagentB", null),
Is.False,
"Solution should not contain reagent B");
Assert.That(
solution![new ReagentId("SolutionRoundingTestReagentC", null)].Quantity,
Is.EqualTo((FixedPoint2) 20));
Assert.That(
solution![new ReagentId("SolutionRoundingTestReagentD", null)].Quantity,
Is.EqualTo((FixedPoint2) 30));
});
});
await pair.CleanReturnAsync();
}
}