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.
This commit is contained in:
Kevin Zheng
2023-12-23 20:56:39 -08:00
committed by GitHub
parent 30ff5fde69
commit ecf65fbacc

View 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);
}
}