Files
wwdpublic/Content.Shared/Atmos/GasMixture.cs
sleepyyapril 885ee5a831 Wizmerge for Station AI (#1351)
<!--
This is a semi-strict format, you can add/remove sections as needed but
the order/format should be kept the same
Remove these comments before submitting
-->

# Description

the adding AI is now up to y'all because i'm not touching loadout code
for name datasets, but it shouldn't be too bad from here

---------

Signed-off-by: sleepyyapril <123355664+sleepyyapril@users.noreply.github.com>
Signed-off-by: SolStar <44028047+ewokswagger@users.noreply.github.com>
Signed-off-by: deltanedas <39013340+deltanedas@users.noreply.github.com>
Co-authored-by: themias <89101928+themias@users.noreply.github.com>
Co-authored-by: Verm <32827189+Vermidia@users.noreply.github.com>
Co-authored-by: DrSmugleaf <10968691+DrSmugleaf@users.noreply.github.com>
Co-authored-by: Sphiral <145869023+SphiraI@users.noreply.github.com>
Co-authored-by: Ed <96445749+TheShuEd@users.noreply.github.com>
Co-authored-by: Mr. 27 <45323883+Dutch-VanDerLinde@users.noreply.github.com>
Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com>
Co-authored-by: Alzore <140123969+Blackern5000@users.noreply.github.com>
Co-authored-by: ravage <142820619+ravage123321@users.noreply.github.com>
Co-authored-by: slarticodefast <161409025+slarticodefast@users.noreply.github.com>
Co-authored-by: Intoxicating-Innocence <188202277+Intoxicating-Innocence@users.noreply.github.com>
Co-authored-by: Saphire <lattice@saphi.re>
Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
Co-authored-by: Errant <35878406+Errant-4@users.noreply.github.com>
Co-authored-by: Tayrtahn <tayrtahn@gmail.com>
Co-authored-by: CaasGit <87243814+CaasGit@users.noreply.github.com>
Co-authored-by: BramvanZijp <56019239+BramvanZijp@users.noreply.github.com>
Co-authored-by: Boaz1111 <149967078+Boaz1111@users.noreply.github.com>
Co-authored-by: NakataRin <45946146+NakataRin@users.noreply.github.com>
Co-authored-by: Kara <lunarautomaton6@gmail.com>
Co-authored-by: Plykiya <58439124+Plykiya@users.noreply.github.com>
Co-authored-by: SlamBamActionman <slambamactionman@gmail.com>
Co-authored-by: Doomsdrayk <robotdoughnut@comcast.net>
Co-authored-by: Brandon Hu <103440971+Brandon-Huu@users.noreply.github.com>
Co-authored-by: SlamBamActionman <83650252+SlamBamActionman@users.noreply.github.com>
Co-authored-by: ElectroJr <leonsfriedrich@gmail.com>
Co-authored-by: Pieter-Jan Briers <pieterjan.briers+git@gmail.com>
Co-authored-by: DrSmugleaf <DrSmugleaf@users.noreply.github.com>
Co-authored-by: Julian Giebel <juliangiebel@live.de>
Co-authored-by: nikthechampiongr <32041239+nikthechampiongr@users.noreply.github.com>
Co-authored-by: Repo <47093363+Titian3@users.noreply.github.com>
Co-authored-by: Chief-Engineer <119664036+Chief-Engineer@users.noreply.github.com>
Co-authored-by: icekot8 <93311212+icekot8@users.noreply.github.com>
Co-authored-by: AJCM-git <60196617+AJCM-git@users.noreply.github.com>
Co-authored-by: Leon Friedrich <60421075+ElectroJr@users.noreply.github.com>
Co-authored-by: no <165581243+pissdemon@users.noreply.github.com>
Co-authored-by: Tornado Tech <54727692+Tornado-Technology@users.noreply.github.com>
Co-authored-by: osjarw <62134478+osjarw@users.noreply.github.com>
Co-authored-by: Simon <63975668+Simyon264@users.noreply.github.com>
Co-authored-by: TGRCDev <tgrc@tgrc.dev>
Co-authored-by: Milon <milonpl.git@proton.me>
Co-authored-by: deltanedas <39013340+deltanedas@users.noreply.github.com>
Co-authored-by: ShadowCommander <10494922+ShadowCommander@users.noreply.github.com>
Co-authored-by: Fildrance <fildrance@gmail.com>
Co-authored-by: pa.pecherskij <pa.pecherskij@interfax.ru>
Co-authored-by: chavonadelal <156101927+chavonadelal@users.noreply.github.com>
Co-authored-by: SolStar <44028047+ewokswagger@users.noreply.github.com>
Co-authored-by: K-Dynamic <20566341+K-Dynamic@users.noreply.github.com>
Co-authored-by: lzk <124214523+lzk228@users.noreply.github.com>
Co-authored-by: ArchRBX <5040911+ArchRBX@users.noreply.github.com>
Co-authored-by: archrbx <punk.gear5260@fastmail.com>
Co-authored-by: Radezolid <snappednexus@gmail.com>
Co-authored-by: Nemanja <98561806+EmoGarbage404@users.noreply.github.com>
Co-authored-by: EmoGarbage404 <retron404@gmail.com>
Co-authored-by: MilenVolf <63782763+MilenVolf@users.noreply.github.com>
Co-authored-by: Velcroboy <107660393+IamVelcroboy@users.noreply.github.com>
Co-authored-by: Velcroboy <velcroboy333@hotmail.com>
Co-authored-by: neuPanda <chriseparton@gmail.com>
Co-authored-by: neuPanda <spainman0@yahoo.com>
Co-authored-by: Dvir <39403717+dvir001@users.noreply.github.com>
Co-authored-by: Whatstone <whatston3@gmail.com>
Co-authored-by: VideoKompany <135313844+VlaDOS1408@users.noreply.github.com>

(cherry picked from commit 93ed70acfeda357133a701f637d3faeec02749bb)
2025-01-14 00:13:42 +03:00

307 lines
9.2 KiB
C#

using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.CompilerServices;
using Content.Shared.Atmos.EntitySystems;
using Content.Shared.Atmos.Reactions;
using Robust.Shared.Serialization;
using Robust.Shared.Utility;
namespace Content.Shared.Atmos
{
/// <summary>
/// A general-purpose, variable volume gas mixture.
/// </summary>
[Serializable]
[DataDefinition]
public sealed partial class GasMixture : IEquatable<GasMixture>, ISerializationHooks
{
public static GasMixture SpaceGas => new() {Volume = Atmospherics.CellVolume, Temperature = Atmospherics.TCMB, Immutable = true};
// No access, to ensure immutable mixtures are never accidentally mutated.
[Access(typeof(SharedAtmosphereSystem), typeof(SharedAtmosDebugOverlaySystem), Other = AccessPermissions.None)]
[DataField]
public float[] Moles = new float[Atmospherics.AdjustedNumberOfGases];
public float this[int gas] => Moles[gas];
[DataField("temperature")]
[ViewVariables(VVAccess.ReadWrite)]
private float _temperature = Atmospherics.TCMB;
[DataField("immutable")]
public bool Immutable { get; private set; }
[ViewVariables]
public readonly Dictionary<GasReaction, float> ReactionResults = new()
{
// We initialize the dictionary here.
{ GasReaction.Fire, 0f }
};
[ViewVariables]
public float TotalMoles
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => NumericsHelpers.HorizontalAdd(Moles);
}
[ViewVariables]
public float Pressure
{
get
{
if (Volume <= 0) return 0f;
return TotalMoles * Atmospherics.R * Temperature / Volume;
}
}
[ViewVariables]
public float Temperature
{
get => _temperature;
set
{
DebugTools.Assert(!float.IsNaN(value));
if (!Immutable)
_temperature = MathF.Min(MathF.Max(value, Atmospherics.TCMB), Atmospherics.Tmax);
}
}
[DataField("volume")]
[ViewVariables(VVAccess.ReadWrite)]
public float Volume { get; set; }
public GasMixture()
{
}
public GasMixture(float volume = 0f)
{
if (volume < 0)
volume = 0;
Volume = volume;
}
public GasMixture(float[] moles, float temp, float volume = Atmospherics.CellVolume)
{
if (moles.Length != Atmospherics.AdjustedNumberOfGases)
throw new InvalidOperationException($"Invalid mole array length");
if (volume < 0)
volume = 0;
DebugTools.Assert(!float.IsNaN(temp));
_temperature = temp;
Moles = moles;
Volume = volume;
}
public GasMixture(GasMixture toClone)
{
CopyFrom(toClone);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void MarkImmutable()
{
Immutable = true;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float GetMoles(int gasId)
{
return Moles[gasId];
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float GetMoles(Gas gas)
{
return GetMoles((int)gas);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetMoles(int gasId, float quantity)
{
if (!float.IsFinite(quantity) || float.IsNegative(quantity))
throw new ArgumentException($"Invalid quantity \"{quantity}\" specified!", nameof(quantity));
if (!Immutable)
Moles[gasId] = quantity;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetMoles(Gas gas, float quantity)
{
SetMoles((int)gas, quantity);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AdjustMoles(int gasId, float quantity)
{
if (Immutable)
return;
if (!float.IsFinite(quantity))
throw new ArgumentException($"Invalid quantity \"{quantity}\" specified!", nameof(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().
ref var moles = ref Moles[gasId];
moles = MathF.Max(moles + quantity, 0);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AdjustMoles(Gas gas, float moles)
{
AdjustMoles((int)gas, moles);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public GasMixture Remove(float amount)
{
return RemoveRatio(amount / TotalMoles);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public GasMixture RemoveRatio(float ratio)
{
switch (ratio)
{
case <= 0:
return new GasMixture(Volume){Temperature = Temperature};
case > 1:
ratio = 1;
break;
}
var removed = new GasMixture(Volume) { Temperature = Temperature };
Moles.CopyTo(removed.Moles.AsSpan());
NumericsHelpers.Multiply(removed.Moles, ratio);
if (!Immutable)
NumericsHelpers.Sub(Moles, removed.Moles);
for (var i = 0; i < Moles.Length; i++)
{
var moles = Moles[i];
var otherMoles = removed.Moles[i];
if ((moles < Atmospherics.GasMinMoles || float.IsNaN(moles)) && !Immutable)
Moles[i] = 0;
if (otherMoles < Atmospherics.GasMinMoles || float.IsNaN(otherMoles))
removed.Moles[i] = 0;
}
return removed;
}
public GasMixture RemoveVolume(float vol)
{
return RemoveRatio(vol / Volume);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void CopyFrom(GasMixture sample)
{
if (Immutable)
return;
Volume = sample.Volume;
sample.Moles.CopyTo(Moles, 0);
Temperature = sample.Temperature;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Clear()
{
if (Immutable) return;
Array.Clear(Moles, 0, Atmospherics.TotalNumberOfGases);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Multiply(float multiplier)
{
if (Immutable) return;
NumericsHelpers.Multiply(Moles, multiplier);
}
void ISerializationHooks.AfterDeserialization()
{
// ISerializationHooks is obsolete.
// TODO add fixed-length-array serializer
// The arrays MUST have a specific length.
Array.Resize(ref Moles, Atmospherics.AdjustedNumberOfGases);
}
public GasMixtureStringRepresentation ToPrettyString()
{
var molesPerGas = new Dictionary<string, float>();
for (int i = 0; i < Moles.Length; i++)
{
if (Moles[i] == 0)
continue;
molesPerGas.Add(((Gas) i).ToString(), Moles[i]);
}
return new GasMixtureStringRepresentation(TotalMoles, Temperature, Pressure, molesPerGas);
}
public override bool Equals(object? obj)
{
if (obj is GasMixture mix)
return Equals(mix);
return false;
}
public bool Equals(GasMixture? other)
{
if (ReferenceEquals(this, other))
return true;
if (ReferenceEquals(null, other))
return false;
return Moles.SequenceEqual(other.Moles)
&& _temperature.Equals(other._temperature)
&& ReactionResults.SequenceEqual(other.ReactionResults)
&& Immutable == other.Immutable
&& Volume.Equals(other.Volume);
}
[SuppressMessage("ReSharper", "NonReadonlyMemberInGetHashCode")]
public override int GetHashCode()
{
var hashCode = new HashCode();
for (var i = 0; i < Atmospherics.TotalNumberOfGases; i++)
{
var moles = Moles[i];
hashCode.Add(moles);
}
hashCode.Add(_temperature);
hashCode.Add(Immutable);
hashCode.Add(Volume);
return hashCode.ToHashCode();
}
public GasMixture Clone()
{
if (Immutable)
return this;
var newMixture = new GasMixture()
{
Moles = (float[])Moles.Clone(),
_temperature = _temperature,
Volume = Volume,
};
return newMixture;
}
}
}