This repository has been archived by the owner on Jan 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AI.cpp
4043 lines (3799 loc) · 117 KB
/
AI.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 <array>
#include <cmath>
#include <queue>
#include <stack>
#include <chrono>
#include <cfloat>
#include <string>
#include <thread>
#include <vector>
#include <cstdarg>
#include <exception>
#include <algorithm>
#include <functional>
#include "AI.h"
#include "API.h"
#include "constants.h"
#define USE_NEW_ASTAR 0
const double PI = acos(-1);
//*****************************************************
// #Basic geometry
//*****************************************************
THUAI6::TrickerType trickerT=THUAI6::TrickerType::NullTrickerType;
class Cell; // 格子 0~49
class Grid; // 像素 0~49999
#if USE_NEW_ASTAR
class Geop; // 实坐标 R
#endif
class Cell
{
public:
int x, y;
Cell();
Cell(int x_, int y_);
Grid ToGrid();
#if USE_NEW_ASTAR
Geop ToGeop();
#endif
};
class Grid
{
public:
int x, y;
Grid();
Grid(int x_, int y_);
Cell ToCell();
#if USE_NEW_ASTAR
Geop ToGeop();
#endif
};
#if USE_NEW_ASTAR
class Geop
{
public:
double x, y;
Geop();
Geop(double x_, double y_);
Cell ToCell();
Grid ToGrid();
};
#endif
Cell::Cell() : x(0), y(0) {}
Cell::Cell(int x_, int y_) : x(x_), y(y_) {}
Grid Cell::ToGrid() { return Grid(x * 1000 + 500, y * 1000 + 500); }
Grid::Grid() : x(0), y(0) {}
Grid::Grid(int x_, int y_) : x(x_), y(y_) {}
Cell Grid::ToCell() { return Cell(x / 1000, y / 1000); }
#if USE_NEW_ASTAR
Geop::Geop() : x(0), y(0) {}
Geop::Geop(double x_, double y_) : x(x_), y(y_) {}
Geop Cell::ToGeop() { return Geop(x * 1000 + 500, y * 1000 + 500); }
Geop Grid::ToGeop() { return Geop(x, y); }
Cell Geop::ToCell() { return Cell((int)x / 1000, (int)y / 1000); }
Grid Geop::ToGrid() { return Grid((int)x, (int)y); }
double Distance(Geop A, Geop B)
{
return sqrt((A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y));
}
// with direction S->T (can be considered as a vector), right side of the segment represents inside
class Geos
{
public:
Geop S, T;
Geos(const Geop& PS, const Geop& PT) : S(PS), T(PT) {}
double GetTheta(Geop P);
};
Geop Project(Geos S, Geop P)
{
double lambda = ((P.x - S.S.x) * (S.T.x - S.S.x) + (P.y - S.S.y) * (S.T.y - S.S.y)) / pow(Distance(S.S, S.T), 2);
// std::cerr << lambda << std::endl;
if (lambda < 0 || lambda > 1)
return S.S; // simple
else
return Geop(S.S.x + lambda * (S.T.x - S.S.x), S.S.y + lambda * (S.T.y - S.S.y));
}
double Geos::GetTheta(Geop P)
{
double CrossDot = (S.x - P.x) * (T.y - P.y) - (S.y - P.y) * (T.x - P.x);
double InnerDot = (S.x - P.x) * (T.x - P.x) + (S.y - P.y) * (T.y - P.y);
double theta = acos(InnerDot / Distance(S, P) / Distance(T, P));
// if (fabs(CrossDot) < 1e-4)
// {
// std::cerr << "[Common Line Warning!]" << std::endl;
// return 0;
// }
return CrossDot > 0 ? theta : -theta;
}
bool Intersect(Geos A, Geos B)
{
// std::cerr << "CheckIntersect" << std::endl;
// std::cerr << '[' << A.S.x / 1000 << ' ' << A.S.y / 1000 << ']' << std::endl;
// std::cerr << '[' << A.T.x / 1000 << ' ' << A.T.y / 1000 << ']' << std::endl;
// std::cerr << '[' << B.S.x / 1000 << ' ' << B.S.y / 1000 << ']' << std::endl;
// std::cerr << '[' << B.T.x / 1000 << ' ' << B.T.y / 1000 << ']' << std::endl;
double CDAS = (B.S.x - A.S.x) * (B.T.y - A.S.y) - (B.S.y - A.S.y) * (B.T.x - A.S.x);
double CDAT = (B.S.x - A.T.x) * (B.T.y - A.T.y) - (B.S.y - A.T.y) * (B.T.x - A.T.x);
double CDBS = (A.S.x - B.S.x) * (A.T.y - B.S.y) - (A.S.y - B.S.y) * (A.T.x - B.S.x);
double CDBT = (A.S.x - B.T.x) * (A.T.y - B.T.y) - (A.S.y - B.T.y) * (A.T.x - B.T.x);
double C = (A.T.x - A.S.x) * (B.T.y - B.S.y) - (A.T.y - A.S.y) * (B.T.x - B.S.x);
// std::cerr << "C = " << C << std::endl;
if (CDAS * CDAT <= 0 && CDBS * CDBT <= 0 && C <= 0)
return true;
return false;
}
#endif
//*****************************************************
// #Information
//*****************************************************
class noway_exception : public std::exception {};
struct MapUpdateInfo
{
THUAI6::PlaceType type;
int x, y, val;
};
typedef std::shared_ptr<const THUAI6::Student> NeedHelpInfo;
//typedef THUAI6::Tricker TrickerInfo_t;
class TrickerInfo_t
{
public:
int64_t playerID;
int32_t x;
int32_t y;
THUAI6::PlayerState playerState;
THUAI6::TrickerType trickerType;
TrickerInfo_t() {}
TrickerInfo_t(const THUAI6::Tricker& t) : playerID(t.playerID), x(t.x), y(t.y), playerState(t.playerState), trickerType(t.trickerType) {}
};
class Doors : public Cell
{
public:
Doors(int x_ = 0, int y_ = 0, bool ds_ = true, THUAI6::PlaceType dt_ = THUAI6::PlaceType::Door3)
: Cell(x_, y_), DoorStatus(ds_), DoorType(dt_) {};
Doors(Cell p_, bool ds_ = true, THUAI6::PlaceType dt_ = THUAI6::PlaceType::Door3)
: Cell(p_), DoorStatus(ds_), DoorType(dt_) {};
bool DoorStatus;
THUAI6::PlaceType DoorType;
};
#if !USE_NEW_ASTAR
class Node : public Cell
{
public:
Node(int x_ = 0, int y_ = 0, int px_ = -1, int py_ = -1,
float fc_ = FLT_MAX, float gc_ = FLT_MAX, float hc_ = FLT_MAX)
: Cell(x_, y_), parentX(px_), parentY(py_), fCost(fc_), gCost(gc_), hCost(hc_) {};
Node(Cell p_, int px_ = -1, int py_ = -1,
float fc_ = FLT_MAX, float gc_ = FLT_MAX, float hc_ = FLT_MAX)
: Cell(p_), parentX(px_), parentY(py_), fCost(fc_), gCost(gc_), hCost(hc_) {};
int parentX;
int parentY;
float fCost;
float gCost;
float hCost;
};
#endif
//*****************************************************
// #Definition & Interface
//*****************************************************
template <typename IFooAPI>
class Friends;
template <typename IFooAPI>
class Geographer;
template <typename IFooAPI>
class Pigeon;
template <typename IFooAPI>
class Predictor;
template <typename IFooAPI>
class CommandPost;
template <typename IFooAPI>
class CommandPost
{
// 这是你和伙伴们所在的指挥所
protected:
Cell TEMP;
int LastAutoUpdateFrame;
IFooAPI& API;
void InitMap(IFooAPI& api);
const int UpdateInterval = 1;
public:
// 这里有你们共享的信息
std::vector < std::vector <THUAI6::PlaceType> > Map;
unsigned char Access[50][50];
// unsigned char Enemy[50][50];
std::vector<Cell> Classroom;
std::vector<Cell> Gate;
std::vector<Cell> HiddenGate;
std::vector<Cell> Chest;
std::vector<Cell> Grass;
std::vector<Doors> Door;
int InfoMem[50][50];
int LastUpdateFrame[50][50];
bool IsStuck;
double LastStuckAngle;
Cell LastStuckDestinationGrid;
Geographer<IFooAPI> Alice;
Predictor<IFooAPI> Bob;
Pigeon<IFooAPI> Gugu;
// 指挥所应当能够直接解决一些基本的问题而不劳烦几位专职人员,比如当前是否在箱子旁边,以及最近的作业位置等等
std::vector<THUAI6::PropType> Inventory;
static std::vector<unsigned char> PickPropPriority;
static std::vector<unsigned char> UsePropPriority;
CommandPost(IFooAPI& api);
void Update(MapUpdateInfo upinfo, int t_); // 更新地图信息,比如门和隐藏校门,需要约定info的格式
void Update(TrickerInfo_t tri, int t_); // 更新Tricker信息
std::vector<THUAI6::PropType> GetInventory() { return Inventory; } // 查看背包
void OrganizeInventory(std::vector<unsigned char> Priority); // 整理背包
// bool MoveToAccurate(Grid Dest, bool WithWindows = true);
bool MoveTo(Cell Dest, bool WithWindows); // 往目的地动一动
bool MoveToNearestClassroom(bool WithWindows); // 往最近的作业的方向动一动
bool MoveToNearestGate(bool WithWindows); // 往最近的关闭的校门旁边动一动
bool MoveToNearestOpenGate(bool WithWindows); // 往最近的开启的校门旁边动一动
bool MoveToNearestChest(bool WithWindows); // 往最近的箱子的方向动一动
bool NearCell(Cell P, int level = 1); // level=0判断当前是否在该格子上,1判断是否在格子上或周围4格,2判断是否在格子上或周围8格
bool NearClassroom(bool checkProgress); // 已经在作业旁边了吗?
bool NearGate(); // 已经在关闭的校门旁边了吗?
bool NearOpenGate(); // 已经在开启的校门旁边了吗?
bool NearChest(); // 已经在箱子旁边了吗?
bool NearWindow(); // 已经在窗户旁边了吗?
bool InGrass(); // 已经在草丛里了吗?
void DirectLearning(bool WithWindows); // 前往最近的作业并学习
void DirectOpeningChest(bool WithWindows); // 前往最近的箱子并开箱
void DirectOpeningGate(bool WithWindows, bool CanDirectGraduate); // 前往最近的关闭的校门并开门
void DirectGraduate(bool WithWindows); // 前往最近的开启的校门并毕业
void DirectGrass(bool WithWindows); // 前往最近的草丛并躲避
void DirectHide(Cell TrickerLocation, int TrickerViewRange, bool WithWindows); // 前往最适合的草丛并躲避
void DirectProp(std::vector<unsigned char> Priority, int DistanceInfluence, int PropInfluence, bool WithWindows); // 前往已知价值最高的道具并捡道具
void DirectUseProp(std::vector<unsigned char> Priority);
int CountFinishedClassroom() const;
int CountNonemptyChest() const;
int CountHiddenGate() const;
int CountClosedGate() const;
int CountOpenGate() const;
int GetChestProgress(int cellx, int celly);
int GetGateProgress(int cellx, int celly);
int GetClassroomProgress(int cellx, int celly);
int GetDoorProgress(int cellx, int celly);
int GetChestProgress(Cell cell) const;
int GetGateProgress(Cell cell) const;
int GetClassroomProgress(Cell cell) const;
int GetDoorProgress(Cell cell) const;
bool IsAccessible(int x, int y, bool WithWindows);
};
template <typename IFooAPI>
class Friends
{
// Student&Tricker的伙伴们
protected:
IFooAPI& API; // 方便起见,每个人都有对api的直接引用,其实可以直接用Center.API
CommandPost<IFooAPI>& Center; // 你和伙伴们都可以访问自己所在的指挥所,并和其他人交流
Friends(IFooAPI& api, CommandPost<IFooAPI>& Center_) : API(api), Center(Center_) {}
};
template <typename IFooAPI>
class Geographer : public Friends<IFooAPI>
{
// 这是一位Geographer,负责告诉要怎么走
protected:
// bool IsAccessible(THUAI6::PlaceType pt);
#if USE_NEW_ASTAR
std::vector<Geos> StableMap;
std::vector<Geop> StableCheckPoint;
std::vector<Geos> VariableMap;
std::vector<Geop> VariableCheckPoint;
bool DirectReachable(Geop A, Geop B, bool IsDest = false);
Geop Escape(Geop P);
void InitStableMap();
const double CheckPointRadius;
double CheckPointCompensate;
const double SegmentRadius;
double SegmentCompensate;
bool InsideObstacle(Geop P);
void ResetVariableMap();
void AddPlayer();
void AddWindow();
void AddLockedDoor();
#endif
public:
Geographer(IFooAPI& api, CommandPost<IFooAPI>& Center_);
#if !USE_NEW_ASTAR
bool IsDestination(int x, int y, Node dest);
double CalculateH(int x, int y, Node dest);
std::vector<Node> MakePath(const std::array<std::array<Node, 50>, 50>& map, Node dest);
std::vector<Node> AStar(Node src, Node dest, bool WithWindows);
int EstimateTime(Cell Dest); // 去目的地的预估时间
#else
std::vector<Geop> FindPath(Geop From_, Geop Dest_);
#endif
void BackwardExpand(Cell Source, int H[50][50]);
bool IsViewable(Cell Src, Cell Dest, int ViewRange); // 判断两个位置是否可视
std::vector<Cell> GetViewableCells(Cell src);
Cell GetNearestGate();
Cell GetNearestClassroom(); // 仅在没写完的作业中找
Cell GetNearestOpenGate();
};
template<typename IFooAPI>
std::vector<Cell> Geographer<IFooAPI>::GetViewableCells(Cell src)
{
int vrange = this->API.GetSelfInfo()->viewRange / 2;
bool chk[50][50];
memset(chk, 0, sizeof(chk));
std::queue<Cell> Q;
Q.push(src);
chk[src.x][src.y] = true;
std::vector<Cell> ans;
while (!Q.empty())
{
Cell now = Q.front();
Q.pop();
ans.push_back(now);
for (int ix = -1; ix <= 1; ix++)
for (int iy = -1; iy <= 1; iy++)
if (std::abs(ix) + std::abs(iy) == 1 &&
now.x + ix >= 0 && now.x + ix < 50 && now.y + iy >= 0 && now.y + iy < 50 &&
!chk[now.x + ix][now.y + iy])
{
Cell nxt(now.x + ix, now.y + iy);
chk[now.x + ix][now.y + iy] = true;
if (IsViewable(src, nxt, vrange)) Q.push(nxt);
}
}
return ans;
}
class Encoder
{
private:
static const int MaxLength = 255;
char msg[MaxLength];
int Celler;
public:
Encoder();
void SetHeader(char header);
template <typename T>
void PushInfo(T info);
std::string ToString();
};
class Decoder
{
private:
std::string msg;
int Celler;
public:
Decoder(std::string code);
template <typename T>
T ReadInfo();
};
template <typename IFooAPI>
class Pigeon : public Friends<IFooAPI>
{
// 这是一只Pigeon,负责传递信息
private:
void sendInfo(int64_t dest, std::string info);
std::string buf;
public:
Pigeon(IFooAPI& api, CommandPost<IFooAPI>& Center_) : Friends<IFooAPI>(api, Center_) {}
void sendMapUpdate(int64_t dest, MapUpdateInfo muinfo);
void sendMapUpdate(int64_t dest, THUAI6::PlaceType type, int x, int y, int val);
void sendTrickerInfo(int64_t dest, TrickerInfo_t tricker);
void sendNeedHelp(int64_t dest, NeedHelpInfo self);
void sendRescue(int64_t dest, bool rescue);
int receiveMessage(); // ���ؽ��յ�����Ϣ����
std::pair<int, MapUpdateInfo> receiveMapUpdate();
TrickerInfo_t receiveTrickerInfo();
std::pair<int, int> receiveNeedHelp();
bool receiveRescue();
};
template <typename IFooAPI>
class Predictor : public Friends<IFooAPI>
{
// 这是一位Predictor,负责告诉其他人可能在哪里
protected:
std::vector<double> DangerAlertLog;
std::vector<double> TrickDesireLog;
std::vector<double> ClassVolumeLog;
double MagicMap[5][50][50];
const int TotalValue;
int PlayerStatus[5], CantMoveStart[5], CantMoveDuration[5];
// 0表示不追踪该玩家,适用于本人或退学/毕业的情况;1表示可以移动并正常追踪;2表示最后一次看到时是不能运动的状态,但不是沉迷;3表示最后一次看到时是沉迷状态;2~3会用到上述两个CantMove数组。
void NormalizeMagicMap(); // 正则化
void DeduceMagicMap(); // 进行一次推算
public:
Predictor(IFooAPI& api, CommandPost<IFooAPI>& Center_);
void FindEnemy();
void SaveDangerAlertLog(int maxNum);
void SaveTrickDesireLog(int maxNum);
void SaveClassVolumeLog(int maxNum);
void AutoUpdate();
void Update(const TrickerInfo_t& tri);
std::pair<Cell, double> Recommend(int PlayerID);
// Always return position with highest probility, even though the player was addicted (In this circumstance, chasing it results in a repetition of finding it addicted.) To chase it or not should be decided in stragety.
// TODO: CommandPost should save info of addiction & quit etc., should it be the responsibility of Predictor? Probably yes. Ask Predictor for info of players' status. This only works for Tricker.
void _display(int PlayerID);
Cell SmartRecommend();
};
class CommandPostStudent : public CommandPost<IStudentAPI>
{
public:
CommandPostStudent(IStudentAPI& api) : CommandPost(api) {}
void AutoUpdate();
void TeacherPunish();
double TeacherPunishCD();
void StraightAStudentWriteAnswers();
double StraightAStudentWriteAnswersCD();
void AtheleteCanBeginToCharge();
double AtheleteCanBeginToChargeCD();
void SunshineRouse();
void SunshineEncourage();
void SunshineInspire();
double SunshineRouseCD();
double SunshineEncourageCD();
double SunshineInspireCD();
};
class CommandPostTricker : public CommandPost<ITrickerAPI>
{
public:
CommandPostTricker(ITrickerAPI& api) : CommandPost(api) {}
void AutoUpdate();
void KleeDefaultAttack(int stux, int stuy); // 刺客普通攻击,传入学生坐标(stux,stuy)
bool KleeDefaultAttackOver(int rank); // 判断能否稳定命中,传入目前能观察到的学生列表的第几个,从0开始计数
void KleeJumpyBomb();
double KleeJumpyBombCD();
void KleeSparksNSplash(int PlayerID);
double KleeSparksNSplashCD();
};
//*****************************************************
// #Implementation
//*****************************************************
//--------------------
// #Predictor
//--------------------
template<typename IFooAPI>
Predictor<IFooAPI>::Predictor(IFooAPI& api, CommandPost<IFooAPI>& Center_) : Friends<IFooAPI>(api, Center_), TotalValue(2500)
{
for (int i = 0; i < 5; i++) PlayerStatus[i] = 1;
PlayerStatus[api.GetSelfInfo()->playerID] = 0;
}
template <typename IFooAPI>
void Predictor<IFooAPI>::NormalizeMagicMap()
{
for (int id = 0; id < 5; id++)
if (PlayerStatus[id] != 0)
{
double sum = 0;
for (int i = 0; i < 50; i++)
for (int j = 0; j < 50; j++)
sum += MagicMap[id][i][j];
if (sum == 0)
{
for (int i = 0; i < 50; i++)
for (int j = 0; j < 50; j++)
if (this->Center.Access[i][j]) MagicMap[id][i][j] = 1, sum += 1;
std::cerr << "sum = " << sum;
}
for (int i = 0; i < 50; i++)
for (int j = 0; j < 50; j++)
MagicMap[id][i][j] *= TotalValue / sum;
}
}
template <typename IFooAPI>
void Predictor<IFooAPI>::DeduceMagicMap()
{
auto selfinfo = this->API.GetSelfInfo();
bool deal[5];
double NextStatus[50][50];
for (int i = 0; i < 5; i++) deal[i] = (PlayerStatus[i] != 0);
auto vision = this->Center.Alice.GetViewableCells(Grid(selfinfo->x, selfinfo->y).ToCell());
for (int id = 0; id < 5; id++)
if (deal[id])
{
memset(NextStatus, 0, sizeof(double) * 50 * 50);
for (int i = 0; i < 50; i++)
for (int j = 0; j < 50; j++)
{
if (this->Center.Access[i][j])
{
double ratio1 = (PlayerStatus[id] == 3 ? 0.01 : 0.15);
int cnt = 0;
for (int ix = -1; ix <= 1; ix++)
for (int jx = -1; jx <= 1; jx++)
if (((ix == 0) ^ (jx == 0)) &&
i + ix >= 0 && i + ix < 50 && j + jx >= 0 && j + jx < 50 &&
this->Center.Access[i + ix][j + jx]) cnt++;
NextStatus[i][j] += MagicMap[id][i][j] * (1 - ratio1);
if (cnt == 0) continue;
for (int ix = -1; ix <= 1; ix++)
for (int jx = -1; jx <= 1; jx++)
if (((ix == 0) ^ (jx == 0)) &&
i + ix >= 0 && i + ix < 50 && j + jx >= 0 && j + jx < 50 &&
this->Center.Access[i + ix][j + jx])
{
NextStatus[i + ix][j + jx] += MagicMap[id][i][j] * ratio1 / cnt;
}
}
}
memcpy(MagicMap[id], NextStatus, sizeof(double) * 50 * 50);
for (auto c : vision) MagicMap[id][c.x][c.y] = 0;
}
NormalizeMagicMap();
}
template<typename IFooAPI>
void Predictor<IFooAPI>::AutoUpdate()
{
DeduceMagicMap();
auto stuinfo = this->API.GetStudents();
auto triinfo = this->API.GetTrickers();
for (auto s : stuinfo)
if (PlayerStatus[s->playerID])
{
Cell pos = Grid(s->x, s->y).ToCell();
memset(MagicMap[s->playerID], 0, sizeof(double) * 50 * 50);
MagicMap[s->playerID][pos.x][pos.y] = TotalValue;
if (s->playerState == THUAI6::PlayerState::Addicted) PlayerStatus[s->playerID] = 3;
else PlayerStatus[s->playerID] = 1;
}
for (auto s : triinfo)
if (PlayerStatus[s->playerID])
{
Cell pos = Grid(s->x, s->y).ToCell();
memset(MagicMap[s->playerID], 0, sizeof(double) * 50 * 50);
MagicMap[s->playerID][pos.x][pos.y] = TotalValue;
if (s->playerState == THUAI6::PlayerState::Addicted) PlayerStatus[s->playerID] = 3;
else PlayerStatus[s->playerID] = 1;
}
}
/* AutoUpdate with bgm
void Predictor<IStudentAPI>::AutoUpdate()
{
DeduceMagicMap();
auto stuinfo = this->API.GetStudents();
auto triinfo = this->API.GetTrickers();
for (auto s : stuinfo)
if (PlayerStatus[s->playerID])
{
Cell pos = Grid(s->x, s->y).ToCell();
memset(MagicMap[s->playerID], 0, sizeof(double) * 50 * 50);
MagicMap[s->playerID][pos.x][pos.y] = TotalValue;
if (s->playerState == THUAI6::PlayerState::Addicted) PlayerStatus[s->playerID] = 3;
else PlayerStatus[s->playerID] = 1;
}
for (auto s : triinfo)
if (PlayerStatus[s->playerID])
{
Cell pos = Grid(s->x, s->y).ToCell();
memset(MagicMap[s->playerID], 0, sizeof(double) * 50 * 50);
MagicMap[s->playerID][pos.x][pos.y] = TotalValue;
if (s->playerState == THUAI6::PlayerState::Addicted) PlayerStatus[s->playerID] = 3;
else PlayerStatus[s->playerID] = 1;
}
}
void Predictor<ITrickerAPI>::AutoUpdate()
{
DeduceMagicMap();
auto stuinfo = this->API.GetStudents();
auto triinfo = this->API.GetTrickers();
for (auto s : stuinfo)
if (PlayerStatus[s->playerID])
{
Cell pos = Grid(s->x, s->y).ToCell();
memset(MagicMap[s->playerID], 0, sizeof(double) * 50 * 50);
MagicMap[s->playerID][pos.x][pos.y] = TotalValue;
if (s->playerState == THUAI6::PlayerState::Addicted) PlayerStatus[s->playerID] = 3;
else PlayerStatus[s->playerID] = 1;
}
for (auto s : triinfo)
if (PlayerStatus[s->playerID])
{
Cell pos = Grid(s->x, s->y).ToCell();
memset(MagicMap[s->playerID], 0, sizeof(double) * 50 * 50);
MagicMap[s->playerID][pos.x][pos.y] = TotalValue;
if (s->playerState == THUAI6::PlayerState::Addicted) PlayerStatus[s->playerID] = 3;
else PlayerStatus[s->playerID] = 1;
}
if (this->API.GetSelfInfo()->trickDesire)
{
double cent = this->API.GetSelfInfo()->radius / this->API.GetSelfInfo()->trickDesire;
std::cerr << "cent = " << cent << std::endl;
int sx = this->API.GetSelfInfo()->x;
int sy = this->API.GetSelfInfo()->y;
for (int i = 0; i < 50; i++)
for (int j = 0; j < 50; j++)
{
if (this->Center.IsAccessible(i, j, true))
{
double dx = i * 1000 + 500 - sx;
double dy = j * 1000 + 500 - sy;
double dist = sqrt(dx * dx + dy * dy) / cent;
// std::cerr << "dist = " << dist << std::endl;;
double distrib = exp(-(dist - 1) * (dist - 1) / 0.18) * .1 + 1;
for (int k = 0; k < 4; k++)
if (PlayerStatus[k]) MagicMap[k][i][j] *= distrib;
}
}
for (int k = 0; k < 4; k++) if (PlayerStatus[k]) NormalizeMagicMap();
}
}
*/
template<typename IFooAPI>
void Predictor<IFooAPI>::Update(const TrickerInfo_t& tri)
{
if (PlayerStatus[tri.playerID])
{
Cell pos = Grid(tri.x, tri.y).ToCell();
memset(MagicMap[tri.playerID], 0, sizeof(double) * 50 * 50);
MagicMap[tri.playerID][pos.x][pos.y] = TotalValue;
if (tri.playerState == THUAI6::PlayerState::Addicted) PlayerStatus[tri.playerID] = 3;
else PlayerStatus[tri.playerID] = 1;
}
}
template<typename IFooAPI>
void Predictor<IFooAPI>::_display(int PlayerID)
{
for (int i = 0; i < 50; i++) {
for (int j = 0; j < 50; j++)
// std::cerr << MagicMap[PlayerID][i][j];
std::cerr.put((MagicMap[PlayerID][i][j] > 1) ? '*' : '.');
std::cerr << std::endl;
}
}
template<typename IFooAPI>
std::pair<Cell, double> Predictor<IFooAPI>::Recommend(int PlayerID)
{
int dist[50][50];
Cell pos = Grid(this->API.GetSelfInfo()->x, this->API.GetSelfInfo()->y).ToCell();
this->Center.Alice.BackwardExpand(pos, dist);
Cell Maxc;
double prob = 0;
// for (int i = 0; i < 50; i++) {
// for (int j = 0; j < 50; j++)
// std::cerr << (dist[i][j] == 10000000 ? -1 : dist[i][j]) << ' ';
// std::cerr << std::endl;
// }
for (int i = 0; i < 50; i++)
for (int j = 0; j < 50; j++)
if (MagicMap[PlayerID][i][j] > prob)
{
Maxc = Cell(i, j);
// std::cerr << i << ' ' << j << std::endl;
// assert(dist[i][j] > 999 && MagicMap[PlayerID][i][j] == 0 || dist[i][j] <= 999);
prob = MagicMap[PlayerID][i][j] / pow(dist[i][j] + 1, 0) * (1 + .5 * sin((i + j) / 10. + this->API.GetFrameCount() / 200. * 3.14));;
}
return std::make_pair(Maxc, prob);
}
template <typename IFooAPI>
void Predictor<IFooAPI>::SaveDangerAlertLog(int maxNum)
{
if (DangerAlertLog.size() < maxNum)
{
DangerAlertLog.push_back(this->API.GetSelfInfo()->dangerAlert);
}
else
{
DangerAlertLog.erase(DangerAlertLog.begin());
DangerAlertLog.push_back(this->API.GetSelfInfo()->dangerAlert);
}
}
template <typename IFooAPI>
void Predictor<IFooAPI>::SaveTrickDesireLog(int maxNum)
{
if (TrickDesireLog.size() < maxNum)
{
TrickDesireLog.push_back(this->API.GetSelfInfo()->trickDesire);
}
else
{
TrickDesireLog.erase(TrickDesireLog.begin());
TrickDesireLog.push_back(this->API.GetSelfInfo()->trickDesire);
}
}
template <typename IFooAPI>
void Predictor<IFooAPI>::SaveClassVolumeLog(int maxNum)
{
if (ClassVolumeLog.size() < maxNum)
{
ClassVolumeLog.push_back(this->API.GetSelfInfo()->classVolume);
}
else
{
ClassVolumeLog.erase(ClassVolumeLog.begin());
ClassVolumeLog.push_back(this->API.GetSelfInfo()->classVolume);
}
}
template <typename IFooAPI>
Cell Predictor<IFooAPI>::SmartRecommend()
{
int x = this->API.GetSelfInfo()->x / 1000;
int y = this->API.GetSelfInfo()->y / 1000;
int dist[50][50];
Cell pos;
double value = -1;
this->Center.Alice.BackwardExpand(Cell(x, y), dist);
int i_ = 0, j_ = 0, k_ = 0;
for (int i = 0; i < 4; i++)
{
if (this->PlayerStatus[i])
{
for (int j = 0; j < 50; j++)
for (int k = 0; k < 50; k++)
{
double eval = 0;
if (PlayerStatus[i] == 3) eval = 1;
else eval = 10000;
eval = eval * MagicMap[i][j][k] / log(dist[j][k] + 2) * (1 + .5 * sin((i + j) / 10. + this->API.GetFrameCount() / 200. * 3.14));
if (eval > value)
{
i_ = i;
j_ = j;
k_ = k;
pos = Cell(j, k);
value = eval;
}
}
}
}
std::cerr << "recommend " << j_ << ' ' << k_ << ' ' << MagicMap[i_][j_][k_] << ' ' << dist[j_][k_] << std::endl;
return pos;
}
//--------------------
// #Pigeon
//--------------------
/* ��ϢЭ��
��Ϣͷ��info[0]��
MapUpdate ��ͼ����
TrickerInfo ��������Ϣ
NeedHelp ����֧Ԯ
info[1-2]������
WantProp �����ȡ���ߣ�������Ҫ��������Ȼ̫Զ����ȥ�����㣩
*/
#define NoMessage 0x00
#define MapUpdate 0x01
#define TrickerInfo 0x02
#define NeedHelp 0x03
#define WantProp 0x04
#define Rescue 0x05
Encoder::Encoder() : Celler(0)
{
memset(msg, 0, sizeof(msg));
}
#define NEW_ENDEC 0
template <typename T>
void Encoder::PushInfo(T info)
{
size_t t = sizeof(T);
void* ptr = &info;
#if !NEW_ENDEC
for (size_t i = 0; i < t; i++)
{
msg[Celler] = ((*((unsigned char*)ptr + i)) >> 4) + 'a';
Celler++;
msg[Celler] = ((*((unsigned char*)ptr + i)) & 0x0f) + 'a';
Celler++;
}
#else
memcpy(msg + Celler, ptr, t);
Celler += t;
#endif
}
void Encoder::SetHeader(char header)
{
PushInfo(header);
}
std::string Encoder::ToString()
{
return std::string(msg, msg + MaxLength);
}
Decoder::Decoder(std::string code) : msg(code), Celler(0) {}
template <typename T>
T Decoder::ReadInfo()
{
std::cerr << msg << std::endl;
T obj;
void* ptr = &obj;
size_t t = sizeof(T);
std::cerr << "sizeofT: " << t << std::endl;
#if !NEW_ENDEC
for (size_t i = 0; i < t; i++)
{
*((unsigned char*)ptr + i) = ((((unsigned char)*(msg.c_str() + Celler) - 'a') << 4)) | (((unsigned char)*(msg.c_str() + Celler + 1)) - 'a');
Celler += 2;
}
#else
memcpy(ptr, msg.c_str() + Celler, t);
Celler += t;
#endif
return obj;
}
template <typename IFooAPI>
void Pigeon<IFooAPI>::sendInfo(int64_t dest, std::string info)
{
if (dest != this->API.GetSelfInfo()->guid)
this->API.SendBinaryMessage(dest, info);
}
template <typename IFooAPI>
void Pigeon<IFooAPI>::sendMapUpdate(int64_t dest, MapUpdateInfo muinfo)
{
Encoder enc;
enc.SetHeader(MapUpdate);
enc.PushInfo(this->API.GetFrameCount());
enc.PushInfo(muinfo);
sendInfo(dest, enc.ToString());
}
template <typename IFooAPI>
void Pigeon<IFooAPI>::sendMapUpdate(int64_t dest, THUAI6::PlaceType type, int x, int y, int val)
{
MapUpdateInfo muinfo = { type, x, y, val };
sendMapUpdate(dest, muinfo);
}
template <typename IFooAPI>
std::pair<int, MapUpdateInfo> Pigeon<IFooAPI>::receiveMapUpdate()
{
Decoder dec(buf);
char header = dec.ReadInfo<char>();
assert(header == MapUpdate);
int frm = dec.ReadInfo<int>();
MapUpdateInfo muinfo = dec.ReadInfo<MapUpdateInfo>();
return std::make_pair<int, MapUpdateInfo>(static_cast<int&&>(frm), static_cast<MapUpdateInfo&&>(muinfo));
}
template <typename IFooAPI>
int Pigeon<IFooAPI>::receiveMessage()
{
if (this->API.HaveMessage())
{
buf = this->API.GetMessage().second; // 是谁发来的好像不太重要,只取信息内容
Decoder dec(buf);
return dec.ReadInfo<char>();
}
else
return NoMessage;
}
template <typename IFooAPI>
void Pigeon<IFooAPI>::sendTrickerInfo(int64_t dest, TrickerInfo_t tricker)
{
Encoder enc;
enc.SetHeader(TrickerInfo);
// enc.PushInfo(this->API.GetFrameCount());
enc.PushInfo<TrickerInfo_t>(tricker);
sendInfo(dest, enc.ToString());
}
template <typename IFooAPI>
TrickerInfo_t Pigeon<IFooAPI>::receiveTrickerInfo()
{
std::cerr << "(receiveTrickerInfo)\n";
Decoder dec(buf);
std::cerr << "(receiveTrickerInfo)\n";
char header = dec.ReadInfo<char>();
std::cerr << "(receiveTrickerInfo)\n";
assert(header == TrickerInfo);
std::cerr << "(receiveTrickerInfo)\n";
return dec.ReadInfo<TrickerInfo_t>();
}
// 捣蛋鬼信息的编码和解码函数
std::string sendOneselfMessage(std::shared_ptr<const THUAI6::Student> self)
{
Encoder enc;
enc.SetHeader(NeedHelp);
enc.PushInfo<std::shared_ptr<const THUAI6::Student>>(self);
std::string info = enc.ToString();
return info;
}
std::shared_ptr<const THUAI6::Student> receiveOneselfMessage(std::string info)
{
Decoder dec(info);
char header = dec.ReadInfo<char>();
std::shared_ptr<const THUAI6::Student> p1 = dec.ReadInfo<std::shared_ptr<const THUAI6::Student>>();
return p1;
}
// 自己信息的编码和解码函数
template <typename IFooAPI>
void Pigeon<IFooAPI>::sendNeedHelp(int64_t dest, NeedHelpInfo self)
{
Encoder enc;
enc.SetHeader(NeedHelp);
enc.PushInfo(this->API.GetFrameCount());
int64_t id = self->playerID;
enc.PushInfo(id);
sendInfo(dest, enc.ToString());
}
template <typename IFooAPI>
std::pair<int, int> Pigeon<IFooAPI>::receiveNeedHelp()
{
Decoder dec(buf);
char header = dec.ReadInfo<char>();
assert(header == NeedHelp);
return std::make_pair<int, int>(dec.ReadInfo<int>(), dec.ReadInfo<int>());
}
template <typename IFooAPI>
void Pigeon<IFooAPI>::sendRescue(int64_t dest, bool rescue)
{
Encoder enc;
enc.SetHeader(Rescue);
enc.PushInfo(rescue);
sendInfo(dest, enc.ToString());
}
template <typename IFooAPI>
bool Pigeon<IFooAPI>::receiveRescue()
{
Decoder dec(buf);
char header = dec.ReadInfo<char>();
assert(header == Rescue);
return dec.ReadInfo<bool>();