Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
AIManager.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2018 DIVIDE-Studio
3 Copyright (c) 2009 Ionut Cava
4
5 This file is part of DIVIDE Framework.
6
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software
9 and associated documentation files (the "Software"), to deal in the Software
10 without restriction,
11 including without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense,
13 and/or sell copies of the Software, and to permit persons to whom the
14 Software is furnished to do so,
15 subject to the following conditions:
16
17 The above copyright notice and this permission notice shall be included in
18 all copies or substantial portions of the Software.
19
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED,
22 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
23 PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25 DAMAGES OR OTHER LIABILITY,
26 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
27 IN CONNECTION WITH THE SOFTWARE
28 OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30 */
31
32#pragma once
33#ifndef DVD_AI_MANAGER_H_
34#define DVD_AI_MANAGER_H_
35
36#include "AI/Headers/AIEntity.h"
38
39namespace Divide {
40
41class Scene;
42struct Task;
43class TaskPool;
44
45namespace GFX {
46 class CommandBuffer;
47
49} //namespace GFX
50
51namespace AI {
52
53namespace Navigation {
54 class NavigationMesh;
55} //namespace Navigation
56
57class AIManager final : public SceneComponent
58{
59 public:
62
63 explicit AIManager(Scene& parentScene, TaskPool& pool);
64 ~AIManager();
65
67 void destroy();
69 void update(U64 deltaTimeUS);
73 bool registerEntity(U32 teamID, AIEntity* entity);
76 void unregisterEntity(U32 teamID, AIEntity* entity);
79 void unregisterEntity(AIEntity* entity);
80 inline AITeam* getTeamByID(const U32 AITeamID)
81 {
82 if (AITeamID != U32_MAX)
83 {
84 const AITeamMap::const_iterator it = _aiTeams.find(AITeamID);
85 if (it != std::end(_aiTeams))
86 {
87 return it->second.get();
88 }
89 }
90 return nullptr;
91 }
95
97 {
98 const NavMeshMap::const_iterator it = _navMeshes.find(radius);
99 if (it != std::end(_navMeshes))
100 {
101 return it->second.get();
102 }
103 return nullptr;
104 }
106
107 inline void setSceneCallback(const DELEGATE<void>& callback)
108 {
110 _sceneCallback = callback;
111 }
112 void pauseUpdate(const bool state) noexcept { _pauseUpdate = state; }
113 bool updatePaused() const noexcept { return _pauseUpdate; }
114 bool updating() const noexcept { return _updating; }
117 void debugDraw(GFX::CommandBuffer& bufferInOut,
118 GFX::MemoryBarrierCommand& memCmdInOut,
119 bool forceAll = true);
120 bool isDebugDraw() const noexcept { return _navMeshDebugDraw; }
122 void toggleNavMeshDebugDraw(bool state);
123 bool navMeshDebugDraw() const noexcept { return _navMeshDebugDraw; }
124
125 void stop() noexcept { _shouldStop = true; }
126
127 bool running() const noexcept { return _running; }
128
130 AITeam* registerTeam( U32 id );
132 bool unregisterTeam( U32 id);
133
134 protected:
135 friend class AITeam;
136
137 bool shouldStop() const noexcept;
138
139 private:
140 bool processInput(U64 deltaTimeUS);
141 bool processData(U64 deltaTimeUS);
142 bool updateEntities(U64 deltaTimeUS);
143
144 private:
147 std::atomic_bool _navMeshDebugDraw;
148 std::atomic_bool _pauseUpdate;
149 std::atomic_bool _updating;
150 std::atomic_bool _shouldStop;
151 std::atomic_bool _running;
157
158};
159
161
162} // namespace AI
163} // namespace Divide
164
165#endif //DVD_AI_MANAGER_H_
#define FWD_DECLARE_MANAGED_CLASS(T)
Based on OgreCrowd.
Definition: AIEntity.h:60
std::atomic_bool _navMeshDebugDraw
Definition: AIManager.h:147
std::atomic_bool _shouldStop
Definition: AIManager.h:150
void stop() noexcept
Definition: AIManager.h:125
void toggleNavMeshDebugDraw(bool state)
Toggle debug draw for all NavMeshes.
Definition: AIManager.cpp:203
std::atomic_bool _running
Definition: AIManager.h:151
NavMeshMap _navMeshes
Definition: AIManager.h:152
bool shouldStop() const noexcept
Definition: AIManager.cpp:226
hashMap< U32, std::unique_ptr< AITeam > > AITeamMap
Definition: AIManager.h:60
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 navMeshDebugDraw() const noexcept
Definition: AIManager.h:123
bool destroyNavMesh(AIEntity::PresetAgentRadius radius)
Definition: AIManager.cpp:152
void pauseUpdate(const bool state) noexcept
Definition: AIManager.h:112
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
Navigation::NavigationMesh * getNavMesh(const AIEntity::PresetAgentRadius radius) const
Definition: AIManager.h:96
bool running() const noexcept
Definition: AIManager.h:127
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
hashMap< AIEntity::PresetAgentRadius, std::unique_ptr< Navigation::NavigationMesh > > NavMeshMap
Definition: AIManager.h:61
AITeam * getTeamByID(const U32 AITeamID)
Definition: AIManager.h:80
DELEGATE< void > _sceneCallback
Definition: AIManager.h:156
bool updatePaused() const noexcept
Definition: AIManager.h:113
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
void setSceneCallback(const DELEGATE< void > &callback)
Remove a NavMesh.
Definition: AIManager.h:107
bool isDebugDraw() const noexcept
Definition: AIManager.h:120
bool unregisterTeam(U32 id)
Unregister an AI Team.
Definition: AIManager.cpp:189
std::atomic_bool _updating
Definition: AIManager.h:149
bool updating() const noexcept
Definition: AIManager.h:114
Scene & parentScene() noexcept
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
DELEGATE_STD< Ret, Args... > DELEGATE
std::lock_guard< mutex > LockGuard
Definition: SharedMutex.h:55
std::mutex Mutex
Definition: SharedMutex.h:40
std::shared_mutex SharedMutex
Definition: SharedMutex.h:43
hashAlg::unordered_map< K, V, HashFun, Predicate > hashMap
Definition: HashMap.h:55
constexpr U32 U32_MAX
uint32_t U32
uint64_t U64