-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathv194.cpp
3699 lines (3574 loc) · 119 KB
/
v194.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
//#undef Adler
//#define QAP_DEBUG
#ifdef Adler
#ifndef CLANG_QAPLITE
#include "qaplite\QapLite.h"
#endif
#include "qaplite\TQapGameV2.inl"
#include "qaplite\perf_sys.inl"
#else
struct QapClock{double MS(){return 0;}};
#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
#define private public
#define protected public
#include "MyStrategy.h"
#include <vector>
#include <string>
#include <math.h>
#include <algorithm>
#include <stack>
#include <memory>
#include <thread>
#include <mutex>
using std::vector;
using std::string;
//#undef Adler
#ifdef Adler
#ifndef QAP_LITE_H
#include <Windows.h>
#include <fstream>
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(){}
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 QapAssert(COND)if(!(COND))__debugbreak();
#define QapNoWay()__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
template<typename TYPE,size_t COUNT>inline size_t lenof(TYPE(&)[COUNT]){return COUNT;}
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);}
};
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
#define PI 3.14159265358979323846
#define _USE_MATH_DEFINES
using namespace model;
using namespace std;
static const auto VH=VehicleType::HELICOPTER;
static const auto VF=VehicleType::FIGHTER;
static const auto VT=VehicleType::TANK;
static const auto VA=VehicleType::ARRV;
static const auto VI=VehicleType::IFV;
struct t_map{
typedef real t_elem;
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=0;}
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);
}
};
template<class t_elem>
struct t_map_v2{
//typedef real t_elem;
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);
}
};
struct t_app{
Player me;World world;Game game;Move move;
vector<Facility> objs;
vector<Vehicle> vehs;
vector<Vehicle> oldvehs;
vector<char> id2updated;
int empty_group_id=1;
bool no_wait=false;
t_map termap;
t_map wetmap;
t_map vis_termap;
t_map vis_wetmap;
vector<int> id2vehid;
vector<int> id2oldvehid;
vector<int> id2groupid;
PRO_FUNCGEN_GETP_BY_FIELD(Facility,id2obj,objs,int,id);
#define PRO_FUNCGEN_GETP_BY_ID(rettype,getp,arr,t_id,id,id2id)\
rettype*getp(t_id id){auto tmp=id2id[id];if(tmp<0)return nullptr;return &arr[tmp];}
//
PRO_FUNCGEN_GETP_BY_ID(Vehicle,id2veh,vehs,int,id,id2vehid);
PRO_FUNCGEN_GETP_BY_ID(Vehicle,id2oldvehs,oldvehs,int,id,id2oldvehid);
real get_speed_factor(WeatherType t){
if(t==WeatherType::CLEAR)return game.clearWeatherSpeedFactor;
if(t==WeatherType::CLOUD)return game.cloudWeatherSpeedFactor;
if(t==WeatherType::RAIN)return game.rainWeatherSpeedFactor;
return 1.0;
}
real get_speed_factor(TerrainType t){
if(t==TerrainType::PLAIN)return game.plainTerrainSpeedFactor;
if(t==TerrainType::FOREST)return game.forestTerrainSpeedFactor;
if(t==TerrainType::SWAMP)return game.swampTerrainSpeedFactor;
return 1.0;
}
real get_vision_factor(WeatherType t){
if(t==WeatherType::CLEAR)return game.clearWeatherVisionFactor;
if(t==WeatherType::CLOUD)return game.cloudWeatherVisionFactor;
if(t==WeatherType::RAIN)return game.rainWeatherVisionFactor;
return 1.0;
}
real get_vision_factor(TerrainType t){
if(t==TerrainType::PLAIN)return game.plainTerrainVisionFactor;
if(t==TerrainType::FOREST)return game.forestTerrainVisionFactor;
if(t==TerrainType::SWAMP)return game.swampTerrainVisionFactor;
return 1.0;
}
template<class TYPE>
void init_map(t_map&dest,const vector<vector<TYPE>>&arr){
for(int y=0;y<dest.h;y++)for(int x=0;x<dest.w;x++)dest.get(x,y)=get_speed_factor(arr[x][y]);
}
template<class TYPE>
void init_map_vis(t_map&dest,const vector<vector<TYPE>>&arr){
for(int y=0;y<dest.h;y++)for(int x=0;x<dest.w;x++)dest.get(x,y)=get_vision_factor(arr[x][y]);
}
void init_maps(){
if(termap.mem.size())return;
auto wh=vec2i(1,1)*world.terrainByCellXY.size();
wetmap.init(wh);
termap.init(wh);
init_map(wetmap,world.weatherByCellXY);
init_map(termap,world.terrainByCellXY);
vis_wetmap.init(wh);
vis_termap.init(wh);
init_map_vis(vis_wetmap,world.weatherByCellXY);
init_map_vis(vis_termap,world.terrainByCellXY);
inv_cell_size=real(wetmap.w)/world.width;
}
real inv_cell_size;
//struct t_env:t_app{
typedef t_app t_env;
real get_k(const vec2d&pos,bool air){
return (air?wetmap:termap).get(pos*inv_cell_size);
}
real get_vis_k(const vec2d&pos,bool air){
return (air?vis_wetmap:vis_termap).get(pos*inv_cell_size);
}
vec2d get_pos(const Vehicle&v)const{return vec2d(v.x,v.y);}
vec2d get_pos(const Facility&ref)const{
static auto wh=vec2d(game.facilityWidth,game.facilityHeight);
return vec2d(ref.left,ref.top)+wh*0.5;
}
vec2d get_obj_size()const{
static auto wh=vec2d(game.facilityWidth,game.facilityHeight);
return wh;
}
vec2d get_spd(const Vehicle&v){auto*p=id2oldvehs(v.id);return !p?vec2d(0,0):get_pos(v)-get_pos(*p);}
bool is_moved(const Vehicle&v){
auto*p=id2oldvehs(v.id);
if(!p)return false;
return get_pos(v)!=get_pos(*p);
}
int new_group(){return empty_group_id++;}
//struct t_world:World{
void update(const World&world)
{
init_maps();
if(id2updated.empty())id2updated.resize(1024*1024);
if(id2vehid.empty())id2vehid.resize(1024*1024,-1);
if(id2oldvehid.empty())id2oldvehid.resize(1024*1024,-1);
if(id2groupid.empty())id2groupid.resize(1024*1024,-1);
oldvehs=vehs;
auto dec_till_zero=[](int&v){
if(!v)return;
v--;
};
QAP_FOREACH(vehs,dec_till_zero(ex.remainingAttackCooldownTicks));
QAP_FOREACH(oldvehs,id2updated[ex.id]=0);
QAP_FOREACH(world.newVehicles,qap_add_back(vehs)=ex;id2updated[ex.id]=1;);
QAP_FOREACH(vehs,id2vehid[ex.id]=i);
QAP_FOREACH(oldvehs,id2oldvehid[ex.id]=i);
auto&arr=world.vehicleUpdates;
for(int i=0;i<arr.size();i++){
auto&ex=arr[i];
//if(ex.durability==0)continue;
auto&out=*id2veh(ex.id);
out=Vehicle(out,ex);
id2updated[ex.id]=1;
if(!ex.durability)id2vehid[ex.id]=-1;
}
clean_if(vehs,[](const Vehicle&ex){return !ex.durability;});
QAP_FOREACH(vehs,id2vehid[ex.id]=i);
QAP_FOREACH(oldvehs,id2oldvehid[ex.id]=i);
w.load(*this);
}
bool is_our(const Vehicle&ref){return ref.playerId==me.id;}
bool is_our(const Facility&ref){return ref.ownerPlayerId==me.id;}
template<class TYPE>bool has_type(const TYPE&ref,VehicleType type){return ref.type==type;}
vec2d GP(const VehicleType&type){
return getSP(type).getpos();
}
vec2d EGP(const VehicleType&type){
return getSP(type,false).getpos();
}
struct t_suppos{
int n=0;
vec2d minp;
vec2d maxp;
vec2d superpos;
vec2d superspd;
real superhp=0;
int moved=0;
int updated=0;
int selected=0;
vec2d getpos()const{return superpos*(!n?0:1.0/n);}
vec2d getspd()const{return superspd*(!n?0:1.0/n);}
vec2d getsize()const{return !n?vec2d(0,0):(maxp-minp).fabs();}
vec2d getcenter()const{return !n?vec2d(1,1):(maxp+minp)*0.5;}
real gethp()const{return superhp*(!n?0:1.0/n);}
};
t_suppos get_suppos_by_type_impl(const VehicleType&type,bool our=true){
auto one=vec2d(1,1);
t_suppos out;out.minp=+one*1e6;out.maxp=-one*1e6;
QAP_FOREACH(vehs,{
if(our!=is_our(ex)||(ex.type!=type))return;
out.n++;
out.moved+=is_moved(ex);
out.updated+=id2updated[ex.id];
out.superpos+=get_pos(ex);
out.superspd+=get_spd(ex);
out.superhp+=ex.durability;
out.selected+=ex.selected;
vec2d::comax(out.maxp,get_pos(ex));
vec2d::comin(out.minp,get_pos(ex));
});
return out;
}
t_suppos get_suppos_by_type(const VehicleType&type,bool our=true){
static int cur_ticks[2][int(VehicleType::_COUNT_)+1]={{-1,-1,-1,-1,-1,-1},{-1,-1,-1,-1,-1,-1}};
static t_suppos arr[2][int(VehicleType::_COUNT_)+1];
auto id=int(type)+1;
auto&ct=cur_ticks[our][id];
auto&out=arr[our][id];
if(ct!=world.tickIndex)out=get_suppos_by_type_impl(type,our);
ct=world.tickIndex;
return out;
}
t_suppos getSP(const VehicleType&type,bool our=true){return get_suppos_by_type(type,our);}
void act_move(vec2d dir){
move.action=ActionType::MOVE;
move.x=dir.x;move.y=dir.y;
}
void act_scale(vec2d pos,real factor){
move.action=ActionType::SCALE;
move.x=pos.x;move.y=pos.y;move.factor=factor;
}
Move&act_select(VehicleType type=VehicleType::_UNKNOWN_)
{
move.setAction(ActionType::CLEAR_AND_SELECT);
move.setRight(world.getWidth());
move.setBottom(world.getHeight());
move.vehicleType=type;
return move;
}
void act_add_select(VehicleType type=VehicleType::_UNKNOWN_)
{
move.setAction(ActionType::ADD_TO_SELECTION);
move.setRight(world.getWidth());
move.setBottom(world.getHeight());
move.vehicleType=type;
}
void act_select_quad(const vec2d&p,const vec2d&half_size,VehicleType type=VehicleType::_UNKNOWN_){
move.action=ActionType::CLEAR_AND_SELECT;
auto hs=half_size;
move.top =p.y-hs.y;
move.bottom=p.y+hs.y;
move.left =p.x-hs.x;
move.right =p.x+hs.x;
move.vehicleType=type;
}
void act_assign(int group){move.action=ActionType::ASSIGN;move.group=group;}
void act_disband(int group){move.action=ActionType::DISBAND;move.group=group;}
void act_dismiss(int group){move.action=ActionType::DISMISS;move.group=group;}
void act_rot(vec2d c,real ang=PI){move.setAction(ActionType::ROTATE);move.angle=ang;move.x=c.x;move.y=c.y;}
//real get_ang_spd_for_center(vec2d c,const Vehicle&ex){
// auto d=get_pos(ex).dist_to(c);
// return !d?1e9:min_speed(ex.type)/d;
//}
//void act_stable_rot(vec2d c,real ang=PI){
// //vehs[0].selected
// auto id=QAP_MINVAL_ID_OF_VEC(vehs,ex.selected&&is_our(ex)?get_ang_spd_for_center(c,ex):1e9);
// if(id<0)return;
// //move.setMaxSpeed(0);//get_speed(vehs[id].type));
// move.setMaxAngularSpeed(get_ang_spd_for_center(c,vehs[id])*(0.25+0.125*0.25));
// move.setAction(ActionType::ROTATE);move.angle=ang;move.x=c.x;move.y=c.y;
//}
real get_vis_range(VehicleType t)
{
if(t==VT)return game.tankVisionRange;
if(t==VH)return game.helicopterVisionRange;
if(t==VA)return game.arrvVisionRange;
if(t==VI)return game.ifvVisionRange;
if(t==VF)return game.fighterVisionRange;
return game.tankVisionRange;
}
real get_vision_by_lvl(VehicleType t,int lvl){
const real wet[]={
game.clearWeatherVisionFactor,
game.cloudWeatherVisionFactor,
game.rainWeatherVisionFactor
};
const real ter[]={
game.plainTerrainVisionFactor,
game.swampTerrainVisionFactor,//don't copypaste
game.forestTerrainVisionFactor//see: forest vs swamp
};
bool air=t==VH||t==VF;
return (air?wet:ter)[lvl]*get_vis_range(t);
}
real get_speed(VehicleType t){
if(t==VT)return game.tankSpeed;
if(t==VH)return game.helicopterSpeed;
if(t==VA)return game.arrvSpeed;
if(t==VI)return game.ifvSpeed;
if(t==VF)return game.fighterSpeed;
return game.tankSpeed;
}
int get_att_cdt(VehicleType t){
if(t==VT)return game.tankAttackCooldownTicks;
if(t==VH)return game.helicopterAttackCooldownTicks;
if(t==VA)return game.tickCount*2;
if(t==VI)return game.ifvAttackCooldownTicks;
if(t==VF)return game.fighterAttackCooldownTicks;
return game.tickCount*2;
}
int get_air_dmg(VehicleType t){
if(t==VT)return game.tankAerialDamage;
if(t==VH)return game.helicopterAerialDamage;
if(t==VA)return 0;
if(t==VI)return game.ifvAerialDamage;
if(t==VF)return game.fighterAerialDamage;
return 0;
}
int get_gnd_dmg(VehicleType t){
if(t==VT)return game.tankGroundDamage;
if(t==VH)return game.helicopterGroundDamage;
if(t==VA)return 0;
if(t==VI)return game.ifvGroundDamage;
if(t==VF)return game.fighterGroundDamage;
return 0;
}
real get_air_range(VehicleType t){
if(t==VT)return game.tankAerialAttackRange;
if(t==VH)return game.helicopterAerialAttackRange;
if(t==VA)return 0;
if(t==VI)return game.ifvAerialAttackRange;
if(t==VF)return game.fighterAerialAttackRange;
return 0;
}
real get_gnd_range(VehicleType t){
if(t==VT)return game.tankGroundAttackRange;
if(t==VH)return game.helicopterGroundAttackRange;
if(t==VA)return 0;
if(t==VI)return game.ifvGroundAttackRange;
if(t==VF)return game.fighterGroundAttackRange;
return 0;
}
int get_air_def(VehicleType t){
if(t==VT)return game.tankAerialDefence;
if(t==VH)return game.helicopterAerialDefence;
if(t==VA)return game.arrvAerialDefence;
if(t==VI)return game.ifvAerialDefence;
if(t==VF)return game.fighterAerialDefence;
return 0;
}
int get_gnd_def(VehicleType t){
if(t==VT)return game.tankGroundDefence;
if(t==VH)return game.helicopterGroundDefence;
if(t==VA)return game.arrvGroundDefence;
if(t==VI)return game.ifvGroundDefence;
if(t==VF)return game.fighterGroundDefence;
return 0;
}
real get_max_att_range(){
static real out=0;
if(out)return out;
VehicleType arr[]={VT,VH,VA,VI,VF};
for(int i=0;i<lenof(arr);i++){
auto&ex=arr[i];
out=std::max(out,get_air_range(ex));
out=std::max(out,get_gnd_range(ex));
}
return out;
}
static real get_exchange_koef_impl(VehicleType u,VehicleType e){
auto always_win=0.5;
if(u==VA)if(e==VA)return 0.0;
if(u==VF)if(e==VF)return 0.3;
if(u==VT)if(e==VT)return 0.4;
if(u==e)return 0.1;
if(u==VF)if(e==VH)return +1.0;
if(u==VF)if(e==VT)return 0.0;
if(u==VF)if(e==VI)return 0.025;//one_dir
if(u==VF)if(e==VA)return 0.0;
if(u==VH)if(e==VF)return -0.15;
if(u==VH)if(e==VT)return +0.8;
if(u==VH)if(e==VI)return +0.075;
if(u==VH)if(e==VA)return always_win;
if(u==VT)if(e==VF)return 0.0;
if(u==VT)if(e==VH)return 0.0;
if(u==VT)if(e==VI)return +0.7;
if(u==VT)if(e==VA)return always_win;
if(u==VI)if(e==VF)return 0.7;//one_dir
if(u==VI)if(e==VH)return 0.7;
if(u==VI)if(e==VT)return 0.0;//one_dir
if(u==VI)if(e==VA)return always_win;
return -get_exchange_koef_impl(e,u);
}
struct t_exchange_table{
real table[int(VehicleType::_COUNT_)][int(VehicleType::_COUNT_)];
real get_koef(VehicleType u,VehicleType e){return table[int(u)][int(e)];}
static t_exchange_table&get(){
static t_exchange_table table;
static bool need_init=true;
if(!need_init)return table;
need_init=false;
int n=int(VehicleType::_COUNT_);
for(int i=0;i<n;i++)for(int j=0;j<n;j++){
auto u=VehicleType(i);
auto e=VehicleType(j);
table.table[int(u)][int(e)]=get_exchange_koef_impl(u,e);
}
return table;
}
};
real min_speed(VehicleType t){
auto ter=game.swampTerrainSpeedFactor;auto wet=game.rainWeatherSpeedFactor;
return (t==VH||t==VF?wet:ter)*get_speed(t);
}
real mid_speed(VehicleType t){
auto ter=game.forestTerrainSpeedFactor;auto wet=game.cloudWeatherSpeedFactor;
return (t==VH||t==VF?wet:ter)*get_speed(t);
}
real max_speed(VehicleType t){
auto ter=game.plainTerrainSpeedFactor;auto wet=game.clearWeatherSpeedFactor;
return (t==VH||t==VF?wet:ter)*get_speed(t);
}
real get_speed_by_lvl(VehicleType t,int lvl){
const real wet[]={
game.clearWeatherSpeedFactor,
game.cloudWeatherSpeedFactor,
game.rainWeatherSpeedFactor
};
const real ter[]={
game.plainTerrainSpeedFactor,
game.forestTerrainSpeedFactor,
game.swampTerrainSpeedFactor
};
bool air=t==VH||t==VF;
return (air?wet:ter)[lvl]*get_speed(t);
}
static VehicleType CharToVehicleType(const char type){
if(type=='H')return VH;
if(type=='I')return VI;
if(type=='F')return VF;
if(type=='T')return VT;
if(type=='A')return VA;
QapAssert(false);
return VehicleType::_UNKNOWN_;
}
#ifdef Adler
static string VehicleTypeToString(VehicleType type){
if(type==VH)return "VH";
if(type==VI)return "VI";
if(type==VF)return "VF";
if(type==VT)return "VT";
if(type==VA)return "VA";
if(type==VehicleType::_UNKNOWN_)return "UNK";
return "WTF("+IToS(int(type))+")";
}
static string ActionToString(ActionType type){
#define F(X)if(ActionType::X==type)return #X;
F(_UNKNOWN_);
F(NONE);
F(CLEAR_AND_SELECT);
F(ADD_TO_SELECTION);
F(DESELECT);
F(ASSIGN);
F(DISMISS);
F(DISBAND);
F(MOVE);
F(ROTATE);
F(SCALE);
F(SETUP_VEHICLE_PRODUCTION);
F(TACTICAL_NUCLEAR_STRIKE);
#undef F
return "WTF("+IToS(int(type))+")";
}
#endif
bool eq_type(const VehicleType&t,const char type)
{
return CharToVehicleType(type)==t;
}
bool any_vtype_of(const Vehicle&v,const char*types)
{
for(;*types;){if(eq_type(v.type,*types))return true;types++;}return false;
}
bool any_vtype_of(const VehicleType&type,const char*types)
{
for(;*types;){if(eq_type(type,*types))return true;types++;}return false;
}
struct t_cmds{
struct t_sys_impl{
static const int counter_base=__COUNTER__+1;
};
#define IMPL()/*struct t_sys_impl{*/static const int cmd_id=__COUNTER__-t_cmds::t_sys_impl::counter_base;/*}*/
struct t_mov{
vec2d dir;
real spd=0;
IMPL();
Move get()const{Move out;out.action=ActionType::MOVE;out.x=dir.x;out.y=dir.y;out.maxSpeed=spd;return out;}
};
struct t_group{
int group_id=-1;
bool add=false;
IMPL();
Move get()const{QapNoWay();Move out;out.action=add?ActionType::ADD_TO_SELECTION:ActionType::CLEAR_AND_SELECT;out.group=group_id;return out;}
};
struct t_rect{
vec2d pos;
vec2d size;
VehicleType type;
bool add=false;
IMPL();
Move get()const{
Move out;
out.action=add?ActionType::ADD_TO_SELECTION:ActionType::CLEAR_AND_SELECT;
auto hs=size*0.5;
out.left=pos.x-hs.x;out.right=pos.x+hs.x;
out.top=pos.y-hs.y;out.bottom=pos.y+hs.y;
out.vehicleType=type;
return out;
}
};
struct t_rot{
vec2d pos;
real ang=0;
real spd=0;
IMPL();
Move get()const{Move out;out.action=ActionType::ROTATE;out.x=pos.x;out.y=pos.y;out.angle=ang;out.maxSpeed=spd;return out;}
};
struct t_scale{
vec2d pos;
real factor=0;
real spd=0;
IMPL();
Move get()const{Move out;out.action=ActionType::SCALE;out.x=pos.x;out.y=pos.y;out.factor=factor;out.maxSpeed=spd;return out;}
};
struct t_bomb{
vec2d pos;
int veh_id=-1;
IMPL();
Move get()const{Move out;out.action=ActionType::TACTICAL_NUCLEAR_STRIKE;out.x=pos.x;out.y=pos.y;out.vehicleId=veh_id;return out;}
};
#undef IMPL
};
struct t_move:t_cmds{
//union{
t_mov mov;
t_group group;
t_rect rect;
t_rot rot;
t_scale scale;
t_bomb bomb;
//};
int type=-1;
//static vector<t_move> get_root_set(){
// return get_mov_set();
//}
Move get()const{
#define F(mov)if(type==t_cmds::t_##mov::cmd_id)return mov.get();
F(mov);
F(rect);
F(group);
F(rot);
F(scale);
F(bomb);
#undef F
if(type==-1)return Move();
QapNoWay();
return Move();
}
static vector<vec2d> get_movdirs(real minlen,int len,int dirs){
/*static */vector<vec2d> id2dir;
//if(id2dir.size())return id2dir;
real inv_dirs=1.0/dirs;
for(int i=0;i<dirs;i++)for(int j=1;j<=len;j++)qap_add_back(id2dir)=Vec2dEx(i*inv_dirs*Pi2,j*minlen);
return id2dir;
}
static vector<vec2d> get_movdirs_norm(int dirs){
/*static */vector<vec2d> id2dir;
//if(id2dir.size())return id2dir;
real inv_dirs=1.0/dirs;
for(int i=0;i<dirs;i++)qap_add_back(id2dir)=Vec2dEx(i*inv_dirs*Pi2,1.0);
return id2dir;
}
static vector<vec2d> mul(const vector<vec2d>&inp,real k){auto out=inp;QAP_FOREACH(out,ex*=k);return out;}
//static vector<t_move> get_mov_set(real minlen=1.2*5.0,int len=1,int dirs=8,real maxspd=0,real dspd=0.1){
// vector<t_move> out;
// auto&arr=get_movdirs(minlen,len,dirs);
// for(int i=0;i<arr.size();i++){
// auto&ex=arr[i];
// for(real spd=0;spd<=maxspd;spd+=dspd){
// auto&c=qap_add_back(out);
// c.type=c.mov.cmd_id;
// auto&m=c.mov;
// m.dir=ex;
// m.spd=spd;
// }
// }
// return out;
//}
//static vector<t_move> get_mov_set_v2(const vector<real>&spds,real minlen=1.2*5.0,int len=1,int dirs=8){
// vector<t_move> out;
// auto&arr=get_movdirs(minlen,len,dirs);
// for(int i=0;i<arr.size();i++){
// auto&ex=arr[i];
// for(real k=0;k<spds.size();k++){
// auto&c=qap_add_back(out);
// c.type=c.mov.cmd_id;
// auto&m=c.mov;
// m.dir=ex;
// m.spd=spds[k];
// }
// }
// return out;
//}
static vector<t_move> get_mov_set_v3(t_app&app,VehicleType type,int GAP=5,int len=1,int dirs=8){
vector<t_move> out;
auto mdn=get_movdirs_norm(dirs);
vector<vec2d> arr;
for(int i=1;i<=len;i++)
{
for(int k=0;k<3;k++)
{
auto spd_lvl=k;
auto spd=app.get_speed_by_lvl(type,spd_lvl);
for(int dir=0;dir<dirs;dir++)
{
auto&c=qap_add_back(out);
c.type=c.mov.cmd_id;
auto&m=c.mov;
m.dir=mdn[dir]*spd*len*GAP;
m.spd=spd;
}
}
}
return out;
}
static vector<t_move> get_mov_set_v4(t_app&app,VehicleType type,int GAP=5,int len=1,int dirs=8){
vector<t_move> out;
auto mdn=get_movdirs_norm(dirs);
vector<vec2d> arr;
for(int i=1;i<=len;i++)
{
auto spd=app.get_speed_by_lvl(type,0);
for(int dir=0;dir<dirs;dir++)
{
auto&c=qap_add_back(out);
c.type=c.mov.cmd_id;
auto&m=c.mov;
m.dir=mdn[dir]*spd*len*GAP;
m.spd=spd;
}
}
return out;
}
};
//real get_k(const vec2d&pos,bool air){
// return 1.0;//(air?wetmap:termap).get(pos*inv_cell_size);
//}
//static const real LEN_KOEF=10;
struct t_building{
int id=-1;