-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
1671 lines (1617 loc) · 61.6 KB
/
main.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
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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "include/template.hpp"
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <string>
#include <tuple>
#include <vector>
#define TIME1 0.15f
#define TIME2 0.2f
#define MAX_NODE_COUNT 20000
int positions[2][35][2] = {{{2, 9}, {4, 9}, {5, 9}, {5, 7}, {6, 9}, {5, 11}, {5, 6}, {6, 7}, {6, 11},
{5, 12}, {4, 3}, {5, 3}, {7, 8}, {7, 10}, {4, 15}, {5, 15}, {4, 2}, {6, 4},
{7, 5}, {8, 7}, {8, 11}, {7, 13}, {6, 14}, {4, 16}, {6, 1}, {6, 2}, {6, 16},
{6, 17}, {7, 1}, {8, 4}, {8, 14}, {7, 17}, {8, 2}, {8, 16}, {3, 9}},
{{16, 9}, {14, 9}, {13, 9}, {13, 7}, {12, 9}, {13, 11}, {12, 6}, {12, 7}, {12, 11},
{12, 12}, {14, 3}, {13, 3}, {10, 8}, {10, 10}, {14, 15}, {13, 15}, {13, 2}, {11, 4},
{11, 5}, {10, 7}, {10, 11}, {11, 13}, {11, 14}, {13, 16}, {12, 1}, {11, 2}, {11, 16},
{12, 17}, {11, 1}, {9, 4}, {9, 14}, {11, 17}, {9, 2}, {9, 16}, {15, 9}}};
bool emp_flag[34];
const int BASE = 0, C1 = 1, C2 = 2, C3 = 4, L1 = 3, L2 = 6, L3 = 7, R1 = 5, R2 = 9, R3 = 8, LL1 = 10, LL2 = 16,
LL3 = 11, RR1 = 14, RR2 = 23, RR3 = 15, M1 = 19, M2 = 12, M3 = 13, M4 = 20, ML1 = 17, ML2 = 18, MR1 = 22,
MR2 = 21, STORM = 34, FL1 = 24, FL2 = 25, FL3 = 28, FR1 = 27, FR2 = 26, FR3 = 31, F1 = 32, F2 = 29, F3 = 30,
F4 = 33;
long total_eva_time = 0;
int SIDE = 0;
int current_round;
int global_state = 0;
int now_hp;
int base_old_count;
int base_die_count;
bool attack_flag = false;
SuperWeaponType last_superweapon_type;
int last_superweapon_round = -1;
short reserved = 0;
int GROUP[4];
int GROUP_COUNT;
inline void get_group(int code) {
switch (code) {
case C1:
case C2:
case C3:
GROUP[0] = C1;
GROUP[1] = C2;
GROUP[2] = C3;
GROUP_COUNT = 3;
break;
case L1:
case L2:
case L3:
GROUP[0] = L1;
GROUP[1] = L2;
GROUP[2] = L3;
GROUP_COUNT = 3;
break;
case R1:
case R2:
case R3:
GROUP[0] = R1;
GROUP[1] = R2;
GROUP[2] = R3;
GROUP_COUNT = 3;
break;
case LL1:
case LL2:
case LL3:
GROUP[0] = LL1;
GROUP[1] = LL2;
GROUP[2] = LL3;
GROUP_COUNT = 3;
break;
case RR1:
case RR2:
case RR3:
GROUP[0] = RR1;
GROUP[1] = RR2;
GROUP[2] = RR3;
GROUP_COUNT = 3;
break;
case M1:
case M2:
case M3:
case M4:
GROUP[0] = M1;
GROUP[1] = M2;
GROUP[2] = M3;
GROUP[3] = M4;
GROUP_COUNT = 4;
break;
case ML1:
case ML2:
GROUP[0] = ML1;
GROUP[1] = ML2;
GROUP_COUNT = 2;
break;
case MR1:
case MR2:
GROUP[0] = MR1;
GROUP[1] = MR2;
GROUP_COUNT = 2;
break;
case FL1:
case FL2:
case FL3:
GROUP[0] = FL1;
GROUP[1] = FL2;
GROUP[2] = FL3;
GROUP_COUNT = 3;
break;
case FR1:
case FR2:
case FR3:
GROUP[0] = FR1;
GROUP[1] = FR2;
GROUP[2] = FR3;
GROUP_COUNT = 3;
break;
case F1:
case F2:
GROUP[0] = F1;
GROUP[1] = F2;
GROUP_COUNT = 2;
break;
case F3:
case F4:
GROUP[0] = F3;
GROUP[1] = F4;
GROUP_COUNT = 2;
break;
}
}
inline Tower get_tower(int x, int y, const GameInfo &game_info) {
for (auto &tower : game_info.towers) {
if (tower.x == x && tower.y == y) {
return tower;
}
}
return Tower(-1, -1, -1, -1);
}
inline Tower get_tower(int id, const GameInfo &game_info) {
for (auto &tower : game_info.towers) {
if (tower.id == id) {
return tower;
}
}
return Tower(-1, -1, -1, -1);
}
inline int nearest_to_base_dis(const GameInfo &game_info) {
int dis = 100;
for (auto &ant : game_info.ants) {
if (ant.player == SIDE) {
dis = std::min(distance(ant.x, ant.y, positions[!SIDE][BASE][0], positions[!SIDE][BASE][1]), dis);
}
}
return dis;
}
inline short safe_coin(const GameInfo &info) {
int res = 0;
if (info.super_weapon_cd[!SIDE][SuperWeaponType::EmpBlaster] >= 90)
return 0;
else if (info.super_weapon_cd[!SIDE][SuperWeaponType::EmpBlaster] > 0)
return std::max((int)(std::min(info.coins[!SIDE], (short)(149)) -
info.super_weapon_cd[!SIDE][SuperWeaponType::EmpBlaster] * 1.66),
0);
else
return std::min(info.coins[!SIDE], (short)(149));
}
inline short get_safe_val(const GameInfo &info) { return std::min(0, info.coins[SIDE] - safe_coin(info)); }
inline char nearest_ant_dis(const GameInfo &info) {
char res = 32, dis;
for (int i = 0; i < info.ants.size(); i++) {
if (info.ants[i].player == !SIDE) {
dis = distance(info.ants[i].x, info.ants[i].y, positions[SIDE][BASE][0], positions[SIDE][BASE][1]);
if (dis < res)
res = dis;
}
}
return res;
}
bool action_r1;
OperationType action_r2;
int action_r3, action_r4;
inline void action(int code, int type, const GameInfo &game_info, int &coins, int &towers, int up = 0,
int ignorecode = -1) {
auto pos = positions[SIDE][code];
if (type == 1) { // build
if (coins < game_info.build_tower_cost(towers)) {
action_r1 = false;
return;
}
get_group(code);
for (int i = 0; i < GROUP_COUNT; i++) {
if (GROUP[i] == ignorecode)
continue;
if (game_info.building_tag[positions[SIDE][GROUP[i]][0]][positions[SIDE][GROUP[i]][1]] !=
BuildingType::Empty) {
action_r1 = false;
return;
}
}
coins -= game_info.build_tower_cost(towers);
towers++;
action_r1 = true;
action_r2 = OperationType::BuildTower;
action_r3 = pos[0];
action_r4 = pos[1];
return;
} else if (type == 2) { // upgrade
if (game_info.building_tag[pos[0]][pos[1]] == BuildingType::Empty) {
action_r1 = false;
return;
}
Tower tower = get_tower(pos[0], pos[1], game_info);
TowerType target_type;
// if (code == C1) {
// if (tower.type == TowerType::Basic) {
// target_type = TowerType::Mortar;
// } else if (tower.type == TowerType::Mortar) {
// target_type = TowerType::MortarPlus;
// } else {
// action_r1 = false;
// return;
// }
// } else {
if (tower.type / 10 > 0) {
action_r1 = false;
return;
}
if (tower.type == TowerType::Basic) {
switch (up) {
case 0:
target_type = TowerType::Heavy;
break;
case 1:
target_type = TowerType::Mortar;
break;
case 2:
target_type = TowerType::Quick;
break;
}
}
if (tower.type == TowerType::Heavy) {
switch (up) {
case 0:
target_type = TowerType::HeavyPlus;
break;
case 1:
target_type = TowerType::Cannon;
break;
case 2:
target_type = TowerType::Ice;
break;
}
}
if (tower.type == TowerType::Mortar) {
switch (up) {
case 0:
target_type = TowerType::MortarPlus;
break;
case 1:
target_type = TowerType::Missile;
break;
case 2:
target_type = TowerType::Pulse;
break;
}
}
if (tower.type == TowerType::Quick) {
switch (up) {
case 0:
target_type = TowerType::QuickPlus;
break;
case 1:
target_type = TowerType::Double;
break;
case 2:
target_type = TowerType::Sniper;
break;
}
}
// }
if (coins < game_info.upgrade_tower_cost(target_type)) {
action_r1 = false;
return;
} else {
coins -= game_info.upgrade_tower_cost(target_type);
action_r1 = true;
action_r2 = OperationType::UpgradeTower;
action_r3 = tower.id;
action_r4 = target_type;
return;
}
} else if (type == 3) { // destroy
if (game_info.building_tag[pos[0]][pos[1]] == BuildingType::Empty) {
action_r1 = false;
return;
}
Tower tower = get_tower(pos[0], pos[1], game_info);
if (tower.type != TowerType::Basic) {
action_r1 = false;
return;
}
coins += game_info.destroy_tower_income(towers);
towers--;
action_r1 = true;
action_r2 = OperationType::DowngradeTower;
action_r3 = tower.id;
action_r4 = -1;
return;
} else if (type == 4) { // downgrade
if (game_info.building_tag[pos[0]][pos[1]] == BuildingType::Empty) {
action_r1 = false;
return;
}
Tower tower = get_tower(pos[0], pos[1], game_info);
if (tower.type == TowerType::Basic) {
action_r1 = false;
return;
}
coins += game_info.downgrade_tower_income(tower.type);
action_r1 = true;
action_r2 = OperationType::DowngradeTower;
action_r3 = tower.id;
action_r4 = -1;
return;
}
action_r1 = false;
return;
}
std::vector<std::vector<Operation>> series_action(int tac, const GameInfo &game_info) {
std::vector<std::vector<Operation>> ops;
bool valid, new_valid;
OperationType type, new_type;
int arg0, arg1, new_arg0, new_arg1;
if (tac == 0) { // build 1
int valid_codes[] = {C1, C2, C3, LL1, LL2, LL3, RR1, RR2, RR3, ML1, ML2, MR1, MR2, L1, L2,
L3, R1, R2, R3, M1, M2, M3, M4, FL1, FL2, FL3, FR1, FR2, FR3};
for (auto &valid_code : valid_codes) {
if (emp_flag[valid_code])
continue;
int coins = game_info.coins[SIDE], towers = game_info.tower_num_of_player(SIDE);
std::vector<Operation> op;
action(valid_code, 1, game_info, coins, towers);
valid = action_r1;
type = action_r2;
arg0 = action_r3;
arg1 = action_r4;
if (valid) {
op.emplace_back(type, arg0, arg1);
ops.emplace_back(op);
}
}
} else if (tac == 1) { // upgrade 1;
int valid_codes[] = {C1, C2, C3, LL1, LL2, LL3, RR1, RR2, RR3, ML1, ML2, MR1, MR2, L1, L2,
L3, R1, R2, R3, M1, M2, M3, M4, FL1, FL2, FL3, FR1, FR2, FR3};
for (auto &valid_code : valid_codes) {
if (emp_flag[valid_code])
continue;
for (int j = 0; j < 3; j++) {
// if (valid_code == C1 && j > 0)
// continue;
int coins = game_info.coins[SIDE], towers = game_info.tower_num_of_player(SIDE);
std::vector<Operation> op;
action(valid_code, 2, game_info, coins, towers, j);
valid = action_r1;
type = action_r2;
arg0 = action_r3;
arg1 = action_r4;
if (valid) {
op.emplace_back(type, arg0, arg1);
ops.emplace_back(op);
}
}
}
} else if (tac == 2) { // downgrade 1 and build 1
int valid_codes[] = {C1, C2, C3, LL1, LL2, LL3, RR1, RR2, RR3, ML1, ML2, MR1, MR2, L1, L2,
L3, R1, R2, R3, M1, M2, M3, M4, FL1, FL2, FL3, FR1, FR2, FR3};
for (auto &valid_code : valid_codes) {
if (emp_flag[valid_code])
continue;
int coins = game_info.coins[SIDE], towers = game_info.tower_num_of_player(SIDE);
action(valid_code, 4, game_info, coins, towers);
valid = action_r1;
type = action_r2;
arg0 = action_r3;
arg1 = action_r4;
if (valid) {
for (auto &code2 : valid_codes) {
if (emp_flag[code2])
continue;
if (code2 == valid_code)
continue;
std::vector<Operation> op;
int new_coins = coins, new_towers = towers;
action(code2, 1, game_info, new_coins, new_towers);
new_valid = action_r1;
new_type = action_r2;
new_arg0 = action_r3;
new_arg1 = action_r4;
if (new_valid) {
op.emplace_back(type, arg0, arg1);
op.emplace_back(new_type, new_arg0, new_arg1);
ops.emplace_back(op);
}
}
}
}
} else if (tac == 3) { // destroy 1
int valid_codes[] = {C1, C2, C3, LL1, LL2, LL3, RR1, RR2, RR3, ML1, ML2, MR1, MR2, L1, L2,
L3, R1, R2, R3, M1, M2, M3, M4, FL1, FL2, FL3, FR1, FR2, FR3};
for (auto &valid_code : valid_codes) {
if (emp_flag[valid_code])
continue;
std::vector<Operation> op;
int coins = game_info.coins[SIDE], towers = game_info.tower_num_of_player(SIDE);
action(valid_code, 3, game_info, coins, towers);
valid = action_r1;
type = action_r2;
arg0 = action_r3;
arg1 = action_r4;
if (valid) {
op.emplace_back(type, arg0, arg1);
ops.emplace_back(op);
}
}
} else if (tac == 4) { // destroy 1 and upgrade 1
int valid_codes[] = {C1, C2, C3, LL1, LL2, LL3, RR1, RR2, RR3, ML1, ML2, MR1, MR2, L1, L2,
L3, R1, R2, R3, M1, M2, M3, M4, FL1, FL2, FL3, FR1, FR2, FR3};
for (auto &valid_code : valid_codes) {
if (emp_flag[valid_code])
continue;
int coins = game_info.coins[SIDE], towers = game_info.tower_num_of_player(SIDE);
action(valid_code, 3, game_info, coins, towers);
valid = action_r1;
type = action_r2;
arg0 = action_r3;
arg1 = action_r4;
if (valid) {
for (auto &code2 : valid_codes) {
if (emp_flag[code2])
continue;
if (code2 == valid_code)
continue;
for (int j = 0; j < 3; j++) {
std::vector<Operation> op;
int new_coins = coins, new_towers = towers;
action(code2, 2, game_info, new_coins, new_towers, j);
new_valid = action_r1;
new_type = action_r2;
new_arg0 = action_r3;
new_arg1 = action_r4;
if (new_valid) {
op.emplace_back(type, arg0, arg1);
op.emplace_back(new_type, new_arg0, new_arg1);
ops.emplace_back(op);
}
}
}
}
}
} else if (tac == 5) { // downgrade 1
int valid_codes[] = {C1, C2, C3, LL1, LL2, LL3, RR1, RR2, RR3, ML1, ML2, MR1, MR2, L1, L2,
L3, R1, R2, R3, M1, M2, M3, M4, FL1, FL2, FL3, FR1, FR2, FR3};
for (auto &valid_code : valid_codes) {
if (emp_flag[valid_code])
continue;
int coins = game_info.coins[SIDE], towers = game_info.tower_num_of_player(SIDE);
std::vector<Operation> op;
action(valid_code, 4, game_info, coins, towers);
valid = action_r1;
type = action_r2;
arg0 = action_r3;
arg1 = action_r4;
if (valid) {
op.emplace_back(type, arg0, arg1);
ops.emplace_back(op);
}
}
} else if (tac == 6) { // destroy 1 and build 1
int valid_codes[] = {C1, C2, C3, LL1, LL2, LL3, RR1, RR2, RR3, ML1, ML2, MR1, MR2, L1, L2,
L3, R1, R2, R3, M1, M2, M3, M4, FL1, FL2, FL3, FR1, FR2, FR3};
for (auto &valid_code : valid_codes) {
if (emp_flag[valid_code])
continue;
int coins = game_info.coins[SIDE], towers = game_info.tower_num_of_player(SIDE);
action(valid_code, 3, game_info, coins, towers);
valid = action_r1;
type = action_r2;
arg0 = action_r3;
arg1 = action_r4;
if (valid) {
for (auto &code2 : valid_codes) {
if (emp_flag[code2])
continue;
if (code2 == valid_code)
continue;
std::vector<Operation> op;
int new_coins = coins, new_towers = towers;
action(code2, 1, game_info, new_coins, new_towers, 0, valid_code);
new_valid = action_r1;
new_type = action_r2;
new_arg0 = action_r3;
new_arg1 = action_r4;
if (new_valid) {
op.emplace_back(type, arg0, arg1);
op.emplace_back(new_type, new_arg0, new_arg1);
ops.emplace_back(op);
}
}
}
}
} else if (tac == 7) { // downgrade 1 and upgrade 1
int valid_codes[] = {C1, C2, C3, LL1, LL2, LL3, RR1, RR2, RR3, ML1, ML2, MR1, MR2, L1, L2,
L3, R1, R2, R3, M1, M2, M3, M4, FL1, FL2, FL3, FR1, FR2, FR3};
for (auto &valid_code : valid_codes) {
if (emp_flag[valid_code])
continue;
int coins = game_info.coins[SIDE], towers = game_info.tower_num_of_player(SIDE);
action(valid_code, 4, game_info, coins, towers);
valid = action_r1;
type = action_r2;
arg0 = action_r3;
arg1 = action_r4;
if (valid) {
for (auto &code2 : valid_codes) {
if (emp_flag[code2])
continue;
if (code2 == valid_code)
continue;
for (int j = 0; j < 3; j++) {
std::vector<Operation> op;
int new_coins = coins, new_towers = towers;
action(code2, 2, game_info, new_coins, new_towers, j);
new_valid = action_r1;
new_type = action_r2;
new_arg0 = action_r3;
new_arg1 = action_r4;
if (new_valid) {
op.emplace_back(type, arg0, arg1);
op.emplace_back(new_type, new_arg0, new_arg1);
ops.emplace_back(op);
}
}
}
}
}
}
return ops;
}
int node_count = 0;
struct Node {
Simulator s;
int id;
int parent;
std::vector<int> children;
Operation actions[3];
float node_val;
float max_val;
short round;
short loss;
short max_expand;
short expand_count;
short fail_round;
bool danger;
bool safe;
char action_num;
char dis_vals[60];
Node() {}
Node(const Simulator &sim) : s(sim) {
round = s.info.round;
children.clear();
loss = 0;
action_num = 0;
expand_count = 0;
max_expand = 0;
danger = false;
fail_round = 0;
safe = true;
}
float evaluate();
void expand(bool is_root = false);
};
Node *nodes[MAX_NODE_COUNT];
inline void del() {
for (int i = 0; i < node_count; i++) {
delete nodes[i];
}
}
int tower_positions[4][2];
int tower_position_count;
float Node::evaluate() {
auto new_s = s;
auto &info = new_s.info;
if (info.round - current_round < 60)
dis_vals[info.round - current_round] = nearest_ant_dis(info);
int safe_val;
if (current_round > 60) {
safe_val = get_safe_val(info);
safe = safe_val == 0;
}
short ruin_round = current_round + 60;
bool fail_flag = false;
if (info.bases[SIDE].hp <= now_hp - 1) {
fail_flag = true;
ruin_round = fail_round;
}
if (!fail_flag) {
fail_round = current_round + 60;
for (int i = info.round; i < current_round + 60; i++) {
if (!new_s.fast_next_round(SIDE)) {
break;
}
dis_vals[i - current_round] = nearest_ant_dis(info);
// if (id == 1) {
// info.cerrdump();
// }
if (info.bases[SIDE].hp <= now_hp - 1) {
fail_round = info.round;
if (info.bases[SIDE].hp <= now_hp - 2)
ruin_round = fail_round;
break;
}
}
}
if (info.bases[SIDE].hp > now_hp - 2) {
for (int i = info.round; i < current_round + 60; i++) {
if (!new_s.fast_next_round(SIDE)) {
break;
}
dis_vals[i - current_round] = nearest_ant_dis(info);
if (info.bases[SIDE].hp <= now_hp - 2) {
ruin_round = info.round;
break;
}
}
}
float ant_ratio = 0;
switch (info.bases[!SIDE].ant_level) {
case 0:
ant_ratio = 3.0;
break;
case 1:
ant_ratio = 5.0;
break;
case 2:
ant_ratio = 7.0;
break;
}
node_val = (info.bases[SIDE].hp - now_hp) + (fail_round - current_round) * 0.8 + (ruin_round - fail_round) * 0.1 -
loss * 1.5 + 20;
if (global_state == 0) {
node_val += -(info.old_count[!SIDE] - base_old_count) * ant_ratio * 2 +
(info.die_count[!SIDE] - base_die_count) * ant_ratio * 1.5;
}
if (fail_round - current_round <= 16) {
danger = true;
node_val -= 500;
if (ruin_round - fail_round <= 8) {
node_val -= 300;
}
}
if (!safe && !danger && global_state >= 0) {
node_val += (-40 + safe_val / 5) * std::min((current_round - 60) / 30, 1);
}
int corrected_tower_num = info.tower_num_of_player(SIDE);
node_val -= (std::pow(2, corrected_tower_num) - 1) * 15 * 0.2 * 0.75;
tower_position_count = 0;
bool distanced = false;
for (int i = 0; i < info.towers.size(); i++) {
auto &tower = info.towers[i];
// if (tower.player == SIDE && !(tower.x == positions[SIDE][C1][0] && tower.y == positions[SIDE][C1][1])) {
if (tower.player == SIDE) {
if (tower.type > 0 && tower.type / 10 == 0) {
node_val -= 60 * 0.2 * 0.75;
} else if (tower.type / 10 > 0) {
node_val -= 260 * 0.2 * 0.75;
}
tower_positions[tower_position_count][0] = tower.x;
tower_positions[tower_position_count][1] = tower.y;
tower_position_count++;
}
}
for (int i = 0; i < tower_position_count - 1; i++) {
for (int j = i + 1; j < tower_position_count; j++) {
int dis =
distance(tower_positions[i][0], tower_positions[i][1], tower_positions[j][0], tower_positions[j][1]);
if (dis <= 3) {
node_val -= 5;
} else if (dis <= 6) {
node_val -= 2;
} else {
distanced = true;
}
}
}
if (corrected_tower_num >= 3 && !distanced) {
node_val -= 20;
}
for (int i = 0; i < tower_position_count; i++) {
int base_dis = distance(tower_positions[i][0], tower_positions[i][1], info.bases[SIDE].x, info.bases[SIDE].y);
node_val += base_dis * 0.4;
}
if (global_state >= 0) {
bool close_flag = false;
for (int i = 0; i < std::min(60, info.round - current_round - 4); i++) {
if (dis_vals[i] <= 3) {
close_flag = true;
}
switch (dis_vals[i]) {
case 5:
node_val -= 0.2;
break;
case 4:
node_val -= 0.5;
case 3:
case 2:
case 1:
node_val -= 2;
break;
default:
break;
}
}
if (close_flag) {
node_val -= 20;
}
int ant_count = 0;
float mis_val = 0;
for (auto &ant : info.ants) {
if (ant.player == !SIDE) {
mis_val += 32 - ant.age - distance(ant.x, ant.y, info.bases[SIDE].x, info.bases[SIDE].y) * 1.5;
ant_count++;
}
}
if (ant_count > 0 && current_round >= 20)
node_val += mis_val / ant_count * 0.5;
}
max_val = node_val;
return node_val;
}
void Node::expand(bool is_root) {
if (s.info.round >= MAX_ROUND || s.info.bases[SIDE].hp <= 0 || s.info.bases[!SIDE].hp <= 0) {
return;
}
if (!is_root) {
if (s.info.round - current_round < 60)
dis_vals[s.info.round - current_round] = nearest_ant_dis(s.info);
if (!s.fast_next_round(SIDE)) {
return;
}
}
std::vector<std::vector<Operation>> ops;
memset(emp_flag, 0, sizeof(emp_flag));
for (auto &sw : s.info.super_weapons) {
if (sw.player == !SIDE && sw.type == SuperWeaponType::EmpBlaster) {
for (int i = 0; i < 34; i++) {
if (distance(sw.x, sw.y, positions[SIDE][i][0], positions[SIDE][i][1]) <= 3)
emp_flag[i] = 1;
}
break;
}
}
// std::cerr << "start find actions" << std::endl;
for (int i = 0; i < 8; i++) {
// if (action_num > 0 && expand_count < 5 && (i == 3 || i == 4))
// continue;
if (action_num > 0 && (i == 3 || i == 5))
continue;
if (action_num == 1 && actions[0].type == OperationType::BuildTower && expand_count < 2 &&
(i == 3 || i == 4 || i == 6))
continue;
if (action_num == 1 && actions[0].type == OperationType::UpgradeTower && expand_count < 2 && i == 2)
continue;
if (action_num == 2 && actions[1].type == OperationType::BuildTower && expand_count < 2 &&
(i == 3 || i == 4 || i == 6))
continue;
if (s.info.tower_num_of_player(SIDE) >= 4 && (i == 0 || i == 2))
continue;
auto res = series_action(i, s.info);
ops.insert(ops.end(), res.begin(), res.end());
}
if (is_root) {
Node *empty = new Node(s);
empty->id = node_count;
empty->parent = id;
empty->evaluate();
nodes[node_count++] = empty;
children.emplace_back(empty->id);
}
for (auto &op : ops) {
if (node_count >= MAX_NODE_COUNT - 10)
break;
Node *child = new Node(s);
auto &new_s = child->s;
child->id = node_count;
child->parent = id;
child->loss = loss;
child->action_num = op.size();
child->fail_round = fail_round;
child->node_val = child->max_val = -1e9;
if (s.info.round > current_round) {
memcpy(child->dis_vals, dis_vals, std::min(60, s.info.round - current_round));
}
auto &info = new_s.info;
new_s.operations[0].clear();
new_s.operations[1].clear();
for (int i = 0; i < op.size(); i++) {
// if (current_round > 170)
// std::cerr << op[i].type << " " << op[i].arg0 << " " << op[i].arg1 << " ";
child->actions[i] = op[i];
if (op[i].type == OperationType::DowngradeTower) {
auto tower = get_tower(op[i].arg0, info);
if (tower.type == TowerType::Basic) {
child->loss += info.build_tower_cost(info.tower_num_of_player(SIDE)) * 0.2;
} else {
child->loss += info.upgrade_tower_cost(tower.type) * 0.2;
}
}
new_s.add_operation_of_player(SIDE, op[i]);
}
new_s.apply_operations_of_player(SIDE);
// auto start_time = clock();
int child_val = child->evaluate();
// total_eva_time += clock() - start_time;
if (child_val > max_val) {
max_val = child_val;
max_expand = expand_count + 1;
}
nodes[node_count++] = child;
children.emplace_back(child->id);
}
if (is_root)
if (!s.fast_next_round(SIDE)) {
return;
}
expand_count++;
}
inline bool select_expand() {
auto &root = nodes[0];
int target_id = -1;
float val = -1e9;
if (root->children.size() == 0)
return false;
for (auto &child_id : root->children) {
auto &child = nodes[child_id];
float value = -child->expand_count;
if (child_id == 0) {
value += reserved;
}
if (child->children.size() == 0)
value += 1000;
if (child->danger) {
value += 20;
}
if (!child->safe) {
value -= 20;
}
if (value > val) {
val = value;
target_id = child_id;
}
}
// std::cerr << "expand " << target_id << std::endl;
nodes[target_id]->expand();
return true;
}
void supp_expand(int bias) {
auto &root = nodes[0];
if (root->children.size() == 0)
return;
for (auto &child_id : root->children) {
auto &child = nodes[child_id];
int fail_round = child->fail_round;
if (fail_round - current_round <= 24) {
int now_round = child->s.info.round;
int target_round = std::min(MAX_ROUND - 1, fail_round - bias);
if (now_round >= target_round) {
continue;
}
for (int i = now_round; i < target_round - 1; i++) {
if (!child->s.fast_next_round(SIDE)) {
break;
}
}
child->expand();
}
}
// std::cerr << "expand " << target_id << std::endl;
// nodes[target_id]->expand();
}
std::vector<Operation> try_sell_all(int &coins, int &towers, int coin_need, const GameInfo &info) {
std::vector<Operation> ops;
int max_coins = coins;
int valid_tower_num = 0;
for (int i = 0; i < info.towers.size(); i++) {
auto &tower = info.towers[i];
if (tower.player == SIDE && !info.is_shielded_by_emp(tower)) {
if (tower.type == TowerType::Basic) {
coins += info.destroy_tower_income(towers);
towers--;
} else {
coins += info.downgrade_tower_income(tower.type);
if (tower.type / 10 == 0) {
max_coins += 48;
} else {
max_coins += 48 + 160;
}
}
ops.emplace_back(OperationType::DowngradeTower, tower.id, -1);
valid_tower_num++;
}
if (coins >= coin_need) {
return ops;
}
}
max_coins +=
(std::pow(2, info.tower_num_of_player(SIDE)) - std::pow(2, info.tower_num_of_player(SIDE) - valid_tower_num)) *
12;
if (max_coins >= coin_need) {
return ops;
}
return {};
}
std::vector<Operation> try_sell(int &coins, int &towers, int coin_need, const GameInfo &info) {
std::vector<int> tower_ids;
for (int i = 0; i < info.towers.size(); i++) {
auto &tower = info.towers[i];
if (tower.player == SIDE && !info.is_shielded_by_emp(tower)) {
tower_ids.emplace_back(tower.id);
}
}
int valid_count = tower_ids.size();
if (valid_count > 0) {
Simulator s(info);
int f_round = 48;
for (int i = 1; i <= 48; i++) {
if (!s.fast_next_round(SIDE)) {
break;
}
if (s.info.bases[SIDE].hp < info.bases[SIDE].hp) {
f_round = i;
break;
}
}
int *idx = new int[valid_count];
for (int i = 0; i < valid_count; i++) {
idx[i] = i;
}
int max_round = -1, max_coins;
std::vector<Operation> best_ops;
while (true) {
std::vector<Operation> ops;
Simulator new_s(info);
auto &new_info = new_s.info;
int new_coin = coins, new_towers = towers;
bool valid = false;
for (int i = 0; i < valid_count; i++) {
int id = tower_ids[idx[i]];
auto tower = get_tower(id, new_info);
if (tower.type == TowerType::Basic) {
new_coin += new_info.destroy_tower_income(new_towers);
new_towers--;
} else {
new_coin += new_info.downgrade_tower_income(tower.type);
}
ops.emplace_back(OperationType::DowngradeTower, id, -1);
if (new_coin >= coin_need) {
valid = true;