RadioDeviceSystem.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. using System.Linq;
  2. using Content.Server.Chat.Systems;
  3. using Content.Server.Interaction;
  4. using Content.Server.Popups;
  5. using Content.Server.Power.Components;
  6. using Content.Server.Power.EntitySystems;
  7. using Content.Server.Radio.Components;
  8. using Content.Server.Speech;
  9. using Content.Server.Speech.Components;
  10. using Content.Shared.Examine;
  11. using Content.Shared.Interaction;
  12. using Content.Shared.Power;
  13. using Content.Shared.Radio;
  14. using Content.Shared.Chat;
  15. using Content.Shared.Radio.Components;
  16. using Robust.Shared.Prototypes;
  17. namespace Content.Server.Radio.EntitySystems;
  18. /// <summary>
  19. /// This system handles radio speakers and microphones (which together form a hand-held radio).
  20. /// </summary>
  21. public sealed class RadioDeviceSystem : EntitySystem
  22. {
  23. [Dependency] private readonly IPrototypeManager _protoMan = default!;
  24. [Dependency] private readonly PopupSystem _popup = default!;
  25. [Dependency] private readonly ChatSystem _chat = default!;
  26. [Dependency] private readonly RadioSystem _radio = default!;
  27. [Dependency] private readonly InteractionSystem _interaction = default!;
  28. [Dependency] private readonly SharedAppearanceSystem _appearance = default!;
  29. // Used to prevent a shitter from using a bunch of radios to spam chat.
  30. private HashSet<(string, EntityUid, RadioChannelPrototype)> _recentlySent = new();
  31. public override void Initialize()
  32. {
  33. base.Initialize();
  34. SubscribeLocalEvent<RadioMicrophoneComponent, ComponentInit>(OnMicrophoneInit);
  35. SubscribeLocalEvent<RadioMicrophoneComponent, ExaminedEvent>(OnExamine);
  36. SubscribeLocalEvent<RadioMicrophoneComponent, ActivateInWorldEvent>(OnActivateMicrophone);
  37. SubscribeLocalEvent<RadioMicrophoneComponent, ListenEvent>(OnListen);
  38. SubscribeLocalEvent<RadioMicrophoneComponent, ListenAttemptEvent>(OnAttemptListen);
  39. SubscribeLocalEvent<RadioMicrophoneComponent, PowerChangedEvent>(OnPowerChanged);
  40. SubscribeLocalEvent<RadioSpeakerComponent, ComponentInit>(OnSpeakerInit);
  41. SubscribeLocalEvent<RadioSpeakerComponent, ActivateInWorldEvent>(OnActivateSpeaker);
  42. SubscribeLocalEvent<RadioSpeakerComponent, RadioReceiveEvent>(OnReceiveRadio);
  43. SubscribeLocalEvent<IntercomComponent, EncryptionChannelsChangedEvent>(OnIntercomEncryptionChannelsChanged);
  44. SubscribeLocalEvent<IntercomComponent, ToggleIntercomMicMessage>(OnToggleIntercomMic);
  45. SubscribeLocalEvent<IntercomComponent, ToggleIntercomSpeakerMessage>(OnToggleIntercomSpeaker);
  46. SubscribeLocalEvent<IntercomComponent, SelectIntercomChannelMessage>(OnSelectIntercomChannel);
  47. }
  48. public override void Update(float frameTime)
  49. {
  50. base.Update(frameTime);
  51. _recentlySent.Clear();
  52. }
  53. #region Component Init
  54. private void OnMicrophoneInit(EntityUid uid, RadioMicrophoneComponent component, ComponentInit args)
  55. {
  56. if (component.Enabled)
  57. EnsureComp<ActiveListenerComponent>(uid).Range = component.ListenRange;
  58. else
  59. RemCompDeferred<ActiveListenerComponent>(uid);
  60. }
  61. private void OnSpeakerInit(EntityUid uid, RadioSpeakerComponent component, ComponentInit args)
  62. {
  63. if (component.Enabled)
  64. EnsureComp<ActiveRadioComponent>(uid).Channels.UnionWith(component.Channels);
  65. else
  66. RemCompDeferred<ActiveRadioComponent>(uid);
  67. }
  68. #endregion
  69. #region Toggling
  70. private void OnActivateMicrophone(EntityUid uid, RadioMicrophoneComponent component, ActivateInWorldEvent args)
  71. {
  72. if (!args.Complex)
  73. return;
  74. if (!component.ToggleOnInteract)
  75. return;
  76. ToggleRadioMicrophone(uid, args.User, args.Handled, component);
  77. args.Handled = true;
  78. }
  79. private void OnActivateSpeaker(EntityUid uid, RadioSpeakerComponent component, ActivateInWorldEvent args)
  80. {
  81. if (!args.Complex)
  82. return;
  83. if (!component.ToggleOnInteract)
  84. return;
  85. ToggleRadioSpeaker(uid, args.User, args.Handled, component);
  86. args.Handled = true;
  87. }
  88. public void ToggleRadioMicrophone(EntityUid uid, EntityUid user, bool quiet = false, RadioMicrophoneComponent? component = null)
  89. {
  90. if (!Resolve(uid, ref component))
  91. return;
  92. SetMicrophoneEnabled(uid, user, !component.Enabled, quiet, component);
  93. }
  94. private void OnPowerChanged(EntityUid uid, RadioMicrophoneComponent component, ref PowerChangedEvent args)
  95. {
  96. if (args.Powered)
  97. return;
  98. SetMicrophoneEnabled(uid, null, false, true, component);
  99. }
  100. public void SetMicrophoneEnabled(EntityUid uid, EntityUid? user, bool enabled, bool quiet = false, RadioMicrophoneComponent? component = null)
  101. {
  102. if (!Resolve(uid, ref component, false))
  103. return;
  104. if (component.PowerRequired && !this.IsPowered(uid, EntityManager))
  105. return;
  106. component.Enabled = enabled;
  107. if (!quiet && user != null)
  108. {
  109. var state = Loc.GetString(component.Enabled ? "handheld-radio-component-on-state" : "handheld-radio-component-off-state");
  110. var message = Loc.GetString("handheld-radio-component-on-use", ("radioState", state));
  111. _popup.PopupEntity(message, user.Value, user.Value);
  112. }
  113. _appearance.SetData(uid, RadioDeviceVisuals.Broadcasting, component.Enabled);
  114. if (component.Enabled)
  115. EnsureComp<ActiveListenerComponent>(uid).Range = component.ListenRange;
  116. else
  117. RemCompDeferred<ActiveListenerComponent>(uid);
  118. }
  119. public void ToggleRadioSpeaker(EntityUid uid, EntityUid user, bool quiet = false, RadioSpeakerComponent? component = null)
  120. {
  121. if (!Resolve(uid, ref component))
  122. return;
  123. SetSpeakerEnabled(uid, user, !component.Enabled, quiet, component);
  124. }
  125. public void SetSpeakerEnabled(EntityUid uid, EntityUid? user, bool enabled, bool quiet = false, RadioSpeakerComponent? component = null)
  126. {
  127. if (!Resolve(uid, ref component))
  128. return;
  129. component.Enabled = enabled;
  130. if (!quiet && user != null)
  131. {
  132. var state = Loc.GetString(component.Enabled ? "handheld-radio-component-on-state" : "handheld-radio-component-off-state");
  133. var message = Loc.GetString("handheld-radio-component-on-use", ("radioState", state));
  134. _popup.PopupEntity(message, user.Value, user.Value);
  135. }
  136. _appearance.SetData(uid, RadioDeviceVisuals.Speaker, component.Enabled);
  137. if (component.Enabled)
  138. EnsureComp<ActiveRadioComponent>(uid).Channels.UnionWith(component.Channels);
  139. else
  140. RemCompDeferred<ActiveRadioComponent>(uid);
  141. }
  142. #endregion
  143. private void OnExamine(EntityUid uid, RadioMicrophoneComponent component, ExaminedEvent args)
  144. {
  145. if (!args.IsInDetailsRange)
  146. return;
  147. var proto = _protoMan.Index<RadioChannelPrototype>(component.BroadcastChannel);
  148. using (args.PushGroup(nameof(RadioMicrophoneComponent)))
  149. {
  150. args.PushMarkup(Loc.GetString("handheld-radio-component-on-examine", ("frequency", proto.Frequency)));
  151. args.PushMarkup(Loc.GetString("handheld-radio-component-chennel-examine",
  152. ("channel", proto.LocalizedName)));
  153. }
  154. }
  155. private void OnListen(EntityUid uid, RadioMicrophoneComponent component, ListenEvent args)
  156. {
  157. if (HasComp<RadioSpeakerComponent>(args.Source))
  158. return; // no feedback loops please.
  159. var channel = _protoMan.Index<RadioChannelPrototype>(component.BroadcastChannel)!;
  160. if (_recentlySent.Add((args.Message, args.Source, channel)))
  161. _radio.SendRadioMessage(args.Source, args.Message, channel, uid);
  162. }
  163. private void OnAttemptListen(EntityUid uid, RadioMicrophoneComponent component, ListenAttemptEvent args)
  164. {
  165. if (component.PowerRequired && !this.IsPowered(uid, EntityManager)
  166. || component.UnobstructedRequired && !_interaction.InRangeUnobstructed(args.Source, uid, 0))
  167. {
  168. args.Cancel();
  169. }
  170. }
  171. private void OnReceiveRadio(EntityUid uid, RadioSpeakerComponent component, ref RadioReceiveEvent args)
  172. {
  173. if (uid == args.RadioSource)
  174. return;
  175. var nameEv = new TransformSpeakerNameEvent(args.MessageSource, Name(args.MessageSource));
  176. RaiseLocalEvent(args.MessageSource, nameEv);
  177. var name = Loc.GetString("speech-name-relay",
  178. ("speaker", Name(uid)),
  179. ("originalName", nameEv.VoiceName));
  180. // log to chat so people can identity the speaker/source, but avoid clogging ghost chat if there are many radios
  181. _chat.TrySendInGameICMessage(uid, args.Message, InGameICChatType.Whisper, ChatTransmitRange.GhostRangeLimit, nameOverride: name, checkRadioPrefix: false);
  182. }
  183. private void OnIntercomEncryptionChannelsChanged(Entity<IntercomComponent> ent, ref EncryptionChannelsChangedEvent args)
  184. {
  185. ent.Comp.SupportedChannels = args.Component.Channels.Select(p => new ProtoId<RadioChannelPrototype>(p)).ToList();
  186. var channel = args.Component.DefaultChannel;
  187. if (ent.Comp.CurrentChannel != null && ent.Comp.SupportedChannels.Contains(ent.Comp.CurrentChannel.Value))
  188. channel = ent.Comp.CurrentChannel;
  189. SetIntercomChannel(ent, channel);
  190. }
  191. private void OnToggleIntercomMic(Entity<IntercomComponent> ent, ref ToggleIntercomMicMessage args)
  192. {
  193. if (ent.Comp.RequiresPower && !this.IsPowered(ent, EntityManager))
  194. return;
  195. SetMicrophoneEnabled(ent, args.Actor, args.Enabled, true);
  196. ent.Comp.MicrophoneEnabled = args.Enabled;
  197. Dirty(ent);
  198. }
  199. private void OnToggleIntercomSpeaker(Entity<IntercomComponent> ent, ref ToggleIntercomSpeakerMessage args)
  200. {
  201. if (ent.Comp.RequiresPower && !this.IsPowered(ent, EntityManager))
  202. return;
  203. SetSpeakerEnabled(ent, args.Actor, args.Enabled, true);
  204. ent.Comp.SpeakerEnabled = args.Enabled;
  205. Dirty(ent);
  206. }
  207. private void OnSelectIntercomChannel(Entity<IntercomComponent> ent, ref SelectIntercomChannelMessage args)
  208. {
  209. if (ent.Comp.RequiresPower && !this.IsPowered(ent, EntityManager))
  210. return;
  211. if (!_protoMan.HasIndex<RadioChannelPrototype>(args.Channel) || !ent.Comp.SupportedChannels.Contains(args.Channel))
  212. return;
  213. SetIntercomChannel(ent, args.Channel);
  214. }
  215. private void SetIntercomChannel(Entity<IntercomComponent> ent, ProtoId<RadioChannelPrototype>? channel)
  216. {
  217. ent.Comp.CurrentChannel = channel;
  218. if (channel == null)
  219. {
  220. SetSpeakerEnabled(ent, null, false);
  221. SetMicrophoneEnabled(ent, null, false);
  222. ent.Comp.MicrophoneEnabled = false;
  223. ent.Comp.SpeakerEnabled = false;
  224. Dirty(ent);
  225. return;
  226. }
  227. if (TryComp<RadioMicrophoneComponent>(ent, out var mic))
  228. mic.BroadcastChannel = channel;
  229. if (TryComp<RadioSpeakerComponent>(ent, out var speaker))
  230. speaker.Channels = new() { channel };
  231. Dirty(ent);
  232. }
  233. }