Uh oh!
There was an error while loading. Please reload this page.
forked from Plankton555/SSCAIT-ObserverModule
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExampleAIModule.cpp
More file actions
Latest commit
304 lines (262 loc) · 8.27 KB
/
Copy pathExampleAIModule.cpp
File metadata and controls
304 lines (262 loc) · 8.27 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include"ExampleAIModule.h"
#include<iostream>
#include<iomanip>
#include<numeric>
usingnamespaceBWAPI;
usingnamespaceFilter;
extern"C"voidgameInit(BWAPI::Game *game) { BWAPI::BroodwarPtr = game; }
extern"C"BWAPI::AIModule *newAIModule()
{
returnnewExampleAIModule();
}
std::string env(std::string name, std::string def)
{
constchar *s = std::getenv(name.c_str());
if (!s)
return def;
return s;
}
voidExampleAIModule::onStart()
{
screen_width = 0;
}
voidExampleAIModule::onEnd(bool isWinner)
{
}
inlineintgetCharWidth(uint8_t c, uint8_t bSize)
{
if (c == '\t')
return12 * bSize;
if (c < '')
return0;
if (c == '')
return3 * bSize;
return6 * bSize;
}
inlineintgetTextWidth(constchar *pszStr, uint8_t bSize)
{
// Retrieve size
int size = 0;
for (; *pszStr; ++pszStr)
size += getCharWidth(*pszStr, bSize);
return size;
}
voidExampleAIModule::onFrame()
{
if (!screen_width)
{
#ifdef OPENBW
screen_width = Broodwar->getScreenSize().x;
screen_height = Broodwar->getScreenSize().y;
#else
screen_width = std::stoi(env("SCREEN_WIDTH", "1280"));
screen_height = std::stoi(env("SCREEN_HEIGHT", "720"));
#endif
if (screen_width)
{
auto startPosition = Position(0, 0);
auto players = Broodwar->getPlayers();
for (auto iter = players.cbegin(); iter != players.end(); ++iter)
{
if (!(*iter)->isObserver() && !(*iter)->isNeutral())
{
startPosition = Position((*iter)->getStartLocation());
}
}
cameraModule.onStart(startPosition, screen_width, screen_height);
}
else
{
return;
}
}
renderPlayerInfos();
renderBuildQueues();
cameraModule.onFrame();
}
voidExampleAIModule::renderPlayerInfos()
{
auto players = Broodwar->getPlayers();
std::vector<std::string> lines;
auto maxLength = 0;
for (auto iter = players.cbegin(); iter != players.end(); ++iter)
{
auto player = *iter;
if (!player->isObserver() && !player->isNeutral())
{
std::stringstream line;
line << std::setfill('\x80') << Text::Align_Right;
line << player->getTextColor();
if (cameraModule.hasVision(player))
line << "*";
else
line << "\x80";
line << player->getName();
line << std::left;
auto playerUnits = player->getUnits();
auto workers = std::count_if(playerUnits.cbegin(), playerUnits.cend(), [](BWAPI::Unit unit) { return unit->isCompleted() && unit->getType().isWorker(); });
auto armySupply = std::accumulate(playerUnits.cbegin(), playerUnits.cend(), 0, [this](int sum, BWAPI::Unit unit) {
return sum + (unit->isCompleted() && cameraModule.isArmyUnit(unit) ? unit->getType().supplyRequired() : 0);
}) /
2.0;
line << " m " << std::setw(4) << player->minerals();
line << " g " << std::setw(4) << player->gas();
line << " s " << std::setw(3) << (player->supplyUsed() + 1) / 2 << "/" << std::setw(3) << player->supplyTotal() / 2;
line << " workers " << std::setw(3) << workers;
line << " army " << std::setw(5) << armySupply;
auto linestr = line.str();
auto strlen = getTextWidth(linestr.c_str(), Text::Size::Large);
maxLength = maxLength > strlen ? maxLength : strlen;
lines.push_back(linestr);
}
}
Broodwar->drawBoxScreen(screen_width - maxLength - 16, 24, screen_width, 24 + lines.size() * 18, Colors::Black, true);
Broodwar->setTextSize(Text::Size::Large);
for (size_t y = 0; y < lines.size(); y++)
{
Broodwar->drawTextScreen(Position(0, y * 16 + 26), lines.at(y).c_str());
}
Broodwar->setTextSize();
}
intf2s(int frames) {
return (frames * 42 + 999) / 1000;
}
voidExampleAIModule::renderBuildQueues()
{
using row = std::tuple<int, std::string>;
auto players = Broodwar->getPlayers();
std::vector<row> lines;
for (auto iter = players.cbegin(); iter != players.end(); ++iter)
{
auto player = *iter;
if (!player->isObserver() && !player->isNeutral())
{
for (auto u: player->getUnits()) {
int bt = u->getRemainingBuildTime();
if (bt) {
std::stringstream line;
line << std::setfill('\x80') << player->getTextColor();
line << std::setw(3) << f2s(bt) << "s ";
std::string name;
if (u->getType() == UnitTypes::Zerg_Egg) {
name = u->getBuildType().getName();
} else {
name = u->getType().getName();
}
line << name.substr(name.find('_') + 1);
lines.push_back(std::make_tuple(bt, line.str()));
}
int ut = u->getRemainingUpgradeTime();
if (ut) {
std::stringstream line;
line << std::setfill('\x80') << player->getTextColor();
line << std::setw(3) << f2s(ut) << "s " << u->getUpgrade().getName();
lines.push_back(std::make_tuple(ut, line.str()));
}
int tt = u->getRemainingResearchTime();
if (tt) {
std::stringstream line;
line << std::setfill('\x80') << player->getTextColor();
line << std::setw(3) << f2s(tt) << "s " << u->getTech().getName();
lines.push_back(std::make_tuple(tt, line.str()));
}
}
}
}
sort(lines.begin(), lines.end(),
[](const row& a , const row& b) {
return std::get<0>(a) < std::get<0>(b);
});
for (size_t y = 0; y < lines.size() && y < 25; y++)
{
auto str = std::get<1>(lines.at(y)).c_str();
auto text_width = getTextWidth(str, Text::Size::Default);
Broodwar->drawBoxScreen(0, y * 10 + 39, text_width, y * 10 + 49, Colors::Black, true);
Broodwar->drawTextScreen(Position(0, y * 10 + 40), str);
}
}
voidExampleAIModule::onSendText(std::string text)
{
// Send the text to the game if it is not being processed.
Broodwar->sendText("%s", text.c_str());
// Make sure to use %s and pass the text as a parameter,
// otherwise you may run into problems when you use the %(percent) character!
}
voidExampleAIModule::onReceiveText(BWAPI::Player player, std::string text)
{
// Parse the received text
Broodwar << player->getName() << " said \"" << text << "\"" << std::endl;
}
voidExampleAIModule::onPlayerLeft(BWAPI::Player player)
{
}
voidExampleAIModule::onNukeDetect(BWAPI::Position target)
{
// Check if the target is a valid position
if (target)
{
// if so, print the location of the nuclear strike target
/* Broodwar << "Nuclear Launch Detected at " << target << std::endl; */
}
else
{
// Otherwise, ask other players where the nuke is!
/* Broodwar->sendText("Where's the nuke?"); */
}
// You can also retrieve all the nuclear missile targets using Broodwar->getNukeDots()!
}
voidExampleAIModule::onUnitDiscover(BWAPI::Unit unit)
{
}
voidExampleAIModule::onUnitEvade(BWAPI::Unit unit)
{
}
voidExampleAIModule::onUnitShow(BWAPI::Unit unit)
{
}
voidExampleAIModule::onUnitHide(BWAPI::Unit unit)
{
}
voidExampleAIModule::onUnitCreate(BWAPI::Unit unit)
{
if (Broodwar->isReplay())
{
// if we are in a replay, then we will print out the build order of the structures
if (unit->getType().isBuilding() && !unit->getPlayer()->isNeutral())
{
int seconds = Broodwar->getFrameCount() / 24;
int minutes = seconds / 60;
seconds %= 60;
Broodwar->sendText("%.2d:%.2d: %s creates a %s", minutes, seconds, unit->getPlayer()->getName().c_str(), unit->getType().c_str());
}
}
}
voidExampleAIModule::onUnitDestroy(BWAPI::Unit unit)
{
cameraModule.onUnitDestroy(unit);
}
voidExampleAIModule::onUnitMorph(BWAPI::Unit unit)
{
if (Broodwar->isReplay())
{
// if we are in a replay, then we will print out the build order of the structures
if (unit->getType().isBuilding() && !unit->getPlayer()->isNeutral())
{
int seconds = Broodwar->getFrameCount() / 24;
int minutes = seconds / 60;
seconds %= 60;
Broodwar->sendText("%.2d:%.2d: %s morphs a %s", minutes, seconds, unit->getPlayer()->getName().c_str(), unit->getType().c_str());
}
}
}
voidExampleAIModule::onUnitRenegade(BWAPI::Unit unit)
{
}
voidExampleAIModule::onSaveGame(std::string gameName)
{
Broodwar << "The game was saved to \"" << gameName << "\"" << std::endl;
}
voidExampleAIModule::onUnitComplete(BWAPI::Unit unit)
{
cameraModule.moveCameraUnitCreated(unit);
}