149 lines
5.8 KiB
C#
149 lines
5.8 KiB
C#
using Content.Server.Chat.Systems;
|
|
using Content.Server.Popups;
|
|
using Content.Server.Radio.Components;
|
|
using Content.Server.Speech;
|
|
using Content.Server.Speech.Components;
|
|
using Content.Shared.Examine;
|
|
using Content.Shared.Interaction;
|
|
using Content.Shared.Radio;
|
|
using Robust.Shared.Player;
|
|
using Robust.Shared.Prototypes;
|
|
|
|
namespace Content.Server.Radio.EntitySystems;
|
|
|
|
/// <summary>
|
|
/// This system handles radio speakers and microphones (which together form a hand-held radio).
|
|
/// </summary>
|
|
public sealed class RadioDeviceSystem : EntitySystem
|
|
{
|
|
[Dependency] private readonly IPrototypeManager _protoMan = default!;
|
|
[Dependency] private readonly PopupSystem _popup = default!;
|
|
[Dependency] private readonly ChatSystem _chat = default!;
|
|
[Dependency] private readonly RadioSystem _radio = default!;
|
|
|
|
// Used to prevent a shitter from using a bunch of radios to spam chat.
|
|
private HashSet<(string, EntityUid)> _recentlySent = new();
|
|
|
|
public override void Initialize()
|
|
{
|
|
base.Initialize();
|
|
SubscribeLocalEvent<RadioMicrophoneComponent, ComponentInit>(OnMicrophoneInit);
|
|
SubscribeLocalEvent<RadioMicrophoneComponent, ExaminedEvent>(OnExamine);
|
|
SubscribeLocalEvent<RadioMicrophoneComponent, ActivateInWorldEvent>(OnActivateMicrophone);
|
|
SubscribeLocalEvent<RadioMicrophoneComponent, ListenEvent>(OnListen);
|
|
|
|
SubscribeLocalEvent<RadioSpeakerComponent, ComponentInit>(OnSpeakerInit);
|
|
SubscribeLocalEvent<RadioSpeakerComponent, ActivateInWorldEvent>(OnActivateSpeaker);
|
|
SubscribeLocalEvent<RadioSpeakerComponent, RadioReceiveEvent>(OnReceiveRadio);
|
|
}
|
|
|
|
public override void Update(float frameTime)
|
|
{
|
|
base.Update(frameTime);
|
|
_recentlySent.Clear();
|
|
}
|
|
|
|
|
|
#region Component Init
|
|
private void OnMicrophoneInit(EntityUid uid, RadioMicrophoneComponent component, ComponentInit args)
|
|
{
|
|
if (component.Enabled)
|
|
EnsureComp<ActiveListenerComponent>(uid).Range = component.ListenRange;
|
|
else
|
|
RemCompDeferred<ActiveListenerComponent>(uid);
|
|
}
|
|
|
|
private void OnSpeakerInit(EntityUid uid, RadioSpeakerComponent component, ComponentInit args)
|
|
{
|
|
if (component.Enabled)
|
|
EnsureComp<ActiveRadioComponent>(uid).Channels.UnionWith(component.Channels);
|
|
else
|
|
RemCompDeferred<ActiveRadioComponent>(uid);
|
|
}
|
|
#endregion
|
|
|
|
#region Toggling
|
|
private void OnActivateMicrophone(EntityUid uid, RadioMicrophoneComponent component, ActivateInWorldEvent args)
|
|
{
|
|
ToggleRadioMicrophone(uid, args.User, args.Handled, component);
|
|
args.Handled = true;
|
|
}
|
|
|
|
private void OnActivateSpeaker(EntityUid uid, RadioSpeakerComponent component, ActivateInWorldEvent args)
|
|
{
|
|
ToggleRadioSpeaker(uid, args.User, args.Handled, component);
|
|
args.Handled = true;
|
|
}
|
|
|
|
public void ToggleRadioMicrophone(EntityUid uid, EntityUid user, bool quiet = false, RadioMicrophoneComponent? component = null)
|
|
{
|
|
if (!Resolve(uid, ref component))
|
|
return;
|
|
|
|
component.Enabled = !component.Enabled;
|
|
|
|
if (!quiet)
|
|
{
|
|
var state = Loc.GetString(component.Enabled ? "handheld-radio-component-on-state" : "handheld-radio-component-off-state");
|
|
var message = Loc.GetString("handheld-radio-component-on-use", ("radioState", state));
|
|
_popup.PopupEntity(message, user, Filter.Entities(user));
|
|
}
|
|
|
|
if (component.Enabled)
|
|
EnsureComp<ActiveListenerComponent>(uid).Range = component.ListenRange;
|
|
else
|
|
RemCompDeferred<ActiveListenerComponent>(uid);
|
|
}
|
|
|
|
public void ToggleRadioSpeaker(EntityUid uid, EntityUid user, bool quiet = false, RadioSpeakerComponent? component = null)
|
|
{
|
|
if (!Resolve(uid, ref component))
|
|
return;
|
|
|
|
component.Enabled = !component.Enabled;
|
|
|
|
if (!quiet)
|
|
{
|
|
var state = Loc.GetString(component.Enabled ? "handheld-radio-component-on-state" : "handheld-radio-component-off-state");
|
|
var message = Loc.GetString("handheld-radio-component-on-use", ("radioState", state));
|
|
_popup.PopupEntity(message, user, Filter.Entities(user));
|
|
}
|
|
|
|
if (component.Enabled)
|
|
EnsureComp<ActiveRadioComponent>(uid).Channels.UnionWith(component.Channels);
|
|
else
|
|
RemCompDeferred<ActiveRadioComponent>(uid);
|
|
}
|
|
#endregion
|
|
|
|
private void OnExamine(EntityUid uid, RadioMicrophoneComponent component, ExaminedEvent args)
|
|
{
|
|
if (!args.IsInDetailsRange)
|
|
return;
|
|
|
|
var freq = _protoMan.Index<RadioChannelPrototype>(component.BroadcastChannel).Frequency;
|
|
args.PushMarkup(Loc.GetString("handheld-radio-component-on-examine", ("frequency", freq)));
|
|
}
|
|
|
|
private void OnListen(EntityUid uid, RadioMicrophoneComponent component, ListenEvent args)
|
|
{
|
|
if (HasComp<RadioSpeakerComponent>(args.Source))
|
|
return; // no feedback loops please.
|
|
|
|
if (_recentlySent.Add((args.Message, args.Source)))
|
|
_radio.SendRadioMessage(args.Source, args.Message, _protoMan.Index<RadioChannelPrototype>(component.BroadcastChannel));
|
|
}
|
|
|
|
private void OnReceiveRadio(EntityUid uid, RadioSpeakerComponent component, RadioReceiveEvent args)
|
|
{
|
|
var nameEv = new TransformSpeakerNameEvent(args.Source, Name(args.Source));
|
|
RaiseLocalEvent(args.Source, nameEv);
|
|
|
|
var name = Loc.GetString("speech-name-relay", ("speaker", Name(uid)),
|
|
("originalName", nameEv.Name));
|
|
|
|
var hideGlobalGhostChat = true; // log to chat so people can identity the speaker/source, but avoid clogging ghost chat if there are many radios
|
|
_chat.TrySendInGameICMessage(uid, args.Message, InGameICChatType.Speak, false, nameOverride: name, hideGlobalGhostChat:hideGlobalGhostChat);
|
|
}
|
|
}
|