forked from yanntm/YoBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSquad.cpp
More file actions
298 lines (246 loc) · 5.96 KB
/
Squad.cpp
File metadata and controls
298 lines (246 loc) · 5.96 KB
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
#include "Squad.h"
#include "YoAgent.h"
#include "Util.h"
Squad::Squad(YoAgent & 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, YoAgent & 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(); //should probably be if enemy mineral+gas armyCost is >2 times ours within a certain radius
// if we do need to regroup, do it
if (needToRegroup)
{
sc2::Point2D regroupPosition = calcRegroupPosition();
m_bot.m_map.drawSphere(regroupPosition, 3, sc2::Colors::Purple);
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<const sc2::Unit *> goodUnits;
for (auto unit : m_units)
{
if (!unit) { continue; }
if (unit->build_progress < 1.0f) { continue; }
if (unit->health <= 0) { continue; }
goodUnits.insert(unit);
}
m_units = goodUnits;
}
void Squad::setNearEnemyUnits()
{
m_nearEnemy.clear();
for (auto unit : m_units)
{
m_nearEnemy[unit] = isUnitNearEnemy(unit);
//sc2::Color color = m_nearEnemy[unit] ? m_bot.Config().ColorUnitNearEnemy : m_bot.Config().ColorUnitNotNearEnemy;
//m_bot.Map().drawSphereAroundUnit(unitTag, color);
}
}
void Squad::addUnitsToMicroManagers()
{
std::vector<const sc2::Unit *> meleeUnits;
std::vector<const sc2::Unit *> rangedUnits;
std::vector<const sc2::Unit *> detectorUnits;
std::vector<const sc2::Unit *> transportUnits;
std::vector<const sc2::Unit *> tankUnits;
// add _units to micro managers
for (auto unit : m_units)
{
//BOT_ASSERT(unit, "null unit in addUnitsToMicroManagers()");
if (unit->unit_type == sc2::UNIT_TYPEID::TERRAN_SIEGETANK || unit->unit_type == sc2::UNIT_TYPEID::TERRAN_SIEGETANKSIEGED)
{
tankUnits.push_back(unit);
}
// TODO: detectors //m_bot.Observation()->GetUnitTypeData()[kv.first]
else if (Util::IsDetector(unit) && !Util::IsBuilding(unit->unit_type))
{
detectorUnits.push_back(unit);
}
// select ranged _units
else if (Util::GetAttackRange(unit->unit_type, m_bot) >= 1.5f)
{
rangedUnits.push_back(unit);
}
// select melee _units
else if (Util::GetAttackRange(unit->unit_type, m_bot) < 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 sc2::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, "null unit in squad clear");
//if (Util::IsWorker(unit)) m_bot.Workers().finishedWithWorker(unit);} //won't use this with workers right away
}
m_units.clear();
}
bool Squad::isUnitNearEnemy(const sc2::Unit * unit) const
{
//BOT_ASSERT(unit, "null unit in squad");
for (auto & u : m_bot.Observation()->GetUnits())
{
if ((Util::GetPlayer(u) == Players::Enemy) && (Util::Dist(unit->pos, u->pos) < 20))
{
return true;
}
}
return false;
}
sc2::Point2D Squad::calcCenter() const
{
if (m_units.empty())
{
return sc2::Point2D(0.0f, 0.0f);
}
sc2::Point2D sum(0, 0);
for (auto unit : m_units)
{
//BOT_ASSERT(unit, "null unit in squad calcCenter");
sum += unit->pos;
}
return sc2::Point2D(sum.x / m_units.size(), sum.y / m_units.size());
}
sc2::Point2D Squad::calcRegroupPosition() const
{
sc2::Point2D regroup(0.0f, 0.0f);
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->pos);
if (dist < minDist)
{
minDist = dist;
regroup = unit->pos;
}
}
}
if (regroup.x == 0.0f && regroup.y == 0.0f)
{
return m_bot.Observation()->GetStartLocation();
}
else
{
return regroup;
}
}
const sc2::Unit * Squad::unitClosestToEnemy() const
{
const sc2::Unit * closest = nullptr;
float closestDist = std::numeric_limits<float>::max();
for (auto unit : m_units)
{
//BOT_ASSERT(unit, "null unit");
// the distance to the order position
int dist = m_bot.m_map.getGroundDistance(unit->pos, m_order.getPosition());
if (dist != -1 && (!closest || dist < closestDist))
{
closest = unit;
closestDist = (float)dist;
}
}
return closest;
}
int Squad::squadUnitsNear(const sc2::Point2D & p) const
{
int numUnits = 0;
for (auto unit : m_units)
{
//BOT_ASSERT(unit, "null unit");
if (Util::Dist(unit->pos, p) < 20.0f)
{
numUnits++;
}
}
return numUnits;
}
const std::set<const sc2::Unit *> & Squad::getUnits() const
{
return m_units;
}
const SquadOrder & Squad::getSquadOrder() const
{
return m_order;
}
void Squad::addUnit(const sc2::Unit * unit)
{
m_units.insert(unit);
}
void Squad::removeUnit(const sc2::Unit * unit)
{
m_units.erase(unit);
}
const std::string & Squad::getName() const
{
return m_name;
}