-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathSquad.cpp
300 lines (248 loc) · 6.28 KB
/
Squad.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
#include "Squad.h"
#include "CCBot.h"
#include "Util.h"
Squad::Squad(CCBot & bot)
: m_bot(bot)
, m_lastRetreatSwitch(0)
, m_lastRetreatSwitchVal(false)
, m_priority(0)
, m_name("Default")
, m_meleeManager(bot)
, m_rangedManager(bot)
{
}
Squad::Squad(const std::string & name, const SquadOrder & order, size_t priority, CCBot & bot)
: m_bot(bot)
, m_name(name)
, m_order(order)
, m_lastRetreatSwitch(0)
, m_lastRetreatSwitchVal(false)
, m_priority(priority)
, m_meleeManager(bot)
, m_rangedManager(bot)
{
}
void Squad::onFrame()
{
// update all necessary unit information within this squad
updateUnits();
// determine whether or not we should regroup
bool needToRegroup = needsToRegroup();
// if we do need to regroup, do it
if (needToRegroup)
{
CCPosition regroupPosition = calcRegroupPosition();
m_bot.Map().drawCircle(regroupPosition, 3, CCColor(255, 0, 255));
m_meleeManager.regroup(regroupPosition);
m_rangedManager.regroup(regroupPosition);
}
else // otherwise, execute micro
{
m_meleeManager.execute(m_order);
m_rangedManager.execute(m_order);
//_detectorManager.setUnitClosestToEnemy(unitClosestToEnemy());
//_detectorManager.execute(_order);
}
}
bool Squad::isEmpty() const
{
return m_units.empty();
}
size_t Squad::getPriority() const
{
return m_priority;
}
void Squad::setPriority(const size_t & priority)
{
m_priority = priority;
}
void Squad::updateUnits()
{
setAllUnits();
setNearEnemyUnits();
addUnitsToMicroManagers();
}
void Squad::setAllUnits()
{
// clean up the _units vector just in case one of them died
std::set<Unit> goodUnits;
for (auto unit : m_units)
{
if (!unit.isValid()) { continue; }
if (unit.isBeingConstructed()) { continue; }
goodUnits.insert(unit);
}
m_units = goodUnits;
}
void Squad::setNearEnemyUnits()
{
m_nearEnemy.clear();
for (auto unit : m_units)
{
m_nearEnemy[unit] = isUnitNearEnemy(unit);
CCColor color = m_nearEnemy[unit] ? m_bot.Config().ColorUnitNearEnemy : m_bot.Config().ColorUnitNotNearEnemy;
//m_bot.Map().drawCircleAroundUnit(unitTag, color);
}
}
void Squad::addUnitsToMicroManagers()
{
std::vector<Unit> meleeUnits;
std::vector<Unit> rangedUnits;
std::vector<Unit> detectorUnits;
std::vector<Unit> transportUnits;
std::vector<Unit> tankUnits;
// add _units to micro managers
for (auto unit : m_units)
{
BOT_ASSERT(unit.isValid(), "null unit in addUnitsToMicroManagers()");
if (unit.getType().isTank())
{
tankUnits.push_back(unit);
}
// TODO: detectors
else if (unit.getType().isDetector() && !unit.getType().isBuilding())
{
detectorUnits.push_back(unit);
}
// select ranged _units
else if (unit.getType().getAttackRange() >= 1.5f)
{
rangedUnits.push_back(unit);
}
// select melee _units
else if (unit.getType().getAttackRange() < 1.5f)
{
meleeUnits.push_back(unit);
}
}
m_meleeManager.setUnits(meleeUnits);
m_rangedManager.setUnits(rangedUnits);
//m_detectorManager.setUnits(detectorUnits);
//m_tankManager.setUnits(tankUnits);
}
// TODO: calculates whether or not to regroup
bool Squad::needsToRegroup() const
{
return false;
}
void Squad::setSquadOrder(const SquadOrder & so)
{
m_order = so;
}
bool Squad::containsUnit(const Unit & unit) const
{
return std::find(m_units.begin(), m_units.end(), unit) != m_units.end();
}
void Squad::clear()
{
for (auto unit : getUnits())
{
BOT_ASSERT(unit.isValid(), "null unit in squad clear");
if (unit.getType().isWorker())
{
m_bot.Workers().finishedWithWorker(unit);
}
}
m_units.clear();
}
bool Squad::isUnitNearEnemy(const Unit & unit) const
{
BOT_ASSERT(unit.isValid(), "null unit in squad");
for (auto & u : m_bot.GetUnits())
{
if ((u.getPlayer() == Players::Enemy) && (Util::Dist(unit, u) < 20))
{
return true;
}
}
return false;
}
CCPosition Squad::calcCenter() const
{
if (m_units.empty())
{
return CCPosition(0, 0);
}
CCPosition sum(0, 0);
for (auto unit: m_units)
{
BOT_ASSERT(unit.isValid(), "null unit in squad calcCenter");
sum += unit.getPosition();
}
return CCPosition(sum.x / m_units.size(), sum.y / m_units.size());
}
CCPosition Squad::calcRegroupPosition() const
{
CCPosition regroup(0, 0);
float minDist = std::numeric_limits<float>::max();
for (auto unit : m_units)
{
if (!m_nearEnemy.at(unit))
{
float dist = Util::Dist(m_order.getPosition(), unit.getPosition());
if (dist < minDist)
{
minDist = dist;
regroup = unit.getPosition();
}
}
}
if (regroup.x == 0.0f && regroup.y == 0.0f)
{
return m_bot.GetStartLocation();
}
else
{
return regroup;
}
}
Unit Squad::unitClosestToEnemy() const
{
Unit closest;
float closestDist = std::numeric_limits<float>::max();
for (auto & unit : m_units)
{
BOT_ASSERT(unit.isValid(), "null unit");
// the distance to the order position
int dist = m_bot.Map().getGroundDistance(unit.getPosition(), m_order.getPosition());
if (dist != -1 && (!closest.isValid() || dist < closestDist))
{
closest = unit;
closestDist = (float)dist;
}
}
return closest;
}
int Squad::squadUnitsNear(const CCPosition & p) const
{
int numUnits = 0;
for (auto unit : m_units)
{
BOT_ASSERT(unit.isValid(), "null unit");
if (Util::Dist(unit, p) < 20.0f)
{
numUnits++;
}
}
return numUnits;
}
const std::set<Unit> & Squad::getUnits() const
{
return m_units;
}
const SquadOrder & Squad::getSquadOrder() const
{
return m_order;
}
void Squad::addUnit(const Unit & unit)
{
m_units.insert(unit);
}
void Squad::removeUnit(const Unit & unit)
{
m_units.erase(unit);
}
const std::string & Squad::getName() const
{
return m_name;
}