Move GasMixture to shared (#27480)

* Moved GasMixture to shared

* Temp Fix for sandbox violation, idk why Array.Resize isn't working properly. It's already sandboxed.

* The most powerful webedit in history
This commit is contained in:
Jezithyr
2024-04-30 14:31:05 -07:00
committed by GitHub
parent b8c0372ac6
commit 6869adfa78
37 changed files with 73 additions and 20 deletions

View File

@@ -1,4 +1,6 @@
namespace Content.Server.Atmos.Components
using Content.Shared.Atmos;
namespace Content.Server.Atmos.Components
{
[RegisterComponent]
public sealed partial class GasMixtureHolderComponent : Component, IGasMixtureHolder

View File

@@ -1,3 +1,4 @@
using Content.Shared.Atmos;
using Content.Shared.Atmos.Components;
using Content.Shared.Atmos.EntitySystems;

View File

@@ -1,3 +1,4 @@
using Content.Shared.Atmos;
using Robust.Shared.Map;
namespace Content.Server.Atmos.EntitySystems

View File

@@ -1,10 +1,10 @@
using System.Linq;
using Content.Server.Atmos.Components;
using Content.Server.Atmos.Piping.Components;
using Content.Server.Atmos.Reactions;
using Content.Server.NodeContainer.NodeGroups;
using Content.Shared.Atmos;
using Content.Shared.Atmos.Components;
using Content.Shared.Atmos.Reactions;
using Robust.Shared.Map.Components;
using Robust.Shared.Utility;

View File

@@ -2,6 +2,7 @@ using System.Linq;
using System.Runtime.CompilerServices;
using Content.Server.Atmos.Reactions;
using Content.Shared.Atmos;
using Content.Shared.Atmos.Reactions;
using Robust.Shared.Prototypes;
using DependencyAttribute = Robust.Shared.IoC.DependencyAttribute;

View File

@@ -2,6 +2,7 @@ using Content.Server.Atmos.Components;
using Content.Server.Atmos.Reactions;
using Content.Shared.Atmos;
using Content.Shared.Atmos.Components;
using Content.Shared.Atmos.Reactions;
using Robust.Shared.Map;
using Robust.Shared.Map.Components;
using Robust.Shared.Utility;

View File

@@ -2,6 +2,7 @@ using Content.Server.Atmos.Components;
using Content.Server.Atmos.Reactions;
using Content.Shared.Atmos;
using Content.Shared.Atmos.Components;
using Content.Shared.Atmos.Reactions;
using Content.Shared.Audio;
using Content.Shared.Database;
using Robust.Shared.Audio;

View File

@@ -1,4 +1,5 @@
using Content.Server.Atmos.Components;
using Content.Shared.Atmos;
using Content.Shared.Atmos.Components;
using Robust.Shared.GameStates;
using Robust.Shared.Map.Components;

View File

@@ -1,299 +0,0 @@
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.CompilerServices;
using Content.Server.Atmos.Reactions;
using Content.Shared.Atmos;
using Content.Shared.Atmos.EntitySystems;
using Robust.Shared.Serialization;
using Robust.Shared.Utility;
namespace Content.Server.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;
}
[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 CopyFromMutable(GasMixture sample)
{
if (Immutable) return;
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;
}
}
}

View File

@@ -1,4 +1,6 @@
namespace Content.Server.Atmos
using Content.Shared.Atmos;
namespace Content.Server.Atmos
{
public interface IGasMixtureHolder
{

View File

@@ -1,5 +1,6 @@
using Content.Server.Atmos.EntitySystems;
using Content.Server.Atmos.Reactions;
using Content.Shared.Atmos;
using Content.Shared.Atmos.Reactions;
namespace Content.Server.Atmos
{

View File

@@ -1,3 +1,5 @@
using Content.Shared.Atmos;
namespace Content.Server.Atmos.Piping.Binary.Components
{
[RegisterComponent]

View File

@@ -13,6 +13,7 @@ using Content.Server.NodeContainer.NodeGroups;
using Content.Server.Audio;
using Content.Server.Administration.Logs;
using Content.Server.NodeContainer.EntitySystems;
using Content.Shared.Atmos;
using Content.Shared.Database;
namespace Content.Server.Atmos.Portable

View File

@@ -1,5 +1,6 @@
using Content.Server.Atmos.EntitySystems;
using Content.Shared.Atmos;
using Content.Shared.Atmos.Reactions;
using JetBrains.Annotations;
namespace Content.Server.Atmos.Reactions;

View File

@@ -1,5 +1,6 @@
using Content.Server.Atmos.EntitySystems;
using Content.Shared.Atmos;
using Content.Shared.Atmos.Reactions;
using JetBrains.Annotations;
namespace Content.Server.Atmos.Reactions;

View File

@@ -1,5 +1,6 @@
using Content.Server.Atmos.EntitySystems;
using Content.Shared.Atmos;
using Content.Shared.Atmos.Reactions;
using JetBrains.Annotations;
namespace Content.Server.Atmos.Reactions;

View File

@@ -1,22 +1,10 @@
using Content.Server.Atmos.EntitySystems;
using Content.Shared.Atmos;
using Content.Shared.Atmos.Reactions;
using Robust.Shared.Prototypes;
namespace Content.Server.Atmos.Reactions
{
[Flags]
public enum ReactionResult : byte
{
NoReaction = 0,
Reacting = 1,
StopReactions = 2,
}
public enum GasReaction : byte
{
Fire = 0,
}
[Prototype("gasReaction")]
public sealed partial class GasReactionPrototype : IPrototype
{

View File

@@ -1,5 +1,6 @@
using Content.Server.Atmos.EntitySystems;
using Content.Shared.Atmos;
using Content.Shared.Atmos.Reactions;
using JetBrains.Annotations;
namespace Content.Server.Atmos.Reactions;

View File

@@ -1,5 +1,6 @@
using Content.Server.Atmos.EntitySystems;
using Content.Shared.Atmos;
using Content.Shared.Atmos.Reactions;
using JetBrains.Annotations;
namespace Content.Server.Atmos.Reactions

View File

@@ -1,5 +1,6 @@
using Content.Server.Atmos.EntitySystems;
using Content.Shared.Atmos;
using Content.Shared.Atmos.Reactions;
using JetBrains.Annotations;
namespace Content.Server.Atmos.Reactions

View File

@@ -1,5 +1,7 @@
using Content.Server.Atmos.EntitySystems;
using Content.Server.Fluids.EntitySystems;
using Content.Shared.Atmos;
using Content.Shared.Atmos.Reactions;
using Content.Shared.Chemistry.Components;
using Content.Shared.FixedPoint;
using Content.Shared.Maps;

View File

@@ -1,4 +1,5 @@
using System.Globalization;
using Content.Shared.Atmos;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.Manager;
using Robust.Shared.Serialization.Markdown;

View File

@@ -1,4 +1,5 @@
using System.Diagnostics.CodeAnalysis;
using Content.Shared.Atmos;
namespace Content.Server.Atmos;

View File

@@ -6,6 +6,7 @@ using Content.Server.Fluids.Components;
using Content.Server.Ghost.Roles.Components;
using Content.Server.Kitchen.Components;
using Content.Server.Popups;
using Content.Shared.Atmos;
using Content.Shared.Botany;
using Content.Shared.Burial.Components;
using Content.Shared.Chemistry.Reagent;

View File

@@ -1,4 +1,5 @@
using Content.Server.Atmos;
using Content.Shared.Atmos;
using JetBrains.Annotations;
namespace Content.Server.Destructible.Thresholds.Behaviors;

View File

@@ -1,5 +1,6 @@
using Content.Server.Atmos;
using Content.Server.Disposal.Tube.Components;
using Content.Shared.Atmos;
using Robust.Shared.Containers;
namespace Content.Server.Disposal.Unit.Components

View File

@@ -8,6 +8,7 @@ using Content.Server.Materials;
using Content.Server.Power.Components;
using Content.Server.Power.EntitySystems;
using Content.Server.Stack;
using Content.Shared.Atmos;
using Content.Shared.UserInterface;
using Content.Shared.Database;
using Content.Shared.Emag.Components;

View File

@@ -1,4 +1,5 @@
using Content.Server.Atmos;
using Content.Shared.Atmos;
namespace Content.Server.Mech.Components;

View File

@@ -13,6 +13,7 @@ using Content.Server.NodeContainer.NodeGroups;
using Content.Server.NodeContainer.Nodes;
using Content.Server.Power.Components;
using Content.Server.Temperature.Components;
using Content.Shared.Atmos;
using Content.Shared.UserInterface;
using Content.Shared.Chemistry;
using Content.Shared.Chemistry.Components;

View File

@@ -13,6 +13,7 @@ using Content.Shared.IdentityManagement;
using Content.Shared.Interaction;
using Content.Shared.Nutrition;
using System.Threading;
using Content.Shared.Atmos;
/// <summary>
/// System for vapes

View File

@@ -7,6 +7,7 @@ using Content.Server.DeviceNetwork.Systems;
using Content.Server.NodeContainer;
using Content.Server.NodeContainer.Nodes;
using Content.Server.Power.Components;
using Content.Shared.Atmos;
using Content.Shared.DeviceNetwork;
using Content.Shared.Examine;
using Content.Shared.Power.Generation.Teg;

View File

@@ -1,5 +1,6 @@
using Content.Server.Atmos;
using Content.Server.Atmos.EntitySystems;
using Content.Shared.Atmos;
using Content.Shared.Power.Generator;
namespace Content.Server.Power.Generator;

View File

@@ -1,4 +1,5 @@
using Content.Server.Atmos;
using Content.Shared.Atmos;
using Content.Shared.Storage.Components;
using Robust.Shared.GameStates;

View File

@@ -2,6 +2,7 @@
using Content.Server.Atmos.EntitySystems;
using Content.Server.Xenoarchaeology.XenoArtifacts.Effects.Components;
using Content.Server.Xenoarchaeology.XenoArtifacts.Events;
using Content.Shared.Atmos;
namespace Content.Server.Xenoarchaeology.XenoArtifacts.Effects.Systems;

View File

@@ -2,6 +2,7 @@
using Content.Server.Atmos.EntitySystems;
using Content.Server.Xenoarchaeology.XenoArtifacts.Effects.Components;
using Content.Server.Xenoarchaeology.XenoArtifacts.Events;
using Content.Shared.Atmos;
using Robust.Server.GameObjects;
namespace Content.Server.Xenoarchaeology.XenoArtifacts.Effects.Systems;