GunAmmoPrecondition.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using Content.Server.Weapons.Ranged.Systems;
  2. using Content.Shared.Weapons.Ranged.Events;
  3. namespace Content.Server.NPC.HTN.Preconditions;
  4. /// <summary>
  5. /// Gets ammo for this NPC's selected gun; either active hand or itself.
  6. /// </summary>
  7. public sealed partial class GunAmmoPrecondition : HTNPrecondition
  8. {
  9. [Dependency] private readonly IEntityManager _entManager = default!;
  10. [DataField("minPercent")]
  11. public float MinPercent = 0f;
  12. [DataField("maxPercent")]
  13. public float MaxPercent = 1f;
  14. public override bool IsMet(NPCBlackboard blackboard)
  15. {
  16. var owner = blackboard.GetValue<EntityUid>(NPCBlackboard.Owner);
  17. var gunSystem = _entManager.System<GunSystem>();
  18. if (!gunSystem.TryGetGun(owner, out var gunUid, out _))
  19. {
  20. return false;
  21. }
  22. var ammoEv = new GetAmmoCountEvent();
  23. _entManager.EventBus.RaiseLocalEvent(gunUid, ref ammoEv);
  24. float percent;
  25. if (ammoEv.Capacity == 0)
  26. percent = 0f;
  27. else
  28. percent = ammoEv.Count / (float) ammoEv.Capacity;
  29. percent = System.Math.Clamp(percent, 0f, 1f);
  30. if (MaxPercent < percent)
  31. return false;
  32. if (MinPercent > percent)
  33. return false;
  34. return true;
  35. }
  36. }