NPC utility queries (#15843)

This commit is contained in:
metalgearsloth
2023-05-02 04:57:11 +10:00
committed by GitHub
parent ac5afa794e
commit ca07522c03
50 changed files with 873 additions and 246 deletions

View File

@@ -20,6 +20,13 @@ public sealed class HTNComponent : NPCComponent
[ViewVariables]
public HTNPlan? Plan;
// TODO: Need dictionary timeoffsetserializer.
/// <summary>
/// Last time we tried a particular <see cref="UtilityService"/>.
/// </summary>
[DataField("serviceCooldowns")]
public Dictionary<string, TimeSpan> ServiceCooldowns = new();
/// <summary>
/// How long to wait after having planned to try planning again.
/// </summary>
@@ -42,6 +49,4 @@ public sealed class HTNComponent : NPCComponent
/// Is this NPC currently planning?
/// </summary>
[ViewVariables] public bool Planning => PlanningJob != null;
}

View File

@@ -13,17 +13,22 @@ using JetBrains.Annotations;
using Robust.Server.Player;
using Robust.Shared.Players;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
using Robust.Shared.Timing;
namespace Content.Server.NPC.HTN;
public sealed class HTNSystem : EntitySystem
{
[Dependency] private readonly IAdminManager _admin = default!;
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly NPCSystem _npc = default!;
[Dependency] private readonly NPCUtilitySystem _utility = default!;
private ISawmill _sawmill = default!;
private readonly JobQueue _planQueue = new();
private readonly JobQueue _planQueue = new(0.004);
private readonly HashSet<ICommonSession> _subscribers = new();
@@ -37,12 +42,22 @@ public sealed class HTNSystem : EntitySystem
base.Initialize();
_sawmill = Logger.GetSawmill("npc.htn");
SubscribeLocalEvent<HTNComponent, ComponentShutdown>(OnHTNShutdown);
SubscribeLocalEvent<HTNComponent, EntityUnpausedEvent>(OnHTNUnpaused);
SubscribeNetworkEvent<RequestHTNMessage>(OnHTNMessage);
_prototypeManager.PrototypesReloaded += OnPrototypeLoad;
OnLoad();
}
private void OnHTNUnpaused(EntityUid uid, HTNComponent component, ref EntityUnpausedEvent args)
{
foreach (var (service, cooldown) in component.ServiceCooldowns)
{
var newCooldown = cooldown + args.PausedTime;
component.ServiceCooldowns[service] = newCooldown;
}
}
private void OnHTNMessage(RequestHTNMessage msg, EntitySessionEventArgs args)
{
if (!_admin.HasAdminFlag((IPlayerSession) args.SenderSession, AdminFlags.Debug))
@@ -251,7 +266,7 @@ public sealed class HTNSystem : EntitySystem
// If it's the selected BTR then highlight.
for (var i = 0; i < btr.Count; i++)
{
text.Append('-');
text.Append("--");
}
text.Append(' ');
@@ -272,7 +287,7 @@ public sealed class HTNSystem : EntitySystem
{
var branch = branches[i];
btr.Add(i);
text.AppendLine($" branch {string.Join(" ", btr)}:");
text.AppendLine($" branch {string.Join(", ", btr)}:");
foreach (var sub in branch)
{
@@ -313,7 +328,25 @@ public sealed class HTNSystem : EntitySystem
{
// Run the existing operator
var currentOperator = component.Plan.CurrentOperator;
var currentTask = component.Plan.CurrentTask;
var blackboard = component.Blackboard;
foreach (var service in currentTask.Services)
{
// Service still on cooldown.
if (component.ServiceCooldowns.TryGetValue(service.ID, out var lastService) &&
_timing.CurTime < lastService)
{
continue;
}
var serviceResult = _utility.GetEntities(blackboard, service.Prototype);
blackboard.SetValue(service.Key, serviceResult.GetHighest());
var cooldown = TimeSpan.FromSeconds(_random.NextFloat(service.MinCooldown, service.MaxCooldown));
component.ServiceCooldowns[service.ID] = _timing.CurTime + cooldown;
}
status = currentOperator.Update(blackboard, frameTime);
switch (status)
@@ -322,6 +355,7 @@ public sealed class HTNSystem : EntitySystem
break;
case HTNOperatorStatus.Failed:
currentOperator.Shutdown(blackboard, status);
component.ServiceCooldowns.Clear();
component.Plan = null;
break;
// Operator completed so go to the next one.
@@ -332,6 +366,7 @@ public sealed class HTNSystem : EntitySystem
// Plan finished!
if (component.Plan.Tasks.Count <= component.Plan.Index)
{
component.ServiceCooldowns.Clear();
component.Plan = null;
break;
}

View File

@@ -1,4 +1,5 @@
using Content.Server.NPC.HTN.Preconditions;
using Content.Server.NPC.Queries;
using Robust.Shared.Prototypes;
namespace Content.Server.NPC.HTN.PrimitiveTasks;
@@ -19,4 +20,9 @@ public sealed class HTNPrimitiveTask : HTNTask
[DataField("preconditions")] public List<HTNPrecondition> Preconditions = new();
[DataField("operator", required:true)] public HTNOperator Operator = default!;
/// <summary>
/// Services actively tick and can potentially update keys, such as combat target.
/// </summary>
[DataField("services")] public List<UtilityService> Services = new();
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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