-
Notifications
You must be signed in to change notification settings - Fork 8
/
CTaskHandler.cpp
197 lines (163 loc) · 4.57 KB
/
CTaskHandler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include "CTaskHandler.h"
#include <iostream>
#include <sstream>
#include <string>
#include <math.h>
#include <limits>
#include "CUnitTable.h"
#include "CPathfinder.h"
#include "CUnit.h"
#include "CGroup.h"
#include "CEconomy.h"
#include "CConfigParser.h"
#include "CThreatMap.h"
#include "CScopedTimer.h"
#include "CDefenseMatrix.h"
CTaskHandler::CTaskHandler(AIClasses *ai): ARegistrar(500) {
this->ai = ai;
statsMaxActiveTasks = 0;
statsMaxTasks = 0;
}
CTaskHandler::~CTaskHandler() {
//LOG_II("CTaskHandler::Stats MaxActiveTasks = " << statsMaxActiveTasks)
LOG_II("CTaskHandler::Stats MaxTasks = " << statsMaxTasks)
std::list<ATask*>::iterator it;
for (it = processQueue.begin(); it != processQueue.end(); ++it)
delete *it;
}
void CTaskHandler::remove(ARegistrar &obj) {
switch(obj.regtype()) {
case ARegistrar::TASK: {
ATask *task = dynamic_cast<ATask*>(&obj);
LOG_II("CTaskHandler::remove " << (*task))
for(std::list<CGroup*>::iterator it = task->groups.begin(); it != task->groups.end(); ++it) {
CGroup *group = *it;
group->unreg(*this);
groupToTask.erase(group->key);
if (task->isMoving)
ai->pathfinder->remove(*group);
}
activeTasks[task->t].erase(task->key);
obsoleteTasks.push(task);
break;
}
case ARegistrar::GROUP: {
CGroup *group = dynamic_cast<CGroup*>(&obj);
LOG_II("CTaskHandler::remove " << (*group))
groupToTask.erase(group->key);
break;
}
default:
assert(false);
}
obj.unreg(*this);
}
void CTaskHandler::update() {
/* delete obsolete tasks from memory */
while(!obsoleteTasks.empty()) {
ATask *task = obsoleteTasks.top();
processQueue.remove(task);
obsoleteTasks.pop();
// make sure task is really detached from groups
assert(task->groups.size() == 0);
delete task;
}
/* Begin task updates */
if (!processQueue.empty()) {
ATask *task;
if (processQueue.size() == 1) {
task = processQueue.front();
if (task->active) task->update();
}
else {
int c = 0; // active tasks counter
std::list<ATask*>::iterator it = processQueue.begin();
ATask *taskFirst = *it;
do {
task = *it;
if (task->active) {
task->update();
c++;
}
++it;
// imitate circle queue...
processQueue.pop_front();
processQueue.push_back(task);
assert(it != processQueue.end());
} while (c < MAX_TASKS_PER_UPDATE && (*it)->key != taskFirst->key);
}
statsMaxTasks = std::max<int>(statsMaxTasks, processQueue.size());
}
}
float3 CTaskHandler::getPos(const CGroup& group) {
assert(groupToTask.find(group.key) != groupToTask.end());
return groupToTask[group.key]->pos;
}
ATask* CTaskHandler::getTask(const CGroup& group) {
std::map<int, ATask*>::iterator i = groupToTask.find(group.key);
if (i == groupToTask.end())
return NULL;
return i->second;
}
ATask* CTaskHandler::getTaskByTarget(int uid) {
std::map<int, ATask*>::iterator i;
for (i = activeTasks[TASK_ATTACK].begin(); i != activeTasks[TASK_ATTACK].end(); ++i) {
if (((AttackTask*)i->second)->target == uid)
return i->second;
}
return NULL;
}
bool CTaskHandler::addTask(ATask* task, ATask::NPriority p) {
if (task == NULL)
return false;
assert(task->t != TASK_UNDEFINED);
task->priority = p;
std::list<CGroup*>::iterator it;
// NOTE: this is required because otherwise memory used by task will never
// be freed
task->reg(*this);
processQueue.push_back(task);
// NOTE: this is required because within ATask::onValidate() a path
// (or even paths) can be added, which in its turn calls
// CTaskHandler::getPos()
for (it = task->groups.begin(); it != task->groups.end(); ++it) {
(*it)->reg(*this);
groupToTask[(*it)->key] = task;
}
LOG_II((*task))
if (!task->onValidate()) {
task->remove();
return false;
}
// TODO: after MoveTask is implemented remove the code below
for(it = task->groups.begin(); it != task->groups.end(); ++it) {
CGroup* group = *it;
if (task->isMoving && !ai->pathfinder->pathAssigned(*group)) {
if(!ai->pathfinder->addGroup(*group)) {
task->remove();
return false;
}
}
}
activeTasks[task->t][task->key] = task;
task->active = true;
return true;
}
void CTaskHandler::onEnemyDestroyed(int enemy, int attacker) {
std::list<ATask*>::iterator it = processQueue.begin();
ATask *task;
while (it != processQueue.end()) {
task = *it; ++it;
if (task->active)
task->onEnemyDestroyed(enemy, attacker);
}
}
void CTaskHandler::onUnitDestroyed(int uid, int attacker) {
std::list<ATask*>::iterator it = processQueue.begin();
ATask *task;
while (it != processQueue.end()) {
task = *it; ++it;
if (task->active)
task->onUnitDestroyed(uid, attacker);
}
}