2023-05-07 08:07:24 +02:00
|
|
|
using Content.Server.DeviceLinking.Components;
|
2023-05-06 22:49:11 -08:00
|
|
|
using Content.Server.DeviceNetwork;
|
2022-04-04 01:13:03 -05:00
|
|
|
using Content.Server.Doors.Systems;
|
|
|
|
|
using Content.Shared.Doors.Components;
|
2023-05-06 22:49:11 -08:00
|
|
|
using Content.Shared.Doors;
|
2022-04-04 01:13:03 -05:00
|
|
|
using JetBrains.Annotations;
|
2023-05-06 22:49:11 -08:00
|
|
|
using SignalReceivedEvent = Content.Server.DeviceLinking.Events.SignalReceivedEvent;
|
2022-04-04 01:13:03 -05:00
|
|
|
|
2023-05-07 08:07:24 +02:00
|
|
|
namespace Content.Server.DeviceLinking.Systems
|
2022-04-04 01:13:03 -05:00
|
|
|
{
|
|
|
|
|
[UsedImplicitly]
|
|
|
|
|
public sealed class DoorSignalControlSystem : EntitySystem
|
|
|
|
|
{
|
2023-06-01 02:23:35 +12:00
|
|
|
[Dependency] private readonly DoorBoltSystem _bolts = default!;
|
2022-04-04 01:13:03 -05:00
|
|
|
[Dependency] private readonly DoorSystem _doorSystem = default!;
|
2023-05-07 08:07:24 +02:00
|
|
|
[Dependency] private readonly DeviceLinkSystem _signalSystem = default!;
|
2022-05-12 20:46:20 +12:00
|
|
|
|
2022-04-04 01:13:03 -05:00
|
|
|
public override void Initialize()
|
|
|
|
|
{
|
|
|
|
|
base.Initialize();
|
|
|
|
|
SubscribeLocalEvent<DoorSignalControlComponent, ComponentInit>(OnInit);
|
|
|
|
|
SubscribeLocalEvent<DoorSignalControlComponent, SignalReceivedEvent>(OnSignalReceived);
|
2023-05-06 22:49:11 -08:00
|
|
|
SubscribeLocalEvent<DoorSignalControlComponent, DoorStateChangedEvent>(OnStateChanged);
|
2022-04-04 01:13:03 -05:00
|
|
|
}
|
2023-05-07 08:07:24 +02:00
|
|
|
|
2022-04-04 01:13:03 -05:00
|
|
|
private void OnInit(EntityUid uid, DoorSignalControlComponent component, ComponentInit args)
|
|
|
|
|
{
|
2023-05-06 22:49:11 -08:00
|
|
|
|
2023-05-07 08:07:24 +02:00
|
|
|
_signalSystem.EnsureSinkPorts(uid, component.OpenPort, component.ClosePort, component.TogglePort);
|
2023-05-06 22:49:11 -08:00
|
|
|
_signalSystem.EnsureSourcePorts(uid, component.OutOpen);
|
2022-04-04 01:13:03 -05:00
|
|
|
}
|
|
|
|
|
|
2023-05-07 08:07:24 +02:00
|
|
|
private void OnSignalReceived(EntityUid uid, DoorSignalControlComponent component, ref SignalReceivedEvent args)
|
2022-04-04 01:13:03 -05:00
|
|
|
{
|
2022-05-12 20:46:20 +12:00
|
|
|
if (!TryComp(uid, out DoorComponent? door))
|
|
|
|
|
return;
|
|
|
|
|
|
2023-05-06 22:49:11 -08:00
|
|
|
var state = SignalState.Momentary;
|
2023-05-12 00:16:02 +02:00
|
|
|
args.Data?.TryGetValue(DeviceNetworkConstants.LogicState, out state);
|
2023-05-06 22:49:11 -08:00
|
|
|
|
|
|
|
|
|
2022-05-12 20:46:20 +12:00
|
|
|
if (args.Port == component.OpenPort)
|
|
|
|
|
{
|
2023-05-06 22:49:11 -08:00
|
|
|
if (state == SignalState.High || state == SignalState.Momentary)
|
|
|
|
|
{
|
2023-05-28 18:14:06 +02:00
|
|
|
if (door.State == DoorState.Closed)
|
2023-05-06 22:49:11 -08:00
|
|
|
_doorSystem.TryOpen(uid, door);
|
|
|
|
|
}
|
2022-05-12 20:46:20 +12:00
|
|
|
}
|
|
|
|
|
else if (args.Port == component.ClosePort)
|
|
|
|
|
{
|
2023-05-06 22:49:11 -08:00
|
|
|
if (state == SignalState.High || state == SignalState.Momentary)
|
|
|
|
|
{
|
2023-05-28 18:14:06 +02:00
|
|
|
if (door.State == DoorState.Open)
|
2023-05-06 22:49:11 -08:00
|
|
|
_doorSystem.TryClose(uid, door);
|
|
|
|
|
}
|
2022-05-12 20:46:20 +12:00
|
|
|
}
|
|
|
|
|
else if (args.Port == component.TogglePort)
|
2022-04-04 01:13:03 -05:00
|
|
|
{
|
2023-05-06 22:49:11 -08:00
|
|
|
if (state == SignalState.High || state == SignalState.Momentary)
|
|
|
|
|
{
|
2023-05-28 18:14:06 +02:00
|
|
|
if (door.State is DoorState.Closed or DoorState.Open)
|
|
|
|
|
_doorSystem.TryToggleDoor(uid, door);
|
2023-05-06 22:49:11 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (args.Port == component.InBolt)
|
|
|
|
|
{
|
2023-06-07 23:48:42 +00:00
|
|
|
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)
|
2023-05-30 05:51:31 +03:00
|
|
|
{
|
2023-06-07 23:48:42 +00:00
|
|
|
bolt = !bolts.BoltsDown;
|
2023-05-30 05:51:31 +03:00
|
|
|
}
|
2023-05-06 22:49:11 -08:00
|
|
|
else
|
|
|
|
|
{
|
2023-06-07 23:48:42 +00:00
|
|
|
bolt = state == SignalState.High;
|
2023-05-06 22:49:11 -08:00
|
|
|
}
|
2023-06-07 23:48:42 +00:00
|
|
|
|
|
|
|
|
_bolts.SetBoltsWithAudio(uid, bolts, bolt);
|
2023-05-06 22:49:11 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnStateChanged(EntityUid uid, DoorSignalControlComponent door, DoorStateChangedEvent args)
|
|
|
|
|
{
|
|
|
|
|
var data = new NetworkPayload()
|
|
|
|
|
{
|
2023-05-12 00:16:02 +02:00
|
|
|
{ DeviceNetworkConstants.LogicState, SignalState.Momentary }
|
2023-05-06 22:49:11 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (args.State == DoorState.Closed)
|
|
|
|
|
{
|
2023-05-12 00:16:02 +02:00
|
|
|
data[DeviceNetworkConstants.LogicState] = SignalState.Low;
|
2023-05-06 22:49:11 -08:00
|
|
|
_signalSystem.InvokePort(uid, door.OutOpen, data);
|
|
|
|
|
}
|
|
|
|
|
else if (args.State == DoorState.Open
|
|
|
|
|
|| args.State == DoorState.Opening
|
|
|
|
|
|| args.State == DoorState.Closing
|
|
|
|
|
|| args.State == DoorState.Emagging)
|
|
|
|
|
{
|
2023-05-12 00:16:02 +02:00
|
|
|
data[DeviceNetworkConstants.LogicState] = SignalState.High;
|
2023-05-06 22:49:11 -08:00
|
|
|
_signalSystem.InvokePort(uid, door.OutOpen, data);
|
2022-04-04 01:13:03 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|