2021-02-22 00:07:46 +00:00
|
|
|
#nullable enable
|
2020-08-22 22:29:20 +02:00
|
|
|
using System;
|
2020-06-19 15:20:59 +02:00
|
|
|
using System.Collections.Generic;
|
2020-10-10 15:25:13 +02:00
|
|
|
using System.Linq;
|
2020-12-17 10:45:04 +03:00
|
|
|
using System.Threading.Tasks;
|
2020-11-02 11:37:37 +01:00
|
|
|
using Content.Server.GameObjects.Components.Body.Behavior;
|
2019-11-12 08:20:03 +11:00
|
|
|
using Content.Server.GameObjects.Components.Chemistry;
|
2021-03-05 01:08:38 +01:00
|
|
|
using Content.Server.GameObjects.Components.Culinary;
|
2020-07-25 15:11:16 +02:00
|
|
|
using Content.Server.GameObjects.Components.GUI;
|
2020-08-13 14:40:27 +02:00
|
|
|
using Content.Server.GameObjects.Components.Items.Storage;
|
2019-11-12 08:20:03 +11:00
|
|
|
using Content.Shared.Chemistry;
|
2020-10-17 12:26:39 +02:00
|
|
|
using Content.Shared.GameObjects.Components.Body;
|
2019-11-12 08:20:03 +11:00
|
|
|
using Content.Shared.Interfaces;
|
2020-07-18 22:51:56 -07:00
|
|
|
using Content.Shared.Interfaces.GameObjects.Components;
|
2020-08-30 11:37:06 +02:00
|
|
|
using Content.Shared.Utility;
|
2021-02-11 01:13:03 -08:00
|
|
|
using Robust.Server.GameObjects;
|
2020-05-29 15:50:23 -05:00
|
|
|
using Robust.Shared.Audio;
|
2019-11-12 08:20:03 +11:00
|
|
|
using Robust.Shared.GameObjects;
|
|
|
|
|
using Robust.Shared.IoC;
|
|
|
|
|
using Robust.Shared.Localization;
|
2021-03-21 09:12:03 -07:00
|
|
|
using Robust.Shared.Player;
|
2021-05-08 03:44:09 +02:00
|
|
|
using Robust.Shared.Prototypes;
|
2021-03-05 01:08:38 +01:00
|
|
|
using Robust.Shared.Serialization.Manager.Attributes;
|
2021-05-08 03:44:09 +02:00
|
|
|
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
|
2019-11-12 08:20:03 +11:00
|
|
|
using Robust.Shared.ViewVariables;
|
|
|
|
|
|
|
|
|
|
namespace Content.Server.GameObjects.Components.Nutrition
|
|
|
|
|
{
|
|
|
|
|
[RegisterComponent]
|
2021-03-05 01:08:38 +01:00
|
|
|
[ComponentReference(typeof(IAfterInteract))]
|
2020-05-29 15:50:23 -05:00
|
|
|
public class FoodComponent : Component, IUse, IAfterInteract
|
2019-11-12 08:20:03 +11:00
|
|
|
{
|
|
|
|
|
public override string Name => "Food";
|
|
|
|
|
|
2021-03-16 15:50:20 +01:00
|
|
|
[ViewVariables] [DataField("useSound")] protected virtual string? UseSound { get; set; } = "/Audio/Items/eatfood.ogg";
|
2021-03-05 01:08:38 +01:00
|
|
|
|
2021-05-08 03:44:09 +02:00
|
|
|
[ViewVariables] [DataField("trash", customTypeSerializer: typeof(PrototypeIdSerializer<EntityPrototype>))] protected virtual string? TrashPrototype { get; set; }
|
2021-03-05 01:08:38 +01:00
|
|
|
|
|
|
|
|
[ViewVariables] [DataField("transferAmount")] protected virtual ReagentUnit TransferAmount { get; set; } = ReagentUnit.New(5);
|
|
|
|
|
|
|
|
|
|
[DataField("utensilsNeeded")] private UtensilType _utensilsNeeded = UtensilType.None;
|
2019-11-12 08:20:03 +11:00
|
|
|
|
2020-08-22 22:29:20 +02:00
|
|
|
[ViewVariables]
|
|
|
|
|
public int UsesRemaining
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2020-09-09 18:32:31 -04:00
|
|
|
if (!Owner.TryGetComponent(out SolutionContainerComponent? solution))
|
2020-08-22 22:29:20 +02:00
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return solution.CurrentVolume == 0
|
|
|
|
|
? 0
|
2021-03-05 01:08:38 +01:00
|
|
|
: Math.Max(1, (int)Math.Ceiling((solution.CurrentVolume / TransferAmount).Float()));
|
2020-08-22 22:29:20 +02:00
|
|
|
}
|
|
|
|
|
}
|
2020-05-29 15:50:23 -05:00
|
|
|
|
2019-11-12 08:20:03 +11:00
|
|
|
public override void Initialize()
|
|
|
|
|
{
|
|
|
|
|
base.Initialize();
|
2020-12-04 13:26:54 +01:00
|
|
|
Owner.EnsureComponentWarn<SolutionContainerComponent>();
|
2019-11-12 08:20:03 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool IUse.UseEntity(UseEntityEventArgs eventArgs)
|
|
|
|
|
{
|
2020-06-19 15:20:59 +02:00
|
|
|
if (_utensilsNeeded != UtensilType.None)
|
|
|
|
|
{
|
2021-02-22 00:07:46 +00:00
|
|
|
eventArgs.User.PopupMessage(Loc.GetString("food-you-need-utensil", ("utensil", _utensilsNeeded)));
|
2020-06-19 15:20:59 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-29 15:50:23 -05:00
|
|
|
return TryUseFood(eventArgs.User, null);
|
2019-11-12 08:20:03 +11:00
|
|
|
}
|
|
|
|
|
|
2020-06-19 15:20:59 +02:00
|
|
|
// Feeding someone else
|
2021-02-03 14:05:31 +01:00
|
|
|
async Task<bool> IAfterInteract.AfterInteract(AfterInteractEventArgs eventArgs)
|
2019-11-12 08:20:03 +11:00
|
|
|
{
|
2020-06-19 15:20:59 +02:00
|
|
|
if (eventArgs.Target == null)
|
|
|
|
|
{
|
2021-02-03 14:05:31 +01:00
|
|
|
return false;
|
2020-06-19 15:20:59 +02:00
|
|
|
}
|
|
|
|
|
|
2020-05-29 15:50:23 -05:00
|
|
|
TryUseFood(eventArgs.User, eventArgs.Target);
|
2021-02-03 14:05:31 +01:00
|
|
|
return true;
|
2019-11-12 08:20:03 +11:00
|
|
|
}
|
|
|
|
|
|
2020-08-22 22:29:20 +02:00
|
|
|
public virtual bool TryUseFood(IEntity? user, IEntity? target, UtensilComponent? utensilUsed = null)
|
2019-11-12 08:20:03 +11:00
|
|
|
{
|
2020-09-09 18:32:31 -04:00
|
|
|
if (!Owner.TryGetComponent(out SolutionContainerComponent? solution))
|
2020-08-22 22:29:20 +02:00
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-12 08:20:03 +11:00
|
|
|
if (user == null)
|
|
|
|
|
{
|
2020-05-29 15:50:23 -05:00
|
|
|
return false;
|
2019-11-12 08:20:03 +11:00
|
|
|
}
|
|
|
|
|
|
2020-05-29 15:50:23 -05:00
|
|
|
if (UsesRemaining <= 0)
|
2019-11-12 08:20:03 +11:00
|
|
|
{
|
2020-09-01 12:34:53 +02:00
|
|
|
user.PopupMessage(Loc.GetString("{0:TheName} is empty!", Owner));
|
2020-05-29 15:50:23 -05:00
|
|
|
return false;
|
2019-11-12 08:20:03 +11:00
|
|
|
}
|
2020-05-29 15:50:23 -05:00
|
|
|
|
|
|
|
|
var trueTarget = target ?? user;
|
|
|
|
|
|
2020-10-17 12:26:39 +02:00
|
|
|
if (!trueTarget.TryGetComponent(out IBody? body) ||
|
2020-11-02 11:37:37 +01:00
|
|
|
!body.TryGetMechanismBehaviors<StomachBehavior>(out var stomachs))
|
2020-06-19 15:20:59 +02:00
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var utensils = utensilUsed != null
|
|
|
|
|
? new List<UtensilComponent> {utensilUsed}
|
|
|
|
|
: null;
|
|
|
|
|
|
|
|
|
|
if (_utensilsNeeded != UtensilType.None)
|
2019-11-12 08:20:03 +11:00
|
|
|
{
|
2020-06-19 15:20:59 +02:00
|
|
|
utensils = new List<UtensilComponent>();
|
|
|
|
|
var types = UtensilType.None;
|
|
|
|
|
|
2020-08-22 22:29:20 +02:00
|
|
|
if (user.TryGetComponent(out HandsComponent? hands))
|
2020-06-19 15:20:59 +02:00
|
|
|
{
|
|
|
|
|
foreach (var item in hands.GetAllHeldItems())
|
|
|
|
|
{
|
2020-08-22 22:29:20 +02:00
|
|
|
if (!item.Owner.TryGetComponent(out UtensilComponent? utensil))
|
2020-06-19 15:20:59 +02:00
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
utensils.Add(utensil);
|
|
|
|
|
types |= utensil.Types;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!types.HasFlag(_utensilsNeeded))
|
2019-11-12 08:20:03 +11:00
|
|
|
{
|
2021-02-22 00:07:46 +00:00
|
|
|
trueTarget.PopupMessage(user, Loc.GetString("food-you-need-to-hold-utensil", ("utensil", _utensilsNeeded)));
|
2020-06-19 15:20:59 +02:00
|
|
|
return false;
|
2020-05-29 15:50:23 -05:00
|
|
|
}
|
2020-06-19 15:20:59 +02:00
|
|
|
}
|
|
|
|
|
|
2020-08-30 11:37:06 +02:00
|
|
|
if (!user.InRangeUnobstructed(trueTarget, popup: true))
|
2020-06-19 15:20:59 +02:00
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-05 01:08:38 +01:00
|
|
|
var transferAmount = ReagentUnit.Min(TransferAmount, solution.CurrentVolume);
|
2020-08-22 22:29:20 +02:00
|
|
|
var split = solution.SplitSolution(transferAmount);
|
2020-10-10 15:25:13 +02:00
|
|
|
var firstStomach = stomachs.FirstOrDefault(stomach => stomach.CanTransferSolution(split));
|
|
|
|
|
|
|
|
|
|
if (firstStomach == null)
|
2020-06-19 15:20:59 +02:00
|
|
|
{
|
|
|
|
|
trueTarget.PopupMessage(user, Loc.GetString("You can't eat any more!"));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-26 14:48:24 +02:00
|
|
|
// TODO: Account for partial transfer.
|
|
|
|
|
|
2021-01-23 16:49:22 +01:00
|
|
|
split.DoEntityReaction(trueTarget, ReactionMethod.Ingestion);
|
2020-09-26 14:48:24 +02:00
|
|
|
|
2020-10-10 15:25:13 +02:00
|
|
|
firstStomach.TryTransferSolution(split);
|
2020-09-26 14:48:24 +02:00
|
|
|
|
2021-03-16 15:50:20 +01:00
|
|
|
if (UseSound != null)
|
|
|
|
|
{
|
2021-03-21 09:12:03 -07:00
|
|
|
SoundSystem.Play(Filter.Pvs(trueTarget), UseSound, trueTarget, AudioParams.Default.WithVolume(-1f));
|
2021-03-16 15:50:20 +01:00
|
|
|
}
|
|
|
|
|
|
2020-06-19 15:20:59 +02:00
|
|
|
trueTarget.PopupMessage(user, Loc.GetString("Nom"));
|
|
|
|
|
|
|
|
|
|
// If utensils were used
|
|
|
|
|
if (utensils != null)
|
|
|
|
|
{
|
|
|
|
|
foreach (var utensil in utensils)
|
2020-05-29 15:50:23 -05:00
|
|
|
{
|
2020-06-19 15:20:59 +02:00
|
|
|
utensil.TryBreak(user);
|
2019-11-12 08:20:03 +11:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-29 15:50:23 -05:00
|
|
|
if (UsesRemaining > 0)
|
2019-11-12 08:20:03 +11:00
|
|
|
{
|
2020-05-29 15:50:23 -05:00
|
|
|
return true;
|
2019-11-12 08:20:03 +11:00
|
|
|
}
|
|
|
|
|
|
2021-03-05 01:08:38 +01:00
|
|
|
if (string.IsNullOrEmpty(TrashPrototype))
|
2020-06-21 23:02:58 +02:00
|
|
|
{
|
|
|
|
|
Owner.Delete();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-29 15:50:23 -05:00
|
|
|
//We're empty. Become trash.
|
2020-09-06 16:11:53 +02:00
|
|
|
var position = Owner.Transform.Coordinates;
|
2021-03-05 01:08:38 +01:00
|
|
|
var finisher = Owner.EntityManager.SpawnEntity(TrashPrototype, position);
|
2020-06-19 15:20:59 +02:00
|
|
|
|
|
|
|
|
// If the user is holding the item
|
2020-08-22 22:29:20 +02:00
|
|
|
if (user.TryGetComponent(out HandsComponent? handsComponent) &&
|
2020-06-19 15:20:59 +02:00
|
|
|
handsComponent.IsHolding(Owner))
|
2019-11-12 08:20:03 +11:00
|
|
|
{
|
2020-06-19 15:20:59 +02:00
|
|
|
Owner.Delete();
|
|
|
|
|
|
|
|
|
|
// Put the trash in the user's hand
|
2020-08-22 22:29:20 +02:00
|
|
|
if (finisher.TryGetComponent(out ItemComponent? item) &&
|
2020-06-19 15:20:59 +02:00
|
|
|
handsComponent.CanPutInHand(item))
|
2019-11-12 08:20:03 +11:00
|
|
|
{
|
2020-06-19 15:20:59 +02:00
|
|
|
handsComponent.PutInHand(item);
|
2019-11-12 08:20:03 +11:00
|
|
|
}
|
|
|
|
|
}
|
2020-06-19 15:20:59 +02:00
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Owner.Delete();
|
|
|
|
|
}
|
2020-05-29 15:50:23 -05:00
|
|
|
|
2020-06-19 15:20:59 +02:00
|
|
|
return true;
|
2019-11-12 08:20:03 +11:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|