forked from yanntm/YoBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CombatCommander.cpp
464 lines (386 loc) · 12.8 KB
/
CombatCommander.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#include <sstream>
#include "CombatCommander.h"
#include "Util.h"
#include "YoAgent.h"
#include "Micro.h"
const size_t IdlePriority = 0;
const size_t AttackPriority = 1;
const size_t BaseDefensePriority = 2;
const size_t ScoutDefensePriority = 3;
const size_t DropPriority = 4;
CombatCommander::CombatCommander(YoAgent & bot)
: m_bot(bot)
, m_squadData(bot)
, m_initialized(false)
, m_attackStarted(false)
{
}
void CombatCommander::onStart()
{
m_squadData.clearSquadData();
SquadOrder idleOrder(SquadOrderTypes::Idle, m_bot.Observation()->GetStartLocation(), 5, "Chill Out");
m_squadData.addSquad("Idle", Squad("Idle", idleOrder, IdlePriority, m_bot));
// the main attack squad that will pressure the enemy's closest base location
SquadOrder mainAttackOrder(SquadOrderTypes::Attack, getMainAttackLocation(), 25, "Attack Enemy Base"); //sc2::Point2D(0.0f, 0.0f), 25, "Attack Enemy Base");
m_squadData.addSquad("MainAttack", Squad("MainAttack", mainAttackOrder, AttackPriority, m_bot));
// the scout defense squad will handle chasing the enemy worker scout
SquadOrder enemyScoutDefense(SquadOrderTypes::Defend, m_bot.Observation()->GetStartLocation(), 25, "Get the scout");
m_squadData.addSquad("ScoutDefense", Squad("ScoutDefense", enemyScoutDefense, ScoutDefensePriority, m_bot));
}
bool CombatCommander::isSquadUpdateFrame()
{
return true;
}
void CombatCommander::onFrame(const std::vector<const sc2::Unit *> & combatUnits)
{
if (!m_attackStarted)
{
m_attackStarted = shouldWeStartAttacking();
}
m_combatUnits = combatUnits;
if (isSquadUpdateFrame())
{
updateIdleSquad();
//updateScoutDefenseSquad(); //transfer scout defense here, or add mineral-speedup take/release code
updateDefenseSquads();
updateAttackSquads();
}
m_squadData.onFrame();
}
bool CombatCommander::shouldWeStartAttacking()
{
// TODO: make this more clever
// For now: start attacking when we have more than 10 combat units
return m_combatUnits.size() >= 8; //m_bot.Config().CombatUnitsForAttack;
}
void CombatCommander::updateIdleSquad()
{
Squad & idleSquad = m_squadData.getSquad("Idle");
for (auto & unit : m_combatUnits)
{
// if it hasn't been assigned to a squad yet, put it in the low priority idle squad
if (m_squadData.canAssignUnitToSquad(unit, idleSquad))
{
idleSquad.addUnit(unit);
}
}
}
void CombatCommander::updateAttackSquads()
{
if (!m_attackStarted)
{
return;
}
Squad & mainAttackSquad = m_squadData.getSquad("MainAttack");
for (auto unit : m_combatUnits)
{
//BOT_ASSERT(unit, "null unit in combat units");
// get every unit of a lower priority and put it into the attack squad
if (!Util::IsWorker(unit)
&& !(unit->unit_type == sc2::UNIT_TYPEID::ZERG_OVERLORD)
&& !(unit->unit_type == sc2::UNIT_TYPEID::ZERG_QUEEN)
&& m_squadData.canAssignUnitToSquad(unit, mainAttackSquad))
{
m_squadData.assignUnitToSquad(unit, mainAttackSquad);
}
}
SquadOrder mainAttackOrder(SquadOrderTypes::Attack, getMainAttackLocation(), 25, "Attack Enemy Base");
mainAttackSquad.setSquadOrder(mainAttackOrder);
}
void CombatCommander::updateScoutDefenseSquad()
{
if (m_combatUnits.empty())
{
return;
}
// if the current squad has units in it then we can ignore this
Squad & scoutDefenseSquad = m_squadData.getSquad("ScoutDefense");
// get the region that our base is located in
const BaseLocation * myBaseLocation = m_bot.m_baseLocationManager.getPlayerStartingBaseLocation(Players::Self);
//BOT_ASSERT(myBaseLocation, "null self base location");
// get all of the enemy units in this region
std::vector<const sc2::Unit *> enemyUnitsInRegion;
for (auto & unit : m_bot.m_unitInfoManager.getUnits(Players::Enemy))
{
if (myBaseLocation->containsPosition(unit->pos))
{
enemyUnitsInRegion.push_back(unit);
}
}
// if there's an enemy worker in our region then assign someone to chase him
bool assignScoutDefender = (enemyUnitsInRegion.size() == 1) && Util::IsWorker(enemyUnitsInRegion[0]);
// if our current squad is empty and we should assign a worker, do it
if (scoutDefenseSquad.isEmpty() && assignScoutDefender)
{
// the enemy worker that is attacking us
const sc2::Unit * enemyWorkerUnit = *enemyUnitsInRegion.begin();
//BOT_ASSERT(enemyWorkerUnit, "null enemy worker unit");
// get our worker unit that is mining that is closest to it
const sc2::Unit * workerDefender = findClosestWorkerTo(m_combatUnits, enemyWorkerUnit->pos);
if (enemyWorkerUnit && workerDefender)
{
// grab it from the worker manager and put it in the squad
if (m_squadData.canAssignUnitToSquad(workerDefender, scoutDefenseSquad))
{
m_bot.m_workerManager.setCombatWorker(workerDefender);
m_squadData.assignUnitToSquad(workerDefender, scoutDefenseSquad);
}
}
}
// if our squad is not empty and we shouldn't have a worker chasing then take him out of the squad
else if (!scoutDefenseSquad.isEmpty() && !assignScoutDefender)
{
for (auto unit : scoutDefenseSquad.getUnits())
{
//BOT_ASSERT(unit, "null unit in scoutDefenseSquad");
Micro::SmartStop(unit, m_bot);
if (Util::IsWorker(unit))
{
m_bot.m_workerManager.finishedWithWorker(unit);
}
}
scoutDefenseSquad.clear();
}
}
void CombatCommander::updateDefenseSquads()
{
if (m_combatUnits.empty())
{
return;
}
// for each of our occupied regions
const BaseLocation * enemyBaseLocation = m_bot.m_baseLocationManager.getPlayerStartingBaseLocation(Players::Enemy);
for (const BaseLocation * myBaseLocation : m_bot.m_baseLocationManager.getOccupiedBaseLocations(Players::Self))
{
// don't defend inside the enemy region, this will end badly when we are stealing gas or cannon rushing
if (myBaseLocation == enemyBaseLocation)
{
continue;
}
sc2::Point2D basePosition = myBaseLocation->getPosition();
// start off assuming all enemy units in region are just workers
int numDefendersPerEnemyUnit = 2;
// all of the enemy units in this region
std::vector<const sc2::Unit *> enemyUnitsInRegion;
for (auto & unit : m_bot.m_unitInfoManager.getUnits(Players::Enemy))
{
// if it's an overlord, don't worry about it for defense, we don't care what they see
if (unit->unit_type == sc2::UNIT_TYPEID::ZERG_OVERLORD)
{
continue;
}
if (myBaseLocation->containsPosition(unit->pos))
{
enemyUnitsInRegion.push_back(unit);
}
}
// we can ignore the first enemy worker in our region since we assume it is a scout
for (auto unit : enemyUnitsInRegion)
{
//BOT_ASSERT(unit, "null enemyt unit in region");
if (Util::IsWorker(unit))
{
enemyUnitsInRegion.erase(std::remove(enemyUnitsInRegion.begin(), enemyUnitsInRegion.end(), unit), enemyUnitsInRegion.end());
break;
}
}
// calculate how many units are flying / ground units
int numEnemyFlyingInRegion = 0;
int numEnemyGroundInRegion = 0;
for (auto & unit : enemyUnitsInRegion)
{
//BOT_ASSERT(unit, "null enemyt unit in region");
if (unit->is_flying)
{
numEnemyFlyingInRegion++;
}
else
{
numEnemyGroundInRegion++;
}
}
std::stringstream squadName;
squadName << "Base Defense " << basePosition.x << " " << basePosition.y;
// if there's nothing in this region to worry about
if (enemyUnitsInRegion.empty())
{
// if a defense squad for this region exists, remove it
if (m_squadData.squadExists(squadName.str()))
{
m_squadData.getSquad(squadName.str()).clear();
}
// and return, nothing to defend here
continue;
}
else
{
// if we don't have a squad assigned to this region already, create one
if (!m_squadData.squadExists(squadName.str()))
{
SquadOrder defendRegion(SquadOrderTypes::Defend, basePosition, 32 * 25, "Defend Region!");
m_squadData.addSquad(squadName.str(), Squad(squadName.str(), defendRegion, BaseDefensePriority, m_bot));
}
}
// assign units to the squad
if (m_squadData.squadExists(squadName.str()))
{
Squad & defenseSquad = m_squadData.getSquad(squadName.str());
// figure out how many units we need on defense
int flyingDefendersNeeded = numDefendersPerEnemyUnit * numEnemyFlyingInRegion;
int groundDefensersNeeded = numDefendersPerEnemyUnit * numEnemyGroundInRegion;
updateDefenseSquadUnits(defenseSquad, flyingDefendersNeeded, groundDefensersNeeded);
}
else
{
//BOT_ASSERT(false, "Squad should have existed: %s", squadName.str().c_str());
}
}
// for each of our defense squads, if there aren't any enemy units near the position, remove the squad
std::set<std::string> uselessDefenseSquads;
for (const auto & kv : m_squadData.getSquads())
{
const Squad & squad = kv.second;
const SquadOrder & order = squad.getSquadOrder();
if (order.getType() != SquadOrderTypes::Defend)
{
continue;
}
bool enemyUnitInRange = false;
for (auto & unit : m_bot.Observation()->GetUnits(sc2::Unit::Alliance::Enemy))
{
if (Util::Dist(unit->pos, order.getPosition()) < order.getRadius())
{
enemyUnitInRange = true;
break;
}
}
if (!enemyUnitInRange)
{
m_squadData.getSquad(squad.getName()).clear();
}
}
}
void CombatCommander::updateDefenseSquadUnits(Squad & defenseSquad, const size_t & flyingDefendersNeeded, const size_t & groundDefendersNeeded)
{
auto & squadUnits = defenseSquad.getUnits();
// TODO: right now this will assign arbitrary defenders, change this so that we make sure they can attack air/ground
// if there's nothing left to defend, clear the squad
if (flyingDefendersNeeded == 0 && groundDefendersNeeded == 0)
{
defenseSquad.clear();
return;
}
size_t defendersNeeded = flyingDefendersNeeded + groundDefendersNeeded;
size_t defendersAdded = 0;
while (defendersNeeded > defendersAdded)
{
const sc2::Unit * defenderToAdd = findClosestDefender(defenseSquad, defenseSquad.getSquadOrder().getPosition());
if (defenderToAdd)
{
m_squadData.assignUnitToSquad(defenderToAdd, defenseSquad);
defendersAdded++;
}
else
{
break;
}
}
}
const sc2::Unit * CombatCommander::findClosestDefender(const Squad & defenseSquad, const sc2::Point2D & pos)
{
const sc2::Unit * closestDefender = nullptr;
float minDistance = std::numeric_limits<float>::max();
// TODO: add back special case of zergling rush defense
for (auto & unit : m_combatUnits)
{
//BOT_ASSERT(unit, "null combat unit");
if (!m_squadData.canAssignUnitToSquad(unit, defenseSquad))
{
continue;
}
float dist = Util::Dist(unit->pos, pos);
if (dist < minDistance) //first dist() will always be less than float.max , don't need "!closestDefender ||"
{
closestDefender = unit;
minDistance = dist;
}
}
return closestDefender;
}
void CombatCommander::drawSquadInformation()
{
m_squadData.drawSquadInformation();
}
sc2::Point2D CombatCommander::getMainAttackLocation()
{
const BaseLocation * enemyBaseLocation = m_bot.m_baseLocationManager.getPlayerStartingBaseLocation(Players::Enemy);
if (enemyBaseLocation == nullptr) {
for (auto & pos : m_bot.Observation()->GetGameInfo().enemy_start_locations)
{
return pos;
}
}
// First choice: Attack an enemy region if we can see units inside it
if (enemyBaseLocation)
{
sc2::Point2D enemyBasePosition = enemyBaseLocation->getPosition();
// If the enemy base hasn't been seen yet, go there.
if (!m_bot.m_map.isExplored(enemyBasePosition))
{
return enemyBasePosition;
}
else
{
// if it has been explored, go there if there are any visible enemy units there
for (auto & enemyUnit : m_bot.m_unitInfoManager.getUnits(Players::Enemy))
{
if (Util::Dist(enemyUnit->pos, enemyBasePosition) < 25)
{
return enemyBasePosition;
}
}
return enemyBasePosition; //go to enemyBase anyway
}
}
// Second choice: Attack known enemy buildings
for (const auto & kv : m_bot.m_unitInfoManager.getUnitInfoMap(Players::Enemy))
{
const UnitInfo & ui = kv.second;
if (Util::IsBuilding(ui.type) && !(ui.lastPosition.x == 0.0f && ui.lastPosition.y == 0.0f))
{
return ui.lastPosition;
}
}
// Third choice: Attack visible enemy units that aren't overlords
for (auto & enemyUnit : m_bot.m_unitInfoManager.getUnits(Players::Enemy))
{
if (enemyUnit->unit_type != sc2::UNIT_TYPEID::ZERG_OVERLORD && !Util::IsWorkerType(enemyUnit->unit_type))
{
return enemyUnit->pos;
}
}
// Fourth choice: We can't see anything so explore the map attacking along the way
return m_bot.m_map.getLeastRecentlySeenPosition();
}
const sc2::Unit * CombatCommander::findClosestWorkerTo(std::vector<const sc2::Unit *> & unitsToAssign, const sc2::Point2D & target)
{
const sc2::Unit * closestMineralWorker = nullptr;
float closestDist = std::numeric_limits<float>::max();
// for each of our workers
for (auto & unit : unitsToAssign)
{
//BOT_ASSERT(unit, "unit to assign was null");
if (!Util::IsWorker(unit)) continue;
// if it is a move worker
if ( m_bot.m_workerManager.isFree(unit))
{
float dist = Util::Dist(unit->pos, target);
if (dist < closestDist)
{
closestMineralWorker = unit;
dist = closestDist;
}
}
}
return closestMineralWorker;
}