Suffocation alerts for nitrogen breathers (#24373)
* Respiratorsystem namespace * WIP gas alert update, does not work * Finally
This commit is contained in:
@@ -13,202 +13,211 @@ using Content.Shared.Mobs.Systems;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.Timing;
|
||||
|
||||
namespace Content.Server.Body.Systems
|
||||
namespace Content.Server.Body.Systems;
|
||||
|
||||
[UsedImplicitly]
|
||||
public sealed class RespiratorSystem : EntitySystem
|
||||
{
|
||||
[UsedImplicitly]
|
||||
public sealed class RespiratorSystem : EntitySystem
|
||||
[Dependency] private readonly IAdminLogManager _adminLogger = default!;
|
||||
[Dependency] private readonly IGameTiming _gameTiming = default!;
|
||||
[Dependency] private readonly AlertsSystem _alertsSystem = default!;
|
||||
[Dependency] private readonly AtmosphereSystem _atmosSys = default!;
|
||||
[Dependency] private readonly BodySystem _bodySystem = default!;
|
||||
[Dependency] private readonly DamageableSystem _damageableSys = default!;
|
||||
[Dependency] private readonly LungSystem _lungSystem = default!;
|
||||
[Dependency] private readonly PopupSystem _popupSystem = default!;
|
||||
[Dependency] private readonly MobStateSystem _mobState = default!;
|
||||
[Dependency] private readonly SolutionContainerSystem _solutionContainerSystem = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
[Dependency] private readonly IAdminLogManager _adminLogger = default!;
|
||||
[Dependency] private readonly IGameTiming _gameTiming = default!;
|
||||
[Dependency] private readonly AlertsSystem _alertsSystem = default!;
|
||||
[Dependency] private readonly AtmosphereSystem _atmosSys = default!;
|
||||
[Dependency] private readonly BodySystem _bodySystem = default!;
|
||||
[Dependency] private readonly DamageableSystem _damageableSys = default!;
|
||||
[Dependency] private readonly LungSystem _lungSystem = default!;
|
||||
[Dependency] private readonly PopupSystem _popupSystem = default!;
|
||||
[Dependency] private readonly MobStateSystem _mobState = default!;
|
||||
[Dependency] private readonly SolutionContainerSystem _solutionContainerSystem = default!;
|
||||
base.Initialize();
|
||||
|
||||
public override void Initialize()
|
||||
// We want to process lung reagents before we inhale new reagents.
|
||||
UpdatesAfter.Add(typeof(MetabolizerSystem));
|
||||
SubscribeLocalEvent<RespiratorComponent, ApplyMetabolicMultiplierEvent>(OnApplyMetabolicMultiplier);
|
||||
}
|
||||
|
||||
public override void Update(float frameTime)
|
||||
{
|
||||
base.Update(frameTime);
|
||||
|
||||
var query = EntityQueryEnumerator<RespiratorComponent, BodyComponent>();
|
||||
while (query.MoveNext(out var uid, out var respirator, out var body))
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
// We want to process lung reagents before we inhale new reagents.
|
||||
UpdatesAfter.Add(typeof(MetabolizerSystem));
|
||||
SubscribeLocalEvent<RespiratorComponent, ApplyMetabolicMultiplierEvent>(OnApplyMetabolicMultiplier);
|
||||
}
|
||||
|
||||
public override void Update(float frameTime)
|
||||
{
|
||||
base.Update(frameTime);
|
||||
|
||||
var query = EntityQueryEnumerator<RespiratorComponent, BodyComponent>();
|
||||
while (query.MoveNext(out var uid, out var respirator, out var body))
|
||||
if (_mobState.IsDead(uid))
|
||||
{
|
||||
if (_mobState.IsDead(uid))
|
||||
continue;
|
||||
}
|
||||
|
||||
respirator.AccumulatedFrametime += frameTime;
|
||||
|
||||
if (respirator.AccumulatedFrametime < respirator.CycleDelay)
|
||||
continue;
|
||||
respirator.AccumulatedFrametime -= respirator.CycleDelay;
|
||||
UpdateSaturation(uid, -respirator.CycleDelay, respirator);
|
||||
|
||||
if (!_mobState.IsIncapacitated(uid)) // cannot breathe in crit.
|
||||
{
|
||||
switch (respirator.Status)
|
||||
{
|
||||
continue;
|
||||
case RespiratorStatus.Inhaling:
|
||||
Inhale(uid, body);
|
||||
respirator.Status = RespiratorStatus.Exhaling;
|
||||
break;
|
||||
case RespiratorStatus.Exhaling:
|
||||
Exhale(uid, body);
|
||||
respirator.Status = RespiratorStatus.Inhaling;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (respirator.Saturation < respirator.SuffocationThreshold)
|
||||
{
|
||||
if (_gameTiming.CurTime >= respirator.LastGaspPopupTime + respirator.GaspPopupCooldown)
|
||||
{
|
||||
respirator.LastGaspPopupTime = _gameTiming.CurTime;
|
||||
_popupSystem.PopupEntity(Loc.GetString("lung-behavior-gasp"), uid);
|
||||
}
|
||||
|
||||
respirator.AccumulatedFrametime += frameTime;
|
||||
|
||||
if (respirator.AccumulatedFrametime < respirator.CycleDelay)
|
||||
continue;
|
||||
respirator.AccumulatedFrametime -= respirator.CycleDelay;
|
||||
UpdateSaturation(uid, -respirator.CycleDelay, respirator);
|
||||
|
||||
if (!_mobState.IsIncapacitated(uid)) // cannot breathe in crit.
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
if (respirator.Saturation < respirator.SuffocationThreshold)
|
||||
{
|
||||
if (_gameTiming.CurTime >= respirator.LastGaspPopupTime + respirator.GaspPopupCooldown)
|
||||
{
|
||||
respirator.LastGaspPopupTime = _gameTiming.CurTime;
|
||||
_popupSystem.PopupEntity(Loc.GetString("lung-behavior-gasp"), uid);
|
||||
}
|
||||
|
||||
TakeSuffocationDamage(uid, respirator);
|
||||
respirator.SuffocationCycles += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
StopSuffocation(uid, respirator);
|
||||
respirator.SuffocationCycles = 0;
|
||||
TakeSuffocationDamage(uid, respirator);
|
||||
respirator.SuffocationCycles += 1;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
public void Inhale(EntityUid uid, BodyComponent? body = null)
|
||||
StopSuffocation(uid, respirator);
|
||||
respirator.SuffocationCycles = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public void Inhale(EntityUid uid, BodyComponent? body = null)
|
||||
{
|
||||
if (!Resolve(uid, ref body, false))
|
||||
return;
|
||||
|
||||
var organs = _bodySystem.GetBodyOrganComponents<LungComponent>(uid, body);
|
||||
|
||||
// Inhale gas
|
||||
var ev = new InhaleLocationEvent();
|
||||
RaiseLocalEvent(uid, ev);
|
||||
|
||||
ev.Gas ??= _atmosSys.GetContainingMixture(uid, false, true);
|
||||
|
||||
if (ev.Gas == null)
|
||||
{
|
||||
if (!Resolve(uid, ref body, false))
|
||||
return;
|
||||
|
||||
var organs = _bodySystem.GetBodyOrganComponents<LungComponent>(uid, body);
|
||||
|
||||
// Inhale gas
|
||||
var ev = new InhaleLocationEvent();
|
||||
RaiseLocalEvent(uid, ev);
|
||||
|
||||
ev.Gas ??= _atmosSys.GetContainingMixture(uid, false, true);
|
||||
|
||||
if (ev.Gas == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var actualGas = ev.Gas.RemoveVolume(Atmospherics.BreathVolume);
|
||||
|
||||
var lungRatio = 1.0f / organs.Count;
|
||||
var gas = organs.Count == 1 ? actualGas : actualGas.RemoveRatio(lungRatio);
|
||||
foreach (var (lung, _) in organs)
|
||||
{
|
||||
// Merge doesn't remove gas from the giver.
|
||||
_atmosSys.Merge(lung.Air, gas);
|
||||
_lungSystem.GasToReagent(lung.Owner, lung);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
public void Exhale(EntityUid uid, BodyComponent? body = null)
|
||||
var actualGas = ev.Gas.RemoveVolume(Atmospherics.BreathVolume);
|
||||
|
||||
var lungRatio = 1.0f / organs.Count;
|
||||
var gas = organs.Count == 1 ? actualGas : actualGas.RemoveRatio(lungRatio);
|
||||
foreach (var (lung, _) in organs)
|
||||
{
|
||||
if (!Resolve(uid, ref body, false))
|
||||
return;
|
||||
|
||||
var organs = _bodySystem.GetBodyOrganComponents<LungComponent>(uid, body);
|
||||
|
||||
// exhale gas
|
||||
|
||||
var ev = new ExhaleLocationEvent();
|
||||
RaiseLocalEvent(uid, ev, false);
|
||||
|
||||
if (ev.Gas == null)
|
||||
{
|
||||
ev.Gas = _atmosSys.GetContainingMixture(uid, false, true);
|
||||
|
||||
// Walls and grids without atmos comp return null. I guess it makes sense to not be able to exhale in walls,
|
||||
// but this also means you cannot exhale on some grids.
|
||||
ev.Gas ??= GasMixture.SpaceGas;
|
||||
}
|
||||
|
||||
var outGas = new GasMixture(ev.Gas.Volume);
|
||||
foreach (var (lung, _) in organs)
|
||||
{
|
||||
_atmosSys.Merge(outGas, lung.Air);
|
||||
lung.Air.Clear();
|
||||
|
||||
if (_solutionContainerSystem.ResolveSolution(lung.Owner, lung.SolutionName, ref lung.Solution))
|
||||
_solutionContainerSystem.RemoveAllSolution(lung.Solution.Value);
|
||||
}
|
||||
|
||||
_atmosSys.Merge(ev.Gas, outGas);
|
||||
// Merge doesn't remove gas from the giver.
|
||||
_atmosSys.Merge(lung.Air, gas);
|
||||
_lungSystem.GasToReagent(lung.Owner, lung);
|
||||
}
|
||||
}
|
||||
|
||||
private void TakeSuffocationDamage(EntityUid uid, RespiratorComponent respirator)
|
||||
public void Exhale(EntityUid uid, BodyComponent? body = null)
|
||||
{
|
||||
if (!Resolve(uid, ref body, false))
|
||||
return;
|
||||
|
||||
var organs = _bodySystem.GetBodyOrganComponents<LungComponent>(uid, body);
|
||||
|
||||
// exhale gas
|
||||
|
||||
var ev = new ExhaleLocationEvent();
|
||||
RaiseLocalEvent(uid, ev, false);
|
||||
|
||||
if (ev.Gas == null)
|
||||
{
|
||||
if (respirator.SuffocationCycles == 2)
|
||||
_adminLogger.Add(LogType.Asphyxiation, $"{ToPrettyString(uid):entity} started suffocating");
|
||||
ev.Gas = _atmosSys.GetContainingMixture(uid, false, true);
|
||||
|
||||
if (respirator.SuffocationCycles >= respirator.SuffocationCycleThreshold)
|
||||
// Walls and grids without atmos comp return null. I guess it makes sense to not be able to exhale in walls,
|
||||
// but this also means you cannot exhale on some grids.
|
||||
ev.Gas ??= GasMixture.SpaceGas;
|
||||
}
|
||||
|
||||
var outGas = new GasMixture(ev.Gas.Volume);
|
||||
foreach (var (lung, _) in organs)
|
||||
{
|
||||
_atmosSys.Merge(outGas, lung.Air);
|
||||
lung.Air.Clear();
|
||||
|
||||
if (_solutionContainerSystem.ResolveSolution(lung.Owner, lung.SolutionName, ref lung.Solution))
|
||||
_solutionContainerSystem.RemoveAllSolution(lung.Solution.Value);
|
||||
}
|
||||
|
||||
_atmosSys.Merge(ev.Gas, outGas);
|
||||
}
|
||||
|
||||
private void TakeSuffocationDamage(EntityUid uid, RespiratorComponent respirator)
|
||||
{
|
||||
if (respirator.SuffocationCycles == 2)
|
||||
_adminLogger.Add(LogType.Asphyxiation, $"{ToPrettyString(uid):entity} started suffocating");
|
||||
|
||||
if (respirator.SuffocationCycles >= respirator.SuffocationCycleThreshold)
|
||||
{
|
||||
// TODO: This is not going work with multiple different lungs, if that ever becomes a possibility
|
||||
var organs = _bodySystem.GetBodyOrganComponents<LungComponent>(uid);
|
||||
foreach (var (comp, _) in organs)
|
||||
{
|
||||
_alertsSystem.ShowAlert(uid, AlertType.LowOxygen);
|
||||
_alertsSystem.ShowAlert(uid, comp.Alert);
|
||||
}
|
||||
|
||||
_damageableSys.TryChangeDamage(uid, respirator.Damage, false, false);
|
||||
}
|
||||
|
||||
private void StopSuffocation(EntityUid uid, RespiratorComponent respirator)
|
||||
_damageableSys.TryChangeDamage(uid, respirator.Damage, false, false);
|
||||
}
|
||||
|
||||
private void StopSuffocation(EntityUid uid, RespiratorComponent respirator)
|
||||
{
|
||||
if (respirator.SuffocationCycles >= 2)
|
||||
_adminLogger.Add(LogType.Asphyxiation, $"{ToPrettyString(uid):entity} stopped suffocating");
|
||||
|
||||
// TODO: This is not going work with multiple different lungs, if that ever becomes a possibility
|
||||
var organs = _bodySystem.GetBodyOrganComponents<LungComponent>(uid);
|
||||
foreach (var (comp, _) in organs)
|
||||
{
|
||||
if (respirator.SuffocationCycles >= 2)
|
||||
_adminLogger.Add(LogType.Asphyxiation, $"{ToPrettyString(uid):entity} stopped suffocating");
|
||||
|
||||
_alertsSystem.ClearAlert(uid, AlertType.LowOxygen);
|
||||
|
||||
_damageableSys.TryChangeDamage(uid, respirator.DamageRecovery);
|
||||
_alertsSystem.ClearAlert(uid, comp.Alert);
|
||||
}
|
||||
|
||||
public void UpdateSaturation(EntityUid uid, float amount,
|
||||
RespiratorComponent? respirator = null)
|
||||
{
|
||||
if (!Resolve(uid, ref respirator, false))
|
||||
return;
|
||||
_damageableSys.TryChangeDamage(uid, respirator.DamageRecovery);
|
||||
}
|
||||
|
||||
respirator.Saturation += amount;
|
||||
respirator.Saturation =
|
||||
Math.Clamp(respirator.Saturation, respirator.MinSaturation, respirator.MaxSaturation);
|
||||
}
|
||||
public void UpdateSaturation(EntityUid uid, float amount,
|
||||
RespiratorComponent? respirator = null)
|
||||
{
|
||||
if (!Resolve(uid, ref respirator, false))
|
||||
return;
|
||||
|
||||
private void OnApplyMetabolicMultiplier(EntityUid uid, RespiratorComponent component,
|
||||
respirator.Saturation += amount;
|
||||
respirator.Saturation =
|
||||
Math.Clamp(respirator.Saturation, respirator.MinSaturation, respirator.MaxSaturation);
|
||||
}
|
||||
|
||||
private void OnApplyMetabolicMultiplier(EntityUid uid, RespiratorComponent component,
|
||||
ApplyMetabolicMultiplierEvent args)
|
||||
{
|
||||
if (args.Apply)
|
||||
{
|
||||
if (args.Apply)
|
||||
{
|
||||
component.CycleDelay *= args.Multiplier;
|
||||
component.Saturation *= args.Multiplier;
|
||||
component.MaxSaturation *= args.Multiplier;
|
||||
component.MinSaturation *= args.Multiplier;
|
||||
return;
|
||||
}
|
||||
|
||||
// This way we don't have to worry about it breaking if the stasis bed component is destroyed
|
||||
component.CycleDelay /= args.Multiplier;
|
||||
component.Saturation /= args.Multiplier;
|
||||
component.MaxSaturation /= args.Multiplier;
|
||||
component.MinSaturation /= args.Multiplier;
|
||||
// Reset the accumulator properly
|
||||
if (component.AccumulatedFrametime >= component.CycleDelay)
|
||||
component.AccumulatedFrametime = component.CycleDelay;
|
||||
component.CycleDelay *= args.Multiplier;
|
||||
component.Saturation *= args.Multiplier;
|
||||
component.MaxSaturation *= args.Multiplier;
|
||||
component.MinSaturation *= args.Multiplier;
|
||||
return;
|
||||
}
|
||||
|
||||
// This way we don't have to worry about it breaking if the stasis bed component is destroyed
|
||||
component.CycleDelay /= args.Multiplier;
|
||||
component.Saturation /= args.Multiplier;
|
||||
component.MaxSaturation /= args.Multiplier;
|
||||
component.MinSaturation /= args.Multiplier;
|
||||
// Reset the accumulator properly
|
||||
if (component.AccumulatedFrametime >= component.CycleDelay)
|
||||
component.AccumulatedFrametime = component.CycleDelay;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user