Files
tbd-station-14/Content.Server/DeviceLinking/Systems/DoorSignalControlSystem.cs
2025-04-05 14:20:14 +02:00

102 lines
3.7 KiB
C#

using Content.Server.DeviceLinking.Components;
using Content.Server.DeviceNetwork;
using Content.Server.Doors.Systems;
using Content.Shared.DeviceLinking.Events;
using Content.Shared.Doors.Components;
using Content.Shared.Doors;
using JetBrains.Annotations;
namespace Content.Server.DeviceLinking.Systems
{
[UsedImplicitly]
public sealed class DoorSignalControlSystem : EntitySystem
{
[Dependency] private readonly DoorSystem _doorSystem = default!;
[Dependency] private readonly DeviceLinkSystem _signalSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<DoorSignalControlComponent, ComponentInit>(OnInit);
SubscribeLocalEvent<DoorSignalControlComponent, SignalReceivedEvent>(OnSignalReceived);
SubscribeLocalEvent<DoorSignalControlComponent, DoorStateChangedEvent>(OnStateChanged);
}
private void OnInit(EntityUid uid, DoorSignalControlComponent component, ComponentInit args)
{
_signalSystem.EnsureSinkPorts(uid, component.OpenPort, component.ClosePort, component.TogglePort);
_signalSystem.EnsureSourcePorts(uid, component.OutOpen);
}
private void OnSignalReceived(EntityUid uid, DoorSignalControlComponent component, ref SignalReceivedEvent args)
{
if (!TryComp(uid, out DoorComponent? door))
return;
var state = SignalState.Momentary;
args.Data?.TryGetValue(DeviceNetworkConstants.LogicState, out state);
if (args.Port == component.OpenPort)
{
if (state == SignalState.High || state == SignalState.Momentary)
{
if (door.State == DoorState.Closed)
_doorSystem.TryOpen(uid, door);
}
}
else if (args.Port == component.ClosePort)
{
if (state == SignalState.High || state == SignalState.Momentary)
{
if (door.State == DoorState.Open)
_doorSystem.TryClose(uid, door);
}
}
else if (args.Port == component.TogglePort)
{
if (state == SignalState.High || state == SignalState.Momentary)
{
_doorSystem.TryToggleDoor(uid, door);
}
}
else if (args.Port == component.InBolt)
{
if (!TryComp<DoorBoltComponent>(uid, out var bolts))
return;
// if its a pulse toggle, otherwise set bolts to high/low
bool bolt;
if (state == SignalState.Momentary)
{
bolt = !bolts.BoltsDown;
}
else
{
bolt = state == SignalState.High;
}
_doorSystem.SetBoltsDown((uid, bolts), bolt);
}
}
private void OnStateChanged(EntityUid uid, DoorSignalControlComponent door, DoorStateChangedEvent args)
{
if (args.State == DoorState.Closed)
{
// only ever say the door is closed when it is completely airtight
_signalSystem.SendSignal(uid, door.OutOpen, false);
}
else if (args.State == DoorState.Open
|| args.State == DoorState.Opening
|| args.State == DoorState.Closing
|| args.State == DoorState.Emagging)
{
// say the door is open whenever it would be letting air pass
_signalSystem.SendSignal(uid, door.OutOpen, true);
}
}
}
}