-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathai_client.cpp
339 lines (310 loc) · 9.58 KB
/
ai_client.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
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
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
#include "ai_client.h"
#include "calculator.h"
void sendLen(std::string s)
{
int len = s.length();
unsigned char lenb[4];
lenb[0] = (unsigned char)(len);
lenb[1] = (unsigned char)(len >> 8);
lenb[2] = (unsigned char)(len >> 16);
lenb[3] = (unsigned char)(len >> 24);
for (int i = 0; i < 4; i++)
printf("%c", lenb[3 - i]);
}
void sendMsg(int player, int round, std::string operation_type, json operation_parameters)
{
json message;
message["player"] = player;
message["round"] = round;
message["operation_type"] = operation_type;
message["operation_parameters"] = operation_parameters;
sendLen(message.dump());
std::cout << (unsigned char *)(message.dump().c_str());
std::cout.flush();
}
json read()
{
std::string len = "";
for (int i = 0; i < 6; ++i)
len += getchar();
std::string recv_msg = "";
for (int i = std::stoi(len); i > 0; --i)
recv_msg += getchar();
return json::parse(recv_msg);
}
AiClient::AiClient()
{
players[0].camp = 0;
players[1].camp = 1;
json game_info = read();
game_info["camp"].get_to(my_camp);
}
void AiClient::updateGameInfo()
{
json game_info = read();
game_info["round"].get_to(round);
game_info["camp"].get_to(my_camp);
game_info["map"].get_to(map);
game_info["players"][0].get_to(players[0]);
game_info["players"][1].get_to(players[1]);
}
void AiClient::chooseCards()
{
my_artifacts = {"HolyLight"};
my_creatures = {"Archer", "Swordsman", "Priest"};
init();
}
void AiClient::play()
{
if (round < 20)
endRound();
else
exit(0);
}
void AiClient::init()
{
json operation_parameters;
operation_parameters["artifacts"] = my_artifacts;
operation_parameters["creatures"] = my_creatures;
sendMsg(my_camp, 0, "init", operation_parameters);
}
void AiClient::summon(std::string type, int level, int x, int y, int z)
{
json operation_parameters;
std::vector<int> position = {x, y, z};
operation_parameters["position"] = position;
operation_parameters["type"] = type;
operation_parameters["level"] = level;
sendMsg(my_camp, round, "summon", operation_parameters);
updateGameInfo();
}
void AiClient::summon(std::string type, int level, std::vector<int> position)
{
json operation_parameters;
operation_parameters["position"] = position;
operation_parameters["type"] = type;
operation_parameters["level"] = level;
sendMsg(my_camp, round, "summon", operation_parameters);
updateGameInfo();
}
void AiClient::summon(std::string type, int level, std::tuple<int, int, int> position)
{
json operation_parameters;
operation_parameters["position"] = position;
operation_parameters["type"] = type;
operation_parameters["level"] = level;
sendMsg(my_camp, round, "summon", operation_parameters);
updateGameInfo();
}
void AiClient::move(int mover, int x, int y, int z)
{
json operation_parameters;
operation_parameters["mover"] = mover;
std::vector<int> position = {x, y, z};
operation_parameters["position"] = position;
sendMsg(my_camp, round, "move", operation_parameters);
updateGameInfo();
}
void AiClient::move(int mover, std::vector<int> position)
{
json operation_parameters;
operation_parameters["mover"] = mover;
operation_parameters["position"] = position;
sendMsg(my_camp, round, "move", operation_parameters);
updateGameInfo();
}
void AiClient::move(int mover, std::tuple<int, int, int> position)
{
json operation_parameters;
operation_parameters["mover"] = mover;
operation_parameters["position"] = position;
sendMsg(my_camp, round, "move", operation_parameters);
updateGameInfo();
}
void AiClient::attack(int attacker, int target)
{
json operation_parameters;
operation_parameters["attacker"] = attacker;
operation_parameters["target"] = target;
sendMsg(my_camp, round, "attack", operation_parameters);
updateGameInfo();
}
void AiClient::use(int artifact, int target)
{
json operation_parameters;
operation_parameters["card"] = artifact;
operation_parameters["target"] = target;
sendMsg(my_camp, round, "use", operation_parameters);
updateGameInfo();
}
void AiClient::use(int artifact, std::vector<int> target)
{
json operation_parameters;
operation_parameters["card"] = artifact;
operation_parameters["target"] = target;
sendMsg(my_camp, round, "use", operation_parameters);
updateGameInfo();
}
void AiClient::use(int artifact, std::tuple<int, int, int> target)
{
json operation_parameters;
operation_parameters["card"] = artifact;
operation_parameters["target"] = target;
sendMsg(my_camp, round, "use", operation_parameters);
updateGameInfo();
}
void AiClient::endRound()
{
json operation_parameters;
sendMsg(my_camp, round, "endround", operation_parameters);
}
int AiClient::getDistanceOnGround(gameunit::Pos pos_a, gameunit::Pos pos_b, int camp)
{
//地图边界
std::vector<gameunit::Pos> obstacles_pos = calculator::MAPBORDER();
//地面障碍
for (auto p = map.ground_obstacles.begin(); p != map.ground_obstacles.end(); p++)
{
obstacles_pos.push_back(p->pos);
}
//敌方地面生物
for (auto p = map.units.begin(); p != map.units.end(); p++)
{
if (p->camp != camp && (!p->flying))
{
obstacles_pos.push_back(p->pos);
}
}
return calculator::search_path(pos_a, pos_b, obstacles_pos, {}).size();
}
int AiClient::getDistanceInSky(gameunit::Pos pos_a, gameunit::Pos pos_b, int camp)
{
//地图边界
std::vector<gameunit::Pos> obstacles_pos = calculator::MAPBORDER();
//飞行障碍
for (auto p = map.flying_obstacles.begin(); p != map.flying_obstacles.end(); p++)
{
obstacles_pos.push_back(p->pos);
}
//敌方飞行生物
for (auto p = map.units.begin(); p != map.units.end(); p++)
{
if (p->camp != camp && p->flying)
{
obstacles_pos.push_back(p->pos);
}
}
return calculator::search_path(pos_a, pos_b, obstacles_pos, {}).size();
}
int AiClient::checkBarrack(gameunit::Pos pos)
{
for (auto barrack = map.barracks.begin(); barrack != map.barracks.end(); barrack++)
{
if (barrack->pos == pos)
return barrack->camp;
}
return -2;
}
bool AiClient::canAttack(gameunit::Unit attacker, gameunit::Unit target)
{
//攻击力小于等于0的生物无法攻击
if (attacker.atk <= 0)
return false;
//攻击范围
int dist = calculator::cube_distance(attacker.pos, target.pos);
if (dist < attacker.atk_range[0] || dist > attacker.atk_range[1])
return false;
//对空攻击
if (target.flying && (!attacker.atk_flying))
return false;
return true;
}
bool AiClient::canUseArtifact(gameunit::Artifact artifact, gameunit::Pos pos, int camp)
{
if (artifact.name == "HolyLight" || artifact.name == "WindBlessing")
{
return calculator::in_map(pos);
}
else if (artifact.name == "InfernoFlame")
{
// 不处于障碍物上
for (auto obstacle = map.obstacles.begin(); obstacle != map.obstacles.end(); obstacle++)
{
if (obstacle->pos == pos)
return false;
}
// 无地面生物
for (auto unit = map.units.begin(); unit != map.units.end(); unit++)
{
if ((unit->pos == pos) && (not unit->flying))
return false;
}
// 距己方神迹范围<=7
if (calculator::cube_distance(pos, map.miracles[camp].pos) <= 7)
return true;
// 距己方占领驻扎点范围<=5
for (auto barrack = map.barracks.begin(); barrack != map.barracks.end(); barrack++)
{
if ((barrack->camp == camp) && (calculator::cube_distance(pos, barrack->pos) <= 5))
return true;
}
}
return false;
}
bool AiClient::canUseArtifact(gameunit::Artifact artifact, gameunit::Unit unit)
{
if (artifact.name == "SalamanderShield")
{
// 己方生物
return artifact.camp == unit.camp;
}
return false;
}
gameunit::Unit AiClient::getUnitByPos(gameunit::Pos pos, bool flying)
{
for (int i = 0; i < map.units.size(); ++i) {
if (map.units[i].pos == pos && map.units[i].flying == flying)
return map.units[i];
}
// 未找到时返回一个id为-1的Unit
gameunit::Unit no_unit;
no_unit.id = -1;
return no_unit;
}
gameunit::Unit AiClient::getUnitById(int unit_id)
{
for (int i = 0; i < map.units.size(); ++i)
{
if (map.units[i].id == unit_id)
return map.units[i];
}
// 未找到时返回一个id为-1的Unit
gameunit::Unit no_unit;
no_unit.id = -1;
return no_unit;
}
std::vector<gameunit::Unit> AiClient::getUnitsByCamp(int unit_camp)
{
std::vector<gameunit::Unit> camp_units;
for (int i = 0; i < map.units.size(); ++i)
{
if (map.units[i].camp == unit_camp)
camp_units.push_back(map.units[i]);
}
return camp_units;
}
std::vector<gameunit::Pos> AiClient::getSummonPosByCamp(int camp)
{
std::vector<gameunit::Pos> summon_pos;
for (auto miracle = map.miracles.begin(); miracle != map.miracles.end(); miracle++)
{
if (miracle->camp == camp)
summon_pos.insert(summon_pos.end(), miracle->summon_pos_list.begin(), miracle->summon_pos_list.end());
}
for (auto barrack = map.barracks.begin(); barrack != map.barracks.end(); barrack++)
{
if (barrack->camp == camp)
summon_pos.insert(summon_pos.end(), barrack->summon_pos_list.begin(), barrack->summon_pos_list.end());
}
return summon_pos;
}