IListener and IRadio purge (#11980)

This commit is contained in:
Leon Friedrich
2022-11-15 17:09:27 +13:00
committed by GitHub
parent bc525425da
commit 0b5a58001c
48 changed files with 946 additions and 643 deletions

View File

@@ -1,37 +1,97 @@
using Content.Shared.IdentityManagement;
using Content.Shared.Interaction;
using Content.Server.Chat.Systems;
using Content.Server.Speech;
using Content.Server.Speech.Components;
using Robust.Server.GameObjects;
using static Content.Server.Chat.Systems.ChatSystem;
namespace Content.Server.SurveillanceCamera;
public sealed class SurveillanceCameraMicrophoneSystem : EntitySystem
{
[Dependency] private SharedInteractionSystem _interactionSystem = default!;
[Dependency] private readonly SharedTransformSystem _xforms = default!;
public bool CanListen(EntityUid source, EntityUid speaker, SurveillanceCameraMicrophoneComponent? microphone = null)
public override void Initialize()
{
if (!Resolve(source, ref microphone))
{
return false;
}
return microphone.Enabled
&& !microphone.BlacklistedComponents.IsValid(speaker)
&& _interactionSystem.InRangeUnobstructed(source, speaker, range: microphone.ListenRange);
base.Initialize();
SubscribeLocalEvent<SurveillanceCameraMicrophoneComponent, ComponentInit>(OnInit);
SubscribeLocalEvent<SurveillanceCameraMicrophoneComponent, ListenEvent>(RelayEntityMessage);
SubscribeLocalEvent<SurveillanceCameraMicrophoneComponent, ListenAttemptEvent>(CanListen);
SubscribeLocalEvent<ExpandICChatRecipientstEvent>(OnExpandRecipients);
}
public void RelayEntityMessage(EntityUid source, EntityUid speaker, string message, SurveillanceCameraComponent? camera = null)
{
if (!Resolve(source, ref camera))
{
return;
}
var ev = new SurveillanceCameraSpeechSendEvent(speaker, message);
private void OnExpandRecipients(ExpandICChatRecipientstEvent ev)
{
var xformQuery = GetEntityQuery<TransformComponent>();
var sourceXform = Transform(ev.Source);
var sourcePos = _xforms.GetWorldPosition(sourceXform, xformQuery);
// This function ensures that chat popups appear on camera views that have connected microphones.
foreach (var (_, __, camera, xform) in EntityQuery<SurveillanceCameraMicrophoneComponent, ActiveListenerComponent, SurveillanceCameraComponent, TransformComponent>())
{
if (camera.ActiveViewers.Count == 0)
continue;
// get range to camera. This way wispers will still appear as obfuscated if they are too far from the camera's microphone
var range = (xform.MapID != sourceXform.MapID)
? -1
: (sourcePos - _xforms.GetWorldPosition(xform, xformQuery)).Length;
if (range < 0 || range > ev.VoiceRange)
continue;
foreach (var viewer in camera.ActiveViewers)
{
// if the player has not already received the chat message, send it to them but don't log it to the chat
// window. This is simply so that it appears in camera.
if (TryComp(viewer, out ActorComponent? actor))
ev.Recipients.TryAdd(actor.PlayerSession, new ICChatRecipientData(range, false, true));
}
}
}
private void OnInit(EntityUid uid, SurveillanceCameraMicrophoneComponent component, ComponentInit args)
{
if (component.Enabled)
EnsureComp<ActiveListenerComponent>(uid).Range = component.Range;
else
RemCompDeferred<ActiveListenerComponent>(uid);
}
public void CanListen(EntityUid uid, SurveillanceCameraMicrophoneComponent microphone, ListenAttemptEvent args)
{
// TODO maybe just make this a part of ActiveListenerComponent?
if (microphone.Blacklist.IsValid(args.Source))
args.Cancel();
}
public void RelayEntityMessage(EntityUid uid, SurveillanceCameraMicrophoneComponent component, ListenEvent args)
{
if (!TryComp(uid, out SurveillanceCameraComponent? camera))
return;
var ev = new SurveillanceCameraSpeechSendEvent(args.Source, args.Message);
foreach (var monitor in camera.ActiveMonitors)
{
RaiseLocalEvent(monitor, ev);
}
}
public void SetEnabled(EntityUid uid, bool value, SurveillanceCameraMicrophoneComponent? microphone = null)
{
if (!Resolve(uid, ref microphone))
return;
if (value == microphone.Enabled)
return;
microphone.Enabled = value;
if (value)
EnsureComp<ActiveListenerComponent>(uid).Range = microphone.Range;
else
RemCompDeferred<ActiveListenerComponent>(uid);
}
}
public sealed class SurveillanceCameraSpeechSendEvent : EntityEventArgs