Files
tbd-station-14/Content.Server/NPC/HTN/PrimitiveTasks/Operators/Specific/PickNearbyInjectableOperator.cs
Hannah Giovanna Dawson cdbe92d37d Update DamageableSystem to modern standards (#39417)
* Update DamageableSystem to modern standards

* DamageContainerId -> DamageContainerID with lint flag

* Replace strings with protoids

* Make CVar subscription declarations all consistently whitespaced

* ChangeDamage -> TryChangeDamage, cope with C# jank

* Revert event signature changes

* Restore a comment

* Re-add two queries

* Init the queries

* Use appearanceQuery in DamageChanged

* Use damageableQuery in TryChangeDamage

* Use damageableQuery in SetDamageModifierSetId

* Final cleanup, fix sandboxing

* Rectify ExplosionSystem:::ProcessEntity's call to TryChangeDamage

* Re-organize DamageableSystem

* first big fuck you breaking change.

* THATS A LOT OF DAMAGE!!!

* Fix test fails

* test fixes 2

* push it

---------

Co-authored-by: Princess Cheeseballs <66055347+Pronana@users.noreply.github.com>
2025-10-27 19:53:04 +00:00

96 lines
3.9 KiB
C#

using System.Threading;
using System.Threading.Tasks;
using Content.Shared.NPC.Components;
using Content.Server.NPC.Pathfinding;
using Content.Shared.Chemistry.Components.SolutionManager;
using Content.Shared.Damage.Components;
using Content.Shared.Interaction;
using Content.Shared.Mobs.Components;
using Content.Shared.Silicons.Bots;
using Content.Shared.Emag.Components;
namespace Content.Server.NPC.HTN.PrimitiveTasks.Operators.Specific;
public sealed partial class PickNearbyInjectableOperator : HTNOperator
{
[Dependency] private readonly IEntityManager _entManager = default!;
private EntityLookupSystem _lookup = default!;
private MedibotSystem _medibot = default!;
private PathfindingSystem _pathfinding = default!;
[DataField("rangeKey")] public string RangeKey = NPCBlackboard.MedibotInjectRange;
/// <summary>
/// Target entity to inject
/// </summary>
[DataField("targetKey", required: true)]
public string TargetKey = string.Empty;
/// <summary>
/// Target entitycoordinates to move to.
/// </summary>
[DataField("targetMoveKey", required: true)]
public string TargetMoveKey = string.Empty;
public override void Initialize(IEntitySystemManager sysManager)
{
base.Initialize(sysManager);
_lookup = sysManager.GetEntitySystem<EntityLookupSystem>();
_medibot = sysManager.GetEntitySystem<MedibotSystem>();
_pathfinding = sysManager.GetEntitySystem<PathfindingSystem>();
}
public override async Task<(bool Valid, Dictionary<string, object>? Effects)> Plan(NPCBlackboard blackboard,
CancellationToken cancelToken)
{
var owner = blackboard.GetValue<EntityUid>(NPCBlackboard.Owner);
if (!blackboard.TryGetValue<float>(RangeKey, out var range, _entManager))
return (false, null);
if (!_entManager.TryGetComponent<MedibotComponent>(owner, out var medibot))
return (false, null);
var damageQuery = _entManager.GetEntityQuery<DamageableComponent>();
var injectQuery = _entManager.GetEntityQuery<InjectableSolutionComponent>();
var recentlyInjected = _entManager.GetEntityQuery<NPCRecentlyInjectedComponent>();
var mobState = _entManager.GetEntityQuery<MobStateComponent>();
var emaggedQuery = _entManager.GetEntityQuery<EmaggedComponent>();
foreach (var entity in _lookup.GetEntitiesInRange(owner, range))
{
if (mobState.TryGetComponent(entity, out var state) &&
injectQuery.HasComponent(entity) &&
damageQuery.TryGetComponent(entity, out var damage) &&
!recentlyInjected.HasComponent(entity))
{
// no treating dead bodies
if (!_medibot.TryGetTreatment(medibot, state.CurrentState, out var treatment))
continue;
// Only go towards a target if the bot can actually help them or if the medibot is emagged
// note: this and the actual injecting don't check for specific damage types so for example,
// radiation damage will trigger injection but the tricordrazine won't heal it.
if (!emaggedQuery.HasComponent(entity) && !treatment.IsValid(damage.TotalDamage))
continue;
//Needed to make sure it doesn't sometimes stop right outside it's interaction range
var pathRange = SharedInteractionSystem.InteractionRange - 1f;
var path = await _pathfinding.GetPath(owner, entity, pathRange, cancelToken);
if (path.Result == PathResult.NoPath)
continue;
return (true, new Dictionary<string, object>()
{
{TargetKey, entity},
{TargetMoveKey, _entManager.GetComponent<TransformComponent>(entity).Coordinates},
{NPCBlackboard.PathfindKey, path},
});
}
}
return (false, null);
}
}