CatStation/Content.Shared/Nutrition/EntitySystems/FlavorProfileSystem.cs
Whatstone a8453723d8
Cleanup: ProtoMan Part 1 - remove Shared EntitySystem IPrototypeManager instances (#44386)
* Remove Shared System IPrototypeManager instances

* Remove RecipeManager IPrototypeManager instance

* Fix ItemSystem IPrototypeManager refs, use TryComp

* SharedItemSystem changes, EntWhitelist CompName

* HasComponent->HasComp
2026-06-29 10:18:44 +00:00

121 lines
3.8 KiB
C#

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;
/// <summary>
/// Deals with flavor profiles when you eat something.
/// </summary>
public sealed partial class FlavorProfileSystem : EntitySystem
{
[Dependency] private IConfigurationManager _configManager = default!;
private const string BackupFlavorMessage = "flavor-profile-unknown";
private int FlavorLimit => _configManager.GetCVar(CCVars.FlavorLimit);
public string GetLocalizedFlavorsMessage(Entity<FlavorProfileComponent?> entity, EntityUid user, Solution? solution)
{
HashSet<ProtoId<FlavorPrototype>> flavors = new();
HashSet<string>? 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<ProtoId<FlavorPrototype>> flavorSet)
{
var flavors = new List<FlavorPrototype>(flavorSet.Count);
foreach (var flavor in flavorSet)
{
flavors.Add(ProtoMan.Index(flavor));
}
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<ProtoId<FlavorPrototype>> GetFlavorsFromReagents(Solution solution, int desiredAmount, HashSet<string>? toIgnore = null)
{
var flavors = new HashSet<ProtoId<FlavorPrototype>>();
foreach (var (reagent, quantity) in solution.GetReagentPrototypes(ProtoMan))
{
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.Value);
}
return flavors;
}
}
public sealed class FlavorProfileModificationEvent : EntityEventArgs
{
public FlavorProfileModificationEvent(EntityUid user, HashSet<ProtoId<FlavorPrototype>> flavors)
{
User = user;
Flavors = flavors;
}
public EntityUid User { get; }
public HashSet<ProtoId<FlavorPrototype>> Flavors { get; }
}