(C++)  1.0.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 
70 
75  std::unique_ptr<Behavior> GetUniqueCurrentBehavior();
76 
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 
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  std::atomic<bool> wasPreviousBehaviorCaptured{false};
160  Blackboard blackboard{};
161  std::mutex entityMutex{};
162  std::mutex statusMutex{};
163  mutable std::mutex behaviorMutex{};
164  std::vector<IReasonerChangeObserver*> reasonerChangeObservers{};
165 
166  Status status = Status::FREE;
167  size_t id;
168  std::string name;
169  };
170 }
171 #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:104
void SetReasoner(std::unique_ptr< Reasoner > reasoner)
SetReasoner.
Definition: AIEntity.cpp:21
Behavior * GetPreviousBehavior()
GetPreviousBehavior.
Definition: AIEntity.cpp:41
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:89
bool HasActiveBehavior() const
HasActiveBehavior - Checks whether current behavior is not nullptr.
Definition: AIEntity.cpp:84
std::unique_ptr< Reasoner > & GetCurrentReasoner()
GetCurrentReasoner.
Definition: AIEntity.cpp:47
std::unique_ptr< Behavior > GetUniqueCurrentBehavior()
GetUniqueCurrentBehavior.
Definition: AIEntity.cpp:36
const Behavior * GetCurrentBehavior() const
GetCurrentBehavior.
Definition: AIEntity.cpp:26
const std::shared_ptr< Blackboard > GetSharedBlackboard(const std::string &name) const
GetSharedBlackboard - Gets shared blackboard identified by given key.
Definition: AIEntity.cpp:94
Blackboard & GetBlackboard()
GetBlackboard.
Definition: AIEntity.cpp:207
void SetNewBehavior(std::unique_ptr< Behavior > behavior)
SetNewBehavior - Finishes current behavior and starts new one.
Definition: AIEntity.cpp:52
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