using System.Linq; using Content.Shared.CCVar; using Content.Shared.Chemistry.Components; using Content.Shared.Nutrition.Components; using Robust.Shared.Configuration; using Robust.Shared.Prototypes; namespace Content.Shared.Nutrition.EntitySystems; /// /// Deals with flavor profiles when you eat something. /// public sealed class FlavorProfileSystem : EntitySystem { [Dependency] private readonly IPrototypeManager _prototypeManager = default!; [Dependency] private readonly IConfigurationManager _configManager = default!; private const string BackupFlavorMessage = "flavor-profile-unknown"; private int FlavorLimit => _configManager.GetCVar(CCVars.FlavorLimit); public string GetLocalizedFlavorsMessage(Entity entity, EntityUid user, Solution? solution) { HashSet flavors = new(); HashSet? ignore = null; if (Resolve(entity, ref entity.Comp, false)) { flavors = entity.Comp.Flavors; ignore = entity.Comp.IgnoreReagents; } if (solution != null) flavors.UnionWith(GetFlavorsFromReagents(solution, FlavorLimit - flavors.Count, ignore)); var ev = new FlavorProfileModificationEvent(user, flavors); RaiseLocalEvent(ev); RaiseLocalEvent(entity, ev); RaiseLocalEvent(user, ev); if (flavors.Count == 0) return Loc.GetString(BackupFlavorMessage); return FlavorsToFlavorMessage(flavors); } public string GetLocalizedFlavorsMessage(EntityUid user, Solution solution) { var flavors = GetFlavorsFromReagents(solution, FlavorLimit); var ev = new FlavorProfileModificationEvent(user, flavors); RaiseLocalEvent(user, ev, true); return FlavorsToFlavorMessage(flavors); } private string FlavorsToFlavorMessage(HashSet flavorSet) { var flavors = new List(); foreach (var flavor in flavorSet) { if (string.IsNullOrEmpty(flavor) || !_prototypeManager.TryIndex(flavor, out var flavorPrototype)) { continue; } flavors.Add(flavorPrototype); } flavors.Sort((a, b) => a.FlavorType.CompareTo(b.FlavorType)); if (flavors.Count == 1 && !string.IsNullOrEmpty(flavors[0].FlavorDescription)) { return Loc.GetString("flavor-profile", ("flavor", Loc.GetString(flavors[0].FlavorDescription))); } if (flavors.Count > 1) { var lastFlavor = Loc.GetString(flavors[^1].FlavorDescription); var allFlavors = string.Join(", ", flavors.GetRange(0, flavors.Count - 1).Select(i => Loc.GetString(i.FlavorDescription))); return Loc.GetString("flavor-profile-multiple", ("flavors", allFlavors), ("lastFlavor", lastFlavor)); } return Loc.GetString(BackupFlavorMessage); } private HashSet GetFlavorsFromReagents(Solution solution, int desiredAmount, HashSet? toIgnore = null) { var flavors = new HashSet(); foreach (var (reagent, quantity) in solution.GetReagentPrototypes(_prototypeManager)) { if (toIgnore != null && toIgnore.Contains(reagent.ID)) { continue; } if (flavors.Count == desiredAmount) { break; } // don't care if the quantity is negligible if (quantity < reagent.FlavorMinimum) { continue; } if (reagent.Flavor != null) flavors.Add(reagent.Flavor); } return flavors; } } public sealed class FlavorProfileModificationEvent : EntityEventArgs { public FlavorProfileModificationEvent(EntityUid user, HashSet flavors) { User = user; Flavors = flavors; } public EntityUid User { get; } public HashSet Flavors { get; } }