1
0

ListenWireAction.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using Content.Server.Chat.Systems;
  2. using Content.Shared.Radio;
  3. using Content.Server.Radio.Components;
  4. using Content.Server.Radio.EntitySystems;
  5. using Content.Server.Speech.Components;
  6. using Content.Server.Wires;
  7. using Content.Shared.Wires;
  8. using Content.Shared.Speech;
  9. using Robust.Shared.Prototypes;
  10. namespace Content.Server.Speech;
  11. public sealed partial class ListenWireAction : BaseToggleWireAction
  12. {
  13. private WiresSystem _wires = default!;
  14. private ChatSystem _chat = default!;
  15. private RadioSystem _radio = default!;
  16. private IPrototypeManager _protoMan = default!;
  17. /// <summary>
  18. /// Length of the gibberish string sent when pulsing the wire
  19. /// </summary>
  20. private const int NoiseLength = 16;
  21. public override Color Color { get; set; } = Color.Green;
  22. public override string Name { get; set; } = "wire-name-listen";
  23. public override object? StatusKey { get; } = ListenWireActionKey.StatusKey;
  24. public override object? TimeoutKey { get; } = ListenWireActionKey.TimeoutKey;
  25. public override int Delay { get; } = 10;
  26. public override void Initialize()
  27. {
  28. base.Initialize();
  29. _wires = EntityManager.System<WiresSystem>();
  30. _chat = EntityManager.System<ChatSystem>();
  31. _radio = EntityManager.System<RadioSystem>();
  32. _protoMan = IoCManager.Resolve<IPrototypeManager>();
  33. }
  34. public override StatusLightState? GetLightState(Wire wire)
  35. {
  36. if (GetValue(wire.Owner))
  37. return StatusLightState.On;
  38. else
  39. {
  40. if (TimeoutKey != null && _wires.HasData(wire.Owner, TimeoutKey))
  41. return StatusLightState.BlinkingSlow;
  42. return StatusLightState.Off;
  43. }
  44. }
  45. public override void ToggleValue(EntityUid owner, bool setting)
  46. {
  47. if (setting)
  48. {
  49. // If we defer removal, the status light gets out of sync
  50. EntityManager.RemoveComponent<BlockListeningComponent>(owner);
  51. }
  52. else
  53. {
  54. EntityManager.EnsureComponent<BlockListeningComponent>(owner);
  55. }
  56. }
  57. public override bool GetValue(EntityUid owner)
  58. {
  59. return !EntityManager.HasComponent<BlockListeningComponent>(owner);
  60. }
  61. public override void Pulse(EntityUid user, Wire wire)
  62. {
  63. if (!GetValue(wire.Owner) || !IsPowered(wire.Owner))
  64. return;
  65. var chars = Loc.GetString("wire-listen-pulse-characters").ToCharArray();
  66. var noiseMsg = _chat.BuildGibberishString(chars, NoiseLength);
  67. if (!EntityManager.TryGetComponent<RadioMicrophoneComponent>(wire.Owner, out var radioMicroPhoneComp))
  68. return;
  69. if (!EntityManager.TryGetComponent<VoiceOverrideComponent>(wire.Owner, out var voiceOverrideComp))
  70. return;
  71. // The reason for the override is to make the voice sound like its coming from electrity rather than the intercom.
  72. voiceOverrideComp.NameOverride = Loc.GetString("wire-listen-pulse-identifier");
  73. voiceOverrideComp.Enabled = true;
  74. _radio.SendRadioMessage(wire.Owner, noiseMsg, _protoMan.Index<RadioChannelPrototype>(radioMicroPhoneComp.BroadcastChannel), wire.Owner);
  75. voiceOverrideComp.Enabled = false;
  76. base.Pulse(user, wire);
  77. }
  78. }