(C++)  1.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> newReasoner);
58 
63  const Behavior* GetCurrentBehavior() const;
64 
70 
75  std::unique_ptr<Behavior> GetUniqueCurrentBehavior();
76 
81  std::unique_ptr<Behavior> MoveSuspendedBehavior();
82 
87  std::unique_ptr<Reasoner>& GetCurrentReasoner();
88 
93  void StageBehavior(std::unique_ptr<Behavior> behavior);
94 
99  bool HasStagedBehavior() const;
100 
105  bool HasActiveBehavior() const;
106 
112  void AddSharedBlackboard(const std::string& name, std::shared_ptr<Blackboard>& sharedBlackboard);
113 
119  std::shared_ptr<Blackboard> GetSharedBlackboard(const std::string& name) const;
120 
126 
131  const Blackboard& GetBlackboard() const;
132 
133  //TODO: after UE4 plugin is ready, verify if these methods' arguments can be changed to shared_ptr
134  //REVIEW: resolve todo
135  void AddReasonerChangeObserver(IReasonerChangeObserver* observer);
136  void RemoveReasonerChangeObserver(IReasonerChangeObserver* observer);
137  void SetId(size_t id);
138  size_t GetId() const;
139  const std::string& GetName() const;
140  std::string GetCurrentBehaviorName() const;
141 
142  protected:
147  virtual void Update(float deltaTime);
148 
149  std::unique_ptr<Reasoner> reasoner;
150  std::map<std::string, std::shared_ptr<Blackboard>> sharedBlackboards{};
151 
152  private:
153  void SelectBehavior();
154  void ExecuteBehavior(float deltaTime);
155  void SuspendPreviousBehavior();
156  void SetupNextBehavior(std::unique_ptr<Behavior> behavior);
157 
158  void ChangeStatus(Status newStatus);
159  bool ChangeStatusNonBlocking(Status newStatus);
160 
161  Status GetStatus();
162  Status GetStatusNonBlocking();
163 
164  std::unique_ptr<Behavior> stagedBehavior = nullptr;
165  std::unique_ptr<Behavior> currentBehavior = nullptr;
166  std::unique_ptr<Behavior> suspendedBehavior = nullptr;
167  Blackboard blackboard{};
168  std::mutex entityMutex{};
169  std::mutex statusMutex{};
170  mutable std::mutex behaviorMutex{};
171  mutable std::mutex stagingMutex{};
172  std::vector<IReasonerChangeObserver*> reasonerChangeObservers{};
173 
174  Status status = Status::FREE;
175  size_t id;
176  std::string name;
177  };
178 }
179 #endif //GRAIL_AI_ENTITY_H
The AIEntity class - Defines a basic object which can execute behaviors.
Definition: AIEntity.hh:39
virtual void Update(float deltaTime)
Update - User defined method. Should in some way update entity's state.
Definition: AIEntity.cpp:109
std::shared_ptr< Blackboard > GetSharedBlackboard(const std::string &name) const
GetSharedBlackboard - Gets shared blackboard identified by given key.
Definition: AIEntity.cpp:99
void AddSharedBlackboard(const std::string &name, std::shared_ptr< Blackboard > &sharedBlackboard)
AddSharedBlackboard - Inserts pair of key and blackboard into container of shared blackboards.
Definition: AIEntity.cpp:94
bool HasActiveBehavior() const
HasActiveBehavior - Checks whether current behavior is not nullptr.
Definition: AIEntity.cpp:89
bool HasStagedBehavior() const
HasStagedBehavior - checks whether this entity has behavior awaiting its execution.
Definition: AIEntity.cpp:83
std::unique_ptr< Reasoner > & GetCurrentReasoner()
GetCurrentReasoner.
Definition: AIEntity.cpp:47
std::unique_ptr< Behavior > GetUniqueCurrentBehavior()
GetUniqueCurrentBehavior.
Definition: AIEntity.cpp:37
void StageBehavior(std::unique_ptr< Behavior > behavior)
SetNewBehavior - Finishes current behavior and starts new one.
Definition: AIEntity.cpp:52
std::unique_ptr< Behavior > MoveSuspendedBehavior()
GetPreviousBehavior.
Definition: AIEntity.cpp:42
void SetReasoner(std::unique_ptr< Reasoner > newReasoner)
SetReasoner.
Definition: AIEntity.cpp:22
const Behavior * GetCurrentBehavior() const
GetCurrentBehavior.
Definition: AIEntity.cpp:27
Blackboard & GetBlackboard()
GetBlackboard.
Definition: AIEntity.cpp:220
The AIManager class - Manages registered entities and shared blackboards.
Definition: AIManager.hh:35
A high-level abstraction of actions in the game.
Definition: Behavior.hh:20
The Blackboard class - grail's universal data container.
Definition: Blackboard.hh:21
Definition: AIEntity.hh:27
The Reasoner class - Entity's "brain", assigns them behaviors chosen by user-defined algorithms.
Definition: Reasoner.hh:20
Definition: SynchronizedEntityContainer.h:13