-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathmain.cpp
186 lines (154 loc) · 5.83 KB
/
main.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
#include "Common.h"
#include "CCBot.h"
#include "JSONTools.h"
#include "Util.h"
#ifdef SC2API
#include "sc2utils/sc2_manage_process.h"
#include "sc2api/sc2_api.h"
int main(int argc, char* argv[])
{
sc2::Coordinator coordinator;
if (!coordinator.LoadSettings(argc, argv))
{
std::cout << "Unable to find or parse settings." << std::endl;
return 1;
}
std::string config = JSONTools::ReadFile("BotConfig.txt");
if (config.length() == 0)
{
std::cerr << "Config file could not be found, and is required for starting the bot\n";
std::cerr << "Please read the instructions and try again\n";
exit(-1);
}
std::ifstream file("BotConfig.txt");
json j;
file >> j;
/*if (parsingFailed)
{
std::cerr << "Config file could not be parsed, and is required for starting the bot\n";
std::cerr << "Please read the instructions and try again\n";
exit(-1);
}*/
std::string botRaceString;
std::string enemyRaceString;
std::string mapString;
int stepSize = 1;
sc2::Difficulty enemyDifficulty = sc2::Difficulty::Easy;
if (j.count("SC2API") && j["SC2API"].is_object())
{
const json & info = j["SC2API"];
JSONTools::ReadString("BotRace", info, botRaceString);
JSONTools::ReadString("EnemyRace", info, enemyRaceString);
JSONTools::ReadString("MapFile", info, mapString);
JSONTools::ReadInt("StepSize", info, stepSize);
JSONTools::ReadInt("EnemyDifficulty", info, enemyDifficulty);
}
else
{
std::cerr << "Config file has no 'Game Info' object, required for starting the bot\n";
std::cerr << "Please read the instructions and try again\n";
exit(-1);
}
// Add the custom bot, it will control the players.
CCBot bot;
// WARNING: Bot logic has not been thorougly tested on step sizes > 1
// Setting this = N means the bot's onFrame gets called once every N frames
// The bot may crash or do unexpected things if its logic is not called every frame
coordinator.SetStepSize(stepSize);
coordinator.SetRealtime(false);
coordinator.SetParticipants({
sc2::CreateParticipant(Util::GetRaceFromString(botRaceString), &bot),
sc2::CreateComputer(Util::GetRaceFromString(enemyRaceString), enemyDifficulty)
});
// Start the game.
coordinator.LaunchStarcraft();
coordinator.StartGame(mapString);
// Step forward the game simulation.
while (true)
{
coordinator.Update();
}
return 0;
}
#else
#include <BWAPI/Client.h>
void UAlbertaBot_BWAPIReconnect()
{
while(!BWAPI::BWAPIClient.connect())
{
std::this_thread::sleep_for(std::chrono::milliseconds{ 1000 });
}
}
void UAlbertaBot_PlayGame()
{
CCBot bot;
// The main game loop, which continues while we are connected to BWAPI and in a game
while (BWAPI::BWAPIClient.isConnected() && BWAPI::Broodwar->isInGame())
{
// Handle each of the events that happened on this frame of the game
for (const BWAPI::Event & e : BWAPI::Broodwar->getEvents())
{
switch (e.getType())
{
case BWAPI::EventType::MatchStart: { bot.OnGameStart(); break; }
case BWAPI::EventType::MatchFrame: { bot.OnStep(); break; }
/*case BWAPI::EventType::MatchEnd: { m->onEnd(e.isWinner()); break; }
case BWAPI::EventType::UnitShow: { m->onUnitShow(e.getUnit()); break; }
case BWAPI::EventType::UnitHide: { m->onUnitHide(e.getUnit()); break; }
case BWAPI::EventType::UnitCreate: { m->onUnitCreate(e.getUnit()); break; }
case BWAPI::EventType::UnitMorph: { m->onUnitMorph(e.getUnit()); break; }
case BWAPI::EventType::UnitDestroy: { m->onUnitDestroy(e.getUnit()); break; }
case BWAPI::EventType::UnitRenegade: { m->onUnitRenegade(e.getUnit()); break; }
case BWAPI::EventType::UnitComplete: { m->onUnitComplete(e.getUnit()); break; }
case BWAPI::EventType::SendText: { m->onSendText(e.getText()); break; }*/
}
}
BWAPI::BWAPIClient.update();
if (!BWAPI::BWAPIClient.isConnected())
{
std::cout << "Disconnected\n";
break;
}
}
std::cout << "Game Over\n";
}
int main(int argc, char * argv[])
{
bool exitIfStarcraftShutdown = true;
size_t gameCount = 0;
while (true)
{
// if we are not currently connected to BWAPI, try to reconnect
if (!BWAPI::BWAPIClient.isConnected())
{
UAlbertaBot_BWAPIReconnect();
}
// if we have connected to BWAPI
while (BWAPI::BWAPIClient.isConnected())
{
// wait for the game to start until the game starts
std::cout << "Waiting for game start\n";
while (BWAPI::BWAPIClient.isConnected() && !BWAPI::Broodwar->isInGame())
{
BWAPI::BWAPIClient.update();
}
// Check to see if Starcraft shut down somehow
if (BWAPI::BroodwarPtr == nullptr)
{
break;
}
// If we are successfully in a game, call the module to play the game
if (BWAPI::Broodwar->isInGame())
{
std::cout << "Playing game " << gameCount++ << " on map " << BWAPI::Broodwar->mapFileName() << "\n";
UAlbertaBot_PlayGame();
}
}
if (exitIfStarcraftShutdown && !BWAPI::BWAPIClient.isConnected())
{
return 0;
}
}
return 0;
}
#endif