1
0

GermanAccentSystem.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System.Text;
  2. using Content.Server.Speech.Components;
  3. using Robust.Shared.Random;
  4. using System.Text.RegularExpressions;
  5. namespace Content.Server.Speech.EntitySystems;
  6. public sealed class GermanAccentSystem : EntitySystem
  7. {
  8. [Dependency] private readonly IRobustRandom _random = default!;
  9. [Dependency] private readonly ReplacementAccentSystem _replacement = default!;
  10. private static readonly Regex RegexTh = new(@"(?<=\s|^)th", RegexOptions.IgnoreCase);
  11. private static readonly Regex RegexThe = new(@"(?<=\s|^)the(?=\s|$)", RegexOptions.IgnoreCase);
  12. public override void Initialize()
  13. {
  14. SubscribeLocalEvent<GermanAccentComponent, AccentGetEvent>(OnAccent);
  15. }
  16. public string Accentuate(string message)
  17. {
  18. var msg = message;
  19. // rarely, "the" should become "das" instead of "ze"
  20. // TODO: The ReplacementAccentSystem should have random replacements this built-in.
  21. foreach (Match match in RegexThe.Matches(msg))
  22. {
  23. if (_random.Prob(0.3f))
  24. {
  25. // just shift T, H and E over to D, A and S to preserve capitalization
  26. msg = msg.Substring(0, match.Index) +
  27. (char)(msg[match.Index] - 16) +
  28. (char)(msg[match.Index + 1] - 7) +
  29. (char)(msg[match.Index + 2] + 14) +
  30. msg.Substring(match.Index + 3);
  31. }
  32. }
  33. // now, apply word replacements
  34. msg = _replacement.ApplyReplacements(msg, "german");
  35. // replace th with zh (for zhis, zhat, etc. the => ze is handled by replacements already)
  36. var msgBuilder = new StringBuilder(msg);
  37. foreach (Match match in RegexTh.Matches(msg))
  38. {
  39. // just shift the T over to a Z to preserve capitalization
  40. msgBuilder[match.Index] = (char) (msgBuilder[match.Index] + 6);
  41. }
  42. // Random Umlaut Time! (The joke outweighs the emotional damage this inflicts on actual Germans)
  43. var umlautCooldown = 0;
  44. for (var i = 0; i < msgBuilder.Length; i++)
  45. {
  46. if (umlautCooldown == 0)
  47. {
  48. if (_random.Prob(0.1f)) // 10% of all eligible vowels become umlauts)
  49. {
  50. msgBuilder[i] = msgBuilder[i] switch
  51. {
  52. 'A' => 'Ä',
  53. 'a' => 'ä',
  54. 'O' => 'Ö',
  55. 'o' => 'ö',
  56. 'U' => 'Ü',
  57. 'u' => 'ü',
  58. _ => msgBuilder[i]
  59. };
  60. umlautCooldown = 4;
  61. }
  62. }
  63. else
  64. {
  65. umlautCooldown--;
  66. }
  67. }
  68. return msgBuilder.ToString();
  69. }
  70. private void OnAccent(Entity<GermanAccentComponent> ent, ref AccentGetEvent args)
  71. {
  72. args.Message = Accentuate(args.Message);
  73. }
  74. }