-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzhangfei.cpp
More file actions
292 lines (265 loc) · 6.74 KB
/
Copy pathzhangfei.cpp
File metadata and controls
292 lines (265 loc) · 6.74 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
#include "teamstyle17.h"
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
typedef Position Vector;
const int safe_threshold = 20; // devours beyond 20 rounds away are considered safe
const int border_threshold = 5; // modify velocity 5 steps from border
int Archer;
double boss_radius;
PlayerObject me; // currently does not support multiple player objects
int team_id;
int oppo_short_att_level = 0;
int action_clock = 0; // the last time an action was done
int long_attack(const Object& target);
int short_attack(const Object& target);
int shield();
int dash();
void my_move(Vector v);
void update_clock()
{
action_clock = GetTime();
}
int long_attack(const Object& target)
{
if (me.skill_cd[LONG_ATTACK] == -1) {
return -1;
}
if (me.long_attack_casting != -1) {
return -1;
}
if (target.shield_time > 0) {
return -1;
}
if (Distance(target.pos, me.pos) - target.radius - me.radius
> kLongAttackRange[me.skill_level[LONG_ATTACK]]) {
return -1;
}
while (GetTime() <= action_clock)
;
LongAttack(me.id, target.id);
update_clock();
return 0;
}
int short_attack(const Object& target)
{
if (me.skill_cd[SHORT_ATTACK] == -1) {
return -1;
}
if (target.shield_time > 0) {
return -1;
}
if (Distance(target.pos, me.pos) - target.radius - me.radius
> kShortAttackRange[me.skill_level[SHORT_ATTACK]]) {
return -1;
}
while (GetTime() <= action_clock)
;
ShortAttack(me.id);
update_clock();
return 0;
}
int shield()
{
if (!me.skill_level[SHIELD])
return -1;
if (me.skill_cd[SHIELD] == -1)
return -1;
while (GetTime() <= action_clock)
;
Shield(me.id);
update_clock();
return 0;
}
int dash()
{
if (!me.skill_level[DASH])
return -1;
if (me.skill_cd[DASH] == -1)
return -1;
while (GetTime() <= action_clock)
;
Dash(me.id);
update_clock();
return 0;
}
void my_move(Vector v)
{
unify(v);
v = Scale(kMaxMoveSpeed, v);
while (GetTime() <= action_clock)
;
Move(me.id, v);
update_clock();
}
Vector operator + (const Vector& v1, const Vector& v2)
{
Vector result;
result.x = v1.x + v2.x;
result.y = v1.y + v2.y;
result.z = v1.z + v2.z;
return result;
}
Vector operator - (const Vector& v1, const Vector& v2)
{
Vector result;
result.x = v1.x - v2.x;
result.y = v1.y - v2.y;
result.z = v1.z - v2.z;
return result;
}
void unify(Vector& v)
{
double norm = Norm(v);
v.x /= norm;
v.y /= norm;
v.z /= norm;
}
void zhang_fei_reset(double& health_prev, double& att_dist)
{
health_prev = att_dist = 0;
cout << "Archer: 1" << endl;
Archer = 1; // fall back to Enshaw
}
int zhang_fei_move(const Object& target, vector<const Object&>& devour_list)
{
Vector velocity = target.pos - me.pos;
if (me.pos.x - me.radius - border_threshold * kMaxMoveSpeed <= 0 && velocity.x < 0 ||
kMapSize - me.pos.x - me.radius - border_threshold * kMaxMoveSpeed <= 0 && velocity.x > 0 ||
me.pos.y - me.radius - border_threshold * kMaxMoveSpeed <= 0 && velocity.y < 0 ||
kMapSize - me.pos.y - me.radius - border_threshold * kMaxMoveSpeed <= 0 && velocity.y > 0 ||
me.pos.z - me.radius - border_threshold * kMaxMoveSpeed <= 0 && velocity.z < 0 ||
kMapSize - me.pos.z - me.radius - border_threshold * kMaxMoveSpeed <= 0 && velocity.z > 0) {
// facing border
return -1;
}
unify(velocity);
for (auto devour : devour_list) {
Vector relative_pos = devour.pos - me.pos;
double product = DotProduct(relative_pos, velocity);
if (product < 0 || product > kMaxMoveSpeed * safe_threshold) {
// devour on the opposite side or too far, safe
continue;
}
Vector projection_v = Scale(product, velocity);
Vector projection_P = relative_pos - projection_v;
double projection_radius = Norm(projection_P);
if (target.radius * 0.95 < projection_radius && projection_radius < me.radius * 1.05) {
// danger, fall back to Enshaw
return -1;
}
}
my_move(velocity);
return 0;
}
double get_health(const Object& obj)
{
return pow(obj.radius / 100, 3);
}
void infer_oppo_att_level(double health_loss, double att_dist)
{
int new_level = -1;
for (int i = 1; i <= kMaxSkillLevel; ++i) {
if (health_loss <= kShortAttackDamage[i] && att_dist <= kShortAttackRange[i]) {
new_level = i;
}
}
if (new_level > oppo_short_att_level) {
oppo_short_att_level = new_level;
cout << "level raised to " << new_level << endl;
}
}
void zhang_fei() {
const Map* map = GetMap();
me = GetStatus()->objects[0];
int boss_i = -1, oppo_i = -1;
vector<const Object&> devour_list;
static double last_time = GetTime(), health_prev = 0, att_dist = 0;
// scan map
for (int i = 0; i < map->objects_number; ++i) {
switch (map->objects[i].type) {
case PLAYER:
if (map->objects[i].team_id != team_id) {
oppo_i = i;
}
break;
case BOSS:
boss_i = i;
boss_radius = map->objects[i].radius;
break;
case DEVOUR:
devour_list.push_back(map->objects[i]);
break;
}
}
if (boss_i == -1 && oppo_i == -1) {
// no attackable object, reset
return zhang_fei_reset(health_prev, att_dist);
}
if (oppo_i == -1) {
// try attacking boss and reset
short_attack(map->objects[boss_i]);
return zhang_fei_reset(health_prev, att_dist);
}
// deal with opponent
const Object& oppo = map->objects[oppo_i];
if (GetTime() - last_time > 0) {
double health_loss;
if (health_prev != 0) {
health_loss = me.health - health_prev;
}
else {
health_loss = 0;
}
health_prev = me.health;
infer_oppo_att_level(health_loss, att_dist);
att_dist = Distance(me.pos, oppo.pos) - me.radius - oppo.radius;
last_time = GetTime();
}
if (short_attack(oppo) == -1) {
if (long_attack(oppo) == -1) {
if (shield() == -1) {
dash();
}
}
}
double k = me.radius / oppo.radius;
if (k < 0.9) {
return zhang_fei_reset(health_prev, att_dist);
}
else if (0.9 <= k && k < 1) {
if (me.skill_level[SHORT_ATTACK] == kMaxSkillLevel) {
// equipped with full SHORT_ATTACK, safe to move,
// but stop when health is getting dangerous
if (me.health / me.max_health < 0.5 || zhang_fei_move(oppo, devour_list) == -1) {
return zhang_fei_reset(health_prev,att_dist);
}
}
else {
return zhang_fei_reset(health_prev, att_dist);
}
}
else if (1 <= k && k < 1.2) {
if (me.health / me.max_health < 0.5) {
// TODO: tell Enshaw not to launch attack when health is too low
return zhang_fei_reset(health_prev, att_dist);
}
// flee if short attack is weaker than opponent
if (me.skill_level[SHORT_ATTACK] < oppo_short_att_level) {
return zhang_fei_reset(health_prev, att_dist);
}
else {
// try chasing opponent
if (zhang_fei_move(oppo, devour_list) == -1) {
return zhang_fei_reset(health_prev, att_dist);
}
}
}
else {
// large enough, attack except when the health level is too dangerous
if (me.health / me.max_health < 0.4 || zhang_fei_move(oppo, devour_list) == -1) {
return zhang_fei_reset(health_prev, att_dist);
}
}
}