Files
tbd-station-14/Content.Server/Speech/SpeechNoiseSystem.cs
metalgearsloth 3da454140d Add department-specific radio channels (#9061)
* Add department-specific radio channels

This commit adds working department-specific radio channels, while
minimizing damage to the current codebase. It is expected that a future
refactor will clean this up a bit.

ChatSystem now has a RadioPrefix() method that recognizes
department-specific channels (e.g. ":e" and ":m") in addition to the
global channel (";"). It strips the prefix from the message and assigns
messages an integer representing the destination channel, if any.

IListen and IRadio now accept optional 'channel' arguments with this
channel in mind.

The ugly is that the integer channel number is hard-coded and also shows
up in chat.

Comms are not modeled at this time. You cannot break comms (yet).

All headsets have channels soldered into them. You cannot change
encryption keys to hop on new channels. Steal a headset instead.

* Remove debugging print

* Convert to prototypes

* Use prototype names in headset prototype

* Adjust list style

* Document prototype fields

* cringe

* some cleanup

* colours

* Remove alphas at least

* cc

Co-authored-by: Kevin Zheng <kevinz5000@gmail.com>
2022-06-23 20:11:03 +10:00

73 lines
2.6 KiB
C#

using Robust.Shared.Audio;
using Content.Server.Chat;
using Content.Server.Chat.Systems;
using Content.Shared.Speech;
using Robust.Shared.Player;
using Robust.Shared.Prototypes;
using Robust.Shared.Timing;
using Robust.Shared.Random;
namespace Content.Server.Speech
{
public sealed class SpeechSoundSystem : EntitySystem
{
[Dependency] private readonly IGameTiming _gameTiming = default!;
[Dependency] private readonly IPrototypeManager _protoManager = default!;
[Dependency] private readonly IRobustRandom _random = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<SharedSpeechComponent, EntitySpokeEvent>(OnEntitySpoke);
}
private void OnEntitySpoke(EntityUid uid, SharedSpeechComponent component, EntitySpokeEvent args)
{
if (component.SpeechSounds == null) return;
var currentTime = _gameTiming.CurTime;
var cooldown = TimeSpan.FromSeconds(component.SoundCooldownTime);
// Ensure more than the cooldown time has passed since last speaking
if (currentTime - component.LastTimeSoundPlayed < cooldown) return;
// Play speech sound
string contextSound;
var prototype = _protoManager.Index<SpeechSoundsPrototype>(component.SpeechSounds);
var message = args.Message;
// Different sounds for ask/exclaim based on last character
switch (args.Message[^1])
{
case '?':
contextSound = prototype.AskSound.GetSound();
break;
case '!':
contextSound = prototype.ExclaimSound.GetSound();
break;
default:
contextSound = prototype.SaySound.GetSound();
break;
}
// Use exclaim sound if most characters are uppercase.
int uppercaseCount = 0;
for (int i = 0; i < message.Length; i++)
{
if (char.IsUpper(message[i])) uppercaseCount++;
}
if (uppercaseCount > (message.Length / 2))
{
contextSound = contextSound = prototype.ExclaimSound.GetSound();
}
var scale = (float) _random.NextGaussian(1, prototype.Variation);
var pitchedAudioParams = component.AudioParams.WithPitchScale(scale);
component.LastTimeSoundPlayed = currentTime;
SoundSystem.Play(contextSound, Filter.Pvs(uid, entityManager: EntityManager), uid, pitchedAudioParams);
}
}
}