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

@@ -0,0 +1,61 @@
using Content.Server.Chat.Systems;
using Content.Server.Speech.Components;
namespace Content.Server.Speech.EntitySystems;
/// <summary>
/// This system redirects local chat messages to listening entities (e.g., radio microphones).
/// </summary>
public sealed class ListeningSystem : EntitySystem
{
[Dependency] private readonly SharedTransformSystem _xforms = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<EntitySpokeEvent>(OnSpeak);
}
private void OnSpeak(EntitySpokeEvent ev)
{
PingListeners(ev.Source, ev.Message, ev.ObfuscatedMessage);
}
public void PingListeners(EntityUid source, string message, string? obfuscatedMessage)
{
// TODO whispering / audio volume? Microphone sensitivity?
// for now, whispering just arbitrarily reduces the listener's max range.
var xformQuery = GetEntityQuery<TransformComponent>();
var sourceXform = xformQuery.GetComponent(source);
var sourcePos = _xforms.GetWorldPosition(sourceXform, xformQuery);
var attemptEv = new ListenAttemptEvent(source);
var ev = new ListenEvent(message, source);
var obfuscatedEv = obfuscatedMessage == null ? null : new ListenEvent(obfuscatedMessage, source);
foreach (var (listener, xform) in EntityQuery<ActiveListenerComponent, TransformComponent>())
{
if (xform.MapID != sourceXform.MapID)
return;
// range checks
// TODO proper speech occlusion
var distance = (sourcePos - _xforms.GetWorldPosition(xform, xformQuery)).LengthSquared;
if (distance > listener.Range * listener.Range)
continue;
RaiseLocalEvent(listener.Owner, attemptEv);
if (attemptEv.Cancelled)
{
attemptEv.Uncancel();
continue;
}
if (obfuscatedEv != null && distance > ChatSystem.WhisperRange)
RaiseLocalEvent(listener.Owner, obfuscatedEv);
else
RaiseLocalEvent(listener.Owner, ev);
}
}
}