forked from yanntm/YoBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnitInfoManager.cpp
More file actions
284 lines (236 loc) · 7.06 KB
/
UnitInfoManager.cpp
File metadata and controls
284 lines (236 loc) · 7.06 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
#include "UnitInfoManager.h"
#include "Util.h"
#include "YoAgent.h"
#include <sstream>
UnitInfoManager::UnitInfoManager(YoAgent & bot)
: m_bot(bot)
{
}
void UnitInfoManager::onStart()
{
updateUnitInfo();
}
void UnitInfoManager::onFrame()
{
updateUnitInfo();
drawUnitInformation(100, 100); //not sure what this does
//drawSelectedUnitDebugInfo(); //draws UNIT_ABILITIES on the cursor-selected unit
}
void UnitInfoManager::updateUnitInfo()
{
m_units[Players::Self].clear();
m_units[Players::Enemy].clear();
for (auto & unit : m_bot.Observation()->GetUnits())
{
if (Util::GetPlayer(unit) == Players::Self || Util::GetPlayer(unit) == Players::Enemy)
{
updateUnit(unit);
m_units[Util::GetPlayer(unit)].push_back(unit);
}
}
// remove bad enemy units
m_unitData[Players::Self].removeBadUnits();
m_unitData[Players::Enemy].removeBadUnits();
}
const std::map<const sc2::Unit *, UnitInfo> & UnitInfoManager::getUnitInfoMap(int player) const
{
return getUnitData(player).getUnitInfoMap();
}
const std::vector<const sc2::Unit *> & UnitInfoManager::getUnits(int player) const
{
//BOT_ASSERT(m_units.find(player) != m_units.end(), "Couldn't find player units: %d", player);
return m_units.at(player);
}
static std::string GetAbilityText(sc2::AbilityID ability_id) {
std::string str;
str += sc2::AbilityTypeToName(ability_id);
str += " (";
str += std::to_string(uint32_t(ability_id));
str += ")";
return str;
}
void UnitInfoManager::drawSelectedUnitDebugInfo()
{
const sc2::Unit * unit = nullptr;
for (auto u : m_bot.Observation()->GetUnits())
{
if (u->is_selected && u->alliance == sc2::Unit::Self) {
unit = u;
break;
}
}
if (!unit) { return; }
auto debug = m_bot.Debug();
auto query = m_bot.Query();
auto abilities = m_bot.Observation()->GetAbilityData();
std::string debug_txt;
debug_txt = UnitTypeToName(unit->unit_type);
if (debug_txt.length() < 1)
{
debug_txt = "(Unknown name)";
assert(0);
}
debug_txt += " (" + std::to_string(unit->unit_type) + ")";
sc2::AvailableAbilities available_abilities = query->GetAbilitiesForUnit(unit);
if (available_abilities.abilities.size() < 1)
{
std::cout << "No abilities available for this unit" << std::endl;
}
else
{
for (const sc2::AvailableAbility & available_ability : available_abilities.abilities)
{
if (available_ability.ability_id >= abilities.size()) { continue; }
const sc2::AbilityData & ability = abilities[available_ability.ability_id];
debug_txt += GetAbilityText(ability.ability_id) + "\n";
}
}
debug->DebugTextOut(debug_txt, unit->pos, sc2::Colors::Green);
// Show the direction of the unit.
sc2::Point3D p1; // Use this to show target distance.
{
const float length = 5.0f;
sc2::Point3D p0 = unit->pos;
p0.z += 0.1f; // Raise the line off the ground a bit so it renders more clearly.
p1 = unit->pos;
assert(unit->facing >= 0.0f && unit->facing < 6.29f);
p1.x += length * std::cos(unit->facing);
p1.y += length * std::sin(unit->facing);
debug->DebugLineOut(p0, p1, sc2::Colors::Yellow);
}
// Box around the unit.
{
sc2::Point3D p_min = unit->pos;
p_min.x -= 2.0f;
p_min.y -= 2.0f;
p_min.z -= 2.0f;
sc2::Point3D p_max = unit->pos;
p_max.x += 2.0f;
p_max.y += 2.0f;
p_max.z += 2.0f;
debug->DebugBoxOut(p_min, p_max, sc2::Colors::Blue);
}
// Sphere around the unit.
{
sc2::Point3D p = unit->pos;
p.z += 0.1f; // Raise the line off the ground a bit so it renders more clearly.
debug->DebugSphereOut(p, 1.25f, sc2::Colors::Purple);
}
// Pathing query to get the target.
bool has_target = false;
sc2::Point3D target;
std::string target_info;
for (const sc2::UnitOrder& unit_order : unit->orders)
{
// TODO: Need to determine if there is a target point, no target point, or the target is a unit/snapshot.
target.x = unit_order.target_pos.x;
target.y = unit_order.target_pos.y;
target.z = p1.z;
has_target = true;
target_info = "Target:\n";
if (unit_order.target_unit_tag != 0x0LL) {
target_info += "Tag: " + std::to_string(unit_order.target_unit_tag) + "\n";
}
if (unit_order.progress != 0.0f && unit_order.progress != 1.0f) {
target_info += "Progress: " + std::to_string(unit_order.progress) + "\n";
}
// Perform the pathing query.
{
float distance = query->PathingDistance(unit->pos, target);
target_info += "\nPathing dist: " + std::to_string(distance);
}
break;
}
if (has_target)
{
sc2::Point3D p = target;
p.z += 0.1f; // Raise the line off the ground a bit so it renders more clearly.
debug->DebugSphereOut(target, 1.25f, sc2::Colors::Blue);
debug->DebugTextOut(target_info, p1, sc2::Colors::Yellow);
}
}
// passing in a unit type of 0 returns a count of all units
size_t UnitInfoManager::getUnitTypeCount(int player, sc2::UnitTypeID type, bool completed) const
{
size_t count = 0;
for (auto & unit : getUnits(player))
{
if ((!type || type == unit->unit_type) && (!completed || unit->build_progress == 1.0f))
{
count++;
}
}
return count;
}
//not sure what this does
void UnitInfoManager::drawUnitInformation(float x, float y) const
{
//if (!m_bot.Config().DrawEnemyUnitInfo) return;
std::stringstream ss;
// for each unit in the queue
for (int t(0); t < 255; t++)
{
int numUnits = m_unitData.at(Players::Self).getNumUnits(t);
int numDeadUnits = m_unitData.at(Players::Enemy).getNumDeadUnits(t);
// if there exist units in the vector
if (numUnits > 0)
{
ss << numUnits << " " << numDeadUnits << " " << sc2::UnitTypeToName(t) << "\n";
}
}
for (auto & kv : getUnitData(Players::Enemy).getUnitInfoMap())
{
m_bot.Debug()->DebugSphereOut(kv.second.lastPosition, 0.5f);
m_bot.Debug()->DebugTextOut(sc2::UnitTypeToName(kv.second.type), kv.second.lastPosition);
}
}
void UnitInfoManager::updateUnit(const sc2::Unit * unit)
{
if (!(Util::GetPlayer(unit) == Players::Self || Util::GetPlayer(unit) == Players::Enemy))
{
return;
}
m_unitData[Util::GetPlayer(unit)].updateUnit(unit);
}
// is the unit valid?
bool UnitInfoManager::isValidUnit(const sc2::Unit * unit)
{
if (!unit) { return false; }
// we only care about our units and enemy units
if (!(Util::GetPlayer(unit) == Players::Self || Util::GetPlayer(unit) == Players::Enemy))
{
return false;
}
// if it's a weird unit, don't bother
if (unit->unit_type == sc2::UNIT_TYPEID::ZERG_EGG || unit->unit_type == sc2::UNIT_TYPEID::ZERG_LARVA)
{
return false;
}
// if the position isn't valid throw it out
if (!m_bot.m_map.isValid(unit->pos))
{
return false;
}
// s'all good baby baby
return true;
}
void UnitInfoManager::getNearbyForce(std::vector<UnitInfo> & unitInfo, sc2::Point2D p, int player, float radius) const
{
bool hasBunker = false;
// for each unit we know about for that player
for (const auto & kv : getUnitData(player).getUnitInfoMap())
{
const UnitInfo & ui(kv.second);
// if it's a combat unit we care about
// and it's finished!
if (Util::IsCombatUnitType(ui.type, m_bot) && Util::Dist(ui.lastPosition, p) <= radius)
{
// add it to the vector
unitInfo.push_back(ui);
}
}
}
const UnitData & UnitInfoManager::getUnitData(int player) const
{
return m_unitData.find(player)->second;
}