RatvarianLanguageSystem.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System.Text;
  2. using System.Text.RegularExpressions;
  3. using Content.Shared.Speech.Components;
  4. using Content.Shared.Speech.EntitySystems;
  5. using Content.Shared.StatusEffect;
  6. namespace Content.Server.Speech.EntitySystems;
  7. public sealed class RatvarianLanguageSystem : SharedRatvarianLanguageSystem
  8. {
  9. [Dependency] private readonly StatusEffectsSystem _statusEffects = default!;
  10. [ValidatePrototypeId<StatusEffectPrototype>]
  11. private const string RatvarianKey = "RatvarianLanguage";
  12. // This is the word of Ratvar and those who speak it shall abide by His rules:
  13. /*
  14. * Any time the word "of" occurs, it's linked to the previous word by a hyphen: "I am-of Ratvar"
  15. * Any time "th", followed by any two letters occurs, you add a grave (`) between those two letters: "Thi`s"
  16. * In the same vein, any time "ti" followed by one letter occurs, you add a grave (`) between "i" and the letter: "Ti`me"
  17. * Wherever "te" or "et" appear and there is another letter next to the "e", add a hyphen between "e" and the letter: "M-etal/Greate-r"
  18. * Where "gua" appears, add a hyphen between "gu" and "a": "Gu-ard"
  19. * Where the word "and" appears it's linked to all surrounding words by hyphens: "Sword-and-shield"
  20. * Where the word "to" appears, it's linked to the following word by a hyphen: "to-use"
  21. * Where the word "my" appears, it's linked to the following word by a hyphen: "my-light"
  22. * Any Ratvarian proper noun is not translated: Ratvar, Nezbere, Sevtug, Nzcrentr and Inath-neq
  23. * This only applies if they're being used as a proper noun: armorer/Nezbere
  24. */
  25. private static Regex THPattern = new Regex(@"th\w\B", RegexOptions.Compiled | RegexOptions.IgnoreCase);
  26. private static Regex ETPattern = new Regex(@"\Bet", RegexOptions.Compiled);
  27. private static Regex TEPattern = new Regex(@"te\B",RegexOptions.Compiled);
  28. private static Regex OFPattern = new Regex(@"(\s)(of)");
  29. private static Regex TIPattern = new Regex(@"ti\B", RegexOptions.Compiled | RegexOptions.IgnoreCase);
  30. private static Regex GUAPattern = new Regex(@"(gu)(a)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
  31. private static Regex ANDPattern = new Regex(@"\b(\s)(and)(\s)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
  32. private static Regex TOMYPattern = new Regex(@"(to|my)\s", RegexOptions.Compiled | RegexOptions.IgnoreCase);
  33. private static Regex ProperNouns = new Regex(@"(ratvar)|(nezbere)|(sevtuq)|(nzcrentr)|(inath-neq)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
  34. public override void Initialize()
  35. {
  36. // Activate before other modifications so translation works properly
  37. SubscribeLocalEvent<RatvarianLanguageComponent, AccentGetEvent>(OnAccent, before: new[] {typeof(SharedSlurredSystem), typeof(SharedStutteringSystem)});
  38. }
  39. public override void DoRatvarian(EntityUid uid, TimeSpan time, bool refresh, StatusEffectsComponent? status = null)
  40. {
  41. if (!Resolve(uid, ref status, false))
  42. return;
  43. _statusEffects.TryAddStatusEffect<RatvarianLanguageComponent>(uid, RatvarianKey, time, refresh, status);
  44. }
  45. private void OnAccent(EntityUid uid, RatvarianLanguageComponent component, AccentGetEvent args)
  46. {
  47. args.Message = Translate(args.Message);
  48. }
  49. private string Translate(string message)
  50. {
  51. var ruleTranslation = message;
  52. var finalMessage = new StringBuilder();
  53. var newWord = new StringBuilder();
  54. ruleTranslation = THPattern.Replace(ruleTranslation, "$&`");
  55. ruleTranslation = TEPattern.Replace(ruleTranslation, "$&-");
  56. ruleTranslation = ETPattern.Replace(ruleTranslation, "-$&");
  57. ruleTranslation = OFPattern.Replace(ruleTranslation, "-$2");
  58. ruleTranslation = TIPattern.Replace(ruleTranslation, "$&`");
  59. ruleTranslation = GUAPattern.Replace(ruleTranslation, "$1-$2");
  60. ruleTranslation = ANDPattern.Replace(ruleTranslation, "-$2-");
  61. ruleTranslation = TOMYPattern.Replace(ruleTranslation, "$1-");
  62. var temp = ruleTranslation.Split(' ');
  63. foreach (var word in temp)
  64. {
  65. newWord.Clear();
  66. if (ProperNouns.IsMatch(word))
  67. newWord.Append(word);
  68. else
  69. {
  70. for (int i = 0; i < word.Length; i++)
  71. {
  72. var letter = word[i];
  73. if (letter >= 97 && letter <= 122)
  74. {
  75. var letterRot = letter + 13;
  76. if (letterRot > 122)
  77. letterRot -= 26;
  78. newWord.Append((char) letterRot);
  79. }
  80. else if (letter >= 65 && letter <= 90)
  81. {
  82. var letterRot = letter + 13;
  83. if (letterRot > 90)
  84. letterRot -= 26;
  85. newWord.Append((char) letterRot);
  86. }
  87. else
  88. {
  89. newWord.Append(word[i]);
  90. }
  91. }
  92. }
  93. finalMessage.Append(newWord + " ");
  94. }
  95. return finalMessage.ToString().Trim();
  96. }
  97. }