PickNearbyInjectableOperator.cs 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System.Threading;
  2. using System.Threading.Tasks;
  3. using Content.Shared.NPC.Components;
  4. using Content.Server.NPC.Pathfinding;
  5. using Content.Shared.Chemistry.Components.SolutionManager;
  6. using Content.Shared.Damage;
  7. using Content.Shared.Interaction;
  8. using Content.Shared.Mobs.Components;
  9. using Content.Shared.Silicons.Bots;
  10. using Content.Shared.Emag.Components;
  11. namespace Content.Server.NPC.HTN.PrimitiveTasks.Operators.Specific;
  12. public sealed partial class PickNearbyInjectableOperator : HTNOperator
  13. {
  14. [Dependency] private readonly IEntityManager _entManager = default!;
  15. private EntityLookupSystem _lookup = default!;
  16. private MedibotSystem _medibot = default!;
  17. private PathfindingSystem _pathfinding = default!;
  18. [DataField("rangeKey")] public string RangeKey = NPCBlackboard.MedibotInjectRange;
  19. /// <summary>
  20. /// Target entity to inject
  21. /// </summary>
  22. [DataField("targetKey", required: true)]
  23. public string TargetKey = string.Empty;
  24. /// <summary>
  25. /// Target entitycoordinates to move to.
  26. /// </summary>
  27. [DataField("targetMoveKey", required: true)]
  28. public string TargetMoveKey = string.Empty;
  29. public override void Initialize(IEntitySystemManager sysManager)
  30. {
  31. base.Initialize(sysManager);
  32. _lookup = sysManager.GetEntitySystem<EntityLookupSystem>();
  33. _medibot = sysManager.GetEntitySystem<MedibotSystem>();
  34. _pathfinding = sysManager.GetEntitySystem<PathfindingSystem>();
  35. }
  36. public override async Task<(bool Valid, Dictionary<string, object>? Effects)> Plan(NPCBlackboard blackboard,
  37. CancellationToken cancelToken)
  38. {
  39. var owner = blackboard.GetValue<EntityUid>(NPCBlackboard.Owner);
  40. if (!blackboard.TryGetValue<float>(RangeKey, out var range, _entManager))
  41. return (false, null);
  42. if (!_entManager.TryGetComponent<MedibotComponent>(owner, out var medibot))
  43. return (false, null);
  44. var damageQuery = _entManager.GetEntityQuery<DamageableComponent>();
  45. var injectQuery = _entManager.GetEntityQuery<InjectableSolutionComponent>();
  46. var recentlyInjected = _entManager.GetEntityQuery<NPCRecentlyInjectedComponent>();
  47. var mobState = _entManager.GetEntityQuery<MobStateComponent>();
  48. var emaggedQuery = _entManager.GetEntityQuery<EmaggedComponent>();
  49. foreach (var entity in _lookup.GetEntitiesInRange(owner, range))
  50. {
  51. if (mobState.TryGetComponent(entity, out var state) &&
  52. injectQuery.HasComponent(entity) &&
  53. damageQuery.TryGetComponent(entity, out var damage) &&
  54. !recentlyInjected.HasComponent(entity))
  55. {
  56. // no treating dead bodies
  57. if (!_medibot.TryGetTreatment(medibot, state.CurrentState, out var treatment))
  58. continue;
  59. // Only go towards a target if the bot can actually help them or if the medibot is emagged
  60. // note: this and the actual injecting don't check for specific damage types so for example,
  61. // radiation damage will trigger injection but the tricordrazine won't heal it.
  62. if (!emaggedQuery.HasComponent(entity) && !treatment.IsValid(damage.TotalDamage))
  63. continue;
  64. //Needed to make sure it doesn't sometimes stop right outside it's interaction range
  65. var pathRange = SharedInteractionSystem.InteractionRange - 1f;
  66. var path = await _pathfinding.GetPath(owner, entity, pathRange, cancelToken);
  67. if (path.Result == PathResult.NoPath)
  68. continue;
  69. return (true, new Dictionary<string, object>()
  70. {
  71. {TargetKey, entity},
  72. {TargetMoveKey, _entManager.GetComponent<TransformComponent>(entity).Coordinates},
  73. {NPCBlackboard.PathfindKey, path},
  74. });
  75. }
  76. }
  77. return (false, null);
  78. }
  79. }