2021-03-21 09:12:03 -07:00
|
|
|
using System.Linq;
|
2020-12-17 10:45:04 +03:00
|
|
|
using System.Threading.Tasks;
|
2020-10-10 15:25:13 +02:00
|
|
|
using Content.Server.GameObjects.Components.Body.Behavior;
|
2021-01-06 18:48:08 -07:00
|
|
|
using Content.Server.GameObjects.Components.Culinary;
|
2021-03-05 01:08:38 +01:00
|
|
|
using Content.Server.GameObjects.Components.Nutrition;
|
2020-07-17 15:41:19 -05:00
|
|
|
using Content.Shared.Chemistry;
|
2020-10-17 12:26:39 +02:00
|
|
|
using Content.Shared.GameObjects.Components.Body;
|
2020-07-17 15:41:19 -05: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-07-17 15:41:19 -05:00
|
|
|
using Robust.Shared.Audio;
|
|
|
|
|
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-03-05 01:08:38 +01:00
|
|
|
using Robust.Shared.Serialization.Manager.Attributes;
|
2020-07-17 15:41:19 -05:00
|
|
|
using Robust.Shared.ViewVariables;
|
|
|
|
|
|
|
|
|
|
namespace Content.Server.GameObjects.Components.Chemistry
|
|
|
|
|
{
|
|
|
|
|
[RegisterComponent]
|
|
|
|
|
public class PillComponent : FoodComponent, IUse, IAfterInteract
|
|
|
|
|
{
|
|
|
|
|
public override string Name => "Pill";
|
|
|
|
|
|
|
|
|
|
[ViewVariables]
|
2021-03-05 01:08:38 +01:00
|
|
|
[DataField("useSound")]
|
2021-03-16 15:50:20 +01:00
|
|
|
protected override string? UseSound { get; set; } = default;
|
2021-03-05 01:08:38 +01:00
|
|
|
|
2020-07-17 15:41:19 -05:00
|
|
|
[ViewVariables]
|
2021-03-05 01:08:38 +01:00
|
|
|
[DataField("trash")]
|
2021-03-16 15:50:20 +01:00
|
|
|
protected override string? TrashPrototype { get; set; } = default;
|
2020-07-17 15:41:19 -05:00
|
|
|
|
2021-03-05 01:08:38 +01:00
|
|
|
[ViewVariables]
|
|
|
|
|
[DataField("transferAmount")]
|
|
|
|
|
protected override ReagentUnit TransferAmount { get; set; } = ReagentUnit.New(1000);
|
2020-07-17 15:41:19 -05:00
|
|
|
|
2021-03-05 01:08:38 +01:00
|
|
|
[ViewVariables]
|
2021-03-16 15:50:20 +01:00
|
|
|
private SolutionContainerComponent _contents = default!;
|
2020-07-17 15:41:19 -05:00
|
|
|
|
|
|
|
|
public override void Initialize()
|
|
|
|
|
{
|
|
|
|
|
base.Initialize();
|
2020-09-26 14:48:24 +02:00
|
|
|
|
2020-12-04 13:26:54 +01:00
|
|
|
Owner.EnsureComponentWarn(out _contents);
|
2020-07-17 15:41:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool IUse.UseEntity(UseEntityEventArgs eventArgs)
|
|
|
|
|
{
|
|
|
|
|
return TryUseFood(eventArgs.User, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Feeding someone else
|
2021-02-04 17:50:28 +01:00
|
|
|
async Task<bool> IAfterInteract.AfterInteract(AfterInteractEventArgs eventArgs)
|
2020-07-17 15:41:19 -05:00
|
|
|
{
|
|
|
|
|
if (eventArgs.Target == null)
|
|
|
|
|
{
|
2021-02-03 14:05:31 +01:00
|
|
|
return false;
|
2020-07-17 15:41:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TryUseFood(eventArgs.User, eventArgs.Target);
|
2021-02-03 14:05:31 +01:00
|
|
|
return true;
|
2020-07-17 15:41:19 -05:00
|
|
|
}
|
|
|
|
|
|
2021-03-16 15:50:20 +01:00
|
|
|
public override bool TryUseFood(IEntity? user, IEntity? target, UtensilComponent? utensilUsed = null)
|
2020-07-17 15:41:19 -05:00
|
|
|
{
|
|
|
|
|
if (user == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var trueTarget = target ?? user;
|
|
|
|
|
|
2021-03-16 15:50:20 +01:00
|
|
|
if (!trueTarget.TryGetComponent(out IBody? body) ||
|
2020-11-02 11:37:37 +01:00
|
|
|
!body.TryGetMechanismBehaviors<StomachBehavior>(out var stomachs))
|
2020-07-17 15:41:19 -05:00
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-30 11:37:06 +02:00
|
|
|
if (!user.InRangeUnobstructed(trueTarget, popup: true))
|
2020-07-17 15:41:19 -05:00
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-05 01:08:38 +01:00
|
|
|
var transferAmount = ReagentUnit.Min(TransferAmount, _contents.CurrentVolume);
|
2020-07-17 15:41:19 -05:00
|
|
|
var split = _contents.SplitSolution(transferAmount);
|
2020-09-26 14:48:24 +02:00
|
|
|
|
2020-10-10 15:25:13 +02:00
|
|
|
var firstStomach = stomachs.FirstOrDefault(stomach => stomach.CanTransferSolution(split));
|
|
|
|
|
|
|
|
|
|
if (firstStomach == null)
|
2020-07-17 15:41:19 -05:00
|
|
|
{
|
|
|
|
|
_contents.TryAddSolution(split);
|
|
|
|
|
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-05 01:08:38 +01:00
|
|
|
if (UseSound != null)
|
2020-07-17 15:41:19 -05:00
|
|
|
{
|
2021-03-21 09:12:03 -07:00
|
|
|
SoundSystem.Play(Filter.Pvs(trueTarget), UseSound, trueTarget, AudioParams.Default.WithVolume(-1f));
|
2020-07-17 15:41:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
trueTarget.PopupMessage(user, Loc.GetString("You swallow the pill."));
|
|
|
|
|
|
|
|
|
|
Owner.Delete();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|