AI pickup changes (#1811)
* AI pickup changes Eating and drinking isn't spammed anymore. AI can do InRangeUnobstructed checks for item pickups. AI can open drink cans. AI littering to be coded. * #nullable enable * github's nullable fails are actively shortening my lifespan * Use a const instead So it's easier to find given the performance implications. Co-authored-by: Metal Gear Sloth <metalgearsloth@gmail.com>
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using Content.Server.AI.Utility;
|
||||
#nullable enable
|
||||
using Content.Server.AI.Utility;
|
||||
using Content.Server.AI.WorldState.States.Inventory;
|
||||
using Content.Server.GameObjects.Components.Items.Storage;
|
||||
using Content.Server.Utility;
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
#nullable enable
|
||||
using Content.Server.GameObjects.Components.GUI;
|
||||
using Content.Server.GameObjects.Components.Items.Storage;
|
||||
using Content.Server.GameObjects.EntitySystems.Click;
|
||||
@@ -20,11 +21,9 @@ namespace Content.Server.AI.Operators.Inventory
|
||||
_target = target;
|
||||
}
|
||||
|
||||
// TODO: When I spawn new entities they seem to duplicate clothing or something?
|
||||
public override Outcome Execute(float frameTime)
|
||||
{
|
||||
if (_target == null ||
|
||||
_target.Deleted ||
|
||||
if (_target.Deleted ||
|
||||
!_target.HasComponent<ItemComponent>() ||
|
||||
ContainerHelpers.IsInContainer(_target) ||
|
||||
!InteractionChecks.InRangeUnobstructed(_owner, _target.Transform.MapPosition))
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
#nullable enable
|
||||
using Content.Server.GameObjects.Components.GUI;
|
||||
using Content.Server.GameObjects.Components.Items.Storage;
|
||||
using Robust.Shared.Interfaces.GameObjects;
|
||||
@@ -7,12 +8,12 @@ namespace Content.Server.AI.Operators.Inventory
|
||||
/// <summary>
|
||||
/// Will find the item in storage, put it in an active hand, then use it
|
||||
/// </summary>
|
||||
public class UseItemInHandsOperator : AiOperator
|
||||
public class UseItemInInventoryOperator : AiOperator
|
||||
{
|
||||
private readonly IEntity _owner;
|
||||
private readonly IEntity _target;
|
||||
|
||||
public UseItemInHandsOperator(IEntity owner, IEntity target)
|
||||
public UseItemInInventoryOperator(IEntity owner, IEntity target)
|
||||
{
|
||||
_owner = owner;
|
||||
_target = target;
|
||||
@@ -20,11 +21,6 @@ namespace Content.Server.AI.Operators.Inventory
|
||||
|
||||
public override Outcome Execute(float frameTime)
|
||||
{
|
||||
if (_target == null)
|
||||
{
|
||||
return Outcome.Failed;
|
||||
}
|
||||
|
||||
// TODO: Also have this check storage a la backpack etc.
|
||||
if (!_owner.TryGetComponent(out HandsComponent handsComponent))
|
||||
{
|
||||
@@ -16,12 +16,20 @@ namespace Content.Server.AI.Operators.Movement
|
||||
public float ArrivalDistance { get; }
|
||||
public float PathfindingProximity { get; }
|
||||
|
||||
public MoveToEntityOperator(IEntity owner, IEntity target, float arrivalDistance = 1.0f, float pathfindingProximity = 1.5f)
|
||||
private bool _requiresInRangeUnobstructed;
|
||||
|
||||
public MoveToEntityOperator(
|
||||
IEntity owner,
|
||||
IEntity target,
|
||||
float arrivalDistance = 1.0f,
|
||||
float pathfindingProximity = 1.5f,
|
||||
bool requiresInRangeUnobstructed = false)
|
||||
{
|
||||
_owner = owner;
|
||||
_target = target;
|
||||
ArrivalDistance = arrivalDistance;
|
||||
PathfindingProximity = pathfindingProximity;
|
||||
_requiresInRangeUnobstructed = requiresInRangeUnobstructed;
|
||||
}
|
||||
|
||||
public override bool TryStartup()
|
||||
@@ -32,7 +40,7 @@ namespace Content.Server.AI.Operators.Movement
|
||||
}
|
||||
|
||||
var steering = EntitySystem.Get<AiSteeringSystem>();
|
||||
_request = new EntityTargetSteeringRequest(_target, ArrivalDistance, PathfindingProximity);
|
||||
_request = new EntityTargetSteeringRequest(_target, ArrivalDistance, PathfindingProximity, _requiresInRangeUnobstructed);
|
||||
steering.Register(_owner, _request);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
#nullable enable
|
||||
using Content.Server.GameObjects.Components.GUI;
|
||||
using Content.Server.GameObjects.Components.Items.Storage;
|
||||
using Content.Server.GameObjects.Components.Nutrition;
|
||||
using Content.Shared.GameObjects.Components.Nutrition;
|
||||
using Robust.Shared.Interfaces.GameObjects;
|
||||
using Robust.Shared.Interfaces.Random;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Random;
|
||||
|
||||
namespace Content.Server.AI.Operators.Nutrition
|
||||
{
|
||||
public class UseDrinkInInventoryOperator : AiOperator
|
||||
{
|
||||
private readonly IEntity _owner;
|
||||
private readonly IEntity _target;
|
||||
private float _interactionCooldown;
|
||||
|
||||
public UseDrinkInInventoryOperator(IEntity owner, IEntity target)
|
||||
{
|
||||
_owner = owner;
|
||||
_target = target;
|
||||
}
|
||||
|
||||
public override Outcome Execute(float frameTime)
|
||||
{
|
||||
if (_interactionCooldown >= 0)
|
||||
{
|
||||
_interactionCooldown -= frameTime;
|
||||
return Outcome.Continuing;
|
||||
}
|
||||
|
||||
// TODO: Also have this check storage a la backpack etc.
|
||||
if (_target.Deleted ||
|
||||
!_owner.TryGetComponent(out HandsComponent handsComponent) ||
|
||||
!_target.TryGetComponent(out ItemComponent itemComponent))
|
||||
{
|
||||
return Outcome.Failed;
|
||||
}
|
||||
|
||||
DrinkComponent? drinkComponent = null;
|
||||
|
||||
foreach (var slot in handsComponent.ActivePriorityEnumerable())
|
||||
{
|
||||
if (handsComponent.GetItem(slot) != itemComponent) continue;
|
||||
handsComponent.ActiveHand = slot;
|
||||
if (!_target.TryGetComponent(out drinkComponent))
|
||||
{
|
||||
return Outcome.Failed;
|
||||
}
|
||||
|
||||
// This should also implicitly open it.
|
||||
handsComponent.ActivateItem();
|
||||
_interactionCooldown = IoCManager.Resolve<IRobustRandom>().NextFloat() + 0.5f;
|
||||
}
|
||||
|
||||
if (drinkComponent == null)
|
||||
{
|
||||
return Outcome.Failed;
|
||||
}
|
||||
|
||||
if (drinkComponent.Deleted ||
|
||||
drinkComponent.Empty ||
|
||||
_owner.TryGetComponent(out ThirstComponent thirstComponent) &&
|
||||
thirstComponent.CurrentThirst >= thirstComponent.ThirstThresholds[ThirstThreshold.Okay])
|
||||
{
|
||||
return Outcome.Success;
|
||||
}
|
||||
|
||||
return Outcome.Continuing;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
#nullable enable
|
||||
using Content.Server.GameObjects.Components.GUI;
|
||||
using Content.Server.GameObjects.Components.Items.Storage;
|
||||
using Content.Server.GameObjects.Components.Nutrition;
|
||||
using Content.Shared.GameObjects.Components.Nutrition;
|
||||
using Robust.Shared.Interfaces.GameObjects;
|
||||
using Robust.Shared.Interfaces.Random;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Random;
|
||||
|
||||
namespace Content.Server.AI.Operators.Nutrition
|
||||
{
|
||||
public class UseFoodInInventoryOperator : AiOperator
|
||||
{
|
||||
private readonly IEntity _owner;
|
||||
private readonly IEntity _target;
|
||||
private float _interactionCooldown;
|
||||
|
||||
public UseFoodInInventoryOperator(IEntity owner, IEntity target)
|
||||
{
|
||||
_owner = owner;
|
||||
_target = target;
|
||||
}
|
||||
|
||||
public override Outcome Execute(float frameTime)
|
||||
{
|
||||
if (_interactionCooldown >= 0)
|
||||
{
|
||||
_interactionCooldown -= frameTime;
|
||||
return Outcome.Continuing;
|
||||
}
|
||||
|
||||
// TODO: Also have this check storage a la backpack etc.
|
||||
if (_target.Deleted ||
|
||||
!_owner.TryGetComponent(out HandsComponent handsComponent) ||
|
||||
!_target.TryGetComponent(out ItemComponent itemComponent))
|
||||
{
|
||||
return Outcome.Failed;
|
||||
}
|
||||
|
||||
FoodComponent? foodComponent = null;
|
||||
|
||||
foreach (var slot in handsComponent.ActivePriorityEnumerable())
|
||||
{
|
||||
if (handsComponent.GetItem(slot) != itemComponent) continue;
|
||||
handsComponent.ActiveHand = slot;
|
||||
if (!_target.TryGetComponent(out foodComponent))
|
||||
{
|
||||
return Outcome.Failed;
|
||||
}
|
||||
|
||||
// This should also implicitly open it.
|
||||
handsComponent.ActivateItem();
|
||||
_interactionCooldown = IoCManager.Resolve<IRobustRandom>().NextFloat() + 0.5f;
|
||||
}
|
||||
|
||||
if (foodComponent == null)
|
||||
{
|
||||
return Outcome.Failed;
|
||||
}
|
||||
|
||||
if (_target.Deleted ||
|
||||
foodComponent.UsesRemaining == 0 ||
|
||||
_owner.TryGetComponent(out HungerComponent hungerComponent) &&
|
||||
hungerComponent.CurrentHunger >= hungerComponent.HungerThresholds[HungerThreshold.Okay])
|
||||
{
|
||||
return Outcome.Success;
|
||||
}
|
||||
|
||||
return Outcome.Continuing;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,7 +11,7 @@ namespace Content.Server.AI.Operators.Sequences
|
||||
{
|
||||
Sequence = new Queue<AiOperator>(new AiOperator[]
|
||||
{
|
||||
new MoveToEntityOperator(owner, target),
|
||||
new MoveToEntityOperator(owner, target, requiresInRangeUnobstructed: true),
|
||||
new OpenStorageOperator(owner, target),
|
||||
new PickupEntityOperator(owner, target),
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user