forked from Error323/E323AI
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ATask.cpp
304 lines (251 loc) · 6.75 KB
/
ATask.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include "ATask.h"
#include "CGroup.h"
#include "CEconomy.h"
#include "CUnit.h"
int ATask::counter = 0;
ATask::ATask(AIClasses *_ai):ARegistrar(++counter) {
t = TASK_UNDEFINED;
ai = _ai;
active = false;
suspended = false;
isMoving = true;
pos = ZEROVECTOR;
initFrame = ai->cb->GetCurrentFrame();
validateInterval = 5 * 30; // 5 sec by default
nextValidateFrame = validateInterval;
priority = NORMAL;
queueID = 0;
}
void ATask::remove() {
LOG_II("ATask::remove " << (*this))
// NOTE: removal order below is VERY important
// remove current task from CTaskHandler, so it will mark this task
// to be killed on next update
std::list<ARegistrar*>::iterator j = records.begin();
while (j != records.end()) {
ARegistrar *regobj = *j; ++j;
regobj->remove(*this);
}
// remove all assisting tasks...
std::list<ATask*>::iterator i = assisters.begin();
while (i != assisters.end()) {
ATask *task = *i; ++i;
task->remove();
}
assert(assisters.size() == 0);
// detach task from groups...
std::list<CGroup*>::iterator itGroup = groups.begin();
while(itGroup != groups.end()) {
CGroup *g = *itGroup; ++itGroup;
removeGroup(*g);
}
active = false;
}
// called on Group removing
void ATask::remove(ARegistrar &group) {
CGroup *g = dynamic_cast<CGroup*>(&group);
assert(g != NULL);
removeGroup(*g);
if (groups.empty()) {
LOG_II("ATask::remove " << (*g))
remove();
}
}
CGroup* ATask::firstGroup() const {
if (groups.empty())
return NULL;
return groups.front();
}
void ATask::addGroup(CGroup &g) {
// FIXME: remove this when task queue will be supported
assert(!g.busy);
/*
if (g->busy) {
ATask *task = ai->tasks->getTask(g);
assert(task != NULL && task != this);
task->suspended = true;
// TODO: nextTask = task;
}
*/
g.reg(*this);
g.busy = true;
g.micro(false);
//g.abilities(true);
if ((g.cats&STATIC).any())
isMoving = false;
groups.push_back(&g);
}
void ATask::removeGroup(CGroup &g) {
g.unreg(*this);
if (!suspended) {
g.busy = false;
g.unwait();
g.micro(false);
//g.abilities(false);
if (isMoving) g.stop();
}
groups.remove(&g);
}
bool ATask::enemyScan(int& target) {
CGroup *group = firstGroup();
bool scout = (group->cats&SCOUTER).any();
bool aircraft = (group->cats&AIR).any();
TargetsFilter tf;
if (scout) {
tf.threatCeiling = 1.1f;
tf.threatRadius = 300.0f;
}
else {
if (aircraft) {
if ((group->cats&ASSAULT).any()) {
tf.exclude = AIR;
tf.threatCeiling = group->strength;
}
else {
tf.threatCeiling = 1.1f;
if((group->cats&ANTIAIR).any()) {
tf.exclude = LAND|SEA|SUB;
}
}
// TODO: replace with maneuvering radius?
tf.threatRadius = 300.0f;
}
else {
tf.exclude = SCOUTER;
tf.threatFactor = 0.001f;
tf.threatCeiling = group->strength;
tf.threatRadius = 0.0f;
}
}
// do not chase after aircraft with non-aircraft groups...
if (!aircraft)
tf.exclude |= AIR;
target = group->selectTarget(group->getScanRange(), tf);
if (target >= 0) {
group->attack(target);
group->micro(true);
if (scout)
LOG_II("ATask::enemyScan scout " << (*group) << " is microing enemy target Unit(" << target << ") (threat = " << tf.threatValue << ")")
else
LOG_II("ATask::enemyScan engage " << (*group) << " is microing enemy target Unit(" << target << ") (threat = " << tf.threatValue << ")")
return true;
}
return false;
}
bool ATask::resourceScan() {
bool isFeature = true;
int bestFeature = -1;
float bestDist = std::numeric_limits<float>::max();
CGroup *group = firstGroup();
// NOTE: do not use group->los because it is too small and does not
// correspond to real map units
float radius = group->buildRange;
float3 gpos = group->pos();
assert(radius > EPS);
// reclaim features when we can store metal only...
if (!ai->economy->mexceeding) {
const int numFeatures = ai->cb->GetFeatures(&ai->unitIDs[0], MAX_RANGESCAN_FEATURES, gpos, 1.5f * radius);
for (int i = 0; i < numFeatures; i++) {
const int uid = ai->unitIDs[i];
const FeatureDef *fd = ai->cb->GetFeatureDef(uid);
if (fd->metal > 0.0f) {
float3 fpos = ai->cb->GetFeaturePos(uid);
float dist = gpos.distance2D(fpos);
if (dist < bestDist) {
bestFeature = uid;
bestDist = dist;
}
}
}
}
// if there is no feature available then reclaim enemy unarmed building,
// hehe :)
if (bestFeature == -1) {
std::map<int, bool> occupied;
TargetsFilter tf;
tf.include = STATIC;
tf.exclude = ATTACKER;
tf.threatCeiling = 1.1f;
tf.threatRadius = radius;
bestFeature = group->selectTarget(radius, tf);
isFeature = false;
}
if (bestFeature != -1) {
group->reclaim(bestFeature, isFeature);
group->micro(true);
LOG_II("ATask::resourceScan group " << (*group) << " is reclaiming")
return true;
}
return false;
}
bool ATask::repairScan() {
if (ai->economy->mstall || ai->economy->estall)
return false;
int bestUnit = -1;
float bestScore = 0.0f;
CGroup *group = firstGroup();
float radius = group->buildRange;
float3 gpos = group->pos();
const int numUnits = ai->cb->GetFriendlyUnits(&ai->unitIDs[0], gpos, 2.0f * radius, MAX_RANGESCAN_FEATURES);
for (int i = 0; i < numUnits; i++) {
const int uid = ai->unitIDs[i];
if (ai->cb->UnitBeingBuilt(uid) || group->firstUnit()->key == uid)
continue;
const float healthDamage = ai->cb->GetUnitMaxHealth(uid) - ai->cb->GetUnitHealth(uid);
if (healthDamage > EPS) {
// TODO: somehow limit number of repairing builders per unit
const UnitDef *ud = ai->cb->GetUnitDef(uid);
const unitCategory cats = UC(ud->id);
if ((cats&AIR).any())
continue;
const float score = healthDamage + (CUnit::isDefense(ud) ? 10000.0f: 0.0f) + (CUnit::isStatic(ud) ? 5000.0f: 0.0f);
if (score > bestScore) {
bestUnit = uid;
bestScore = score;
}
}
}
if (bestUnit != -1) {
group->repair(bestUnit);
group->micro(true);
LOG_II("ATask::repairScan group " << (*group) << " is repairing")
return true;
}
return false;
}
int ATask::lifeFrames() const {
return ai->cb->GetCurrentFrame() - initFrame;
}
float ATask::lifeTime() const {
return (float)(ai->cb->GetCurrentFrame() - initFrame) / 30.0f;
}
void ATask::update() {
if (!active) return;
if (validateInterval > 0) {
int lifetime = lifeFrames();
if (lifetime >= nextValidateFrame) {
if (!onValidate()) {
remove();
return;
}
else
nextValidateFrame = lifetime + validateInterval;
}
}
if (suspended) return;
onUpdate();
}
std::ostream& operator<<(std::ostream &out, const ATask &atask) {
atask.toStream(out);
if (atask.assisters.size() > 0) {
out << " Assisters: amount(" << atask.assisters.size() << ") [";
std::list<ATask*>::const_iterator i;
for (i = atask.assisters.begin(); i != atask.assisters.end(); ++i) {
CGroup *group = (*i)->firstGroup();
if (group)
out << (*group);
}
out << "]";
}
return out;
}