ParrotAccentSystem.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 partial class ParrotAccentSystem : EntitySystem
  7. {
  8. private static readonly Regex WordCleanupRegex = new Regex("[^A-Za-z0-9 -]");
  9. [Dependency] private readonly IRobustRandom _random = default!;
  10. public override void Initialize()
  11. {
  12. base.Initialize();
  13. SubscribeLocalEvent<ParrotAccentComponent, AccentGetEvent>(OnAccentGet);
  14. }
  15. private void OnAccentGet(Entity<ParrotAccentComponent> entity, ref AccentGetEvent args)
  16. {
  17. args.Message = Accentuate(entity, args.Message);
  18. }
  19. public string Accentuate(Entity<ParrotAccentComponent> entity, string message)
  20. {
  21. // Sometimes repeat the longest word at the end of the message, after a squawk! SQUAWK! Sometimes!
  22. if (_random.Prob(entity.Comp.LongestWordRepeatChance))
  23. {
  24. // Don't count non-alphanumeric characters as parts of words
  25. var cleaned = WordCleanupRegex.Replace(message, string.Empty);
  26. // Split on whitespace and favor words towards the end of the message
  27. var words = cleaned.Split(null).Reverse();
  28. // Find longest word
  29. var longest = words.MaxBy(word => word.Length);
  30. if (longest?.Length >= entity.Comp.LongestWordMinLength)
  31. {
  32. message = EnsurePunctuation(message);
  33. // Capitalize the first letter of the repeated word
  34. longest = string.Concat(longest[0].ToString().ToUpper(), longest.AsSpan(1));
  35. message = string.Format("{0} {1} {2}!", message, GetRandomSquawk(entity), longest);
  36. return message; // No more changes, or it's too much
  37. }
  38. }
  39. if (_random.Prob(entity.Comp.SquawkPrefixChance))
  40. {
  41. // AWWK! Sometimes add a squawk at the begining of the message
  42. message = string.Format("{0} {1}", GetRandomSquawk(entity), message);
  43. }
  44. else
  45. {
  46. // Otherwise add a squawk at the end of the message! RAWWK!
  47. message = EnsurePunctuation(message);
  48. message = string.Format("{0} {1}", message, GetRandomSquawk(entity));
  49. }
  50. return message;
  51. }
  52. /// <summary>
  53. /// Adds a "!" to the end of the string, if there isn't already a sentence-ending punctuation mark.
  54. /// </summary>
  55. private string EnsurePunctuation(string message)
  56. {
  57. if (!message.EndsWith('!') && !message.EndsWith('?') && !message.EndsWith('.'))
  58. return message + '!';
  59. return message;
  60. }
  61. /// <summary>
  62. /// Returns a random, localized squawk sound.
  63. /// </summary>
  64. private string GetRandomSquawk(Entity<ParrotAccentComponent> entity)
  65. {
  66. return Loc.GetString(_random.Pick(entity.Comp.Squawks));
  67. }
  68. }