UseHealthPotionDecision.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System.Linq;
  2. namespace MazeGen
  3. {
  4. /// <summary>
  5. /// Decides to use a health potion if the agent's health is low.
  6. /// Only executes when outside of combat and health is below 50%.
  7. /// </summary>
  8. public class UseHealthPotionDecision : AgentDecision
  9. {
  10. private const float LowHealthThreshold = 0.5f;
  11. public override string DecisionName => "Use Health Potion";
  12. public override bool CanExecute(MazeAgent agent)
  13. {
  14. if (agent == null || agent.IsEngaged) return false; // Can't use potions while fighting
  15. CharacterSheet sheet = agent.CharacterSheet;
  16. float healthPercent = sheet.Stats.MaxHealth > 0 ? sheet.Stats.CurrentHealth / sheet.Stats.MaxHealth : 1f;
  17. if (healthPercent >= LowHealthThreshold) return false; // Health is fine
  18. // Check if we have a potion available (peek without consuming)
  19. return sheet.Inventory.Any(i => i is Consumable c && c.HealthBonus > 0);
  20. }
  21. public override bool Execute(MazeAgent agent)
  22. {
  23. CharacterSheet sheet = agent.CharacterSheet;
  24. Consumable potion = sheet.GetHealthPotionFromInventory();
  25. if (potion != null && potion.HealthBonus > 0)
  26. {
  27. sheet.Stats.Heal(potion.HealthBonus);
  28. return true;
  29. }
  30. return false;
  31. }
  32. public override void Think(MazeAgent agent)
  33. {
  34. CharacterSheet sheet = agent.CharacterSheet;
  35. // The potion was consumed in Execute, so we narrate the result
  36. agent.Thoughts.Add(ThoughtKind.Peek,
  37. $"Used health potion, recovered HP (now {sheet.Stats.CurrentHealth:0.#}/{sheet.Stats.MaxHealth:0.#})");
  38. }
  39. }
  40. }