-
Notifications
You must be signed in to change notification settings - Fork 1
/
ai-sample.cpp
2063 lines (2011 loc) · 79.9 KB
/
ai-sample.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 "ai_client.h"
#include "gameunit.h"
#include "card.hpp"
#include "calculator.h"
#include <fstream>
#include <stdlib.h>
#include <iostream>
#include <random>
#include <time.h>
using gameunit::Pos;
using gameunit::Unit;
using calculator::all_pos_in_map;
using calculator::cube_distance;
using calculator::cube_reachable;
using calculator::reachable;
using calculator::units_in_range;
using card::CARD_DICT;
using std::cerr;
using std::default_random_engine;
using std::endl;
using std::get;
using std::make_tuple;
using std::map;
using std::string;
using std::uniform_int_distribution;
using std::vector;
struct pos_with_value
{
Pos pos;
int value1;
int value2;
};
class AI : public AiClient
{
private:
Pos miracle_pos;
Pos enemy_pos;
Pos target_barrack;
Pos pos_1, pos_2, pos_3, pos_4, pos_5, pos_6, pos_7, pos_8, pos_9, pos_a, pos_b, pos_c, pos_d, pos_e, pos_p, pos_q, pos_r;
int enemy_miracle_hp;
int gj_summon_level, ms_summon_level, js_summon_level;
//各个生物召唤出来的等级
int enemy_camp;
Unit archer1, archer2, js1, ms1, js2;
enum attack_modes
{
ATTACK,
DEFENSE,
PROTECT
} attack_mode;
Pos posShift(Pos pos, string direct);
Pos posShift_n(Pos pos, string direct, int num);
void change_attack_mode();
void game_init_pos();
void set_summon_level(int level);
void set_attack_mode();
void attack_task(); //攻击指令
void force_attack_task(); //发动勇猛,能打到则攻击
void move_task(); //移动
void set_summon_level_by_round(int round);
void artifacts_task(); //使用神器
void summon_task(); //召唤生物
bool near_my_miracle(Unit unit); //检测生物距离我方基地在7格以内
bool canAttackMiracle(Unit a);
public:
//选择初始卡组
string archer_str = "Archer", sworderman_str = "Swordsman", pristest_str = "Priest", inferno_str = "Inferno";
void chooseCards(); //(根据初始阵营)选择初始卡组
void play(); //玩家需要编写的ai操作函数
void battle(); //处理生物的战斗
void march(); //处理生物的移动
int probobility(int p)
{
static default_random_engine g(static_cast<unsigned int>(time(nullptr)));
int randnum = uniform_int_distribution<>(0, 100)(g);
cerr << "random num" << p << endl;
if (randnum <= p)
{
return 1;
}
else
{
return 0;
}
}
};
bool AI::canAttackMiracle(Unit a)
{
//能否达到地方基地
int dis = cube_distance(a.pos, enemy_pos);
if (dis >= a.atk_range[0] && dis <= a.atk_range[1])
{
return true;
}
else
{
return false;
}
}
bool AI::near_my_miracle(Unit unit)
{
if (cube_distance(unit.pos, miracle_pos) <= 6)
{
return true;
}
else
{
return false;
}
}
void AI::set_attack_mode()
{
int protect_mode_flag = 0;
int enemy_population; //敌方人口数量
int my_population; //我方人口数量
auto enemy_allay_list = getUnitsByCamp(enemy_camp);
auto my_allay_list = getUnitsByCamp(my_camp);
enemy_population = enemy_allay_list.size();
my_population = my_allay_list.size();
for (auto allay : enemy_allay_list)
{
if (cube_distance(allay.pos, miracle_pos) <= 3)
{
protect_mode_flag = 1;
break;
}
}
if (protect_mode_flag == 1)
{ //protect模式,有敌方单位在3格以内
attack_mode = PROTECT;
cerr << "PROTECT mode" << endl;
return;
}
if (my_population >= enemy_population - 1)
{
attack_mode = ATTACK;
cerr << "ATTACK mode" << endl;
return;
}
else
{
attack_mode = DEFENSE;
cerr << "DENFENSE mode" << endl;
return;
}
}
void AI::set_summon_level_by_round(int round)
{
if (round == 0 || round == 1)
{
set_summon_level(1);
}
else if (round == 10 || round == 11)
{
set_summon_level(2);
}
else if (round == 14 || round == 15)
{
set_summon_level(3);
}
else if (round >= 24)
{
set_summon_level(4);
}
}
void AI::set_summon_level(int level)
{
if (level == 1)
{
gj_summon_level = 1;
ms_summon_level = 2;
js_summon_level = 1;
}
else if (level == 2)
{
gj_summon_level = 2;
ms_summon_level = 2;
js_summon_level = 2;
}
else if (level == 3)
{
gj_summon_level = 3;
ms_summon_level = 2;
js_summon_level = 2;
}
else if (level == 4)
{
gj_summon_level = 3;
js_summon_level = 2;
if (attack_mode == ATTACK)
{
ms_summon_level = 2;
}
else
{
ms_summon_level = 2;
}
}
}
Pos AI::posShift_n(Pos pos, string direct, int num)
{
//偏移n次
if (num <= 0)
return pos;
else
{
return posShift_n(posShift(pos, direct), direct, num - 1);
}
}
void AI::chooseCards()
{
// (根据初始阵营)选择初始卡组
/*
* artifacts和creatures可以修改
* 【进阶】在选择卡牌时,就已经知道了自己的所在阵营和先后手,因此可以在此处根据先后手的不同设置不同的卡组和神器
*/
//地狱火 弓箭 剑士 牧师
my_artifacts = {"InfernoFlame"};
my_creatures = {"Archer", "Swordsman", "Priest"};
init();
}
void AI::game_init_pos()
{
attack_mode = DEFENSE;
//先确定自己的基地、对方的基地
miracle_pos = map.miracles[my_camp].pos;
enemy_pos = map.miracles[my_camp ^ 1].pos;
enemy_camp = my_camp ^ 1;
//设定目标驻扎点为最近的驻扎点
target_barrack = map.barracks[0].pos;
//确定离自己基地最近的驻扎点的位置
for (const auto &barrack : map.barracks)
{
if (cube_distance(miracle_pos, barrack.pos) <
cube_distance(miracle_pos, target_barrack))
target_barrack = barrack.pos;
}
enemy_miracle_hp = 30;
pos_a = posShift_n(miracle_pos, "FF", 1);
pos_b = posShift_n(miracle_pos, "IF", 1);
pos_c = posShift_n(miracle_pos, "SF", 1);
pos_d = posShift_n(pos_b, "IB", 1);
pos_e = posShift_n(pos_c, "SB", 1);
pos_p = posShift_n(target_barrack, "IF", 1);
pos_q = posShift_n(target_barrack, "FF", 1);
pos_r = posShift_n(target_barrack, "SF", 1);
pos_1 = posShift_n(pos_d, "FF", 3);
pos_6 = posShift_n(pos_1, "SF", 1);
pos_5 = posShift_n(pos_6, "SF", 1);
pos_2 = posShift_n(pos_6, "IF", 3);
pos_3 = posShift_n(pos_2, "FF", 1);
pos_7 = posShift_n(pos_5, "SF", 2);
pos_8 = posShift_n(pos_5, "IF", 2);
pos_4 = posShift_n(pos_7, "FF", 1);
pos_9 = posShift_n(pos_a, "SF", 3);
cerr << "pos_a "
<< get<0>(pos_a) << " "
<< get<1>(pos_a) << " "
<< get<2>(pos_a) << " "
<< endl;
cerr << "pos_b "
<< get<0>(pos_b) << " "
<< get<1>(pos_b) << " "
<< get<2>(pos_b) << " "
<< endl;
cerr << "pos_c "
<< get<0>(pos_c) << " "
<< get<1>(pos_c) << " "
<< get<2>(pos_c) << " "
<< endl;
cerr << "pos_d "
<< get<0>(pos_d) << " "
<< get<1>(pos_d) << " "
<< get<2>(pos_d) << " "
<< endl;
cerr << "pos_e "
<< get<0>(pos_e) << " "
<< get<1>(pos_e) << " "
<< get<2>(pos_e) << " "
<< endl;
cerr << "pos_p "
<< get<0>(pos_p) << " "
<< get<1>(pos_p) << " "
<< get<2>(pos_p) << " "
<< endl;
cerr << "pos_q "
<< get<0>(pos_q) << " "
<< get<1>(pos_q) << " "
<< get<2>(pos_q) << " "
<< endl;
cerr << "pos_r "
<< get<0>(pos_r) << " "
<< get<1>(pos_r) << " "
<< get<2>(pos_r) << " "
<< endl;
cerr << "pos_1 "
<< get<0>(pos_1) << " "
<< get<1>(pos_1) << " "
<< get<2>(pos_1) << " "
<< endl;
cerr << "pos_2 "
<< get<0>(pos_2) << " "
<< get<1>(pos_2) << " "
<< get<2>(pos_2) << " "
<< endl;
cerr << "pos_3 "
<< get<0>(pos_3) << " "
<< get<1>(pos_3) << " "
<< get<2>(pos_3) << " "
<< endl;
cerr << "pos_4 "
<< get<0>(pos_4) << " "
<< get<1>(pos_4) << " "
<< get<2>(pos_4) << " "
<< endl;
cerr << "pos_5 "
<< get<0>(pos_5) << " "
<< get<1>(pos_5) << " "
<< get<2>(pos_5) << " "
<< endl;
cerr << "pos_6 "
<< get<0>(pos_6) << " "
<< get<1>(pos_6) << " "
<< get<2>(pos_6) << " "
<< endl;
cerr << "pos_7 "
<< get<0>(pos_7) << " "
<< get<1>(pos_7) << " "
<< get<2>(pos_7) << " "
<< endl;
cerr << "pos_8 "
<< get<0>(pos_8) << " "
<< get<1>(pos_8) << " "
<< get<2>(pos_8) << " "
<< endl;
cerr << "pos_9 "
<< get<0>(pos_9) << " "
<< get<1>(pos_9) << " "
<< get<2>(pos_9) << " "
<< endl;
}
void AI::attack_task() //仅仅考虑单个单位致命,不考虑多个单位联合致命
{
auto my_allay_list = getUnitsByCamp(my_camp);
sort(my_allay_list.begin(), my_allay_list.end(), [&](Unit a, Unit b) {
int vala, valb;
if (a.type == "Swordsman")
{
vala = 1;
}
else if (a.type == "Inferno")
{
vala = 2;
}
else if (a.type == "Archer")
{
vala = 3;
}
else
{
vala = 4;
}
if (b.type == "Swordsman")
{
valb = 1;
}
else if (b.type == "Inferno")
{
valb = 2;
}
else if (b.type == "Archer")
{
valb = 3;
}
else
{
valb = 4;
}
if (vala != valb)
{
return vala < valb;
}
else
{
return cube_distance(miracle_pos, a.pos) < cube_distance(miracle_pos, b.pos);
}
}); //按照剑士,地狱火,弓箭手,牧师排序,相同类型距离基地近的排在前面
auto enemy_allay_list = getUnitsByCamp(enemy_camp);
int shield_flag = 0;
Unit shield_unit;
//检测是否能杀死圣盾怪
//1检测是否圣盾存在
for (auto enemy : enemy_allay_list)
{
if (enemy.holy_shield == true)
{
shield_flag = 1;
shield_unit = enemy;
break;
}
}
//2检测是否能杀死圣盾
vector<Unit> atk_sield;
int shield_damage = 0;
for (auto allay : my_allay_list)
{
if (allay.can_atk == true && canAttack(allay, shield_unit))
{
shield_damage += allay.atk;
atk_sield.push_back(allay);
}
}
if (shield_damage >= shield_unit.hp)
{
for (auto allay : atk_sield)
{
if (allay.can_atk == true && canAttack(allay, shield_unit) && shield_unit.hp > 0)
{
attack(allay.id, shield_unit.id);
cerr << "6 unit id " << allay.id << " attack unit id" << shield_unit.id << endl;
}
}
}
//=========
for (auto allay : my_allay_list)
{
//第五优先级:对方在基地三格以内,我不是牧师
if (allay.can_atk == true && allay.type != pristest_str)
{
int target_id = -1;
for (auto enemy : enemy_allay_list)
{
if (canAttack(allay, enemy) && (cube_distance(miracle_pos, enemy.pos) <= 3) && enemy.hp > 0)
{
target_id = enemy.id;
}
}
if (target_id != -1)
{
Unit target = getUnitById(target_id);
//检测目标hp大于0
if (target.hp > 0)
{
attack(allay.id, target_id);
cerr << "5 unit id " << allay.id << " attack unit id" << target_id << endl;
}
}
}
}
for (auto allay : my_allay_list)
{
//第四优先级,对方是牧师,我可以杀死对方
if (allay.can_atk)
{
int target_id = -1;
for (auto enemy : enemy_allay_list)
{
if (enemy.type == pristest_str && canAttack(allay, enemy) && allay.atk >= enemy.hp && enemy.hp > 0)
{
target_id = enemy.id;
}
}
if (target_id != -1)
{
Unit target = getUnitById(target_id);
if (target.hp > 0)
{
attack(allay.id, target_id);
cerr << "4 unit id " << allay.id << " attack unit id" << target_id << endl;
}
}
}
}
for (auto allay : my_allay_list)
{
//第三优先级,可以杀死对方,对方不能杀死我
if (allay.can_atk)
{
int target_id = -1;
for (auto enemy : enemy_allay_list)
{
//能杀死
if (canAttack(allay, enemy) && allay.atk >= enemy.hp)
{
//对方打不死我或者对方打不到我
if (canAttack(enemy, allay) == false || canAttack(enemy, allay) && enemy.atk < allay.hp && enemy.hp > 0)
{
target_id = enemy.id;
}
}
}
if (target_id != -1)
{
Unit target = getUnitById(target_id);
if (target.hp > 0)
{
attack(allay.id, target_id);
cerr << "3 unit id " << allay.id << " attack unit id" << target_id << endl;
}
}
}
}
for (auto allay : my_allay_list)
{
//第二优先级 可以攻击基地
if (allay.can_atk)
{
if (canAttackMiracle(allay))
{
attack(allay.id, enemy_camp);
enemy_miracle_hp -= allay.atk;
cerr << "2 attack miracle left hp:" << enemy_miracle_hp << endl;
}
}
}
for (auto allay : my_allay_list)
{
//第一优先级 可以攻击敌方单位但是自身不会致命
if (allay.can_atk)
{
int target_id = -1;
for (auto enemy : enemy_allay_list)
{
if (canAttack(allay, enemy))
{
//对方打不死我或者对方打不到我
if (canAttack(enemy, allay) == false || canAttack(enemy, allay) && enemy.atk < allay.hp && enemy.hp > 0)
{
target_id = enemy.id;
}
}
}
if (target_id != -1)
{
Unit target = getUnitById(target_id);
if (target.hp > 0)
{
attack(allay.id, target_id);
cerr << "1 unit id " << allay.id << " attack unit id" << target_id << endl;
}
}
}
}
for (auto allay : my_allay_list)
{
//第零优先级 可以换掉对面且对面在我方半区
if (allay.can_atk && allay.type != pristest_str)
{
int target_id = -1;
for (auto enemy : enemy_allay_list)
{
//对方在我方半区,不管我方单位死活,我的单位可以杀死对方,
if (near_my_miracle(enemy) && canAttack(allay, enemy) && enemy.hp > 0 && allay.atk > enemy.hp)
{
target_id = enemy.id;
}
}
if (target_id != -1)
{
Unit target = getUnitById(target_id);
if (target.hp > 0)
{
attack(allay.id, target_id);
cerr << "0 unit id " << allay.id << " attack unit id" << target_id << endl;
}
}
}
}
//攻击结束
}
void AI::move_task()
{
auto my_allay_list = getUnitsByCamp(my_camp);
sort(my_allay_list.begin(), my_allay_list.end(), [&](Unit a, Unit b) {
int vala, valb;
if (a.type == "Swordsman")
{
vala = 1;
}
else if (a.type == "Inferno")
{
vala = 2;
}
else if (a.type == "Archer")
{
vala = 3;
}
else
{
vala = 4;
}
if (b.type == "Swordsman")
{
valb = 1;
}
else if (b.type == "Inferno")
{
valb = 2;
}
else if (b.type == "Archer")
{
valb = 3;
}
else
{
valb = 4;
}
if (vala != valb)
{
return vala < valb;
}
else
{
return cube_distance(miracle_pos, a.pos) < cube_distance(miracle_pos, b.pos);
}
});
//按照剑士,地狱火,弓箭手,牧师排序,相同类型距离基地近的排在前面
auto enemy_allay_list = getUnitsByCamp(enemy_camp);
if (attack_mode == ATTACK)
{
for (auto allay : my_allay_list)
{
//准备每个距离可以到达的点和所有可达的点
auto reach_pos_with_dis = reachable(allay, map);
vector<Pos> reach_pos_list;
for (const auto &reach_pos : reach_pos_with_dis)
{
for (auto pos : reach_pos)
reach_pos_list.push_back(pos);
}
if (allay.can_move && allay.type != pristest_str)
{
//第一优先级 占领驻扎点
for (auto pos : reach_pos_list)
{
int barrack_status = checkBarrack(pos);
if (barrack_status == -1 || barrack_status == enemy_camp)
{
if (getUnitByPos(pos, false).id == -1)
{ //该驻扎点可以占领
cerr << "move id " << allay.id << " to " << get<0>(pos) << " " << get<1>(pos) << " " << get<2>(pos) << " "
<< endl;
move(allay.id, pos);
}
}
}
}
if (allay.can_move)
{
//攻击状态第二优先级
if (allay.type == sworderman_str || allay.type == inferno_str)
{
//剑士 或者地狱火 50%朝向能打到敌方最多的点移动 50%朝向最靠近地方的非致命位置最靠近对方基地的位置移动
int hit = probobility(50); //检测是否被概率命中,概率为百分比
if (hit == 1)
{
vector<pos_with_value> max_enemy_attack_pos;
for (auto pos : reach_pos_list)
{
int enemy_conut = 0; //如果走到该pos能打到敌方多少单位
for (auto enemy : enemy_allay_list)
{
if (enemy.flying == true && allay.atk_flying == true)
{
int dis = cube_distance(pos, enemy.pos);
if (dis >= allay.atk_range[0] && dis <= allay.atk_range[1])
{
enemy_conut++;
}
}
else
{
int dis = cube_distance(pos, enemy.pos);
if (dis >= allay.atk_range[0] && dis <= allay.atk_range[1])
{
enemy_conut++;
}
}
}
pos_with_value temp;
temp.pos = pos;
temp.value1 = enemy_conut;
temp.value2 = 0;
max_enemy_attack_pos.push_back(temp);
}
sort(max_enemy_attack_pos.begin(), max_enemy_attack_pos.end(), [&](pos_with_value a, pos_with_value b) {
return a.value1 > b.value1;
});
if (!max_enemy_attack_pos.empty() && max_enemy_attack_pos.front().value1 > 0)
{
cerr << "move id " << allay.id << " to " << get<0>(max_enemy_attack_pos.front().pos) << " " << get<1>(max_enemy_attack_pos.front().pos) << " " << get<2>(max_enemy_attack_pos.front().pos) << " "
<< endl;
move(allay.id, max_enemy_attack_pos.front().pos);
}
}
if (allay.can_move)
{ //上面没有移动所以还能移动
//朝向最靠近地方的非致命位置最靠近对方基地的位置移动
vector<pos_with_value> nearest_enemy_mircle_pos;
for (auto pos : reach_pos_list)
{
int max_damage = 0;
for (auto enemy : enemy_allay_list)
{
int dis = cube_distance(pos, enemy.pos);
if (dis >= enemy.atk_range[0] && dis <= enemy.atk_range[1])
{
if (enemy.atk > max_damage)
{
max_damage = enemy.atk;
}
}
}
if (max_damage < allay.hp)
{
pos_with_value temp;
temp.pos = pos;
temp.value1 = cube_distance(enemy_pos, pos);
temp.value2 = 0;
nearest_enemy_mircle_pos.push_back(temp);
}
}
sort(nearest_enemy_mircle_pos.begin(), nearest_enemy_mircle_pos.end(), [&](pos_with_value a, pos_with_value b) {
return a.value1 < b.value1;
});
if (!nearest_enemy_mircle_pos.empty())
{
cerr << "move id " << allay.id << " to " << get<0>(nearest_enemy_mircle_pos.front().pos) << " " << get<1>(nearest_enemy_mircle_pos.front().pos) << " " << get<2>(nearest_enemy_mircle_pos.front().pos) << " "
<< endl;
move(allay.id, nearest_enemy_mircle_pos.front().pos);
}
}
//=====攻击模式 剑士 地狱火 完===================================
}
else if (allay.type == archer_str)
{
//弓箭手 朝向距离对方基地最近的非致命位置移动
if (allay.can_move)
{ //上面没有移动所以还能移动
//朝向最靠近地方的非致命位置最靠近对方基地的位置移动
vector<pos_with_value> nearest_enemy_mircle_pos;
for (auto pos : reach_pos_list)
{
int max_damage = 0;
for (auto enemy : enemy_allay_list)
{
int dis = cube_distance(pos, enemy.pos);
if (dis >= enemy.atk_range[0] && dis <= enemy.atk_range[1])
{
if (enemy.atk > max_damage)
{
max_damage = enemy.atk;
}
}
}
if (max_damage < allay.hp)
{
//弓箭手特殊距离计算 使得目标点能打到基地
pos_with_value temp;
temp.pos = pos;
int enemy_pos_dis = cube_distance(enemy_pos, pos);
if (enemy_pos_dis <= 3)
{
enemy_pos_dis = 3 - enemy_pos_dis;
}
else if (enemy_pos_dis >= 4)
{
enemy_pos_dis = enemy_pos_dis - 4;
}
temp.value1 = enemy_pos_dis;
temp.value2 = 0;
nearest_enemy_mircle_pos.push_back(temp);
}
}
sort(nearest_enemy_mircle_pos.begin(), nearest_enemy_mircle_pos.end(), [&](pos_with_value a, pos_with_value b) {
return a.value1 < b.value1;
});
if (!nearest_enemy_mircle_pos.empty())
{
cerr << "move id " << allay.id << " to " << get<0>(nearest_enemy_mircle_pos.front().pos) << " " << get<1>(nearest_enemy_mircle_pos.front().pos) << " " << get<2>(nearest_enemy_mircle_pos.front().pos) << " "
<< endl;
move(allay.id, nearest_enemy_mircle_pos.front().pos);
}
}
}
else if (allay.type == pristest_str)
{
if (allay.can_move)
{
//朝向能覆盖最多己方单位的最靠近己方神迹的点行动
vector<pos_with_value> most_cover_my_unit_pos;
for (auto pos : reach_pos_list)
{
auto unit_list = units_in_range(pos, 3, map, my_camp);
int cover_num = 0;
for (auto un : unit_list)
{
if (un.type == sworderman_str)
{
cover_num++;
}
}
pos_with_value temp;
temp.pos = pos;
temp.value1 = cover_num;
temp.value2 = cube_distance(pos, miracle_pos);
most_cover_my_unit_pos.push_back(temp);
}
sort(most_cover_my_unit_pos.begin(), most_cover_my_unit_pos.end(), [&](pos_with_value a, pos_with_value b) {
if (a.value1 != b.value1)
{
return a.value1 > b.value1;
}
else
{
return a.value2 < b.value2;
}
});
if (!most_cover_my_unit_pos.empty())
{
cerr << "move id " << allay.id << " to " << get<0>(most_cover_my_unit_pos.front().pos) << " " << get<1>(most_cover_my_unit_pos.front().pos) << " " << get<2>(most_cover_my_unit_pos.front().pos) << " "
<< endl;
move(allay.id, most_cover_my_unit_pos.front().pos);
}
}
}
//攻击模式 牧师移动 完
}
}
//攻击模式完
}
else if (attack_mode == DEFENSE)
{
for (auto allay : my_allay_list)
{
//准备每个距离可以到达的点和所有可达的点
auto reach_pos_with_dis = reachable(allay, map);
vector<Pos> reach_pos_list;
for (const auto &reach_pos : reach_pos_with_dis)
{
for (auto pos : reach_pos)
reach_pos_list.push_back(pos);
}
if (allay.can_move && allay.type != pristest_str)
{
//第一优先级 占领驻扎点
for (auto pos : reach_pos_list)
{
int barrack_status = checkBarrack(pos);
if (barrack_status == -1 || barrack_status == enemy_camp)
{
if (getUnitByPos(pos, false).id == -1)
{ //该驻扎点可以占领
cerr << "move id " << allay.id << " to " << get<0>(pos) << " " << get<1>(pos) << " " << get<2>(pos) << " "
<< endl;
move(allay.id, pos);
}
}
}
}
if (allay.can_move)
{
//攻击状态第二优先级
if (allay.type == sworderman_str || allay.type == inferno_str)
{
//剑士 或者地狱火 朝向不超过中线,能打到敌方单位最多的点移动 否则朝向最靠近位置7 8的位置移动
vector<pos_with_value> max_enemy_attack_pos;
for (auto pos : reach_pos_list)
{
int enemy_conut = 0; //如果走到该pos能打到敌方多少单位
for (auto enemy : enemy_allay_list)
{
if (enemy.flying == true && allay.atk_flying == true)
{
int dis = cube_distance(pos, enemy.pos);
if (dis >= allay.atk_range[0] && dis <= allay.atk_range[1])
{
enemy_conut++;
}
}
else
{
int dis = cube_distance(pos, enemy.pos);
if (dis >= allay.atk_range[0] && dis <= allay.atk_range[1])
{
enemy_conut++;
}
}
}
pos_with_value temp;
temp.pos = pos;
temp.value1 = enemy_conut;
temp.value2 = 0;
if (cube_distance(pos, miracle_pos) <= 6)
{
max_enemy_attack_pos.push_back(temp);
}
}
sort(max_enemy_attack_pos.begin(), max_enemy_attack_pos.end(), [&](pos_with_value a, pos_with_value b) {
return a.value1 > b.value1;
});
if (!max_enemy_attack_pos.empty() && max_enemy_attack_pos.front().value1 > 0)
{
cerr << "move id " << allay.id << " to " << get<0>(max_enemy_attack_pos.front().pos) << " " << get<1>(max_enemy_attack_pos.front().pos) << " " << get<2>(max_enemy_attack_pos.front().pos) << " "
<< endl;
move(allay.id, max_enemy_attack_pos.front().pos);
}
if (allay.can_move)
{
//否则朝向最靠近位置7 8的位置移动
vector<pos_with_value> max_near78_attack_pos;
for (auto pos : reach_pos_list)
{
int dis7 = cube_distance(pos_2, pos);
int dis8 = cube_distance(pos_5, pos);
int min_dis = std::min(dis7, dis8);
pos_with_value temp;
temp.pos = pos;
temp.value1 = min_dis;
temp.value2 = 0;
max_enemy_attack_pos.push_back(temp);
}
sort(max_enemy_attack_pos.begin(), max_enemy_attack_pos.end(), [&](pos_with_value a, pos_with_value b) {
return a.value1 < b.value1;
});
if (!max_enemy_attack_pos.empty())
{
cerr << "move id " << allay.id << " to " << get<0>(max_enemy_attack_pos.front().pos) << " " << get<1>(max_enemy_attack_pos.front().pos) << " " << get<2>(max_enemy_attack_pos.front().pos) << " "
<< endl;
move(allay.id, max_enemy_attack_pos.front().pos);
}
}
}
else if (allay.type == archer_str)
{
//弓箭手 朝向距离最靠近位置5的非致命位置移动
if (allay.can_move)
{
//朝向距离最靠近位置5的非致命位置移动
vector<pos_with_value> nearest_pos5_mircle_pos;
for (auto pos : reach_pos_list)
{
int max_damage = 0;
for (auto enemy : enemy_allay_list)
{
int dis = cube_distance(pos, enemy.pos);
if (dis >= enemy.atk_range[0] && dis <= enemy.atk_range[1])
{
if (enemy.atk > max_damage)
{
max_damage = enemy.atk;
}
}
}
if (max_damage < allay.hp)
{
pos_with_value temp;
temp.pos = pos;
int pos5_pos_dis = cube_distance(pos_5, pos);
temp.value1 = pos5_pos_dis;
temp.value2 = 0;
nearest_pos5_mircle_pos.push_back(temp);
}