Newer
Older
#include "IDABot.h"
#include "Util.h"
#include "sc2api/sc2_score.h"
IDABot::IDABot()
: m_map(*this)
, m_bases(*this)
, m_unitInfo(*this)
, m_techTree(*this)
, m_buildingPlacer(*this)
{
}
void IDABot::OnGameStart()
{
// -----------------------------------------------------------------
// Initialize all start (base) locations.
// -----------------------------------------------------------------
for (auto & loc : Observation()->GetGameInfo().enemy_start_locations)
{
m_baseLocations.push_back(loc);
}
m_baseLocations.push_back(Observation()->GetStartLocation());
// -----------------------------------------------------------------
// Initialize all info, units, etc.
// -----------------------------------------------------------------
setUnits();
m_techTree.onStart();
m_map.onStart();
m_unitInfo.onStart();
m_bases.onStart();
m_buildingPlacer.onStart();
UnitType target { sc2::UNIT_TYPEID::TERRAN_FUSIONCORE, *this };
std::vector<UnitType> needed = { target };
build_order.clear();
std::cout << "Creating plan: ";
while (!needed.empty())
{
UnitType target = needed.back();
TypeData target_data = m_techTree.getData(target);
build_order.push_back(target);
std::cout << target.getName();
needed.pop_back();
if (!target_data.requiredUnits.empty())
{
// We only need one of the required units, so we pick the first.
// TODO: Pick in a smarter way.
UnitType subtarget = target_data.requiredUnits.front();
needed.push_back(subtarget);
std::cout << ", ";
}
}
std::cout << "." << std::endl;
std::cout << "Created build plan of " << build_order.size() << " steps." << std::endl;
}
void IDABot::OnStep()
{
// -----------------------------------------------------------------
// Update units, map info, unit info, and base info.
// -----------------------------------------------------------------
setUnits();
m_map.onFrame();
m_unitInfo.onFrame();
m_bases.onFrame();
// suppress warnings while we update the tiles occupied by units
bool old_suppress = m_techTree.getSuppressWarnings();
m_techTree.setSuppressWarnings(true);
m_buildingPlacer.updateReserved(GetAllUnits());
m_techTree.setSuppressWarnings(old_suppress);
// -----------------------------------------------------------------
// Draw debug interface, and send debug interface to the Sc2 client.
// -----------------------------------------------------------------
Debug()->SendDebug();
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
}
void IDABot::setUnits()
{
m_allUnits.clear();
Control()->GetObservation();
for (auto & unit : Observation()->GetUnits())
{
m_allUnits.push_back(Unit(unit, *this));
}
}
CCRace IDABot::GetPlayerRace(int player) const
{
auto playerID = Observation()->GetPlayerID();
for (auto & playerInfo : Observation()->GetGameInfo().player_info)
{
if (playerInfo.player_id == playerID)
{
return playerInfo.race_actual;
}
}
BOT_ASSERT(false, "Failed to find the player's race!");
return sc2::Race::Random;
}
const MapTools & IDABot::Map() const
{
return m_map;
}
const BaseLocationManager & IDABot::Bases() const
{
return m_bases;
}
const UnitInfoManager & IDABot::UnitInfo() const
{
return m_unitInfo;
}
int IDABot::GetCurrentFrame() const
{
return (int)Observation()->GetGameLoop();
}
const TechTree & IDABot::GetTechTree() const
int IDABot::GetCurrentSupply() const
{
return Observation()->GetFoodUsed();
}
int IDABot::GetMaxSupply() const
{
return Observation()->GetFoodCap();
}
int IDABot::GetMinerals() const
{
return Observation()->GetMinerals();
}
int IDABot::GetGas() const
{
return Observation()->GetVespene();
}
Unit IDABot::GetUnit(const CCUnitID & tag) const
{
if (Observation()->GetUnit(tag) == nullptr) {
return Unit();
}
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
return Unit(Observation()->GetUnit(tag), *(IDABot *)this);
}
const std::vector<Unit> & IDABot::GetAllUnits() const
{
return m_allUnits;
}
const std::vector<Unit> & IDABot::GetMyUnits() const
{
return UnitInfo().getUnits(Players::Self);
}
const std::vector<Unit> IDABot::GetUnits(const UnitType & type, int player) const
{
std::vector<Unit> units;
for (const Unit & unit : GetAllUnits()) {
if (unit.getPlayer() == player)
{
if (!type.isValid() || (type.isValid() && unit.getType() == type))
{
units.push_back(unit);
}
}
}
return units;
}
CCPosition IDABot::GetStartLocation() const
{
return Observation()->GetStartLocation();
}
const std::vector<CCPosition> & IDABot::GetStartLocations() const
{
return m_baseLocations;
}
void IDABot::OnError(const std::vector<sc2::ClientError> & client_errors, const std::vector<std::string> & protocol_errors)
{
// This is called when the sc2api (Google's API) has an error.
BuildingPlacer & IDABot::GetBuildingPlacer()
void IDABot::SendChat(const std::string & message)
{
Actions()->SendChat(message);
}
const TypeData & IDABot::Data(const UnitType & type) const
{
return m_techTree.getData(type);
}
const TypeData & IDABot::Data(const Unit & unit) const
{
return m_techTree.getData(unit.getType());
}
const TypeData & IDABot::Data(const CCUpgrade & type) const
{
return m_techTree.getData(type);
}
const TypeData & IDABot::Data(const MetaType & type) const
{
return m_techTree.getData(type);
}
/*
API extended summer 2020
*/
void IDABot::DebugCreateUnit(UnitTypeID unit_type, const CCPosition& p, uint32_t player_id, uint32_t count)
{
switch (player_id) // playerconstants for the IDAbot is not the same as for the sc2 API
{
case 0:
Debug()->DebugCreateUnit(unit_type, p, 1, count);
break;
case 1:
Debug()->DebugCreateUnit(unit_type, p, 2, count);
break;
case 2:
Debug()->DebugCreateUnit(unit_type, p, 0, count);
break;
case 3:
Debug()->DebugCreateUnit(unit_type, p, 3, count);
default:
break;
}
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
}
void IDABot::DebugKillUnit(const Unit unit)
{
Debug()->DebugKillUnit(unit.getUnitPtr());
}
void IDABot::DebugShowMap()
{
Debug()->DebugShowMap();
}
void IDABot::DebugFastBuild()
{
Debug()->DebugFastBuild();
}
void IDABot::DebugEnemyControl()
{
Debug()->DebugEnemyControl();
}
void IDABot::DebugIgnoreFood()
{
Debug()->DebugIgnoreFood();
}
void IDABot::DebugIgnoreResourceCost()
{
Debug()->DebugIgnoreResourceCost();
}
void IDABot::DebugGiveAllResources()
{
Debug()->DebugGiveAllResources();
}
void IDABot::DebugGodMode()
{
Debug()->DebugGodMode();
}
void IDABot::DebugIgnoreMineral()
{
Debug()->DebugIgnoreMineral();
}
void IDABot::DebugNoCooldowns()
{
Debug()->DebugIgnoreMineral();
}
void IDABot::DebugGiveAllTech()
{
Debug()->DebugGiveAllTech();
}
void IDABot::DebugGiveAllUpgrades()
{
Debug()->DebugGiveAllUpgrades();
}
void IDABot::DebugSetScore(float score)
{
Debug()->DebugSetScore(score);
}
void IDABot::DebugEndGame(bool victory)
{
Debug()->DebugEndGame(victory);
}
void IDABot::DebugSetEnergy(float value, const Unit unit)
{
Debug()->DebugSetEnergy(value, unit.getUnitPtr());
}
void IDABot::DebugSetLife(float value, const Unit unit)
{
Debug()->DebugSetLife(value, unit.getUnitPtr());
}
void IDABot::DebugSetShields(float value, const Unit unit)
{
Debug()->DebugSetShields(value, unit.getUnitPtr());
}
// There is a bug in the latest SC2 (if using Blizzard API with game >=4.10)
// This a function to get the enemy base instead of using build location manager
// Switched over to other API where this is solved
// Leaving function incase of it breaking
const std::vector<Point2D> IDABot::GetEnemyBaseLocations()
{
return Observation()->GetGameInfo().enemy_start_locations;
}
bool IDABot::HasCreep(Point2D p) const
{
return Observation()->HasCreep(p);
}
void IDABot::CameraMove(Point2DI p)
{
ActionsFeatureLayer()->CameraMove(p);
}
sc2::ABILITY_ID IDABot::abilityForUpgrade(sc2::UpgradeID upgrade_id) const
{
return (sc2::ABILITY_ID)Observation()->GetUpgradeData()[upgrade_id].ability_id;
}
uint32_t IDABot::UpgradeMineralCost(sc2::UpgradeID upgrade_id) const
{
return Observation()->GetUpgradeData()[upgrade_id].mineral_cost;
}
uint32_t IDABot::UpgradeGasCost(sc2::UpgradeID upgrade_id) const
{
return Observation()->GetUpgradeData()[upgrade_id].vespene_cost;
}
float IDABot::UpgradeResearchTime(sc2::UpgradeID upgrade_id) const
{
return Observation()->GetUpgradeData()[upgrade_id].research_time;
}
float IDABot::RadiusEffect(sc2::EffectID effect_id) const
{
return Observation()->GetEffectData()[effect_id].radius;
float IDABot::GetScore() const
{
return Observation()->GetScore().score;
}
const sc2::GameResult IDABot::GetPlayerResults() const
{
auto res = Observation()->GetResults();
for(int i = 0; i < res.size(); i++)
{
if (res.at(i).player_id == Observation()->GetPlayerID()) return res.at(i).result;
}
std::cout << "The player can not be found" << std::endl;
return sc2::GameResult::Undecided;
}
bool IDABot::SaveReplay(const std::string& path)
{
return this->Control()->SaveReplay(path);
}