Surveillance camera listening/speaking (#11640)

This commit is contained in:
Flipp Syder
2022-10-16 10:44:14 -07:00
committed by GitHub
parent 7003a35cb9
commit 6e108bd400
12 changed files with 219 additions and 5 deletions

View File

@@ -0,0 +1,48 @@
using Content.Shared.IdentityManagement;
using Content.Shared.Interaction;
namespace Content.Server.SurveillanceCamera;
public sealed class SurveillanceCameraMicrophoneSystem : EntitySystem
{
[Dependency] private SharedInteractionSystem _interactionSystem = default!;
public bool CanListen(EntityUid source, EntityUid speaker, SurveillanceCameraMicrophoneComponent? microphone = null)
{
if (!Resolve(source, ref microphone))
{
return false;
}
return microphone.Enabled
&& !microphone.BlacklistedComponents.IsValid(speaker)
&& _interactionSystem.InRangeUnobstructed(source, speaker, range: microphone.ListenRange);
}
public void RelayEntityMessage(EntityUid source, EntityUid speaker, string message, SurveillanceCameraComponent? camera = null)
{
if (!Resolve(source, ref camera))
{
return;
}
var ev = new SurveillanceCameraSpeechSendEvent(speaker, message);
foreach (var monitor in camera.ActiveMonitors)
{
RaiseLocalEvent(monitor, ev);
}
}
}
public sealed class SurveillanceCameraSpeechSendEvent : EntityEventArgs
{
public EntityUid Speaker { get; }
public string Message { get; }
public SurveillanceCameraSpeechSendEvent(EntityUid speaker, string message)
{
Speaker = speaker;
Message = message;
}
}

View File

@@ -1,13 +1,18 @@
using System.Linq;
using Content.Server.Chat.Systems;
using Content.Server.DeviceNetwork;
using Content.Server.DeviceNetwork.Systems;
using Content.Server.Power.Components;
using Content.Server.UserInterface;
using Content.Server.Wires;
using Content.Shared.Interaction;
using Content.Shared.Speech;
using Content.Shared.SurveillanceCamera;
using Robust.Server.GameObjects;
using Robust.Server.Player;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
using Robust.Shared.Timing;
namespace Content.Server.SurveillanceCamera;
@@ -370,8 +375,7 @@ public sealed class SurveillanceCameraMonitorSystem : EntitySystem
if (monitor.ActiveCamera != null)
{
EntityUid? monitorUid = monitor.Viewers.Count == 0 ? uid : null;
_surveillanceCameras.RemoveActiveViewer(monitor.ActiveCamera.Value, player, monitorUid);
_surveillanceCameras.RemoveActiveViewer(monitor.ActiveCamera.Value, player);
}
}

View File

@@ -0,0 +1,85 @@
using Content.Server.Chat.Systems;
using Content.Shared.Speech;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
using Robust.Shared.Timing;
namespace Content.Server.SurveillanceCamera;
/// <summary>
/// This handles speech for surveillance camera monitors.
/// </summary>
public sealed class SurveillanceCameraSpeakerSystem : EntitySystem
{
[Dependency] private readonly SharedAudioSystem _audioSystem = default!;
[Dependency] private readonly ChatSystem _chatSystem = default!;
[Dependency] private readonly IGameTiming _gameTiming = default!;
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IRobustRandom _random = default!;
/// <inheritdoc/>
public override void Initialize()
{
SubscribeLocalEvent<SurveillanceCameraSpeakerComponent, SurveillanceCameraSpeechSendEvent>(OnSpeechSent);
SubscribeLocalEvent<SurveillanceCameraSpeakerComponent, TransformSpeakerNameEvent>(OnTransformSpeech);
}
private void OnSpeechSent(EntityUid uid, SurveillanceCameraSpeakerComponent component,
SurveillanceCameraSpeechSendEvent args)
{
if (!component.SpeechEnabled)
{
return;
}
var time = _gameTiming.CurTime;
var cd = TimeSpan.FromSeconds(component.SpeechSoundCooldown);
// this part's mostly copied from speech
if (time - component.LastSoundPlayed < cd
&& TryComp<SharedSpeechComponent>(args.Speaker, out var speech)
&& speech.SpeechSounds != null
&& _prototypeManager.TryIndex(speech.SpeechSounds, out SpeechSoundsPrototype? speechProto))
{
var sound = args.Message[^1] switch
{
'?' => speechProto.AskSound,
'!' => speechProto.ExclaimSound,
_ => speechProto.SaySound
};
var uppercase = 0;
for (var i = 0; i < args.Message.Length; i++)
{
if (char.IsUpper(args.Message[i]))
{
uppercase++;
}
}
if (uppercase > args.Message.Length / 2)
{
sound = speechProto.ExclaimSound;
}
var scale = (float) _random.NextGaussian(1, speechProto.Variation);
var param = speech.AudioParams.WithPitchScale(scale);
_audioSystem.PlayPvs(sound, uid, param);
component.LastSoundPlayed = time;
}
var nameEv = new TransformSpeakerNameEvent(args.Speaker, Name(args.Speaker));
RaiseLocalEvent(args.Speaker, nameEv);
component.LastSpokenNames.Enqueue(nameEv.Name);
_chatSystem.TrySendInGameICMessage(uid, args.Message, InGameICChatType.Speak, false);
}
private void OnTransformSpeech(EntityUid uid, SurveillanceCameraSpeakerComponent component,
TransformSpeakerNameEvent args)
{
args.Name = Loc.GetString("surveillance-camera-microphone-message", ("speaker", Name(uid)),
("originalName", component.LastSpokenNames.Dequeue()));
}
}