Forcefeed DoAfters, again. (#5582)
This commit is contained in:
@@ -1,14 +1,18 @@
|
||||
using System.Linq;
|
||||
using Content.Server.Body.Behavior;
|
||||
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.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.Interaction;
|
||||
@@ -34,6 +38,9 @@ namespace Content.Server.Nutrition.EntitySystems
|
||||
[Dependency] private readonly PopupSystem _popupSystem = default!;
|
||||
[Dependency] private readonly BodySystem _bodySystem = default!;
|
||||
[Dependency] private readonly StomachSystem _stomachSystem = default!;
|
||||
[Dependency] private readonly DoAfterSystem _doAfterSystem = default!;
|
||||
[Dependency] private readonly SharedAdminLogSystem _logSystem = default!;
|
||||
[Dependency] private readonly ActionBlockerSystem _actionBlockerSystem = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
@@ -45,6 +52,8 @@ namespace Content.Server.Nutrition.EntitySystems
|
||||
SubscribeLocalEvent<DrinkComponent, UseInHandEvent>(OnUse);
|
||||
SubscribeLocalEvent<DrinkComponent, AfterInteractEvent>(AfterInteract);
|
||||
SubscribeLocalEvent<DrinkComponent, ExaminedEvent>(OnExamined);
|
||||
SubscribeLocalEvent<SharedBodyComponent, ForceDrinkEvent>(OnForceDrink);
|
||||
SubscribeLocalEvent<ForceDrinkCancelledEvent>(OnForceDrinkCancelled);
|
||||
}
|
||||
|
||||
public bool IsEmpty(EntityUid uid, DrinkComponent? component = null)
|
||||
@@ -103,19 +112,49 @@ namespace Content.Server.Nutrition.EntitySystems
|
||||
|
||||
private void AfterInteract(EntityUid uid, DrinkComponent component, AfterInteractEvent args)
|
||||
{
|
||||
if (args.Handled)
|
||||
if (args.Handled || args.TargetUid == null)
|
||||
return;
|
||||
|
||||
if (args.Target == null)
|
||||
if (!_actionBlockerSystem.CanInteract(args.UserUid) || !_actionBlockerSystem.CanUse(args.UserUid))
|
||||
return;
|
||||
|
||||
if (TryUseDrink(uid, args.User.Uid, args.Target.Uid, true, component))
|
||||
if (!args.UserUid.InRangeUnobstructed(uid, popup: true))
|
||||
{
|
||||
args.Handled = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (args.UserUid == args.TargetUid)
|
||||
{
|
||||
args.Handled = TryUseDrink(uid, args.UserUid);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!args.UserUid.InRangeUnobstructed(args.TargetUid.Value, popup: true))
|
||||
{
|
||||
args.Handled = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (args.User == args.Target)
|
||||
args.Handled = TryUseDrink(uid, args.UserUid, component);
|
||||
else
|
||||
args.Handled = TryForceDrink(uid, args.UserUid, args.TargetUid.Value, component);
|
||||
}
|
||||
|
||||
private void OnUse(EntityUid uid, DrinkComponent component, UseInHandEvent args)
|
||||
{
|
||||
if (args.Handled) return;
|
||||
|
||||
if (!_actionBlockerSystem.CanInteract(args.UserUid) || !_actionBlockerSystem.CanUse(args.UserUid))
|
||||
return;
|
||||
|
||||
if (!args.UserUid.InRangeUnobstructed(uid, popup: true))
|
||||
{
|
||||
args.Handled = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!component.Opened)
|
||||
{
|
||||
//Do the opening stuff like playing the sounds.
|
||||
@@ -131,8 +170,7 @@ namespace Content.Server.Nutrition.EntitySystems
|
||||
return;
|
||||
}
|
||||
|
||||
if (TryUseDrink(uid, args.User.Uid, args.User.Uid, false, component))
|
||||
args.Handled = true;
|
||||
args.Handled = TryUseDrink(uid, args.UserUid, component);
|
||||
}
|
||||
|
||||
private void HandleLand(EntityUid uid, DrinkComponent component, LandEvent args)
|
||||
@@ -189,68 +227,208 @@ namespace Content.Server.Nutrition.EntitySystems
|
||||
appearance.SetData(DrinkCanStateVisual.Opened, component.Opened);
|
||||
}
|
||||
|
||||
private bool TryUseDrink(EntityUid uid, EntityUid userUid, EntityUid targetUid, bool forced, DrinkComponent? component = null)
|
||||
/// <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)
|
||||
{
|
||||
if(!Resolve(uid, ref component))
|
||||
if (!Resolve(uid, ref drink))
|
||||
return false;
|
||||
|
||||
var owner = component.Owner;
|
||||
|
||||
if (!component.Opened)
|
||||
if (!drink.Opened)
|
||||
{
|
||||
_popupSystem.PopupEntity(Loc.GetString("drink-component-try-use-drink-not-open", ("owner", owner)), targetUid, Filter.Entities(userUid));
|
||||
return false;
|
||||
_popupSystem.PopupEntity(Loc.GetString("drink-component-try-use-drink-not-open",
|
||||
("owner", drink.Owner.Name)), uid, Filter.Entities(userUid));
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!_solutionContainerSystem.TryGetDrainableSolution(component.OwnerUid, out var interactions) ||
|
||||
interactions.DrainAvailable <= 0)
|
||||
{
|
||||
if (!forced)
|
||||
{
|
||||
_popupSystem.PopupEntity(Loc.GetString("drink-component-try-use-drink-is-empty", ("entity", owner)), targetUid, Filter.Entities(userUid));
|
||||
}
|
||||
|
||||
if (!EntityManager.TryGetComponent(userUid, out SharedBodyComponent? body))
|
||||
return false;
|
||||
|
||||
if (!_solutionContainerSystem.TryGetDrainableSolution(drink.OwnerUid, out var drinkSolution) ||
|
||||
drinkSolution.DrainAvailable <= 0)
|
||||
{
|
||||
_popupSystem.PopupEntity(Loc.GetString("drink-component-try-use-drink-is-empty",
|
||||
("entity", drink.Owner.Name)), uid, Filter.Entities(userUid));
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!EntityManager.TryGetComponent(targetUid, out SharedBodyComponent? body) ||
|
||||
!_bodySystem.TryGetComponentsOnMechanisms<StomachComponent>(targetUid, out var stomachs, body))
|
||||
if (!_bodySystem.TryGetComponentsOnMechanisms<StomachComponent>(userUid, out var stomachs, body))
|
||||
{
|
||||
_popupSystem.PopupEntity(Loc.GetString("drink-component-try-use-drink-cannot-drink", ("owner", owner)), targetUid, Filter.Entities(targetUid));
|
||||
return false;
|
||||
_popupSystem.PopupEntity(Loc.GetString("drink-component-try-use-drink-cannot-drink"),
|
||||
userUid, Filter.Entities(userUid));
|
||||
return true;
|
||||
}
|
||||
|
||||
if (userUid != targetUid && !userUid.InRangeUnobstructed(targetUid, popup: true))
|
||||
return false;
|
||||
|
||||
var transferAmount = FixedPoint2.Min(component.TransferAmount, interactions.DrainAvailable);
|
||||
var drain = _solutionContainerSystem.Drain(owner.Uid, interactions, transferAmount);
|
||||
var firstStomach = stomachs.FirstOrDefault(stomach => _stomachSystem.CanTransferSolution(stomach.OwnerUid, drain));
|
||||
var transferAmount = FixedPoint2.Min(drink.TransferAmount, drinkSolution.DrainAvailable);
|
||||
var drain = _solutionContainerSystem.Drain(uid, drinkSolution, transferAmount);
|
||||
var firstStomach = stomachs.FirstOrDefault(
|
||||
stomach => _stomachSystem.CanTransferSolution(stomach.OwnerUid, 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", ("owner", owner)), targetUid, Filter.Entities(targetUid));
|
||||
_popupSystem.PopupEntity(Loc.GetString("drink-component-try-use-drink-had-enough"),
|
||||
userUid, Filter.Entities(userUid));
|
||||
|
||||
if (EntityManager.HasComponent<RefillableSolutionComponent>(uid))
|
||||
{
|
||||
drain.SpillAt(targetUid, "PuddleSmear");
|
||||
return false;
|
||||
drain.SpillAt(userUid, "PuddleSmear");
|
||||
return true;
|
||||
}
|
||||
|
||||
_solutionContainerSystem.Refill(owner.Uid, interactions, drain);
|
||||
return false;
|
||||
_solutionContainerSystem.Refill(uid, drinkSolution, drain);
|
||||
return true;
|
||||
}
|
||||
|
||||
SoundSystem.Play(Filter.Pvs(targetUid), component.UseSound.GetSound(), targetUid, AudioParams.Default.WithVolume(-2f));
|
||||
SoundSystem.Play(Filter.Pvs(userUid), drink.UseSound.GetSound(), userUid,
|
||||
AudioParams.Default.WithVolume(-2f));
|
||||
|
||||
_popupSystem.PopupEntity(Loc.GetString("drink-component-try-use-drink-success-slurp"), targetUid, Filter.Pvs(targetUid));
|
||||
_popupSystem.PopupEntity(Loc.GetString("drink-component-try-use-drink-success-slurp"), userUid,
|
||||
Filter.Pvs(userUid));
|
||||
|
||||
// TODO: Account for partial transfer.
|
||||
drain.DoEntityReaction(targetUid, ReactionMethod.Ingestion);
|
||||
drain.DoEntityReaction(userUid, ReactionMethod.Ingestion);
|
||||
_stomachSystem.TryTransferSolution(firstStomach.OwnerUid, drain, firstStomach);
|
||||
|
||||
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.InUse)
|
||||
return false;
|
||||
|
||||
if (!EntityManager.HasComponent<SharedBodyComponent>(targetUid))
|
||||
return false;
|
||||
|
||||
if (!drink.Opened)
|
||||
{
|
||||
_popupSystem.PopupEntity(Loc.GetString("drink-component-try-use-drink-not-open",
|
||||
("owner", drink.Owner.Name)), uid, Filter.Entities(userUid));
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!_solutionContainerSystem.TryGetDrainableSolution(uid, out var drinkSolution) ||
|
||||
drinkSolution.DrainAvailable <= 0)
|
||||
{
|
||||
_popupSystem.PopupEntity(Loc.GetString("drink-component-try-use-drink-is-empty",
|
||||
("entity", drink.Owner.Name)), uid, Filter.Entities(userUid));
|
||||
return true;
|
||||
}
|
||||
|
||||
EntityManager.TryGetComponent(userUid, out MetaDataComponent? meta);
|
||||
var userName = meta?.EntityName ?? string.Empty;
|
||||
|
||||
_popupSystem.PopupEntity(Loc.GetString("drink-component-force-feed", ("user", userName)),
|
||||
userUid, Filter.Entities(targetUid));
|
||||
|
||||
_doAfterSystem.DoAfter(new DoAfterEventArgs(userUid, drink.ForceFeedDelay, target: targetUid)
|
||||
{
|
||||
BreakOnUserMove = true,
|
||||
BreakOnDamage = true,
|
||||
BreakOnStun = true,
|
||||
BreakOnTargetMove = true,
|
||||
MovementThreshold = 1.0f,
|
||||
TargetFinishedEvent = new ForceDrinkEvent(userUid, drink, drinkSolution),
|
||||
BroadcastCancelledEvent = new ForceDrinkCancelledEvent(drink)
|
||||
});
|
||||
|
||||
// logging
|
||||
var user = EntityManager.GetEntity(userUid);
|
||||
var target = EntityManager.GetEntity(targetUid);
|
||||
var drinkable = EntityManager.GetEntity(uid);
|
||||
_logSystem.Add(LogType.ForceFeed, LogImpact.Medium, $"{user} is forcing {target} to drink {drinkable}");
|
||||
|
||||
drink.InUse = true;
|
||||
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)
|
||||
{
|
||||
args.Drink.InUse = false;
|
||||
var transferAmount = FixedPoint2.Min(args.Drink.TransferAmount, args.DrinkSolution.DrainAvailable);
|
||||
var drained = _solutionContainerSystem.Drain(args.Drink.OwnerUid, args.DrinkSolution, transferAmount);
|
||||
|
||||
if (!_bodySystem.TryGetComponentsOnMechanisms<StomachComponent>(uid, out var stomachs, body))
|
||||
{
|
||||
_popupSystem.PopupEntity(Loc.GetString("drink-component-try-use-drink-cannot-drink-other"),
|
||||
uid, Filter.Entities(args.User));
|
||||
|
||||
drained.SpillAt(uid, "PuddleSmear");
|
||||
return;
|
||||
}
|
||||
|
||||
var firstStomach = stomachs.FirstOrDefault(
|
||||
stomach => _stomachSystem.CanTransferSolution(stomach.OwnerUid, drained));
|
||||
|
||||
// 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));
|
||||
|
||||
drained.SpillAt(uid, "PuddleSmear");
|
||||
return;
|
||||
}
|
||||
|
||||
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("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));
|
||||
|
||||
SoundSystem.Play(Filter.Pvs(uid), args.Drink.UseSound.GetSound(), uid, AudioParams.Default.WithVolume(-2f));
|
||||
|
||||
drained.DoEntityReaction(uid, ReactionMethod.Ingestion);
|
||||
_stomachSystem.TryTransferSolution(firstStomach.OwnerUid, drained, firstStomach);
|
||||
}
|
||||
|
||||
private void OnForceDrinkCancelled(ForceDrinkCancelledEvent args)
|
||||
{
|
||||
args.Drink.InUse = false;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class ForceDrinkEvent : EntityEventArgs
|
||||
{
|
||||
public readonly EntityUid User;
|
||||
public readonly DrinkComponent Drink;
|
||||
public readonly Solution DrinkSolution;
|
||||
|
||||
public ForceDrinkEvent(EntityUid user, DrinkComponent drink, Solution drinkSolution)
|
||||
{
|
||||
User = user;
|
||||
Drink = drink;
|
||||
DrinkSolution = drinkSolution;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class ForceDrinkCancelledEvent : EntityEventArgs
|
||||
{
|
||||
public readonly DrinkComponent Drink;
|
||||
|
||||
public ForceDrinkCancelledEvent( DrinkComponent drink)
|
||||
{
|
||||
Drink = drink;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user