(C++)  1.0.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 "../GrailSystem/Reasoner.hh"
5 #include "../GrailSystem/Behavior.hh"
6 #include "../GrailSystem/Plan.hh"
7 #include "../GrailData/GrailDebugInfo/PlannerSnapshots.h"
8 #include "GoalSelector.hh"
9 
10 #include <functional>
11 
12 namespace grail
13 {
14  using PlannerCallback = std::function<void(const planning::Plan&, const planning::Goal&)>;
15 
16  namespace planning
17  {
18  class DomainTranslator;
19  }
22  {
23  public:
24  struct Config
25  {
26  Config() noexcept {}
27  std::size_t maxIterations = 1000;
28  int maxPlanLength = -1;
29  std::size_t iterationsPerFrame = 10;
30  double maxPlanCost = -1;
31  bool usePartialPlans = true;
32  };
33 
34  PlannerReasoner(MemoryPool& memory);
36  std::shared_ptr<planning::GoalSelector> goalSelector,
37  std::unique_ptr<planning::DomainTranslator> actionTranslator,
38  const Config& config);
39  void SelectBehavior(AIEntity& entity) override;
40  void SetNewGoal(std::unique_ptr<planning::Goal> newGoal, AIEntity& entity) override;
41 
45  void SetupNewPlanner(std::unique_ptr<planning::DomainTranslator> domainTranslator, const Config& config);
46 
48  void SetFallbackBehavior(std::unique_ptr<Behavior> behavior);
49  const planning::Goal* GetCurrentGoal() const override;
50 
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  void SetSnapshotProduction(bool shouldProduce);
58  bool IsComputing() const;
59 
60  PlannerReasonerSnapshot ProduceDebugSnapshot();
61  std::unique_ptr<ISnapshotGenerator> CreateSnapshotGenerator(std::size_t) override;
62  void ClearCurrentDebugSnapshot();
63 
64  void SetDebugSnapshotFirstIteration(std::size_t iterationNumber);
65  void SetDebugSnapshotLastIteration(std::size_t iterationNumber);
66  std::size_t GetDebugSnapshotFirstIteration() const;
67  std::size_t GetDebugSnapshotLastIteration() const;
68  private:
69  void Reset(AIEntity& entity, const class planning::Goal& goal);
70 
71  planning::Planner planner;
72  std::shared_ptr<planning::GoalSelector> goalSelector;
73  std::unique_ptr<planning::DomainTranslator> domainTranslator;
74 
75  planning::Plan currentPlan;
76  std::size_t iterationsPerFrame = 0;
77  bool usePartialPlans = false;
78 
79  std::unique_ptr<planning::Goal> chosenGoal = nullptr;
80  std::unique_ptr<Behavior> fallbackBehavior = nullptr;
81 
82  PlannerCallback onPlanningSucceeded;
83  PlannerCallback onPartialPlanFound;
84  std::function<void(const planning::Goal&)> onPlanningFailed;
85  std::function<void()> onPlanExecuted;
86 
87  //TODO: revise this approach
88  bool produceSnapshot = false;
89  PlannerReasonerSnapshot snapshot;
90 
91  std::size_t debugSnapshotFirstIteration = 0;
92  std::size_t debugSnapshotLastIteration = std::numeric_limits<std::size_t>::max();
93  };
94 }
95 #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:22
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:119
void SetupNewPlanner(std::unique_ptr< planning::DomainTranslator > domainTranslator, const Config &config)
Definition: PlannerReasoner.cpp:108
void SelectBehavior(AIEntity &entity) override
selectBehavior - Runs reasoner's selection algorithm and assigns chosen behavior to provided entity.
Definition: PlannerReasoner.cpp:32
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:25
Definition: PlannerSnapshots.h:64