Dedupe lots of nutrition and fix usedelay (#6406)
This commit is contained in:
@@ -12,7 +12,7 @@ namespace Content.Server.Nutrition.Components
|
||||
{
|
||||
[RegisterComponent]
|
||||
[Friend(typeof(DrinkSystem))]
|
||||
public class DrinkComponent : Component
|
||||
public sealed class DrinkComponent : Component
|
||||
{
|
||||
[DataField("solution")]
|
||||
public string SolutionName { get; set; } = DefaultSolutionName;
|
||||
@@ -41,6 +41,12 @@ namespace Content.Server.Nutrition.Components
|
||||
[DataField("burstSound")]
|
||||
public SoundSpecifier BurstSound = new SoundPathSpecifier("/Audio/Effects/flash_bang.ogg");
|
||||
|
||||
/// <summary>
|
||||
/// How long it takes to drink this yourself.
|
||||
/// </summary>
|
||||
[DataField("delay")]
|
||||
public float Delay = 1;
|
||||
|
||||
/// <summary>
|
||||
/// This is how many seconds it takes to force feed someone this drink.
|
||||
/// </summary>
|
||||
|
||||
@@ -46,6 +46,12 @@ namespace Content.Server.Nutrition.Components
|
||||
[DataField("eatMessage")]
|
||||
public string EatMessage = "food-nom";
|
||||
|
||||
/// <summary>
|
||||
/// How long it takes to eat the food personally.
|
||||
/// </summary>
|
||||
[DataField("delay")]
|
||||
public float Delay = 1;
|
||||
|
||||
/// <summary>
|
||||
/// This is how many seconds it takes to force feed someone this food.
|
||||
/// Should probably be smaller for small items like pills.
|
||||
|
||||
@@ -1,25 +1,22 @@
|
||||
using System.Threading;
|
||||
using Content.Server.Body.Components;
|
||||
using Content.Server.Body.Systems;
|
||||
using Content.Server.Chemistry.Components.SolutionManager;
|
||||
using Content.Server.Chemistry.EntitySystems;
|
||||
using Content.Server.DoAfter;
|
||||
using Content.Server.Fluids.Components;
|
||||
using Content.Server.Fluids.EntitySystems;
|
||||
using Content.Server.Nutrition.Components;
|
||||
using Content.Server.Popups;
|
||||
using Content.Shared.ActionBlocker;
|
||||
using Content.Shared.Administration.Logs;
|
||||
using Content.Shared.Body.Components;
|
||||
using Content.Shared.Chemistry.Components;
|
||||
using Content.Shared.Chemistry.Reagent;
|
||||
using Content.Shared.Database;
|
||||
using Content.Shared.Examine;
|
||||
using Content.Shared.FixedPoint;
|
||||
using Content.Shared.Hands;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Interaction.Helpers;
|
||||
using Content.Shared.Nutrition.Components;
|
||||
using Content.Shared.Popups;
|
||||
using Content.Shared.Throwing;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.Audio;
|
||||
@@ -54,24 +51,10 @@ namespace Content.Server.Nutrition.EntitySystems
|
||||
SubscribeLocalEvent<DrinkComponent, ComponentInit>(OnDrinkInit);
|
||||
SubscribeLocalEvent<DrinkComponent, LandEvent>(HandleLand);
|
||||
SubscribeLocalEvent<DrinkComponent, UseInHandEvent>(OnUse);
|
||||
SubscribeLocalEvent<DrinkComponent, HandDeselectedEvent>(OnDrinkDeselected);
|
||||
SubscribeLocalEvent<DrinkComponent, AfterInteractEvent>(AfterInteract);
|
||||
SubscribeLocalEvent<DrinkComponent, ExaminedEvent>(OnExamined);
|
||||
SubscribeLocalEvent<SharedBodyComponent, ForceDrinkEvent>(OnForceDrink);
|
||||
SubscribeLocalEvent<ForceDrinkCancelledEvent>(OnForceDrinkCancelled);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// If the user is currently forcing someone do drink, this cancels the attempt if they swap hands or
|
||||
/// otherwise loose the item. Prevents force-feeding dual-wielding.
|
||||
/// </summary>
|
||||
private void OnDrinkDeselected(EntityUid uid, DrinkComponent component, HandDeselectedEvent args)
|
||||
{
|
||||
if (component.CancelToken != null)
|
||||
{
|
||||
component.CancelToken.Cancel();
|
||||
component.CancelToken = null;
|
||||
}
|
||||
SubscribeLocalEvent<SharedBodyComponent, DrinkEvent>(OnDrink);
|
||||
SubscribeLocalEvent<DrinkCancelledEvent>(OnDrinkCancelled);
|
||||
}
|
||||
|
||||
public bool IsEmpty(EntityUid uid, DrinkComponent? component = null)
|
||||
@@ -85,9 +68,7 @@ namespace Content.Server.Nutrition.EntitySystems
|
||||
private void OnExamined(EntityUid uid, DrinkComponent component, ExaminedEvent args)
|
||||
{
|
||||
if (!component.Opened || !args.IsInDetailsRange)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var color = IsEmpty(uid, component) ? "gray" : "yellow";
|
||||
var openedText =
|
||||
@@ -106,9 +87,7 @@ namespace Content.Server.Nutrition.EntitySystems
|
||||
component.Opened = opened;
|
||||
|
||||
if (!_solutionContainerSystem.TryGetSolution(uid, component.SolutionName, out _))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (EntityManager.TryGetComponent<AppearanceComponent>(uid, out var appearance))
|
||||
{
|
||||
@@ -125,7 +104,6 @@ namespace Content.Server.Nutrition.EntitySystems
|
||||
EntityManager.RemoveComponent<RefillableSolutionComponent>(uid);
|
||||
EntityManager.RemoveComponent<DrainableSolutionComponent>(uid);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void AfterInteract(EntityUid uid, DrinkComponent component, AfterInteractEvent args)
|
||||
@@ -133,22 +111,23 @@ namespace Content.Server.Nutrition.EntitySystems
|
||||
if (args.Handled || args.Target == null || !args.CanReach)
|
||||
return;
|
||||
|
||||
if (args.User == args.Target)
|
||||
{
|
||||
args.Handled = TryUseDrink(uid, args.User);
|
||||
// CanInteract already checked CanInteract
|
||||
if (!_actionBlockerSystem.CanUse(args.User))
|
||||
return;
|
||||
}
|
||||
|
||||
if (args.User == args.Target)
|
||||
args.Handled = TryUseDrink(uid, args.User, component);
|
||||
else
|
||||
args.Handled = TryForceDrink(uid, args.User, args.Target.Value, component);
|
||||
args.Handled = TryDrink(args.User, args.Target.Value, component);
|
||||
}
|
||||
|
||||
private void OnUse(EntityUid uid, DrinkComponent component, UseInHandEvent args)
|
||||
{
|
||||
if (args.Handled) return;
|
||||
|
||||
if (!args.User.InRangeUnobstructed(uid, popup: true))
|
||||
{
|
||||
args.Handled = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!component.Opened)
|
||||
{
|
||||
//Do the opening stuff like playing the sounds.
|
||||
@@ -158,13 +137,11 @@ namespace Content.Server.Nutrition.EntitySystems
|
||||
return;
|
||||
}
|
||||
|
||||
if (_solutionContainerSystem.DrainAvailable(uid) <= 0)
|
||||
{
|
||||
args.User.PopupMessage(Loc.GetString("drink-component-on-use-is-empty", ("owner", uid)));
|
||||
// CanUse already checked; trying to keep it consistent if we interact with ourselves.
|
||||
if (!_actionBlockerSystem.CanInteract(args.User))
|
||||
return;
|
||||
}
|
||||
|
||||
args.Handled = TryUseDrink(uid, args.User, component);
|
||||
args.Handled = TryDrink(args.User, args.User, component);
|
||||
}
|
||||
|
||||
private void HandleLand(EntityUid uid, DrinkComponent component, LandEvent args)
|
||||
@@ -219,94 +196,8 @@ namespace Content.Server.Nutrition.EntitySystems
|
||||
appearance.SetData(DrinkCanStateVisual.Opened, component.Opened);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attempt to drink some of a drink. Returns true if any interaction took place, including generation of
|
||||
/// pop-up messages.
|
||||
/// </summary>
|
||||
private bool TryUseDrink(EntityUid uid, EntityUid userUid, DrinkComponent? drink = null)
|
||||
private bool TryDrink(EntityUid user, EntityUid target, DrinkComponent drink)
|
||||
{
|
||||
if (!Resolve(uid, ref drink))
|
||||
return false;
|
||||
|
||||
// if currently being used to force-feed, cancel that action.
|
||||
if (drink.CancelToken != null)
|
||||
{
|
||||
drink.CancelToken.Cancel();
|
||||
drink.CancelToken = null;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!drink.Opened)
|
||||
{
|
||||
_popupSystem.PopupEntity(Loc.GetString("drink-component-try-use-drink-not-open",
|
||||
("owner", EntityManager.GetComponent<MetaDataComponent>(drink.Owner).EntityName)), uid, Filter.Entities(userUid));
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!EntityManager.TryGetComponent(userUid, out SharedBodyComponent? body))
|
||||
return false;
|
||||
|
||||
if (!_solutionContainerSystem.TryGetDrainableSolution((drink).Owner, out var drinkSolution) ||
|
||||
drinkSolution.DrainAvailable <= 0)
|
||||
{
|
||||
_popupSystem.PopupEntity(Loc.GetString("drink-component-try-use-drink-is-empty",
|
||||
("entity", EntityManager.GetComponent<MetaDataComponent>(drink.Owner).EntityName)), uid, Filter.Entities(userUid));
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!_bodySystem.TryGetComponentsOnMechanisms<StomachComponent>(userUid, out var stomachs, body))
|
||||
{
|
||||
_popupSystem.PopupEntity(Loc.GetString("drink-component-try-use-drink-cannot-drink"),
|
||||
userUid, Filter.Entities(userUid));
|
||||
return true;
|
||||
}
|
||||
|
||||
if (_foodSystem.IsMouthBlocked(userUid, userUid))
|
||||
return true;
|
||||
|
||||
var transferAmount = FixedPoint2.Min(drink.TransferAmount, drinkSolution.DrainAvailable);
|
||||
var drain = _solutionContainerSystem.Drain(uid, drinkSolution, transferAmount);
|
||||
var firstStomach = stomachs.FirstOrNull(
|
||||
stomach => _stomachSystem.CanTransferSolution((stomach.Comp).Owner, drain));
|
||||
|
||||
// All stomach are full or can't handle whatever solution we have.
|
||||
if (firstStomach == null)
|
||||
{
|
||||
_popupSystem.PopupEntity(Loc.GetString("drink-component-try-use-drink-had-enough"),
|
||||
userUid, Filter.Entities(userUid));
|
||||
|
||||
if (EntityManager.HasComponent<RefillableSolutionComponent>(uid))
|
||||
{
|
||||
_spillableSystem.SpillAt(userUid, drain, "PuddleSmear");
|
||||
return true;
|
||||
}
|
||||
|
||||
_solutionContainerSystem.Refill(uid, drinkSolution, drain);
|
||||
return true;
|
||||
}
|
||||
|
||||
SoundSystem.Play(Filter.Pvs(userUid), drink.UseSound.GetSound(), userUid,
|
||||
AudioParams.Default.WithVolume(-2f));
|
||||
|
||||
_popupSystem.PopupEntity(Loc.GetString("drink-component-try-use-drink-success-slurp"), userUid,
|
||||
Filter.Pvs(userUid));
|
||||
|
||||
drain.DoEntityReaction(userUid, ReactionMethod.Ingestion);
|
||||
_stomachSystem.TryTransferSolution((firstStomach.Value.Comp).Owner, drain, firstStomach.Value.Comp);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attempt to force someone else to drink some of a drink. Returns true if any interaction took place,
|
||||
/// including generation of pop-up messages.
|
||||
/// </summary>
|
||||
private bool TryForceDrink(EntityUid uid, EntityUid userUid, EntityUid targetUid,
|
||||
DrinkComponent? drink = null)
|
||||
{
|
||||
if (!Resolve(uid, ref drink))
|
||||
return false;
|
||||
|
||||
// cannot stack do-afters
|
||||
if (drink.CancelToken != null)
|
||||
{
|
||||
@@ -315,69 +206,89 @@ namespace Content.Server.Nutrition.EntitySystems
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!EntityManager.HasComponent<SharedBodyComponent>(targetUid))
|
||||
if (!EntityManager.HasComponent<SharedBodyComponent>(target))
|
||||
return false;
|
||||
|
||||
if (!drink.Opened)
|
||||
{
|
||||
_popupSystem.PopupEntity(Loc.GetString("drink-component-try-use-drink-not-open",
|
||||
("owner", EntityManager.GetComponent<MetaDataComponent>(drink.Owner).EntityName)), uid, Filter.Entities(userUid));
|
||||
("owner", EntityManager.GetComponent<MetaDataComponent>(drink.Owner).EntityName)), drink.Owner, Filter.Entities(user));
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!_solutionContainerSystem.TryGetDrainableSolution(uid, out var drinkSolution) ||
|
||||
if (!_solutionContainerSystem.TryGetDrainableSolution(drink.Owner, out var drinkSolution) ||
|
||||
drinkSolution.DrainAvailable <= 0)
|
||||
{
|
||||
_popupSystem.PopupEntity(Loc.GetString("drink-component-try-use-drink-is-empty",
|
||||
("entity", EntityManager.GetComponent<MetaDataComponent>(drink.Owner).EntityName)), uid, Filter.Entities(userUid));
|
||||
("entity", EntityManager.GetComponent<MetaDataComponent>(drink.Owner).EntityName)), drink.Owner, Filter.Entities(user));
|
||||
return true;
|
||||
}
|
||||
|
||||
if (_foodSystem.IsMouthBlocked(targetUid, userUid))
|
||||
if (_foodSystem.IsMouthBlocked(target, user))
|
||||
return true;
|
||||
|
||||
EntityManager.TryGetComponent(userUid, out MetaDataComponent? meta);
|
||||
var userName = meta?.EntityName ?? string.Empty;
|
||||
if (!user.InRangeUnobstructed(drink.Owner, popup: true))
|
||||
return true;
|
||||
|
||||
_popupSystem.PopupEntity(Loc.GetString("drink-component-force-feed", ("user", userName)),
|
||||
userUid, Filter.Entities(targetUid));
|
||||
var forceDrink = user != target;
|
||||
|
||||
drink.CancelToken = new();
|
||||
_doAfterSystem.DoAfter(new DoAfterEventArgs(userUid, drink.ForceFeedDelay, drink.CancelToken.Token, targetUid)
|
||||
if (forceDrink)
|
||||
{
|
||||
EntityManager.TryGetComponent(user, out MetaDataComponent? meta);
|
||||
var userName = meta?.EntityName ?? string.Empty;
|
||||
|
||||
_popupSystem.PopupEntity(Loc.GetString("drink-component-force-feed", ("user", userName)),
|
||||
user, Filter.Entities(target));
|
||||
|
||||
// logging
|
||||
_logSystem.Add(LogType.ForceFeed, LogImpact.Medium, $"{ToPrettyString(user):user} is forcing {ToPrettyString(target):target} to drink {ToPrettyString(drink.Owner):drink} {SolutionContainerSystem.ToPrettyString(drinkSolution)}");
|
||||
}
|
||||
|
||||
drink.CancelToken = new CancellationTokenSource();
|
||||
_doAfterSystem.DoAfter(new DoAfterEventArgs(user, forceDrink ? drink.ForceFeedDelay : drink.Delay, drink.CancelToken.Token, target)
|
||||
{
|
||||
BreakOnUserMove = true,
|
||||
BreakOnDamage = true,
|
||||
BreakOnStun = true,
|
||||
BreakOnTargetMove = true,
|
||||
MovementThreshold = 1.0f,
|
||||
TargetFinishedEvent = new ForceDrinkEvent(userUid, drink, drinkSolution),
|
||||
BroadcastCancelledEvent = new ForceDrinkCancelledEvent(drink),
|
||||
MovementThreshold = 0.01f,
|
||||
TargetFinishedEvent = new DrinkEvent(user, drink, drinkSolution),
|
||||
BroadcastCancelledEvent = new DrinkCancelledEvent(drink),
|
||||
NeedHand = true,
|
||||
});
|
||||
|
||||
// logging
|
||||
_logSystem.Add(LogType.ForceFeed, LogImpact.Medium, $"{ToPrettyString(userUid):user} is forcing {ToPrettyString(targetUid):target} to drink {ToPrettyString(uid):drink} {SolutionContainerSystem.ToPrettyString(drinkSolution):solution}");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Raised directed at a victim when someone has force fed them a drink.
|
||||
/// </summary>
|
||||
private void OnForceDrink(EntityUid uid, SharedBodyComponent body, ForceDrinkEvent args)
|
||||
private void OnDrink(EntityUid uid, SharedBodyComponent body, DrinkEvent args)
|
||||
{
|
||||
if (args.Drink.Deleted)
|
||||
return;
|
||||
|
||||
args.Drink.CancelToken = null;
|
||||
var transferAmount = FixedPoint2.Min(args.Drink.TransferAmount, args.DrinkSolution.DrainAvailable);
|
||||
var drained = _solutionContainerSystem.Drain((args.Drink).Owner, args.DrinkSolution, transferAmount);
|
||||
var drained = _solutionContainerSystem.Drain(args.Drink.Owner, args.DrinkSolution, transferAmount);
|
||||
|
||||
var forceDrink = uid != args.User;
|
||||
|
||||
if (!_bodySystem.TryGetComponentsOnMechanisms<StomachComponent>(uid, out var stomachs, body))
|
||||
{
|
||||
_popupSystem.PopupEntity(Loc.GetString("drink-component-try-use-drink-cannot-drink-other"),
|
||||
_popupSystem.PopupEntity(
|
||||
forceDrink ?
|
||||
Loc.GetString("drink-component-try-use-drink-cannot-drink-other") :
|
||||
Loc.GetString("drink-component-try-use-drink-had-enough"),
|
||||
uid, Filter.Entities(args.User));
|
||||
|
||||
_spillableSystem.SpillAt(uid, drained, "PuddleSmear");
|
||||
if (EntityManager.HasComponent<RefillableSolutionComponent>(uid))
|
||||
{
|
||||
_spillableSystem.SpillAt(args.User, drained, "PuddleSmear");
|
||||
return;
|
||||
}
|
||||
|
||||
_solutionContainerSystem.Refill(uid, args.DrinkSolution, drained);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -394,25 +305,34 @@ namespace Content.Server.Nutrition.EntitySystems
|
||||
return;
|
||||
}
|
||||
|
||||
EntityManager.TryGetComponent(uid, out MetaDataComponent? targetMeta);
|
||||
var targetName = targetMeta?.EntityName ?? string.Empty;
|
||||
if (forceDrink)
|
||||
{
|
||||
EntityManager.TryGetComponent(uid, out MetaDataComponent? targetMeta);
|
||||
var targetName = targetMeta?.EntityName ?? string.Empty;
|
||||
|
||||
EntityManager.TryGetComponent(args.User, out MetaDataComponent? userMeta);
|
||||
var userName = userMeta?.EntityName ?? string.Empty;
|
||||
EntityManager.TryGetComponent(args.User, out MetaDataComponent? userMeta);
|
||||
var userName = userMeta?.EntityName ?? string.Empty;
|
||||
|
||||
_popupSystem.PopupEntity(Loc.GetString("drink-component-force-feed-success", ("user", userName)),
|
||||
uid, Filter.Entities(uid));
|
||||
_popupSystem.PopupEntity(
|
||||
Loc.GetString("drink-component-force-feed-success", ("user", userName)), uid, Filter.Entities(uid));
|
||||
|
||||
_popupSystem.PopupEntity(Loc.GetString("drink-component-force-feed-success-user", ("target", targetName)),
|
||||
args.User, Filter.Entities(args.User));
|
||||
_popupSystem.PopupEntity(
|
||||
Loc.GetString("drink-component-force-feed-success-user", ("target", targetName)),
|
||||
args.User, Filter.Entities(args.User));
|
||||
}
|
||||
else
|
||||
{
|
||||
_popupSystem.PopupEntity(
|
||||
Loc.GetString("drink-component-try-use-drink-success-slurp"), args.User, Filter.Pvs(args.User));
|
||||
}
|
||||
|
||||
SoundSystem.Play(Filter.Pvs(uid), args.Drink.UseSound.GetSound(), uid, AudioParams.Default.WithVolume(-2f));
|
||||
|
||||
drained.DoEntityReaction(uid, ReactionMethod.Ingestion);
|
||||
_stomachSystem.TryTransferSolution((firstStomach.Value.Comp).Owner, drained, firstStomach.Value.Comp);
|
||||
_stomachSystem.TryTransferSolution(firstStomach.Value.Comp.Owner, drained, firstStomach.Value.Comp);
|
||||
}
|
||||
|
||||
private void OnForceDrinkCancelled(ForceDrinkCancelledEvent args)
|
||||
private static void OnDrinkCancelled(DrinkCancelledEvent args)
|
||||
{
|
||||
args.Drink.CancelToken = null;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using Content.Server.Body.Components;
|
||||
using Content.Server.Body.Systems;
|
||||
using Content.Server.Chemistry.EntitySystems;
|
||||
@@ -22,7 +23,6 @@ using Robust.Shared.IoC;
|
||||
using Robust.Shared.Localization;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Utility;
|
||||
using Content.Shared.Hands;
|
||||
using Content.Shared.Inventory;
|
||||
using Content.Shared.Item;
|
||||
|
||||
@@ -31,7 +31,7 @@ namespace Content.Server.Nutrition.EntitySystems
|
||||
/// <summary>
|
||||
/// Handles feeding attempts both on yourself and on the target.
|
||||
/// </summary>
|
||||
internal class FoodSystem : EntitySystem
|
||||
public sealed class FoodSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly SolutionContainerSystem _solutionContainerSystem = default!;
|
||||
[Dependency] private readonly BodySystem _bodySystem = default!;
|
||||
@@ -49,26 +49,12 @@ namespace Content.Server.Nutrition.EntitySystems
|
||||
|
||||
SubscribeLocalEvent<FoodComponent, UseInHandEvent>(OnUseFoodInHand);
|
||||
SubscribeLocalEvent<FoodComponent, AfterInteractEvent>(OnFeedFood);
|
||||
SubscribeLocalEvent<FoodComponent, HandDeselectedEvent>(OnFoodDeselected);
|
||||
SubscribeLocalEvent<FoodComponent, GetInteractionVerbsEvent>(AddEatVerb);
|
||||
SubscribeLocalEvent<SharedBodyComponent, ForceFeedEvent>(OnForceFeed);
|
||||
SubscribeLocalEvent<ForceFeedCancelledEvent>(OnForceFeedCancelled);
|
||||
SubscribeLocalEvent<SharedBodyComponent, FeedEvent>(OnFeed);
|
||||
SubscribeLocalEvent<ForceFeedCancelledEvent>(OnFeedCancelled);
|
||||
SubscribeLocalEvent<InventoryComponent, IngestionAttemptEvent>(OnInventoryIngestAttempt);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// If the user is currently force feeding someone, this cancels the attempt if they swap hands or otherwise
|
||||
/// loose the item. Prevents force-feeding dual-wielding.
|
||||
/// </summary>
|
||||
private void OnFoodDeselected(EntityUid uid, FoodComponent component, HandDeselectedEvent args)
|
||||
{
|
||||
if (component.CancelToken != null)
|
||||
{
|
||||
component.CancelToken.Cancel();
|
||||
component.CancelToken = null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Eat item
|
||||
/// </summary>
|
||||
@@ -77,7 +63,7 @@ namespace Content.Server.Nutrition.EntitySystems
|
||||
if (ev.Handled)
|
||||
return;
|
||||
|
||||
ev.Handled = TryUseFood(uid, ev.User);
|
||||
ev.Handled = TryFeed(ev.User, ev.User, foodComponent);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -88,27 +74,15 @@ namespace Content.Server.Nutrition.EntitySystems
|
||||
if (args.Handled || args.Target == null || !args.CanReach)
|
||||
return;
|
||||
|
||||
if (args.User == args.Target)
|
||||
{
|
||||
args.Handled = TryUseFood(uid, args.User);
|
||||
return;
|
||||
}
|
||||
|
||||
args.Handled = TryForceFeed(uid, args.User, args.Target.Value);
|
||||
args.Handled = TryFeed(args.User, args.Target.Value, foodComponent);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to eat some food
|
||||
/// </summary>
|
||||
/// <param name="uid">Food entity.</param>
|
||||
/// <param name="user">Feeding initiator.</param>
|
||||
/// <returns>True if an interaction occurred (i.e., food was consumed, or a pop-up message was created)</returns>
|
||||
public bool TryUseFood(EntityUid uid, EntityUid user, FoodComponent? food = null)
|
||||
public bool TryFeed(EntityUid user, EntityUid target, FoodComponent food)
|
||||
{
|
||||
if (!Resolve(uid, ref food))
|
||||
if (!_actionBlockerSystem.CanInteract(user) || !_actionBlockerSystem.CanUse(user))
|
||||
return false;
|
||||
|
||||
// if currently being used to force-feed, cancel that action.
|
||||
// if currently being used to feed, cancel that action.
|
||||
if (food.CancelToken != null)
|
||||
{
|
||||
food.CancelToken.Cancel();
|
||||
@@ -116,73 +90,134 @@ namespace Content.Server.Nutrition.EntitySystems
|
||||
return true;
|
||||
}
|
||||
|
||||
if (uid == user || //Suppresses self-eating
|
||||
EntityManager.TryGetComponent<MobStateComponent>(uid, out var mobState) && mobState.IsAlive()) // Suppresses eating alive mobs
|
||||
if (food.Owner == user || //Suppresses self-eating
|
||||
EntityManager.TryGetComponent<MobStateComponent>(food.Owner, out var mobState) && mobState.IsAlive()) // Suppresses eating alive mobs
|
||||
return false;
|
||||
|
||||
if (!_solutionContainerSystem.TryGetSolution(uid, food.SolutionName, out var solution))
|
||||
// Target can't be fed
|
||||
if (!EntityManager.HasComponent<SharedBodyComponent>(target))
|
||||
return false;
|
||||
|
||||
if (!_solutionContainerSystem.TryGetSolution(food.Owner, food.SolutionName, out var foodSolution))
|
||||
return false;
|
||||
|
||||
if (food.UsesRemaining <= 0)
|
||||
{
|
||||
_popupSystem.PopupEntity(Loc.GetString("food-system-try-use-food-is-empty", ("entity", uid)), user, Filter.Entities(user));
|
||||
_popupSystem.PopupEntity(Loc.GetString("food-system-try-use-food-is-empty",
|
||||
("entity", food.Owner)), user, Filter.Entities(user));
|
||||
DeleteAndSpawnTrash(food, user);
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!EntityManager.TryGetComponent(user, out SharedBodyComponent ? body) ||
|
||||
!_bodySystem.TryGetComponentsOnMechanisms<StomachComponent>(user, out var stomachs, body))
|
||||
if (IsMouthBlocked(target, user))
|
||||
return false;
|
||||
|
||||
if (IsMouthBlocked(user, user))
|
||||
{
|
||||
if (!TryGetRequiredUtensils(user, food, out var utensils))
|
||||
return false;
|
||||
|
||||
if (!user.InRangeUnobstructed(food.Owner, popup: true))
|
||||
return true;
|
||||
|
||||
var forceFeed = user != target;
|
||||
food.CancelToken = new CancellationTokenSource();
|
||||
|
||||
if (forceFeed)
|
||||
{
|
||||
EntityManager.TryGetComponent(user, out MetaDataComponent? meta);
|
||||
var userName = meta?.EntityName ?? string.Empty;
|
||||
|
||||
_popupSystem.PopupEntity(Loc.GetString("food-system-force-feed", ("user", userName)),
|
||||
user, Filter.Entities(target));
|
||||
|
||||
// logging
|
||||
_logSystem.Add(LogType.ForceFeed, LogImpact.Medium, $"{ToPrettyString(user):user} is forcing {ToPrettyString(target):target} to eat {ToPrettyString(food.Owner):food} {SolutionContainerSystem.ToPrettyString(foodSolution)}");
|
||||
}
|
||||
|
||||
var usedUtensils = new List<UtensilComponent>();
|
||||
_doAfterSystem.DoAfter(new DoAfterEventArgs(user, forceFeed ? food.ForceFeedDelay : food.Delay, food.CancelToken.Token, target)
|
||||
{
|
||||
BreakOnUserMove = true,
|
||||
BreakOnDamage = true,
|
||||
BreakOnStun = true,
|
||||
BreakOnTargetMove = true,
|
||||
MovementThreshold = 0.01f,
|
||||
TargetFinishedEvent = new FeedEvent(user, food, foodSolution, utensils),
|
||||
BroadcastCancelledEvent = new ForceFeedCancelledEvent(food),
|
||||
NeedHand = true,
|
||||
});
|
||||
|
||||
if (!TryGetRequiredUtensils(user, food, out var utensils))
|
||||
return true;
|
||||
return true;
|
||||
|
||||
if (!user.InRangeUnobstructed(uid, popup: true))
|
||||
return true;
|
||||
}
|
||||
|
||||
var transferAmount = food.TransferAmount != null ? FixedPoint2.Min((FixedPoint2) food.TransferAmount, solution.CurrentVolume) : solution.CurrentVolume;
|
||||
var split = _solutionContainerSystem.SplitSolution(uid, solution, transferAmount);
|
||||
private void OnFeed(EntityUid uid, SharedBodyComponent body, FeedEvent args)
|
||||
{
|
||||
if (args.Food.Deleted)
|
||||
return;
|
||||
|
||||
args.Food.CancelToken = null;
|
||||
|
||||
if (!_bodySystem.TryGetComponentsOnMechanisms<StomachComponent>(uid, out var stomachs, body))
|
||||
return;
|
||||
|
||||
var transferAmount = args.Food.TransferAmount != null
|
||||
? FixedPoint2.Min((FixedPoint2) args.Food.TransferAmount, args.FoodSolution.CurrentVolume)
|
||||
: args.FoodSolution.CurrentVolume;
|
||||
|
||||
var split = _solutionContainerSystem.SplitSolution((args.Food).Owner, args.FoodSolution, transferAmount);
|
||||
var firstStomach = stomachs.FirstOrNull(
|
||||
stomach => _stomachSystem.CanTransferSolution((stomach.Comp).Owner, split));
|
||||
|
||||
var forceFeed = uid != args.User;
|
||||
|
||||
// No stomach so just popup a message that they can't eat.
|
||||
if (firstStomach == null)
|
||||
{
|
||||
_solutionContainerSystem.TryAddSolution(uid, solution, split);
|
||||
_popupSystem.PopupEntity(Loc.GetString("food-system-you-cannot-eat-any-more"), user, Filter.Entities(user));
|
||||
return true;
|
||||
_solutionContainerSystem.TryAddSolution(uid, args.FoodSolution, split);
|
||||
_popupSystem.PopupEntity(
|
||||
forceFeed ?
|
||||
Loc.GetString("food-system-you-cannot-eat-any-more-other") :
|
||||
Loc.GetString("food-system-you-cannot-eat-any-more")
|
||||
, uid, Filter.Entities(args.User));
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO: Account for partial transfer.
|
||||
split.DoEntityReaction(user, ReactionMethod.Ingestion);
|
||||
_stomachSystem.TryTransferSolution((firstStomach.Value.Comp).Owner, split, firstStomach.Value.Comp);
|
||||
split.DoEntityReaction(uid, ReactionMethod.Ingestion);
|
||||
_stomachSystem.TryTransferSolution(firstStomach.Value.Comp.Owner, split, firstStomach.Value.Comp);
|
||||
|
||||
SoundSystem.Play(Filter.Pvs(user), food.UseSound.GetSound(), user, AudioParams.Default.WithVolume(-1f));
|
||||
_popupSystem.PopupEntity(Loc.GetString(food.EatMessage, ("food", food.Owner)), user, Filter.Entities(user));
|
||||
if (forceFeed)
|
||||
{
|
||||
EntityManager.TryGetComponent(uid, out MetaDataComponent? targetMeta);
|
||||
var targetName = targetMeta?.EntityName ?? string.Empty;
|
||||
|
||||
EntityManager.TryGetComponent(args.User, out MetaDataComponent? userMeta);
|
||||
var userName = userMeta?.EntityName ?? string.Empty;
|
||||
|
||||
_popupSystem.PopupEntity(Loc.GetString("food-system-force-feed-success", ("user", userName)),
|
||||
uid, Filter.Entities(uid));
|
||||
|
||||
_popupSystem.PopupEntity(Loc.GetString("food-system-force-feed-success-user", ("target", targetName)),
|
||||
args.User, Filter.Entities(args.User));
|
||||
}
|
||||
else
|
||||
{
|
||||
_popupSystem.PopupEntity(Loc.GetString(args.Food.EatMessage, ("food", args.Food.Owner)), args.User, Filter.Entities(args.User));
|
||||
}
|
||||
|
||||
SoundSystem.Play(Filter.Pvs(uid), args.Food.UseSound.GetSound(), uid, AudioParams.Default.WithVolume(-1f));
|
||||
|
||||
// Try to break all used utensils
|
||||
foreach (var utensil in usedUtensils)
|
||||
foreach (var utensil in args.Utensils)
|
||||
{
|
||||
_utensilSystem.TryBreak((utensil).Owner, user);
|
||||
_utensilSystem.TryBreak((utensil).Owner, args.User);
|
||||
}
|
||||
|
||||
if (food.UsesRemaining > 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (args.Food.UsesRemaining > 0)
|
||||
return;
|
||||
|
||||
if (string.IsNullOrEmpty(food.TrashPrototype))
|
||||
EntityManager.QueueDeleteEntity(food.Owner);
|
||||
if (string.IsNullOrEmpty(args.Food.TrashPrototype))
|
||||
EntityManager.QueueDeleteEntity(args.Food.Owner);
|
||||
else
|
||||
DeleteAndSpawnTrash(food, user);
|
||||
|
||||
return true;
|
||||
DeleteAndSpawnTrash(args.Food, args.User);
|
||||
}
|
||||
|
||||
private void DeleteAndSpawnTrash(FoodComponent component, EntityUid? user = null)
|
||||
@@ -229,7 +264,7 @@ namespace Content.Server.Nutrition.EntitySystems
|
||||
{
|
||||
Act = () =>
|
||||
{
|
||||
TryUseFood(uid, ev.User, component);
|
||||
TryFeed(uid, ev.User, component);
|
||||
},
|
||||
Text = Loc.GetString("food-system-verb-eat"),
|
||||
Priority = -1
|
||||
@@ -238,131 +273,12 @@ namespace Content.Server.Nutrition.EntitySystems
|
||||
ev.Verbs.Add(verb);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Attempts to force feed a target. Returns true if any interaction occurred, including pop-up generation
|
||||
/// </summary>
|
||||
public bool TryForceFeed(EntityUid uid, EntityUid user, EntityUid target, FoodComponent? food = null)
|
||||
{
|
||||
if (!Resolve(uid, ref food))
|
||||
return false;
|
||||
|
||||
// if currently being used to force-feed, cancel that action.
|
||||
if (food.CancelToken != null)
|
||||
{
|
||||
food.CancelToken.Cancel();
|
||||
food.CancelToken = null;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!EntityManager.HasComponent<SharedBodyComponent>(target))
|
||||
return false;
|
||||
|
||||
if (!_solutionContainerSystem.TryGetSolution(uid, food.SolutionName, out var foodSolution))
|
||||
return false;
|
||||
|
||||
if (food.UsesRemaining <= 0)
|
||||
{
|
||||
_popupSystem.PopupEntity(Loc.GetString("food-system-try-use-food-is-empty",
|
||||
("entity", uid)), user, Filter.Entities(user));
|
||||
DeleteAndSpawnTrash(food, user);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (IsMouthBlocked(target, user))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!TryGetRequiredUtensils(user, food, out var utensils))
|
||||
return true;
|
||||
|
||||
EntityManager.TryGetComponent(user, out MetaDataComponent? meta);
|
||||
var userName = meta?.EntityName ?? string.Empty;
|
||||
|
||||
_popupSystem.PopupEntity(Loc.GetString("food-system-force-feed", ("user", userName)),
|
||||
user, Filter.Entities(target));
|
||||
|
||||
food.CancelToken = new();
|
||||
_doAfterSystem.DoAfter(new DoAfterEventArgs(user, food.ForceFeedDelay, food.CancelToken.Token, target)
|
||||
{
|
||||
BreakOnUserMove = true,
|
||||
BreakOnDamage = true,
|
||||
BreakOnStun = true,
|
||||
BreakOnTargetMove = true,
|
||||
MovementThreshold = 1.0f,
|
||||
TargetFinishedEvent = new ForceFeedEvent(user, food, foodSolution, utensils),
|
||||
BroadcastCancelledEvent = new ForceFeedCancelledEvent(food)
|
||||
});
|
||||
|
||||
// logging
|
||||
_logSystem.Add(LogType.ForceFeed, LogImpact.Medium, $"{ToPrettyString(user):user} is forcing {ToPrettyString(target):target} to eat {ToPrettyString(uid):food} {SolutionContainerSystem.ToPrettyString(foodSolution):solution}");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void OnForceFeed(EntityUid uid, SharedBodyComponent body, ForceFeedEvent args)
|
||||
{
|
||||
if (args.Food.Deleted)
|
||||
return;
|
||||
|
||||
args.Food.CancelToken = null;
|
||||
|
||||
if (!_bodySystem.TryGetComponentsOnMechanisms<StomachComponent>(uid, out var stomachs, body))
|
||||
return;
|
||||
|
||||
var transferAmount = args.Food.TransferAmount != null
|
||||
? FixedPoint2.Min((FixedPoint2) args.Food.TransferAmount, args.FoodSolution.CurrentVolume)
|
||||
: args.FoodSolution.CurrentVolume;
|
||||
|
||||
var split = _solutionContainerSystem.SplitSolution((args.Food).Owner, args.FoodSolution, transferAmount);
|
||||
var firstStomach = stomachs.FirstOrNull(
|
||||
stomach => _stomachSystem.CanTransferSolution((stomach.Comp).Owner, split));
|
||||
|
||||
if (firstStomach == null)
|
||||
{
|
||||
_solutionContainerSystem.TryAddSolution(uid, args.FoodSolution, split);
|
||||
_popupSystem.PopupEntity(Loc.GetString("food-system-you-cannot-eat-any-more-other"), uid, Filter.Entities(args.User));
|
||||
return;
|
||||
}
|
||||
|
||||
split.DoEntityReaction(uid, ReactionMethod.Ingestion);
|
||||
_stomachSystem.TryTransferSolution((firstStomach.Value.Comp).Owner, split, firstStomach.Value.Comp);
|
||||
|
||||
EntityManager.TryGetComponent(uid, out MetaDataComponent? targetMeta);
|
||||
var targetName = targetMeta?.EntityName ?? string.Empty;
|
||||
|
||||
EntityManager.TryGetComponent(args.User, out MetaDataComponent? userMeta);
|
||||
var userName = userMeta?.EntityName ?? string.Empty;
|
||||
|
||||
_popupSystem.PopupEntity(Loc.GetString("food-system-force-feed-success", ("user", userName)),
|
||||
uid, Filter.Entities(uid));
|
||||
|
||||
_popupSystem.PopupEntity(Loc.GetString("food-system-force-feed-success-user", ("target", targetName)),
|
||||
args.User, Filter.Entities(args.User));
|
||||
|
||||
SoundSystem.Play(Filter.Pvs(uid), args.Food.UseSound.GetSound(), uid, AudioParams.Default.WithVolume(-1f));
|
||||
|
||||
// Try to break all used utensils
|
||||
foreach (var utensil in args.Utensils)
|
||||
{
|
||||
_utensilSystem.TryBreak((utensil).Owner, args.User);
|
||||
}
|
||||
|
||||
if (args.Food.UsesRemaining > 0)
|
||||
return;
|
||||
|
||||
if (string.IsNullOrEmpty(args.Food.TrashPrototype))
|
||||
EntityManager.QueueDeleteEntity((args.Food).Owner);
|
||||
else
|
||||
DeleteAndSpawnTrash(args.Food, args.User);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Force feeds someone remotely. Does not require utensils (well, not the normal type anyways).
|
||||
/// </summary>
|
||||
public void ProjectileForceFeed(EntityUid uid, EntityUid target, EntityUid? user, FoodComponent? food = null, BodyComponent? body = null)
|
||||
{
|
||||
// TODO: Combine with regular feeding because holy code duplication batman.
|
||||
if (!Resolve(uid, ref food, false) || !Resolve(target, ref body, false))
|
||||
return;
|
||||
|
||||
@@ -398,7 +314,7 @@ namespace Content.Server.Nutrition.EntitySystems
|
||||
SoundSystem.Play(Filter.Pvs(target), food.UseSound.GetSound(), target, AudioParams.Default.WithVolume(-1f));
|
||||
|
||||
if (string.IsNullOrEmpty(food.TrashPrototype))
|
||||
EntityManager.QueueDeleteEntity(((IComponent) food).Owner);
|
||||
EntityManager.QueueDeleteEntity(food.Owner);
|
||||
else
|
||||
DeleteAndSpawnTrash(food);
|
||||
}
|
||||
@@ -406,7 +322,7 @@ namespace Content.Server.Nutrition.EntitySystems
|
||||
private bool TryGetRequiredUtensils(EntityUid user, FoodComponent component,
|
||||
out List<UtensilComponent> utensils, HandsComponent? hands = null)
|
||||
{
|
||||
utensils = new();
|
||||
utensils = new List<UtensilComponent>();
|
||||
|
||||
if (component.Utensil != UtensilType.None)
|
||||
return true;
|
||||
@@ -441,7 +357,7 @@ namespace Content.Server.Nutrition.EntitySystems
|
||||
return true;
|
||||
}
|
||||
|
||||
private void OnForceFeedCancelled(ForceFeedCancelledEvent args)
|
||||
private static void OnFeedCancelled(ForceFeedCancelledEvent args)
|
||||
{
|
||||
args.Food.CancelToken = null;
|
||||
}
|
||||
|
||||
@@ -54,7 +54,7 @@ namespace Content.Server.Nutrition.EntitySystems
|
||||
if (!user.InRangeUnobstructed(target, popup: true))
|
||||
return false;
|
||||
|
||||
return _foodSystem.TryUseFood(target, user);
|
||||
return _foodSystem.TryFeed(user, target, food);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -17,16 +17,16 @@ public sealed class IngestionAttemptEvent : CancellableEntityEventArgs
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Raised directed at the food after a successful force-feed do-after.
|
||||
/// Raised directed at the food after a successful feed do-after.
|
||||
/// </summary>
|
||||
public sealed class ForceFeedEvent : EntityEventArgs
|
||||
public sealed class FeedEvent : EntityEventArgs
|
||||
{
|
||||
public readonly EntityUid User;
|
||||
public readonly FoodComponent Food;
|
||||
public readonly Solution FoodSolution;
|
||||
public readonly List<UtensilComponent> Utensils;
|
||||
|
||||
public ForceFeedEvent(EntityUid user, FoodComponent food, Solution foodSolution, List<UtensilComponent> utensils)
|
||||
public FeedEvent(EntityUid user, FoodComponent food, Solution foodSolution, List<UtensilComponent> utensils)
|
||||
{
|
||||
User = user;
|
||||
Food = food;
|
||||
@@ -51,13 +51,13 @@ public sealed class ForceFeedCancelledEvent : EntityEventArgs
|
||||
/// <summary>
|
||||
/// Raised directed at the drink after a successful force-drink do-after.
|
||||
/// </summary>
|
||||
public sealed class ForceDrinkEvent : EntityEventArgs
|
||||
public sealed class DrinkEvent : EntityEventArgs
|
||||
{
|
||||
public readonly EntityUid User;
|
||||
public readonly DrinkComponent Drink;
|
||||
public readonly Solution DrinkSolution;
|
||||
|
||||
public ForceDrinkEvent(EntityUid user, DrinkComponent drink, Solution drinkSolution)
|
||||
public DrinkEvent(EntityUid user, DrinkComponent drink, Solution drinkSolution)
|
||||
{
|
||||
User = user;
|
||||
Drink = drink;
|
||||
@@ -68,11 +68,11 @@ public sealed class ForceDrinkEvent : EntityEventArgs
|
||||
/// <summary>
|
||||
/// Raised directed at the food after a failed force-dink do-after.
|
||||
/// </summary>
|
||||
public sealed class ForceDrinkCancelledEvent : EntityEventArgs
|
||||
public sealed class DrinkCancelledEvent : EntityEventArgs
|
||||
{
|
||||
public readonly DrinkComponent Drink;
|
||||
|
||||
public ForceDrinkCancelledEvent(DrinkComponent drink)
|
||||
public DrinkCancelledEvent(DrinkComponent drink)
|
||||
{
|
||||
Drink = drink;
|
||||
}
|
||||
|
||||
@@ -7,9 +7,6 @@
|
||||
abstract: true
|
||||
components:
|
||||
- type: Food
|
||||
- type: ItemCooldown
|
||||
- type: UseDelay
|
||||
delay: 1.5
|
||||
|
||||
# This base type is used to cover all of the "obvious" things that should be doable to open-package food.
|
||||
# Practically this means injection.
|
||||
|
||||
@@ -20,9 +20,6 @@
|
||||
interfaces:
|
||||
- key: enum.TransferAmountUiKey.Key
|
||||
type: TransferAmountBoundUserInterface
|
||||
- type: ItemCooldown
|
||||
- type: UseDelay
|
||||
delay: 1.0
|
||||
|
||||
- type: entity
|
||||
parent: DrinkBase
|
||||
|
||||
@@ -38,8 +38,6 @@
|
||||
tags:
|
||||
- Trash
|
||||
- type: ItemCooldown
|
||||
- type: UseDelay
|
||||
delay: 1.0
|
||||
|
||||
- type: entity
|
||||
parent: DrinkCanBaseFull
|
||||
|
||||
@@ -29,8 +29,6 @@
|
||||
- type: Spillable
|
||||
solution: drink
|
||||
- type: ItemCooldown
|
||||
- type: UseDelay
|
||||
delay: 1.0
|
||||
|
||||
- type: entity
|
||||
parent: DrinkBaseCup
|
||||
|
||||
Reference in New Issue
Block a user