StutteringSystem.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System.Text;
  2. using System.Text.RegularExpressions;
  3. using Content.Server.Speech.Components;
  4. using Content.Shared.Speech.EntitySystems;
  5. using Content.Shared.StatusEffect;
  6. using Robust.Shared.Random;
  7. namespace Content.Server.Speech.EntitySystems
  8. {
  9. public sealed class StutteringSystem : SharedStutteringSystem
  10. {
  11. [Dependency] private readonly StatusEffectsSystem _statusEffectsSystem = default!;
  12. [Dependency] private readonly IRobustRandom _random = default!;
  13. // Regex of characters to stutter.
  14. private static readonly Regex Stutter = new(@"[b-df-hj-np-tv-wxyz]",
  15. RegexOptions.Compiled | RegexOptions.IgnoreCase);
  16. public override void Initialize()
  17. {
  18. SubscribeLocalEvent<StutteringAccentComponent, AccentGetEvent>(OnAccent);
  19. }
  20. public override void DoStutter(EntityUid uid, TimeSpan time, bool refresh, StatusEffectsComponent? status = null)
  21. {
  22. if (!Resolve(uid, ref status, false))
  23. return;
  24. _statusEffectsSystem.TryAddStatusEffect<StutteringAccentComponent>(uid, StutterKey, time, refresh, status);
  25. }
  26. private void OnAccent(EntityUid uid, StutteringAccentComponent component, AccentGetEvent args)
  27. {
  28. args.Message = Accentuate(args.Message, component);
  29. }
  30. public string Accentuate(string message, StutteringAccentComponent component)
  31. {
  32. var length = message.Length;
  33. var finalMessage = new StringBuilder();
  34. string newLetter;
  35. for (var i = 0; i < length; i++)
  36. {
  37. newLetter = message[i].ToString();
  38. if (Stutter.IsMatch(newLetter) && _random.Prob(component.MatchRandomProb))
  39. {
  40. if (_random.Prob(component.FourRandomProb))
  41. {
  42. newLetter = $"{newLetter}-{newLetter}-{newLetter}-{newLetter}";
  43. }
  44. else if (_random.Prob(component.ThreeRandomProb))
  45. {
  46. newLetter = $"{newLetter}-{newLetter}-{newLetter}";
  47. }
  48. else if (_random.Prob(component.CutRandomProb))
  49. {
  50. newLetter = "";
  51. }
  52. else
  53. {
  54. newLetter = $"{newLetter}-{newLetter}";
  55. }
  56. }
  57. finalMessage.Append(newLetter);
  58. }
  59. return finalMessage.ToString();
  60. }
  61. }
  62. }