]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
Clamp after AdjustMoles() (#22907)
authorKevin Zheng <kevinz5000@gmail.com>
Sun, 24 Dec 2023 04:56:39 +0000 (20:56 -0800)
committerGitHub <noreply@github.com>
Sun, 24 Dec 2023 04:56:39 +0000 (23:56 -0500)
Clamping is needed because x - x can be negative with floating point
numbers. If we don't clamp here, the caller always has to call
GetMoles(), clamp, then SetMoles(), which makes this function not very
useful.

Content.Server/Atmos/GasMixture.cs

index 1547c259e61ec23bff3c4008e90a31f0bb0803cc..d49d1b78c1fc1495083c434e6c5b756b75490720 100644 (file)
@@ -120,12 +120,9 @@ namespace Content.Server.Atmos
                 if (!float.IsFinite(quantity))
                     throw new ArgumentException($"Invalid quantity \"{quantity}\" specified!", nameof(quantity));
 
-                Moles[gasId] += quantity;
-
-                var moles = Moles[gasId];
-
-                if (!float.IsFinite(moles) || float.IsNegative(moles))
-                    throw new Exception($"Invalid mole quantity \"{moles}\" in gas Id {gasId} after adjusting moles with \"{quantity}\"!");
+                // Clamping is needed because x - x can be negative with floating point numbers. If we don't
+                // clamp here, the caller always has to call GetMoles(), clamp, then SetMoles().
+                Moles[gasId] = MathF.Max(Moles[gasId] + quantity, 0);
             }
         }