From 5ab1cc0c846775e438517bbff9d7713eeb5ef85d Mon Sep 17 00:00:00 2001 From: Ed <96445749+TheShuEd@users.noreply.github.com> Date: Sat, 4 May 2024 14:11:46 +0300 Subject: [PATCH] Pathological Liar (#27618) * content * upgrade * n't * ye ya * Update speech-liar.ftl * Mith replacement ideas * fix * more! * Revert "more!" This reverts commit 6d10bdf694985c525a2b451ed39380f975059b44. * Update Content.Server/Speech/Components/ReplacementAccentComponent.cs --------- Co-authored-by: Kara --- .../Components/ReplacementAccentComponent.cs | 6 + .../EntitySystems/ReplacementAccentSystem.cs | 15 +- Resources/Locale/en-US/speech/speech-liar.ftl | 132 ++++++++++++++++++ Resources/Locale/en-US/traits/traits.ftl | 3 + .../Prototypes/Accents/word_replacements.yml | 47 +++++++ Resources/Prototypes/Traits/neutral.yml | 9 ++ 6 files changed, 210 insertions(+), 2 deletions(-) create mode 100644 Resources/Locale/en-US/speech/speech-liar.ftl diff --git a/Content.Server/Speech/Components/ReplacementAccentComponent.cs b/Content.Server/Speech/Components/ReplacementAccentComponent.cs index ac4e9fbafe..e7f57b80d0 100644 --- a/Content.Server/Speech/Components/ReplacementAccentComponent.cs +++ b/Content.Server/Speech/Components/ReplacementAccentComponent.cs @@ -32,5 +32,11 @@ namespace Content.Server.Speech.Components { [DataField("accent", customTypeSerializer: typeof(PrototypeIdSerializer), required: true)] public string Accent = default!; + + /// + /// Allows you to substitute words, not always, but with some chance + /// + [DataField] + public float ReplacementChance = 1f; } } diff --git a/Content.Server/Speech/EntitySystems/ReplacementAccentSystem.cs b/Content.Server/Speech/EntitySystems/ReplacementAccentSystem.cs index 36fa7e07ad..c332c6cda8 100644 --- a/Content.Server/Speech/EntitySystems/ReplacementAccentSystem.cs +++ b/Content.Server/Speech/EntitySystems/ReplacementAccentSystem.cs @@ -24,6 +24,9 @@ namespace Content.Server.Speech.EntitySystems private void OnAccent(EntityUid uid, ReplacementAccentComponent component, AccentGetEvent args) { + if (!_random.Prob(component.ReplacementChance)) + return; + args.Message = ApplyReplacements(args.Message, component.Accent); } @@ -46,6 +49,12 @@ namespace Content.Server.Speech.EntitySystems if (prototype.WordReplacements == null) return message; + // Prohibition of repeated word replacements. + // All replaced words placed in the final message are placed here as dashes (___) with the same length. + // The regex search goes through this buffer message, from which the already replaced words are crossed out, + // ensuring that the replaced words cannot be replaced again. + var maskMessage = message; + foreach (var (first, replace) in prototype.WordReplacements) { var f = _loc.GetString(first); @@ -53,10 +62,10 @@ namespace Content.Server.Speech.EntitySystems // this is kind of slow but its not that bad // essentially: go over all matches, try to match capitalization where possible, then replace // rather than using regex.replace - for (int i = Regex.Count(message, $@"(? 0; i--) + for (int i = Regex.Count(maskMessage, $@"(? 0; i--) { // fetch the match again as the character indices may have changed - Match match = Regex.Match(message, $@"(?