using System.Linq; using System.Threading; using System.Threading.Tasks; using Content.Server.Chemistry.EntitySystems; using Content.Server.Fluids.EntitySystems; using Content.Server.NPC.Pathfinding; using Content.Shared.Fluids.Components; using Robust.Shared.Map; namespace Content.Server.NPC.HTN.PrimitiveTasks.Operators.Fluid; /// /// Picks a nearby evaporatable puddle. /// public sealed class PickPuddleOperator : HTNOperator { // This is similar to PickAccessibleComponent however I have an idea on generic utility queries // that can also be re-used for melee that needs further fleshing out. [Dependency] private readonly IComponentFactory _factory = default!; [Dependency] private readonly IEntityManager _entManager = default!; private PathfindingSystem _pathfinding = default!; private EntityLookupSystem _lookup = default!; [DataField("rangeKey", required: true)] public string RangeKey = string.Empty; [DataField("target")] public string Target = "Target"; [DataField("targetKey", required: true)] public string TargetKey = string.Empty; /// /// Where the pathfinding result will be stored (if applicable). This gets removed after execution. /// [DataField("pathfindKey")] public string PathfindKey = NPCBlackboard.PathfindKey; public override void Initialize(IEntitySystemManager sysManager) { base.Initialize(sysManager); _lookup = sysManager.GetEntitySystem(); _pathfinding = sysManager.GetEntitySystem(); } /// [Obsolete("Obsolete")] public override async Task<(bool Valid, Dictionary? Effects)> Plan(NPCBlackboard blackboard, CancellationToken cancelToken) { var range = blackboard.GetValueOrDefault(RangeKey, _entManager); var owner = blackboard.GetValue(NPCBlackboard.Owner); if (!blackboard.TryGetValue(NPCBlackboard.OwnerCoordinates, out var coordinates, _entManager)) { return (false, null); } var targets = new List(); var puddleSystem = _entManager.System(); var solSystem = _entManager.System(); foreach (var comp in _lookup.GetComponentsInRange(coordinates, range)) { if (comp.Owner == owner || !solSystem.TryGetSolution(comp.Owner, comp.SolutionName, out var puddleSolution) || puddleSystem.CanFullyEvaporate(puddleSolution)) { continue; } targets.Add((comp.Owner)); } if (targets.Count == 0) { return (false, null); } foreach (var target in targets) { var path = await _pathfinding.GetPath( owner, target, 1f, cancelToken, flags: _pathfinding.GetFlags(blackboard)); if (path.Result != PathResult.Path) { return (false, null); } var xform = _entManager.GetComponent(target); return (true, new Dictionary() { { Target, target }, { TargetKey, xform.Coordinates }, { PathfindKey, path} }); } return (false, null); } }