(C++)  1.1.0
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 "../../GrailCore/Reasoner.hh"
5 #include "../GoalSelector.hh"
6 #include "../../GrailCore/Behavior.hh"
7 #include "../../GrailCore/Plan.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  }
20  {
21  public:
22  struct Config
23  {
24  Config() noexcept {}
25  std::size_t maxIterations = 1000;
26  int maxPlanLength = -1;
27  std::size_t iterationsPerFrame = 10;
28  double maxPlanCost = -1;
29  bool usePartialPlans = true;
30  };
31 
32  PlannerReasoner(MemoryPool& memory);
34  std::shared_ptr<planning::GoalSelector> goalSelector,
35  std::unique_ptr<planning::DomainTranslator> domainTranslator,
36  const Config& config);
37  void SelectBehavior(AIEntity& entity) override;
38  void SetNewGoal(std::unique_ptr<planning::Goal> newGoal, AIEntity& entity) override;
39 
43  void SetupNewPlanner(std::unique_ptr<planning::DomainTranslator> domainTranslator, const Config& config);
44 
46  void SetFallbackBehavior(std::unique_ptr<Behavior> behavior);
47 
48  const planning::Goal* GetCurrentGoal() const override;
49 
50  //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
51  void BindPlanningSucceededCallback(PlannerCallback callback);
52  void BindPartialPlanFoundCallback(PlannerCallback callback);
53  void BindPlanningFailedCallback(std::function<void(const planning::Goal&)> callback);
54  void BindPlanExecutedCallback(std::function<void()> callback);
55 
56  //TODO: revise this approach
57  //REVIEW: resolve todo
58  void SetSnapshotProduction(bool shouldProduce);
59  bool IsComputing() const;
60 
61  PlannerReasonerSnapshot ProduceDebugSnapshot();
62  std::unique_ptr<ISnapshotGenerator> CreateSnapshotGenerator(std::size_t) override;
63  void ClearCurrentDebugSnapshot();
64 
65  void SetDebugSnapshotFirstIteration(std::size_t iterationNumber);
66  void SetDebugSnapshotLastIteration(std::size_t iterationNumber);
67  std::size_t GetDebugSnapshotFirstIteration() const;
68  std::size_t GetDebugSnapshotLastIteration() const;
69  private:
70  void Reset(AIEntity& entity, const class planning::Goal& goal);
71 
72  planning::Planner planner;
73  std::shared_ptr<planning::GoalSelector> goalSelector;
74  std::unique_ptr<planning::DomainTranslator> domainTranslator;
75 
76  planning::Plan currentPlan;
77  std::size_t iterationsPerFrame = 0;
78  bool usePartialPlans = false;
79 
80  std::unique_ptr<planning::Goal> chosenGoal = nullptr;
81  std::unique_ptr<Behavior> fallbackBehavior = nullptr;
82 
83  PlannerCallback onPlanningSucceeded;
84  PlannerCallback onPartialPlanFound;
85  std::function<void(const planning::Goal&)> onPlanningFailed;
86  std::function<void()> onPlanExecuted;
87 
88  //TODO: revise this approach
89  //REVIEW: resolve todo
90  bool produceSnapshot = false;
91  PlannerReasonerSnapshot snapshot;
92 
93  std::size_t debugSnapshotFirstIteration = 0;
94  std::size_t debugSnapshotLastIteration = std::numeric_limits<std::size_t>::max();
95  };
96 }
97 #endif //GRAIL_PLANNER_REASONER_H
The AIEntity class - Defines a basic object which can execute behaviors.
Definition: AIEntity.hh:39
The MemoryPool class - preallocated memory container for optimization issues.
Definition: MemoryPool.hh:74
A reasoner which uses planners to find optimal sequences of behaviors based on goals.
Definition: PlannerReasoner.hh:20
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:120
void SetupNewPlanner(std::unique_ptr< planning::DomainTranslator > domainTranslator, const Config &config)
Definition: PlannerReasoner.cpp:109
void SelectBehavior(AIEntity &entity) override
selectBehavior - Runs reasoner's selection algorithm and assigns chosen behavior to provided entity.
Definition: PlannerReasoner.cpp:31
The Reasoner class - Entity's "brain", assigns them behaviors chosen by user-defined algorithms.
Definition: Reasoner.hh:20
Represents a planner goal, used by PlannerReasoner.
Definition: Goal.hh:15
Definition: GoalSelector.hh:12
A data structure used by PlannerReasoner to execute a sequence of behaviors.
Definition: Plan.hh:16
The main class responsible for finding paths in plan space.
Definition: Planner.hh:23
Definition: PlannerReasoner.hh:23
Definition: PlannerSnapshots.h:70