Files
crystall-punk-14/Content.Server/AI/Operators/Nutrition/UseFoodInInventoryOperator.cs

58 lines
2.1 KiB
C#
Raw Normal View History

2021-06-09 22:19:39 +02:00
using Content.Server.Hands.Components;
using Content.Server.Nutrition.Components;
2022-03-17 20:13:31 +13:00
using Content.Shared.Hands.EntitySystems;
2021-06-09 22:19:39 +02:00
using Content.Shared.Nutrition.Components;
namespace Content.Server.AI.Operators.Nutrition
{
public sealed class UseFoodInInventoryOperator : AiOperator
{
2021-12-05 18:09:01 +01:00
private readonly EntityUid _owner;
private readonly EntityUid _target;
private float _interactionCooldown;
2021-12-05 18:09:01 +01:00
public UseFoodInInventoryOperator(EntityUid owner, EntityUid target)
{
_owner = owner;
_target = target;
}
public override Outcome Execute(float frameTime)
{
if (_interactionCooldown >= 0)
{
_interactionCooldown -= frameTime;
return Outcome.Continuing;
}
2020-08-22 12:24:59 +02:00
2021-12-05 18:09:01 +01:00
var entities = IoCManager.Resolve<IEntityManager>();
2022-03-17 20:13:31 +13:00
var sysMan = IoCManager.Resolve<IEntitySystemManager>();
var handsSys = sysMan.GetEntitySystem<SharedHandsSystem>();
2021-12-05 18:09:01 +01:00
// TODO: Also have this check storage a la backpack etc.
2021-12-09 12:29:27 +01:00
if (entities.Deleted(_target) ||
2022-03-17 20:13:31 +13:00
!entities.TryGetComponent(_owner, out HandsComponent? handsComponent))
{
return Outcome.Failed;
}
2022-03-17 20:13:31 +13:00
if (!handsSys.TrySelect<FoodComponent>(_owner, out var foodComponent, handsComponent))
return Outcome.Failed;
2022-03-17 20:13:31 +13:00
if (!handsSys.TryUseItemInHand(_owner, false, handsComponent))
return Outcome.Failed;
2020-08-22 12:24:59 +02:00
2021-12-05 18:09:01 +01:00
if ((!entities.EntityExists(_target) ? EntityLifeStage.Deleted : entities.GetComponent<MetaDataComponent>(_target).EntityLifeStage) >= EntityLifeStage.Deleted ||
2020-08-22 12:24:59 +02:00
foodComponent.UsesRemaining == 0 ||
2021-12-05 18:09:01 +01:00
entities.TryGetComponent(_owner, out HungerComponent? hungerComponent) &&
hungerComponent.CurrentHunger >= hungerComponent.HungerThresholds[HungerThreshold.Okay])
{
return Outcome.Success;
}
2022-03-17 20:13:31 +13:00
/// do afters for food might mess this up?
return Outcome.Continuing;
}
}
2020-08-22 12:24:59 +02:00
}