Grail (C++)  1.4.0
A multi-platform, modular, universal engine for embedding advanced AI in games.
AIManager.hh
1 // Copyright QED Software 2023.
2 
3 #ifndef GRAIL_AI_MANAGER_H
4 #define GRAIL_AI_MANAGER_H
5 
6 #include "AIEntity.hh"
7 #include "EntityGroup.h"
8 #include "SynchronizedEntityContainer.h"
9 #include <map>
10 #include <set>
11 #include <thread>
12 
13 namespace grail
14 {
16  {
17  public:
18  IEntityChangeObserver() = default;
21  IEntityChangeObserver& operator=(const IEntityChangeObserver&) = delete;
22  IEntityChangeObserver& operator=(IEntityChangeObserver&&) = delete;
23  virtual ~IEntityChangeObserver() = default;
24 
25  virtual void OnEntityAdded(const AIManager& manager, AIEntity& entity) = 0;
26  virtual void OnEntityRemoved(const AIManager& manager, size_t entityId) = 0;
27  };
28 
32  class AIManager final
33  {
34  public:
40  AIManager(std::size_t threadsNumber = 0);
41  ~AIManager();
42 
46  void UpdateReasoners();
47 
52  void UpdateEntities(float deltaTime);
53 
62  void RegisterAIEntity(const std::shared_ptr<AIEntity>& entity, int priority);
63 
70  void RemoveAIEntity(AIEntity* entity);
71 
81  void RegisterEntityGroup(const std::shared_ptr<EntityGroup>& entityGroup, int priority);
82 
89  void RemoveEntityGroup(const std::shared_ptr<EntityGroup>& entityGroup);
90 
96  bool ContainsEntity(const AIEntity& entity) const;
97 
103  Blackboard& CreateBlackboard(const std::string& name);
104 
110  void SubscribeToBlackboard(const std::string& blackboardName, AIEntity& entity);
111 
117 
123 
124  const std::vector<std::pair<std::shared_ptr<AIEntity>, int>>& GetEntities() const;
125  const std::vector<std::shared_ptr<EntityGroup>>& GetEntityGroups() const;
126  const std::map<std::string, std::shared_ptr<Blackboard>>& GetSharedBlackboards() const;
127 
128  private:
129  EntityToken WaitForAcquiringEntity(AIEntity& entity) const;
130  void RequestProcessingEntities();
131  void ThreadUpdate(std::size_t id);
132  void SortEntities();
133  void RemoveEntities();
134  void RegisterEntity(const std::shared_ptr<AIEntity>& entity, int priority);
135 
136  std::vector<std::pair<std::shared_ptr<AIEntity>, int>> entities{};
137  std::map<std::string, std::shared_ptr<Blackboard>> sharedBlackboards{};
138 
139  SynchronizedEntityContainer processedEntities{};
140  std::atomic<bool> shouldTerminateWorkers = {0};
141 
142  std::vector<std::thread> threads{};
143  std::mutex blackboardMutex{};
144  mutable std::mutex entitiesMutex{};
145 
146  std::vector<std::shared_ptr<EntityGroup>> entityGroups{};
147  std::vector<IEntityChangeObserver*> entityChangeObservers{};
148 
149  std::thread::id mainThreadId{};
150  };
151 }
152 #endif //GRAIL_AI_MANAGER_H
grail::AIManager::RegisterAIEntity
void RegisterAIEntity(const std::shared_ptr< AIEntity > &entity, int priority)
RegisterAIEntity - Manager registers an entity with a given priority. As a result entities and their ...
Definition: AIManager.cpp:141
grail::AIManager::RegisterEntityGroup
void RegisterEntityGroup(const std::shared_ptr< EntityGroup > &entityGroup, int priority)
RegisterEntityGroup - Manager registers an entity group (and its entities) with the given priority....
Definition: AIManager.cpp:239
grail::AIManager::RemoveAIEntity
void RemoveAIEntity(AIEntity *entity)
RemoveAIEntity - Manager caches given entity. Later on all cached entities will be unregistered from ...
Definition: AIManager.cpp:206
grail::AIEntity
The AIEntity class - Defines a basic object which can execute behaviors.
Definition: AIEntity.hh:50
grail::AIManager
The AIManager class - Manages registered entities and shared blackboards.
Definition: AIManager.hh:32
grail::AIManager::AIManager
AIManager(std::size_t threadsNumber=0)
AIManager - Constructs a manager and spawns worker threads defined by the parameter....
Definition: AIManager.cpp:12
grail::AIManager::UpdateEntities
void UpdateEntities(float deltaTime)
UpdateEntities - Updates entities and their assigned behaviors.
Definition: AIManager.cpp:124
grail::AIManager::CreateBlackboard
Blackboard & CreateBlackboard(const std::string &name)
CreateBlackboard - Constructs blackboard owned by this manager.
Definition: AIManager.cpp:308
grail::AIManager::ContainsEntity
bool ContainsEntity(const AIEntity &entity) const
ContainsEntity - Checks if a given entity is registered in the manager.
Definition: AIManager.cpp:271
grail::Blackboard
The Blackboard class - grail's universal data container.
Definition: Blackboard.hh:15
grail::AIManager::RemoveEntityChangeObserver
void RemoveEntityChangeObserver(IEntityChangeObserver *observer)
RemoveEntityChangeObserver - Removes an entity change observer so it no longer will be notified on ad...
Definition: AIManager.cpp:348
grail::AIManager::RegisterEntityChangeObserver
void RegisterEntityChangeObserver(IEntityChangeObserver *observer)
RegisterEntityChangeObserver - Registers an entity change observer that will be notified via OnEntity...
Definition: AIManager.cpp:343
grail::EntityToken
Definition: EntityToken.hh:8
grail::IEntityChangeObserver
Definition: AIManager.hh:15
grail::AIManager::SubscribeToBlackboard
void SubscribeToBlackboard(const std::string &blackboardName, AIEntity &entity)
SubscribeToBlackboard - Subscribes a given entity to a blackboard identified by the provided blackboa...
Definition: AIManager.cpp:316
grail::AIManager::UpdateReasoners
void UpdateReasoners()
UpdateReasoners - Lets registered entities think by updating their reasoner.
Definition: AIManager.cpp:88
grail::AIManager::RemoveEntityGroup
void RemoveEntityGroup(const std::shared_ptr< EntityGroup > &entityGroup)
RemoveEntityGroup - Manager removes entity group and caches its entities for removal....
Definition: AIManager.cpp:262
grail::SynchronizedEntityContainer
Definition: SynchronizedEntityContainer.h:15