Files
OldThink/Content.Server/Nutrition/EntitySystems/FoodSystem.cs

500 lines
20 KiB
C#
Raw Normal View History

using System.Linq;
using Content.Server.Body.Components;
using Content.Server.Body.Systems;
using Content.Server.Chemistry.EntitySystems;
using Content.Server.Nutrition.Components;
using Content.Server.Popups;
2023-05-06 10:23:05 +03:00
using Content.Server.Stack;
2021-11-29 16:27:15 +13:00
using Content.Shared.Administration.Logs;
using Content.Shared.Body.Components;
using Content.Shared.Body.Organ;
using Content.Shared.Chemistry;
using Content.Shared.Chemistry.Reagent;
2021-11-29 16:27:15 +13:00
using Content.Shared.Database;
using Content.Shared.DoAfter;
using Content.Shared.FixedPoint;
using Content.Shared.Hands.Components;
2022-07-29 14:13:12 +12:00
using Content.Shared.Hands.EntitySystems;
using Content.Shared.IdentityManagement;
using Content.Shared.Interaction;
2022-07-29 14:13:12 +12:00
using Content.Shared.Interaction.Events;
using Content.Shared.Inventory;
using Content.Shared.Mobs.Components;
using Content.Shared.Mobs.Systems;
using Content.Shared.Nutrition;
using Content.Shared.Verbs;
2023-05-06 10:23:05 +03:00
using Content.Shared.Stacks;
using Robust.Shared.Audio;
using Robust.Shared.Player;
using Robust.Shared.Utility;
2023-08-03 22:21:28 -07:00
using Content.Shared.Tag;
using Content.Server.Storage.Components;
namespace Content.Server.Nutrition.EntitySystems
{
/// <summary>
/// Handles feeding attempts both on yourself and on the target.
/// </summary>
public sealed class FoodSystem : EntitySystem
{
[Dependency] private readonly SolutionContainerSystem _solutionContainerSystem = default!;
Flavor profiles (#10991) * flavor profiles TODO: every single flavor! yeah!!! * adds basic localization, and flavors/lastFlavor values for when you get the flavor profile message * multiple and single flavor messages * start on flavor localization, multiple flavors in localized flavors * flavor prototypes * a few more flavors, descriptions on what each section of the flavor file should be doing * localization for flavor profiles in drink/food system * adds an event that allows a flavor profile list to be transformed base on the user entity * raises it on the food entity too * changes a field in flavor, adds some more flavors, starts adding flavor prototypes * adds basic flavors to several entities, and consumable drinks, renames flavor field to 'flavors' * changes call ordering in flavorprofile, adds flavor to ignored components server-side flavor is really just a popup message, and those are all processed server-side * fixes where food tried to get the flavor of the user instead of the food * single flavors will now get the localized string * getting the flavor message now ensures that flavors are deduplicated * makes flavor processing more strictly unique bu making everything hashsets * yeah, that could just not have distinctby now * adds flavorprofile directly to food base instead for generic food taste * FlavorProfileModificationEvent now passes a hashset of strings and not flavorprototypes * flavorprofilesystem now broadcasts the flavor profile modification event * adds more flavors to the flavor profile loc file * skips a flavor, if the flavor string is null/empty * adds some more flavors, adds generic medicine flavor to medicinal chemicals * more food flavors, adds flavors to swallowing * adds some cocktails to the set of flavor profiles * regenerates flavor prototypes * adds flavor type to all flavors, adds whitespace between variants * adds more flavors, adds flavors to several chemicals and food items this is the part that took the longest * changes backup flavor message * spelling mistake * more flavors, and flavors on food * readds all the type fields, whoops * fixes localization strings for forcefeeding food/drink * fixes multiple flavor profile * adds flavor limit for flavors * makes that fetch the cvardef instead
2022-09-08 16:14:49 -07:00
[Dependency] private readonly FlavorProfileSystem _flavorProfileSystem = default!;
[Dependency] private readonly BodySystem _bodySystem = default!;
[Dependency] private readonly StomachSystem _stomachSystem = default!;
[Dependency] private readonly PopupSystem _popupSystem = default!;
[Dependency] private readonly MobStateSystem _mobStateSystem = default!;
[Dependency] private readonly UtensilSystem _utensilSystem = default!;
[Dependency] private readonly SharedDoAfterSystem _doAfterSystem = default!;
[Dependency] private readonly ISharedAdminLogManager _adminLogger = default!;
get that crap outta here (completely rewrites inventorysystem) (#5807) * some work * equip: done unequip: todo * unequipping done & refactored events * workin * movin * reee namespaces * stun * mobstate * fixes * some work on events * removes serverside itemcomp & misc fixes * work * smol merge fix * ports template to prototype & finishes ui * moves relay & adds containerenumerator * actions & cuffs * my god what is actioncode * more fixes * im loosing my grasp on reality * more fixes * more work * explosions * yes * more work * more fixes * merge master & misc fixed because i forgot to commit before merging master * more fixes * fixes * moar * more work * moar fixes * suffixmap * more work on client * motivation low * no. no containers * mirroring client to server * fixes * move serverinvcomp * serverinventorycomponent is dead * gaming * only strippable & ai left... * only ai and richtext left * fixes ai * fixes * fixes sprite layers * more fixes * resolves optional * yes * stable:tm: * fixes * moar fixes * moar * fix some tests * lmao * no comment * good to merge:tm: * fixes build but for real * adresses some reviews * adresses some more reviews * nullables, yo * fixes lobbyscreen * timid refactor to differentiate actor & target * adresses more reviews * more * my god what a mess * removed the rest of duplicates * removed duplicate slotflags and renamed shoes to feet * removes another unused one * yes * fixes lobby & makes tryunequip return unequipped item * fixes * some funny renames * fixes * misc improvements to attemptevents * fixes * merge fixes Co-authored-by: Paul Ritter <ritter.paul1@gmail.com>
2021-12-30 22:56:10 +01:00
[Dependency] private readonly InventorySystem _inventorySystem = default!;
[Dependency] private readonly SharedInteractionSystem _interactionSystem = default!;
2022-03-17 20:13:31 +13:00
[Dependency] private readonly SharedHandsSystem _handsSystem = default!;
[Dependency] private readonly ReactiveSystem _reaction = default!;
[Dependency] private readonly SharedAudioSystem _audio = default!;
2023-05-06 10:23:05 +03:00
[Dependency] private readonly StackSystem _stack = default!;
2023-08-03 22:21:28 -07:00
[Dependency] private readonly TagSystem _tags = default!;
2023-05-07 14:58:20 +12:00
public const float MaxFeedDistance = 1.0f;
public override void Initialize()
{
base.Initialize();
// TODO add InteractNoHandEvent for entities like mice.
SubscribeLocalEvent<FoodComponent, UseInHandEvent>(OnUseFoodInHand);
SubscribeLocalEvent<FoodComponent, AfterInteractEvent>(OnFeedFood);
2022-03-28 13:53:20 -07:00
SubscribeLocalEvent<FoodComponent, GetVerbsEvent<AlternativeVerb>>(AddEatVerb);
SubscribeLocalEvent<FoodComponent, ConsumeDoAfterEvent>(OnDoAfter);
2021-12-05 07:56:27 +13:00
SubscribeLocalEvent<InventoryComponent, IngestionAttemptEvent>(OnInventoryIngestAttempt);
}
/// <summary>
/// Eat item
/// </summary>
private void OnUseFoodInHand(EntityUid uid, FoodComponent foodComponent, UseInHandEvent ev)
{
if (ev.Handled)
return;
2023-05-07 14:58:20 +12:00
var result = TryFeed(ev.User, ev.User, uid, foodComponent);
ev.Handled = result.Handled;
}
/// <summary>
/// Feed someone else
/// </summary>
2021-11-29 16:27:15 +13:00
private void OnFeedFood(EntityUid uid, FoodComponent foodComponent, AfterInteractEvent args)
{
if (args.Handled || args.Target == null || !args.CanReach)
2021-11-29 16:27:15 +13:00
return;
2023-05-07 14:58:20 +12:00
var result = TryFeed(args.User, args.Target.Value, uid, foodComponent);
args.Handled = result.Handled;
}
2023-05-07 14:58:20 +12:00
public (bool Success, bool Handled) TryFeed(EntityUid user, EntityUid target, EntityUid food, FoodComponent foodComp)
{
//Suppresses self-eating
if (food == user || TryComp<MobStateComponent>(food, out var mobState) && _mobStateSystem.IsAlive(food, mobState)) // Suppresses eating alive mobs
2023-05-07 14:58:20 +12:00
return (false, false);
// Target can't be fed or they're already eating
if (!TryComp<BodyComponent>(target, out var body))
2023-05-07 14:58:20 +12:00
return (false, false);
2021-11-30 13:45:33 +03:00
if (!_solutionContainerSystem.TryGetSolution(food, foodComp.SolutionName, out var foodSolution) || foodSolution.Name == null)
2023-05-07 14:58:20 +12:00
return (false, false);
if (!_bodySystem.TryGetBodyOrganComponents<StomachComponent>(target, out var stomachs, body))
2023-05-07 14:58:20 +12:00
return (false, false);
var forceFeed = user != target;
2023-08-03 22:21:28 -07:00
// Check for special digestibles
if (!IsDigestibleBy(food, foodComp, stomachs))
{
_popupSystem.PopupEntity(
forceFeed
? Loc.GetString("food-system-cant-digest-other", ("entity", food))
: Loc.GetString("food-system-cant-digest", ("entity", food)), user, user);
2023-05-07 14:58:20 +12:00
return (false, true);
}
// Check for used storage on the food item
if (TryComp<ServerStorageComponent>(food, out var storageState) && storageState.StorageUsed != 0)
{
_popupSystem.PopupEntity(Loc.GetString("food-has-used-storage", ("food", food)), user, user);
return (false, true);
}
var flavors = _flavorProfileSystem.GetLocalizedFlavorsMessage(food, user, foodSolution);
if (foodComp.UsesRemaining <= 0)
{
_popupSystem.PopupEntity(Loc.GetString("food-system-try-use-food-is-empty", ("entity", food)), user, user);
DeleteAndSpawnTrash(foodComp, food, user);
2023-05-07 14:58:20 +12:00
return (false, true);
}
if (IsMouthBlocked(target, user))
2023-05-07 14:58:20 +12:00
return (false, true);
if (!_interactionSystem.InRangeUnobstructed(user, food, popup: true))
2023-05-07 14:58:20 +12:00
return (false, true);
if (!_interactionSystem.InRangeUnobstructed(user, target, MaxFeedDistance, popup: true))
return (false, true);
// TODO make do-afters account for fixtures in the range check.
if (!Transform(user).MapPosition.InRange(Transform(target).MapPosition, MaxFeedDistance))
{
var message = Loc.GetString("interaction-system-user-interaction-cannot-reach");
_popupSystem.PopupEntity(message, user, user);
return (false, true);
}
if (!TryGetRequiredUtensils(user, foodComp, out _))
2023-05-07 14:58:20 +12:00
return (false, true);
if (forceFeed)
{
var userName = Identity.Entity(user, EntityManager);
_popupSystem.PopupEntity(Loc.GetString("food-system-force-feed", ("user", userName)),
user, target);
// logging
_adminLogger.Add(LogType.ForceFeed, LogImpact.Medium, $"{ToPrettyString(user):user} is forcing {ToPrettyString(target):target} to eat {ToPrettyString(food):food} {SolutionContainerSystem.ToPrettyString(foodSolution)}");
}
2022-12-02 19:19:44 -06:00
else
{
// log voluntary eating
_adminLogger.Add(LogType.Ingestion, LogImpact.Low, $"{ToPrettyString(target):target} is eating {ToPrettyString(food):food} {SolutionContainerSystem.ToPrettyString(foodSolution)}");
2022-12-02 19:19:44 -06:00
}
var doAfterArgs = new DoAfterArgs(
user,
forceFeed ? foodComp.ForceFeedDelay : foodComp.Delay,
new ConsumeDoAfterEvent(foodSolution.Name, flavors),
eventTarget: food,
target: target,
used: food)
{
BreakOnUserMove = forceFeed,
BreakOnDamage = true,
BreakOnTargetMove = forceFeed,
MovementThreshold = 0.01f,
2023-05-07 14:58:20 +12:00
DistanceThreshold = MaxFeedDistance,
// Mice and the like can eat without hands.
// TODO maybe set this based on some CanEatWithoutHands event or component?
NeedHand = forceFeed,
};
_doAfterSystem.TryStartDoAfter(doAfterArgs);
2023-05-07 14:58:20 +12:00
return (true, true);
}
private void OnDoAfter(EntityUid uid, FoodComponent component, ConsumeDoAfterEvent args)
{
if (args.Cancelled || args.Handled || component.Deleted || args.Target == null)
return;
if (!TryComp<BodyComponent>(args.Target.Value, out var body))
return;
if (!_bodySystem.TryGetBodyOrganComponents<StomachComponent>(args.Target.Value, out var stomachs, body))
return;
Flavor profiles (#10991) * flavor profiles TODO: every single flavor! yeah!!! * adds basic localization, and flavors/lastFlavor values for when you get the flavor profile message * multiple and single flavor messages * start on flavor localization, multiple flavors in localized flavors * flavor prototypes * a few more flavors, descriptions on what each section of the flavor file should be doing * localization for flavor profiles in drink/food system * adds an event that allows a flavor profile list to be transformed base on the user entity * raises it on the food entity too * changes a field in flavor, adds some more flavors, starts adding flavor prototypes * adds basic flavors to several entities, and consumable drinks, renames flavor field to 'flavors' * changes call ordering in flavorprofile, adds flavor to ignored components server-side flavor is really just a popup message, and those are all processed server-side * fixes where food tried to get the flavor of the user instead of the food * single flavors will now get the localized string * getting the flavor message now ensures that flavors are deduplicated * makes flavor processing more strictly unique bu making everything hashsets * yeah, that could just not have distinctby now * adds flavorprofile directly to food base instead for generic food taste * FlavorProfileModificationEvent now passes a hashset of strings and not flavorprototypes * flavorprofilesystem now broadcasts the flavor profile modification event * adds more flavors to the flavor profile loc file * skips a flavor, if the flavor string is null/empty * adds some more flavors, adds generic medicine flavor to medicinal chemicals * more food flavors, adds flavors to swallowing * adds some cocktails to the set of flavor profiles * regenerates flavor prototypes * adds flavor type to all flavors, adds whitespace between variants * adds more flavors, adds flavors to several chemicals and food items this is the part that took the longest * changes backup flavor message * spelling mistake * more flavors, and flavors on food * readds all the type fields, whoops * fixes localization strings for forcefeeding food/drink * fixes multiple flavor profile * adds flavor limit for flavors * makes that fetch the cvardef instead
2022-09-08 16:14:49 -07:00
if (!_solutionContainerSystem.TryGetSolution(args.Used, args.Solution, out var solution))
return;
if (!TryGetRequiredUtensils(args.User, component, out var utensils))
return;
// TODO this should really be checked every tick.
if (IsMouthBlocked(args.Target.Value))
return;
// TODO this should really be checked every tick.
if (!_interactionSystem.InRangeUnobstructed(args.User, args.Target.Value))
return;
2023-05-02 04:57:11 +10:00
var forceFeed = args.User != args.Target;
args.Handled = true;
var transferAmount = component.TransferAmount != null ? FixedPoint2.Min((FixedPoint2) component.TransferAmount, solution.Volume) : solution.Volume;
Flavor profiles (#10991) * flavor profiles TODO: every single flavor! yeah!!! * adds basic localization, and flavors/lastFlavor values for when you get the flavor profile message * multiple and single flavor messages * start on flavor localization, multiple flavors in localized flavors * flavor prototypes * a few more flavors, descriptions on what each section of the flavor file should be doing * localization for flavor profiles in drink/food system * adds an event that allows a flavor profile list to be transformed base on the user entity * raises it on the food entity too * changes a field in flavor, adds some more flavors, starts adding flavor prototypes * adds basic flavors to several entities, and consumable drinks, renames flavor field to 'flavors' * changes call ordering in flavorprofile, adds flavor to ignored components server-side flavor is really just a popup message, and those are all processed server-side * fixes where food tried to get the flavor of the user instead of the food * single flavors will now get the localized string * getting the flavor message now ensures that flavors are deduplicated * makes flavor processing more strictly unique bu making everything hashsets * yeah, that could just not have distinctby now * adds flavorprofile directly to food base instead for generic food taste * FlavorProfileModificationEvent now passes a hashset of strings and not flavorprototypes * flavorprofilesystem now broadcasts the flavor profile modification event * adds more flavors to the flavor profile loc file * skips a flavor, if the flavor string is null/empty * adds some more flavors, adds generic medicine flavor to medicinal chemicals * more food flavors, adds flavors to swallowing * adds some cocktails to the set of flavor profiles * regenerates flavor prototypes * adds flavor type to all flavors, adds whitespace between variants * adds more flavors, adds flavors to several chemicals and food items this is the part that took the longest * changes backup flavor message * spelling mistake * more flavors, and flavors on food * readds all the type fields, whoops * fixes localization strings for forcefeeding food/drink * fixes multiple flavor profile * adds flavor limit for flavors * makes that fetch the cvardef instead
2022-09-08 16:14:49 -07:00
var split = _solutionContainerSystem.SplitSolution(uid, solution, transferAmount);
//TODO: Get the stomach UID somehow without nabbing owner
// Get the stomach with the highest available solution volume
var highestAvailable = FixedPoint2.Zero;
StomachComponent? stomachToUse = null;
foreach (var (stomach, _) in stomachs)
{
var owner = stomach.Owner;
if (!_stomachSystem.CanTransferSolution(owner, split))
continue;
if (!_solutionContainerSystem.TryGetSolution(owner, StomachSystem.DefaultSolutionName,
out var stomachSol))
continue;
if (stomachSol.AvailableVolume <= highestAvailable)
continue;
stomachToUse = stomach;
highestAvailable = stomachSol.AvailableVolume;
}
// No stomach so just popup a message that they can't eat.
if (stomachToUse == null)
{
_solutionContainerSystem.TryAddSolution(uid, solution, split);
_popupSystem.PopupEntity(forceFeed ? Loc.GetString("food-system-you-cannot-eat-any-more-other") : Loc.GetString("food-system-you-cannot-eat-any-more"), args.Target.Value, args.User);
return;
}
_reaction.DoEntityReaction(args.Target.Value, solution, ReactionMethod.Ingestion);
_stomachSystem.TryTransferSolution(stomachToUse.Owner, split, stomachToUse);
var flavors = args.FlavorMessage;
Flavor profiles (#10991) * flavor profiles TODO: every single flavor! yeah!!! * adds basic localization, and flavors/lastFlavor values for when you get the flavor profile message * multiple and single flavor messages * start on flavor localization, multiple flavors in localized flavors * flavor prototypes * a few more flavors, descriptions on what each section of the flavor file should be doing * localization for flavor profiles in drink/food system * adds an event that allows a flavor profile list to be transformed base on the user entity * raises it on the food entity too * changes a field in flavor, adds some more flavors, starts adding flavor prototypes * adds basic flavors to several entities, and consumable drinks, renames flavor field to 'flavors' * changes call ordering in flavorprofile, adds flavor to ignored components server-side flavor is really just a popup message, and those are all processed server-side * fixes where food tried to get the flavor of the user instead of the food * single flavors will now get the localized string * getting the flavor message now ensures that flavors are deduplicated * makes flavor processing more strictly unique bu making everything hashsets * yeah, that could just not have distinctby now * adds flavorprofile directly to food base instead for generic food taste * FlavorProfileModificationEvent now passes a hashset of strings and not flavorprototypes * flavorprofilesystem now broadcasts the flavor profile modification event * adds more flavors to the flavor profile loc file * skips a flavor, if the flavor string is null/empty * adds some more flavors, adds generic medicine flavor to medicinal chemicals * more food flavors, adds flavors to swallowing * adds some cocktails to the set of flavor profiles * regenerates flavor prototypes * adds flavor type to all flavors, adds whitespace between variants * adds more flavors, adds flavors to several chemicals and food items this is the part that took the longest * changes backup flavor message * spelling mistake * more flavors, and flavors on food * readds all the type fields, whoops * fixes localization strings for forcefeeding food/drink * fixes multiple flavor profile * adds flavor limit for flavors * makes that fetch the cvardef instead
2022-09-08 16:14:49 -07:00
if (forceFeed)
{
var targetName = Identity.Entity(args.Target.Value, EntityManager);
var userName = Identity.Entity(args.User, EntityManager);
Flavor profiles (#10991) * flavor profiles TODO: every single flavor! yeah!!! * adds basic localization, and flavors/lastFlavor values for when you get the flavor profile message * multiple and single flavor messages * start on flavor localization, multiple flavors in localized flavors * flavor prototypes * a few more flavors, descriptions on what each section of the flavor file should be doing * localization for flavor profiles in drink/food system * adds an event that allows a flavor profile list to be transformed base on the user entity * raises it on the food entity too * changes a field in flavor, adds some more flavors, starts adding flavor prototypes * adds basic flavors to several entities, and consumable drinks, renames flavor field to 'flavors' * changes call ordering in flavorprofile, adds flavor to ignored components server-side flavor is really just a popup message, and those are all processed server-side * fixes where food tried to get the flavor of the user instead of the food * single flavors will now get the localized string * getting the flavor message now ensures that flavors are deduplicated * makes flavor processing more strictly unique bu making everything hashsets * yeah, that could just not have distinctby now * adds flavorprofile directly to food base instead for generic food taste * FlavorProfileModificationEvent now passes a hashset of strings and not flavorprototypes * flavorprofilesystem now broadcasts the flavor profile modification event * adds more flavors to the flavor profile loc file * skips a flavor, if the flavor string is null/empty * adds some more flavors, adds generic medicine flavor to medicinal chemicals * more food flavors, adds flavors to swallowing * adds some cocktails to the set of flavor profiles * regenerates flavor prototypes * adds flavor type to all flavors, adds whitespace between variants * adds more flavors, adds flavors to several chemicals and food items this is the part that took the longest * changes backup flavor message * spelling mistake * more flavors, and flavors on food * readds all the type fields, whoops * fixes localization strings for forcefeeding food/drink * fixes multiple flavor profile * adds flavor limit for flavors * makes that fetch the cvardef instead
2022-09-08 16:14:49 -07:00
_popupSystem.PopupEntity(Loc.GetString("food-system-force-feed-success", ("user", userName), ("flavors", flavors)),
uid, uid);
_popupSystem.PopupEntity(Loc.GetString("food-system-force-feed-success-user", ("target", targetName)), args.User, args.User);
2022-12-02 19:19:44 -06:00
// log successful force feed
_adminLogger.Add(LogType.ForceFeed, LogImpact.Medium, $"{ToPrettyString(uid):user} forced {ToPrettyString(args.User):target} to eat {ToPrettyString(uid):food}");
}
else
{
_popupSystem.PopupEntity(Loc.GetString(component.EatMessage, ("food", uid), ("flavors", flavors)), args.User, args.User);
2022-12-02 19:19:44 -06:00
// log successful voluntary eating
_adminLogger.Add(LogType.Ingestion, LogImpact.Low, $"{ToPrettyString(args.User):target} ate {ToPrettyString(uid):food}");
}
_audio.Play(component.UseSound, Filter.Pvs(args.Target.Value), args.Target.Value, true, AudioParams.Default.WithVolume(-1f));
// Try to break all used utensils
foreach (var utensil in utensils)
{
_utensilSystem.TryBreak(utensil, args.User);
}
2023-05-06 10:23:05 +03:00
args.Repeat = !forceFeed;
2023-05-07 14:58:20 +12:00
2023-05-06 10:23:05 +03:00
if (TryComp<StackComponent>(uid, out var stack))
{
//Not deleting whole stack piece will make troubles with grinding object
2023-05-07 14:58:20 +12:00
if (stack.Count > 1)
2023-05-06 10:23:05 +03:00
{
_stack.SetCount(uid, stack.Count - 1);
_solutionContainerSystem.TryAddSolution(uid, solution, split);
return;
}
}
else if (component.UsesRemaining > 0)
{
return;
}
var ev = new BeforeFullyEatenEvent
{
User = args.User
};
RaiseLocalEvent(uid, ev);
if (ev.Cancelled)
return;
if (string.IsNullOrEmpty(component.TrashPrototype))
QueueDel(uid);
else
DeleteAndSpawnTrash(component, uid, args.User);
}
public void DeleteAndSpawnTrash(FoodComponent component, EntityUid food, EntityUid? user = null)
{
//We're empty. Become trash.
var position = Transform(food).MapPosition;
2021-12-05 18:09:01 +01:00
var finisher = EntityManager.SpawnEntity(component.TrashPrototype, position);
// If the user is holding the item
if (user != null && _handsSystem.IsHolding(user.Value, food, out var hand))
{
EntityManager.DeleteEntity(food);
// Put the trash in the user's hand
2022-03-17 20:13:31 +13:00
_handsSystem.TryPickup(user.Value, finisher, hand);
2021-11-29 16:27:15 +13:00
return;
}
2021-11-29 16:27:15 +13:00
EntityManager.QueueDeleteEntity(food);
}
2022-03-28 13:53:20 -07:00
private void AddEatVerb(EntityUid uid, FoodComponent component, GetVerbsEvent<AlternativeVerb> ev)
{
2021-12-05 18:09:01 +01:00
if (uid == ev.User ||
2021-11-30 13:45:33 +03:00
!ev.CanInteract ||
!ev.CanAccess ||
!EntityManager.TryGetComponent(ev.User, out BodyComponent? body) ||
!_bodySystem.TryGetBodyOrganComponents<StomachComponent>(ev.User, out var stomachs, body))
2021-11-30 13:45:33 +03:00
return;
if (EntityManager.TryGetComponent<MobStateComponent>(uid, out var mobState) && _mobStateSystem.IsAlive(uid, mobState))
return;
2022-03-28 13:53:20 -07:00
AlternativeVerb verb = new()
{
2021-12-05 18:09:01 +01:00
Act = () =>
{
TryFeed(ev.User, ev.User, uid, component);
2021-12-05 18:09:01 +01:00
},
Icon = new SpriteSpecifier.Texture(new ("/Textures/Interface/VerbIcons/cutlery.svg.192dpi.png")),
2021-12-05 18:09:01 +01:00
Text = Loc.GetString("food-system-verb-eat"),
Priority = -1
};
ev.Verbs.Add(verb);
}
2021-11-29 16:27:15 +13:00
/// <summary>
/// Returns true if the food item can be digested by the user.
2021-11-29 16:27:15 +13:00
/// </summary>
public bool IsDigestibleBy(EntityUid uid, EntityUid food, FoodComponent? foodComp = null)
2021-11-29 16:27:15 +13:00
{
if (!Resolve(food, ref foodComp, false))
return false;
2021-11-29 16:27:15 +13:00
if (!_bodySystem.TryGetBodyOrganComponents<StomachComponent>(uid, out var stomachs))
return false;
2021-11-29 16:27:15 +13:00
return IsDigestibleBy(food, foodComp, stomachs);
}
2021-11-29 16:27:15 +13:00
/// <summary>
2023-08-03 22:21:28 -07:00
/// Returns true if <paramref name="stomachs"/> has a <see cref="StomachComponent.SpecialDigestible"/> that whitelists
/// this <paramref name="food"/> (or if they even have enough stomachs in the first place).
/// </summary>
private bool IsDigestibleBy(EntityUid food, FoodComponent component, List<(StomachComponent, OrganComponent)> stomachs)
{
var digestible = true;
2023-08-03 22:21:28 -07:00
// Does the mob have enough stomachs?
if (stomachs.Count < component.RequiredStomachs)
return false;
2021-11-29 16:27:15 +13:00
2023-08-03 22:21:28 -07:00
// Run through the mobs' stomachs
foreach (var (comp, _) in stomachs)
{
2023-08-03 22:21:28 -07:00
// Find a stomach with a SpecialDigestible
if (comp.SpecialDigestible == null)
continue;
2023-08-03 22:21:28 -07:00
// Check if the food is in the whitelist
if (comp.SpecialDigestible.IsValid(food, EntityManager))
return true;
// They can only eat whitelist food and the food isn't in the whitelist. It's not edible.
return false;
}
2021-11-29 16:27:15 +13:00
2023-08-03 22:21:28 -07:00
if (component.RequiresSpecialDigestion)
return false;
2021-11-29 16:27:15 +13:00
return digestible;
2021-11-29 16:27:15 +13:00
}
2021-12-05 18:09:01 +01:00
private bool TryGetRequiredUtensils(EntityUid user, FoodComponent component,
out List<EntityUid> utensils, HandsComponent? hands = null)
2021-11-29 16:27:15 +13:00
{
utensils = new List<EntityUid>();
2021-11-29 16:27:15 +13:00
if (component.Utensil != UtensilType.None)
return true;
2021-12-05 18:09:01 +01:00
if (!Resolve(user, ref hands, false))
2021-11-29 16:27:15 +13:00
return false;
var usedTypes = UtensilType.None;
2022-03-17 20:13:31 +13:00
foreach (var item in _handsSystem.EnumerateHeld(user, hands))
2021-11-29 16:27:15 +13:00
{
// Is utensil?
2022-03-17 20:13:31 +13:00
if (!EntityManager.TryGetComponent(item, out UtensilComponent? utensil))
2021-11-29 16:27:15 +13:00
continue;
if ((utensil.Types & component.Utensil) != 0 && // Acceptable type?
(usedTypes & utensil.Types) != utensil.Types) // Type is not used already? (removes usage of identical utensils)
{
// Add to used list
usedTypes |= utensil.Types;
utensils.Add(item);
2021-11-29 16:27:15 +13:00
}
}
// If "required" field is set, try to block eating without proper utensils used
if (component.UtensilRequired && (usedTypes & component.Utensil) != component.Utensil)
{
_popupSystem.PopupEntity(Loc.GetString("food-you-need-to-hold-utensil", ("utensil", component.Utensil ^ usedTypes)), user, user);
2021-11-29 16:27:15 +13:00
return false;
}
return true;
}
/// <summary>
2021-12-05 07:56:27 +13:00
/// Block ingestion attempts based on the equipped mask or head-wear
/// </summary>
2021-12-05 07:56:27 +13:00
private void OnInventoryIngestAttempt(EntityUid uid, InventoryComponent component, IngestionAttemptEvent args)
{
2021-12-05 07:56:27 +13:00
if (args.Cancelled)
return;
2022-06-04 19:17:48 +12:00
IngestionBlockerComponent? blocker;
get that crap outta here (completely rewrites inventorysystem) (#5807) * some work * equip: done unequip: todo * unequipping done & refactored events * workin * movin * reee namespaces * stun * mobstate * fixes * some work on events * removes serverside itemcomp & misc fixes * work * smol merge fix * ports template to prototype & finishes ui * moves relay & adds containerenumerator * actions & cuffs * my god what is actioncode * more fixes * im loosing my grasp on reality * more fixes * more work * explosions * yes * more work * more fixes * merge master & misc fixed because i forgot to commit before merging master * more fixes * fixes * moar * more work * moar fixes * suffixmap * more work on client * motivation low * no. no containers * mirroring client to server * fixes * move serverinvcomp * serverinventorycomponent is dead * gaming * only strippable & ai left... * only ai and richtext left * fixes ai * fixes * fixes sprite layers * more fixes * resolves optional * yes * stable:tm: * fixes * moar fixes * moar * fix some tests * lmao * no comment * good to merge:tm: * fixes build but for real * adresses some reviews * adresses some more reviews * nullables, yo * fixes lobbyscreen * timid refactor to differentiate actor & target * adresses more reviews * more * my god what a mess * removed the rest of duplicates * removed duplicate slotflags and renamed shoes to feet * removes another unused one * yes * fixes lobby & makes tryunequip return unequipped item * fixes * some funny renames * fixes * misc improvements to attemptevents * fixes * merge fixes Co-authored-by: Paul Ritter <ritter.paul1@gmail.com>
2021-12-30 22:56:10 +01:00
if (_inventorySystem.TryGetSlotEntity(uid, "mask", out var maskUid) &&
EntityManager.TryGetComponent(maskUid, out blocker) &&
2021-12-05 07:56:27 +13:00
blocker.Enabled)
{
get that crap outta here (completely rewrites inventorysystem) (#5807) * some work * equip: done unequip: todo * unequipping done & refactored events * workin * movin * reee namespaces * stun * mobstate * fixes * some work on events * removes serverside itemcomp & misc fixes * work * smol merge fix * ports template to prototype & finishes ui * moves relay & adds containerenumerator * actions & cuffs * my god what is actioncode * more fixes * im loosing my grasp on reality * more fixes * more work * explosions * yes * more work * more fixes * merge master & misc fixed because i forgot to commit before merging master * more fixes * fixes * moar * more work * moar fixes * suffixmap * more work on client * motivation low * no. no containers * mirroring client to server * fixes * move serverinvcomp * serverinventorycomponent is dead * gaming * only strippable & ai left... * only ai and richtext left * fixes ai * fixes * fixes sprite layers * more fixes * resolves optional * yes * stable:tm: * fixes * moar fixes * moar * fix some tests * lmao * no comment * good to merge:tm: * fixes build but for real * adresses some reviews * adresses some more reviews * nullables, yo * fixes lobbyscreen * timid refactor to differentiate actor & target * adresses more reviews * more * my god what a mess * removed the rest of duplicates * removed duplicate slotflags and renamed shoes to feet * removes another unused one * yes * fixes lobby & makes tryunequip return unequipped item * fixes * some funny renames * fixes * misc improvements to attemptevents * fixes * merge fixes Co-authored-by: Paul Ritter <ritter.paul1@gmail.com>
2021-12-30 22:56:10 +01:00
args.Blocker = maskUid;
2021-12-05 07:56:27 +13:00
args.Cancel();
return;
}
get that crap outta here (completely rewrites inventorysystem) (#5807) * some work * equip: done unequip: todo * unequipping done & refactored events * workin * movin * reee namespaces * stun * mobstate * fixes * some work on events * removes serverside itemcomp & misc fixes * work * smol merge fix * ports template to prototype & finishes ui * moves relay & adds containerenumerator * actions & cuffs * my god what is actioncode * more fixes * im loosing my grasp on reality * more fixes * more work * explosions * yes * more work * more fixes * merge master & misc fixed because i forgot to commit before merging master * more fixes * fixes * moar * more work * moar fixes * suffixmap * more work on client * motivation low * no. no containers * mirroring client to server * fixes * move serverinvcomp * serverinventorycomponent is dead * gaming * only strippable & ai left... * only ai and richtext left * fixes ai * fixes * fixes sprite layers * more fixes * resolves optional * yes * stable:tm: * fixes * moar fixes * moar * fix some tests * lmao * no comment * good to merge:tm: * fixes build but for real * adresses some reviews * adresses some more reviews * nullables, yo * fixes lobbyscreen * timid refactor to differentiate actor & target * adresses more reviews * more * my god what a mess * removed the rest of duplicates * removed duplicate slotflags and renamed shoes to feet * removes another unused one * yes * fixes lobby & makes tryunequip return unequipped item * fixes * some funny renames * fixes * misc improvements to attemptevents * fixes * merge fixes Co-authored-by: Paul Ritter <ritter.paul1@gmail.com>
2021-12-30 22:56:10 +01:00
if (_inventorySystem.TryGetSlotEntity(uid, "head", out var headUid) &&
EntityManager.TryGetComponent(headUid, out blocker) &&
2021-12-05 07:56:27 +13:00
blocker.Enabled)
{
get that crap outta here (completely rewrites inventorysystem) (#5807) * some work * equip: done unequip: todo * unequipping done & refactored events * workin * movin * reee namespaces * stun * mobstate * fixes * some work on events * removes serverside itemcomp & misc fixes * work * smol merge fix * ports template to prototype & finishes ui * moves relay & adds containerenumerator * actions & cuffs * my god what is actioncode * more fixes * im loosing my grasp on reality * more fixes * more work * explosions * yes * more work * more fixes * merge master & misc fixed because i forgot to commit before merging master * more fixes * fixes * moar * more work * moar fixes * suffixmap * more work on client * motivation low * no. no containers * mirroring client to server * fixes * move serverinvcomp * serverinventorycomponent is dead * gaming * only strippable & ai left... * only ai and richtext left * fixes ai * fixes * fixes sprite layers * more fixes * resolves optional * yes * stable:tm: * fixes * moar fixes * moar * fix some tests * lmao * no comment * good to merge:tm: * fixes build but for real * adresses some reviews * adresses some more reviews * nullables, yo * fixes lobbyscreen * timid refactor to differentiate actor & target * adresses more reviews * more * my god what a mess * removed the rest of duplicates * removed duplicate slotflags and renamed shoes to feet * removes another unused one * yes * fixes lobby & makes tryunequip return unequipped item * fixes * some funny renames * fixes * misc improvements to attemptevents * fixes * merge fixes Co-authored-by: Paul Ritter <ritter.paul1@gmail.com>
2021-12-30 22:56:10 +01:00
args.Blocker = headUid;
2021-12-05 07:56:27 +13:00
args.Cancel();
}
}
2021-11-29 16:27:15 +13:00
2021-12-05 07:56:27 +13:00
/// <summary>
/// Check whether the target's mouth is blocked by equipment (masks or head-wear).
/// </summary>
/// <param name="uid">The target whose equipment is checked</param>
/// <param name="popupUid">Optional entity that will receive an informative pop-up identifying the blocking
/// piece of equipment.</param>
/// <returns></returns>
public bool IsMouthBlocked(EntityUid uid, EntityUid? popupUid = null)
2021-11-29 16:27:15 +13:00
{
2021-12-05 07:56:27 +13:00
var attempt = new IngestionAttemptEvent();
RaiseLocalEvent(uid, attempt, false);
if (attempt.Cancelled && attempt.Blocker != null && popupUid != null)
{
var name = EntityManager.GetComponent<MetaDataComponent>(attempt.Blocker.Value).EntityName;
_popupSystem.PopupEntity(Loc.GetString("food-system-remove-mask", ("entity", name)),
uid, popupUid.Value);
2021-12-05 07:56:27 +13:00
}
2021-11-29 16:27:15 +13:00
2021-12-05 07:56:27 +13:00
return attempt.Cancelled;
2021-11-29 16:27:15 +13:00
}
}
}