forked from Archiatrus/5minBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Micro.cpp
546 lines (507 loc) · 17.7 KB
/
Micro.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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
#include "Micro.h"
#include "Util.h"
#include "CCBot.h"
const float dotRadius = 0.1f;
const sc2::Units CUnits2Units(CUnits units)
{
sc2::Units output;
for (const auto & unit : units)
{
output.push_back(unit->getUnit_ptr());
}
return output;
}
/*
void Micro::SmartStop(CUnit_ptr attacker, CCBot & bot)
{
BOT_ASSERT(attacker != nullptr, "Attacker is null");
bot.Actions()->UnitCommand(attacker->getUnit_ptr(), sc2::ABILITY_ID::STOP);
}
*/
void Micro::SmartAttackUnit(CUnit_ptr attacker, CUnit_ptr target, CCBot & bot, bool queue)
{
BOT_ASSERT(attacker != nullptr, "Attacker is null");
BOT_ASSERT(target != nullptr, "Target is null");
// if we are already attack it, we do not need to spam the attack
if (!attacker->getOrders().empty() && attacker->getOrders().back().target_unit_tag == target->getTag())
{
return;
}
bot.Actions()->UnitCommand(attacker->getUnit_ptr(), sc2::ABILITY_ID::ATTACK_ATTACK, target->getUnit_ptr(), queue);
}
void Micro::SmartAttackUnit(const CUnits & attacker, const CUnit_ptr target, CCBot & bot, bool queue)
{
BOT_ASSERT(target != nullptr, "Target is null");
// if we are already attack it, we do not need to spam the attack
sc2::Units attackerThatNeedToAttack;
sc2::Units attackerThatNeedToAttackMove;
for (const auto & a : attacker)
{
if (!(a->getOrders().empty()) && (a->getOrders().back().target_unit_tag == target->getTag() || (a->getOrders().back().ability_id == sc2::ABILITY_ID::ATTACK && Util::DistSq(a->getOrders().back().target_pos, target->getPos()) < 1.0f)))
{
continue;
}
if(target->isVisible())
{
attackerThatNeedToAttack.push_back(a->getUnit_ptr());
}
else
{
attackerThatNeedToAttackMove.push_back(a->getUnit_ptr());
}
}
if (attackerThatNeedToAttack.size() > 0)
{
bot.Actions()->UnitCommand(attackerThatNeedToAttack, sc2::ABILITY_ID::ATTACK_ATTACK, target->getUnit_ptr(), queue);
}
if (attackerThatNeedToAttackMove.size() > 0)
{
bot.Actions()->UnitCommand(attackerThatNeedToAttackMove, sc2::ABILITY_ID::ATTACK_ATTACK, target->getPos(), queue);
}
}
void Micro::SmartAttackMove(CUnit_ptr attacker, const sc2::Point2D & targetPosition, CCBot & bot)
{
BOT_ASSERT(attacker != nullptr, "Attacker is null");
if (attacker->getOrders().empty() || attacker->getOrders().back().ability_id != sc2::ABILITY_ID::ATTACK || Util::DistSq(attacker->getOrders().back().target_pos, targetPosition) > 1.0f)
{
bot.Actions()->UnitCommand(attacker->getUnit_ptr(), sc2::ABILITY_ID::ATTACK_ATTACK, targetPosition);
}
}
void Micro::SmartAttackMoveToUnit(const CUnits & attacker, const CUnit_ptr target, CCBot & bot)
{
sc2::Units attackerThatNeedToAttackTarget;
sc2::Units attackerThatNeedToMove;
sc2::Units attackerThatNeedToAttackMove;
for (const auto & a : attacker)
{
const float range = a->getAttackRange(target);
const float dist = Util::Dist(a->getPos(), target->getPos());
if (dist <= range)
{
if (a->getOrders().empty() || a->getOrders().front().ability_id != sc2::ABILITY_ID::ATTACK || a->getOrders().front().target_unit_tag != target->getTag())
{
attackerThatNeedToAttackTarget.push_back(a->getUnit_ptr());
}
}
else
{
if (a->getWeaponCooldown() > 0.0f)
{
if (a->getOrders().empty() || a->getOrders().front().ability_id != sc2::ABILITY_ID::MOVE || Util::DistSq(a->getOrders().front().target_pos,target->getPos())>0.01f)
{
attackerThatNeedToMove.push_back(a->getUnit_ptr());
}
}
else
{
if (a->getOrders().empty() || Util::DistSq(a->getOrders().front().target_pos,target->getPos())>0.01f)
{
attackerThatNeedToAttackMove.push_back(a->getUnit_ptr());
}
}
}
}
if (attackerThatNeedToAttackTarget.size() > 0)
{
bot.Actions()->UnitCommand(attackerThatNeedToAttackTarget, sc2::ABILITY_ID::ATTACK_ATTACK, target->getUnit_ptr());
}
if (attackerThatNeedToMove.size() > 0)
{
bot.Actions()->UnitCommand(attackerThatNeedToMove, sc2::ABILITY_ID::MOVE, target->getPos());
}
if (attackerThatNeedToAttackMove.size() > 0)
{
bot.Actions()->UnitCommand(attackerThatNeedToAttackMove, sc2::ABILITY_ID::ATTACK, target->getPos());
}
}
void Micro::SmartAttackMoveToPos(const CUnits & attacker, const sc2::Point2D pos, CCBot & bot)
{
sc2::Units attackerThatNeedToMove;
sc2::Units attackerThatNeedToAttackMove;
for (const auto & a : attacker)
{
if (a->getWeaponCooldown() > 0.0f)
{
if (a->getOrders().empty() || a->getOrders().front().ability_id != sc2::ABILITY_ID::MOVE || Util::DistSq(a->getOrders().front().target_pos,pos)>0.01f)
{
attackerThatNeedToMove.push_back(a->getUnit_ptr());
}
}
else
{
if (a->getOrders().empty() || a->getOrders().front().ability_id == sc2::ABILITY_ID::MOVE || Util::DistSq(a->getOrders().front().target_pos,pos)>0.01f)
{
attackerThatNeedToAttackMove.push_back(a->getUnit_ptr());
}
}
}
if (attackerThatNeedToMove.size() > 0)
{
bot.Actions()->UnitCommand(attackerThatNeedToMove, sc2::ABILITY_ID::MOVE, pos);
}
if (attackerThatNeedToAttackMove.size() > 0)
{
bot.Actions()->UnitCommand(attackerThatNeedToAttackMove, sc2::ABILITY_ID::ATTACK, pos);
}
}
void Micro::SmartAttackMove(CUnits & attacker, const sc2::Point2D & targetPosition, CCBot & bot)
{
sc2::Units attackerThatNeedToMove;
for (const auto & a : attacker)
{
if (a->getOrders().empty() || a->getOrders().back().ability_id != sc2::ABILITY_ID::ATTACK || Util::DistSq(a->getOrders().back().target_pos, targetPosition) > 1.0f)
{
attackerThatNeedToMove.push_back(a->getUnit_ptr());
}
}
if (attackerThatNeedToMove.size() > 0)
{
bot.Actions()->UnitCommand(attackerThatNeedToMove, sc2::ABILITY_ID::ATTACK, targetPosition);
}
}
void Micro::SmartMove(const CUnits attackers, const CUnit_ptr target, CCBot & bot,const bool queue)
{
sc2::Units attackersThatNeedToMove;
for (const auto & attacker : attackers)
{
if (!attacker->getOrders().empty() && attacker->getOrders().back().ability_id == sc2::ABILITY_ID::MOVE && attacker->getOrders().back().target_unit_tag == target->getTag())
{
continue;
}
attackersThatNeedToMove.push_back(attacker->getUnit_ptr());
}
bot.Actions()->UnitCommand(attackersThatNeedToMove, sc2::ABILITY_ID::MOVE, target->getUnit_ptr(), queue);
}
void Micro::SmartMove(CUnit_ptr attacker, CUnit_ptr target, CCBot & bot, bool queue)
{
// SmartMove(attacker, target->getPos(), bot, queue);
if (!attacker->getOrders().empty() && attacker->getOrders().back().ability_id == sc2::ABILITY_ID::MOVE && attacker->getOrders().back().target_unit_tag == target->getTag())
{
return;
}
bot.Actions()->UnitCommand(attacker->getUnit_ptr(), sc2::ABILITY_ID::MOVE, target->getUnit_ptr(), queue);
}
void Micro::SmartMove(CUnit_ptr attacker, const sc2::Point2D & targetPosition, CCBot & bot,bool queue)
{
BOT_ASSERT(attacker != nullptr, "Attacker is null");
// If we are already going there we do not have to spam it
if (!attacker->getOrders().empty() && attacker->getOrders().back().ability_id == sc2::ABILITY_ID::MOVE && Util::DistSq(attacker->getOrders().back().target_pos,targetPosition) < 0.01f || Util::DistSq(attacker->getPos(),targetPosition) < 0.01f)
{
return;
}
if (!attacker->isFlying())
{
sc2::Point2D validWalkableTargetPosition = bot.Map().getClosestWalkableTo(targetPosition);
bot.Actions()->UnitCommand(attacker->getUnit_ptr(), sc2::ABILITY_ID::MOVE, validWalkableTargetPosition, queue);
}
else
{
sc2::Point2D targetPositionNew = targetPosition;
float x_min = static_cast<float>(bot.Observation()->GetGameInfo().playable_min.x);
float x_max = static_cast<float>(bot.Observation()->GetGameInfo().playable_max.x);
float y_min = static_cast<float>(bot.Observation()->GetGameInfo().playable_min.y);
float y_max = static_cast<float>(bot.Observation()->GetGameInfo().playable_max.y);
if (targetPosition.x < x_min)
{
if (targetPosition.y > attacker->getPos().y)
{
targetPositionNew = sc2::Point2D(x_min, y_max);
}
else
{
targetPositionNew = sc2::Point2D(x_min, y_min);
}
}
else if (targetPosition.x > x_max)
{
if (targetPosition.y > attacker->getPos().y)
{
targetPositionNew = sc2::Point2D(x_max, y_max);
}
else
{
targetPositionNew = sc2::Point2D(x_max, y_min);
}
}
else if (targetPosition.y < y_min)
{
if (targetPosition.x > attacker->getPos().x)
{
targetPositionNew = sc2::Point2D(x_max, y_min);
}
else
{
targetPositionNew = sc2::Point2D(x_min, y_min);
}
}
else if (targetPosition.y > y_max)
{
if (targetPosition.x > attacker->getPos().x)
{
targetPositionNew = sc2::Point2D(x_max, y_max);
}
else
{
targetPositionNew = sc2::Point2D(x_min, y_max);
}
}
bot.Actions()->UnitCommand(attacker->getUnit_ptr(), sc2::ABILITY_ID::MOVE, targetPositionNew, queue);
}
}
void Micro::SmartMove(CUnits attackers, const sc2::Point2D & targetPosition, CCBot & bot, bool queue)
{
sc2::Point2D validWalkableTargetPosition = targetPosition;
if (!(bot.Map().isWalkable(targetPosition) && bot.Map().isValid(targetPosition)))
{
sc2::Point2D attackersPos = Util::CalcCenter(attackers);
sc2::Point2D homeVector = bot.Bases().getPlayerStartingBaseLocation(Players::Self)->getCenterOfBase() - attackersPos;
homeVector *= Util::DistSq(attackersPos, targetPosition) / Util::DistSq(homeVector);
validWalkableTargetPosition += homeVector;
}
sc2::Units flyingMover;
sc2::Units walkingMover;
for (const auto & attacker : attackers)
{
//If we are already going there we do not have to spam it
if (!attacker->getOrders().empty() && attacker->getOrders().back().ability_id == sc2::ABILITY_ID::MOVE && Util::DistSq(attacker->getOrders().back().target_pos, targetPosition) < 0.01f || Util::DistSq(attacker->getPos(), targetPosition) < 0.01f)
{
continue;
}
if (!attacker->isFlying())
{
walkingMover.push_back(attacker->getUnit_ptr());
}
else
{
flyingMover.push_back(attacker->getUnit_ptr());
}
}
if (walkingMover.size() > 0)
{
bot.Actions()->UnitCommand(walkingMover, sc2::ABILITY_ID::MOVE, validWalkableTargetPosition, queue);
}
if (flyingMover.size() > 0)
{
bot.Actions()->UnitCommand(flyingMover, sc2::ABILITY_ID::MOVE, targetPosition, queue);
}
}
void Micro::SmartRightClick(CUnit_ptr unit, CUnit_ptr target, CCBot & bot, bool queue)
{
BOT_ASSERT(unit != nullptr, "Unit is null");
BOT_ASSERT(target != nullptr, "Unit is null");
bot.Actions()->UnitCommand(unit->getUnit_ptr(), sc2::ABILITY_ID::SMART, target->getUnit_ptr(),queue);
}
void Micro::SmartRightClick(CUnits units, CUnit_ptr target, CCBot & bot, bool queue)
{
BOT_ASSERT(units.size() > 0, "Unit is null");
BOT_ASSERT(target != nullptr, "Unit is null");
bot.Actions()->UnitCommand(CUnits2Units(units), sc2::ABILITY_ID::SMART, target->getUnit_ptr(), queue);
}
void Micro::SmartRightClick(CUnit_ptr unit, CUnits targets, CCBot & bot)
{
BOT_ASSERT(unit != nullptr, "Unit is null");
BOT_ASSERT(targets.size() > 0, "Unit is null");
if (unit->getUnitType() == sc2::UNIT_TYPEID::TERRAN_MEDIVAC)
{
CUnit_ptr target = nullptr;
float minDist = std::numeric_limits<float>::max();
for (const auto & t : targets)
{
float dist = Util::Dist(t->getPos(), unit->getPos());
if (t->getLastSeenGameLoop()==unit->getLastSeenGameLoop() && (!target || minDist > dist))
{
minDist = dist;
target = t;
}
}
if (target)
{
bot.Actions()->UnitCommand(unit->getUnit_ptr(), sc2::ABILITY_ID::LOAD_MEDIVAC, target->getUnit_ptr());
}
}
else
{
for (const auto & t : targets)
{
bot.Actions()->UnitCommand(unit->getUnit_ptr(), sc2::ABILITY_ID::SMART, t->getUnit_ptr());
}
}
}
void Micro::SmartRepair(CUnit_ptr unit, CUnit_ptr target, CCBot & bot)
{
BOT_ASSERT(unit != nullptr, "Unit is null");
bot.Actions()->UnitCommand(unit->getUnit_ptr(), sc2::ABILITY_ID::EFFECT_REPAIR, target->getUnit_ptr());
if (!(target->isBuilding()))
{
bot.Actions()->UnitCommand(target->getUnit_ptr(), sc2::ABILITY_ID::MOVE, unit->getPos());
}
}
void Micro::SmartKiteTarget(CUnit_ptr rangedUnit, CUnit_ptr target, CCBot & bot,bool queue)
{
BOT_ASSERT(rangedUnit != nullptr, "RangedUnit is null");
BOT_ASSERT(target != nullptr, "Target is null");
//Distance to target
float dist = Util::Dist(rangedUnit, target);
//Our range
float range = rangedUnit->getAttackRange(target);
bool attack = false;
if (dist > range + 0.5f)
{
attack = true;
}
else if (rangedUnit->getWeaponCooldown() == 0.0f && !(rangedUnit->isType(sc2::UNIT_TYPEID::TERRAN_MARINE) && target->isType(sc2::UNIT_TYPEID::ZERG_BANELING) && dist < 2.0f))
{
const float enemyRange = target->getAttackRange(rangedUnit);
const float speed = rangedUnit->getMovementSpeed();
const float enemySpeed = target->getMovementSpeed();
if (enemyRange < range && enemySpeed < speed && dist < enemyRange + 0.5f)
{
}
else
{
attack = true;
}
}
if (attack)
{
SmartAttackUnit(rangedUnit, target, bot, queue);
}
else
{
auto buffs = rangedUnit->getBuffs();
if (rangedUnit->getHealth() == rangedUnit->getHealthMax() && (buffs.empty() || std::find(buffs.begin(), buffs.end(), sc2::BUFF_ID::STIMPACK) == buffs.end()))
{
sc2::AvailableAbilities abilities = rangedUnit->getAbilities();
for (const auto & ability : abilities.abilities)
{
if (ability.ability_id == sc2::ABILITY_ID::EFFECT_STIM)
{
bot.Actions()->UnitCommand(rangedUnit->getUnit_ptr(), sc2::ABILITY_ID::EFFECT_STIM);
return;
}
}
}
//A normed vector to the target
sc2::Point2D targetPos = rangedUnit->getPos();
//If its a building we want range -1 distance
//The same is true if it outranges us. We dont want to block following units
if (target->isBuilding())
{
targetPos += Util::normalizeVector(target->getPos() - rangedUnit->getPos(), dist - (range - 1));
}
else if (target->getAttackRange(rangedUnit) >= range || (!rangedUnit->canHitMe(target) && rangedUnit->getUnitType().ToType() != sc2::UNIT_TYPEID::TERRAN_VIKINGFIGHTER))
{
targetPos += Util::normalizeVector(target->getPos() - rangedUnit->getPos(), dist - (range - 1));
}
else
{
targetPos += Util::normalizeVector(target->getPos() - rangedUnit->getPos(), dist - range);
}
SmartMove(rangedUnit, targetPos, bot, queue);
}
}
void Micro::SmartBuild(CUnit_ptr builder, const sc2::UnitTypeID & buildingType, sc2::Point2D pos, CCBot & bot)
{
BOT_ASSERT(builder != nullptr, "Builder is null");
bot.Actions()->UnitCommand(builder->getUnit_ptr(), bot.Data(buildingType).buildAbility, pos);
}
void Micro::SmartBuildTarget(CUnit_ptr builder, const sc2::UnitTypeID & buildingType, CUnit_ptr target, CCBot & bot)
{
BOT_ASSERT(builder != nullptr, "Builder is null");
BOT_ASSERT(target != nullptr, "Target is null");
bot.Actions()->UnitCommand(builder->getUnit_ptr(), bot.Data(buildingType).buildAbility, target->getUnit_ptr());
}
/*
void Micro::SmartTrain(CUnit_ptr builder, const sc2::UnitTypeID & buildingType, CCBot & bot)
{
BOT_ASSERT(builder != nullptr, "Builder is null");
bot.Actions()->UnitCommand(builder->getUnit_ptr(), bot.Data(buildingType).buildAbility);
}
*/
void Micro::SmartAbility(CUnit_ptr unit, const sc2::AbilityID & abilityID, CCBot & bot)
{
BOT_ASSERT(unit != nullptr, "Builder is null");
if (unit->getOrders().empty() || unit->getOrders().back().ability_id != abilityID)
{
bot.Actions()->UnitCommand(unit->getUnit_ptr(), abilityID);
}
}
void Micro::SmartAbility(CUnits units, const sc2::AbilityID & abilityID, CCBot & bot,bool queue)
{
bot.Actions()->UnitCommand(CUnits2Units(units), abilityID,queue);
}
void Micro::SmartAbility(CUnit_ptr unit, const sc2::AbilityID & abilityID,const sc2::Point2D pos,CCBot & bot,bool queue)
{
BOT_ASSERT(unit != nullptr, "Builder is null");
if (unit->getOrders().empty() || unit->getOrders().back().ability_id != abilityID || Util::DistSq(unit->getOrders().front().target_pos,pos)>0.01f)
{
bot.Actions()->UnitCommand(unit->getUnit_ptr(), abilityID,pos,queue);
}
}
void Micro::SmartAbility(CUnit_ptr unit, const sc2::AbilityID & abilityID, CUnit_ptr target, CCBot & bot, bool queue)
{
BOT_ASSERT(unit != nullptr, "Builder is null");
if (unit->getOrders().empty() || unit->getOrders().back().ability_id != abilityID || unit->getOrders().back().target_unit_tag != target->getTag())
{
bot.Actions()->UnitCommand(unit->getUnit_ptr(), abilityID, target->getUnit_ptr(), queue);
}
}
void Micro::SmartCDAbility(CUnit_ptr builder, const sc2::AbilityID & abilityID, CCBot & bot, bool queue)
{
BOT_ASSERT(builder != nullptr, "Builder is null");
if (builder->getAbilityCoolDown() <= bot.Observation()->GetGameLoop())
{
sc2::AvailableAbilities abilities = builder->getAbilities();
for (const auto & ability : abilities.abilities)
{
if (ability.ability_id == abilityID)
{
builder->newAbilityCoolDown(bot.Observation()->GetGameLoop() + bot.Data(abilityID));
bot.Actions()->UnitCommand(builder->getUnit_ptr(), abilityID, queue);
return;
}
}
}
}
void Micro::SmartCDAbility(CUnits units, const sc2::AbilityID & abilityID, CCBot & bot, bool queue)
{
sc2::Units targets;
for (const auto & unit : units)
{
if (unit->getAbilityCoolDown() <= bot.Observation()->GetGameLoop())
{
for (const auto & ability : unit->getAbilities().abilities)
{
if (ability.ability_id == abilityID)
{
unit->newAbilityCoolDown(bot.Observation()->GetGameLoop()+bot.Data(abilityID));
targets.push_back(unit->getUnit_ptr());
continue;
}
}
}
}
bot.Actions()->UnitCommand(targets, abilityID,queue);
return;
}
void Micro::SmartStim(CUnits units, CCBot & bot, bool queue)
{
sc2::Units targets;
for (const auto & unit : units)
{
auto buffs = unit->getBuffs();
if (unit->getHealth() == unit->getHealthMax() && (buffs.empty() || std::find(buffs.begin(), buffs.end(), sc2::BUFF_ID::STIMPACK) == buffs.end()))
{
sc2::AvailableAbilities abilities = unit->getAbilities();
for (const auto & ability : abilities.abilities)
{
if (ability.ability_id == sc2::ABILITY_ID::EFFECT_STIM)
{
targets.push_back(unit->getUnit_ptr());
continue;
}
}
}
}
bot.Actions()->UnitCommand(targets, sc2::ABILITY_ID::EFFECT_STIM,queue);
}