Files
wwdpublic/Content.Server/Speech/SpeechNoiseSystem.cs
RadsammyT 4920a5fdfc Make Non-Verbal Langs Not Play Speech (#1448)
<!--
This is a semi-strict format, you can add/remove sections as needed but
the order/format should be kept the same
Remove these comments before submitting
-->

# Description

<!--
Explain this PR in as much detail as applicable

Some example prompts to consider:
How might this affect the game? The codebase?
What might be some alternatives to this?
How/Who does this benefit/hurt [the game/codebase]?
-->

Makes non-verbal languages no longer emit speech sounds. Ported from
[Goob-Station-MRP#29](https://github.com/Goob-Station/Goob-Station-MRP/pull/29)
at @angelofallars's request.

---

<!--
This is default collapsed, readers click to expand it and see all your
media
The PR media section can get very large at times, so this is a good way
to keep it clean
The title is written using HTML tags
The title must be within the <summary> tags or you won't see it
-->

<details><summary><h1>Media</h1></summary>
<p>

![PR
Example](https://github.com/user-attachments/assets/8be6b705-eff7-4e2e-ab53-5648dccfc7eb)

</p>
</details>

---

# Changelog

<!--
You can add an author after the `🆑` to change the name that appears
in the changelog (ex: `🆑 Death`)
Leaving it blank will default to your GitHub display name
This includes all available types for the changelog
-->

🆑
- tweak: Languages that don't require speech (I.E. Sign Language) will
no longer play speech sounds.

---------

Signed-off-by: RadsammyT <32146976+RadsammyT@users.noreply.github.com>
Co-authored-by: Skubman <ba.fallaria@gmail.com>
Co-authored-by: flyingkarii <123355664+flyingkarii@users.noreply.github.com>
Co-authored-by: sleepyyapril <123355664+sleepyyapril@users.noreply.github.com>
Co-authored-by: SimpleStation Changelogs <SimpleStation14@users.noreply.github.com>
(cherry picked from commit 990a57cc77fd535f39826b125b59054e75dfcfb9)
2025-01-14 01:43:48 +03:00

79 lines
2.7 KiB
C#

using Robust.Shared.Audio;
using Content.Server.Chat;
using Content.Server.Chat.Systems;
using Content.Shared.Speech;
using Robust.Shared.Audio.Systems;
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!;
[Dependency] private readonly SharedAudioSystem _audio = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<SpeechComponent, EntitySpokeEvent>(OnEntitySpoke);
}
public SoundSpecifier? GetSpeechSound(Entity<SpeechComponent> ent, string message)
{
if (ent.Comp.SpeechSounds == null)
return null;
// Play speech sound
SoundSpecifier? contextSound;
var prototype = _protoManager.Index<SpeechSoundsPrototype>(ent.Comp.SpeechSounds);
// Different sounds for ask/exclaim based on last character
contextSound = message[^1] switch
{
'?' => prototype.AskSound,
'!' => prototype.ExclaimSound,
_ => prototype.SaySound
};
// 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 = prototype.ExclaimSound;
}
var scale = (float) _random.NextGaussian(1, prototype.Variation);
contextSound.Params = ent.Comp.AudioParams.WithPitchScale(scale);
return contextSound;
}
private void OnEntitySpoke(EntityUid uid, SpeechComponent component, EntitySpokeEvent args)
{
if (component.SpeechSounds == null || !args.Language.SpeechOverride.RequireSpeech)
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;
var sound = GetSpeechSound((uid, component), args.Message);
component.LastTimeSoundPlayed = currentTime;
_audio.PlayPvs(sound, uid);
}
}
}