forked from yanntm/YoBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BaseLocationManager.cpp
321 lines (257 loc) · 9.04 KB
/
BaseLocationManager.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
#include "BaseLocationManager.h"
#include "Util.h"
#include "YoAgent.h"
BaseLocationManager::BaseLocationManager(YoAgent & bot)
: m_bot(bot)
{
}
void BaseLocationManager::onStart()
{
m_tileBaseLocations = std::vector<std::vector<BaseLocation *>>(m_bot.m_map.width(), std::vector<BaseLocation *>(m_bot.m_map.height(), nullptr));
m_playerStartingBaseLocations[Players::Self] = nullptr;
m_playerStartingBaseLocations[Players::Enemy] = nullptr;
// a BaseLocation will be anything where there are minerals to mine
// so we will first look over all minerals and cluster them based on some distance
const int clusterDistance = 14;
// stores each cluster of resources based on some ground distance
std::vector<std::vector<const sc2::Unit *>> resourceClusters;
for (auto mineral : m_bot.Observation()->GetUnits(sc2::Unit::Alliance::Neutral))
{
// skip minerals that don't have more than 100 starting minerals
// these are probably stupid map-blocking minerals to confuse us
if (!Util::IsMineral(mineral)) { continue; }
bool foundCluster = false;
for (auto & cluster : resourceClusters)
{
float dist = Util::Dist(mineral->pos, Util::CalcCenter(cluster));
// quick initial air distance check to eliminate most resources
if (dist < clusterDistance)
{
// now do a more expensive ground distance check
float groundDist = dist; //m_bot.m_map.getGroundDistance(mineral.pos, Util::CalcCenter(cluster));
if (groundDist >= 0 && groundDist < clusterDistance)
{
cluster.push_back(mineral);
foundCluster = true;
break;
}
}
}
if (!foundCluster)
{
resourceClusters.push_back(std::vector<const sc2::Unit *>());
resourceClusters.back().push_back(mineral);
}
}
// add geysers only to existing resource clusters
for (auto & geyser : m_bot.Observation()->GetUnits(sc2::Unit::Alliance::Neutral))
{
if (!Util::IsGeyser(geyser))
{
continue;
}
for (auto & cluster : resourceClusters)
{
//int groundDist = m_bot.m_map.getGroundDistance(geyser.pos, Util::CalcCenter(cluster));
float groundDist = Util::Dist(geyser->pos, Util::CalcCenter(cluster));
if (groundDist >= 0 && groundDist < clusterDistance)
{
cluster.push_back(geyser);
break;
}
}
}
// add the base locations if there are more than 4 resouces in the cluster
int baseID = 0;
for (auto & cluster : resourceClusters)
{
if (cluster.size() > 4)
{
m_baseLocationData.push_back(BaseLocation(m_bot, baseID++, cluster));
}
}
// construct the vectors of base location pointers, this is safe since they will never change
for (auto & baseLocation : m_baseLocationData)
{
m_baseLocationPtrs.push_back(&baseLocation);
// if it's a start location, add it to the start locations
if (baseLocation.isStartLocation())
{
m_startingBaseLocations.push_back(&baseLocation);
}
// if it's our starting location, set the pointer
if (baseLocation.isPlayerStartLocation(Players::Self))
{
m_playerStartingBaseLocations[Players::Self] = &baseLocation;
}
if (baseLocation.isPlayerStartLocation(Players::Enemy))
{
m_playerStartingBaseLocations[Players::Enemy] = &baseLocation;
}
}
// construct the map of tile positions to base locations
for (float x = 0; x < m_bot.m_map.width(); ++x)
{
for (int y = 0; y < m_bot.m_map.height(); ++y)
{
for (auto & baseLocation : m_baseLocationData)
{
sc2::Point2D pos(x + 0.5f, y + 0.5f);
if (baseLocation.containsPosition(pos))
{
m_tileBaseLocations[(int)x][(int)y] = &baseLocation;
break;
}
}
}
}
// construct the sets of occupied base locations
m_occupiedBaseLocations[Players::Self] = std::set<const BaseLocation *>();
m_occupiedBaseLocations[Players::Enemy] = std::set<const BaseLocation *>();
}
void BaseLocationManager::onFrame()
{
drawBaseLocations();
// reset the player occupation information for each location
for (auto & baseLocation : m_baseLocationData)
{
baseLocation.setPlayerOccupying(Players::Self, false);
baseLocation.setPlayerOccupying(Players::Enemy, false); //TODO: why is this called twice???
}
// for each unit on the map, update which base location it may be occupying
for (auto & unit : m_bot.Observation()->GetUnits(sc2::Unit::Alliance::Ally))
{
// we only care about buildings on the ground
if (!Util::IsBuilding(unit->unit_type) || unit->is_flying) continue;
BaseLocation * baseLocation = getBaseLocation(unit->pos);
if (baseLocation != nullptr)
baseLocation->setPlayerOccupying(Util::GetPlayer(unit), true);
}
// update enemy base occupations
for (const auto & kv : m_bot.m_unitInfoManager.getUnitInfoMap(Players::Enemy))
{
const UnitInfo & ui = kv.second;
if (!Util::IsBuilding(ui.type)) continue;
BaseLocation * baseLocation = getBaseLocation(ui.lastPosition);
if (baseLocation != nullptr)
baseLocation->setPlayerOccupying(Players::Enemy, true);
}
// update the starting locations of the enemy player
// this will happen one of two ways:
// 1. we've seen the enemy base directly, so the baselocation will know
if (m_playerStartingBaseLocations[Players::Enemy] == nullptr)
{
for (auto & baseLocation : m_baseLocationData)
{
if (baseLocation.isPlayerStartLocation(Players::Enemy))
m_playerStartingBaseLocations[Players::Enemy] = &baseLocation;
}
}
// 2. we've explored every other start location and haven't seen the enemy yet
if (m_playerStartingBaseLocations[Players::Enemy] == nullptr)
{
int numStartLocations = (int)getStartingBaseLocations().size();
int numExploredLocations = 0;
BaseLocation * unexplored = nullptr;
for (auto & baseLocation : m_baseLocationData)
{
if (!baseLocation.isStartLocation()) continue;
if (baseLocation.isExplored())
numExploredLocations++;
else
unexplored = &baseLocation;
}
// if we have explored all but one location, then the unexplored one is the enemy start location
if (numExploredLocations == numStartLocations - 1 && unexplored != nullptr)
{
m_playerStartingBaseLocations[Players::Enemy] = unexplored;
unexplored->setPlayerOccupying(Players::Enemy, true);
}
}
// update the occupied base locations for each player
m_occupiedBaseLocations[Players::Self] = std::set<const BaseLocation *>();
m_occupiedBaseLocations[Players::Enemy] = std::set<const BaseLocation *>();
for (auto & baseLocation : m_baseLocationData)
{
if (baseLocation.isOccupiedByPlayer(Players::Self))
m_occupiedBaseLocations[Players::Self].insert(&baseLocation);
if (baseLocation.isOccupiedByPlayer(Players::Enemy))
m_occupiedBaseLocations[Players::Enemy].insert(&baseLocation);
}
// draw the debug information for each base location
}
BaseLocation * BaseLocationManager::getBaseLocation(const sc2::Point2D & pos) const
{
if (!m_bot.m_map.isValid(pos)) { return nullptr; }
return m_tileBaseLocations[(int)pos.x][(int)pos.y];
}
void BaseLocationManager::drawBaseLocations()
{
//if (!m_bot.Config().DrawBaseLocationInfo)
return;
for (auto & baseLocation : m_baseLocationData)
{
baseLocation.draw();
for (auto & baseLocation2 : m_baseLocationData)
{
if (baseLocation2.getGroundDistance(baseLocation.getDepotPosition()) < 70) {
m_bot.m_map.drawLine(baseLocation.getDepotPosition(), baseLocation2.getDepotPosition());
}
}
}
// draw a purple sphere at the next expansion location
sc2::Point2D nextExpansionPosition = getNextExpansion(Players::Self);
m_bot.m_map.drawSphere(nextExpansionPosition, 1, sc2::Colors::Purple);
m_bot.m_map.drawText(nextExpansionPosition, "Next Expansion Location", sc2::Colors::Purple);
}
const std::vector<const BaseLocation *> & BaseLocationManager::getBaseLocations() const
{
return m_baseLocationPtrs;
}
const std::vector<const BaseLocation *> & BaseLocationManager::getStartingBaseLocations() const
{
return m_startingBaseLocations;
}
const BaseLocation * BaseLocationManager::getPlayerStartingBaseLocation(int player) const
{
return m_playerStartingBaseLocations.at(player);
}
const std::set<const BaseLocation *> & BaseLocationManager::getOccupiedBaseLocations(int player) const
{
return m_occupiedBaseLocations.at(player);
}
sc2::Point2D BaseLocationManager::getNextExpansion(int player) const
{
const BaseLocation * homeBase = getPlayerStartingBaseLocation(player);
const BaseLocation * closestBase = nullptr;
int minDistance = INT_MAX; //std::numeric_limits<int>::max();
sc2::Point2D homeTile = homeBase->getPosition();
for (auto & base : getBaseLocations())
{
// skip mineral only and starting locations (TODO: fix this)
if (base->isMineralOnly() || base->isStartLocation())
{
continue;
}
// get the tile position of the base
auto tile = base->getDepotPosition();
bool buildingInTheWay = false; // TODO: check if there are any units on the tile
if (buildingInTheWay)
{
continue;
}
// the base's distance from our main nexus
int distanceFromHome = homeBase->getGroundDistance(tile);
// if it is not connected, continue
if (distanceFromHome < 0)
{
continue;
}
if (!closestBase || distanceFromHome < minDistance)
{
closestBase = base;
minDistance = distanceFromHome;
}
}
return closestBase ? closestBase->getDepotPosition() : sc2::Point2D(0.0f, 0.0f);
}