forked from yanntm/YoBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseLocation.cpp
More file actions
243 lines (199 loc) · 6.58 KB
/
BaseLocation.cpp
File metadata and controls
243 lines (199 loc) · 6.58 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
#include "BaseLocation.h"
#include "Util.h"
#include "YoAgent.h"
#include <sstream>
#include <iostream>
const int NearBaseLocationTileDistance = 20;
BaseLocation::BaseLocation(YoAgent & bot, int baseID, const std::vector<const sc2::Unit *> & resources)
: m_bot(bot)
, m_baseID(baseID)
, m_isStartLocation(false)
, m_left(std::numeric_limits<float>::max())
, m_right(std::numeric_limits<float>::lowest())
, m_top(std::numeric_limits<float>::lowest())
, m_bottom(std::numeric_limits<float>::max())
{
m_isPlayerStartLocation[0] = false;
m_isPlayerStartLocation[1] = false;
m_isPlayerOccupying[0] = false;
m_isPlayerOccupying[1] = false;
float resourceCenterX = 0;
float resourceCenterY = 0;
// add each of the resources to its corresponding container
for (auto resource : resources)
{
if (Util::IsMineral(resource))
{ //if (resource->display_type == 1) { //if Visible(1) to Player //not Snapshot(2) int i = 1; //just a place for a Breakpoint }
m_minerals.push_back(resource);
m_mineralPositions.push_back(resource->pos);
// add the position of the minerals to the center
resourceCenterX += resource->pos.x;
resourceCenterY += resource->pos.y;
}
else
{
m_geysers.push_back(resource);
m_geyserPositions.push_back(resource->pos);
// pull the resource center toward the geyser if it exists
resourceCenterX += resource->pos.x;
resourceCenterY += resource->pos.y;
}
// set the limits of the base location bounding box
float resWidth = 1;
float resHeight = 0.5;
m_left = std::min(m_left, resource->pos.x - resWidth);
m_right = std::max(m_right, resource->pos.x + resWidth);
m_top = std::max(m_top, resource->pos.y + resHeight);
m_bottom = std::min(m_bottom, resource->pos.y - resHeight);
}
// calculate the center of the resources
size_t numResources = m_minerals.size() + m_geysers.size();
m_centerOfResources = sc2::Point2D(m_left + (m_right - m_left) / 2.0f, m_top + (m_bottom - m_top) / 2.0f);
// compute this BaseLocation's DistanceMap, which will compute the ground distance
// from the center of its recourses to every other tile on the map
m_distanceMap = m_bot.m_map.getDistanceMap(m_centerOfResources); //THIS MAY BE VERY EXPENSIVE TO DO AT THE VERY START
// check to see if this is a start location for the map
for (auto & pos : m_bot.Observation()->GetGameInfo().enemy_start_locations)
{
if (containsPosition(pos)) //if within 20 tiles of a start location
{
m_isStartLocation = true;
m_depotPosition = pos;
}
}
// if this base location position is near our own resource depot, it's our start location
for (auto & unit : m_bot.Observation()->GetUnits())
{
if (Util::GetPlayer(unit) == Players::Self && Util::IsTownHall(unit) && containsPosition(unit->pos))
{
m_isPlayerStartLocation[Players::Self] = true;
m_isStartLocation = true;
m_isPlayerOccupying[Players::Self] = true;
break;
}
}
// if it's not a start location, we need to calculate the depot position
if (!isStartLocation())
{
// the position of the depot will be the closest spot we can build one from the resource center
for (auto & tile : getClosestTiles())
{
// TODO: m_depotPosition = depot position for this base location
}
}
}
// TODO: calculate the actual depot position
const sc2::Point2D & BaseLocation::getDepotPosition() const
{
return getPosition();
}
void BaseLocation::setPlayerOccupying(int player, bool occupying)
{
m_isPlayerOccupying[player] = occupying;
// if this base is a start location that's occupied by the enemy, it's that enemy's start location
if (occupying && player == Players::Enemy && isStartLocation() && m_isPlayerStartLocation[player] == false)
{
m_isPlayerStartLocation[player] = true;
}
}
bool BaseLocation::isInResourceBox(int x, int y) const
{
return x >= m_left && x < m_right && y < m_top && y >= m_bottom;
}
bool BaseLocation::isOccupiedByPlayer(int player) const
{
return m_isPlayerOccupying.at(player);
}
bool BaseLocation::isExplored() const
{
return false;
}
bool BaseLocation::isPlayerStartLocation(int player) const
{
return m_isPlayerStartLocation.at(player);
}
bool BaseLocation::containsPosition(const sc2::Point2D & pos) const
{
if (!m_bot.m_map.isValid(pos) || (pos.x == 0 && pos.y == 0))
{
return false;
}
return getGroundDistance(pos) < NearBaseLocationTileDistance;
}
const std::vector<const sc2::Unit *> & BaseLocation::getGeysers() const
{
return m_geysers;
}
const std::vector<const sc2::Unit *> & BaseLocation::getMinerals() const
{
return m_minerals;
}
const sc2::Point2D & BaseLocation::getPosition() const
{
return m_centerOfResources;
}
int BaseLocation::getGroundDistance(const sc2::Point2D & pos) const
{
//return Util::Dist(pos, m_centerOfResources);
return m_distanceMap.getDistance(pos);
}
bool BaseLocation::isStartLocation() const
{
return m_isStartLocation;
}
const std::vector<sc2::Point2D> & BaseLocation::getClosestTiles() const
{
return m_distanceMap.getSortedTiles();
}
void BaseLocation::draw()
{
m_bot.m_map.drawSphere(m_centerOfResources, 1.0f, sc2::Colors::Yellow);
std::stringstream ss;
ss << "BaseLocation: " << m_baseID << "\n";
ss << "Start Loc: " << (isStartLocation() ? "true" : "false") << "\n";
ss << "Minerals: " << m_mineralPositions.size() << "\n";
ss << "Geysers: " << m_geyserPositions.size() << "\n";
ss << "Occupied By: ";
if (isOccupiedByPlayer(Players::Self))
{
ss << "Self ";
}
if (isOccupiedByPlayer(Players::Enemy))
{
ss << "Enemy ";
}
m_bot.m_map.drawText(sc2::Point2D(m_left, m_top + 3), ss.str().c_str());
m_bot.m_map.drawText(sc2::Point2D(m_left, m_bottom), ss.str().c_str());
// draw the base bounding box
m_bot.m_map.drawBox(m_left, m_top, m_right, m_bottom);
for (float x = m_left; x < m_right; ++x)
{
m_bot.m_map.drawLine(x, m_top, x, m_bottom, sc2::Color(160, 160, 160));
}
for (float y = m_bottom; y<m_top; ++y)
{
m_bot.m_map.drawLine(m_left, y, m_right, y, sc2::Color(160, 160, 160));
}
for (auto & mineralPos : m_mineralPositions)
{
m_bot.m_map.drawSphere(mineralPos, 1.0f, sc2::Colors::Teal);
}
for (auto & geyserPos : m_geyserPositions)
{
m_bot.m_map.drawSphere(geyserPos, 1.0f, sc2::Colors::Green);
}
if (m_isStartLocation)
{
m_bot.m_map.drawSphere(m_depotPosition, 1.0f, sc2::Colors::Red);
}
float ccWidth = 5;
float ccHeight = 4;
sc2::Point2D boxtl = m_depotPosition - sc2::Point2D(ccWidth / 2, -ccHeight / 2);
sc2::Point2D boxbr = m_depotPosition + sc2::Point2D(ccWidth / 2, -ccHeight / 2);
m_bot.m_map.drawBox(boxtl.x, boxtl.y, boxbr.x, boxbr.y, sc2::Colors::Red);
//m_distanceMap.draw(m_bot);
}
bool BaseLocation::isMineralOnly() const
{
return getGeysers().empty();
}