DoorSignalControlSystem.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using Content.Server.DeviceLinking.Components;
  2. using Content.Server.DeviceNetwork;
  3. using Content.Server.Doors.Systems;
  4. using Content.Shared.DeviceNetwork;
  5. using Content.Shared.Doors.Components;
  6. using Content.Shared.Doors;
  7. using JetBrains.Annotations;
  8. using SignalReceivedEvent = Content.Server.DeviceLinking.Events.SignalReceivedEvent;
  9. namespace Content.Server.DeviceLinking.Systems
  10. {
  11. [UsedImplicitly]
  12. public sealed class DoorSignalControlSystem : EntitySystem
  13. {
  14. [Dependency] private readonly DoorSystem _doorSystem = default!;
  15. [Dependency] private readonly DeviceLinkSystem _signalSystem = default!;
  16. public override void Initialize()
  17. {
  18. base.Initialize();
  19. SubscribeLocalEvent<DoorSignalControlComponent, ComponentInit>(OnInit);
  20. SubscribeLocalEvent<DoorSignalControlComponent, SignalReceivedEvent>(OnSignalReceived);
  21. SubscribeLocalEvent<DoorSignalControlComponent, DoorStateChangedEvent>(OnStateChanged);
  22. }
  23. private void OnInit(EntityUid uid, DoorSignalControlComponent component, ComponentInit args)
  24. {
  25. _signalSystem.EnsureSinkPorts(uid, component.OpenPort, component.ClosePort, component.TogglePort);
  26. _signalSystem.EnsureSourcePorts(uid, component.OutOpen);
  27. }
  28. private void OnSignalReceived(EntityUid uid, DoorSignalControlComponent component, ref SignalReceivedEvent args)
  29. {
  30. if (!TryComp(uid, out DoorComponent? door))
  31. return;
  32. var state = SignalState.Momentary;
  33. args.Data?.TryGetValue(DeviceNetworkConstants.LogicState, out state);
  34. if (args.Port == component.OpenPort)
  35. {
  36. if (state == SignalState.High || state == SignalState.Momentary)
  37. {
  38. if (door.State == DoorState.Closed)
  39. _doorSystem.TryOpen(uid, door);
  40. }
  41. }
  42. else if (args.Port == component.ClosePort)
  43. {
  44. if (state == SignalState.High || state == SignalState.Momentary)
  45. {
  46. if (door.State == DoorState.Open)
  47. _doorSystem.TryClose(uid, door);
  48. }
  49. }
  50. else if (args.Port == component.TogglePort)
  51. {
  52. if (state == SignalState.High || state == SignalState.Momentary)
  53. {
  54. _doorSystem.TryToggleDoor(uid, door);
  55. }
  56. }
  57. else if (args.Port == component.InBolt)
  58. {
  59. if (!TryComp<DoorBoltComponent>(uid, out var bolts))
  60. return;
  61. // if its a pulse toggle, otherwise set bolts to high/low
  62. bool bolt;
  63. if (state == SignalState.Momentary)
  64. {
  65. bolt = !bolts.BoltsDown;
  66. }
  67. else
  68. {
  69. bolt = state == SignalState.High;
  70. }
  71. _doorSystem.SetBoltsDown((uid, bolts), bolt);
  72. }
  73. }
  74. private void OnStateChanged(EntityUid uid, DoorSignalControlComponent door, DoorStateChangedEvent args)
  75. {
  76. if (args.State == DoorState.Closed)
  77. {
  78. // only ever say the door is closed when it is completely airtight
  79. _signalSystem.SendSignal(uid, door.OutOpen, false);
  80. }
  81. else if (args.State == DoorState.Open
  82. || args.State == DoorState.Opening
  83. || args.State == DoorState.Closing
  84. || args.State == DoorState.Emagging)
  85. {
  86. // say the door is open whenever it would be letting air pass
  87. _signalSystem.SendSignal(uid, door.OutOpen, true);
  88. }
  89. }
  90. }
  91. }