Draw MapAtmosphere gasses (#17688)

This commit is contained in:
metalgearsloth
2023-06-28 21:22:03 +10:00
committed by GitHub
parent 2488dd4ecd
commit 44a3db398b
11 changed files with 250 additions and 81 deletions

View File

@@ -1,11 +1,13 @@
using Content.Shared.Atmos.Components;
using Content.Shared.Atmos.EntitySystems;
namespace Content.Server.Atmos.Components;
/// <summary>
/// Component that defines the default GasMixture for a map.
/// </summary>
/// <remarks>Honestly, no need to [Friend] this. It's just two simple data fields... Change them to your heart's content.</remarks>
[RegisterComponent]
public sealed class MapAtmosphereComponent : Component
[RegisterComponent, Access(typeof(SharedAtmosphereSystem))]
public sealed class MapAtmosphereComponent : SharedMapAtmosphereComponent
{
/// <summary>
/// The default GasMixture a map will have. Space mixture by default.

View File

@@ -1,4 +1,6 @@
using Content.Server.Atmos.Components;
using Content.Shared.Atmos.Components;
using Robust.Shared.GameStates;
namespace Content.Server.Atmos.EntitySystems;
@@ -9,6 +11,7 @@ public partial class AtmosphereSystem
SubscribeLocalEvent<MapAtmosphereComponent, IsTileSpaceMethodEvent>(MapIsTileSpace);
SubscribeLocalEvent<MapAtmosphereComponent, GetTileMixtureMethodEvent>(MapGetTileMixture);
SubscribeLocalEvent<MapAtmosphereComponent, GetTileMixturesMethodEvent>(MapGetTileMixtures);
SubscribeLocalEvent<MapAtmosphereComponent, ComponentGetState>(OnMapGetState);
}
private void MapIsTileSpace(EntityUid uid, MapAtmosphereComponent component, ref IsTileSpaceMethodEvent args)
@@ -42,4 +45,37 @@ public partial class AtmosphereSystem
args.Mixtures[i] ??= component.Mixture.Clone();
}
}
private void OnMapGetState(EntityUid uid, MapAtmosphereComponent component, ref ComponentGetState args)
{
args.State = new MapAtmosphereComponentState(_gasTileOverlaySystem.GetOverlayData(component.Mixture));
}
public void SetMapAtmosphere(EntityUid uid, bool space, GasMixture mixture, MapAtmosphereComponent? component = null)
{
if (!Resolve(uid, ref component))
return;
component.Space = space;
component.Mixture = mixture;
Dirty(component);
}
public void SetMapGasMixture(EntityUid uid, GasMixture? mixture, MapAtmosphereComponent? component = null)
{
if (!Resolve(uid, ref component))
return;
component.Mixture = mixture;
Dirty(component);
}
public void SetMapSpace(EntityUid uid, bool space, MapAtmosphereComponent? component = null)
{
if (!Resolve(uid, ref component))
return;
component.Space = space;
Dirty(component);
}
}

View File

@@ -139,6 +139,39 @@ namespace Content.Server.Atmos.EntitySystems
}
}
private byte GetOpacity(float moles, float molesVisible, float molesVisibleMax)
{
return (byte) (ContentHelpers.RoundToLevels(
MathHelper.Clamp01((moles - molesVisible) /
(molesVisibleMax - molesVisible)) * 255, byte.MaxValue,
_thresholds) * 255 / (_thresholds - 1));
}
public GasOverlayData GetOverlayData(GasMixture? mixture)
{
var data = new GasOverlayData(0, new byte[VisibleGasId.Length]);
for (var i = 0; i < VisibleGasId.Length; i++)
{
var id = VisibleGasId[i];
var gas = _atmosphereSystem.GetGas(id);
var moles = mixture?.Moles[id] ?? 0f;
ref var opacity = ref data.Opacity[i];
if (moles < gas.GasMolesVisible)
{
continue;
}
opacity = (byte) (ContentHelpers.RoundToLevels(
MathHelper.Clamp01((moles - gas.GasMolesVisible) /
(gas.GasMolesVisibleMax - gas.GasMolesVisible)) * 255, byte.MaxValue,
_thresholds) * 255 / (_thresholds - 1));
}
return data;
}
/// <summary>
/// Updates the visuals for a tile on some grid chunk. Returns true if the visuals have changed.
/// </summary>
@@ -187,10 +220,7 @@ namespace Content.Server.Atmos.EntitySystems
continue;
}
var opacity = (byte) (ContentHelpers.RoundToLevels(
MathHelper.Clamp01((moles - gas.GasMolesVisible) /
(gas.GasMolesVisibleMax - gas.GasMolesVisible)) * 255, byte.MaxValue,
_thresholds) * 255 / (_thresholds - 1));
var opacity = GetOpacity(moles, gas.GasMolesVisible, gas.GasMolesVisibleMax);
if (oldOpacity == opacity)
continue;

View File

@@ -2,6 +2,7 @@ using System.Linq;
using Content.Server.Administration;
using Content.Server.Atmos;
using Content.Server.Atmos.Components;
using Content.Server.Atmos.EntitySystems;
using Content.Server.Parallax;
using Content.Shared.Administration;
using Content.Shared.Atmos;
@@ -85,17 +86,18 @@ public sealed class PlanetCommand : IConsoleCommand
// Atmos
var atmos = _entManager.EnsureComponent<MapAtmosphereComponent>(mapUid);
atmos.Space = false;
var moles = new float[Atmospherics.AdjustedNumberOfGases];
moles[(int) Gas.Oxygen] = 21.824779f;
moles[(int) Gas.Nitrogen] = 82.10312f;
atmos.Mixture = new GasMixture(2500)
var mixture = new GasMixture(2500)
{
Temperature = 293.15f,
Moles = moles,
};
_entManager.System<AtmosphereSystem>().SetMapAtmosphere(mapUid, false, mixture, atmos);
_entManager.EnsureComponent<MapGridComponent>(mapUid);
shell.WriteLine(Loc.GetString("cmd-planet-success", ("mapId", mapId)));
}

View File

@@ -3,6 +3,7 @@ using System.Threading;
using System.Threading.Tasks;
using Content.Server.Atmos;
using Content.Server.Atmos.Components;
using Content.Server.Atmos.EntitySystems;
using Robust.Shared.CPUJob.JobQueues;
using Content.Server.Ghost.Roles.Components;
using Content.Server.Parallax;
@@ -111,13 +112,13 @@ public sealed class SpawnSalvageMissionJob : Job<bool>
var moles = new float[Atmospherics.AdjustedNumberOfGases];
air.Gases.CopyTo(moles, 0);
var atmos = _entManager.EnsureComponent<MapAtmosphereComponent>(mapUid);
atmos.Space = air.Space;
atmos.Mixture = new GasMixture(2500)
_entManager.System<AtmosphereSystem>().SetMapSpace(mapUid, air.Space, atmos);
_entManager.System<AtmosphereSystem>().SetMapGasMixture(mapUid, new GasMixture(2500)
{
// TODO: temperature mods
Temperature = 293.15f,
Moles = moles,
};
}, atmos);
if (mission.Color != null)
{