2021-06-09 22:19:39 +02:00
|
|
|
using Content.Server.Body.Behavior;
|
|
|
|
|
using Content.Server.Fluids.Components;
|
|
|
|
|
using Content.Shared.Body.Components;
|
2021-09-06 15:49:44 +02:00
|
|
|
using Content.Shared.Chemistry.Components.SolutionManager;
|
|
|
|
|
using Content.Shared.Chemistry.EntitySystems;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Shared.Chemistry.Reagent;
|
|
|
|
|
using Content.Shared.Examine;
|
|
|
|
|
using Content.Shared.Interaction;
|
|
|
|
|
using Content.Shared.Interaction.Helpers;
|
|
|
|
|
using Content.Shared.Nutrition.Components;
|
2021-09-26 15:18:45 +02:00
|
|
|
using Content.Shared.Popups;
|
2021-07-10 17:35:33 +02:00
|
|
|
using Content.Shared.Sound;
|
2021-02-06 14:31:44 +01:00
|
|
|
using JetBrains.Annotations;
|
2019-11-12 08:20:03 +11:00
|
|
|
using Robust.Server.GameObjects;
|
2020-05-22 16:23:18 -05:00
|
|
|
using Robust.Shared.Audio;
|
2019-11-12 08:20:03 +11:00
|
|
|
using Robust.Shared.GameObjects;
|
|
|
|
|
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-05-23 02:27:31 -07:00
|
|
|
using Robust.Shared.Utility;
|
2020-05-29 15:50:23 -05:00
|
|
|
using Robust.Shared.ViewVariables;
|
2021-10-03 06:56:29 +02:00
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
2019-11-12 08:20:03 +11:00
|
|
|
|
2021-06-09 22:19:39 +02:00
|
|
|
namespace Content.Server.Nutrition.Components
|
2019-11-12 08:20:03 +11:00
|
|
|
{
|
|
|
|
|
[RegisterComponent]
|
2021-09-12 16:22:58 +10:00
|
|
|
public class DrinkComponent : Component, IUse, IAfterInteract, IExamine
|
2019-11-12 08:20:03 +11:00
|
|
|
{
|
2021-09-06 15:49:44 +02:00
|
|
|
[DataField("solution")]
|
|
|
|
|
public string SolutionName { get; set; } = DefaultSolutionName;
|
|
|
|
|
public const string DefaultSolutionName = "drink";
|
|
|
|
|
|
2019-11-12 08:20:03 +11:00
|
|
|
public override string Name => "Drink";
|
2020-05-29 15:50:23 -05:00
|
|
|
|
2021-02-13 19:52:00 +03:00
|
|
|
int IAfterInteract.Priority => 10;
|
|
|
|
|
|
2019-11-12 08:20:03 +11:00
|
|
|
[ViewVariables]
|
2021-02-06 14:31:44 +01:00
|
|
|
private bool _opened;
|
|
|
|
|
|
2019-11-12 08:20:03 +11:00
|
|
|
[ViewVariables]
|
2021-03-05 01:08:38 +01:00
|
|
|
[DataField("useSound")]
|
2021-07-10 17:35:33 +02:00
|
|
|
private SoundSpecifier _useSound = new SoundPathSpecifier("/Audio/Items/drink.ogg");
|
2021-02-06 14:31:44 +01:00
|
|
|
|
2019-11-12 08:20:03 +11:00
|
|
|
[ViewVariables]
|
2021-03-05 01:08:38 +01:00
|
|
|
[DataField("isOpen")]
|
2021-09-06 15:49:44 +02:00
|
|
|
internal bool DefaultToOpened;
|
2021-02-06 14:31:44 +01:00
|
|
|
|
2020-09-08 13:30:22 +02:00
|
|
|
[ViewVariables(VVAccess.ReadWrite)]
|
2021-05-03 11:17:22 +00:00
|
|
|
public ReagentUnit TransferAmount { get; [UsedImplicitly] private set; } = ReagentUnit.New(5);
|
2021-02-06 14:31:44 +01:00
|
|
|
|
2020-09-08 13:30:22 +02:00
|
|
|
[ViewVariables(VVAccess.ReadWrite)]
|
2021-02-06 14:31:44 +01:00
|
|
|
public bool Opened
|
|
|
|
|
{
|
|
|
|
|
get => _opened;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (_opened == value)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_opened = value;
|
2021-10-03 06:56:29 +02:00
|
|
|
OnOpenedChanged();
|
2021-02-06 14:31:44 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-06 15:49:44 +02:00
|
|
|
[ViewVariables] public bool Empty => IsEmpty();
|
|
|
|
|
|
|
|
|
|
private bool IsEmpty()
|
|
|
|
|
{
|
|
|
|
|
var drainAvailable = EntitySystem.Get<SolutionContainerSystem>()
|
|
|
|
|
.DrainAvailable(Owner);
|
|
|
|
|
return drainAvailable <= 0;
|
|
|
|
|
}
|
2019-11-12 08:20:03 +11:00
|
|
|
|
2021-03-05 01:08:38 +01:00
|
|
|
[DataField("openSounds")]
|
2021-07-10 17:35:33 +02:00
|
|
|
private SoundSpecifier _openSounds = new SoundCollectionSpecifier("canOpenSounds");
|
2021-09-12 16:22:58 +10:00
|
|
|
|
|
|
|
|
[DataField("pressurized")] public bool Pressurized;
|
|
|
|
|
|
|
|
|
|
[DataField("burstSound")] public SoundSpecifier BurstSound = new SoundPathSpecifier("/Audio/Effects/flash_bang.ogg");
|
2019-11-12 08:20:03 +11:00
|
|
|
|
2021-10-03 06:56:29 +02:00
|
|
|
private void OnOpenedChanged()
|
2021-02-06 14:31:44 +01:00
|
|
|
{
|
2021-09-06 15:49:44 +02:00
|
|
|
var solutionSys = EntitySystem.Get<SolutionContainerSystem>();
|
|
|
|
|
if (!solutionSys.TryGetSolution(Owner, SolutionName, out _))
|
2020-05-29 15:50:23 -05:00
|
|
|
{
|
2021-02-06 14:31:44 +01:00
|
|
|
return;
|
2020-05-29 15:50:23 -05:00
|
|
|
}
|
|
|
|
|
|
2021-10-03 06:56:29 +02:00
|
|
|
if (Owner.TryGetComponent(out AppearanceComponent? appearance))
|
|
|
|
|
{
|
|
|
|
|
appearance.SetData(DrinkCanStateVisual.Opened, Opened);
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-06 14:31:44 +01:00
|
|
|
if (Opened)
|
|
|
|
|
{
|
2021-09-06 15:49:44 +02:00
|
|
|
var refillable = Owner.EnsureComponent<RefillableSolutionComponent>();
|
|
|
|
|
refillable.Solution = SolutionName;
|
|
|
|
|
var drainable = Owner.EnsureComponent<DrainableSolutionComponent>();
|
|
|
|
|
drainable.Solution = SolutionName;
|
2021-02-06 14:31:44 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-09-06 15:49:44 +02:00
|
|
|
Owner.RemoveComponent<RefillableSolutionComponent>();
|
|
|
|
|
Owner.RemoveComponent<DrainableSolutionComponent>();
|
2021-02-06 14:31:44 +01:00
|
|
|
}
|
2019-11-12 08:20:03 +11:00
|
|
|
}
|
|
|
|
|
|
2020-05-29 15:50:23 -05:00
|
|
|
bool IUse.UseEntity(UseEntityEventArgs args)
|
|
|
|
|
{
|
2020-07-23 01:42:09 +02:00
|
|
|
if (!Opened)
|
2020-05-29 15:50:23 -05:00
|
|
|
{
|
|
|
|
|
//Do the opening stuff like playing the sounds.
|
2021-07-31 19:52:33 +02:00
|
|
|
SoundSystem.Play(Filter.Pvs(args.User), _openSounds.GetSound(), args.User, AudioParams.Default);
|
2020-05-31 12:47:46 -05:00
|
|
|
|
2020-07-23 01:42:09 +02:00
|
|
|
Opened = true;
|
2020-05-29 15:50:23 -05:00
|
|
|
return false;
|
|
|
|
|
}
|
2020-09-09 18:32:31 -04:00
|
|
|
|
2021-09-06 15:49:44 +02:00
|
|
|
if (!Owner.HasComponent<SolutionContainerManagerComponent>() ||
|
|
|
|
|
EntitySystem.Get<SolutionContainerSystem>().DrainAvailable(Owner) <= 0)
|
2020-09-09 18:32:31 -04:00
|
|
|
{
|
2021-07-31 19:52:33 +02:00
|
|
|
args.User.PopupMessage(Loc.GetString("drink-component-on-use-is-empty", ("owner", Owner)));
|
2020-09-09 18:32:31 -04:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-16 01:46:14 +01:00
|
|
|
return TryUseDrink(args.User, args.User);
|
2019-11-12 08:20:03 +11:00
|
|
|
}
|
|
|
|
|
|
2020-05-29 15:50:23 -05:00
|
|
|
//Force feeding a drink to someone.
|
2021-02-03 14:05:31 +01:00
|
|
|
async Task<bool> IAfterInteract.AfterInteract(AfterInteractEventArgs eventArgs)
|
2019-11-12 08:20:03 +11:00
|
|
|
{
|
2021-02-06 14:31:44 +01:00
|
|
|
if (eventArgs.Target == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-16 01:46:14 +01:00
|
|
|
return TryUseDrink(eventArgs.User, eventArgs.Target, true);
|
2019-11-12 08:20:03 +11:00
|
|
|
}
|
|
|
|
|
|
2020-05-31 19:29:06 +01:00
|
|
|
public void Examine(FormattedMessage message, bool inDetailsRange)
|
2019-11-12 08:20:03 +11:00
|
|
|
{
|
2020-05-31 19:29:06 +01:00
|
|
|
if (!Opened || !inDetailsRange)
|
2019-11-12 08:20:03 +11:00
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-09-06 15:49:44 +02:00
|
|
|
|
2020-05-29 15:50:23 -05:00
|
|
|
var color = Empty ? "gray" : "yellow";
|
2021-09-06 15:49:44 +02:00
|
|
|
var openedText =
|
|
|
|
|
Loc.GetString(Empty ? "drink-component-on-examine-is-empty" : "drink-component-on-examine-is-opened");
|
2021-07-31 19:52:33 +02:00
|
|
|
message.AddMarkup(Loc.GetString("drink-component-on-examine-details-text", ("colorName", color), ("text", openedText)));
|
2020-05-29 15:50:23 -05:00
|
|
|
}
|
|
|
|
|
|
2021-02-16 01:46:14 +01:00
|
|
|
private bool TryUseDrink(IEntity user, IEntity target, bool forced = false)
|
2020-05-29 15:50:23 -05:00
|
|
|
{
|
2020-07-23 01:42:09 +02:00
|
|
|
if (!Opened)
|
2019-11-12 08:20:03 +11:00
|
|
|
{
|
2021-07-31 19:52:33 +02:00
|
|
|
target.PopupMessage(Loc.GetString("drink-component-try-use-drink-not-open", ("owner", Owner)));
|
2020-07-03 16:26:13 -05:00
|
|
|
return false;
|
2019-11-12 08:20:03 +11:00
|
|
|
}
|
|
|
|
|
|
2021-09-06 15:49:44 +02:00
|
|
|
if (!EntitySystem.Get<SolutionContainerSystem>().TryGetDrainableSolution(Owner.Uid, out var interactions) ||
|
2021-02-06 14:31:44 +01:00
|
|
|
interactions.DrainAvailable <= 0)
|
2019-11-12 08:20:03 +11:00
|
|
|
{
|
2020-09-09 18:32:31 -04:00
|
|
|
if (!forced)
|
|
|
|
|
{
|
2021-07-31 19:52:33 +02:00
|
|
|
target.PopupMessage(Loc.GetString("drink-component-try-use-drink-is-empty", ("entity", Owner)));
|
2020-09-09 18:32:31 -04:00
|
|
|
}
|
2020-09-26 14:48:24 +02:00
|
|
|
|
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
|
|
|
|
2021-06-16 16:44:38 +02:00
|
|
|
if (!target.TryGetComponent(out SharedBodyComponent? body) ||
|
2020-11-02 11:37:37 +01:00
|
|
|
!body.TryGetMechanismBehaviors<StomachBehavior>(out var stomachs))
|
2020-05-29 15:50:23 -05:00
|
|
|
{
|
2021-07-31 19:52:33 +02:00
|
|
|
target.PopupMessage(Loc.GetString("drink-component-try-use-drink-cannot-drink", ("owner", Owner)));
|
2020-05-29 15:50:23 -05:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-16 01:46:14 +01:00
|
|
|
|
|
|
|
|
if (user != target &&
|
|
|
|
|
!user.InRangeUnobstructed(target, popup: true))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-06 15:49:44 +02:00
|
|
|
var solutionContainerSystem = EntitySystem.Get<SolutionContainerSystem>();
|
2021-02-06 14:31:44 +01:00
|
|
|
var transferAmount = ReagentUnit.Min(TransferAmount, interactions.DrainAvailable);
|
2021-09-06 15:49:44 +02:00
|
|
|
var drain = solutionContainerSystem.Drain(Owner.Uid, interactions, transferAmount);
|
2021-02-06 14:31:44 +01:00
|
|
|
var firstStomach = stomachs.FirstOrDefault(stomach => stomach.CanTransferSolution(drain));
|
2020-10-10 15:25:13 +02:00
|
|
|
|
|
|
|
|
// All stomach are full or can't handle whatever solution we have.
|
|
|
|
|
if (firstStomach == null)
|
2020-05-29 15:50:23 -05:00
|
|
|
{
|
2021-07-31 19:52:33 +02:00
|
|
|
target.PopupMessage(Loc.GetString("drink-component-try-use-drink-had-enough", ("owner", Owner)));
|
2021-02-06 14:31:44 +01:00
|
|
|
|
2021-09-06 15:49:44 +02:00
|
|
|
if (Owner.EntityManager.TryGetEntity(Owner.Uid, out var interactionEntity)
|
|
|
|
|
&& !interactionEntity.HasComponent<RefillableSolutionComponent>())
|
2021-02-06 14:31:44 +01:00
|
|
|
{
|
|
|
|
|
drain.SpillAt(target, "PuddleSmear");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-06 15:49:44 +02:00
|
|
|
solutionContainerSystem.Refill(Owner.Uid, interactions, drain);
|
2020-10-10 15:25:13 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
2020-09-09 18:32:31 -04:00
|
|
|
|
2021-07-31 19:52:33 +02:00
|
|
|
SoundSystem.Play(Filter.Pvs(target), _useSound.GetSound(), target, AudioParams.Default.WithVolume(-2f));
|
2020-09-26 14:48:24 +02:00
|
|
|
|
2021-06-21 02:13:54 +02:00
|
|
|
target.PopupMessage(Loc.GetString("drink-component-try-use-drink-success-slurp"));
|
2020-09-26 14:48:24 +02:00
|
|
|
|
2020-10-10 15:25:13 +02:00
|
|
|
// TODO: Account for partial transfer.
|
2020-09-26 14:48:24 +02:00
|
|
|
|
2021-02-06 14:31:44 +01:00
|
|
|
drain.DoEntityReaction(target, ReactionMethod.Ingestion);
|
2020-05-29 15:50:23 -05:00
|
|
|
|
2021-02-06 14:31:44 +01:00
|
|
|
firstStomach.TryTransferSolution(drain);
|
2020-10-10 15:25:13 +02:00
|
|
|
|
|
|
|
|
return true;
|
2020-01-28 20:07:02 -05:00
|
|
|
}
|
2019-11-12 08:20:03 +11:00
|
|
|
}
|
|
|
|
|
}
|