2025-05-02 18:22:29 +10:00
|
|
|
|
using Content.Shared.Alert;
|
|
|
|
|
|
using Content.Shared.Atmos.Components;
|
|
|
|
|
|
using Content.Shared.Atmos.EntitySystems;
|
|
|
|
|
|
using Content.Shared.Body.Components;
|
|
|
|
|
|
using Content.Shared.DoAfter;
|
|
|
|
|
|
using Content.Shared.Hands.Components;
|
2025-06-02 12:09:45 +01:00
|
|
|
|
using Content.Shared.IdentityManagement;
|
2025-05-02 18:22:29 +10:00
|
|
|
|
using Content.Shared.Internals;
|
|
|
|
|
|
using Content.Shared.Inventory;
|
|
|
|
|
|
using Content.Shared.Popups;
|
|
|
|
|
|
using Content.Shared.Verbs;
|
|
|
|
|
|
using Robust.Shared.Containers;
|
|
|
|
|
|
using Robust.Shared.Utility;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Content.Shared.Body.Systems;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Handles lung breathing with gas tanks for entities.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public abstract class SharedInternalsSystem : EntitySystem
|
|
|
|
|
|
{
|
|
|
|
|
|
[Dependency] private readonly AlertsSystem _alerts = default!;
|
|
|
|
|
|
[Dependency] private readonly InventorySystem _inventory = default!;
|
|
|
|
|
|
[Dependency] private readonly SharedDoAfterSystem _doAfter = default!;
|
|
|
|
|
|
[Dependency] private readonly SharedGasTankSystem _gasTank = default!;
|
|
|
|
|
|
[Dependency] private readonly SharedPopupSystem _popupSystem = default!;
|
|
|
|
|
|
|
|
|
|
|
|
public override void Initialize()
|
|
|
|
|
|
{
|
|
|
|
|
|
base.Initialize();
|
|
|
|
|
|
SubscribeLocalEvent<InternalsComponent, GetVerbsEvent<InteractionVerb>>(OnGetInteractionVerbs);
|
|
|
|
|
|
|
|
|
|
|
|
SubscribeLocalEvent<InternalsComponent, ComponentStartup>(OnInternalsStartup);
|
|
|
|
|
|
SubscribeLocalEvent<InternalsComponent, ComponentShutdown>(OnInternalsShutdown);
|
|
|
|
|
|
|
|
|
|
|
|
SubscribeLocalEvent<InternalsComponent, InternalsDoAfterEvent>(OnDoAfter);
|
|
|
|
|
|
SubscribeLocalEvent<InternalsComponent, ToggleInternalsAlertEvent>(OnToggleInternalsAlert);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OnGetInteractionVerbs(
|
|
|
|
|
|
Entity<InternalsComponent> ent,
|
|
|
|
|
|
ref GetVerbsEvent<InteractionVerb> args)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!args.CanAccess || !args.CanInteract || args.Hands is null)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
if (!AreInternalsWorking(ent) && ent.Comp.BreathTools.Count == 0)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
var user = args.User;
|
|
|
|
|
|
|
|
|
|
|
|
InteractionVerb verb = new()
|
|
|
|
|
|
{
|
|
|
|
|
|
Icon = new SpriteSpecifier.Texture(new("/Textures/Interface/VerbIcons/dot.svg.192dpi.png")),
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-06-02 12:09:45 +01:00
|
|
|
|
if (AreInternalsWorking(ent))
|
|
|
|
|
|
{
|
|
|
|
|
|
verb.Act = () => ToggleInternals(ent, user, force: false, ent, ToggleMode.Off);
|
|
|
|
|
|
verb.Message = Loc.GetString("action-description-internals-toggle-off");
|
|
|
|
|
|
verb.Text = Loc.GetString("action-name-internals-toggle-off");
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
verb.Act = () => ToggleInternals(ent, user, force: false, ent, ToggleMode.On);
|
|
|
|
|
|
verb.Message = Loc.GetString("action-description-internals-toggle-on");
|
|
|
|
|
|
verb.Text = Loc.GetString("action-name-internals-toggle-on");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-05-02 18:22:29 +10:00
|
|
|
|
args.Verbs.Add(verb);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-02 12:09:45 +01:00
|
|
|
|
protected bool ToggleInternals(
|
|
|
|
|
|
EntityUid target,
|
2025-05-02 18:22:29 +10:00
|
|
|
|
EntityUid user,
|
|
|
|
|
|
bool force,
|
2025-06-02 12:09:45 +01:00
|
|
|
|
InternalsComponent? internals = null,
|
|
|
|
|
|
ToggleMode mode = ToggleMode.Toggle)
|
2025-05-02 18:22:29 +10:00
|
|
|
|
{
|
2025-06-02 12:09:45 +01:00
|
|
|
|
if (!Resolve(target, ref internals, logMissing: false))
|
2025-05-02 18:22:29 +10:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
// Check if a mask is present.
|
|
|
|
|
|
if (internals.BreathTools.Count == 0)
|
|
|
|
|
|
{
|
2025-06-02 12:09:45 +01:00
|
|
|
|
var message = user == target ? Loc.GetString("internals-self-no-breath-tool") : Loc.GetString("internals-other-no-breath-tool", ("ent", Identity.Name(target, EntityManager, user)));
|
|
|
|
|
|
_popupSystem.PopupClient(message, target, user);
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Check if tank is present.
|
|
|
|
|
|
var tank = FindBestGasTank(target);
|
|
|
|
|
|
|
|
|
|
|
|
// If they're not on then check if we have a mask to use
|
|
|
|
|
|
if (tank == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
var message = user == target ? Loc.GetString("internals-self-no-tank") : Loc.GetString("internals-other-no-tank", ("ent", Identity.Name(target, EntityManager, user)));
|
|
|
|
|
|
_popupSystem.PopupClient(message, target, user);
|
2025-05-02 18:22:29 +10:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Start the toggle do-after if it's on someone else.
|
2025-06-02 12:09:45 +01:00
|
|
|
|
if (!force && user != target)
|
2025-05-02 18:22:29 +10:00
|
|
|
|
{
|
2025-06-02 12:09:45 +01:00
|
|
|
|
return StartToggleInternalsDoAfter(user, (target, internals), mode);
|
2025-05-02 18:22:29 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Toggle off.
|
|
|
|
|
|
if (TryComp(internals.GasTankEntity, out GasTankComponent? gas))
|
|
|
|
|
|
{
|
2025-06-02 12:09:45 +01:00
|
|
|
|
if (mode == ToggleMode.On)
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
2025-05-02 18:22:29 +10:00
|
|
|
|
return _gasTank.DisconnectFromInternals((internals.GasTankEntity.Value, gas), user);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-02 12:09:45 +01:00
|
|
|
|
// No tank was connected, we’ll try to toggle internals on
|
2025-05-02 18:22:29 +10:00
|
|
|
|
|
2025-06-02 12:09:45 +01:00
|
|
|
|
// If the intent was to disable internals there’s nothing left to do
|
|
|
|
|
|
if (mode == ToggleMode.Off)
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
return _gasTank.ConnectToInternals(tank.Value, user: user);
|
2025-05-02 18:22:29 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-02 12:09:45 +01:00
|
|
|
|
private bool StartToggleInternalsDoAfter(EntityUid user, Entity<InternalsComponent> targetEnt, ToggleMode mode)
|
2025-05-02 18:22:29 +10:00
|
|
|
|
{
|
|
|
|
|
|
// Is the target not you? If yes, use a do-after to give them time to respond.
|
|
|
|
|
|
var isUser = user == targetEnt.Owner;
|
|
|
|
|
|
var delay = !isUser ? targetEnt.Comp.Delay : TimeSpan.Zero;
|
|
|
|
|
|
|
2025-06-02 12:09:45 +01:00
|
|
|
|
return _doAfter.TryStartDoAfter(
|
|
|
|
|
|
new DoAfterArgs(EntityManager, user, delay, new InternalsDoAfterEvent(mode), targetEnt, target: targetEnt)
|
|
|
|
|
|
{
|
|
|
|
|
|
BreakOnDamage = true,
|
|
|
|
|
|
BreakOnMove = true,
|
|
|
|
|
|
MovementThreshold = 0.1f,
|
|
|
|
|
|
});
|
2025-05-02 18:22:29 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OnDoAfter(Entity<InternalsComponent> ent, ref InternalsDoAfterEvent args)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (args.Cancelled || args.Handled)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
2025-06-02 12:09:45 +01:00
|
|
|
|
ToggleInternals(ent, args.User, force: true, ent, args.ToggleMode);
|
2025-05-02 18:22:29 +10:00
|
|
|
|
|
|
|
|
|
|
args.Handled = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OnToggleInternalsAlert(Entity<InternalsComponent> ent, ref ToggleInternalsAlertEvent args)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (args.Handled)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
args.Handled |= ToggleInternals(ent, ent, false, internals: ent.Comp);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OnInternalsStartup(Entity<InternalsComponent> ent, ref ComponentStartup args)
|
|
|
|
|
|
{
|
2025-09-05 02:45:48 -07:00
|
|
|
|
_alerts.ShowAlert(ent.Owner, ent.Comp.InternalsAlert, GetSeverity(ent));
|
2025-05-02 18:22:29 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OnInternalsShutdown(Entity<InternalsComponent> ent, ref ComponentShutdown args)
|
|
|
|
|
|
{
|
2025-09-05 02:45:48 -07:00
|
|
|
|
_alerts.ClearAlert(ent.Owner, ent.Comp.InternalsAlert);
|
2025-05-02 18:22:29 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void ConnectBreathTool(Entity<InternalsComponent> ent, EntityUid toolEntity)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!ent.Comp.BreathTools.Add(toolEntity))
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
if (TryComp(toolEntity, out BreathToolComponent? breathTool))
|
|
|
|
|
|
{
|
|
|
|
|
|
breathTool.ConnectedInternalsEntity = ent.Owner;
|
|
|
|
|
|
Dirty(toolEntity, breathTool);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Dirty(ent);
|
2025-09-05 02:45:48 -07:00
|
|
|
|
_alerts.ShowAlert(ent.Owner, ent.Comp.InternalsAlert, GetSeverity(ent));
|
2025-05-02 18:22:29 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void DisconnectBreathTool(Entity<InternalsComponent> ent, EntityUid toolEntity, bool forced = false)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!ent.Comp.BreathTools.Remove(toolEntity))
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
Dirty(ent);
|
|
|
|
|
|
|
|
|
|
|
|
if (TryComp(toolEntity, out BreathToolComponent? breathTool))
|
|
|
|
|
|
{
|
|
|
|
|
|
breathTool.ConnectedInternalsEntity = null;
|
|
|
|
|
|
Dirty(toolEntity, breathTool);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (ent.Comp.BreathTools.Count == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
DisconnectTank(ent, forced: forced);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-05 02:45:48 -07:00
|
|
|
|
_alerts.ShowAlert(ent.Owner, ent.Comp.InternalsAlert, GetSeverity(ent));
|
2025-05-02 18:22:29 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void DisconnectTank(Entity<InternalsComponent> ent, bool forced = false)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (TryComp(ent.Comp.GasTankEntity, out GasTankComponent? tank))
|
|
|
|
|
|
_gasTank.DisconnectFromInternals((ent.Comp.GasTankEntity.Value, tank), forced: forced);
|
|
|
|
|
|
|
|
|
|
|
|
ent.Comp.GasTankEntity = null;
|
|
|
|
|
|
Dirty(ent);
|
|
|
|
|
|
_alerts.ShowAlert(ent.Owner, ent.Comp.InternalsAlert, GetSeverity(ent.Comp));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public bool TryConnectTank(Entity<InternalsComponent> ent, EntityUid tankEntity)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (ent.Comp.BreathTools.Count == 0)
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
if (TryComp(ent.Comp.GasTankEntity, out GasTankComponent? tank))
|
|
|
|
|
|
_gasTank.DisconnectFromInternals((ent.Comp.GasTankEntity.Value, tank));
|
|
|
|
|
|
|
|
|
|
|
|
ent.Comp.GasTankEntity = tankEntity;
|
|
|
|
|
|
Dirty(ent);
|
2025-09-05 02:45:48 -07:00
|
|
|
|
_alerts.ShowAlert(ent.Owner, ent.Comp.InternalsAlert, GetSeverity(ent));
|
2025-05-02 18:22:29 +10:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public bool AreInternalsWorking(EntityUid uid, InternalsComponent? component = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Resolve(uid, ref component, logMissing: false)
|
|
|
|
|
|
&& AreInternalsWorking(component);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public bool AreInternalsWorking(InternalsComponent component)
|
|
|
|
|
|
{
|
|
|
|
|
|
return TryComp(component.BreathTools.FirstOrNull(), out BreathToolComponent? breathTool)
|
|
|
|
|
|
&& breathTool.IsFunctional
|
|
|
|
|
|
&& HasComp<GasTankComponent>(component.GasTankEntity);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected short GetSeverity(InternalsComponent component)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (component.BreathTools.Count == 0 || !AreInternalsWorking(component))
|
|
|
|
|
|
return 2;
|
|
|
|
|
|
|
|
|
|
|
|
// If pressure in the tank is below low pressure threshold, flash warning on internals UI
|
|
|
|
|
|
if (TryComp<GasTankComponent>(component.GasTankEntity, out var gasTank)
|
|
|
|
|
|
&& gasTank.IsLowPressure)
|
|
|
|
|
|
{
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Entity<GasTankComponent>? FindBestGasTank(
|
|
|
|
|
|
Entity<HandsComponent?, InventoryComponent?, ContainerManagerComponent?> user)
|
|
|
|
|
|
{
|
|
|
|
|
|
// TODO use _respirator.CanMetabolizeGas() to prioritize metabolizable gasses
|
|
|
|
|
|
// Prioritise
|
|
|
|
|
|
// 1. back equipped tanks
|
|
|
|
|
|
// 2. exo-slot tanks
|
|
|
|
|
|
// 3. in-hand tanks
|
|
|
|
|
|
// 4. pocket/belt tanks
|
|
|
|
|
|
|
|
|
|
|
|
if (!Resolve(user, ref user.Comp2, ref user.Comp3))
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
|
|
if (_inventory.TryGetSlotEntity(user, "back", out var backEntity, user.Comp2, user.Comp3) &&
|
|
|
|
|
|
TryComp<GasTankComponent>(backEntity, out var backGasTank) &&
|
|
|
|
|
|
_gasTank.CanConnectToInternals((backEntity.Value, backGasTank)))
|
|
|
|
|
|
{
|
|
|
|
|
|
return (backEntity.Value, backGasTank);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (_inventory.TryGetSlotEntity(user, "suitstorage", out var entity, user.Comp2, user.Comp3) &&
|
|
|
|
|
|
TryComp<GasTankComponent>(entity, out var gasTank) &&
|
|
|
|
|
|
_gasTank.CanConnectToInternals((entity.Value, gasTank)))
|
|
|
|
|
|
{
|
|
|
|
|
|
return (entity.Value, gasTank);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var item in _inventory.GetHandOrInventoryEntities((user.Owner, user.Comp1, user.Comp2)))
|
|
|
|
|
|
{
|
|
|
|
|
|
if (TryComp(item, out gasTank) && _gasTank.CanConnectToInternals((item, gasTank)))
|
|
|
|
|
|
return (item, gasTank);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|