(C++)  1.0
A multi-platform, modular, universal engine for embedding advanced AI in games.
AIEntity.hh
1 #ifndef GRAIL_AI_ENTITY_H
2 #define GRAIL_AI_ENTITY_H
3 
4 #include "Behavior.hh"
5 #include "Blackboard.hh"
6 
7 #include <vector>
8 #include <string>
9 #include <random>
10 #include <memory>
11 #include <atomic>
12 #include <mutex>
13 #include <map>
14 
15 namespace grail
16 {
17  enum class Status
18  {
19  FREE = 0,
20  RUNNING,
21  DONE,
22  };
23 
24  class AIEntity;
25  class Reasoner;
27  {
28  public:
29  virtual ~IReasonerChangeObserver() = default;
30  virtual void OnReasonerSet(AIEntity& entity, Reasoner* newReasoner) = 0;
31  };
32 
33  class AIManager;
34 
38  class AIEntity
39  {
40  friend class AIManager;
41  friend class SynchronizedEntityContainer;
42 
43  public:
44  AIEntity(const std::string& name = "");
45  AIEntity(const AIEntity&) = delete;
46  AIEntity(AIEntity&&) = delete;
47 
48  virtual ~AIEntity();
49 
50  AIEntity& operator = (const AIEntity&) = delete;
51  AIEntity& operator = (AIEntity&&) = delete;
52 
57  void SetReasoner(std::unique_ptr<Reasoner> reasoner);
58 
63  const Behavior* GetCurrentBehavior() const;
64 
69  Behavior* GetCurrentBehavior();
70 
75  std::unique_ptr<Behavior> GetUniqueCurrentBehavior();
76 
81  Behavior* GetPreviousBehavior();
82 
87  std::unique_ptr<Reasoner>& GetCurrentReasoner();
88 
93  void SetNewBehavior(std::unique_ptr<Behavior> behavior);
94 
99  bool HasActiveBehavior() const;
100 
106  void AddSharedBlackboard(const std::string& name, std::shared_ptr<Blackboard>& sharedBlackboard);
107 
113  const std::shared_ptr<Blackboard> GetSharedBlackboard(const std::string& name) const;
114 
119  Blackboard& GetBlackboard();
120 
125  const Blackboard& GetBlackboard() const;
126 
127  //TODO: after UE4 plugin is ready, verify if these methods' arguments can be changed to shared_ptr
128  void AddReasonerChangeObserver(IReasonerChangeObserver* observer);
129  void RemoveReasonerChangeObserver(IReasonerChangeObserver* observer);
130  void SetId(size_t id);
131  size_t GetId() const;
132  const std::string& GetName() const;
133  std::string GetCurrentBehaviorName() const;
134 
135  protected:
140  virtual void Update(float deltaTime);
141 
142  std::unique_ptr<Reasoner> playingModel;
143  std::map<std::string, std::shared_ptr<Blackboard>> sharedBlackboards{};
144 
145  private:
146  void SelectBehavior();
147  void ExecuteBehavior(float deltaTime);
148  void FinishPreviousBehavior();
149  void SetupNextBehavior(std::unique_ptr<Behavior> behavior);
150 
151  void ChangeStatus(Status newStatus);
152  bool ChangeStatusNonBlocking(Status newStatus);
153 
154  Status GetStatus();
155  Status GetStatusNonBlocking();
156 
157  std::unique_ptr<Behavior> currentBehavior = nullptr;
158  Behavior* previousBehavior = nullptr;
159  Blackboard blackboard{};
160  std::mutex entityMutex{};
161  std::mutex statusMutex{};
162  mutable std::mutex behaviorMutex{};
163  std::vector<IReasonerChangeObserver*> reasonerChangeObservers{};
164 
165  Status status = Status::FREE;
166  size_t id;
167  std::string name;
168  };
169 }
170 #endif //GRAIL_AI_ENTITY_H
The Reasoner class - Entity&#39;s "brain", assigns them behaviors chosen by user-defined algorithms...
Definition: Reasoner.hh:20
The AIManager class - Manages registered entities and shared blackboards.
Definition: AIManager.hh:34
The Blackboard class - grail&#39;s universal data container.
Definition: Blackboard.hh:17
Definition: SynchronizedEntityContainer.h:12
A high-level abstraction of actions in the game.
Definition: Behavior.hh:19
Definition: AIEntity.cpp:10
Definition: AIEntity.hh:26
The AIEntity class - Defines a basic object which can execute behaviors.
Definition: AIEntity.hh:38