UtilityOperator.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System.Numerics;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. using Content.Server.NPC.Queries;
  5. using Content.Server.NPC.Systems;
  6. using Robust.Shared.Map;
  7. using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
  8. namespace Content.Server.NPC.HTN.PrimitiveTasks.Operators;
  9. /// <summary>
  10. /// Utilises a <see cref="UtilityQueryPrototype"/> to determine the best target and sets it to the Key.
  11. /// </summary>
  12. public sealed partial class UtilityOperator : HTNOperator
  13. {
  14. [Dependency] private readonly IEntityManager _entManager = default!;
  15. [DataField("key")] public string Key = "Target";
  16. /// <summary>
  17. /// The EntityCoordinates of the specified target.
  18. /// </summary>
  19. [DataField("keyCoordinates")]
  20. public string KeyCoordinates = "TargetCoordinates";
  21. [DataField("proto", required: true, customTypeSerializer:typeof(PrototypeIdSerializer<UtilityQueryPrototype>))]
  22. public string Prototype = string.Empty;
  23. public override async Task<(bool Valid, Dictionary<string, object>? Effects)> Plan(NPCBlackboard blackboard,
  24. CancellationToken cancelToken)
  25. {
  26. var result = _entManager.System<NPCUtilitySystem>().GetEntities(blackboard, Prototype);
  27. var target = result.GetHighest();
  28. if (!target.IsValid())
  29. {
  30. return (false, new Dictionary<string, object>());
  31. }
  32. var effects = new Dictionary<string, object>()
  33. {
  34. {Key, target},
  35. {KeyCoordinates, new EntityCoordinates(target, Vector2.Zero)}
  36. };
  37. return (true, effects);
  38. }
  39. }