-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
2938 lines (2847 loc) · 92.5 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
// 2018.04.29 20:22:19 commit 2dac6bb4e96029384c6b608910aaa283de5d8826
//#undef Adler
#define QAP_DEBUG
#ifdef Adler
//#define _ITERATOR_DEBUG_LEVEL 0
#ifndef CLANG_QAPLITE
#include "qaplite\QapLite.h"
#endif
#include "qaplite\TQapGameV2.inl"
#include "qaplite\perf_sys.inl"
#else
#ifdef WIN32
#define NOMINMAX
#include <Windows.h>
class QapClock
{
public:
INT64 freq,beg,tmp;
bool run;
public:
QapClock(){QueryPerformanceFrequency((LARGE_INTEGER*)&freq);run=false;tmp=0;Start();}
void Start(){QueryPerformanceCounter((LARGE_INTEGER*)&beg);run=true;}
void Stop(){QueryPerformanceCounter((LARGE_INTEGER*)&tmp);run=false;tmp-=beg;}
double Time(){if(run)QueryPerformanceCounter((LARGE_INTEGER*)&tmp);return run?double(tmp-beg)/double(freq):double(tmp)/double(freq);}
double MS()
{
double d1000=1000.0;
if(run)QueryPerformanceCounter((LARGE_INTEGER*)&tmp);
if(run)return (double(tmp-beg)*d1000)/double(freq);
if(!run)return (double(tmp)*d1000)/double(freq);
return 0;
}
};
#else
#include <unistd.h>
#include <sys/time.h>
//struct QapClock{double MS(){return 0;}};
class QapClock
{
public:
double beg,tmp;
bool run;
public:
QapClock(){run=false;Start();}
double em_perf_now(){
timeval t;
gettimeofday(&t,NULL);
return (t.tv_sec*1e6+t.tv_usec)*1e-3;
}
void Start(){beg=em_perf_now();run=true;}
void Stop(){tmp=em_perf_now();run=false;tmp-=beg;}
double Time(){if(run)tmp=em_perf_now();return double(run?(tmp-beg):tmp)/1000.0;}
double MS()
{
double d1000=1000.0;
if(run)tmp=em_perf_now();
if(run)return tmp-beg;
if(!run)return tmp;
//timespec ts;clock_gettime(CLOCK_MONOTONIC,&ts);
return 0;
}
};
#endif
#define QAP_PERF(NAME)
#define QAP_PERF_CODE(CODE){CODE;}
#define NO_QAP_PERF_CODE(CODE){CODE;}
#endif
#define _ALLOW_KEYWORD_MACROS
#if(!defined(_DEBUG)&&!defined(Adler))
#ifndef QAP_MSVC
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#pragma GCC diagnostic ignored "-Wsign-compare"
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-variable"
#pragma clang diagnostic ignored "-Wreorder"
#pragma warning(push,1)
#endif
#include <cstring>
#endif
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <vector>
#include <string>
#include <math.h>
#include <algorithm>
#include <stack>
#include <set>
#include <memory>
#include <thread>
#include <mutex>
#include <fstream>
#include <bitset>
using std::vector;
using std::string;
//#undef Adler
static bool unix_SaveFile(const string&FN,const string&mem)
{
using namespace std;
fstream f;
f.open(FN.c_str(),ios::out|ios::binary);
if(!f)return false;
if(!mem.empty())f.write(&mem[0],mem.size());
f.flush();
f.close();
return true;
};
#ifdef Adler
#ifndef QAP_LITE_H
#include <Windows.h>
inline string IToS(const int&val){char c[16];_itoa_s(val,c,10);return string(c);}
inline string FToS(const double&val){char c[64];if(abs(val)>1e9){_snprintf_s(c,32,32,"%e",val);}else{sprintf_s(c,"%f",val);}return string(c);}
inline string FToS2(const double&val){char c[64];if(abs(val)>1e9){_snprintf_s(c,32,32,"%e",val);}else{sprintf_s(c,"%.2f",val);}return string(c);}
static bool IsKeyDown(int vKey){int i=GetAsyncKeyState(vKey);return i<0;}
#define KB_CODE(){auto mwta=game.getWizardMaxTurnAngle();if(IsKeyDown('Q'))move.setTurn(-mwta);if(IsKeyDown('E'))move.setTurn(+mwta);if(IsKeyDown('W'))move.setSpeed(+100);if(IsKeyDown('S'))move.setSpeed(-100);if(IsKeyDown('D'))move.setStrafeSpeed(+100);if(IsKeyDown('A'))move.setStrafeSpeed(-100);}
static bool file_put_contents(const string&FN,const string&mem)
{
using namespace std;
auto*f=fopen(FN.c_str(),"w+b");
if(!f)return false;
if(!mem.empty())fwrite(&mem[0],mem.size(),1,f);
fclose(f);
return true;
};
#endif
#else
void KB_CODE(){}
#define file_put_contents(...)(true)
//static bool file_put_contents(const string&FN,const string&mem){return true;}
string file_get_contents(const string&fn){return "";}
#endif
#ifndef QAP_LITE_H
#define QapDebugMsg(MSG){printf("QapDebugMsg :: %s:%i :: %s\n",__FILE__,__LINE__,string(MSG).c_str());fflush(stdout);}//__debugbreak();
#define QapAssert(COND)if(!(COND)){printf("QapAssert :: %s:%i :: %s\n",__FILE__,__LINE__,#COND);fflush(stdout);exit(0);}//__debugbreak();
#define QapNoWay(){printf("QapNoWay :: %s:%i\n",__FILE__,__LINE__);fflush(stdout);exit(0);}//__debugbreak();
#endif
#define DEF_PRO_SORT_BY_FIELD(sort_by_field,TYPE,FIELD)\
struct t_help_struct_for_sort_vec_of_##TYPE##_by_##FIELD{\
static int __cdecl cmp_func(const void*a,const void*b){return cmp(*(TYPE*)a,*(TYPE*)b);}\
static int cmp(const TYPE&a,const TYPE&b){return a.FIELD-b.FIELD;}\
};\
static void sort_by_field(vector<TYPE>&arr){\
if(arr.empty())return;\
std::qsort(&arr[0],arr.size(),sizeof(TYPE),t_help_struct_for_sort_vec_of_##TYPE##_by_##FIELD::cmp_func);\
}
#define PRO_FUNCGEN_GETP_BY_FIELD(rettype,getp,arr,field_type,field)\
rettype*getp(field_type value)\
{\
rettype*p=nullptr;\
for(int i=0;i<arr.size();i++){\
auto&ex=arr[i];\
if(ex.field!=value)continue;\
QapAssert(!p);\
p=&ex;\
}\
return p;\
}
#define PRO_FUNCGEN_ADD_UNIQUE_OBJ_BY_FIELD_V2(rettype,adduni,arr,field_type,field)\
rettype*adduni(field_type value)\
{\
rettype*p=nullptr;\
for(int i=0;i<arr.size();i++){\
auto&ex=arr[i];\
if(ex.field!=value)continue;\
QapAssert(!p);\
p=&ex;\
}\
if(!p){p=&qap_add_back(arr);p->field=value;}\
return p;\
}
template<class TYPE,class FUNC>
int qap_minval_id_for_vec(vector<TYPE>&arr,FUNC func){
if(arr.empty())return -1;
decltype(func(arr[0],0)) val;int id=-1;
for(int i=0;i<arr.size();i++){
auto&ex=arr[i];
auto tmp=func(ex,i);
if(!i||tmp<val){
val=tmp;id=i;
}
}
return id;
}
template<class TYPE,class FUNC>
int qap_minval_id_for_vec(const vector<TYPE>&arr,FUNC func){
if(arr.empty())return -1;
decltype(func(arr[0],0)) val;int id=-1;
for(int i=0;i<arr.size();i++){
auto&ex=arr[i];
auto tmp=func(ex,i);
if(!i||tmp<val){
val=tmp;id=i;
}
}
return id;
}
template<class TYPE>
static void operator+=(vector<TYPE>&dest,const vector<TYPE>&arr){
for(int i=0;i<arr.size();i++){
dest.push_back(arr[i]);
}
}
template<class TYPE>int qap_includes(const vector<TYPE>&arr,const TYPE&value){for(int i=0;i<arr.size();i++){if(arr[i]==value)return true;}return false;}
#define QAP_MINVAL_ID_OF_VEC(arr,code)qap_minval_id_for_vec(arr,[&](decltype(arr[0])&ex,int i){return code;})
template<class TYPE,class FUNC>void qap_foreach(TYPE&&arr,FUNC func){auto n=arr.size();for(int i=0;i<n;i++)func(arr[i],i);}
template<class TYPE,class FUNC>void qap_foreach(const TYPE&arr,FUNC func){auto n=arr.size();for(int i=0;i<n;i++)func(arr[i],i);}
#define QAP_FOREACH(arr,code)qap_foreach(arr,[&](decltype(arr[0])&ex,int i){code;})
#ifndef QAP_LITE_H
static double sqr(double x){return x*x;}
inline int SToI(const string&S){int i;sscanf(S.c_str(),"%i",&i);return i;};
inline string IToS(const int&v){return std::to_string(v);};
inline string FToS(const double&v){return std::to_string(v);};
template<class TYPE>void qap_sort(vector<TYPE>&arr){std::sort(arr.begin(),arr.end());}
template<class TYPE>
static bool qap_add_unique_val(vector<TYPE>&arr,const TYPE&value){
if(qap_includes(arr,value))return false;
arr.push_back(value);
return true;
}
static vector<string> split(const string&s,const string&needle)
{
vector<string> arr;
if(s.empty())return arr;
size_t p=0;
for(;;){
auto pos=s.find(needle,p);
if(pos==std::string::npos){arr.push_back(s.substr(p));return arr;}
arr.push_back(s.substr(p,pos-p));
p=pos+needle.size();
}
return arr;
}
//-------------------------------------------//
static string join(const vector<string>&arr,const string&glue)
{
string out;
size_t c=0;
size_t dc=glue.size();
for(int i=0;i<arr.size();i++){if(i)c+=dc;c+=arr[i].size();}
out.reserve(c);
for(int i=0;i<arr.size();i++){if(i)out+=glue;out+=arr[i];}
return out;
}
template<class TYPE>
static bool qap_find_val_once(const vector<TYPE>&arr,const TYPE&val){
for(int i=0;i<arr.size();i++)if(val==arr[i])return true;
return false;
}
template<class TYPE>
static bool qap_find_val_once(const std::set<TYPE>&arr,const TYPE&val){
auto it=arr.find(val);
return it!=arr.end();
}
template<typename TYPE,size_t COUNT>inline size_t lenof(TYPE(&)[COUNT]){return COUNT;}
template<class TYPE>static bool qap_check_id(const vector<TYPE>&arr,int id){return id>=0&&id<arr.size();}
template<class TYPE,class FUNC>void clean_if(vector<TYPE>&Arr,FUNC&&Pred){int last=0;for(int i=0;i<Arr.size();i++){auto&ex=Arr[i];if(Pred(ex))continue;if(last!=i){auto&ax=Arr[last];ax=std::move(ex);}last++;}if(last==Arr.size())return;Arr.resize(last);}
template<class TYPE>static TYPE&qap_add_back(vector<TYPE>&arr){arr.resize(arr.size()+1);return arr.back();}
template<typename TYPE>TYPE Sign(TYPE value){return (value>0)?TYPE(+1):TYPE(value<0?-1:0);}
typedef double real;const real Pi=3.14159265;const real Pi2=Pi*2;const real PiD2=Pi/2;const real PiD4=Pi/4;
template<class TYPE>inline TYPE Clamp(const TYPE&v,const TYPE&a,const TYPE&b){return std::max(a,std::min(v, b));}
template<typename TYPE>inline TYPE Lerp(const TYPE&A,const TYPE&B,const real&v){return A+(B-A)*v;}
class vec2d{
public:
real x;real y;
vec2d():x(0.0),y(0.0) {}
vec2d(const real&x,const real&y):x(x),y(y) {}
vec2d(const vec2d&v):x(v.x),y(v.y) {}
public:
vec2d&operator=(const vec2d&v){x=v.x;y=v.y;return *this;}
vec2d operator+()const{return *this;}
vec2d operator-()const{return vec2d(-x,-y);}
vec2d&operator+=(const vec2d&v){x+=v.x;y +=v.y;return *this;}
vec2d&operator-=(const vec2d&v){x-=v.x; y-=v.y;return *this;}
vec2d&operator*=(const real&f){x*=f;y*=f;return *this;}
vec2d&operator/=(const real&f){x/=f;y/=f;return *this;}
public:
vec2d Rot(const vec2d&OX)const{real M=OX.Mag();return vec2d(((x*+OX.x)+(y*OX.y))/M,((x*-OX.y)+(y*OX.x))/M);}
vec2d UnRot(const vec2d&OX)const{real M=OX.Mag();if(M==0.0f){return vec2d(0,0);};return vec2d(((x*OX.x)+(y*-OX.y))/M,((x*OX.y)+(y*+OX.x))/M);}
vec2d Ort()const{return vec2d(-y,x);}
vec2d Norm()const{if((x==0)&&(y==0)){return vec2d(0,0);}return vec2d(x/this->Mag(),y/this->Mag());}
vec2d SetMag(const real&val)const{return this->Norm().Mul(vec2d(val,val));}
vec2d Mul(const vec2d&v)const{return vec2d(x*v.x,y*v.y);}
vec2d Div(const vec2d&v)const{return vec2d(v.x!=0?x/v.x:x,v.y!=0?y/v.y:y);}
real GetAng()const{return atan2(y,x);}
real Mag()const{return sqrt(x*x+y*y);}
real SqrMag()const{return x*x+y*y;}
public:
real dist_to(const vec2d&p)const{return vec2d(p.x-x,p.y-y).Mag();}
real sqr_dist_to(const vec2d&p)const{return vec2d(p.x-x,p.y-y).SqrMag();}
bool dist_to_point_less_that_r(const vec2d&p,real r)const{return vec2d(p.x-x,p.y-y).SqrMag()<r*r;}
public:
static vec2d min(const vec2d&a,const vec2d&b){return vec2d(std::min(a.x,b.x),std::min(a.y,b.y));}
static vec2d max(const vec2d&a,const vec2d&b){return vec2d(std::max(a.x,b.x),std::max(a.y,b.y));}
static void comin(vec2d&a,const vec2d&b){a=min(a,b);}
static void comax(vec2d&a,const vec2d&b){a=max(a,b);}
static vec2d sign(const vec2d&p){return vec2d(Sign(p.x),Sign(p.y));}
public:
inline static real dot(const vec2d&a,const vec2d&b){return a.x*b.x+a.y*b.y;}
inline static real cross(const vec2d&a,const vec2d&b){return a.x*b.y-a.y*b.x;}
vec2d fabs()const{return vec2d(::fabs(x),::fabs(y));}
real max()const{return std::max(x,y);}
real min()const{return std::min(x,y);}
//string dump()const{return "{\"X\":"+FToS(x)+",\"Y\":"+FToS(y)+"}";}
};
vec2d operator+(const vec2d&u,const vec2d&v){return vec2d(u.x+v.x,u.y+v.y);}
vec2d operator-(const vec2d&u,const vec2d&v){return vec2d(u.x-v.x,u.y-v.y);}
vec2d operator*(const vec2d&u,const real&v){return vec2d(u.x*v,u.y*v);}
vec2d operator*(const real&u,const vec2d&v){return vec2d(u*v.x,u*v.y);}
bool operator==(const vec2d&u,const vec2d&v){return (u.x==v.x)&&(u.y==v.y);}
bool operator!=(const vec2d&u,const vec2d&v){return !(u==v);}
inline vec2d Vec2dEx(const real&ang,const real&mag){return vec2d(cos(ang)*mag,sin(ang)*mag);}
class vec2i{
public:
public:
typedef vec2i SelfClass;
public:
int x;
int y;
public:
public:
vec2i():x(0),y(0) {}
vec2i(int x,int y):x(x),y(y) {};
friend vec2i operator*(int u,const vec2i&v)
{
return vec2i(u*v.x,u*v.y);
}
friend vec2i operator*(const vec2i&v,int u)
{
return vec2i(u*v.x,u*v.y);
}
friend vec2i operator/(const vec2i&v,int d)
{
return vec2i(v.x/d,v.y/d);
}
friend vec2i operator+(const vec2i&u,const vec2i&v)
{
return vec2i(u.x+v.x,u.y+v.y);
}
friend vec2i operator-(const vec2i&u,const vec2i&v)
{
return vec2i(u.x-v.x,u.y-v.y);
}
void operator+=(const vec2i&v)
{
x+=v.x;
y+=v.y;
}
void operator-=(const vec2i&v)
{
x-=v.x;
y-=v.y;
}
int SqrMag()
{
return x*x+y*y;
}
float Mag()
{
return sqrt(float(x*x+y*y));
}
operator vec2d()const
{
return vec2d(x,y);
}
vec2i operator+()const
{
return vec2i(+x,+y);
}
vec2i operator-()const
{
return vec2i(-x,-y);
}
friend bool operator==(const vec2i&u,const vec2i&v)
{
return (u.x==v.x)&&(u.y==v.y);
}
friend bool operator!=(const vec2i&u,const vec2i&v)
{
return (u.x!=v.x)||(u.y!=v.y);
}
static vec2i fromVec2d(const vec2d&v){return vec2i(int(v.x),int(v.y));}
vec2i Ort()const{return vec2i(-y,x);}
vec2i Mul(const vec2i&v)const{return vec2i(x*v.x,y*v.y);}
static vec2i sign(const vec2i&p){return vec2i(Sign(p.x),Sign(p.y));}
static vec2i sign(const vec2d&p){return vec2i(Sign(p.x),Sign(p.y));}
};
static vec2i tovec2i(const vec2d&p){return vec2i(p.x,p.y);}
#endif
template<class t_elem>
struct t_map{
vector<t_elem> mem;
int w;
int h;
public:
void fill(int value){for(int i=0;i<w*h;i++)mem[i]=value;}
public:
static void set_to_def(t_elem&v){v.set_to_def();}
t_elem&get(const vec2d&p){return get(p.x,p.y);}
const t_elem&get(const vec2d&p)const{return get(p.x,p.y);}
t_elem&get(const vec2i&p){return get(p.x,p.y);}
const t_elem&get(const vec2i&p)const{return get(p.x,p.y);}
t_elem&get_unsafe(int x,int y){return mem[x+y*w];}
const t_elem&get_unsafe(int x,int y)const{return mem[x+y*w];}
t_elem&get_unsafe(const vec2i&p){return mem[p.x+p.y*w];}
const t_elem&get_unsafe(const vec2i&p)const{return mem[p.x+p.y*w];}
t_elem&fail_id(){return sys_fail_id_const(*this);};
template<class T>static t_elem&sys_fail_id_const(T&v){const T&ref=(const T&)v;return (t_elem&)ref.fail_id();};
const t_elem&fail_id()const{static t_elem buff;set_to_def(buff);return buff;};
t_elem&get(int x,int y){if(x<0||y<0)return fail_id();if(x<w&&y<h)return get_unsafe(x,y);return fail_id();}
bool check(const vec2i&p)const{if(p.x<0||p.y<0||p.x+1>w||p.y+1>h)return false;return true;}
const t_elem&get(int x,int y)const{if(x<0||y<0)return fail_id();if(x<w&&y<h)return get_unsafe(x,y);return fail_id();}
int conv_vec_to_id(const vec2i&v)const{return v.x+v.y*w;}
void init(vec2i wh){
w=wh.x;h=wh.y;
mem.resize(w*h);
}
vec2d get_wh(){return vec2d(w,h);}
};
#include "constants.h"
#undef GetObject
#include "rapidjson/rapidjson.h"
#include "rapidjson/document.h" // rapidjson's DOM-style API
#include "rapidjson/stringbuffer.h"
#include "rapidjson/writer.h"
using namespace rapidjson;
using namespace std;
struct t_conf{
bool need_init=true;
#define LIST(F)\
F(FOOD_MASS )\
F(GAME_HEIGHT )\
F(GAME_TICKS )\
F(GAME_WIDTH )\
F(INERTION_FACTOR )\
F(MAX_FRAGS_CNT )\
F(SPEED_FACTOR )\
F(TICKS_TIL_FUSION)\
F(VIRUS_RADIUS )\
F(VIRUS_SPLIT_MASS)\
F(VISCOSITY )\
//===
#define F(VAR)double VAR;
LIST(F)
#undef F
void load(Document&d)
{
auto&c=Constants::instance();
#define F(VAR)VAR=d[#VAR].GetDouble();c.VAR=VAR;
LIST(F);
#undef F
need_init=false;
}
#undef LIST
};
struct t_cell{
int call_id=-1;
int beg_t=-1024*4;
int t=0;
real beg_food=0;
real food_value=0;
//
int beg_food_n=0;
int food_n=0;
std::array<vec2d,7+10> food_pos;
std::bitset<8*4> beg_food_flag;
std::bitset<8*4> food_flag;
void set_to_def(){call_id=-1;beg_t=0;t=0;beg_food=0;food_value=0;food_n=0;beg_food_n=0;}
real&get_food_value(int sim_id,int tick,double food_spawn_speed){
if(call_id!=sim_id){call_id=sim_id;t=beg_t;food_value=beg_food;food_n=beg_food_n;food_flag=beg_food_flag;}
auto dt=tick-t;
food_value+=dt*food_spawn_speed;
t=tick;
return food_value;
}
real get_beg_food_value(int tick,real food_spawn_speed)const{
return beg_food_n+beg_food+(tick-beg_t)*food_spawn_speed;
}
};
static const size_t t_cell_size=sizeof(t_cell);
struct t_foodmap{
t_map<t_cell> map;
int sim_id=0;
real food_spawn_speed=0;
real cell_area=0;
real inv_cell_area=0;
real inv_cell_size=0;
vec2d offset;
vec2d half_cell_size;
bool need_use_inside_sim=false;
bool need_use_foodvalue=false;
void init(t_conf&conf,real cell_size=30){
auto wh=vec2i(conf.GAME_WIDTH,conf.GAME_HEIGHT);
auto map_wh=vec2i(wh.x/cell_size,wh.y/cell_size)+vec2i(0,0);
auto map_area=wh.x*wh.y;
map.init(map_wh);
//cell_size=wh.x/map.w;
inv_cell_size=1.0/cell_size;
cell_area=sqr(cell_size);
inv_cell_area=1.0/cell_area;
food_spawn_speed=4.0*ADD_FOOD_SETS*cell_area/(ADD_FOOD_DELAY*map_area);
half_cell_size=vec2d(1,1)*cell_size*0.5;
}
template<class Mechanic>
real get_score(Mechanic&mech,int pId){
typedef typename Mechanic::MechPlayer Player;
auto&tick=mech.tick;
real out=0;
auto me_frag_cnt=mech.get_fragments_cnt(pId);
mech.foreach_players(pId,[this,tick,&out,me_frag_cnt](Player*ptr){
Player&ex=*ptr;
auto pos=ex.get_pos();
auto p=Player::get_vis_center(pos,ex.v);
auto vr=Player::get_vision_radius(ex.radius,me_frag_cnt);
out+=draw(p,vr,tick,true);
});
return out;
}
t_cell&get_cell(const vec2d&pos){
auto p=pos*inv_cell_size;
return map.get(p);
}
static vec2i tovec2i(const vec2d&p){return vec2i(p.x,p.y);}
real draw(const vec2d&pos,real radius,int tick,bool inside_sim)
{
real out=0;
auto half=vec2d(0.5,0.5);
auto pc=pos*inv_cell_size;
auto p=tovec2i(pc);
auto r=radius*inv_cell_size;
auto diag=sqrt(2);
for(int y=p.y-r+1;y<=p.y+r;y++)for(int x=p.x-r+1;x<=p.x+r;x++){
auto coords=vec2i(x,y);
if(!map.check(coords))continue;
auto cc=vec2d(coords)+half;
//auto vertex=cc+vec2d::sign(cc-pc)/**diag*/*0.5;
auto v=half;int ok=0;
#define F()if((cc+v).dist_to_point_less_that_r(pc,r)){ok++;};v=v.Ort();
F();F();F();F();
#undef F
if(ok!=4)continue;
//dir.SqrMag()
//if(!(vec2d(coords)+half).dist_to_point_less_that_r(pd,R+diag))continue;
auto&c=map.get(coords);
if(!inside_sim)
{
c.beg_t=tick;
c.beg_food=0;
c.beg_food_n=0;
c.beg_food_flag.reset();
}else{
auto&fv=c.get_food_value(sim_id,tick,food_spawn_speed);
out+=fv;
fv=0;
}
}
return out;
}
void draw_food(const vec2d&pos,int tick){
auto&c=map.get(pos*inv_cell_size);
if(bool need_use_food_pos=true){
int&n=c.beg_food_n;
for(int i=0;i<n;i++){
if(c.food_pos[i]==pos){
int gg=1;
return;
}
}
auto id=(n++)%c.food_pos.size();
#ifdef Adler
//QapAssert(n!=1);
//QapAssert(n!=2);
//QapAssert(n!=3);
//QapAssert(n!=4);
QapAssert(n!=5);
QapAssert(n!=6);
QapAssert(n!=7);
QapAssert(n<=c.food_pos.size());
#endif
c.food_pos[id]=pos;
c.beg_food_flag[id]=true;
c.beg_food=0;
n=std::min<int>(c.food_pos.size(),n);
}else{
QapNoWay();//deprecated
c.beg_food=1;
}
}
int sim_eat(const vec2d&pos,real food_eat_radius,int tick){
auto half=vec2d(0.5,0.5);
auto pc=pos*inv_cell_size;
auto p=tovec2i(pc);
auto R=food_eat_radius*inv_cell_size;
auto r=R+1;
auto diag=sqrt(2);
int out=0;
for(int y=p.y-r;y<=p.y+r;y++)for(int x=p.x-r;x<=p.x+r;x++){
auto coords=vec2i(x,y);
if(!map.check(coords))continue;
auto&c=map.get(coords);
if(!c.food_n)continue;
QapAssert(c.food_n<=c.food_pos.size());
auto&arr=c.food_pos;
for(int i=0;i<c.beg_food_n;i++){
if(!c.food_flag[i])continue;
if(!arr[i].dist_to_point_less_that_r(pos,food_eat_radius))continue;
c.food_flag[i]=false;
out++;
c.food_n--;
}
int gg=1;
}
return out;
}
#ifdef Adler
void use_dump(TDataIO&IO,bool need_save){
#define ADD(TYPE,NAME,VALUE)if(need_save)IO.save_as_pod(this->NAME);if(!need_save)IO.load_as_pod(this->NAME);
//ADD(t_map<t_cell>,map,$);
ADD(vector<t_cell>,map.mem,$);
ADD(int,map.w,0);
ADD(int,map.h,0);
ADD(int,sim_id,0);
ADD(real,food_spawn_speed,0);
ADD(real,cell_area,0);
ADD(real,inv_cell_area,0);
ADD(real,inv_cell_size,0);
ADD(vec2d,offset,$);
ADD(vec2d,half_cell_size,$);
#undef ADD
}
#endif
};
string dir="./";//"C:/Users/Adler/Desktop/miniraic/miniaicups/agario/local_runner_bin/win/";
string input_log_fn=dir+"inp_log.txt";
string full_game_fn=dir+"full_game.txt";
template<class JSON>
static void load_vec2d(vec2d&dest,JSON&d,bool speed=false){
dest.x=d[speed?"SX":"X"].GetDouble();
dest.y=d[speed?"SY":"Y"].GetDouble();
}
struct t_player{
int id=-1;
int fid=-1;
vec2d pos;
vec2d v;
real r=0;
real m=0;
int TTF=0;
bool me=false;
//---
int t=-1;
int predicted_spawn_tick=0;
int found_tick=-1;
bool is_fast=false;
//---
template<class TYPE>
void load(TYPE&d,bool mine){
me=mine;
auto Id=split(string(d["Id"].GetString()),".");
id=SToI(Id[0]);
fid=Id.size()<2?0:SToI(Id[1]);
load_vec2d(pos,d);
r=d["R"].GetDouble();
m=d["M"].GetDouble();
if(!mine)return;
load_vec2d(v,d,true);
if(d.HasMember("TTF"))TTF=d["TTF"].GetInt();
}
template<class TYPE>
void load_from(const TYPE&d,bool mine){
me=mine;
id=d.id;
fid=d.fragmentId;
pos=vec2d(d.x,d.y);
r=d.radius;
m=d.mass;
if(!mine)return;
v=d.v;
if(d.fuse_timer>0)TTF=d.fuse_timer;
}
void sync_with(t_player&ref){
QapAssert(fid==ref.fid);
QapAssert(id==ref.id);
if(ref.me){*this=ref;return;}
vec2d prev_pos=pos;
*this=ref;
v=pos-prev_pos;
ref.v=v;
}
};
struct t_food{
vec2d pos;
template<class TYPE>void load(TYPE&d){load_vec2d(pos,d);}
template<class TYPE>void load_from(const TYPE&d,bool mine){pos=vec2d(d.x,d.y);}
};
struct t_eject{
vec2d pos;
int pId=0;
template<class TYPE>void load(TYPE&d){load_vec2d(pos,d);if(d.HasMember("pId"))pId=d["pId"].GetInt();}
template<class TYPE>void load_from(const TYPE&d,bool mine){pos=vec2d(d.x,d.y);pId=d.player;}
};
struct t_virus{
vec2d pos;
real m=0;
template<class TYPE>void load(TYPE&d){load_vec2d(pos,d);m=d["M"].GetDouble();}
template<class TYPE>void load_from(const TYPE&d,bool mine){pos=vec2d(d.x,d.y);m=d.mass;}
};
struct t_world_parsed{
int tick=0;
vector<t_player> parr;
vector<t_food> farr;
vector<t_eject> earr;
vector<t_virus> varr;
void load(Document&d,int tick)
{
this->tick=tick;
auto mine=d["Mine"].GetArray();
if(auto n=mine.Size())
{
for(int i=0;i<n;i++)
{
auto ex=mine[i].GetObject();
qap_add_back(parr).load(ex,true);
}
}
auto objects=d["Objects"].GetArray();
if(auto n=objects.Size())
{
for(int i=0;i<n;i++)
{
auto ex=objects[i].GetObject();
string type=ex["T"].GetString();
if("F"==type)qap_add_back(farr).load(ex);
if("E"==type)qap_add_back(earr).load(ex);
if("V"==type)qap_add_back(varr).load(ex);
if("P"==type)qap_add_back(parr).load(ex,false);
}
}
}
bool can_do_split(int sId){
bool out=false;int n=0;int max_frags=Constants::instance().MAX_FRAGS_CNT;
QAP_FOREACH(parr,if(ex.id==sId)n++);
if(max_frags-n<=0)return false;
QAP_FOREACH(parr,if(ex.id==sId)out=out||ex.m>MIN_SPLIT_MASS);
return out;
}
vector<int> get_enemy_pIds(int our_id){
vector<int> out;
QAP_FOREACH(parr,if(ex.id!=our_id)qap_add_unique_val(out,ex.id));
return out;
}
#ifdef Adler
void use_dump(TDataIO&IO,bool need_save){
#define ADD(TYPE,NAME,VALUE)if(need_save)IO.save_as_pod(this->NAME);if(!need_save)IO.load_as_pod(this->NAME);
ADD(int,tick,0);
ADD(vector<t_player>,parr,$);
ADD(vector<t_food>,farr,$);
ADD(vector<t_eject>,earr,$);
ADD(vector<t_virus>,varr,$);
#undef ADD
}
#endif
};
//#include "strategymodal.h"
#include "mechanic.h"
#include <iostream>
void load(t_world_parsed&out,const PlayerArray&FA,const CircleArray&CA)
{
for(int i=0;i<FA.size();i++)
{
auto ex=FA[i];
qap_add_back(out.parr).load_from(*ex,true);
}
for(int i=0;i<CA.size();i++)
{
auto ex=CA[i];
ex->save_to(out);
}
}
struct t_avg_real{
real total=0;
int n=0;
real get(real value_when_n_is_zero=0){return n?total/n:value_when_n_is_zero;}
void add(real v){total+=v;n++;}
};
real get_survive_probability(real er,real espd,real r,real spd,real ang,vec2d offset){return 0;}
static t_avg_real get_safety(Mechanic&mech,int pId){
t_avg_real out;
auto&arr=mech.player_array;
for(int i=0;i<arr.size();i++){
auto&a=arr[i];
if(a->id==pId)continue;
for(int i=0;i<arr.size();i++){
auto&b=arr[i];
if(b->id!=pId)continue;
auto d=a->get_pos().dist_to(b->get_pos());
QapAssert(a->mass);
QapAssert(b->mass);
out.add(d*b->mass/a->mass);
}
}
return out;
}
struct t_frags_info{
int n=0;
real total_mass=0;
real total_TFF=0;
vec2d avg_pos;
};
static t_frags_info get_frags_info(Mechanic&mech,int pId){
auto max_TTF=mech.get_max_TTF();
t_frags_info out;
auto&arr=mech.player_array;
for(int i=0;i<arr.size();i++){
auto&ex=arr[i];
if(ex->id!=pId){
int gg=1;
continue;
}
out.avg_pos+=vec2d(ex->x,ex->y);
out.total_mass+=ex->mass;
out.total_TFF+=max_TTF-ex->fuse_timer;
out.n++;
}
if(out.n)out.avg_pos*=1.0/out.n;
return out;
}
struct t_score{
real rank=-1;
#define LIST(ADD)\
ADD(real,total_score,0)\
ADD(real,total_mass,0)\
ADD(real,total_TFF,0)\
ADD(real,total_safety,0)\
ADD(real,total_food,0)\
ADD(real,total_dist,0)\
ADD(real,score,0)\
ADD(real,mass,0)\
ADD(real,TFF,0)\
ADD(real,safety,0)\
ADD(real,food,0)\
ADD(real,dist,0)\
//===
//ADD(real,split_n,0)\
//ADD(real,eject_n,0)\
//ADD(real,total_avg_dist_to_enemys,0)\
//ADD(real,total_avg_sqrdist_to_enemys,0)\
//===
#define F(TYPE,NAME,VALUE)TYPE NAME=VALUE;
LIST(F)
#undef F
//
int id=-1;
real probability=1.0;
t_frags_info bef;
t_frags_info aft;
real sources=0.0;
void set_rank(real rank,int n){
//real pos=(n-rank)/real(n);
real N=1.25*n;
probability=(N-rank)/N;
}
real total_mass_and_total_score()const{
return total_mass+total_score;
}
real pos_diff()const{
return bef.avg_pos.dist_to(aft.avg_pos);
}
bool operator<(const t_score&ref)const{
#define MAJOR(SIGN,field)if(field!=ref.field)return SIGN field<SIGN ref.field;
//MAJOR(-,aft.total_mass);
//MAJOR(-,total_mass);
MAJOR(-,total_mass_and_total_score());
MAJOR(-,total_TFF);
MAJOR(-,total_safety);
MAJOR(-,total_score);
MAJOR(-,total_food);
MAJOR(-,total_dist);
#undef MAJOR
return id>ref.id;
}
bool almost_equal_to(const t_score&ref){
t_score a=*this;
t_score b=ref;
std::swap(a.id,b.id);
return ((*this)<ref)!=(a<b);
}
void add(const t_score&ref)
{
sources+=ref.probability;
#define F(TYPE,NAME,VALUE)this->NAME+=ref.NAME*ref.probability;
LIST(F);
#undef F
}
t_score get_average()const
{
t_score out=*this;
#define F(TYPE,NAME,VALUE)out.NAME=real(this->NAME)/sources;
LIST(F);
#undef F
return out;
}
template<class FUNC>
void foreach(FUNC func){
#define F(TYPE,NAME,VALUE)func(#NAME,this->NAME);
LIST(F);
#undef F
}
string to_str(bool json=true,const string&glue=",")const{
vector<string> out;
#define F(TYPE,NAME,VALUE)qap_add_back(out)="\""+string(#NAME)+"\":"+std::to_string(this->NAME);
F(int,id,-1);
F(int,rank,-1);
F(real,probability,1.0);
F(real,sources,0.0);
LIST(F);
#undef F
auto s=join(out,glue);
return json?"{"+s+"}":s;
}
//t_score&set(int id){this->id=id;return *this;}
#undef LIST
};
struct t_move{
vec2d pos;
bool split=false;// "Split": true,
bool eject=false;// "Eject": true,
t_move&set(vec2d p,bool s,bool e){split=s;eject=e;pos=p;return *this;}
t_move operator=(const vec2d&ref){pos=ref;return *this;}
t_move(){}
t_move(const vec2d&ref){pos=ref;}
operator Direct&(){return *(Direct*)(void*)this;}
};
struct t_plan_rec{
int tick=-1;
t_move move;
t_plan_rec with_offset(int offset)const{t_plan_rec out=*this;out.tick+=offset;return out;}
};
struct t_plan{
vector<t_plan_rec> arr;
t_plan&add(int tick,const t_move&move){auto&b=qap_add_back(arr);b.tick=tick;b.move=move;return *this;}
struct t_rec{bool ok;t_plan_rec pr;};
t_rec get_rec(int tick)const{
for(int i=0;i<arr.size();i++){auto&ex=arr[i];if(tick<=ex.tick){return {true,ex};}}
return {false};