Revert "Revert "epic Respiration Rework"" (#6527)

This commit is contained in:
Leon Friedrich
2022-02-10 17:53:10 +13:00
committed by GitHub
parent b417c022bf
commit 706ac6af40
32 changed files with 596 additions and 586 deletions

View File

@@ -44,34 +44,4 @@ public class BloodstreamSystem : EntitySystem
return _solutionContainerSystem.TryAddSolution(uid, component.Solution, solution);
}
public void PumpToxins(EntityUid uid, GasMixture to, BloodstreamComponent? blood=null, RespiratorComponent? respiration=null)
{
if (!Resolve(uid, ref blood))
return;
if(!Resolve(uid, ref respiration, false))
{
_atmosSystem.Merge(to, blood.Air);
blood.Air.Clear();
return;
}
var toxins = _respiratorSystem.Clean(uid, respiration, blood);
var toOld = new float[to.Moles.Length];
Array.Copy(to.Moles, toOld, toOld.Length);
_atmosSystem.Merge(to, 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);
}
_atmosSystem.Merge(blood.Air, toxins);
}
}

View File

@@ -0,0 +1,25 @@
using Content.Server.Atmos.Components;
using Content.Server.Body.Components;
using Content.Shared.Atmos;
using Robust.Shared.GameObjects;
namespace Content.Server.Body.Systems;
public class InternalsSystem : EntitySystem
{
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<InternalsComponent, InhaleLocationEvent>(OnInhaleLocation);
}
private void OnInhaleLocation(EntityUid uid, InternalsComponent component, InhaleLocationEvent args)
{
if (component.AreInternalsWorking())
{
var gasTank = Comp<GasTankComponent>(component.GasTankEntity!.Value);
args.Gas = gasTank.RemoveAirVolume(Atmospherics.BreathVolume);
}
}
}

View File

@@ -1,33 +1,28 @@
using System;
using Content.Server.Atmos;
using System.Linq;
using Content.Server.Atmos.Components;
using Content.Server.Atmos.EntitySystems;
using Content.Server.Body.Components;
using Content.Server.Popups;
using Content.Server.Chemistry.EntitySystems;
using Content.Shared.Atmos;
using Content.Shared.Body.Components;
using Content.Shared.Body.Events;
using Content.Shared.Chemistry.Components;
using Content.Shared.Inventory.Events;
using Content.Shared.MobState.Components;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Player;
using Robust.Shared.Timing;
using Robust.Shared.Utility;
namespace Content.Server.Body.Systems;
public class LungSystem : EntitySystem
{
[Dependency] private readonly IGameTiming _gameTiming = default!;
[Dependency] private readonly AtmosphereSystem _atmosSys = default!;
[Dependency] private readonly BloodstreamSystem _bloodstreamSystem = default!;
[Dependency] private readonly PopupSystem _popupSystem = default!;
[Dependency] private readonly SolutionContainerSystem _solutionContainerSystem = default!;
[Dependency] private readonly AtmosphereSystem _atmosphereSystem = default!;
public static string LungSolutionName = "Lung";
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<LungComponent, AddedToBodyEvent>(OnAddedToBody);
SubscribeLocalEvent<LungComponent, ComponentInit>(OnComponentInit);
SubscribeLocalEvent<BreathToolComponent, GotEquippedEvent>(OnGotEquipped);
SubscribeLocalEvent<BreathToolComponent, GotUnequippedEvent>(OnGotUnequipped);
}
@@ -50,175 +45,30 @@ public class LungSystem : EntitySystem
}
}
private void OnAddedToBody(EntityUid uid, LungComponent component, AddedToBodyEvent args)
private void OnComponentInit(EntityUid uid, LungComponent component, ComponentInit args)
{
Inhale(uid, component.CycleDelay);
component.LungSolution = _solutionContainerSystem.EnsureSolution(uid, LungSolutionName);
component.LungSolution.MaxVolume = 100.0f;
component.LungSolution.CanReact = false; // No dexalin lungs
}
public void Gasp(EntityUid uid,
LungComponent? lung=null,
MechanismComponent? mech=null)
public void GasToReagent(EntityUid uid, LungComponent lung)
{
if (!Resolve(uid, ref lung, ref mech))
return;
if (_gameTiming.CurTime >= lung.LastGaspPopupTime + lung.GaspPopupCooldown)
foreach (var gas in lung.ValidGases)
{
lung.LastGaspPopupTime = _gameTiming.CurTime;
_popupSystem.PopupEntity(Loc.GetString("lung-behavior-gasp"), uid, Filter.Pvs(uid));
var i = (int) gas;
var moles = lung.Air.Moles[i];
if (moles <= 0)
continue;
var reagent = _atmosphereSystem.GasReagents[i];
if (reagent == null) continue;
var amount = moles * Atmospherics.BreathMolesToReagentMultiplier;
_solutionContainerSystem.TryAddReagent(uid, lung.LungSolution, reagent, amount, out _);
// We don't remove the gas from the lung mix,
// that's the responsibility of whatever gas is being metabolized.
// Most things will just want to exhale again.
}
if (mech.Body != null && TryComp((mech.Body).Owner, out MobStateComponent? mobState) && !mobState.IsAlive())
return;
Inhale(uid, lung.CycleDelay);
}
public void UpdateLung(EntityUid uid,
LungComponent? lung=null,
MechanismComponent? mech=null)
{
if (!Resolve(uid, ref lung, ref mech))
return;
if (mech.Body != null && EntityManager.TryGetComponent((mech.Body).Owner, out MobStateComponent? mobState) && mobState.IsCritical())
{
return;
}
if (lung.Status == LungStatus.None)
{
lung.Status = LungStatus.Inhaling;
}
lung.AccumulatedFrametime += lung.Status switch
{
LungStatus.Inhaling => 1,
LungStatus.Exhaling => -1,
_ => throw new ArgumentOutOfRangeException()
};
var absoluteTime = Math.Abs(lung.AccumulatedFrametime);
var delay = lung.CycleDelay;
if (absoluteTime < delay)
{
return;
}
switch (lung.Status)
{
case LungStatus.Inhaling:
Inhale(uid, absoluteTime);
lung.Status = LungStatus.Exhaling;
break;
case LungStatus.Exhaling:
Exhale(uid, absoluteTime);
lung.Status = LungStatus.Inhaling;
break;
default:
throw new ArgumentOutOfRangeException();
}
lung.AccumulatedFrametime = absoluteTime - delay;
}
/// <summary>
/// Tries to find an air mixture to inhale from, then inhales from it.
/// </summary>
public void Inhale(EntityUid uid, float frameTime,
LungComponent? lung=null,
MechanismComponent? mech=null)
{
if (!Resolve(uid, ref lung, ref mech))
return;
// TODO Jesus Christ make this event based.
if (mech.Body != null &&
EntityManager.TryGetComponent((mech.Body).Owner, out InternalsComponent? internals) &&
internals.BreathToolEntity != null &&
internals.GasTankEntity != null &&
EntityManager.TryGetComponent(internals.BreathToolEntity, out BreathToolComponent? breathTool) &&
breathTool.IsFunctional &&
EntityManager.TryGetComponent(internals.GasTankEntity, out GasTankComponent? gasTank))
{
TakeGasFrom(uid, frameTime, gasTank.RemoveAirVolume(Atmospherics.BreathVolume), lung);
return;
}
if (_atmosSys.GetTileMixture(EntityManager.GetComponent<TransformComponent>(uid).Coordinates, true) is not { } tileAir)
{
return;
}
TakeGasFrom(uid, frameTime, tileAir, lung);
}
/// <summary>
/// Inhales directly from a given mixture.
/// </summary>
public void TakeGasFrom(EntityUid uid, float frameTime, GasMixture from,
LungComponent? lung=null,
MechanismComponent? mech=null)
{
if (!Resolve(uid, ref lung, ref mech))
return;
var ratio = (Atmospherics.BreathVolume / from.Volume) * frameTime;
_atmosSys.Merge(lung.Air, from.RemoveRatio(ratio));
// Push to bloodstream
if (mech.Body == null)
return;
if (!EntityManager.TryGetComponent((mech.Body).Owner, out BloodstreamComponent? bloodstream))
return;
var to = bloodstream.Air;
_atmosSys.Merge(to, lung.Air);
lung.Air.Clear();
}
/// <summary>
/// Tries to find a gas mixture to exhale to, then pushes gas to it.
/// </summary>
public void Exhale(EntityUid uid, float frameTime,
LungComponent? lung=null,
MechanismComponent? mech=null)
{
if (!Resolve(uid, ref lung, ref mech))
return;
if (_atmosSys.GetTileMixture(EntityManager.GetComponent<TransformComponent>(uid).Coordinates, true) is not { } tileAir)
{
return;
}
PushGasTo(uid, tileAir, lung, mech);
}
/// <summary>
/// Pushes gas from the lungs to a gas mixture.
/// </summary>
public void PushGasTo(EntityUid uid, GasMixture to,
LungComponent? lung=null,
MechanismComponent? mech=null)
{
if (!Resolve(uid, ref lung, ref mech))
return;
// TODO: Make the bloodstream separately pump toxins into the lungs, making the lungs' only job to empty.
if (mech.Body == null)
return;
if (!EntityManager.TryGetComponent((mech.Body).Owner, out BloodstreamComponent? bloodstream))
return;
_bloodstreamSystem.PumpToxins((mech.Body).Owner, lung.Air, bloodstream);
var lungRemoved = lung.Air.RemoveRatio(0.5f);
_atmosSys.Merge(to, lungRemoved);
}
}

View File

@@ -77,6 +77,8 @@ namespace Content.Server.Body.Systems
// First step is get the solution we actually care about
Solution? solution = null;
EntityUid? solutionEntityUid = null;
EntityUid? bodyEntityUid = mech?.Body?.Owner;
SolutionContainerManagerComponent? manager = null;
if (meta.SolutionOnBody)
@@ -144,6 +146,10 @@ namespace Content.Server.Body.Systems
if (entry.MetabolismRate > mostToRemove)
mostToRemove = entry.MetabolismRate;
mostToRemove *= group.MetabolismRateModifier;
mostToRemove = FixedPoint2.Clamp(mostToRemove, 0, reagent.Quantity);
// if it's possible for them to be dead, and they are,
// then we shouldn't process any effects, but should probably
// still remove reagents
@@ -153,7 +159,8 @@ namespace Content.Server.Body.Systems
continue;
}
var args = new ReagentEffectArgs(solutionEntityUid.Value, (meta).Owner, solution, proto, entry.MetabolismRate,
var actualEntity = bodyEntityUid != null ? bodyEntityUid.Value : solutionEntityUid.Value;
var args = new ReagentEffectArgs(actualEntity, (meta).Owner, solution, proto, mostToRemove,
EntityManager, null);
// do all effects, if conditions apply
@@ -164,9 +171,8 @@ namespace Content.Server.Body.Systems
if (effect.ShouldLog)
{
var entity = args.SolutionEntity;
_logSystem.Add(LogType.ReagentEffect, effect.LogImpact,
$"Metabolism effect {effect.GetType().Name:effect} of reagent {args.Reagent.Name:reagent} applied on entity {entity} at {Transform(entity).Coordinates:coordinates}");
$"Metabolism effect {effect.GetType().Name:effect} of reagent {args.Reagent.Name:reagent} applied on entity {actualEntity:entity} at {Transform(actualEntity).Coordinates:coordinates}");
}
effect.Effect(args);

View File

@@ -1,9 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Content.Server.Administration.Logs;
using Content.Server.Atmos;
using Content.Server.Atmos.EntitySystems;
using Content.Server.Body.Components;
using Content.Server.Popups;
using Content.Shared.Alert;
using Content.Shared.Atmos;
using Content.Shared.Body.Components;
@@ -13,6 +14,9 @@ using Content.Shared.MobState.Components;
using JetBrains.Annotations;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Player;
using Robust.Shared.Timing;
namespace Content.Server.Body.Systems
{
@@ -23,14 +27,25 @@ namespace Content.Server.Body.Systems
[Dependency] private readonly AdminLogSystem _logSys = default!;
[Dependency] private readonly BodySystem _bodySystem = default!;
[Dependency] private readonly LungSystem _lungSystem = default!;
[Dependency] private readonly AtmosphereSystem _atmosSys = default!;
[Dependency] private readonly PopupSystem _popupSystem = default!;
[Dependency] private readonly AlertsSystem _alertsSystem = default!;
[Dependency] private readonly IGameTiming _gameTiming = default!;
public override void Initialize()
{
base.Initialize();
// We want to process lung reagents before we inhale new reagents.
UpdatesAfter.Add(typeof(MetabolizerSystem));
}
public override void Update(float frameTime)
{
base.Update(frameTime);
foreach (var (respirator, blood, body) in
EntityManager.EntityQuery<RespiratorComponent, BloodstreamComponent, SharedBodyComponent>())
foreach (var (respirator, body) in
EntityManager.EntityQuery<RespiratorComponent, SharedBodyComponent>())
{
var uid = respirator.Owner;
if (!EntityManager.TryGetComponent<MobStateComponent>(uid, out var state) ||
@@ -41,211 +56,142 @@ namespace Content.Server.Body.Systems
respirator.AccumulatedFrametime += frameTime;
if (respirator.AccumulatedFrametime < 1)
{
if (respirator.AccumulatedFrametime < respirator.CycleDelay)
continue;
respirator.AccumulatedFrametime -= respirator.CycleDelay;
UpdateSaturation(respirator.Owner, -respirator.CycleDelay, respirator);
switch (respirator.Status)
{
case RespiratorStatus.Inhaling:
Inhale(uid, body);
respirator.Status = RespiratorStatus.Exhaling;
break;
case RespiratorStatus.Exhaling:
Exhale(uid, body);
respirator.Status = RespiratorStatus.Inhaling;
break;
}
ProcessGases(uid, respirator, blood, body);
respirator.AccumulatedFrametime -= 1;
if (SuffocatingPercentage(respirator) > 0)
if (respirator.Saturation < respirator.SuffocationThreshold)
{
if (_gameTiming.CurTime >= respirator.LastGaspPopupTime + respirator.GaspPopupCooldown)
{
respirator.LastGaspPopupTime = _gameTiming.CurTime;
_popupSystem.PopupEntity(Loc.GetString("lung-behavior-gasp"), uid, Filter.Pvs(uid));
}
TakeSuffocationDamage(uid, respirator);
respirator.SuffocationCycles += 1;
continue;
}
StopSuffocation(uid, respirator);
respirator.SuffocationCycles = 0;
}
}
private Dictionary<Gas, float> NeedsAndDeficit(RespiratorComponent respirator)
public void Inhale(EntityUid uid, SharedBodyComponent? body=null)
{
var needs = new Dictionary<Gas, float>(respirator.NeedsGases);
foreach (var (gas, amount) in respirator.DeficitGases)
{
var newAmount = (needs.GetValueOrDefault(gas) + amount);
needs[gas] = newAmount;
}
return needs;
}
private void ClampDeficit(RespiratorComponent respirator)
{
var deficitGases = new Dictionary<Gas, float>(respirator.DeficitGases);
foreach (var (gas, deficit) in deficitGases)
{
if (!respirator.NeedsGases.TryGetValue(gas, out var need))
{
respirator.DeficitGases.Remove(gas);
continue;
}
if (deficit > need)
{
respirator.DeficitGases[gas] = need;
}
}
}
private float SuffocatingPercentage(RespiratorComponent respirator)
{
var total = 0f;
foreach (var (gas, deficit) in respirator.DeficitGases)
{
var lack = 1f;
if (respirator.NeedsGases.TryGetValue(gas, out var needed))
{
lack = deficit / needed;
}
total += lack / Atmospherics.TotalNumberOfGases;
}
return total;
}
private float GasProducedMultiplier(RespiratorComponent respirator, Gas gas, float usedAverage)
{
if (!respirator.ProducesGases.TryGetValue(gas, out var produces))
{
return 0;
}
if (!respirator.NeedsGases.TryGetValue(gas, out var needs))
{
needs = 1;
}
return needs * produces * usedAverage;
}
private Dictionary<Gas, float> GasProduced(RespiratorComponent respirator, float usedAverage)
{
return respirator.ProducesGases.ToDictionary(pair => pair.Key, pair => GasProducedMultiplier(respirator, pair.Key, usedAverage));
}
private void ProcessGases(EntityUid uid, RespiratorComponent respirator,
BloodstreamComponent? bloodstream,
SharedBodyComponent? body)
{
if (!Resolve(uid, ref bloodstream, ref body, false))
if (!Resolve(uid, ref body, false))
return;
var lungs = _bodySystem.GetComponentsOnMechanisms<LungComponent>(uid, body).ToArray();
var organs = _bodySystem.GetComponentsOnMechanisms<LungComponent>(uid, body).ToArray();
var needs = NeedsAndDeficit(respirator);
var used = 0f;
// Inhale gas
var ev = new InhaleLocationEvent();
RaiseLocalEvent(uid, ev, false);
foreach (var (lung, mech) in lungs)
if (ev.Gas == null)
{
_lungSystem.UpdateLung(lung.Owner, lung, mech);
ev.Gas = _atmosSys.GetTileMixture(Transform(uid).Coordinates);
if (ev.Gas == null) return;
}
foreach (var (gas, amountNeeded) in needs)
var ratio = (Atmospherics.BreathVolume / ev.Gas.Volume);
var actualGas = ev.Gas.RemoveRatio(ratio);
var lungRatio = 1.0f / organs.Length;
var gas = organs.Length == 1 ? actualGas : actualGas.RemoveRatio(lungRatio);
foreach (var (lung, _) in organs)
{
var bloodstreamAmount = bloodstream.Air.GetMoles(gas);
var deficit = 0f;
// Merge doesn't remove gas from the giver.
_atmosSys.Merge(lung.Air, gas);
_lungSystem.GasToReagent(lung.Owner, lung);
}
}
if (bloodstreamAmount < amountNeeded)
{
// Panic inhale
foreach (var (lung, mech) in lungs)
{
_lungSystem.Gasp((lung).Owner, lung, mech);
}
public void Exhale(EntityUid uid, SharedBodyComponent? body=null)
{
if (!Resolve(uid, ref body, false))
return;
bloodstreamAmount = bloodstream.Air.GetMoles(gas);
var organs = _bodySystem.GetComponentsOnMechanisms<LungComponent>(uid, body).ToArray();
deficit = Math.Max(0, amountNeeded - bloodstreamAmount);
// exhale gas
if (deficit > 0)
{
bloodstream.Air.SetMoles(gas, 0);
}
else
{
bloodstream.Air.AdjustMoles(gas, -amountNeeded);
}
}
else
{
bloodstream.Air.AdjustMoles(gas, -amountNeeded);
}
var ev = new ExhaleLocationEvent();
RaiseLocalEvent(uid, ev, false);
respirator.DeficitGases[gas] = deficit;
used += (amountNeeded - deficit) / amountNeeded;
if (ev.Gas == null)
{
ev.Gas = _atmosSys.GetTileMixture(Transform(uid).Coordinates);
if (ev.Gas == null) return;
}
var produced = GasProduced(respirator, used / needs.Count);
foreach (var (gas, amountProduced) in produced)
var outGas = new GasMixture(ev.Gas.Volume);
foreach (var (lung, _) in organs)
{
bloodstream.Air.AdjustMoles(gas, amountProduced);
_atmosSys.Merge(outGas, lung.Air);
lung.Air.Clear();
lung.LungSolution.RemoveAllSolution();
}
ClampDeficit(respirator);
_atmosSys.Merge(ev.Gas, outGas);
}
private void TakeSuffocationDamage(EntityUid uid, RespiratorComponent respirator)
{
if (!respirator.Suffocating)
if (respirator.SuffocationCycles == 2)
_logSys.Add(LogType.Asphyxiation, $"{ToPrettyString(uid):entity} started suffocating");
respirator.Suffocating = true;
_alertsSystem.ShowAlert(uid, AlertType.LowOxygen);
if (respirator.SuffocationCycles >= respirator.SuffocationCycleThreshold)
{
_alertsSystem.ShowAlert(uid, AlertType.LowOxygen);
}
_damageableSys.TryChangeDamage(uid, respirator.Damage, true, false);
}
private void StopSuffocation(EntityUid uid, RespiratorComponent respirator)
{
if (respirator.Suffocating)
if (respirator.SuffocationCycles >= 2)
_logSys.Add(LogType.Asphyxiation, $"{ToPrettyString(uid):entity} stopped suffocating");
respirator.Suffocating = false;
_alertsSystem.ClearAlert(uid, AlertType.LowOxygen);
_damageableSys.TryChangeDamage(uid, respirator.DamageRecovery, true);
}
public GasMixture Clean(EntityUid uid, RespiratorComponent respirator, BloodstreamComponent bloodstream)
public void UpdateSaturation(EntityUid uid, float amount,
RespiratorComponent? respirator = null)
{
var gasMixture = new GasMixture(bloodstream.Air.Volume)
{
Temperature = bloodstream.Air.Temperature
};
if (!Resolve(uid, ref respirator, false))
return;
for (Gas gas = 0; gas < (Gas) Atmospherics.TotalNumberOfGases; gas++)
{
float amount;
var molesInBlood = bloodstream.Air.GetMoles(gas);
if (!respirator.NeedsGases.TryGetValue(gas, out var needed))
{
amount = molesInBlood;
}
else
{
var overflowThreshold = needed * 5f;
amount = molesInBlood > overflowThreshold
? molesInBlood - overflowThreshold
: 0;
}
gasMixture.AdjustMoles(gas, amount);
bloodstream.Air.AdjustMoles(gas, -amount);
}
return gasMixture;
respirator.Saturation += amount;
respirator.Saturation =
Math.Clamp(respirator.Saturation, respirator.MinSaturation, respirator.MaxSaturation);
}
}
}
public class InhaleLocationEvent : EntityEventArgs
{
public GasMixture? Gas;
}
public class ExhaleLocationEvent : EntityEventArgs
{
public GasMixture? Gas;
}