using System.Threading; using System.Threading.Tasks; using Content.Server.Chemistry.Components.SolutionManager; using Content.Server.NPC.Components; using Content.Server.NPC.Pathfinding; using Content.Shared.Damage; using Content.Shared.Interaction; using Content.Shared.MobState.Components; namespace Content.Server.NPC.HTN.PrimitiveTasks.Operators.Specific; public sealed class PickNearbyInjectableOperator : HTNOperator { [Dependency] private readonly IEntityManager _entManager = default!; private EntityLookupSystem _lookup = default!; private PathfindingSystem _pathfinding = default!; [DataField("rangeKey")] public string RangeKey = NPCBlackboard.MedibotInjectRange; /// /// Target entity to inject /// [DataField("targetKey", required: true)] public string TargetKey = string.Empty; /// /// Target entitycoordinates to move to. /// [DataField("targetMoveKey", required: true)] public string TargetMoveKey = string.Empty; public override void Initialize(IEntitySystemManager sysManager) { base.Initialize(sysManager); _lookup = sysManager.GetEntitySystem(); _pathfinding = sysManager.GetEntitySystem(); } public override async Task<(bool Valid, Dictionary? Effects)> Plan(NPCBlackboard blackboard, CancellationToken cancelToken) { var owner = blackboard.GetValue(NPCBlackboard.Owner); if (!blackboard.TryGetValue(RangeKey, out var range)) return (false, null); var damageQuery = _entManager.GetEntityQuery(); var injectQuery = _entManager.GetEntityQuery(); var recentlyInjected = _entManager.GetEntityQuery(); var mobState = _entManager.GetEntityQuery(); foreach (var entity in _lookup.GetEntitiesInRange(owner, range)) { if (mobState.HasComponent(entity) && injectQuery.HasComponent(entity) && damageQuery.TryGetComponent(entity, out var damage) && damage.TotalDamage > 0 && !recentlyInjected.HasComponent(entity)) { var path = await _pathfinding.GetPath(owner, entity, SharedInteractionSystem.InteractionRange, cancelToken); if (path.Result == PathResult.NoPath) continue; return (true, new Dictionary() { {TargetKey, entity}, {TargetMoveKey, _entManager.GetComponent(entity).Coordinates}, {NPCBlackboard.PathfindKey, path}, }); } } return (false, null); } }