2021-06-23 11:35:30 +02:00
|
|
|
using Content.Server.Atmos.EntitySystems;
|
2021-06-19 13:25:05 +02:00
|
|
|
using Content.Server.Atmos.Piping.Components;
|
|
|
|
|
using Content.Server.NodeContainer;
|
2021-07-04 18:11:52 +02:00
|
|
|
using Content.Server.NodeContainer.Nodes;
|
2021-11-09 08:08:30 +01:00
|
|
|
using Content.Server.Popups;
|
2021-06-19 13:25:05 +02:00
|
|
|
using Content.Shared.Atmos;
|
2022-04-02 16:52:44 +13:00
|
|
|
using Content.Shared.Construction.Components;
|
2023-12-10 23:51:45 -08:00
|
|
|
using Content.Shared.Destructible;
|
2022-07-09 02:09:52 -07:00
|
|
|
using Content.Shared.Popups;
|
2021-06-19 13:25:05 +02:00
|
|
|
using JetBrains.Annotations;
|
2021-11-09 08:08:30 +01:00
|
|
|
using Robust.Shared.Player;
|
2021-06-19 13:25:05 +02:00
|
|
|
|
|
|
|
|
namespace Content.Server.Atmos.Piping.EntitySystems
|
|
|
|
|
{
|
|
|
|
|
[UsedImplicitly]
|
2022-02-16 00:23:23 -07:00
|
|
|
public sealed class AtmosUnsafeUnanchorSystem : EntitySystem
|
2021-06-19 13:25:05 +02:00
|
|
|
{
|
2022-09-11 06:30:10 +00:00
|
|
|
[Dependency] private readonly AtmosphereSystem _atmosphere = default!;
|
|
|
|
|
[Dependency] private readonly PopupSystem _popup = default!;
|
2021-07-26 12:58:17 +02:00
|
|
|
|
2021-06-19 13:25:05 +02:00
|
|
|
public override void Initialize()
|
|
|
|
|
{
|
|
|
|
|
SubscribeLocalEvent<AtmosUnsafeUnanchorComponent, BeforeUnanchoredEvent>(OnBeforeUnanchored);
|
|
|
|
|
SubscribeLocalEvent<AtmosUnsafeUnanchorComponent, UnanchorAttemptEvent>(OnUnanchorAttempt);
|
2023-12-10 23:51:45 -08:00
|
|
|
SubscribeLocalEvent<AtmosUnsafeUnanchorComponent, BreakageEventArgs>(OnBreak);
|
2021-06-19 13:25:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnUnanchorAttempt(EntityUid uid, AtmosUnsafeUnanchorComponent component, UnanchorAttemptEvent args)
|
|
|
|
|
{
|
2021-09-28 13:35:29 +02:00
|
|
|
if (!component.Enabled || !EntityManager.TryGetComponent(uid, out NodeContainerComponent? nodes))
|
2021-06-19 13:25:05 +02:00
|
|
|
return;
|
|
|
|
|
|
2022-09-11 06:30:10 +00:00
|
|
|
if (_atmosphere.GetContainingMixture(uid, true) is not {} environment)
|
2021-06-19 13:25:05 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
foreach (var node in nodes.Nodes.Values)
|
|
|
|
|
{
|
2022-09-11 06:30:10 +00:00
|
|
|
if (node is not PipeNode pipe)
|
|
|
|
|
continue;
|
2021-06-19 13:25:05 +02:00
|
|
|
|
2022-09-11 06:30:10 +00:00
|
|
|
if (pipe.Air.Pressure - environment.Pressure > 2 * Atmospherics.OneAtmosphere)
|
2021-06-19 13:25:05 +02:00
|
|
|
{
|
2022-09-11 06:30:10 +00:00
|
|
|
args.Delay += 2f;
|
|
|
|
|
_popup.PopupEntity(Loc.GetString("comp-atmos-unsafe-unanchor-warning"), pipe.Owner,
|
2022-12-19 10:41:47 +13:00
|
|
|
args.User, PopupType.MediumCaution);
|
2021-06-19 13:25:05 +02:00
|
|
|
return; // Show the warning only once.
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnBeforeUnanchored(EntityUid uid, AtmosUnsafeUnanchorComponent component, BeforeUnanchoredEvent args)
|
|
|
|
|
{
|
2023-12-10 23:51:45 -08:00
|
|
|
if (component.Enabled)
|
|
|
|
|
LeakGas(uid);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnBreak(EntityUid uid, AtmosUnsafeUnanchorComponent component, BreakageEventArgs args)
|
|
|
|
|
{
|
|
|
|
|
LeakGas(uid);
|
|
|
|
|
// Can't use DoActsBehavior["Destruction"] in the same trigger because that would prevent us
|
|
|
|
|
// from leaking. So we make up for this by queueing deletion here.
|
|
|
|
|
QueueDel(uid);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Leak gas from the uid's NodeContainer into the tile atmosphere.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void LeakGas(EntityUid uid)
|
|
|
|
|
{
|
|
|
|
|
if (!EntityManager.TryGetComponent(uid, out NodeContainerComponent? nodes))
|
2021-06-19 13:25:05 +02:00
|
|
|
return;
|
|
|
|
|
|
2022-09-11 06:30:10 +00:00
|
|
|
if (_atmosphere.GetContainingMixture(uid, true, true) is not {} environment)
|
2021-07-19 12:07:37 +02:00
|
|
|
environment = GasMixture.SpaceGas;
|
2021-06-19 13:25:05 +02:00
|
|
|
|
|
|
|
|
var lost = 0f;
|
|
|
|
|
var timesLost = 0;
|
|
|
|
|
|
|
|
|
|
foreach (var node in nodes.Nodes.Values)
|
|
|
|
|
{
|
2022-09-11 06:30:10 +00:00
|
|
|
if (node is not PipeNode pipe)
|
|
|
|
|
continue;
|
2021-06-19 13:25:05 +02:00
|
|
|
|
2021-07-19 12:07:37 +02:00
|
|
|
var difference = pipe.Air.Pressure - environment.Pressure;
|
2024-06-29 19:06:25 +03:00
|
|
|
lost += Math.Min(
|
|
|
|
|
pipe.Volume / pipe.Air.Volume * pipe.Air.TotalMoles,
|
|
|
|
|
difference * environment.Volume / (environment.Temperature * Atmospherics.R)
|
|
|
|
|
);
|
2021-06-19 13:25:05 +02:00
|
|
|
timesLost++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var sharedLoss = lost / timesLost;
|
|
|
|
|
var buffer = new GasMixture();
|
|
|
|
|
|
|
|
|
|
foreach (var node in nodes.Nodes.Values)
|
|
|
|
|
{
|
2022-09-11 06:30:10 +00:00
|
|
|
if (node is not PipeNode pipe)
|
|
|
|
|
continue;
|
2021-06-19 13:25:05 +02:00
|
|
|
|
2022-09-11 06:30:10 +00:00
|
|
|
_atmosphere.Merge(buffer, pipe.Air.Remove(sharedLoss));
|
2021-06-19 13:25:05 +02:00
|
|
|
}
|
|
|
|
|
|
2022-09-11 06:30:10 +00:00
|
|
|
_atmosphere.Merge(environment, buffer);
|
2021-06-19 13:25:05 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|