NPC utility queries (#15843)
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Content.Shared.DoAfter;
|
||||
using Content.Shared.Interaction;
|
||||
|
||||
namespace Content.Server.NPC.HTN.PrimitiveTasks.Operators;
|
||||
|
||||
public sealed class AltInteractOperator : HTNOperator
|
||||
{
|
||||
[Dependency] private readonly IEntityManager _entManager = default!;
|
||||
|
||||
[DataField("targetKey")]
|
||||
public string Key = "CombatTarget";
|
||||
|
||||
/// <summary>
|
||||
/// If this alt-interaction started a do_after where does the key get stored.
|
||||
/// </summary>
|
||||
[DataField("idleKey")]
|
||||
public string IdleKey = "IdleTime";
|
||||
|
||||
public override async Task<(bool Valid, Dictionary<string, object>? Effects)> Plan(NPCBlackboard blackboard, CancellationToken cancelToken)
|
||||
{
|
||||
return new(true, new Dictionary<string, object>()
|
||||
{
|
||||
{ IdleKey, 1f }
|
||||
});
|
||||
}
|
||||
|
||||
public override HTNOperatorStatus Update(NPCBlackboard blackboard, float frameTime)
|
||||
{
|
||||
var owner = blackboard.GetValue<EntityUid>(NPCBlackboard.Owner);
|
||||
var target = blackboard.GetValue<EntityUid>(Key);
|
||||
var intSystem = _entManager.System<SharedInteractionSystem>();
|
||||
var count = 0;
|
||||
|
||||
if (_entManager.TryGetComponent<DoAfterComponent>(owner, out var doAfter))
|
||||
{
|
||||
count = doAfter.DoAfters.Count;
|
||||
}
|
||||
|
||||
var result = intSystem.AltInteract(owner, target);
|
||||
|
||||
// Interaction started a doafter so set the idle time to it.
|
||||
if (result && doAfter != null && count != doAfter.DoAfters.Count)
|
||||
{
|
||||
var wait = doAfter.DoAfters.First().Value.Args.Delay;
|
||||
blackboard.SetValue(IdleKey, (float) wait.TotalSeconds + 0.5f);
|
||||
}
|
||||
else
|
||||
{
|
||||
blackboard.SetValue(IdleKey, 1f);
|
||||
}
|
||||
|
||||
return result ? HTNOperatorStatus.Finished : HTNOperatorStatus.Failed;
|
||||
}
|
||||
}
|
||||
@@ -45,7 +45,6 @@ public sealed class MeleeOperator : HTNOperator
|
||||
}
|
||||
|
||||
if (_entManager.TryGetComponent<MobStateComponent>(target, out var mobState) &&
|
||||
mobState.CurrentState != null &&
|
||||
mobState.CurrentState > TargetState)
|
||||
{
|
||||
return (false, null);
|
||||
@@ -65,13 +64,15 @@ public sealed class MeleeOperator : HTNOperator
|
||||
{
|
||||
base.Update(blackboard, frameTime);
|
||||
var owner = blackboard.GetValue<EntityUid>(NPCBlackboard.Owner);
|
||||
var status = HTNOperatorStatus.Continuing;
|
||||
HTNOperatorStatus status;
|
||||
|
||||
if (_entManager.TryGetComponent<NPCMeleeCombatComponent>(owner, out var combat))
|
||||
if (_entManager.TryGetComponent<NPCMeleeCombatComponent>(owner, out var combat) &&
|
||||
blackboard.TryGetValue<EntityUid>(TargetKey, out var target, _entManager))
|
||||
{
|
||||
combat.Target = target;
|
||||
|
||||
// Success
|
||||
if (_entManager.TryGetComponent<MobStateComponent>(combat.Target, out var mobState) &&
|
||||
mobState.CurrentState != null &&
|
||||
if (_entManager.TryGetComponent<MobStateComponent>(target, out var mobState) &&
|
||||
mobState.CurrentState > TargetState)
|
||||
{
|
||||
status = HTNOperatorStatus.Finished;
|
||||
@@ -90,6 +91,10 @@ public sealed class MeleeOperator : HTNOperator
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
status = HTNOperatorStatus.Failed;
|
||||
}
|
||||
|
||||
if (status != HTNOperatorStatus.Continuing)
|
||||
{
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
using JetBrains.Annotations;
|
||||
|
||||
namespace Content.Server.NPC.HTN.PrimitiveTasks.Operators.Melee;
|
||||
|
||||
/// <summary>
|
||||
/// Selects a target for melee.
|
||||
/// </summary>
|
||||
[MeansImplicitUse]
|
||||
public sealed class PickMeleeTargetOperator : NPCCombatOperator
|
||||
{
|
||||
protected override float GetRating(NPCBlackboard blackboard, EntityUid uid, EntityUid existingTarget, float distance, bool canMove, EntityQuery<TransformComponent> xformQuery)
|
||||
{
|
||||
var rating = 0f;
|
||||
|
||||
if (existingTarget == uid)
|
||||
{
|
||||
rating += 2f;
|
||||
}
|
||||
|
||||
if (distance > 0f)
|
||||
rating += 50f / distance;
|
||||
|
||||
return rating;
|
||||
}
|
||||
}
|
||||
@@ -1,170 +0,0 @@
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Content.Server.Interaction;
|
||||
using Content.Server.NPC.Components;
|
||||
using Content.Server.NPC.Pathfinding;
|
||||
using Content.Server.NPC.Systems;
|
||||
using Content.Shared.Examine;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Mobs;
|
||||
using Content.Shared.Mobs.Components;
|
||||
using Robust.Shared.Map;
|
||||
//using Robust.Shared.Prototypes;
|
||||
|
||||
namespace Content.Server.NPC.HTN.PrimitiveTasks.Operators;
|
||||
|
||||
public abstract class NPCCombatOperator : HTNOperator
|
||||
{
|
||||
[Dependency] protected readonly IEntityManager EntManager = default!;
|
||||
private FactionSystem _factions = default!;
|
||||
private FactionExceptionSystem _factionException = default!;
|
||||
protected InteractionSystem Interaction = default!;
|
||||
private PathfindingSystem _pathfinding = default!;
|
||||
|
||||
[DataField("key")] public string Key = "CombatTarget";
|
||||
|
||||
/// <summary>
|
||||
/// The EntityCoordinates of the specified target.
|
||||
/// </summary>
|
||||
[DataField("keyCoordinates")]
|
||||
public string KeyCoordinates = "CombatTargetCoordinates";
|
||||
|
||||
/// <summary>
|
||||
/// Regardless of pathfinding or LOS these are the max we'll check
|
||||
/// </summary>
|
||||
private const int MaxConsideredTargets = 10;
|
||||
|
||||
protected virtual bool IsRanged => false;
|
||||
|
||||
public override void Initialize(IEntitySystemManager sysManager)
|
||||
{
|
||||
base.Initialize(sysManager);
|
||||
sysManager.GetEntitySystem<ExamineSystemShared>();
|
||||
_factions = sysManager.GetEntitySystem<FactionSystem>();
|
||||
_factionException = sysManager.GetEntitySystem<FactionExceptionSystem>();
|
||||
Interaction = sysManager.GetEntitySystem<InteractionSystem>();
|
||||
_pathfinding = sysManager.GetEntitySystem<PathfindingSystem>();
|
||||
}
|
||||
|
||||
public override async Task<(bool Valid, Dictionary<string, object>? Effects)> Plan(NPCBlackboard blackboard,
|
||||
CancellationToken cancelToken)
|
||||
{
|
||||
var targets = await GetTargets(blackboard);
|
||||
|
||||
if (targets.Count == 0)
|
||||
{
|
||||
return (false, null);
|
||||
}
|
||||
|
||||
// TODO: Need some level of rng in ratings (outside of continuing to attack the same target)
|
||||
var selectedTarget = targets[0].Entity;
|
||||
|
||||
var effects = new Dictionary<string, object>()
|
||||
{
|
||||
{Key, selectedTarget},
|
||||
{KeyCoordinates, new EntityCoordinates(selectedTarget, Vector2.Zero)}
|
||||
};
|
||||
|
||||
return (true, effects);
|
||||
}
|
||||
|
||||
private async Task<List<(EntityUid Entity, float Rating, float Distance)>> GetTargets(NPCBlackboard blackboard)
|
||||
{
|
||||
var owner = blackboard.GetValue<EntityUid>(NPCBlackboard.Owner);
|
||||
var ownerCoordinates = blackboard.GetValueOrDefault<EntityCoordinates>(NPCBlackboard.OwnerCoordinates, EntManager);
|
||||
var radius = blackboard.GetValueOrDefault<float>(NPCBlackboard.VisionRadius, EntManager);
|
||||
var targets = new List<(EntityUid Entity, float Rating, float Distance)>();
|
||||
|
||||
blackboard.TryGetValue<EntityUid>(Key, out var existingTarget, EntManager);
|
||||
var xformQuery = EntManager.GetEntityQuery<TransformComponent>();
|
||||
var mobQuery = EntManager.GetEntityQuery<MobStateComponent>();
|
||||
var canMove = blackboard.GetValueOrDefault<bool>(NPCBlackboard.CanMove, EntManager);
|
||||
var count = 0;
|
||||
var paths = new List<Task>();
|
||||
// TODO: Really this should be a part of perception so we don't have to constantly re-plan targets.
|
||||
|
||||
// Special-case existing target.
|
||||
if (EntManager.EntityExists(existingTarget))
|
||||
{
|
||||
paths.Add(UpdateTarget(owner, existingTarget, existingTarget, ownerCoordinates, blackboard, radius, canMove, xformQuery, targets));
|
||||
}
|
||||
|
||||
EntManager.TryGetComponent<FactionExceptionComponent>(owner, out var factionException);
|
||||
|
||||
// TODO: Need a perception system instead
|
||||
// TODO: This will be expensive so will be good to optimise and cut corners.
|
||||
foreach (var target in _factions
|
||||
.GetNearbyHostiles(owner, radius))
|
||||
{
|
||||
if (mobQuery.TryGetComponent(target, out var mobState) &&
|
||||
mobState.CurrentState > MobState.Alive ||
|
||||
target == existingTarget ||
|
||||
target == owner ||
|
||||
(factionException != null && _factionException.IsIgnored(factionException, target)))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
count++;
|
||||
|
||||
if (count >= MaxConsideredTargets)
|
||||
break;
|
||||
|
||||
paths.Add(UpdateTarget(owner, target, existingTarget, ownerCoordinates, blackboard, radius, canMove, xformQuery, targets));
|
||||
}
|
||||
|
||||
await Task.WhenAll(paths);
|
||||
|
||||
targets.Sort((x, y) => y.Rating.CompareTo(x.Rating));
|
||||
return targets;
|
||||
}
|
||||
|
||||
private async Task UpdateTarget(
|
||||
EntityUid owner,
|
||||
EntityUid target,
|
||||
EntityUid existingTarget,
|
||||
EntityCoordinates ownerCoordinates,
|
||||
NPCBlackboard blackboard,
|
||||
float radius,
|
||||
bool canMove,
|
||||
EntityQuery<TransformComponent> xformQuery,
|
||||
List<(EntityUid Entity, float Rating, float Distance)> targets)
|
||||
{
|
||||
if (!xformQuery.TryGetComponent(target, out var targetXform))
|
||||
return;
|
||||
|
||||
var inLos = false;
|
||||
|
||||
// If it's not an existing target then check LOS.
|
||||
if (target != existingTarget)
|
||||
{
|
||||
inLos = ExamineSystemShared.InRangeUnOccluded(owner, target, radius, null);
|
||||
|
||||
if (!inLos)
|
||||
return;
|
||||
}
|
||||
|
||||
// Turret or the likes, check LOS only.
|
||||
if (IsRanged && !canMove)
|
||||
{
|
||||
inLos = inLos || ExamineSystemShared.InRangeUnOccluded(owner, target, radius, null);
|
||||
|
||||
if (!inLos || !targetXform.Coordinates.TryDistance(EntManager, ownerCoordinates, out var distance))
|
||||
return;
|
||||
|
||||
targets.Add((target, GetRating(blackboard, target, existingTarget, distance, canMove, xformQuery), distance));
|
||||
return;
|
||||
}
|
||||
|
||||
var nDistance = await _pathfinding.GetPathDistance(owner, targetXform.Coordinates,
|
||||
SharedInteractionSystem.InteractionRange, default, _pathfinding.GetFlags(blackboard));
|
||||
|
||||
if (nDistance == null)
|
||||
return;
|
||||
|
||||
targets.Add((target, GetRating(blackboard, target, existingTarget, nDistance.Value, canMove, xformQuery), nDistance.Value));
|
||||
}
|
||||
|
||||
protected abstract float GetRating(NPCBlackboard blackboard, EntityUid uid, EntityUid existingTarget, float distance, bool canMove,
|
||||
EntityQuery<TransformComponent> xformQuery);
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
using JetBrains.Annotations;
|
||||
|
||||
namespace Content.Server.NPC.HTN.PrimitiveTasks.Operators.Ranged;
|
||||
|
||||
/// <summary>
|
||||
/// Selects a target for ranged combat.
|
||||
/// </summary>
|
||||
[UsedImplicitly]
|
||||
public sealed class PickRangedTargetOperator : NPCCombatOperator
|
||||
{
|
||||
protected override bool IsRanged => true;
|
||||
|
||||
protected override float GetRating(NPCBlackboard blackboard, EntityUid uid, EntityUid existingTarget, float distance, bool canMove, EntityQuery<TransformComponent> xformQuery)
|
||||
{
|
||||
// Yeah look I just came up with values that seemed okay but they will need a lot of tweaking.
|
||||
// Having a debug overlay just to project these would be very useful when finetuning in future.
|
||||
var rating = 0f;
|
||||
|
||||
if (existingTarget == uid)
|
||||
{
|
||||
rating += 2f;
|
||||
}
|
||||
|
||||
rating += 1f / distance * 4f;
|
||||
return rating;
|
||||
}
|
||||
}
|
||||
@@ -35,7 +35,6 @@ public sealed class RangedOperator : HTNOperator
|
||||
}
|
||||
|
||||
if (_entManager.TryGetComponent<MobStateComponent>(target, out var mobState) &&
|
||||
mobState.CurrentState != null &&
|
||||
mobState.CurrentState > TargetState)
|
||||
{
|
||||
return (false, null);
|
||||
@@ -72,13 +71,15 @@ public sealed class RangedOperator : HTNOperator
|
||||
{
|
||||
base.Update(blackboard, frameTime);
|
||||
var owner = blackboard.GetValue<EntityUid>(NPCBlackboard.Owner);
|
||||
var status = HTNOperatorStatus.Continuing;
|
||||
HTNOperatorStatus status;
|
||||
|
||||
if (_entManager.TryGetComponent<NPCRangedCombatComponent>(owner, out var combat))
|
||||
if (_entManager.TryGetComponent<NPCRangedCombatComponent>(owner, out var combat) &&
|
||||
blackboard.TryGetValue<EntityUid>(TargetKey, out var target, _entManager))
|
||||
{
|
||||
combat.Target = target;
|
||||
|
||||
// Success
|
||||
if (_entManager.TryGetComponent<MobStateComponent>(combat.Target, out var mobState) &&
|
||||
mobState.CurrentState != null &&
|
||||
mobState.CurrentState > TargetState)
|
||||
{
|
||||
status = HTNOperatorStatus.Finished;
|
||||
@@ -100,6 +101,10 @@ public sealed class RangedOperator : HTNOperator
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
status = HTNOperatorStatus.Failed;
|
||||
}
|
||||
|
||||
if (status != HTNOperatorStatus.Continuing)
|
||||
{
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Content.Server.NPC.Queries;
|
||||
using Content.Server.NPC.Systems;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
|
||||
|
||||
namespace Content.Server.NPC.HTN.PrimitiveTasks.Operators;
|
||||
|
||||
/// <summary>
|
||||
/// Utilises a <see cref="UtilityQueryPrototype"/> to determine the best target and sets it to the Key.
|
||||
/// </summary>
|
||||
public sealed class UtilityOperator : HTNOperator
|
||||
{
|
||||
[Dependency] private readonly IEntityManager _entManager = default!;
|
||||
|
||||
[DataField("key")] public string Key = "CombatTarget";
|
||||
|
||||
/// <summary>
|
||||
/// The EntityCoordinates of the specified target.
|
||||
/// </summary>
|
||||
[DataField("keyCoordinates")]
|
||||
public string KeyCoordinates = "CombatTargetCoordinates";
|
||||
|
||||
[DataField("proto", required: true, customTypeSerializer:typeof(PrototypeIdSerializer<UtilityQueryPrototype>))]
|
||||
public string Prototype = string.Empty;
|
||||
|
||||
public override async Task<(bool Valid, Dictionary<string, object>? Effects)> Plan(NPCBlackboard blackboard,
|
||||
CancellationToken cancelToken)
|
||||
{
|
||||
var result = _entManager.System<NPCUtilitySystem>().GetEntities(blackboard, Prototype);
|
||||
var target = result.GetHighest();
|
||||
|
||||
if (!target.IsValid())
|
||||
{
|
||||
return (false, new Dictionary<string, object>());
|
||||
}
|
||||
|
||||
var effects = new Dictionary<string, object>()
|
||||
{
|
||||
{Key, target},
|
||||
{KeyCoordinates, new EntityCoordinates(target, Vector2.Zero)}
|
||||
};
|
||||
|
||||
return (true, effects);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user