1
0

MobsterAccentSystem.cs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System.Linq;
  2. using System.Text.RegularExpressions;
  3. using Content.Server.Speech.Components;
  4. using Robust.Shared.Random;
  5. namespace Content.Server.Speech.EntitySystems;
  6. public sealed class MobsterAccentSystem : EntitySystem
  7. {
  8. private static readonly Regex RegexIng = new(@"(?<=\w\w)(in)g(?!\w)", RegexOptions.IgnoreCase);
  9. private static readonly Regex RegexLowerOr = new(@"(?<=\w)o[Rr](?=\w)");
  10. private static readonly Regex RegexUpperOr = new(@"(?<=\w)O[Rr](?=\w)");
  11. private static readonly Regex RegexLowerAr = new(@"(?<=\w)a[Rr](?=\w)");
  12. private static readonly Regex RegexUpperAr = new(@"(?<=\w)A[Rr](?=\w)");
  13. private static readonly Regex RegexFirstWord = new(@"^(\S+)");
  14. private static readonly Regex RegexLastWord = new(@"(\S+)$");
  15. [Dependency] private readonly IRobustRandom _random = default!;
  16. [Dependency] private readonly ReplacementAccentSystem _replacement = default!;
  17. public override void Initialize()
  18. {
  19. base.Initialize();
  20. SubscribeLocalEvent<MobsterAccentComponent, AccentGetEvent>(OnAccentGet);
  21. }
  22. public string Accentuate(string message, MobsterAccentComponent component)
  23. {
  24. // Order:
  25. // Do text manipulations first
  26. // Then prefix/suffix funnyies
  27. // direct word replacements
  28. var msg = _replacement.ApplyReplacements(message, "mobster");
  29. // thinking -> thinkin'
  30. // king -> king
  31. //Uses captures groups to make sure the captialization of IN is kept
  32. msg = RegexIng.Replace(msg, "$1'");
  33. // or -> uh and ar -> ah in the middle of words (fuhget, tahget)
  34. msg = RegexLowerOr.Replace(msg, "uh");
  35. msg = RegexUpperOr.Replace(msg, "UH");
  36. msg = RegexLowerAr.Replace(msg, "ah");
  37. msg = RegexUpperAr.Replace(msg, "AH");
  38. // Prefix
  39. if (_random.Prob(0.15f))
  40. {
  41. //Checks if the first word of the sentence is all caps
  42. //So the prefix can be allcapped and to not resanitize the captial
  43. var firstWordAllCaps = !RegexFirstWord.Match(msg).Value.Any(char.IsLower);
  44. var pick = _random.Next(1, 2);
  45. // Reverse sanitize capital
  46. var prefix = Loc.GetString($"accent-mobster-prefix-{pick}");
  47. if (!firstWordAllCaps)
  48. msg = msg[0].ToString().ToLower() + msg.Remove(0, 1);
  49. else
  50. prefix = prefix.ToUpper();
  51. msg = prefix + " " + msg;
  52. }
  53. // Sanitize capital again, in case we substituted a word that should be capitalized
  54. msg = msg[0].ToString().ToUpper() + msg.Remove(0, 1);
  55. // Suffixes
  56. if (_random.Prob(0.4f))
  57. {
  58. //Checks if the last word of the sentence is all caps
  59. //So the suffix can be allcapped
  60. var lastWordAllCaps = !RegexLastWord.Match(msg).Value.Any(char.IsLower);
  61. var suffix = "";
  62. if (component.IsBoss)
  63. {
  64. var pick = _random.Next(1, 4);
  65. suffix = Loc.GetString($"accent-mobster-suffix-boss-{pick}");
  66. }
  67. else
  68. {
  69. var pick = _random.Next(1, 3);
  70. suffix = Loc.GetString($"accent-mobster-suffix-minion-{pick}");
  71. }
  72. if (lastWordAllCaps)
  73. suffix = suffix.ToUpper();
  74. msg += suffix;
  75. }
  76. return msg;
  77. }
  78. private void OnAccentGet(EntityUid uid, MobsterAccentComponent component, AccentGetEvent args)
  79. {
  80. args.Message = Accentuate(args.Message, component);
  81. }
  82. }