Grail (C++)  1.1.1
A multi-platform, modular, universal engine for embedding advanced AI in games.
PlannerReasoner.hh
1 #ifndef GRAIL_PLANNER_REASONER_H
2 #define GRAIL_PLANNER_REASONER_H
3 
4 #include "../GoalSelector.hh"
5 #include "../../GrailCore/Behavior.hh"
6 #include "../../GrailCore/Plan.hh"
7 #include "../../GrailCore/Reasoner.hh"
8 #include "../../GrailData/DebugInfo/PlannerSnapshots.h"
9 
10 namespace grail
11 {
12  using PlannerCallback = std::function<void(const planning::Plan&, const planning::Goal&)>;
13 
14  namespace planning
15  {
16  class DomainTranslator;
17  }
18 
21  {
22  public:
23  struct Config
24  {
25  Config() noexcept
26  {
27  }
28 
29  std::size_t maxIterations = 1000;
30  int maxPlanLength = -1;
31  std::size_t iterationsPerFrame = 10;
32  double maxPlanCost = -1;
33  bool usePartialPlans = true;
34  };
35 
36  PlannerReasoner(std::unique_ptr<MemoryPool> memory);
37  PlannerReasoner(std::unique_ptr<MemoryPool> memory,
38  std::shared_ptr<planning::GoalSelector> goalSelector,
39  std::unique_ptr<planning::DomainTranslator> domainTranslator,
40  const Config& config);
41  virtual void SelectBehavior(AIEntity& entity) override;
42  virtual void SetNewGoal(std::unique_ptr<planning::Goal> newGoal, AIEntity& entity) override;
43 
47  void SetupNewPlanner(std::unique_ptr<planning::DomainTranslator> domainTranslator, const Config& config);
48 
50  void SetFallbackBehavior(std::unique_ptr<Behavior> behavior);
51 
52  virtual const planning::Goal* GetCurrentGoal() const override;
53 
54  //REVIEW: method names are pretty self explanatory, but some of them could be commented, e.g., what it means that a partial plan has been found or planning failed and explain exemplar handling of them
55  void BindPlanningSucceededCallback(PlannerCallback callback);
56  void BindPartialPlanFoundCallback(PlannerCallback callback);
57  void BindPlanningFailedCallback(std::function<void(const planning::Goal&)> callback);
58  void BindPlanExecutedCallback(std::function<void()> callback);
59 
60  //TODO: revise this approach
61  //REVIEW: resolve todo
62  void SetSnapshotProduction(bool shouldProduce);
63  bool IsComputing() const;
64 
65  PlannerReasonerSnapshot ProduceDebugSnapshot();
66  virtual std::unique_ptr<ISnapshotGenerator> CreateSnapshotGenerator(std::size_t) override;
67  void ClearCurrentDebugSnapshot();
68 
69  void SetDebugSnapshotFirstIteration(std::size_t iterationNumber);
70  void SetDebugSnapshotLastIteration(std::size_t iterationNumber);
71  std::size_t GetDebugSnapshotFirstIteration() const;
72  std::size_t GetDebugSnapshotLastIteration() const;
73  private:
74  void Reset(AIEntity& entity, const class planning::Goal& goal);
75  void DevisePlan(AIEntity& entity);
76 
77  planning::Planner planner;
78  std::shared_ptr<planning::GoalSelector> goalSelector;
79  std::unique_ptr<planning::DomainTranslator> domainTranslator;
80  std::unique_ptr<MemoryPool> memory;
81 
82  planning::Plan currentPlan;
83  std::size_t iterationsPerFrame = 0;
84  bool usePartialPlans = false;
85 
86  std::unique_ptr<planning::Goal> chosenGoal = nullptr;
87  std::unique_ptr<Behavior> fallbackBehavior = nullptr;
88 
89  PlannerCallback onPlanningSucceeded;
90  PlannerCallback onPartialPlanFound;
91  std::function<void(const planning::Goal&)> onPlanningFailed;
92  std::function<void()> onPlanExecuted;
93 
94  //TODO: revise this approach
95  //REVIEW: resolve todo
96  bool produceSnapshot = false;
97  PlannerReasonerSnapshot snapshot;
98 
99  std::size_t debugSnapshotFirstIteration = 0;
100  std::size_t debugSnapshotLastIteration = std::numeric_limits<std::size_t>::max();
101  };
102 }
103 #endif //GRAIL_PLANNER_REASONER_H
grail::planning::IGoalAcceptor
Definition: GoalSelector.hh:10
grail::AIEntity
The AIEntity class - Defines a basic object which can execute behaviors.
Definition: AIEntity.hh:39
grail::planning::Planner
The main class responsible for finding paths in plan space.
Definition: Planner.hh:27
grail::Reasoner
The Reasoner class - Entity's "brain", assigns them behaviors chosen by user-defined algorithms.
Definition: Reasoner.hh:19
grail::PlannerReasoner::SetFallbackBehavior
void SetFallbackBehavior(std::unique_ptr< Behavior > behavior)
Sets the behavior that should be performed during plan computation or if no valid plan is found.
Definition: PlannerReasoner.cpp:113
grail::PlannerReasoner::SetupNewPlanner
void SetupNewPlanner(std::unique_ptr< planning::DomainTranslator > domainTranslator, const Config &config)
Definition: PlannerReasoner.cpp:101
grail::PlannerReasonerSnapshot
Definition: PlannerSnapshots.h:69
grail::PlannerReasoner::SelectBehavior
virtual void SelectBehavior(AIEntity &entity) override
selectBehavior - Runs reasoner's selection algorithm and assigns chosen behavior to provided entity.
Definition: PlannerReasoner.cpp:30
grail::planning::Goal
Represents a planner goal, used by PlannerReasoner.
Definition: Goal.hh:14
grail::planning::Plan
A data structure used by PlannerReasoner to execute a sequence of behaviors.
Definition: Plan.hh:14
grail::PlannerReasoner
A reasoner which uses planners to find optimal sequences of behaviors based on goals.
Definition: PlannerReasoner.hh:20
grail::PlannerReasoner::Config
Definition: PlannerReasoner.hh:23