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, $@"(?