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

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