Files
tbd-station-14/Content.Server/AI/Operators/Inventory/PickupEntityOperator.cs
DrSmugleaf 9d6c394f6b Refactor InRangeUnobstructed and add extension methods (#1925)
* Sort out InRangeUnobstructed and add extension methods

* Rename client RangeChecks to RangeExtensions

* Add container extension methods and test

* Add missing component methods

Component to container
Grid coordinates to container
Map coordinates to container
Local player to container

* Actually use the field

* Merge fixes

* Add popup argument to local player extension methods

* Reduce code repetition for client range extensions
2020-08-30 11:37:06 +02:00

67 lines
1.9 KiB
C#

#nullable enable
using Content.Server.GameObjects.Components.GUI;
using Content.Server.GameObjects.Components.Items.Storage;
using Content.Server.GameObjects.EntitySystems.Click;
using Content.Server.Utility;
using Content.Shared.Utility;
using Robust.Shared.Containers;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.IoC;
namespace Content.Server.AI.Operators.Inventory
{
public class PickupEntityOperator : AiOperator
{
// Input variables
private readonly IEntity _owner;
private readonly IEntity _target;
public PickupEntityOperator(IEntity owner, IEntity target)
{
_owner = owner;
_target = target;
}
public override Outcome Execute(float frameTime)
{
if (_target.Deleted ||
!_target.HasComponent<ItemComponent>() ||
ContainerHelpers.IsInContainer(_target) ||
!_owner.InRangeUnobstructed(_target, popup: true))
{
return Outcome.Failed;
}
if (!_owner.TryGetComponent(out HandsComponent? handsComponent))
{
return Outcome.Failed;
}
var emptyHands = false;
foreach (var hand in handsComponent.ActivePriorityEnumerable())
{
if (handsComponent.GetItem(hand) == null)
{
if (handsComponent.ActiveHand != hand)
{
handsComponent.ActiveHand = hand;
}
emptyHands = true;
break;
}
}
if (!emptyHands)
{
return Outcome.Failed;
}
var interactionSystem = IoCManager.Resolve<IEntitySystemManager>().GetEntitySystem<InteractionSystem>();
interactionSystem.Interaction(_owner, _target);
return Outcome.Success;
}
}
}