From: Kevin Zheng Date: Sun, 24 Dec 2023 04:56:39 +0000 (-0800) Subject: Clamp after AdjustMoles() (#22907) X-Git-Url: https://git.smokeofanarchy.ru/gitweb.cgi?a=commitdiff_plain;h=ecf65fbaccbb4bbd9b19359eff0a37e412f19604;p=space-station-14.git Clamp after AdjustMoles() (#22907) 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. --- diff --git a/Content.Server/Atmos/GasMixture.cs b/Content.Server/Atmos/GasMixture.cs index 1547c259e6..d49d1b78c1 100644 --- a/Content.Server/Atmos/GasMixture.cs +++ b/Content.Server/Atmos/GasMixture.cs @@ -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); } }