-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0x04 avoid+ 其辅助函数
631 lines (630 loc) · 16.2 KB
/
0x04 avoid+ 其辅助函数
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
#include "teamstyle17.h"
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<time.h>
#include<iostream>
#define MAX_SIZE 1000
#define WAIT while(GetTime()==operate_time);
#define GO (operate_time=GetTime());
#define NUM_OF_SOLUTION 3
enum value {
ENERGY_VALUE = (int)1e6,
HIGHLY_ADVANCED_VALUE= (int)1e8,
MID_ADVANCED_VALUE= (int)1e7,
LOW_ADVANCED_VALUE= (int)1e6,
TRASH=0,
};
enum situation {
NONE = 0,
OPPONENT = 1 << 0,
SEE_BOSS = 1 << 1,
};
struct point {
int weight;
Position pos;
};
struct ball {
Position center;
double radius;
};
double opponent_radius;// initial() opponent()
double boss_radius;// initial() greedy()
Position boss_pos;// initial() action()
Position opponent_pos;// initial() opponent()
PlayerObject me;// initial() almost everywhere
int emergency; // opponent(),boss() greedy()
int ad_weight = HIGHLY_ADVANCED_VALUE;// initial() initial()
int num_of_aim;// greedy() avoid()
int num_of_food;// initial() greedy()
int num_of_devour;// initial() avoid()
int code;// initial() AIMain()&greedy()
double me_radius;// initial() almost everywhere
Object boss_obj;// initial() action()
Object opponent_obj;// initial() opponent()
point aim[MAX_SIZE];// greedy() avoid()
point food[MAX_SIZE];// initial() greedy()
Position devour[MAX_SIZE];// initial() avoid()
ball devour_for_YQY[MAX_SIZE];// initial() avoid()
point solution[NUM_OF_SOLUTION];// opponent(),boss() avoid()
Position go_for;// avoid() move()
Position last_move;// move() move()
int operate_time;// after any action before any action
//core function usage author time cost
int initial();// initial,update the basic value ARC
void greedy();// find the best food ZWT
int update();// update the skills, shield ZWT 1cost
void avoid();// avoid the devour and border YQY
int opponent();// deal with the opponent PLU 1cost
int boss();// smaller, kill; bigger, eat ARC 1cost
void move();// move to PLU 1cost
//auxiliary variables
int en_weight = 20;
char bitmap[(MAX_SIZE >> 3) + 1];
//auxiliary function
int zw_cost(int skill);
int zw_cmp(const void*, const void*);
int long_attack(const Object& target);
int short_attack(const Object& target);
void zw_enshaw();
//lower level function
Position add(Position a, Position b);//a+b
Position minus(Position a, Position b);//a-b,从B指向A的向量
Position multiple(double k, Position a);//ka,数乘
Position cross_product(Position a, Position b);//a*b 叉乘
double dot_product(Position a, Position b);//ab
double length(Position a);//求矢量模长
Position norm(Position a);//求单位矢量
double distance(Position a, Position b);//求AB两点距离
void show(Position a);//输出矢量
//Main
void AIMain() {
if (GetStatus()->team_id == 1)return;
for (;;) {
srand(time(0));
code = initial();
printf("time=%d\n",GetTime());
if (distance(GetStatus()->objects[0].pos, me.pos) > 2 * me.radius) goto AVOID;
update();
if (distance(GetStatus()->objects[0].pos, me.pos) > 2 * me.radius) goto AVOID;
if (code&OPPONENT) {
//opponent();
}
if (distance(GetStatus()->objects[0].pos, me.pos) > 2 * me.radius) goto AVOID;
if (code&SEE_BOSS) {
boss();
}
if (distance(GetStatus()->objects[0].pos, me.pos) > 2 * me.radius) goto AVOID;
if (!emergency) {
greedy();
}
AVOID:
avoid();
move();
}
}
//function body
Position add(Position a, Position b)
{
Position c;
c.x = a.x + b.x;
c.y = a.y + b.y;
c.z = a.z + b.z;
return c;
}
Position minus(Position a, Position b)
{
Position c;
c.x = a.x - b.x;
c.y = a.y - b.y;
c.z = a.z - b.z;
return c;
}
Position multiple(double k, Position a)
{
Position c;
c.x = k*a.x;
c.y = k*a.y;
c.z = k*a.z;
return c;
}
Position cross_product(Position a, Position b)
{
Position c;
c.x = a.y*b.z - a.z*b.y;
c.y = -a.x*b.z + a.z*b.x;
c.z = a.x*b.y - a.y*b.x;
return c;
}
double dot_product(Position a, Position b)
{
return (a.x*b.x + a.y*b.y + a.z*b.z);
}
double length(Position a)
{
return sqrt(a.x*a.x + a.y*a.y + a.z*a.z);
}
Position norm(Position a)
{
double l = 1 / length(a);
return multiple(l, a);
}
double distance(Position a, Position b)
{
return length(minus(a, b));
}
void show(Position a)
{
printf("The position is (%f,%f,%f).\n", a.x, a.y, a.z);
}
int zw_cost(int skill) {
if (me.skill_level[skill]) {
return kBasicSkillPrice[skill] << me.skill_level[skill];
}
else {
int count = 0;
for (int i = 0;i < kSkillTypes;i++) {
if (me.skill_level[i]) count++;
}
return kBasicSkillPrice[skill] << count;
}
}
int update() {
if (me.skill_cd[SHIELD] == 0 /*&& me.short_attack_casting == -1*/ && me.long_attack_casting == -1) {
WAIT;
Shield(me.id);
GO;
return 1;
}
else {
if (me.skill_level[HEALTH_UP] < kMaxSkillLevel) {
if (me.ability >= zw_cost(HEALTH_UP)) {
WAIT;
UpgradeSkill(me.id, HEALTH_UP);
GO;
return 1;
}
else return 0;
}
else if (me.skill_level[SHIELD] < kMaxSkillLevel) {
if (me.ability >= zw_cost(SHIELD)) {
WAIT;
UpgradeSkill(me.id, SHIELD);
GO;
return 1;
}
else return 0;
}
else {
ad_weight = MID_ADVANCED_VALUE;//1st step of update finish
if (me.skill_level[SHORT_ATTACK] < kMaxSkillLevel) {
if (me.skill_level[DASH] < kMaxSkillLevel) {
if (me.ability >= zw_cost(SHORT_ATTACK)) {
WAIT;
UpgradeSkill(me.id, SHORT_ATTACK);
GO;
return 1;
}
else if (me.ability >= zw_cost(DASH)) {
WAIT;
UpgradeSkill(me.id, DASH);
GO;
return 1;
}
else return 0;
}
else {
if (me.ability >= zw_cost(SHORT_ATTACK)) {
WAIT;
UpgradeSkill(me.id, SHORT_ATTACK);
GO;
return 1;
}
else return 0;
}
}
else if (me.skill_level[DASH] < kMaxSkillLevel) {
if (me.ability >= zw_cost(DASH)) {
WAIT;
UpgradeSkill(me.id, DASH);
GO;
return 1;
}
else return 0;
}
else {
ad_weight = LOW_ADVANCED_VALUE;//2st step of update finish
if (me.skill_level[LONG_ATTACK] < kMaxSkillLevel) {
if (me.ability >= zw_cost(LONG_ATTACK)) {
WAIT;
UpgradeSkill(me.id, LONG_ATTACK);
GO;
return 1;
}
else return 0;
}
else {
ad_weight = TRASH;//all finish
return 0;
}
}
return 0;
}
}
}
void greedy() {
double check = 1.2*me_radius;
if (emergency)return;
zw_enshaw();
int temp = MAX_SIZE >> 3;
for (;temp >= 0;--temp) {
bitmap[temp] = 0;
}
while (true) {
int next;
for (next = 0;next < num_of_food;next++) {
if (!(bitmap[next >> 3] & (0x80 >> (next & 0x07)))) break;
}
if (next == num_of_food)break;
aim[num_of_aim] = food[next];
bitmap[next >> 3] |= (0x80 >> (next & 0x07));
int i;
for (i = 0;i < num_of_food;i++) {
if (!(bitmap[i >> 3] & (0x80 >> (i & 0x07)))) {
if (distance(aim[num_of_aim].pos, food[i].pos) < check) {
bitmap[i >> 3] |= (0x80 >> (i & 0x07));
}
}
}
num_of_aim++;
}
for (int temp = num_of_aim - 1;temp>0;--temp) {
aim[temp].weight /= distance(aim[temp].pos, me.pos);
}
//std::cout << "++" << std::endl;
for (int temp = num_of_aim - 1;~temp;--temp) {
//std::cout << aim[temp].weight << std::endl;
}
//std::cout << "++" << std::endl;
qsort(aim, num_of_aim, sizeof(point), zw_cmp);
}
int zw_cmp(const void* p, const void* q) {
int w1 = (((point*)p)->weight);
int w2 = (((point*)q)->weight);
return ((w2 - w1) > 0);
}
void avoid()
{
me = GetStatus()->objects[0];
Position default_pos[6];
const Position a[6]={{ 1000,0,0 },{ -1000,0,0 },{ 0,1000,0 },{ 0,-1000,0 },{ 0,0,1000 },{ 0,0,-1000 }};
default_pos[0] = add(me.pos, a[0]);
default_pos[1] = add(me.pos, a[1]);
default_pos[2] = add(me.pos, a[2]);
default_pos[3] = add(me.pos, a[3]);
default_pos[4] = add(me.pos, a[4]);
default_pos[5] = add(me.pos, a[5]);
int IsDevour(double d, Position des, Position speed);
Position Schmidt(Position a1, Position a2);
Position FBorder(double r,Position speed);
int IsBorder(double d,Position des);
int flag;//记录是否选取
int flag2;//记录是否在瞬移后前进方向有devour
int j, devour_count;
int i;
Position aim_devour;
if (solution[2].weight>aim[0].weight)
{
go_for=solution[2].pos;
printf("遇见boss了!!好怕怕!!\n");
return;
}
for (i = 0;i<num_of_aim;i++)
{
flag = 1;
for (j = 0;j<num_of_devour;j++)
{
if (distance(aim[i].pos, devour[j])<1 * me.radius)//如果目标附近有吞噬者
if (distance(aim[i].pos, me.pos)> 0.5*distance(devour[j], me.pos))
flag = 0;
}
if (flag == 0)
{
printf("throw it away!\n");
continue;
}
else
{
devour_count = 0;
Position speed = minus(aim[i].pos, me.pos);
for (j = 0;j<num_of_devour;j++)
{
if (IsDevour(1.1*me.radius, devour[j], speed))
{
devour_count++;
aim_devour = devour[j];
}
}
if (devour_count >= 2)
continue;
else
if (devour_count == 1)
{
printf("warning\n");
Position a2 = minus(aim_devour, me.pos);
/*if (IsBorder(1.1*me.radius,add(me.pos, speed)))
{
printf("边界哦!\n");
speed=FBorder(1.1*me.radius,speed);
}*/
speed=Schmidt(speed, a2);
go_for = add(me.pos, speed);
break;
}
else
{
go_for = aim[i].pos;
break;
}
}
}
if (i == num_of_aim)
{
printf("瞬移了!\n");
flag2=0;
for(j=0;j<num_of_devour;j++)
{
if (IsDevour(1.5*me.radius, devour[j], me.speed))
flag2=1;
}
if (!flag2)//如果没有问题
{
go_for=add(me.pos,me.speed);
return;
}
else//有问题,随机一个没有devour的方向
{
for(i=0;i<6;i++)
{
for(j=0;j<num_of_devour;j++)
if (!IsDevour(1.5*me.radius, devour[j], a[i]))
{
go_for=default_pos[i];
return;
}
}
}
}
}
Position Schmidt(Position a1, Position a2)
{
Position temp1, temp2;
temp2 = multiple(dot_product(a1, a2) / dot_product(a2, a2), a2);
temp1 = minus(a1, temp2);
return temp1;
}
int IsDevour(double d, Position des, Position speed)//判断下一时刻会不会碰到吞噬者
{
Position MaximumSpeed(Position vec);
int flag = 0;
Position Next;
for (int i = 1;i <= 10;i++)
{
Next = add(me.pos, multiple(i, MaximumSpeed(speed)));
if (distance(Next, des)<d)
flag = 1;
}
return flag;
}
Position MaximumSpeed(Position vec) {
register double len = length(vec);
vec.x *= (kMaxMoveSpeed + kDashSpeed[me.skill_level[DASH]]) / len;
vec.y *= (kMaxMoveSpeed + kDashSpeed[me.skill_level[DASH]]) / len;
vec.z *= (kMaxMoveSpeed + kDashSpeed[me.skill_level[DASH]]) / len;
return vec;
}
enum AR_BORDER {
NONEBorder = 0,
LEFT = 1 << 0,
RIGHT = 1 << 1,
BEHIND = 1 << 2,
FRONT = 1 << 3,
DOWN = 1 << 4,
UP = 1 << 5,
};
int IsBorder(double d,Position des)//判断物体是否在边界旁
{
int R = NONEBorder;
if (des.x < d) R |= LEFT;
else if (kMapSize - des.x < d) R |= RIGHT;
if (des.y < d) R |= BEHIND;
else if (kMapSize - des.y < d) R |= FRONT;
if (des.z < d) R |= DOWN;
else if (kMapSize - des.z < d) R |= UP;
return R;
}
Position FBorder(double r,Position speed)//判断下一时刻会不会碰到边界,如果会,速度置0;
{
register int flag = NONE,count=0;
Position temp=speed;
if (me.pos.x<r && speed.x<0)
temp.x=-0,flag=LEFT,count++;
if (me.pos.x > kMapSize - r && speed.x>0)
temp.x=-0,flag=RIGHT,count++;
if (me.pos.y<r && speed.y<0)
temp.y=-0,flag=BEHIND,count++;
if (me.pos.y > kMapSize - r && speed.y>0)
temp.y=-0,flag=FRONT,count++;
if (me.pos.z<r && speed.z<0)
temp.z=-0,flag=DOWN,count++;
if (me.pos.z > kMapSize - r && speed.z>0)
temp.z=-0,flag=UP,count++;
if (count>=2)
{
Position center={kMapSize/2,kMapSize/2,kMapSize/2};
Position a2=minus(center,me.pos);
speed=a2;
}
return temp;
}
void move() {
Position speed;
double mode = (10+kMaxMoveSpeed + kDashSpeed[me.skill_level[DASH]]) / distance(go_for, me.pos);
speed = multiple(mode, minus(go_for, me.pos));
Move(me.id, speed);
last_move = norm(speed);
}
point mi_zhi_yin_qiu_yang(int n) {
if (n >= 2) {
return aim[n - 2];
}
else {
return solution[n];
}
}
void zw_enshaw() {
double len = 2*me.radius;
double div = 3.5e-7;//?
num_of_aim = 1;
Position force = { 0.0,0.0,0.0 };
int n = num_of_food - 1;
for (;n >= 0;n--) {
Position point_to = minus(food[n].pos, me.pos);
double k = me_radius / length(point_to);
force = add(force, multiple(k*k*food[n].weight, point_to));
}
aim[0].weight = (int)((length(force))*div);
aim[0].pos = add(me.pos, force);
}
int initial() {
me = GetStatus()->objects[0];
num_of_aim = num_of_food = num_of_devour = 0;
emergency = code = 0;
me_radius = me.radius;
Position a[8]={{ 0, 0, 0 }, { 0, 0, kMapSize },{ 0, kMapSize, 0 },{ 0, kMapSize, kMapSize },{ kMapSize, 0, 0 },{ kMapSize, 0, kMapSize },{ kMapSize, kMapSize, 0 },{ kMapSize, kMapSize, kMapSize }};
const Map *map = GetMap();
int i = (*map).objects_number - 1;
double r=1;
for (; ~i; --i) {
switch ((*map).objects[i].type) {
case PLAYER:
if ((*map).objects[i].team_id == GetStatus()->team_id) break;
opponent_obj = (*map).objects[i];
code ^= OPPONENT;
break;
case ENERGY:
if ((*map).objects[i].pos.x - r * me_radius < 0) break;
if ((*map).objects[i].pos.y - r * me_radius < 0) break;
if ((*map).objects[i].pos.z - r * me_radius < 0) break;
if ((*map).objects[i].pos.x + r * me_radius > kMapSize) break;
if ((*map).objects[i].pos.y + r * me_radius > kMapSize) break;
if ((*map).objects[i].pos.z + r * me_radius > kMapSize) break;
food[num_of_food].weight = ENERGY_VALUE;
food[num_of_food].pos = (*map).objects[i].pos;
++num_of_food;
break;
case ADVANCED_ENERGY:
if (distance((*map).objects[i].pos, a[0]) < r * me_radius) break;
if (distance((*map).objects[i].pos, a[1]) < r * me_radius) break;
if (distance((*map).objects[i].pos, a[2]) < r * me_radius) break;
if (distance((*map).objects[i].pos, a[3]) < r * me_radius) break;
if (distance((*map).objects[i].pos, a[4]) < r * me_radius) break;
if (distance((*map).objects[i].pos, a[5]) < r * me_radius) break;
if (distance((*map).objects[i].pos, a[6]) < r * me_radius) break;
if (distance((*map).objects[i].pos, a[7]) < r * me_radius) break;
food[num_of_food].weight = ad_weight;
food[num_of_food].pos = (*map).objects[i].pos;
++num_of_food;
break;
case SOURCE:
break;
case DEVOUR:
devour[num_of_devour] = (*map).objects[i].pos;
++num_of_devour;
break;
case BOSS:
boss_obj = (*map).objects[i];
code ^= SEE_BOSS;
break;
default:
break;
}
}
return code;
}
int boss() {
if (boss_radius < me_radius * kEatableRatio) {
solution[SEE_BOSS].weight = 100;
solution[SEE_BOSS].pos = boss_obj.pos;
return 0;
}
else {
emergency = me_radius < boss_radius * kEatableRatio ? 1 : 0;
int tmp = short_attack(boss_obj);
if (!~tmp) tmp = long_attack(boss_obj);
return (~tmp) ? 1 : 0;
}
}
int long_attack(const Object& target)
{
if (me.skill_cd[LONG_ATTACK] == -1) {
return -1;
}
if (me.long_attack_casting != -1 /*|| me.short_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;
}
WAIT;
LongAttack(me.id, target.id);
GO;
return 0;
}
int short_attack(const Object& target)
{
if (me.skill_cd[SHORT_ATTACK] == -1) {
return -1;
}
if (me.long_attack_casting != -1 /*|| me.short_attack_casting != -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;
}
WAIT;
ShortAttack(me.id);
GO;
return 0;
}
int opponent()
{
const double safe_distance = 1000;
int result = 0;
Position temp={ kMapSize / 2, kMapSize / 2, kMapSize / 2 };
solution[OPPONENT].pos = temp;
solution[OPPONENT].weight = 0;
if (short_attack(opponent_obj) == 0 || long_attack(opponent_obj) == 0) {
result = 1;
}
if (opponent_obj.radius > me.radius * 1.1) {
solution[OPPONENT].pos = add(me.pos, minus(me.pos, opponent_obj.pos));
solution[OPPONENT].weight = 10000;
if (distance(me.pos, opponent_obj.pos) - opponent_obj.radius < safe_distance) {
emergency = 1;
}
}
else if (opponent_obj.radius < me.radius * 0.8) {
solution[OPPONENT].pos = add(me.pos, minus(opponent_obj.pos, me.pos));
solution[OPPONENT].weight = 15;
}
return result;
}