2022-02-07 00:37:38 +11:00
using System.Threading ;
2021-11-11 16:10:57 -07:00
using Content.Server.Body.Components ;
using Content.Server.Body.Systems ;
2021-10-29 13:40:15 +01:00
using Content.Server.Chemistry.Components.SolutionManager ;
using Content.Server.Chemistry.EntitySystems ;
2021-11-29 16:27:15 +13:00
using Content.Server.DoAfter ;
2021-12-05 04:18:30 +01:00
using Content.Server.Fluids.EntitySystems ;
2021-09-12 16:22:58 +10:00
using Content.Server.Nutrition.Components ;
2021-11-09 11:28:27 +01:00
using Content.Server.Popups ;
2021-11-29 16:27:15 +13:00
using Content.Shared.ActionBlocker ;
using Content.Shared.Administration.Logs ;
2021-11-02 11:40:55 +11:00
using Content.Shared.Body.Components ;
using Content.Shared.Chemistry.Reagent ;
2021-11-29 16:27:15 +13:00
using Content.Shared.Database ;
2021-11-02 11:40:55 +11:00
using Content.Shared.Examine ;
2021-11-03 16:48:03 -07:00
using Content.Shared.FixedPoint ;
2021-11-02 11:40:55 +11:00
using Content.Shared.Interaction ;
2022-03-13 01:33:23 +13:00
using Content.Shared.Interaction.Events ;
2021-11-02 11:40:55 +11:00
using Content.Shared.Interaction.Helpers ;
2021-10-03 06:56:29 +02:00
using Content.Shared.Nutrition.Components ;
2021-09-12 16:22:58 +10:00
using Content.Shared.Throwing ;
2021-09-06 15:49:44 +02:00
using JetBrains.Annotations ;
2021-09-12 16:22:58 +10:00
using Robust.Shared.Audio ;
2021-09-06 15:49:44 +02:00
using Robust.Shared.GameObjects ;
using Robust.Shared.IoC ;
2021-11-02 11:40:55 +11:00
using Robust.Shared.Localization ;
2021-09-12 16:22:58 +10:00
using Robust.Shared.Player ;
using Robust.Shared.Random ;
2021-11-30 18:25:02 -07:00
using Robust.Shared.Utility ;
2021-09-06 15:49:44 +02:00
namespace Content.Server.Nutrition.EntitySystems
{
[UsedImplicitly]
2022-02-16 00:23:23 -07:00
public sealed class DrinkSystem : EntitySystem
2021-09-06 15:49:44 +02:00
{
2021-12-03 03:51:05 +13:00
[Dependency] private readonly FoodSystem _foodSystem = default ! ;
2021-09-12 16:22:58 +10:00
[Dependency] private readonly IRobustRandom _random = default ! ;
2021-09-06 15:49:44 +02:00
[Dependency] private readonly SolutionContainerSystem _solutionContainerSystem = default ! ;
2021-11-09 11:28:27 +01:00
[Dependency] private readonly PopupSystem _popupSystem = default ! ;
2021-11-11 16:10:57 -07:00
[Dependency] private readonly BodySystem _bodySystem = default ! ;
[Dependency] private readonly StomachSystem _stomachSystem = default ! ;
2021-11-29 16:27:15 +13:00
[Dependency] private readonly DoAfterSystem _doAfterSystem = default ! ;
[Dependency] private readonly SharedAdminLogSystem _logSystem = default ! ;
2021-12-05 04:18:30 +01:00
[Dependency] private readonly SpillableSystem _spillableSystem = default ! ;
2022-02-17 15:40:03 +13:00
[Dependency] private readonly SharedInteractionSystem _interactionSystem = default ! ;
2021-09-06 15:49:44 +02:00
public override void Initialize ( )
{
base . Initialize ( ) ;
SubscribeLocalEvent < DrinkComponent , SolutionChangedEvent > ( OnSolutionChange ) ;
SubscribeLocalEvent < DrinkComponent , ComponentInit > ( OnDrinkInit ) ;
2021-09-12 16:22:58 +10:00
SubscribeLocalEvent < DrinkComponent , LandEvent > ( HandleLand ) ;
2021-11-02 11:40:55 +11:00
SubscribeLocalEvent < DrinkComponent , UseInHandEvent > ( OnUse ) ;
SubscribeLocalEvent < DrinkComponent , AfterInteractEvent > ( AfterInteract ) ;
SubscribeLocalEvent < DrinkComponent , ExaminedEvent > ( OnExamined ) ;
2022-02-07 00:37:38 +11:00
SubscribeLocalEvent < SharedBodyComponent , DrinkEvent > ( OnDrink ) ;
SubscribeLocalEvent < DrinkCancelledEvent > ( OnDrinkCancelled ) ;
2021-12-07 19:19:26 +13:00
}
2021-11-02 11:40:55 +11:00
public bool IsEmpty ( EntityUid uid , DrinkComponent ? component = null )
{
if ( ! Resolve ( uid , ref component ) )
return true ;
return _solutionContainerSystem . DrainAvailable ( uid ) < = 0 ;
}
private void OnExamined ( EntityUid uid , DrinkComponent component , ExaminedEvent args )
{
if ( ! component . Opened | | ! args . IsInDetailsRange )
return ;
var color = IsEmpty ( uid , component ) ? "gray" : "yellow" ;
var openedText =
Loc . GetString ( IsEmpty ( uid , component ) ? "drink-component-on-examine-is-empty" : "drink-component-on-examine-is-opened" ) ;
2021-11-04 23:16:28 -05:00
args . Message . AddMarkup ( $"\n{Loc.GetString(" drink - component - on - examine - details - text ", (" colorName ", color), (" text ", openedText))}" ) ;
2021-11-02 11:40:55 +11:00
}
private void SetOpen ( EntityUid uid , bool opened = false , DrinkComponent ? component = null )
{
if ( ! Resolve ( uid , ref component ) )
return ;
if ( opened = = component . Opened )
return ;
component . Opened = opened ;
if ( ! _solutionContainerSystem . TryGetSolution ( uid , component . SolutionName , out _ ) )
return ;
if ( EntityManager . TryGetComponent < AppearanceComponent > ( uid , out var appearance ) )
{
appearance . SetData ( DrinkCanStateVisual . Opened , opened ) ;
}
if ( opened )
{
EntityManager . EnsureComponent < RefillableSolutionComponent > ( uid ) . Solution = component . SolutionName ;
EntityManager . EnsureComponent < DrainableSolutionComponent > ( uid ) . Solution = component . SolutionName ;
}
else
{
EntityManager . RemoveComponent < RefillableSolutionComponent > ( uid ) ;
EntityManager . RemoveComponent < DrainableSolutionComponent > ( uid ) ;
}
}
private void AfterInteract ( EntityUid uid , DrinkComponent component , AfterInteractEvent args )
{
2022-02-05 15:39:01 +13:00
if ( args . Handled | | args . Target = = null | | ! args . CanReach )
2021-11-02 11:40:55 +11:00
return ;
2022-02-07 00:37:38 +11:00
args . Handled = TryDrink ( args . User , args . Target . Value , component ) ;
2021-11-02 11:40:55 +11:00
}
private void OnUse ( EntityUid uid , DrinkComponent component , UseInHandEvent args )
{
if ( args . Handled ) return ;
2021-11-29 16:27:15 +13:00
2021-11-02 11:40:55 +11:00
if ( ! component . Opened )
{
//Do the opening stuff like playing the sounds.
SoundSystem . Play ( Filter . Pvs ( args . User ) , component . OpenSounds . GetSound ( ) , args . User , AudioParams . Default ) ;
SetOpen ( uid , true , component ) ;
return ;
}
2022-02-07 00:37:38 +11:00
args . Handled = TryDrink ( args . User , args . User , component ) ;
2021-09-12 16:22:58 +10:00
}
private void HandleLand ( EntityUid uid , DrinkComponent component , LandEvent args )
{
if ( component . Pressurized & &
! component . Opened & &
_random . Prob ( 0.25f ) & &
_solutionContainerSystem . TryGetDrainableSolution ( uid , out var interactions ) )
{
component . Opened = true ;
2021-10-03 06:56:29 +02:00
UpdateAppearance ( component ) ;
2021-09-12 16:22:58 +10:00
var solution = _solutionContainerSystem . Drain ( uid , interactions , interactions . DrainAvailable ) ;
2021-12-07 17:48:49 +01:00
_spillableSystem . SpillAt ( uid , solution , "PuddleSmear" ) ;
2021-09-12 16:22:58 +10:00
2021-12-05 18:09:01 +01:00
SoundSystem . Play ( Filter . Pvs ( uid ) , component . BurstSound . GetSound ( ) , uid , AudioParams . Default . WithVolume ( - 4 ) ) ;
2021-09-12 16:22:58 +10:00
}
2021-09-06 15:49:44 +02:00
}
private void OnDrinkInit ( EntityUid uid , DrinkComponent component , ComponentInit args )
{
2021-11-02 11:40:55 +11:00
SetOpen ( uid , component . DefaultToOpened , component ) ;
2021-09-06 15:49:44 +02:00
2021-11-02 11:40:55 +11:00
if ( EntityManager . TryGetComponent ( uid , out DrainableSolutionComponent ? existingDrainable ) )
2021-09-06 15:49:44 +02:00
{
// Beakers have Drink component but they should use the existing Drainable
component . SolutionName = existingDrainable . Solution ;
}
else
{
2021-11-02 11:40:55 +11:00
_solutionContainerSystem . EnsureSolution ( uid , component . SolutionName ) ;
2021-09-06 15:49:44 +02:00
}
2021-10-03 06:56:29 +02:00
UpdateAppearance ( component ) ;
2021-09-06 15:49:44 +02:00
}
private void OnSolutionChange ( EntityUid uid , DrinkComponent component , SolutionChangedEvent args )
{
2021-10-03 06:56:29 +02:00
UpdateAppearance ( component ) ;
}
public void UpdateAppearance ( DrinkComponent component )
{
2021-12-07 22:22:34 +11:00
if ( ! EntityManager . TryGetComponent ( ( component ) . Owner , out AppearanceComponent ? appearance ) | |
! EntityManager . HasComponent < SolutionContainerManagerComponent > ( ( component ) . Owner ) )
2021-10-03 06:56:29 +02:00
{
return ;
}
2021-12-07 22:22:34 +11:00
var drainAvailable = _solutionContainerSystem . DrainAvailable ( ( component ) . Owner ) ;
2021-10-03 06:56:29 +02:00
appearance . SetData ( FoodVisuals . Visual , drainAvailable . Float ( ) ) ;
appearance . SetData ( DrinkCanStateVisual . Opened , component . Opened ) ;
2021-09-06 15:49:44 +02:00
}
2021-11-02 11:40:55 +11:00
2022-02-07 00:37:38 +11:00
private bool TryDrink ( EntityUid user , EntityUid target , DrinkComponent drink )
2021-11-02 11:40:55 +11:00
{
2022-02-07 00:37:38 +11:00
// cannot stack do-afters
2021-12-07 19:19:26 +13:00
if ( drink . CancelToken ! = null )
{
drink . CancelToken . Cancel ( ) ;
drink . CancelToken = null ;
return true ;
}
2022-02-07 00:37:38 +11:00
if ( ! EntityManager . HasComponent < SharedBodyComponent > ( target ) )
return false ;
2021-11-29 16:27:15 +13:00
if ( ! drink . Opened )
2021-11-02 11:40:55 +11:00
{
2021-11-29 16:27:15 +13:00
_popupSystem . PopupEntity ( Loc . GetString ( "drink-component-try-use-drink-not-open" ,
2022-02-07 00:37:38 +11:00
( "owner" , EntityManager . GetComponent < MetaDataComponent > ( drink . Owner ) . EntityName ) ) , drink . Owner , Filter . Entities ( user ) ) ;
2021-11-29 16:27:15 +13:00
return true ;
2021-11-02 11:40:55 +11:00
}
2022-02-07 00:37:38 +11:00
if ( ! _solutionContainerSystem . TryGetDrainableSolution ( drink . Owner , out var drinkSolution ) | |
2021-11-29 16:27:15 +13:00
drinkSolution . DrainAvailable < = 0 )
2021-11-02 11:40:55 +11:00
{
2021-11-29 16:27:15 +13:00
_popupSystem . PopupEntity ( Loc . GetString ( "drink-component-try-use-drink-is-empty" ,
2022-02-07 00:37:38 +11:00
( "entity" , EntityManager . GetComponent < MetaDataComponent > ( drink . Owner ) . EntityName ) ) , drink . Owner , Filter . Entities ( user ) ) ;
2021-11-29 16:27:15 +13:00
return true ;
}
2021-11-02 11:40:55 +11:00
2022-02-07 00:37:38 +11:00
if ( _foodSystem . IsMouthBlocked ( target , user ) )
2021-12-03 03:51:05 +13:00
return true ;
2022-02-17 15:40:03 +13:00
if ( ! _interactionSystem . InRangeUnobstructed ( user , drink . Owner , popup : true ) )
2021-11-29 16:27:15 +13:00
return true ;
2021-11-02 11:40:55 +11:00
2022-02-07 00:37:38 +11:00
var forceDrink = user ! = target ;
2021-11-29 16:27:15 +13:00
2022-02-07 00:37:38 +11:00
if ( forceDrink )
2021-12-07 19:19:26 +13:00
{
2022-02-07 00:37:38 +11:00
EntityManager . TryGetComponent ( user , out MetaDataComponent ? meta ) ;
var userName = meta ? . EntityName ? ? string . Empty ;
2021-11-29 16:27:15 +13:00
2022-02-07 00:37:38 +11:00
_popupSystem . PopupEntity ( Loc . GetString ( "drink-component-force-feed" , ( "user" , userName ) ) ,
user , Filter . Entities ( target ) ) ;
2021-11-29 16:27:15 +13:00
2022-02-07 00:37:38 +11:00
// logging
_logSystem . Add ( LogType . ForceFeed , LogImpact . Medium , $"{ToPrettyString(user):user} is forcing {ToPrettyString(target):target} to drink {ToPrettyString(drink.Owner):drink} {SolutionContainerSystem.ToPrettyString(drinkSolution)}" ) ;
2021-11-29 16:27:15 +13:00
}
2022-02-07 00:37:38 +11:00
drink . CancelToken = new CancellationTokenSource ( ) ;
2022-03-17 12:46:18 +11:00
var moveBreak = user ! = target ;
2022-02-07 00:37:38 +11:00
_doAfterSystem . DoAfter ( new DoAfterEventArgs ( user , forceDrink ? drink . ForceFeedDelay : drink . Delay , drink . CancelToken . Token , target )
2021-11-29 16:27:15 +13:00
{
2022-03-17 12:46:18 +11:00
BreakOnUserMove = moveBreak ,
2021-11-29 16:27:15 +13:00
BreakOnDamage = true ,
BreakOnStun = true ,
2022-03-17 12:46:18 +11:00
BreakOnTargetMove = moveBreak ,
2022-02-07 00:37:38 +11:00
MovementThreshold = 0.01f ,
TargetFinishedEvent = new DrinkEvent ( user , drink , drinkSolution ) ,
BroadcastCancelledEvent = new DrinkCancelledEvent ( drink ) ,
NeedHand = true ,
2021-11-29 16:27:15 +13:00
} ) ;
return true ;
}
/// <summary>
/// Raised directed at a victim when someone has force fed them a drink.
/// </summary>
2022-02-07 00:37:38 +11:00
private void OnDrink ( EntityUid uid , SharedBodyComponent body , DrinkEvent args )
2021-11-29 16:27:15 +13:00
{
2021-12-07 19:19:26 +13:00
if ( args . Drink . Deleted )
return ;
args . Drink . CancelToken = null ;
2021-11-29 16:27:15 +13:00
var transferAmount = FixedPoint2 . Min ( args . Drink . TransferAmount , args . DrinkSolution . DrainAvailable ) ;
2022-02-07 00:37:38 +11:00
var drained = _solutionContainerSystem . Drain ( args . Drink . Owner , args . DrinkSolution , transferAmount ) ;
var forceDrink = uid ! = args . User ;
2021-11-29 16:27:15 +13:00
if ( ! _bodySystem . TryGetComponentsOnMechanisms < StomachComponent > ( uid , out var stomachs , body ) )
{
2022-02-07 00:37:38 +11:00
_popupSystem . PopupEntity (
forceDrink ?
Loc . GetString ( "drink-component-try-use-drink-cannot-drink-other" ) :
Loc . GetString ( "drink-component-try-use-drink-had-enough" ) ,
2021-11-29 16:27:15 +13:00
uid , Filter . Entities ( args . User ) ) ;
2022-02-07 00:37:38 +11:00
if ( EntityManager . HasComponent < RefillableSolutionComponent > ( uid ) )
{
_spillableSystem . SpillAt ( args . User , drained , "PuddleSmear" ) ;
return ;
}
_solutionContainerSystem . Refill ( uid , args . DrinkSolution , drained ) ;
2021-11-29 16:27:15 +13:00
return ;
}
2021-11-30 18:25:02 -07:00
var firstStomach = stomachs . FirstOrNull (
2021-12-07 22:22:34 +11:00
stomach = > _stomachSystem . CanTransferSolution ( ( stomach . Comp ) . Owner , drained ) ) ;
2021-11-29 16:27:15 +13:00
// 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-other" ) ,
uid , Filter . Entities ( args . User ) ) ;
2021-12-05 04:18:30 +01:00
_spillableSystem . SpillAt ( uid , drained , "PuddleSmear" ) ;
2021-11-29 16:27:15 +13:00
return ;
}
2022-02-07 00:37:38 +11:00
if ( forceDrink )
{
EntityManager . TryGetComponent ( uid , out MetaDataComponent ? targetMeta ) ;
var targetName = targetMeta ? . EntityName ? ? string . Empty ;
2021-11-29 16:27:15 +13:00
2022-02-07 00:37:38 +11:00
EntityManager . TryGetComponent ( args . User , out MetaDataComponent ? userMeta ) ;
var userName = userMeta ? . EntityName ? ? string . Empty ;
2021-11-29 16:27:15 +13:00
2022-02-07 00:37:38 +11:00
_popupSystem . PopupEntity (
Loc . GetString ( "drink-component-force-feed-success" , ( "user" , userName ) ) , uid , Filter . Entities ( uid ) ) ;
2021-11-29 16:27:15 +13:00
2022-02-07 00:37:38 +11:00
_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 ) ) ;
}
2021-11-29 16:27:15 +13:00
SoundSystem . Play ( Filter . Pvs ( uid ) , args . Drink . UseSound . GetSound ( ) , uid , AudioParams . Default . WithVolume ( - 2f ) ) ;
drained . DoEntityReaction ( uid , ReactionMethod . Ingestion ) ;
2022-02-07 00:37:38 +11:00
_stomachSystem . TryTransferSolution ( firstStomach . Value . Comp . Owner , drained , firstStomach . Value . Comp ) ;
2021-11-29 16:27:15 +13:00
}
2022-02-07 00:37:38 +11:00
private static void OnDrinkCancelled ( DrinkCancelledEvent args )
2021-11-29 16:27:15 +13:00
{
2021-12-07 19:19:26 +13:00
args . Drink . CancelToken = null ;
2021-11-29 16:27:15 +13:00
}
2021-09-06 15:49:44 +02:00
}
}