PickAccessibleComponentOperator.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System.Linq;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. using Content.Server.NPC.Pathfinding;
  5. using Robust.Shared.Map;
  6. namespace Content.Server.NPC.HTN.PrimitiveTasks.Operators;
  7. /// <summary>
  8. /// Picks a nearby component that is accessible.
  9. /// </summary>
  10. public sealed partial class PickAccessibleComponentOperator : HTNOperator
  11. {
  12. [Dependency] private readonly IComponentFactory _factory = default!;
  13. [Dependency] private readonly IEntityManager _entManager = default!;
  14. private PathfindingSystem _pathfinding = default!;
  15. private EntityLookupSystem _lookup = default!;
  16. [DataField("rangeKey", required: true)]
  17. public string RangeKey = string.Empty;
  18. [DataField("targetKey", required: true)]
  19. public string TargetKey = string.Empty;
  20. [DataField("target")]
  21. public string TargetEntity = "Target";
  22. [DataField("component", required: true)]
  23. public string Component = string.Empty;
  24. /// <summary>
  25. /// Where the pathfinding result will be stored (if applicable). This gets removed after execution.
  26. /// </summary>
  27. [DataField("pathfindKey")]
  28. public string PathfindKey = NPCBlackboard.PathfindKey;
  29. public override void Initialize(IEntitySystemManager sysManager)
  30. {
  31. base.Initialize(sysManager);
  32. _lookup = sysManager.GetEntitySystem<EntityLookupSystem>();
  33. _pathfinding = sysManager.GetEntitySystem<PathfindingSystem>();
  34. }
  35. /// <inheritdoc/>
  36. public override async Task<(bool Valid, Dictionary<string, object>? Effects)> Plan(NPCBlackboard blackboard,
  37. CancellationToken cancelToken)
  38. {
  39. // Check if the component exists
  40. if (!_factory.TryGetRegistration(Component, out var registration))
  41. {
  42. return (false, null);
  43. }
  44. var range = blackboard.GetValueOrDefault<float>(RangeKey, _entManager);
  45. var owner = blackboard.GetValue<EntityUid>(NPCBlackboard.Owner);
  46. if (!blackboard.TryGetValue<EntityCoordinates>(NPCBlackboard.OwnerCoordinates, out var coordinates, _entManager))
  47. {
  48. return (false, null);
  49. }
  50. var compType = registration.Type;
  51. var query = _entManager.GetEntityQuery(compType);
  52. var targets = new List<EntityUid>();
  53. // TODO: Need to get ones that are accessible.
  54. // TODO: Look at unreal HTN to see repeatable ones maybe?
  55. // TODO: Need type
  56. foreach (var entity in _lookup.GetEntitiesInRange(coordinates, range))
  57. {
  58. if (entity == owner || !query.TryGetComponent(entity, out var comp))
  59. continue;
  60. targets.Add(entity);
  61. }
  62. if (targets.Count == 0)
  63. {
  64. return (false, null);
  65. }
  66. foreach (var target in targets)
  67. {
  68. var path = await _pathfinding.GetPath(
  69. owner,
  70. target,
  71. 1f,
  72. cancelToken,
  73. flags: _pathfinding.GetFlags(blackboard));
  74. if (path.Result != PathResult.Path)
  75. {
  76. return (false, null);
  77. }
  78. var xform = _entManager.GetComponent<TransformComponent>(target);
  79. return (true, new Dictionary<string, object>()
  80. {
  81. { TargetEntity, target },
  82. { TargetKey, xform.Coordinates },
  83. { PathfindKey, path }
  84. });
  85. }
  86. return (false, null);
  87. }
  88. }