forked from Error323/E323AI
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCUnit.cpp
383 lines (327 loc) · 7.67 KB
/
CUnit.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#include "CUnit.h"
#include "CAI.h"
#include "CUnitTable.h"
#include "CRNG.h"
void CUnit::remove() {
remove(*this);
}
void CUnit::remove(ARegistrar ®) {
LOG_II("CUnit::remove " << (*this))
std::list<ARegistrar*>::iterator i = records.begin();
while(i != records.end()) {
ARegistrar *regobj = *i; i++;
// remove from CUnitTable, CGroup
regobj->remove(reg);
}
assert(records.empty());
// TODO: remove next line when prev assertion is never raised
records.clear();
}
float3 CUnit::pos() {
return ai->cb->GetUnitPos(key);
}
void CUnit::reset(int uid, int bid) {
records.clear();
this->key = uid;
this->def = ai->cb->GetUnitDef(uid);
this->type = UT(def->id);
this->builtBy = bid;
this->waiting = false;
this->microing= false;
this->techlvl = 0;
this->group = NULL;
}
bool CUnit::isEconomy() {
static const unsigned int economic =
FACTORY|BUILDER|ASSISTER|RESURRECTOR|COMMANDER|MEXTRACTOR|MMAKER
|EMAKER|MSTORAGE|ESTORAGE;
return type->cats&economic;
}
bool CUnit::reclaim(float3 pos, float radius) {
Command c = createPosCommand(CMD_RECLAIM, pos, radius);
if (c.id != 0) {
ai->cb->GiveOrder(key, &c);
ai->unittable->idle[key] = false;
return true;
}
return false;
}
bool CUnit::reclaim(int target, bool enqueue) {
Command c = createTargetCommand(CMD_RECLAIM, target);
if (c.id != 0) {
if (enqueue)
c.options |= SHIFT_KEY;
ai->cb->GiveOrder(key, &c);
ai->unittable->idle[key] = false;
return true;
}
return false;
}
int CUnit::queueSize() {
return ai->cb->GetCurrentUnitCommands(key)->size();
}
bool CUnit::attack(int target, bool enqueue) {
Command c = createTargetCommand(CMD_ATTACK, target);
if (c.id != 0) {
if (enqueue)
c.options |= SHIFT_KEY;
ai->cb->GiveOrder(key, &c);
ai->unittable->idle[key] = false;
return true;
}
return false;
}
bool CUnit::moveForward(float dist, bool enqueue) {
float3 upos = ai->cb->GetUnitPos(key);
facing f = getBestFacing(upos);
float3 pos(upos);
switch(f) {
case NORTH:
pos.z -= dist;
break;
case SOUTH:
pos.z += dist;
break;
case EAST:
pos.x += dist;
break;
case WEST:
pos.x -= dist;
break;
default: break;
}
return move(pos, enqueue);
}
bool CUnit::setOnOff(bool on) {
Command c = createTargetCommand(CMD_ONOFF, on);
if (c.id != 0) {
ai->cb->GiveOrder(key, &c);
return true;
}
return false;
}
bool CUnit::moveRandom(float radius, bool enqueue) {
float3 pos = ai->cb->GetUnitPos(key);
float3 newpos(rng.RandFloat(2.0f) - 1.0f, 0.0f, rng.RandFloat(2.0f) - 1.0f);
newpos.Normalize();
newpos *= radius;
newpos.x += pos.x;
newpos.z += pos.z;
newpos.y = ai->cb->GetElevation(pos.x, pos.z);
return move(newpos, enqueue);
}
bool CUnit::move(float3 &pos, bool enqueue) {
Command c = createPosCommand(CMD_MOVE, pos);
if (c.id != 0) {
if (enqueue)
c.options |= SHIFT_KEY;
ai->cb->GiveOrder(key, &c);
ai->unittable->idle[key] = false;
return true;
}
return false;
}
bool CUnit::patrol(const float3 &pos, bool enqueue) {
Command c = createPosCommand(CMD_PATROL, pos);
if (c.id != 0) {
if (enqueue)
c.options |= SHIFT_KEY;
ai->cb->GiveOrder(key, &c);
ai->unittable->idle[key] = false;
return true;
}
return false;
}
bool CUnit::move_state(move_states state, bool enqueue) {
Command c = createTargetCommand(CMD_MOVE_STATE, state);
if (c.id != 0) {
if (enqueue)
c.options |= SHIFT_KEY;
ai->cb->GiveOrder(key, &c);
return true;
}
return false;
}
bool CUnit::guard(int target, bool enqueue) {
Command c = createTargetCommand(CMD_GUARD, target);
if (c.id != 0) {
if (enqueue)
c.options |= SHIFT_KEY;
ai->cb->GiveOrder(key, &c);
ai->unittable->idle[key] = false;
return true;
}
return false;
}
bool CUnit::cloak(bool on) {
Command c = createTargetCommand(CMD_CLOAK, on);
if (c.id != 0) {
ai->cb->GiveOrder(key, &c);
return true;
}
return false;
}
bool CUnit::repair(int target) {
Command c = createTargetCommand(CMD_REPAIR, target);
if (c.id != 0) {
ai->cb->GiveOrder(key, &c);
ai->unittable->idle[key] = false;
return true;
}
return false;
}
bool CUnit::build(UnitType *toBuild, float3 &pos) {
int mindist = 8;
if (toBuild->cats&FACTORY || toBuild->cats&EMAKER) {
mindist = 10;
if (toBuild->cats&VEHICLE || toBuild->cats&TECH3)
mindist = 15;
}
else if(toBuild->cats&MEXTRACTOR)
mindist = 0;
float startRadius = def->buildDistance*2.0f;
facing f = getBestFacing(pos);
float3 start = ai->cb->GetUnitPos(key);
float3 goal = ai->cb->ClosestBuildSite(toBuild->def, pos, startRadius, mindist, f);
int i = 0;
while (goal.x < 0.0f) {
startRadius += def->buildDistance;
goal = ai->cb->ClosestBuildSite(toBuild->def, pos, startRadius, mindist, f);
i++;
if (i > 15) {
/* Building in this area seems impossible, relocate unit */
moveRandom(startRadius);
return false;
}
}
Command c = createPosCommand(-(toBuild->id), goal, -1.0f, f);
if (c.id != 0) {
ai->cb->GiveOrder(key, &c);
ai->unittable->idle[key] = false;
return true;
}
return false;
}
bool CUnit::stop() {
Command c;
c.id = CMD_STOP;
ai->cb->GiveOrder(key, &c);
return true;
}
bool CUnit::micro(bool on) {
microing = on;
return microing;
}
bool CUnit::isMicroing() {
return microing;
}
bool CUnit::isOn() {
return ai->cb->IsUnitActivated(key);
}
bool CUnit::wait() {
if (!waiting) {
Command c;
c.id = CMD_WAIT;
ai->cb->GiveOrder(key, &c);
waiting = true;
}
return waiting;
}
bool CUnit::unwait() {
if (waiting) {
Command c;
c.id = CMD_WAIT;
ai->cb->GiveOrder(key, &c);
waiting = false;
}
return waiting;
}
/* From KAIK */
bool CUnit::factoryBuild(UnitType *ut, bool enqueue) {
Command c;
if (enqueue)
c.options |= SHIFT_KEY;
c.id = -(ut->def->id);
ai->cb->GiveOrder(key, &c);
ai->unittable->idle[key] = false;
return true;
}
/* From KAIK */
Command CUnit::createTargetCommand(int cmd, int target) {
Command c;
c.id = cmd;
c.params.push_back(target);
return c;
}
/* From KAIK */
Command CUnit::createPosCommand(int cmd, float3 pos, float radius, facing f) {
if (pos.x > ai->cb->GetMapWidth() * 8)
pos.x = ai->cb->GetMapWidth() * 8;
if (pos.z > ai->cb->GetMapHeight() * 8)
pos.z = ai->cb->GetMapHeight() * 8;
if (pos.x < 0)
pos.x = 0;
if (pos.y < 0)
pos.y = 0;
Command c;
c.id = cmd;
c.params.push_back(pos.x);
c.params.push_back(pos.y);
c.params.push_back(pos.z);
if (f != NONE)
c.params.push_back(f);
if (radius > 0.0f)
c.params.push_back(radius);
return c;
}
quadrant CUnit::getQuadrant(float3 &pos) {
int mapWidth = ai->cb->GetMapWidth() * 8;
int mapHeight = ai->cb->GetMapHeight() * 8;
quadrant mapQuadrant = NORTH_EAST;
if (pos.x < (mapWidth >> 1)) {
/* left half of map */
if (pos.z < (mapHeight >> 1)) {
mapQuadrant = NORTH_WEST;
} else {
mapQuadrant = SOUTH_WEST;
}
}
else {
/* right half of map */
if (pos.z < (mapHeight >> 1)) {
mapQuadrant = NORTH_EAST;
} else {
mapQuadrant = SOUTH_EAST;
}
}
return mapQuadrant;
}
/* From KAIK */
facing CUnit::getBestFacing(float3 &pos) {
int mapWidth = ai->cb->GetMapWidth() * 8;
int mapHeight = ai->cb->GetMapHeight() * 8;
quadrant mapQuadrant = getQuadrant(pos);
facing f = NONE;
switch (mapQuadrant) {
case NORTH_WEST: {
f = (mapHeight > mapWidth) ? SOUTH: EAST;
} break;
case NORTH_EAST: {
f = (mapHeight > mapWidth) ? SOUTH: WEST;
} break;
case SOUTH_WEST: {
f = (mapHeight > mapWidth) ? NORTH: EAST;
} break;
case SOUTH_EAST: {
f = (mapHeight > mapWidth) ? NORTH: WEST;
} break;
}
return f;
}
std::ostream& operator<<(std::ostream &out, const CUnit &unit) {
if (unit.def == NULL)
out << "Unknown" << "(" << unit.key << ", " << unit.builtBy << ")";
else
out << unit.def->humanName << "(" << unit.key << ", " << unit.builtBy << ")";
return out;
}