Files
Bubberstation/code/datums
SkyratBot d66b98b984 [MIRROR] Fix issue where Plumbing Reaction Chambers can get stuck filling (#5820)
* Fix issue where Plumbing Reaction Chambers can get stuck filling (#59131)

About The Pull Request

This Pull Requests aims to fix the issue #58993 by changing two parts of the logic I've seen the chambers get stuck on.

    Chamber gets stuck requesting a unit that is always rounded down to 0
    Chamber gets stuck requesting an insanely small number that gets eaten by float math

Part 1 Explanation
Take the example where a chamber is trying to request one unit of chemical from three synthesizers. A chamber will divide it's request amongst all suppliers who can satisfy it. In this case, 1 / 3 becomes asking each synthesizer for 0.33 (due to rounding). After one update, the chamber has 0.99 of the chemical, not 1. On the second update, it then requires 0.01 of the chemical and asks each chamber for 0.0033, which gets rounded down to 0. This means the chamber NEVER fills as it spends every update cycle doing the same logic and trying to transfer in parts of 0 in size.

This has been fixed by changing it from flat dividing the amount required by the number of suppliers to a more dynamic approach that looks at the target volume and how many requests it needs to make. This mean that instead of asking for 0.33 three times in the above example, it actually works out more to asking for 0.33 then 0.34 then 0.33. Meaning it gets the whole 1 it wanted in the first update, fixing the issue.

Part 2 Explanation
Even with the above fix, when working with the right numbers, floats do not add as expected. Take the above example. I lied. 1/3 as a float is NOT 0.33. 0.33 does not exist as a float, so the actual closest value is 0.32999998, which is what the code will use, even when rounding to 0.01. What this causes is, in some scenarios, chambers getting incredibly close to their target volume but never being able to actually reach it because currentVolume + missingAmount comes out as just currentVolume, due to the insanely small float that it's missing having no impact on the larger float when added together. Again, this is due to how floats work.

So to avoid a chamber getting stuck on 98.9999999998 when it needs 99, I'm adding the CHEMICAL_QUANTISATION_LEVEL constant (used elsewhere for similar issues) to the chamber's volume when checking if it has enough. This way, the chamber will exit the filling mode even though it was short by a tiny fraction. These discrepancies seem to get handled anyway in the actual reaction code so I haven't seen any changes/problems to my outputs. For all intents and purposes, 98.9999999998 is 99 in float arithmetic when rounding as we do.
Why It's Good For The Game

Fixes an incredibly annoying issue that plagues chemistry automation. Machines, in many scenarios, currently get stuck when they shouldn't. This means a chemist has to actively keep monitoring all their machines and then do some investigation when suddenly something stops. Eventually finding the problem chamber that is stuck on "Filling" and then plungering it. Not all Chemists know of this either and just assume it's something they have done or that it's just broken and unaware how to fix it.

Now a Chemist can move on to automating more or helping elsewhere rather than babysitting their setups.
Changelog

cl
fix: fixed issue where plumbing Reaction Chambers get stuck on "Filling"
/cl

* Fix issue where Plumbing Reaction Chambers can get stuck filling

Co-authored-by: mGuv <mguv.dev@gmail.com>
2021-05-20 22:56:45 +01:00
..
2021-04-27 17:13:22 +01:00
2021-04-28 15:27:35 +01:00
2021-03-23 00:16:26 +00:00