PickAccessibleOperator.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System.Linq;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. using Content.Server.NPC.Pathfinding;
  5. namespace Content.Server.NPC.HTN.PrimitiveTasks.Operators;
  6. /// <summary>
  7. /// Chooses a nearby coordinate and puts it into the resulting key.
  8. /// </summary>
  9. public sealed partial class PickAccessibleOperator : HTNOperator
  10. {
  11. [Dependency] private readonly IEntityManager _entManager = default!;
  12. private PathfindingSystem _pathfinding = default!;
  13. [DataField("rangeKey", required: true)]
  14. public string RangeKey = string.Empty;
  15. [DataField("targetCoordinates")]
  16. public string TargetCoordinates = "TargetCoordinates";
  17. [DataField("targetKey")]
  18. public string TargetKey = default!;
  19. /// supposed be be a toggle to check if the chosen tile is safe
  20. [DataField("isSafe")]
  21. public bool IsSafe = false;
  22. /// <summary>
  23. /// Where the pathfinding result will be stored (if applicable). This gets removed after execution.
  24. /// </summary>
  25. [DataField("pathfindKey")]
  26. public string PathfindKey = NPCBlackboard.PathfindKey;
  27. public override void Initialize(IEntitySystemManager sysManager)
  28. {
  29. base.Initialize(sysManager);
  30. _pathfinding = sysManager.GetEntitySystem<PathfindingSystem>();
  31. }
  32. /// <inheritdoc/>
  33. public override async Task<(bool Valid, Dictionary<string, object>? Effects)> Plan(NPCBlackboard blackboard,
  34. CancellationToken cancelToken)
  35. {
  36. // Very inefficient (should weight each region by its node count) but better than the old system
  37. var owner = blackboard.GetValue<EntityUid>(NPCBlackboard.Owner);
  38. blackboard.TryGetValue<float>(RangeKey, out var maxRange, _entManager);
  39. if (maxRange == 0f)
  40. maxRange = 7f;
  41. var path = await _pathfinding.GetRandomPath(
  42. owner,
  43. maxRange,
  44. cancelToken,
  45. flags: _pathfinding.GetFlags(blackboard));
  46. if (path.Result != PathResult.Path)
  47. {
  48. return (false, null);
  49. }
  50. //CIV14, written by the ever incompetent Aciad
  51. //todo, make it redo the selection if the path doesn't meet the criteria of being further away from
  52. //the target than the current possition, I mean it seems to me like it should
  53. //rn it seems like this doesn't actually do anything
  54. if (IsSafe == true) //not sure entirely if this bit works
  55. {
  56. //this should get the value of whatever entity is set as the target
  57. blackboard.TryGetValue<EntityUid>(TargetKey, out var targetEntity, _entManager);
  58. var ownerTransform = _entManager.GetComponent<TransformComponent>(owner);
  59. var targetTransform = _entManager.GetComponent<TransformComponent>(targetEntity);
  60. //figures out current positions of the entities & the destination target
  61. var pathEndCoordinates = path.Path.Last().Coordinates;
  62. var ownerWorldPosition = ownerTransform.WorldPosition;
  63. var targetWorldPosition = targetTransform.WorldPosition;
  64. var ownerToTargetDistance = (ownerWorldPosition - targetWorldPosition).Length;
  65. var targetToPathEndDistance = (targetWorldPosition - pathEndCoordinates.Position).Length;
  66. if (ownerToTargetDistance.Invoke() < targetToPathEndDistance.Invoke()) //if it ain't far enough away, quit
  67. {
  68. return (false, null);
  69. }
  70. }
  71. var target = path.Path.Last().Coordinates;
  72. return (true, new Dictionary<string, object>()
  73. {
  74. { TargetCoordinates, target },
  75. { PathfindKey, path}
  76. });
  77. }
  78. }