forked from yanntm/YoBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LadderInterface.h
165 lines (153 loc) · 3.98 KB
/
LadderInterface.h
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
static sc2::Difficulty GetDifficultyFromString(std::string InDifficulty)
{
if (InDifficulty == "VeryEasy")
{
return sc2::Difficulty::VeryEasy;
}
if (InDifficulty == "Easy")
{
return sc2::Difficulty::Easy;
}
if (InDifficulty == "Medium")
{
return sc2::Difficulty::Medium;
}
if (InDifficulty == "MediumHard")
{
return sc2::Difficulty::MediumHard;
}
if (InDifficulty == "Hard")
{
return sc2::Difficulty::Hard;
}
if (InDifficulty == "HardVeryHard")
{
return sc2::Difficulty::HardVeryHard;
}
if (InDifficulty == "VeryHard")
{
return sc2::Difficulty::VeryHard;
}
if (InDifficulty == "CheatVision")
{
return sc2::Difficulty::CheatVision;
}
if (InDifficulty == "CheatMoney")
{
return sc2::Difficulty::CheatMoney;
}
if (InDifficulty == "CheatInsane")
{
return sc2::Difficulty::CheatInsane;
}
return sc2::Difficulty::Easy;
}
static sc2::Race GetRaceFromString(const std::string & RaceIn)
{
std::string race(RaceIn);
std::transform(race.begin(), race.end(), race.begin(), ::tolower);
if (race == "terran")
{
return sc2::Race::Terran;
}
else if (race == "protoss")
{
return sc2::Race::Protoss;
}
else if (race == "zerg")
{
return sc2::Race::Zerg;
}
else if (race == "random")
{
return sc2::Race::Random;
}
return sc2::Race::Random;
}
struct ConnectionOptions
{
int32_t GamePort;
int32_t StartPort;
std::string ServerAddress;
bool ComputerOpponent;
sc2::Difficulty ComputerDifficulty;
sc2::Race ComputerRace;
};
static void ParseArguments(int argc, char *argv[], ConnectionOptions &connect_options)
{
sc2::ArgParser arg_parser(argv[0]);
arg_parser.AddOptions({
{ "-g", "--GamePort", "Port of client to connect to", false },
{ "-o", "--StartPort", "Starting server port", false },
{ "-l", "--LadderServer", "Ladder server address", false },
{ "-c", "--ComputerOpponent", "If we set up a computer oppenent" },
{ "-a", "--ComputerRace", "Race of computer oppent"},
{ "-d", "--ComputerDifficulty", "Difficulty of computer oppenent"}
});
arg_parser.Parse(argc, argv);
std::string GamePortStr;
if (arg_parser.Get("GamePort", GamePortStr)) {
connect_options.GamePort = atoi(GamePortStr.c_str());
}
std::string StartPortStr;
if (arg_parser.Get("StartPort", StartPortStr)) {
connect_options.StartPort = atoi(StartPortStr.c_str());
}
arg_parser.Get("LadderServer", connect_options.ServerAddress);
std::string CompOpp;
if (arg_parser.Get("ComputerOpponent", CompOpp))
{
connect_options.ComputerOpponent = true;
std::string CompRace;
if (arg_parser.Get("ComputerRace", CompRace))
{
connect_options.ComputerRace = GetRaceFromString(CompRace);
}
std::string CompDiff;
if (arg_parser.Get("ComputerDifficulty", CompDiff))
{
connect_options.ComputerDifficulty = GetDifficultyFromString(CompDiff);
}
}
else
{
connect_options.ComputerOpponent = false;
}
}
static void RunBot(int argc, char *argv[], sc2::Agent *Agent,sc2::Race race)
{
ConnectionOptions Options;
ParseArguments(argc, argv, Options);
sc2::Coordinator coordinator;
// if (!coordinator.LoadSettings(argc, argv)) {
// return;
// }
// Add the custom bot, it will control the players.
int num_agents;
if (Options.ComputerOpponent)
{
num_agents = 1;
coordinator.SetParticipants({
CreateParticipant(race, Agent),
CreateComputer(Options.ComputerRace, Options.ComputerDifficulty)
});
}
else
{
num_agents = 2;
coordinator.SetParticipants({
CreateParticipant(race, Agent),
});
}
// Start the game.
// Step forward the game simulation.
std::cout << "Connecting to port " << Options.GamePort << std::endl;
coordinator.Connect(Options.GamePort);
coordinator.SetupPorts(num_agents, Options.StartPort, false);
// Step forward the game simulation.
coordinator.JoinGame();
coordinator.SetTimeoutMS(10000);
std::cout << " Successfully joined game" << std::endl;
while (coordinator.Update()) {
}
}