AgentDecisionTree.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace MazeGen
  5. {
  6. /// <summary>
  7. /// Manages a prioritized list of agent decisions. On each call, iterates through decisions
  8. /// in order, finds the first one that CanExecute(), and calls both Execute() and Think()
  9. /// in lockstep so behavior and narration stay synchronized.
  10. ///
  11. /// Usage pattern:
  12. /// var tree = new AgentDecisionTree();
  13. /// tree.Add(new HandleExistingFightDecision(director));
  14. /// tree.Add(new DeclareFightDecision(director));
  15. /// tree.Add(new SneakAwayDecision(director));
  16. /// tree.Add(new MoveOnDecision(generator));
  17. ///
  18. /// // In your update loop:
  19. /// if (!tree.TryDecide(agent)) {
  20. /// // No decision could execute (shouldn't happen if the last decision is a catch-all)
  21. /// }
  22. /// </summary>
  23. public class AgentDecisionTree
  24. {
  25. private List<AgentDecision> _decisions = new List<AgentDecision>();
  26. public void Add(AgentDecision decision)
  27. {
  28. if (decision == null)
  29. throw new ArgumentNullException(nameof(decision));
  30. _decisions.Add(decision);
  31. }
  32. public void Insert(int index, AgentDecision decision)
  33. {
  34. if (decision == null)
  35. throw new ArgumentNullException(nameof(decision));
  36. _decisions.Insert(index, decision);
  37. }
  38. public void Remove(AgentDecision decision) => _decisions.Remove(decision);
  39. public void RemoveAt(int index) => _decisions.RemoveAt(index);
  40. public int Count => _decisions.Count;
  41. /// <summary>
  42. /// Evaluate the decision tree: find the first decision that can execute, then Execute it.
  43. /// If Execute returns true, call Think to narrate it.
  44. /// Returns true if a decision was executed, false if none could execute.
  45. /// </summary>
  46. public bool TryDecide(MazeAgent agent)
  47. {
  48. if (agent == null) return false;
  49. foreach (var decision in _decisions)
  50. {
  51. if (!decision.CanExecute(agent))
  52. continue;
  53. // This decision can execute - do it
  54. bool consumed = decision.Execute(agent);
  55. if (consumed)
  56. {
  57. // Narrate the decision
  58. decision.Think(agent);
  59. return true;
  60. }
  61. }
  62. // No decision could execute
  63. return false;
  64. }
  65. /// <summary>
  66. /// Evaluate just the preconditions without executing or thinking.
  67. /// Useful for debugging or logging what decisions are available.
  68. /// </summary>
  69. public AgentDecision FindApplicableDecision(MazeAgent agent)
  70. {
  71. foreach (var decision in _decisions)
  72. {
  73. if (decision.CanExecute(agent))
  74. return decision;
  75. }
  76. return null;
  77. }
  78. /// <summary>
  79. /// Get the list of all decisions that could currently execute.
  80. /// </summary>
  81. public List<AgentDecision> GetAvailableDecisions(MazeAgent agent)
  82. {
  83. var available = new List<AgentDecision>();
  84. foreach (var decision in _decisions)
  85. {
  86. if (decision.CanExecute(agent))
  87. available.Add(decision);
  88. }
  89. return available;
  90. }
  91. }
  92. }