Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
AIManager.cpp
Go to the documentation of this file.
1
2
3#include "config.h"
4
5#include "Headers/AIManager.h"
6
9
10namespace Divide {
11
12using namespace AI;
13
15 : SceneComponent(parentScene),
16 _parentPool(pool),
17 _deltaTimeUS(0ULL),
18 _currentTimeUS(0ULL),
19 _previousTimeUS(0ULL),
20 _navMeshDebugDraw(false),
21 _pauseUpdate(true),
22 _updating(false)
23{
24 _shouldStop = false;
25 _running = false;
26}
27
29{
30 destroy();
31}
32
35{
36 {
38 _aiTeams.clear();
39 }
40 {
42 _navMeshes.clear();
43 }
44}
45
46void AIManager::update(const U64 deltaTimeUS) {
48
49 _currentTimeUS += deltaTimeUS;
50 if (_currentTimeUS >= _previousTimeUS + updateFreqUS && !shouldStop()) {
51 _running = true;
54 {
58 if (!_aiTeams.empty() && !_pauseUpdate) {
59 _updating = true;
60 if (_sceneCallback) {
62 }
63
64 if (processInput(deltaTimeUS)) { // sensors
65 if (processData(deltaTimeUS)) { // think
66 updateEntities(deltaTimeUS); // react
67 }
68 }
69
70 _updating = false;
71 }
72 }
74 _running = false;
75 }
76}
77
78bool AIManager::processInput(const U64 deltaTimeUS) { // sensors
79 for (AITeamMap::value_type& team : _aiTeams) {
80 if (!team.second->processInput(_parentPool, deltaTimeUS)) {
81 return false;
82 }
83 }
84 return true;
85}
86
87bool AIManager::processData(const U64 deltaTimeUS) { // think
88 for (AITeamMap::value_type& team : _aiTeams) {
89 if (!team.second->processData(_parentPool, deltaTimeUS)) {
90 return false;
91 }
92 }
93 return true;
94}
95
96bool AIManager::updateEntities(const U64 deltaTimeUS) { // react
97 for (AITeamMap::value_type& team : _aiTeams) {
98 if (!team.second->update(_parentPool, deltaTimeUS)) {
99 return false;
100 }
101 }
102 return true;
103}
104
105bool AIManager::registerEntity(const U32 teamID, AIEntity* entity) {
107 const AITeamMap::const_iterator it = _aiTeams.find(teamID);
108 DIVIDE_ASSERT(it != std::end(_aiTeams),
109 "AIManager error: attempt to register an AI Entity to a "
110 "non-existent team!");
111 it->second->addTeamMember(entity);
112 return true;
113}
114
116 for (AITeamMap::value_type& team : _aiTeams) {
117 unregisterEntity(team.second->teamID(), entity);
118 }
119}
120
121void AIManager::unregisterEntity(const U32 teamID, AIEntity* entity) {
123 const AITeamMap::const_iterator it = _aiTeams.find(teamID);
124 DIVIDE_ASSERT(it != std::end(_aiTeams),
125 "AIManager error: attempt to remove an AI Entity from a "
126 "non-existent team!");
127 it->second->removeTeamMember(entity);
128}
129
131{
132 _navMeshMutex.lock();
133 std::unique_ptr<Navigation::NavigationMesh>& navMesh = _navMeshes[radius];
134 if (navMesh == nullptr)
135 {
136 navMesh = std::make_unique<Navigation::NavigationMesh>(context, recastInterface, parentScene);
137 navMesh->debugDraw(_navMeshDebugDraw);
138 }
139 _navMeshMutex.unlock();
140
141 {
143 for (AITeamMap::value_type& team : _aiTeams)
144 {
145 team.second->addCrowd(radius, navMesh.get());
146 }
147 }
148
149 return navMesh.get();
150}
151
153{
154 {
156 const NavMeshMap::iterator it = _navMeshes.find(radius);
157 if ( it == std::end(_navMeshes))
158 {
159 return false;
160 }
161
162 _navMeshes.erase( it );
163 }
164
165 {
167 for (AITeamMap::value_type& team : _aiTeams)
168 {
169 team.second->removeCrowd(radius);
170 }
171 }
172
173 return true;
174}
175
177{
179 auto& team = _aiTeams[id];
180 auto it = _aiTeams.find(id);
181 if ( team == nullptr )
182 {
183 team.reset( new AITeam( id, *this ) );
184 }
185
186 return team.get();
187}
188
190{
192
193 const AITeamMap::iterator it = _aiTeams.find( id );
194 if (it != std::end(_aiTeams))
195 {
196 _aiTeams.erase( it );
197 return true;
198 }
199
200 return false;
201}
202
203void AIManager::toggleNavMeshDebugDraw(const bool state) {
204 _navMeshDebugDraw = state;
205
207 for (NavMeshMap::value_type& it : _navMeshes) {
208 it.second->debugDraw(state);
209 }
210}
211
213 GFX::MemoryBarrierCommand& memCmdInOut,
214 const bool forceAll)
215{
216 if (forceAll || _navMeshDebugDraw) {
218 for (NavMeshMap::value_type& it : _navMeshes) {
219 if (forceAll || it.second->debugDraw()) {
220 it.second->draw(forceAll, bufferInOut, memCmdInOut);
221 }
222 }
223 }
224}
225
226bool AIManager::shouldStop() const noexcept {
227 return _shouldStop;
228}
229}; // namespace Divide
#define DIVIDE_ASSERT(...)
Based on OgreCrowd.
Definition: AIEntity.h:60
std::atomic_bool _navMeshDebugDraw
Definition: AIManager.h:147
std::atomic_bool _shouldStop
Definition: AIManager.h:150
void toggleNavMeshDebugDraw(bool state)
Toggle debug draw for all NavMeshes.
Definition: AIManager.cpp:203
std::atomic_bool _running
Definition: AIManager.h:151
AIManager(Scene &parentScene, TaskPool &pool)
Definition: AIManager.cpp:14
friend class AITeam
Definition: AIManager.h:135
NavMeshMap _navMeshes
Definition: AIManager.h:152
bool shouldStop() const noexcept
Definition: AIManager.cpp:226
bool processInput(U64 deltaTimeUS)
sensors
Definition: AIManager.cpp:78
TaskPool & _parentPool
Definition: AIManager.h:145
bool updateEntities(U64 deltaTimeUS)
react
Definition: AIManager.cpp:96
std::atomic_bool _pauseUpdate
Definition: AIManager.h:148
bool destroyNavMesh(AIEntity::PresetAgentRadius radius)
Definition: AIManager.cpp:152
void update(U64 deltaTimeUS)
Called at a fixed interval (preferably in a separate thread);.
Definition: AIManager.cpp:46
bool registerEntity(U32 teamID, AIEntity *entity)
Definition: AIManager.cpp:105
Navigation::NavigationMesh * addNavMesh(PlatformContext &context, Navigation::DivideRecast &recastInterface, Scene &parentScene, AIEntity::PresetAgentRadius radius)
Add a NavMesh.
Definition: AIManager.cpp:130
void debugDraw(GFX::CommandBuffer &bufferInOut, GFX::MemoryBarrierCommand &memCmdInOut, bool forceAll=true)
Definition: AIManager.cpp:212
AITeam * registerTeam(U32 id)
Register an AI Team.
Definition: AIManager.cpp:176
DELEGATE< void > _sceneCallback
Definition: AIManager.h:156
void destroy()
Clear all AI related data (teams, entities, NavMeshes, etc);.
Definition: AIManager.cpp:34
void unregisterEntity(U32 teamID, AIEntity *entity)
Definition: AIManager.cpp:121
SharedMutex _navMeshMutex
Definition: AIManager.h:155
bool processData(U64 deltaTimeUS)
think
Definition: AIManager.cpp:87
bool unregisterTeam(U32 id)
Unregister an AI Team.
Definition: AIManager.cpp:189
std::atomic_bool _updating
Definition: AIManager.h:149
Scene & parentScene() noexcept
constexpr U16 TARGET_FRAME_RATE
Application desired framerate for physics and input simulations.
Definition: config.h:97
constexpr U8 TICK_DIVISOR
Definition: config.h:104
constexpr T SecondsToMicroseconds(U a) noexcept
Definition: MathHelper.inl:767
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
std::lock_guard< mutex > LockGuard
Definition: SharedMutex.h:55
std::shared_lock< mutex > SharedLock
Definition: SharedMutex.h:49
uint32_t U32
uint64_t U64