Fix breathing once and for all (#1996)

* Fix breathing

* WIP changes because I don't trust git stash after 2 weeks

* My imports

* Add gasping, adjust breathing values and fix test

* Make the gasp message appear to others

* Add PopupMessageEveryone extension

* Change used percentage to use a single number instead

* Remove unnecessary logging

* Fix air consistency test

* Add test map to SkippedMaps array
This commit is contained in:
DrSmugleaf
2020-09-12 22:52:50 +02:00
committed by GitHub
parent 68bf099cbe
commit 65d7775665
7 changed files with 543 additions and 72 deletions

View File

@@ -1,7 +1,9 @@
using System.Linq;
using Content.Server.Atmos;
using Content.Server.GameObjects.Components.Chemistry;
using Content.Server.GameObjects.Components.Metabolism;
using Content.Server.Interfaces;
using Content.Shared.Atmos;
using Content.Shared.Chemistry;
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization;
@@ -45,7 +47,7 @@ namespace Content.Server.GameObjects.Components.Body.Circulatory
{
base.ExposeData(serializer);
Air = new GasMixture(6);
Air = new GasMixture(6) {Temperature = Atmospherics.NormalBodyTemperature};
serializer.DataField(ref _initialMaxVolume, "maxVolume", ReagentUnit.New(250));
}
@@ -68,17 +70,29 @@ namespace Content.Server.GameObjects.Components.Body.Circulatory
return true;
}
public void PumpToxins(GasMixture into, float pressure)
public void PumpToxins(GasMixture to)
{
if (!Owner.TryGetComponent(out MetabolismComponent metabolism))
{
Air.PumpGasTo(into, pressure);
to.Merge(Air);
Air.Clear();
return;
}
var toxins = metabolism.Clean(this);
var toOld = to.Gases.ToArray();
to.Merge(toxins);
for (var i = 0; i < toOld.Length; i++)
{
var newAmount = to.GetMoles(i);
var oldAmount = toOld[i];
var delta = newAmount - oldAmount;
toxins.AdjustMoles(i, -delta);
}
toxins.PumpGasTo(into, pressure);
Air.Merge(toxins);
}
}

View File

@@ -1,8 +1,11 @@
using System;
using System.Linq;
using Content.Server.Atmos;
using Content.Server.GameObjects.Components.Body.Circulatory;
using Content.Server.Interfaces;
using Content.Server.Utility;
using Content.Shared.Atmos;
using Content.Shared.Interfaces;
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;
@@ -16,27 +19,31 @@ namespace Content.Server.GameObjects.Components.Body.Respiratory
private float _accumulatedFrameTime;
/// <summary>
/// The pressure that this lung exerts on the air around it
/// </summary>
[ViewVariables(VVAccess.ReadWrite)] private float Pressure { get; set; }
[ViewVariables] public GasMixture Air { get; set; }
[ViewVariables] public LungStatus Status { get; set; }
[ViewVariables] public float CycleDelay { get; set; }
public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);
Air = new GasMixture();
Air = new GasMixture {Temperature = Atmospherics.NormalBodyTemperature};
serializer.DataReadWriteFunction(
"volume",
6,
vol => Air.Volume = vol,
() => Air.Volume);
serializer.DataField(this, l => l.Pressure, "pressure", 100);
serializer.DataReadWriteFunction(
"temperature",
Atmospherics.NormalBodyTemperature,
temp => Air.Temperature = temp,
() => Air.Temperature);
serializer.DataField(this, l => l.CycleDelay, "cycleDelay", 2);
}
public void Update(float frameTime)
@@ -54,7 +61,9 @@ namespace Content.Server.GameObjects.Components.Body.Respiratory
};
var absoluteTime = Math.Abs(_accumulatedFrameTime);
if (absoluteTime < 2)
var delay = CycleDelay;
if (absoluteTime < delay)
{
return;
}
@@ -73,50 +82,100 @@ namespace Content.Server.GameObjects.Components.Body.Respiratory
throw new ArgumentOutOfRangeException();
}
_accumulatedFrameTime = absoluteTime - 2;
_accumulatedFrameTime = absoluteTime - delay;
}
public void Transfer(GasMixture from, GasMixture to, float ratio)
{
var removed = from.RemoveRatio(ratio);
var toOld = to.Gases.ToArray();
to.Merge(removed);
for (var gas = 0; gas < Atmospherics.TotalNumberOfGases; gas++)
{
var newAmount = to.GetMoles(gas);
var oldAmount = toOld[gas];
var delta = newAmount - oldAmount;
removed.AdjustMoles(gas, -delta);
}
from.Merge(removed);
}
public void ToBloodstream(GasMixture mixture)
{
if (!Owner.TryGetComponent(out BloodstreamComponent bloodstream))
{
return;
}
var to = bloodstream.Air;
to.Merge(mixture);
mixture.Clear();
}
public void Inhale(float frameTime)
{
if (!Owner.TryGetComponent(out BloodstreamComponent bloodstream))
{
return;
}
if (!Owner.Transform.Coordinates.TryGetTileAir(out var tileAir))
{
return;
}
var amount = Atmospherics.BreathPercentage * frameTime;
var volumeRatio = amount / tileAir.Volume;
var temp = tileAir.RemoveRatio(volumeRatio);
Inhale(frameTime, tileAir);
}
temp.PumpGasTo(Air, Pressure);
Air.PumpGasTo(bloodstream.Air, Pressure);
tileAir.Merge(temp);
public void Inhale(float frameTime, GasMixture from)
{
var ratio = Atmospherics.BreathPercentage * frameTime;
Transfer(from, Air, ratio);
ToBloodstream(Air);
}
public void Exhale(float frameTime)
{
if (!Owner.TryGetComponent(out BloodstreamComponent bloodstream))
{
return;
}
if (!Owner.Transform.Coordinates.TryGetTileAir(out var tileAir))
{
return;
}
bloodstream.PumpToxins(Air, Pressure);
Exhale(frameTime, tileAir);
}
var amount = Atmospherics.BreathPercentage * frameTime;
var volumeRatio = amount / tileAir.Volume;
var temp = tileAir.RemoveRatio(volumeRatio);
public void Exhale(float frameTime, GasMixture to)
{
// TODO: Make the bloodstream separately pump toxins into the lungs, making the lungs' only job to empty.
if (!Owner.TryGetComponent(out BloodstreamComponent bloodstream))
{
return;
}
temp.PumpGasTo(tileAir, Pressure);
Air.Merge(temp);
bloodstream.PumpToxins(Air);
var lungRemoved = Air.RemoveRatio(0.5f);
var toOld = to.Gases.ToArray();
to.Merge(lungRemoved);
for (var gas = 0; gas < Atmospherics.TotalNumberOfGases; gas++)
{
var newAmount = to.GetMoles(gas);
var oldAmount = toOld[gas];
var delta = newAmount - oldAmount;
lungRemoved.AdjustMoles(gas, -delta);
}
Air.Merge(lungRemoved);
}
public void Gasp()
{
Owner.PopupMessageEveryone("Gasp");
Inhale(CycleDelay);
}
}

View File

@@ -3,16 +3,16 @@ using System.Collections.Generic;
using System.Linq;
using Content.Server.Atmos;
using Content.Server.GameObjects.Components.Body.Circulatory;
using Content.Server.GameObjects.Components.Body.Respiratory;
using Content.Server.GameObjects.Components.Temperature;
using Content.Server.GameObjects.EntitySystems;
using Content.Shared.Atmos;
using Content.Shared.Chemistry;
using Content.Shared.Damage;
using Content.Shared.GameObjects.Components.Damage;
using Content.Shared.GameObjects.EntitySystems;
using Content.Shared.Interfaces;
using Content.Shared.Interfaces.Chemistry;
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Systems;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
@@ -28,7 +28,6 @@ namespace Content.Server.GameObjects.Components.Metabolism
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IEntityManager _entityManager = default!;
public override string Name => "Metabolism";
private float _accumulatedFrameTime;
@@ -85,7 +84,7 @@ namespace Content.Server.GameObjects.Components.Metabolism
/// </summary>
public float ThermalRegulationTemperatureThreshold { get; private set; }
[ViewVariables] public bool Suffocating => SuffocatingPercentage() > 0;
[ViewVariables] public bool Suffocating { get; private set; }
public override void ExposeData(ObjectSerializer serializer)
{
@@ -156,12 +155,16 @@ namespace Content.Server.GameObjects.Components.Metabolism
private float GasProducedMultiplier(Gas gas, float usedAverage)
{
if (!NeedsGases.TryGetValue(gas, out var needs) ||
!ProducesGases.TryGetValue(gas, out var produces))
if (!ProducesGases.TryGetValue(gas, out var produces))
{
return 0;
}
if (!NeedsGases.TryGetValue(gas, out var needs))
{
needs = 1;
}
return needs * produces * usedAverage;
}
@@ -177,31 +180,44 @@ namespace Content.Server.GameObjects.Components.Metabolism
return;
}
var usedPercentages = new float[Atmospherics.TotalNumberOfGases];
var needs = NeedsAndDeficit(frameTime);
var used = 0f;
foreach (var (gas, amountNeeded) in needs)
{
var bloodstreamAmount = bloodstream.Air.GetMoles(gas);
var deficit = 0f;
if (bloodstreamAmount >= amountNeeded)
if (bloodstreamAmount < amountNeeded)
{
bloodstream.Air.AdjustMoles(gas, -amountNeeded);
// Panic inhale
if (Owner.TryGetComponent(out LungComponent lung))
{
lung.Gasp();
bloodstreamAmount = bloodstream.Air.GetMoles(gas);
}
deficit = Math.Max(0, amountNeeded - bloodstreamAmount);
if (deficit > 0)
{
bloodstream.Air.SetMoles(gas, 0);
}
else
{
bloodstream.Air.AdjustMoles(gas, -amountNeeded);
}
}
else
{
deficit = amountNeeded - bloodstreamAmount;
bloodstream.Air.SetMoles(gas, 0);
bloodstream.Air.AdjustMoles(gas, -amountNeeded);
}
DeficitGases[gas] = deficit;
var used = amountNeeded - deficit;
usedPercentages[(int) gas] = used / amountNeeded;
used += (amountNeeded - deficit) / amountNeeded;
}
var usedAverage = usedPercentages.Average();
var produced = GasProduced(usedAverage);
var produced = GasProduced(used / needs.Count);
foreach (var (gas, amountProduced) in produced)
{
@@ -280,7 +296,6 @@ namespace Content.Server.GameObjects.Components.Metabolism
}
}
/// <summary>
/// Loops through each reagent in _internalSolution,
/// and calls <see cref="IMetabolizable.Metabolize"/> for each of them.
@@ -338,42 +353,65 @@ namespace Content.Server.GameObjects.Components.Metabolism
return;
}
ProcessGases(_accumulatedFrameTime);
ProcessNutrients(_accumulatedFrameTime);
ProcessThermalRegulation(_accumulatedFrameTime);
_accumulatedFrameTime -= 1;
ProcessGases(frameTime);
ProcessNutrients(frameTime);
ProcessThermalRegulation(frameTime);
if (Suffocating)
if (SuffocatingPercentage() > 0)
{
// damageable.ChangeDamage(DamageClass.Airloss, _suffocationDamage, false);
TakeSuffocationDamage();
return;
}
StopSuffocation();
}
public void Transfer(BloodstreamComponent @from, GasMixture to, Gas gas, float pressure)
private void TakeSuffocationDamage()
{
var transfer = new GasMixture();
var molesInBlood = @from.Air.GetMoles(gas);
Suffocating = true;
transfer.SetMoles(gas, molesInBlood);
transfer.ReleaseGasTo(to, pressure);
if (!Owner.TryGetComponent(out IDamageableComponent damageable))
{
return;
}
@from.Air.Merge(transfer);
damageable.ChangeDamage(DamageClass.Airloss, _suffocationDamage, false);
}
public GasMixture Clean(BloodstreamComponent bloodstream, float pressure = 100)
private void StopSuffocation()
{
var gasMixture = new GasMixture(bloodstream.Air.Volume);
Suffocating = false;
}
public GasMixture Clean(BloodstreamComponent bloodstream)
{
var gasMixture = new GasMixture(bloodstream.Air.Volume)
{
Temperature = bloodstream.Air.Temperature
};
for (Gas gas = 0; gas < (Gas) Atmospherics.TotalNumberOfGases; gas++)
{
if (NeedsGases.TryGetValue(gas, out var needed) &&
bloodstream.Air.GetMoles(gas) < needed * 1.5f)
float amount;
var molesInBlood = bloodstream.Air.GetMoles(gas);
if (!NeedsGases.TryGetValue(gas, out var needed))
{
continue;
amount = molesInBlood;
}
else
{
var overflowThreshold = needed * 1.5f;
amount = molesInBlood > overflowThreshold
? molesInBlood - overflowThreshold
: 0;
}
Transfer(bloodstream, gasMixture, gas, pressure);
gasMixture.AdjustMoles(gas, amount);
bloodstream.Air.AdjustMoles(gas, -amount);
}
return gasMixture;