Clean up gas miners (#29657)
Separate the environment check from CapSpawnAmount into GetValidEnvironment to make the code a little cleaner, and also makes these two checks independent. CapSpawnAmount and GetValidEnvironment now both have zero side-effects Broken renamed Idle to reflect its use. Broken in my mind implies that there's some method for fixing. --------- Co-authored-by: Partmedia <kevinz5000@gmail.com>
This commit is contained in:
@@ -5,14 +5,22 @@ namespace Content.Server.Atmos.Piping.Other.Components
|
||||
[RegisterComponent]
|
||||
public sealed partial class GasMinerComponent : Component
|
||||
{
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
public bool Enabled { get; set; } = true;
|
||||
|
||||
public bool Broken { get; set; } = false;
|
||||
[ViewVariables(VVAccess.ReadOnly)]
|
||||
public bool Idle { get; set; } = false;
|
||||
|
||||
/// <summary>
|
||||
/// If the number of moles in the external environment exceeds this number, no gas will be mined.
|
||||
/// </summary>
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
[DataField("maxExternalAmount")]
|
||||
public float MaxExternalAmount { get; set; } = float.PositiveInfinity;
|
||||
|
||||
/// <summary>
|
||||
/// If the pressure (in kPA) of the external environment exceeds this number, no gas will be mined.
|
||||
/// </summary>
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
[DataField("maxExternalPressure")]
|
||||
public float MaxExternalPressure { get; set; } = Atmospherics.GasMinerDefaultMaxExternalPressure;
|
||||
|
||||
@@ -25,10 +25,17 @@ namespace Content.Server.Atmos.Piping.Other.EntitySystems
|
||||
{
|
||||
var miner = ent.Comp;
|
||||
|
||||
if (!GetValidEnvironment(ent, out var environment))
|
||||
{
|
||||
miner.Idle = true;
|
||||
return;
|
||||
}
|
||||
|
||||
// SpawnAmount is declared in mol/s so to get the amount of gas we hope to mine, we have to multiply this by
|
||||
// how long we have been waiting to spawn it and further cap the number according to the miner's state.
|
||||
var toSpawn = CapSpawnAmount(ent, miner.SpawnAmount * args.dt, out var environment);
|
||||
if (toSpawn <= 0f || environment == null || !miner.Enabled || !miner.SpawnGas.HasValue)
|
||||
var toSpawn = CapSpawnAmount(ent, miner.SpawnAmount * args.dt, environment);
|
||||
miner.Idle = toSpawn == 0;
|
||||
if (miner.Idle || !miner.Enabled || !miner.SpawnGas.HasValue)
|
||||
return;
|
||||
|
||||
// Time to mine some gas.
|
||||
@@ -39,27 +46,26 @@ namespace Content.Server.Atmos.Piping.Other.EntitySystems
|
||||
_atmosphereSystem.Merge(environment, merger);
|
||||
}
|
||||
|
||||
private float CapSpawnAmount(Entity<GasMinerComponent> ent, float toSpawnTarget, out GasMixture? environment)
|
||||
private bool GetValidEnvironment(Entity<GasMinerComponent> ent, [NotNullWhen(true)] out GasMixture? environment)
|
||||
{
|
||||
var (uid, miner) = ent;
|
||||
var transform = Transform(uid);
|
||||
environment = _atmosphereSystem.GetContainingMixture((uid, transform), true, true);
|
||||
|
||||
var position = _transformSystem.GetGridOrMapTilePosition(uid, transform);
|
||||
|
||||
// Space.
|
||||
// Treat space as an invalid environment
|
||||
if (_atmosphereSystem.IsTileSpace(transform.GridUid, transform.MapUid, position))
|
||||
{
|
||||
miner.Broken = true;
|
||||
return 0f;
|
||||
environment = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Air-blocked location.
|
||||
if (environment == null)
|
||||
{
|
||||
miner.Broken = true;
|
||||
return 0f;
|
||||
}
|
||||
environment = _atmosphereSystem.GetContainingMixture((uid, transform), true, true);
|
||||
return environment != null;
|
||||
}
|
||||
|
||||
private float CapSpawnAmount(Entity<GasMinerComponent> ent, float toSpawnTarget, GasMixture environment)
|
||||
{
|
||||
var (uid, miner) = ent;
|
||||
|
||||
// How many moles could we theoretically spawn. Cap by pressure and amount.
|
||||
var allowableMoles = Math.Min(
|
||||
@@ -69,11 +75,9 @@ namespace Content.Server.Atmos.Piping.Other.EntitySystems
|
||||
var toSpawnReal = Math.Clamp(allowableMoles, 0f, toSpawnTarget);
|
||||
|
||||
if (toSpawnReal < Atmospherics.GasMinMoles) {
|
||||
miner.Broken = true;
|
||||
return 0f;
|
||||
}
|
||||
|
||||
miner.Broken = false;
|
||||
return toSpawnReal;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,8 +66,6 @@
|
||||
parent: GasMinerBase
|
||||
id: GasMinerNitrogen
|
||||
suffix: Shuttle, 300kPa
|
||||
placement:
|
||||
mode: SnapgridCenter
|
||||
components:
|
||||
- type: GasMiner
|
||||
spawnGas: Nitrogen
|
||||
@@ -94,8 +92,6 @@
|
||||
name: CO2 gas miner
|
||||
parent: GasMinerBase
|
||||
id: GasMinerCarbonDioxide
|
||||
placement:
|
||||
mode: SnapgridCenter
|
||||
components:
|
||||
- type: GasMiner
|
||||
spawnGas: CarbonDioxide
|
||||
@@ -104,8 +100,6 @@
|
||||
name: plasma gas miner
|
||||
parent: GasMinerBase
|
||||
id: GasMinerPlasma
|
||||
placement:
|
||||
mode: SnapgridCenter
|
||||
components:
|
||||
- type: GasMiner
|
||||
spawnGas: Plasma
|
||||
@@ -114,8 +108,6 @@
|
||||
name: tritium gas miner
|
||||
parent: GasMinerBase
|
||||
id: GasMinerTritium
|
||||
placement:
|
||||
mode: SnapgridCenter
|
||||
components:
|
||||
- type: GasMiner
|
||||
spawnGas: Tritium
|
||||
@@ -124,8 +116,6 @@
|
||||
name: water vapor gas miner
|
||||
parent: GasMinerBase
|
||||
id: GasMinerWaterVapor
|
||||
placement:
|
||||
mode: SnapgridCenter
|
||||
components:
|
||||
- type: GasMiner
|
||||
spawnGas: WaterVapor
|
||||
@@ -134,8 +124,6 @@
|
||||
name: ammonia gas miner
|
||||
parent: GasMinerBase
|
||||
id: GasMinerAmmonia
|
||||
placement:
|
||||
mode: SnapgridCenter
|
||||
components:
|
||||
- type: GasMiner
|
||||
spawnGas: Ammonia
|
||||
@@ -144,8 +132,6 @@
|
||||
name: nitrous oxide gas miner
|
||||
parent: GasMinerBase
|
||||
id: GasMinerNitrousOxide
|
||||
placement:
|
||||
mode: SnapgridCenter
|
||||
components:
|
||||
- type: GasMiner
|
||||
spawnGas: NitrousOxide
|
||||
|
||||
Reference in New Issue
Block a user