FlavorProfileSystem.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using Content.Server.Nutrition.Components;
  2. using Content.Shared.CCVar;
  3. using Content.Shared.Chemistry.Components;
  4. using Content.Shared.Nutrition;
  5. using Robust.Shared.Configuration;
  6. using Robust.Shared.Prototypes;
  7. using System.Linq;
  8. namespace Content.Server.Nutrition.EntitySystems;
  9. /// <summary>
  10. /// Deals with flavor profiles when you eat something.
  11. /// </summary>
  12. public sealed class FlavorProfileSystem : EntitySystem
  13. {
  14. [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
  15. [Dependency] private readonly IConfigurationManager _configManager = default!;
  16. private const string BackupFlavorMessage = "flavor-profile-unknown";
  17. private int FlavorLimit => _configManager.GetCVar(CCVars.FlavorLimit);
  18. public string GetLocalizedFlavorsMessage(EntityUid uid, EntityUid user, Solution solution,
  19. FlavorProfileComponent? flavorProfile = null)
  20. {
  21. if (!Resolve(uid, ref flavorProfile, false))
  22. {
  23. return Loc.GetString(BackupFlavorMessage);
  24. }
  25. var flavors = new HashSet<string>(flavorProfile.Flavors);
  26. flavors.UnionWith(GetFlavorsFromReagents(solution, FlavorLimit - flavors.Count, flavorProfile.IgnoreReagents));
  27. var ev = new FlavorProfileModificationEvent(user, flavors);
  28. RaiseLocalEvent(ev);
  29. RaiseLocalEvent(uid, ev);
  30. RaiseLocalEvent(user, ev);
  31. return FlavorsToFlavorMessage(flavors);
  32. }
  33. public string GetLocalizedFlavorsMessage(EntityUid user, Solution solution)
  34. {
  35. var flavors = GetFlavorsFromReagents(solution, FlavorLimit);
  36. var ev = new FlavorProfileModificationEvent(user, flavors);
  37. RaiseLocalEvent(user, ev, true);
  38. return FlavorsToFlavorMessage(flavors);
  39. }
  40. private string FlavorsToFlavorMessage(HashSet<string> flavorSet)
  41. {
  42. var flavors = new List<FlavorPrototype>();
  43. foreach (var flavor in flavorSet)
  44. {
  45. if (string.IsNullOrEmpty(flavor) || !_prototypeManager.TryIndex<FlavorPrototype>(flavor, out var flavorPrototype))
  46. {
  47. continue;
  48. }
  49. flavors.Add(flavorPrototype);
  50. }
  51. flavors.Sort((a, b) => a.FlavorType.CompareTo(b.FlavorType));
  52. if (flavors.Count == 1 && !string.IsNullOrEmpty(flavors[0].FlavorDescription))
  53. {
  54. return Loc.GetString("flavor-profile", ("flavor", Loc.GetString(flavors[0].FlavorDescription)));
  55. }
  56. if (flavors.Count > 1)
  57. {
  58. var lastFlavor = Loc.GetString(flavors[^1].FlavorDescription);
  59. var allFlavors = string.Join(", ", flavors.GetRange(0, flavors.Count - 1).Select(i => Loc.GetString(i.FlavorDescription)));
  60. return Loc.GetString("flavor-profile-multiple", ("flavors", allFlavors), ("lastFlavor", lastFlavor));
  61. }
  62. return Loc.GetString(BackupFlavorMessage);
  63. }
  64. private HashSet<string> GetFlavorsFromReagents(Solution solution, int desiredAmount, HashSet<string>? toIgnore = null)
  65. {
  66. var flavors = new HashSet<string>();
  67. foreach (var (reagent, quantity) in solution.GetReagentPrototypes(_prototypeManager))
  68. {
  69. if (toIgnore != null && toIgnore.Contains(reagent.ID))
  70. {
  71. continue;
  72. }
  73. if (flavors.Count == desiredAmount)
  74. {
  75. break;
  76. }
  77. // don't care if the quantity is negligible
  78. if (quantity < reagent.FlavorMinimum)
  79. {
  80. continue;
  81. }
  82. if (reagent.Flavor != null)
  83. flavors.Add(reagent.Flavor);
  84. }
  85. return flavors;
  86. }
  87. }
  88. public sealed class FlavorProfileModificationEvent : EntityEventArgs
  89. {
  90. public FlavorProfileModificationEvent(EntityUid user, HashSet<string> flavors)
  91. {
  92. User = user;
  93. Flavors = flavors;
  94. }
  95. public EntityUid User { get; }
  96. public HashSet<string> Flavors { get; }
  97. }