-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathRangedManager.cpp
121 lines (100 loc) · 3.32 KB
/
RangedManager.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
#include "RangedManager.h"
#include "Util.h"
#include "CCBot.h"
RangedManager::RangedManager(CCBot & bot)
: MicroManager(bot)
{
}
void RangedManager::executeMicro(const std::vector<Unit> & targets)
{
assignTargets(targets);
}
void RangedManager::assignTargets(const std::vector<Unit> & targets)
{
const std::vector<Unit> & rangedUnits = getUnits();
// figure out targets
std::vector<Unit> rangedUnitTargets;
for (auto target : targets)
{
if (!target.isValid()) { continue; }
if (target.getType().isEgg()) { continue; }
if (target.getType().isLarva()) { continue; }
rangedUnitTargets.push_back(target);
}
// for each meleeUnit
for (auto rangedUnit : rangedUnits)
{
BOT_ASSERT(rangedUnit.isValid(), "ranged unit is null");
// if the order is to attack or defend
if (order.getType() == SquadOrderTypes::Attack || order.getType() == SquadOrderTypes::Defend)
{
if (!rangedUnitTargets.empty())
{
// find the best target for this meleeUnit
Unit target = getTarget(rangedUnit, rangedUnitTargets);
// attack it
if (m_bot.Config().KiteWithRangedUnits)
{
// TODO: implement kiting
rangedUnit.attackUnit(target);
}
else
{
rangedUnit.attackUnit(target);
}
}
// if there are no targets
else
{
// if we're not near the order position
if (Util::Dist(rangedUnit, order.getPosition()) > 4)
{
// move to it
rangedUnit.move(order.getPosition());
}
}
}
if (m_bot.Config().DrawUnitTargetInfo)
{
// TODO: draw the line to the unit's target
}
}
}
// get a target for the ranged unit to attack
// TODO: this is the melee targeting code, replace it with something better for ranged units
Unit RangedManager::getTarget(const Unit & rangedUnit, const std::vector<Unit> & targets)
{
BOT_ASSERT(rangedUnit.isValid(), "null melee unit in getTarget");
int highPriority = 0;
double closestDist = std::numeric_limits<double>::max();
Unit closestTarget;
// for each target possiblity
for (auto & targetUnit : targets)
{
BOT_ASSERT(targetUnit.isValid(), "null target unit in getTarget");
int priority = getAttackPriority(rangedUnit, targetUnit);
float distance = Util::Dist(rangedUnit, targetUnit);
// if it's a higher priority, or it's closer, set it
if (!closestTarget.isValid() || (priority > highPriority) || (priority == highPriority && distance < closestDist))
{
closestDist = distance;
highPriority = priority;
closestTarget = targetUnit;
}
}
return closestTarget;
}
// get the attack priority of a type in relation to a zergling
int RangedManager::getAttackPriority(const Unit & attacker, const Unit & target)
{
BOT_ASSERT(target.isValid(), "null unit in getAttackPriority");
if (target.getType().isCombatUnit())
{
return 10;
}
if (target.getType().isWorker())
{
return 9;
}
return 1;
}