2021-06-19 13:25:05 +02:00
|
|
|
using Content.Server.Atmos.EntitySystems;
|
|
|
|
|
using Content.Server.Atmos.Piping.Components;
|
|
|
|
|
using JetBrains.Annotations;
|
|
|
|
|
using Robust.Shared.Timing;
|
|
|
|
|
|
|
|
|
|
namespace Content.Server.Atmos.Piping.EntitySystems
|
|
|
|
|
{
|
|
|
|
|
[UsedImplicitly]
|
2022-02-16 00:23:23 -07:00
|
|
|
public sealed class AtmosDeviceSystem : EntitySystem
|
2021-06-19 13:25:05 +02:00
|
|
|
{
|
2021-08-02 13:59:41 +02:00
|
|
|
[Dependency] private readonly IGameTiming _gameTiming = default!;
|
2021-07-26 12:58:17 +02:00
|
|
|
[Dependency] private readonly AtmosphereSystem _atmosphereSystem = default!;
|
2021-06-19 13:25:05 +02:00
|
|
|
|
2021-08-02 13:59:41 +02:00
|
|
|
private readonly AtmosDeviceUpdateEvent _updateEvent = new();
|
|
|
|
|
|
|
|
|
|
private float _timer = 0f;
|
|
|
|
|
private readonly HashSet<AtmosDeviceComponent> _joinedDevices = new();
|
|
|
|
|
|
2021-06-19 13:25:05 +02:00
|
|
|
public override void Initialize()
|
|
|
|
|
{
|
|
|
|
|
base.Initialize();
|
|
|
|
|
|
|
|
|
|
SubscribeLocalEvent<AtmosDeviceComponent, ComponentInit>(OnDeviceInitialize);
|
|
|
|
|
SubscribeLocalEvent<AtmosDeviceComponent, ComponentShutdown>(OnDeviceShutdown);
|
2022-04-24 13:54:25 +10:00
|
|
|
// Re-anchoring should be handled by the parent change.
|
2021-06-19 13:25:05 +02:00
|
|
|
SubscribeLocalEvent<AtmosDeviceComponent, EntParentChangedMessage>(OnDeviceParentChanged);
|
2021-07-23 11:09:01 +02:00
|
|
|
SubscribeLocalEvent<AtmosDeviceComponent, AnchorStateChangedEvent>(OnDeviceAnchorChanged);
|
2021-06-19 13:25:05 +02:00
|
|
|
}
|
|
|
|
|
|
2022-07-04 16:51:34 +02:00
|
|
|
private bool CanJoinAtmosphere(AtmosDeviceComponent component, TransformComponent transform)
|
2021-06-19 13:25:05 +02:00
|
|
|
{
|
2022-07-04 16:51:34 +02:00
|
|
|
return (!component.RequireAnchored || transform.Anchored) && transform.GridUid != null;
|
2021-06-19 13:25:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void JoinAtmosphere(AtmosDeviceComponent component)
|
|
|
|
|
{
|
2022-07-04 16:51:34 +02:00
|
|
|
var transform = Transform(component.Owner);
|
|
|
|
|
|
|
|
|
|
if (!CanJoinAtmosphere(component, transform))
|
2021-08-02 13:59:41 +02:00
|
|
|
{
|
2021-06-19 13:25:05 +02:00
|
|
|
return;
|
2021-08-02 13:59:41 +02:00
|
|
|
}
|
2021-06-19 13:25:05 +02:00
|
|
|
|
2022-07-04 16:51:34 +02:00
|
|
|
// TODO: low-hanging fruit for perf improvements around here
|
|
|
|
|
|
|
|
|
|
// GridUid is not null because we can join atmosphere.
|
2021-08-02 13:59:41 +02:00
|
|
|
// We try to add the device to a valid atmosphere, and if we can't, try to add it to the entity system.
|
2022-07-04 16:51:34 +02:00
|
|
|
if (!_atmosphereSystem.AddAtmosDevice(transform.GridUid!.Value, component))
|
2021-08-02 13:59:41 +02:00
|
|
|
{
|
|
|
|
|
if (component.JoinSystem)
|
|
|
|
|
{
|
|
|
|
|
_joinedDevices.Add(component);
|
|
|
|
|
component.JoinedSystem = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-19 13:25:05 +02:00
|
|
|
|
|
|
|
|
component.LastProcess = _gameTiming.CurTime;
|
|
|
|
|
|
2021-12-03 15:53:09 +01:00
|
|
|
RaiseLocalEvent(component.Owner, new AtmosDeviceEnabledEvent(), false);
|
2021-06-19 13:25:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void LeaveAtmosphere(AtmosDeviceComponent component)
|
|
|
|
|
{
|
2021-08-02 13:59:41 +02:00
|
|
|
// Try to remove the component from an atmosphere, and if not
|
2022-07-04 16:51:34 +02:00
|
|
|
if (component.JoinedGrid != null && !_atmosphereSystem.RemoveAtmosDevice(component.JoinedGrid.Value, component))
|
2021-08-02 13:59:41 +02:00
|
|
|
{
|
|
|
|
|
// The grid might have been removed but not us... This usually shouldn't happen.
|
|
|
|
|
component.JoinedGrid = null;
|
2021-07-23 11:09:01 +02:00
|
|
|
return;
|
2021-08-02 13:59:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (component.JoinedSystem)
|
|
|
|
|
{
|
|
|
|
|
_joinedDevices.Remove(component);
|
|
|
|
|
component.JoinedSystem = false;
|
|
|
|
|
}
|
2021-06-19 13:25:05 +02:00
|
|
|
|
2021-07-23 11:09:01 +02:00
|
|
|
component.LastProcess = TimeSpan.Zero;
|
2021-12-03 15:53:09 +01:00
|
|
|
RaiseLocalEvent(component.Owner, new AtmosDeviceDisabledEvent(), false);
|
2021-06-19 13:25:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RejoinAtmosphere(AtmosDeviceComponent component)
|
|
|
|
|
{
|
|
|
|
|
LeaveAtmosphere(component);
|
|
|
|
|
JoinAtmosphere(component);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnDeviceInitialize(EntityUid uid, AtmosDeviceComponent component, ComponentInit args)
|
|
|
|
|
{
|
|
|
|
|
JoinAtmosphere(component);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnDeviceShutdown(EntityUid uid, AtmosDeviceComponent component, ComponentShutdown args)
|
|
|
|
|
{
|
|
|
|
|
LeaveAtmosphere(component);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-21 11:49:31 +02:00
|
|
|
private void OnDeviceAnchorChanged(EntityUid uid, AtmosDeviceComponent component, ref AnchorStateChangedEvent args)
|
2021-06-19 13:25:05 +02:00
|
|
|
{
|
|
|
|
|
// Do nothing if the component doesn't require being anchored to function.
|
|
|
|
|
if (!component.RequireAnchored)
|
|
|
|
|
return;
|
|
|
|
|
|
2022-04-24 13:54:25 +10:00
|
|
|
if (args.Anchored)
|
2021-06-19 13:25:05 +02:00
|
|
|
JoinAtmosphere(component);
|
|
|
|
|
else
|
|
|
|
|
LeaveAtmosphere(component);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-21 11:49:31 +02:00
|
|
|
private void OnDeviceParentChanged(EntityUid uid, AtmosDeviceComponent component, ref EntParentChangedMessage args)
|
2021-06-19 13:25:05 +02:00
|
|
|
{
|
|
|
|
|
RejoinAtmosphere(component);
|
|
|
|
|
}
|
2021-08-02 13:59:41 +02:00
|
|
|
|
|
|
|
|
public override void Update(float frameTime)
|
|
|
|
|
{
|
|
|
|
|
_timer += frameTime;
|
|
|
|
|
|
|
|
|
|
if (_timer < _atmosphereSystem.AtmosTime)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
_timer -= _atmosphereSystem.AtmosTime;
|
|
|
|
|
|
|
|
|
|
var time = _gameTiming.CurTime;
|
|
|
|
|
foreach (var device in _joinedDevices)
|
|
|
|
|
{
|
2021-12-03 15:53:09 +01:00
|
|
|
RaiseLocalEvent(device.Owner, _updateEvent, false);
|
2021-08-02 13:59:41 +02:00
|
|
|
device.LastProcess = time;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-06-19 13:25:05 +02:00
|
|
|
}
|
|
|
|
|
}
|