diff --git a/Content.Server/Atmos/GasMixture.cs b/Content.Server/Atmos/GasMixture.cs index 96e8afa4c5..8035e32a65 100644 --- a/Content.Server/Atmos/GasMixture.cs +++ b/Content.Server/Atmos/GasMixture.cs @@ -120,8 +120,8 @@ namespace Content.Server.Atmos public GasMixture(AtmosphereSystem? atmosphereSystem) { _atmosphereSystem = atmosphereSystem ?? EntitySystem.Get(); - _moles = new float[MathHelper.NextMultipleOf(Atmospherics.TotalNumberOfGases, 4)]; - _molesArchived = new float[_moles.Length]; + _moles = new float[Atmospherics.AdjustedNumberOfGases]; + _molesArchived = new float[Atmospherics.AdjustedNumberOfGases]; } public GasMixture(float volume, AtmosphereSystem? atmosphereSystem = null): this(atmosphereSystem) @@ -222,6 +222,15 @@ namespace Content.Server.Atmos if (!Immutable) NumericsHelpers.Sub(_moles, removed._moles); + for (var i = 0; i < _moles.Length; i++) + { + if (_moles[i] < Atmospherics.GasMinMoles) + _moles[i] = 0; + + if (removed._moles[i] < Atmospherics.GasMinMoles) + removed._moles[i] = 0; + } + return removed; } @@ -508,19 +517,17 @@ namespace Content.Server.Atmos public void ExposeData(ObjectSerializer serializer) { - var length = MathHelper.NextMultipleOf(Atmospherics.TotalNumberOfGases, 4); - serializer.DataField(this, x => Immutable, "immutable", false); serializer.DataField(this, x => Volume, "volume", 0f); serializer.DataField(this, x => LastShare, "lastShare", 0f); serializer.DataField(this, x => TemperatureArchived, "temperatureArchived", 0f); - serializer.DataField(ref _moles, "moles", new float[length]); - serializer.DataField(ref _molesArchived, "molesArchived", new float[length]); + serializer.DataField(ref _moles, "moles", new float[Atmospherics.AdjustedNumberOfGases]); + serializer.DataField(ref _molesArchived, "molesArchived", new float[Atmospherics.AdjustedNumberOfGases]); serializer.DataField(ref _temperature, "temperature", Atmospherics.TCMB); // The arrays MUST have a specific length. - Array.Resize(ref _moles, length); - Array.Resize(ref _molesArchived, length); + Array.Resize(ref _moles, Atmospherics.AdjustedNumberOfGases); + Array.Resize(ref _molesArchived, Atmospherics.AdjustedNumberOfGases); } public override bool Equals(object? obj) diff --git a/Content.Shared/Atmos/Atmospherics.cs b/Content.Shared/Atmos/Atmospherics.cs index a3769e78bc..76a7a45056 100644 --- a/Content.Shared/Atmos/Atmospherics.cs +++ b/Content.Shared/Atmos/Atmospherics.cs @@ -1,10 +1,17 @@ -namespace Content.Shared.Atmos +using Robust.Shared.Maths; + +namespace Content.Shared.Atmos { /// /// Class to store atmos constants. /// - public class Atmospherics + public static class Atmospherics { + static Atmospherics() + { + AdjustedNumberOfGases = MathHelper.NextMultipleOf(TotalNumberOfGases, 4); + } + #region ATMOS /// /// The universal gas constant, in kPa*L/(K*mol) @@ -144,7 +151,13 @@ /// /// Total number of gases. Increase this if you want to add more! /// - public const byte TotalNumberOfGases = 6; + public const int TotalNumberOfGases = 6; + + /// + /// This is the actual length of the gases arrays in mixtures. + /// Set to the closest multiple of 4 relative to for SIMD reasons. + /// + public static readonly int AdjustedNumberOfGases; /// /// Amount of heat released per mole of burnt hydrogen or tritium (hydrogen isotope)