Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
AITeam.cpp
Go to the documentation of this file.
1
2
3#include "Headers/AITeam.h"
4
8
9namespace Divide::AI {
10
11namespace {
13}
14
15AITeam::AITeam(const U32 id, AIManager& parentManager)
16 : GUIDWrapper(),
17 _teamID(id),
18 _parentManager(parentManager)
19{
20 _team.clear();
21}
22
24{
25 {
27 _aiTeamCrowd.clear();
28 }
29 {
31 for (TeamMap::value_type& entity : _team) {
32 Attorney::AIEntityAITeam::setTeamPtr(*entity.second, nullptr);
33 }
34 _team.clear();
35 }
36}
37
39 DIVIDE_ASSERT(_aiTeamCrowd.find(radius) == std::end(_aiTeamCrowd), "AITeam error: DtCrowd already existed for new navmesh!");
40
41 _aiTeamCrowd[radius] = std::make_unique<Navigation::DivideDtCrowd>(navMesh);
42}
43
45 const AITeamCrowd::iterator it = _aiTeamCrowd.find(radius);
47 it != std::end(_aiTeamCrowd),
48 "AITeam error: DtCrowd does not exist for specified navmesh!");
49 _aiTeamCrowd.erase(it);
50}
51
53 //ToDo: Cache this? -Ionut
55
56 U32 i = 0;
57 vector<AIEntity*> entities(_team.size(), nullptr);
58 for (const TeamMap::value_type& entity : _team) {
59 entities[i++] = entity.second;
60 }
61
62 return entities;
63}
64
65bool AITeam::update(TaskPool& parentPool, const U64 deltaTimeUS) {
66 // Crowds
67 {
69 for (AITeamCrowd::value_type& it : _aiTeamCrowd) {
70 it.second->update(deltaTimeUS);
71 }
72 }
73
75 for (AIEntity* entity : entities) {
76 if (!Attorney::AIEntityAITeam::update(*entity, deltaTimeUS)) {
77 return false;
78 }
79 }
80 const U16 entityCount = to_U16(entities.size());
81 if (entityCount <= g_entityThreadedThreashold) {
82 for (AIEntity* entity : entities) {
83 if (!Attorney::AIEntityAITeam::update(*entity, deltaTimeUS)) {
84 //print error;
85 }
86 }
87 } else {
88 ParallelForDescriptor descriptor = {};
89 descriptor._iterCount = entityCount;
90 descriptor._partitionSize = g_entityThreadedThreashold;
91 Parallel_For( parentPool, descriptor, [deltaTimeUS, &entities](const Task*, const U32 start, const U32 end)
92 {
93 for (U32 i = start; i < end; ++i) {
94 if (!Attorney::AIEntityAITeam::update(*entities[i], deltaTimeUS)) {
95 //print error;
96 }
97 }
98 });
99 }
100 return true;
101}
102
103bool AITeam::processInput(TaskPool& parentPool, const U64 deltaTimeUS) {
104 vector<AIEntity*> entities = getEntityList();
105
106 const U16 entityCount = to_U16(entities.size());
107 if (entityCount <= g_entityThreadedThreashold) {
108 for (AIEntity* entity : entities) {
109 if (!Attorney::AIEntityAITeam::processInput(*entity, deltaTimeUS)) {
110 //print error;
111 }
112 }
113 } else {
114 ParallelForDescriptor descriptor = {};
115 descriptor._iterCount = entityCount;
116 descriptor._partitionSize = g_entityThreadedThreashold;
117 Parallel_For( parentPool, descriptor, [deltaTimeUS, &entities](const Task*, const U32 start, const U32 end)
118 {
119 for (U32 i = start; i < end; ++i) {
120 if (!Attorney::AIEntityAITeam::processInput(*entities[i], deltaTimeUS)) {
121 //print error;
122 }
123 }
124 });
125 }
126
127 return true;
128}
129
130bool AITeam::processData(TaskPool& parentPool, const U64 deltaTimeUS) {
131 vector<AIEntity*> entities = getEntityList();
132
133 const U16 entityCount = to_U16(entities.size());
134 if (entityCount <= g_entityThreadedThreashold) {
135 for (AIEntity* entity : entities) {
136 if (!Attorney::AIEntityAITeam::processData(*entity, deltaTimeUS)) {
137 //print error;
138 }
139 }
140 } else {
141 ParallelForDescriptor descriptor = {};
142 descriptor._iterCount = entityCount;
143 descriptor._partitionSize = g_entityThreadedThreashold;
144 Parallel_For( parentPool, descriptor, [deltaTimeUS, &entities](const Task*, const U32 start, const U32 end)
145 {
146 for (U32 i = start; i < end; ++i) {
147 if (!Attorney::AIEntityAITeam::processData(*entities[i], deltaTimeUS)) {
148 //print error;
149 }
150 }
151 });
152 }
153
154 return true;
155}
156
158 vector<AIEntity*> entities = getEntityList();
159 for (AIEntity* entity : entities) {
160 entity->resetCrowd();
161 }
162}
163
165 if (!entity) {
166 return false;
167 }
170 if (_team.find(entity->getGUID()) != std::end(_team)) {
171 return true;
172 }
173 insert(_team, entity->getGUID(), entity);
175
176 return true;
177}
178
179// Removes an entity from this list
181 if (!entity) {
182 return false;
183 }
184
186 if (_team.find(entity->getGUID()) != std::end(_team)) {
187 _team.erase(entity->getGUID());
188 }
189 return true;
190}
191
192bool AITeam::addEnemyTeam(const U32 enemyTeamID) {
193 if (findEnemyTeamEntry(enemyTeamID) == std::end(_enemyTeams)) {
195 _enemyTeams.push_back(enemyTeamID);
196 return true;
197 }
198 return false;
199}
200
201bool AITeam::removeEnemyTeam(const U32 enemyTeamID) {
202 const vector<U32>::iterator it = findEnemyTeamEntry(enemyTeamID);
203 if (it != end(_enemyTeams)) {
205 _enemyTeams.erase(it);
206 return true;
207 }
208 return false;
209}
210
211} // namespace Divide::AI
#define DIVIDE_ASSERT(...)
Based on OgreCrowd.
Definition: AIEntity.h:60
bool removeEnemyTeam(U32 enemyTeamID)
Definition: AITeam.cpp:201
SharedMutex _updateMutex
Definition: AITeam.h:170
SharedMutex _crowdMutex
Definition: AITeam.h:171
void removeCrowd(AIEntity::PresetAgentRadius radius)
Definition: AITeam.cpp:44
void resetCrowd()
Update the crowding system.
Definition: AITeam.cpp:157
bool removeTeamMember(AIEntity *entity)
Definition: AITeam.cpp:180
vector< AIEntity * > getEntityList() const
Definition: AITeam.cpp:52
AITeam(U32 id, AIManager &parentManager)
Definition: AITeam.cpp:15
bool addTeamMember(AIEntity *entity)
Definition: AITeam.cpp:164
void addCrowd(AIEntity::PresetAgentRadius radius, Navigation::NavigationMesh *navMesh)
Definition: AITeam.cpp:38
bool processInput(TaskPool &parentPool, U64 deltaTimeUS)
Definition: AITeam.cpp:103
vector< U32 >::iterator findEnemyTeamEntry(U32 enemyTeamID)
Definition: AITeam.h:155
bool addEnemyTeam(U32 enemyTeamID)
Definition: AITeam.cpp:192
bool update(TaskPool &parentPool, U64 deltaTimeUS)
Definition: AITeam.cpp:65
~AITeam() override
Definition: AITeam.cpp:23
TeamMap _team
Definition: AITeam.h:165
AITeamCrowd _aiTeamCrowd
Definition: AITeam.h:169
vector< U32 > _enemyTeams
Definition: AITeam.h:172
bool processData(TaskPool &parentPool, U64 deltaTimeUS)
Definition: AITeam.cpp:130
static bool update(AIEntity &entity, const U64 deltaTimeUS)
Definition: AIEntity.h:222
static void setTeamPtr(AIEntity &entity, AITeam *const teamPtr)
Definition: AIEntity.h:213
static bool processInput(AIEntity &entity, const U64 deltaTimeUS)
Definition: AIEntity.h:216
static bool processData(AIEntity &entity, const U64 deltaTimeUS)
Definition: AIEntity.h:219
Utility class that adds basic GUID management to objects.
Definition: GUIDWrapper.h:44
FORCE_INLINE I64 getGUID() const noexcept
Definition: GUIDWrapper.h:51
std::lock_guard< mutex > LockGuard
Definition: SharedMutex.h:55
void insert(eastl::vector< T, A1 > &target, const eastl::vector< T, A2 > &source)
Definition: Vector.h:97
constexpr U16 to_U16(const T value)
eastl::vector< Type > vector
Definition: Vector.h:42
std::shared_lock< mutex > SharedLock
Definition: SharedMutex.h:49
uint16_t U16
void Parallel_For(TaskPool &pool, const ParallelForDescriptor &descriptor, const DELEGATE< void, const Task *, U32, U32 > &cbk)
Definition: TaskPool.cpp:428
uint32_t U32
uint64_t U64
U32 _partitionSize
How many elements should we process per async task.
Definition: TaskPool.h:45
U32 _iterCount
For loop iteration count.
Definition: TaskPool.h:43