]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
Implement Health Consideration for NPCs (#29922)
authorosjarw <62134478+osjarw@users.noreply.github.com>
Fri, 12 Jul 2024 11:37:47 +0000 (14:37 +0300)
committerGitHub <noreply@github.com>
Fri, 12 Jul 2024 11:37:47 +0000 (21:37 +1000)
Implement TargetHealthCon

Content.Server/NPC/Queries/Considerations/TargetHealthCon.cs
Content.Server/NPC/Systems/NPCUtilitySystem.cs

index d4e8a277ea69575b27020b7875ee9b1b32c0541e..cc9c6df83f37a299e3ca85d102eda072cc488a1b 100644 (file)
@@ -1,6 +1,16 @@
+using Content.Shared.Mobs;
+
 namespace Content.Server.NPC.Queries.Considerations;
 
+/// <summary>
+/// Goes linearly from 1f to 0f, with 0 damage returning 1f and <see cref=TargetState> damage returning 0f
+/// </summary>
 public sealed partial class TargetHealthCon : UtilityConsideration
 {
 
+    /// <summary>
+    /// Which MobState the consideration returns 0f at, defaults to choosing earliest incapacitating MobState
+    /// </summary>
+    [DataField("targetState")]
+    public MobState TargetState = MobState.Invalid;
 }
index ca74d7133574245587b05d465176a8761f20ac06..72833fc35ef9cc8be08b007d965ea2119d9aa9ca 100644 (file)
@@ -7,10 +7,13 @@ using Content.Server.NPC.Queries.Queries;
 using Content.Server.Nutrition.Components;
 using Content.Server.Nutrition.EntitySystems;
 using Content.Server.Storage.Components;
+using Content.Shared.Damage;
 using Content.Shared.Examine;
 using Content.Shared.Fluids.Components;
 using Content.Shared.Hands.Components;
 using Content.Shared.Inventory;
+using Content.Shared.Mobs;
+using Content.Shared.Mobs.Components;
 using Content.Shared.Mobs.Systems;
 using Content.Shared.NPC.Systems;
 using Content.Shared.Nutrition.Components;
@@ -48,6 +51,7 @@ public sealed class NPCUtilitySystem : EntitySystem
     [Dependency] private readonly WeldableSystem _weldable = default!;
     [Dependency] private readonly ExamineSystemShared _examine = default!;
     [Dependency] private readonly EntityWhitelistSystem _whitelistSystem = default!;
+    [Dependency] private readonly MobThresholdSystem _thresholdSystem = default!;
 
     private EntityQuery<PuddleComponent> _puddleQuery;
     private EntityQuery<TransformComponent> _xformQuery;
@@ -293,8 +297,14 @@ public sealed class NPCUtilitySystem : EntitySystem
 
                 return (float) ev.Count / ev.Capacity;
             }
-            case TargetHealthCon:
+            case TargetHealthCon con:
             {
+                if (!TryComp(targetUid, out DamageableComponent? damage))
+                    return 0f;
+                if (con.TargetState != MobState.Invalid && _thresholdSystem.TryGetPercentageForState(targetUid, con.TargetState, damage.TotalDamage, out var percentage))
+                    return Math.Clamp((float)(1 - percentage), 0f, 1f);
+                if (_thresholdSystem.TryGetIncapPercentage(targetUid, damage.TotalDamage, out var incapPercentage))
+                    return Math.Clamp((float)(1 - incapPercentage), 0f, 1f);
                 return 0f;
             }
             case TargetInLOSCon: