(C++)  1.0.0
A multi-platform, modular, universal engine for embedding advanced AI in games.
AIManager.hh
1 #ifndef GRAIL_AI_MANAGER_H
2 #define GRAIL_AI_MANAGER_H
3 
4 #include "AIEntity.hh"
5 #include "SynchronizedEntityContainer.h"
6 #include "EntityGroup.h"
7 
8 #include <set>
9 #include <queue>
10 #include <map>
11 #include <thread>
12 #include <condition_variable>
13 
14 
15 namespace grail
16 {
18  {
19  public:
20  IEntityChangeObserver() = default;
23  IEntityChangeObserver& operator=(const IEntityChangeObserver&) = delete;
24  IEntityChangeObserver& operator=(IEntityChangeObserver&&) = delete;
25  virtual ~IEntityChangeObserver() = default;
26 
27  virtual void OnEntityAdded(const AIManager& manager, AIEntity& entity) = 0;
28  virtual void OnEntityRemoved(const AIManager& manager, size_t entityId) = 0;
29  };
30 
34  class AIManager final
35  {
36  public:
41  AIManager(std::size_t threadsNumber = 0);
42  ~AIManager();
43 
47  void UpdateReasoners();
48 
53  void UpdateEntities(float deltaTime);
54 
60  void RegisterAIEntity(const std::shared_ptr<AIEntity>& entity, int priority);
61 
68  void RegisterAIEntity(const std::shared_ptr<AIEntity>& entity, const std::shared_ptr<AIEntity>& parent, int priorityOffset = 1);
69 
74  void RemoveAIEntity(const AIEntity* entity);
75 
76  void RegisterEntityGroup(const std::shared_ptr<EntityGroup>& entityGroup, int priority);
77  void RemoveEntityGroup(const std::shared_ptr<EntityGroup>& entityGroup);
78 
79  bool ContainsEntity(const AIEntity& entity) const;
80 
86  Blackboard& CreateBlackboard(const std::string& name);
87 
93  void SubscribeToBlackboard(const std::string& blackboardName, AIEntity& entity);
94 
95  void RegisterEntityChangeObserver(IEntityChangeObserver* observer);
96  void RemoveEntityChangeObserver(IEntityChangeObserver* observer);
97 
98  const std::vector<std::pair<std::shared_ptr<AIEntity>, int>>& GetEntities() const;
99  const std::vector<std::shared_ptr<EntityGroup>>& GetEntityGroups() const;
100  const std::map<std::string, std::shared_ptr<Blackboard>>& GetSharedBlackboards() const;
101 
102  private:
103  void RequestProcessingEntities();
104  void ThreadUpdate(std::size_t id);
105  void SortEntities();
106  void RemoveEntities();
107  void RegisterEntity(const std::shared_ptr<AIEntity>& entity, int priority);
108  std::shared_ptr<EntityGroup>& RegisterEntityGroup(const std::string& groupName,
109  int groupPriority,
110  const std::vector<std::pair<std::shared_ptr<AIEntity>, int>>& entityPriorityPairs,
111  const std::map<std::string, std::shared_ptr<Blackboard>>& nameBlackboardPairs);
112 
113  std::vector<std::pair<std::shared_ptr<AIEntity>, int>> entities{};
114  std::set<const AIEntity*> entitiesToRemove{};
115  std::map<std::string, std::shared_ptr<Blackboard>> sharedBlackboards{};
116 
117  SynchronizedEntityContainer processedEntities{};
118  std::atomic<bool> shouldTerminateWorkers = { 0 };
119 
120  std::vector<std::thread> threads{};
121  std::mutex blackboardMutex{};
122  std::mutex entityRemovalMutex{};
123 
124  size_t nextEntityId = 0;
125  size_t nextEntityGroupId = 0;
126  std::vector<std::shared_ptr<EntityGroup>> entityGroups{};
127  std::vector<IEntityChangeObserver*> entityChangeObservers{};
128  std::shared_ptr<EntityGroup> ungroupedEntities;
129  };
130 }
131 #endif //GRAIL_AI_MANAGER_H
The AIEntity class - Defines a basic object which can execute behaviors.
Definition: AIEntity.hh:39
The AIManager class - Manages registered entities and shared blackboards.
Definition: AIManager.hh:35
void RemoveAIEntity(const AIEntity *entity)
RemoveAIEntity - Manager caches given entity. Later on all cached entities will be unregistered from ...
Definition: AIManager.cpp:166
void UpdateReasoners()
UpdateReasoners - Lets registered entities think by updating their reasoner.
Definition: AIManager.cpp:75
void RegisterAIEntity(const std::shared_ptr< AIEntity > &entity, int priority)
RegisterAIEntity - Manager registers entity with given priority.
Definition: AIManager.cpp:110
Blackboard & CreateBlackboard(const std::string &name)
CreateBlackboard - Constructs blackboard owned by this manager.
Definition: AIManager.cpp:245
void SubscribeToBlackboard(const std::string &blackboardName, AIEntity &entity)
SubscribeToBlackboard - Subscribed given entity to blackboard identified by blackboard name and owned...
Definition: AIManager.cpp:253
AIManager(std::size_t threadsNumber=0)
AIManager - Constructs manager and spawns worker threads defined by parameter. If threadsNumber equal...
Definition: AIManager.cpp:9
void UpdateEntities(float deltaTime)
UpdateEntities - Updates entities and their assigned behaviors.
Definition: AIManager.cpp:96
The Blackboard class - grail's universal data container.
Definition: Blackboard.hh:21
Definition: AIManager.hh:18
Definition: SynchronizedEntityContainer.h:13