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:
metalgearsloth
2020-08-22 20:03:24 +10:00
committed by GitHub
parent b3156e9934
commit 72e50cce94
21 changed files with 233 additions and 41 deletions

View File

@@ -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;

View File

@@ -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))

View File

@@ -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))
{

View File

@@ -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;
}

View File

@@ -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;
}
}
}

View File

@@ -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;
}
}
}

View File

@@ -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),
});

View File

@@ -26,7 +26,7 @@ namespace Content.Server.AI.Utility.Actions.Clothing.Gloves
ActionOperators = new Queue<AiOperator>(new AiOperator[]
{
new EquipEntityOperator(Owner, _entity),
new UseItemInHandsOperator(Owner, _entity),
new UseItemInInventoryOperator(Owner, _entity),
});
}

View File

@@ -26,7 +26,7 @@ namespace Content.Server.AI.Utility.Actions.Clothing.Head
ActionOperators = new Queue<AiOperator>(new AiOperator[]
{
new EquipEntityOperator(Owner, _entity),
new UseItemInHandsOperator(Owner, _entity),
new UseItemInInventoryOperator(Owner, _entity),
});
}

View File

@@ -26,7 +26,7 @@ namespace Content.Server.AI.Utility.Actions.Clothing.OuterClothing
ActionOperators = new Queue<AiOperator>(new AiOperator[]
{
new EquipEntityOperator(Owner, _entity),
new UseItemInHandsOperator(Owner, _entity),
new UseItemInInventoryOperator(Owner, _entity),
});
}

View File

@@ -26,7 +26,7 @@ namespace Content.Server.AI.Utility.Actions.Clothing.Shoes
ActionOperators = new Queue<AiOperator>(new AiOperator[]
{
new EquipEntityOperator(Owner, _entity),
new UseItemInHandsOperator(Owner, _entity),
new UseItemInInventoryOperator(Owner, _entity),
});
}

View File

@@ -40,10 +40,6 @@ namespace Content.Server.AI.Utility.Actions.Nutrition.Drink
return new[]
{
considerationsManager.Get<FreeHandCon>()
.BoolCurve(context),
considerationsManager.Get<ThirstCon>()
.PresetCurve(context, PresetCurve.Nutrition),
considerationsManager.Get<TargetDistanceCon>()
.PresetCurve(context, PresetCurve.Distance),
considerationsManager.Get<DrinkValueCon>()

View File

@@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using Content.Server.AI.Operators;
using Content.Server.AI.Operators.Inventory;
using Content.Server.AI.Operators.Nutrition;
using Content.Server.AI.Utility.Considerations;
using Content.Server.AI.Utility.Considerations.Inventory;
using Content.Server.AI.Utility.Considerations.Nutrition.Drink;
@@ -27,7 +28,7 @@ namespace Content.Server.AI.Utility.Actions.Nutrition.Drink
ActionOperators = new Queue<AiOperator>(new AiOperator[]
{
new EquipEntityOperator(Owner, _entity),
new UseItemInHandsOperator(Owner, _entity),
new UseDrinkInInventoryOperator(Owner, _entity),
});
}

View File

@@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using Content.Server.AI.Operators;
using Content.Server.AI.Operators.Inventory;
using Content.Server.AI.Operators.Nutrition;
using Content.Server.AI.Utility.Considerations;
using Content.Server.AI.Utility.Considerations.Inventory;
using Content.Server.AI.Utility.Considerations.Nutrition.Food;
@@ -27,7 +28,7 @@ namespace Content.Server.AI.Utility.Actions.Nutrition.Food
ActionOperators = new Queue<AiOperator>(new AiOperator[]
{
new EquipEntityOperator(Owner, _entity),
new UseItemInHandsOperator(Owner, _entity),
new UseFoodInInventoryOperator(Owner, _entity),
});
}

View File

@@ -11,7 +11,7 @@ namespace Content.Server.AI.Utility.Considerations.Combat
{
var target = context.GetState<TargetEntityState>().GetValue();
if (target == null || !target.TryGetComponent(out IDamageableComponent damageableComponent))
if (target == null || target.Deleted || !target.TryGetComponent(out IDamageableComponent damageableComponent))
{
return 0.0f;
}

View File

@@ -9,7 +9,7 @@ namespace Content.Server.AI.Utility.Considerations.Movement
{
var self = context.GetState<SelfState>().GetValue();
var target = context.GetState<TargetEntityState>().GetValue();
if (target == null || target.Transform.GridID != self.Transform.GridID)
if (target == null || target.Deleted || target.Transform.GridID != self.Transform.GridID)
{
return 0.0f;
}

View File

@@ -44,7 +44,7 @@ namespace Content.Server.GameObjects.Components.Nutrition
[ViewVariables]
public ReagentUnit TransferAmount { get; private set; } = ReagentUnit.New(2);
[ViewVariables]
protected bool Opened { get; set; }
public bool Opened { get; protected set; }
[ViewVariables]
public bool Empty => _contents.CurrentVolume.Float() <= 0;

View File

@@ -7,6 +7,7 @@ using Content.Server.GameObjects.Components.Movement;
using Content.Server.GameObjects.EntitySystems.AI.Pathfinding;
using Content.Server.GameObjects.EntitySystems.AI.Pathfinding.Pathfinders;
using Content.Server.GameObjects.EntitySystems.JobQueues;
using Content.Server.Utility;
using Content.Shared.GameObjects.EntitySystems;
using Robust.Server.Interfaces.Timing;
using Robust.Shared.GameObjects.Components;
@@ -42,6 +43,11 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Steering
/// </summary>
private const float TileTolerance = 0.8f;
/// <summary>
/// How long to wait between checks (if necessary).
/// </summary>
private const float InRangeUnobstructedCooldown = 0.25f;
private Dictionary<IEntity, IAiSteeringRequest> RunningAgents => _agentLists[_listIndex];
// We'll cycle the running list every tick as all we're doing is getting a vector2 for the
@@ -208,7 +214,8 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Steering
foreach (var (agent, steering) in RunningAgents)
{
var result = Steer(agent, steering);
// Yeah look it's not true frametime but good enough.
var result = Steer(agent, steering, frameTime * RunningAgents.Count);
steering.Status = result;
switch (result)
@@ -236,9 +243,10 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Steering
/// </summary>
/// <param name="entity"></param>
/// <param name="steeringRequest"></param>
/// <param name="frameTime"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
private SteeringStatus Steer(IEntity entity, IAiSteeringRequest steeringRequest)
private SteeringStatus Steer(IEntity entity, IAiSteeringRequest steeringRequest, float frameTime)
{
// Main optimisation to be done below is the redundant calls and adding more variables
if (!entity.TryGetComponent(out AiControllerComponent controller) || !ActionBlockerSystem.CanMove(entity))
@@ -262,15 +270,29 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Steering
// Check if we have arrived
var targetDistance = (entity.Transform.MapPosition.Position - steeringRequest.TargetMap.Position).Length;
if (targetDistance <= steeringRequest.ArrivalDistance)
steeringRequest.TimeUntilInteractionCheck -= frameTime;
if (targetDistance <= steeringRequest.ArrivalDistance && steeringRequest.TimeUntilInteractionCheck <= 0.0f)
{
// TODO: If we need LOS and are moving to an entity then we may not be in range yet
// Chuck out a ray every half second or so and keep moving until we are?
// Alternatively could use tile-based LOS checks via the pathfindingsystem I guess
if (!steeringRequest.RequiresInRangeUnobstructed ||
InteractionChecks.InRangeUnobstructed(entity, steeringRequest.TargetMap, steeringRequest.ArrivalDistance, ignoredEnt: entity))
{
// TODO: Need cruder LOS checks for ranged weaps
controller.VelocityDir = Vector2.Zero;
return SteeringStatus.Arrived;
}
steeringRequest.TimeUntilInteractionCheck = InRangeUnobstructedCooldown;
// Welp, we'll keep on moving.
}
// If we're really close don't swiggity swoogity back and forth and just wait for the interaction check maybe?
if (steeringRequest.TimeUntilInteractionCheck > 0.0f && targetDistance <= 0.1f)
{
controller.VelocityDir = Vector2.Zero;
return SteeringStatus.Moving;
}
// Handle pathfinding job
// If we still have an existing path then keep following that until the new path arrives
if (_pathfindingRequests.TryGetValue(entity, out var pathRequest) && pathRequest.Job.Status == JobStatus.Finished)

View File

@@ -10,20 +10,26 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Steering
public GridCoordinates TargetGrid => _target.Transform.GridPosition;
public IEntity Target => _target;
private IEntity _target;
/// <inheritdoc />
public float ArrivalDistance { get; }
/// <inheritdoc />
public float PathfindingProximity { get; }
/// <summary>
/// How far the target can move before we re-path
/// </summary>
public float TargetMaxMove { get; } = 1.5f;
/// <summary>
/// If we need LOS on the entity first before interaction
/// </summary>
/// <inheritdoc />
public bool RequiresInRangeUnobstructed { get; }
/// <summary>
/// To avoid spamming InRangeUnobstructed we'll apply a cd to it.
/// </summary>
public float TimeUntilInteractionCheck { get; set; }
public EntityTargetSteeringRequest(IEntity target, float arrivalDistance, float pathfindingProximity = 0.5f, bool requiresInRangeUnobstructed = false)
{
_target = target;

View File

@@ -14,7 +14,12 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Steering
/// <inheritdoc />
public float PathfindingProximity { get; }
public GridTargetSteeringRequest(GridCoordinates targetGrid, float arrivalDistance, float pathfindingProximity = 0.5f)
public bool RequiresInRangeUnobstructed { get; }
public float TimeUntilInteractionCheck { get; set; } = 0.0f;
public GridTargetSteeringRequest(GridCoordinates targetGrid, float arrivalDistance, float pathfindingProximity = 0.5f, bool requiresInRangeUnobstructed = false)
{
// Get it once up front so we the manager doesn't have to continuously get it
var mapManager = IoCManager.Resolve<IMapManager>();
@@ -22,6 +27,7 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Steering
TargetGrid = targetGrid;
ArrivalDistance = arrivalDistance;
PathfindingProximity = pathfindingProximity;
RequiresInRangeUnobstructed = requiresInRangeUnobstructed;
}
}
}

View File

@@ -16,5 +16,15 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Steering
/// How close the pathfinder needs to get. Typically you want this set lower than ArrivalDistance
/// </summary>
float PathfindingProximity { get; }
/// <summary>
/// If we need LOS on the entity first before interaction
/// </summary>
bool RequiresInRangeUnobstructed { get; }
/// <summary>
/// To avoid spamming InRangeUnobstructed we'll apply a cd to it.
/// </summary>
public float TimeUntilInteractionCheck { get; set; }
}
}