-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyStrategy.cpp
2052 lines (1751 loc) · 72.6 KB
/
MyStrategy.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 "MyStrategy.h"
#include <cmath>
#include <cstdlib>
#include <cassert>
#include <algorithm>
#include <limits>
#include <map>
//#define PRINT_LOG
//#define CHECK_PREDICTION
#include <iostream> // DEBUG
#include <iomanip> // DEBUG
using namespace std;
constexpr double pi = 3.14159265358979323846264338327950288;
inline double frand()
{
return rand() * (2.0 / RAND_MAX) - 1;
}
inline constexpr double sqr(double x)
{
return x * x;
}
inline constexpr double rem(double x, double y)
{
return y * (x / y - floor(x / y));
}
inline constexpr double relAngle(double angle)
{
return rem(angle + pi, 2 * pi) - pi;
}
struct Vec2D
{
double x, y;
Vec2D()
{
}
constexpr Vec2D(const Vec2D &v) : x(v.x), y(v.y)
{
}
constexpr Vec2D(double x_, double y_) : x(x_), y(y_)
{
}
Vec2D &operator = (const Vec2D &v)
{
x = v.x; y = v.y; return *this;
}
constexpr Vec2D operator + (const Vec2D &v) const
{
return Vec2D(x + v.x, y + v.y);
}
Vec2D &operator += (const Vec2D &v)
{
x += v.x; y += v.y; return *this;
}
constexpr Vec2D operator - (const Vec2D &v) const
{
return Vec2D(x - v.x, y - v.y);
}
Vec2D &operator -= (const Vec2D &v)
{
x -= v.x; y -= v.y; return *this;
}
constexpr Vec2D operator - () const
{
return Vec2D(-x, -y);
}
constexpr Vec2D operator * (double a) const
{
return Vec2D(a * x, a * y);
}
Vec2D &operator *= (double a)
{
x *= a; y *= a; return *this;
}
constexpr double operator * (const Vec2D &v) const
{
return x * v.x + y * v.y;
}
constexpr Vec2D operator / (double a) const
{
return (*this) * (1 / a);
}
Vec2D operator /= (double a)
{
return (*this) *= (1 / a);
}
constexpr Vec2D operator ~ () const
{
return Vec2D(y, -x);
}
constexpr double operator % (const Vec2D &v) const
{
return *this * ~v;
}
constexpr double sqr() const
{
return x * x + y * y;
}
constexpr double len() const
{
return std::sqrt(x * x + y * y);
}
};
inline constexpr Vec2D operator * (double a, const Vec2D &v)
{
return v * a;
}
inline constexpr Vec2D normalize(const Vec2D &v)
{
return v / v.len();
}
inline constexpr Vec2D sincos(double angle)
{
return Vec2D(cos(angle), sin(angle));
}
inline constexpr Vec2D sincos_fast(float angle)
{
return Vec2D(cos(angle), sin(angle));
}
inline constexpr Vec2D rotate(const Vec2D &v1, const Vec2D &v2)
{
return Vec2D(v1.x * v2.x - v1.y * v2.y, v1.x * v2.y + v1.y * v2.x);
}
inline constexpr Vec2D conj(const Vec2D &v)
{
return Vec2D(v.x, -v.y);
}
enum PuckStatus
{
HOLD, FLY, PUCK_STATUS_COUNT
};
constexpr int MAX_ACTIVE = 3;
constexpr int maxLookahead = 160, stickLookahead = 20;
constexpr int turnStep = 4, strikeStep = 4, strikeDelta = 8, targetCount = 2;
constexpr int optSurvive = 4, optOffspring = 4, optStepCount = 4, optFinalCount = 4;
constexpr double optStepBase = 8, optStepMul = 0.5;
constexpr double stickThreshold = 20;
constexpr double cellSize = 32;
constexpr double timeGamma = 1 - 1.0 / maxLookahead;
constexpr double dangerMultiplier = 4, passBonus = 2, passMultiplier = 0.1;
constexpr double dangerThreshold = 0.1;
constexpr double hockeyistFrict = 0.02, angularFrict = 0.0270190131;
constexpr double puckFrict = 0.001, puckBeta = -log(1 - puckFrict);
constexpr double wallBounce = 0.25, hockeyistBounce = 0.325, goalieFrict = 0.1;
constexpr double minDepth = 0.01, depthFactor = 0.8;
bool leftPlayer;
double rinkLeft, rinkRight, rinkTop, rinkBottom;
double rinkWidth, rinkHeight; Vec2D rinkCenter;
int gridHalfWidth, gridHalfHeight, gridLine, gridHeight, gridCenter, gridSize;
double goalCenter, goalHalf, hockeyistRad, puckRad, goalieSpd, goalieRange;
double staminaBase, staminaGrowth, accelMin, accelMax, turnAngle;
double stickLength, effStickLength, stickSector, stickSectorTan, holdDist, timeGammaSwing; int maxSwing;
double strikeBase, strikeGrowth, passBase, strikeBeta, passBeta, timeSpread; Vec2D passSector;
double minChance, maxChance, knockdownChance, chanceDrop;
double strikeChance, pickChanceDelta[PUCK_STATUS_COUNT];
int globalTick = -1, goalieTime, activeCount = 0;
void initConsts(const model::Game& game, const model::World& world)
{
rinkLeft = game.getRinkLeft(); rinkRight = game.getRinkRight();
rinkTop = game.getRinkTop(); rinkBottom = game.getRinkBottom();
rinkWidth = rinkRight - rinkLeft; rinkHeight = rinkBottom - rinkTop;
rinkCenter.x = (rinkLeft + rinkRight) / 2;
rinkCenter.y = (rinkTop + rinkBottom) / 2;
leftPlayer = (world.getMyPlayer().getNetBack() < rinkCenter.x);
gridHalfWidth = lround(rinkWidth / (2 * cellSize)) + 3;
gridHalfHeight = lround(rinkHeight / (2 * cellSize)) + 3;
gridLine = 2 * gridHalfWidth + 1; gridHeight = 2 * gridHalfHeight + 1;
gridCenter = gridHalfHeight * gridLine + gridHalfWidth;
gridSize = gridHeight * gridLine;
goalHalf = game.getGoalNetHeight() / 2; goalCenter = game.getGoalNetTop() + goalHalf;
hockeyistRad = world.getHockeyists()[0].getRadius(); puckRad = world.getPuck().getRadius();
goalieSpd = game.getGoalieMaxSpeed(); goalieRange = goalHalf - hockeyistRad;
staminaBase = 0.01 * game.getZeroStaminaHockeyistEffectivenessFactor();
staminaGrowth = (0.01 - staminaBase) / game.getHockeyistMaxStamina();
accelMin = -game.getHockeyistSpeedDownFactor(); accelMax = game.getHockeyistSpeedUpFactor();
turnAngle = game.getHockeyistTurnAngleFactor();
stickLength = game.getStickLength(); effStickLength = stickLength / sqrt(3.0);
stickSector = game.getStickSector() / 2; stickSectorTan = tan(stickSector);
holdDist = game.getPuckBindingRange(); timeGammaSwing = pow(timeGamma, maxSwing);
maxSwing = game.getMaxEffectiveSwingTicks();
strikeBase = game.getStruckPuckInitialSpeedFactor() * game.getStrikePowerBaseFactor();
strikeGrowth = game.getStruckPuckInitialSpeedFactor() * game.getStrikePowerGrowthFactor();
passBase = game.getStruckPuckInitialSpeedFactor() * game.getPassPowerFactor();
strikeBeta = 1 / (game.getStrikeAngleDeviation() * sqrt(2.0));
passBeta = 1 / (game.getPassAngleDeviation() * sqrt(2.0));
timeSpread = strikeBase / strikeBeta;
passSector = sincos(game.getPassSector() / 2);
minChance = game.getMinActionChance();
maxChance = game.getMaxActionChance();
knockdownChance = game.getKnockdownChanceFactor();
chanceDrop = 1 / game.getStruckPuckInitialSpeedFactor();
strikeChance = game.getStrikePuckBaseChance();
pickChanceDelta[HOLD] = strikeChance - game.getTakePuckAwayBaseChance();
pickChanceDelta[FLY] = strikeChance - game.getPickUpPuckBaseChance();
}
inline double toChance(double chance)
{
return max(minChance, min(maxChance, chance));
}
inline int gridPos(const Vec2D &pos)
{
Vec2D offs = (pos - rinkCenter) / cellSize + Vec2D(gridHalfWidth + 0.5, gridHalfHeight + 0.5);
return int(offs.y) * gridLine + int(offs.x);
}
struct UnitState
{
Vec2D pos, spd;
void set(const model::Unit& unit)
{
pos = Vec2D(unit.getX(), unit.getY());
spd = Vec2D(unit.getSpeedX(), unit.getSpeedY());
}
bool bounceCircle(const Vec2D ¢er, double rad, double bounce, double frict = 0)
{
Vec2D delta = center - pos; if(!(delta.sqr() < rad * rad))return false;
double normSpd = max(0.0, spd * delta), normImp = (1 + bounce) * normSpd;
double tanImp = frict * normImp; tanImp = max(-tanImp, min(tanImp, spd % delta)); // TODO: incorrect tanImp
double invLen2 = 1 / delta.sqr(), invLen = sqrt(invLen2), depth = rad * invLen - 1;
if((normImp - normSpd) * invLen2 < depth)pos -= depthFactor * (depth - minDepth * invLen) * delta;
spd -= (normImp * delta + tanImp * ~delta) * invLen2; return true;
}
};
struct StickSector
{
Vec2D pos, dir, side;
double back, forw, base, coeff;
void set(const Vec2D &pos_, const Vec2D &dir_, double back_, const Vec2D &forw_, double angle)
{
pos = pos_; dir = dir_; side = sincos(angle + stickSector); back = back_;
double phi = angle + stickSector / 2, dr = forw_.y / sin(phi); forw = forw_.x - dr * cos(phi);
base = 0.5 * (stickLength + dr); coeff = 0.5 / (stickLength + dr);
}
double distance(const Vec2D &pt) const
{
double dot = (pt - pos) * dir, cross = (pt - pos) % dir;
return max((sqr(dot - forw) + sqr(cross)) * coeff - base, side.x * abs(cross) - side.y * (dot + back));
}
};
struct HockeyistInfo
{
long long id; int index; double stamina;
double accelMin, accelMax, turnAngle, maxTurnSteps;
int maxTurnCount; Vec2D turnRot, endTurnRot;
double strikeBase, strikeGrowth, passBase, strikeBeta, passBeta;
double attack[PUCK_STATUS_COUNT], defence;
double knockoutChance, knockdownChance;
double followMoveCoeff, followRotCoeff;
double staminaCoeff, staminaBias, defenceBias;
bool havePuck;
int knockdown, cooldown;
vector<StickSector> stick;
vector<Vec2D> rot;
HockeyistInfo(const model::Hockeyist& hockeyist) : id(hockeyist.getId())
{
}
void set(const model::Hockeyist& hockeyist, long long puckOwner, int index_)
{
index = index_; stamina = hockeyist.getStamina();
double mul = staminaBase + staminaGrowth * stamina;
double str = mul * hockeyist.getStrength();
double dex = mul * hockeyist.getDexterity();
double end = mul * hockeyist.getEndurance();
double agi = mul * hockeyist.getAgility();
accelMin = ::accelMin * agi; accelMax = ::accelMax * agi;
turnAngle = ::turnAngle * agi; maxTurnSteps = (pi / 2) / turnAngle; maxTurnCount = int(maxTurnSteps);
turnRot = sincos(turnAngle); endTurnRot = sincos(turnAngle * (maxTurnSteps - maxTurnCount));
strikeBase = ::strikeBase * str; strikeGrowth = ::strikeGrowth * str; passBase = ::passBase * str;
strikeBeta = ::strikeBeta * dex; passBeta = ::passBeta * dex;
attack[HOLD] = ::strikeChance + max(str, dex);
attack[FLY] = ::strikeChance + max(dex, agi); defence = max(end, agi);
knockoutChance = toChance(::strikeChance - defence + 1);
knockdownChance = toChance(::knockdownChance - end + 1);
double timeCoeff = 0.01 / (1 - timeGamma);
followMoveCoeff = timeCoeff * sqr(hockeyistFrict / accelMax);
followRotCoeff = 0.02 * timeCoeff / sqr(turnAngle);
staminaCoeff = 1; staminaBias = 500 * staminaCoeff;
defenceBias = 500 * defence;
havePuck = (id == puckOwner);
knockdown = hockeyist.getRemainingKnockdownTicks();
cooldown = max(knockdown, hockeyist.getRemainingCooldownTicks());
double angSpd = hockeyist.getAngularSpeed();
rot.resize(maxLookahead + 1); rot[0] = sincos(angSpd);
for(int i = 1; i <= maxLookahead; i++)
{
angSpd -= angularFrict * angSpd; rot[i] = sincos(angSpd);
}
UnitState state; state.set(hockeyist); Vec2D dir = sincos(hockeyist.getAngle());
stick.resize(stickLookahead + 1); stick[0].set(state.pos, dir, 0, Vec2D(0, 0), 0);
double back = 0, backSpd = 0; Vec2D forw(0, 0), forwSpd(0, 0), forwDir(1, 0);
for(int i = 1; i <= stickLookahead; i++)
{
state.spd -= hockeyistFrict * state.spd;
state.pos += state.spd; dir = rotate(dir, rot[i]);
if(i > knockdown)
{
backSpd -= accelMin; backSpd -= hockeyistFrict * backSpd; back += backSpd;
forwSpd += accelMax * forwDir; forwSpd -= hockeyistFrict * forwSpd;
forw += forwSpd; forwDir = rotate(forwDir, turnRot);
}
stick[i].set(state.pos, dir, back, forw, (i - knockdown) * turnAngle);
}
}
};
struct HockeyistState : public UnitState
{
Vec2D dir;
void set(const model::Hockeyist& hockeyist)
{
UnitState::set(hockeyist);
dir = sincos(hockeyist.getAngle());
}
void nextStep(const HockeyistInfo &info, double accel, const Vec2D &turn, int time)
{
spd += accel * dir; spd -= hockeyistFrict * spd;
double delta;
if((delta = rinkLeft + hockeyistRad - pos.x) > 0)
{
double sign = pos.y - goalCenter, offs = abs(sign) - goalHalf;
if(offs < 0)
{
if(delta > +1)pos.x = rinkLeft - hockeyistRad - 1;
}
else if(!bounceCircle(Vec2D(rinkLeft, goalCenter + copysign(goalHalf, sign)), hockeyistRad, hockeyistBounce) &&
!bounceCircle(Vec2D(rinkLeft, goalCenter + copysign(goalHalf + 25, sign)), hockeyistRad, hockeyistBounce / 10))
{
if(offs > 25)
{
if(spd.x < 0)spd.x *= -hockeyistBounce;
if(spd.x < delta)pos.x += depthFactor * (delta + minDepth);
}
else
{
if(spd.x < 0)spd.x *= -hockeyistBounce / 10;
if(spd.x < delta)pos.x += depthFactor * (delta + minDepth);
}
}
}
if((delta = rinkRight - hockeyistRad - pos.x) < 0)
{
double sign = pos.y - goalCenter, offs = abs(sign) - goalHalf;
if(offs < 0)
{
if(delta < -1)pos.x = rinkRight - hockeyistRad - 1;
}
else if(!bounceCircle(Vec2D(rinkRight, goalCenter + copysign(goalHalf, sign)), hockeyistRad, hockeyistBounce) &&
!bounceCircle(Vec2D(rinkRight, goalCenter + copysign(goalHalf + 25, sign)), hockeyistRad, hockeyistBounce / 10))
{
if(offs > 25)
{
if(spd.x > 0)spd.x *= -hockeyistBounce;
if(spd.x > delta)pos.x += depthFactor * (delta + minDepth);
}
else
{
if(spd.x > 0)spd.x *= -hockeyistBounce / 10;
if(spd.x > delta)pos.x += depthFactor * (delta + minDepth);
}
}
}
if((delta = rinkTop + hockeyistRad - pos.y) > 0)
{
if(spd.y < 0)spd.y *= -hockeyistBounce;
if(spd.y < delta)pos.y += depthFactor * (delta - minDepth);
}
if((delta = rinkBottom - hockeyistRad - pos.y) < 0)
{
if(spd.y > 0)spd.y *= -hockeyistBounce;
if(spd.y > delta)pos.y += depthFactor * (delta + minDepth);
}
pos += spd; dir = rotate(dir, info.rot[time]); dir = rotate(dir, turn);
}
};
struct SubstituteInfo
{
int index; double stamina;
SubstituteInfo(const model::Hockeyist& hockeyist) :
index(hockeyist.getTeammateIndex()), stamina(hockeyist.getStamina())
{
}
bool operator < (const SubstituteInfo &info) const
{
return stamina > info.stamina;
}
};
struct Safety
{
double val[MAX_ACTIVE];
Safety(double val_ = 1)
{
for(int i = 0; i < activeCount; i++)val[i] = val_;
}
Safety &update(const Safety &safety)
{
for(int i = 0; i < activeCount; i++)val[i] = min(val[i], safety.val[i]); return *this;
}
double update(int index, double val_)
{
return val[index] = min(val[index], val_);
}
double multiply(double mul) const
{
for(int i = 0; i < activeCount; i++)mul *= val[i]; return mul;
}
};
struct SimplePuckState : public UnitState
{
double spdLen;
SimplePuckState(const Vec2D &pos_, const Vec2D &spd_)
{
pos = pos_; spd = spd_; spdLen = spd.len();
}
bool nextStep()
{
spd -= puckFrict * spd; spdLen -= puckFrict * spdLen; pos += spd;
if(abs(pos.x - rinkCenter.x) > rinkWidth / 2 + cellSize)return false;
double delta;
if((delta = rinkTop + puckRad - pos.y) > 0)
{
if(spd.y < 0)
{
spd.y *= -wallBounce; spdLen = spd.len();
}
if(spd.y < delta)pos.y += depthFactor * (delta - minDepth);
}
if((delta = rinkBottom - puckRad - pos.y) < 0)
{
if(spd.y > 0)
{
spd.y *= -wallBounce; spdLen = spd.len();
}
if(spd.y > delta)pos.y += depthFactor * (delta + minDepth);
}
return true;
}
};
struct PuckState : public UnitState
{
Vec2D goalie[2];
PuckState() = default;
PuckState(const Vec2D &pos_, const Vec2D &spd_)
{
pos = pos_; spd = spd_;
goalie[0] = Vec2D(rinkLeft + hockeyistRad, pos_.y);
goalie[1] = Vec2D(rinkRight - hockeyistRad, pos_.y);
}
int nextStep(int time)
{
double y = max(goalCenter - goalieRange, min(goalCenter + goalieRange, pos.y));
for(int i = 0; i < 2; i++)goalie[i].y = max(goalie[i].y - goalieSpd, min(goalie[i].y + goalieSpd, y));
spd -= puckFrict * spd;
double delta;
constexpr double eps = 10;
if((delta = rinkLeft + puckRad - pos.x) > 0)
{
if(abs(pos.y - goalCenter) < goalHalf + eps)
{
if(delta > puckRad)return -1;
//bounceCircle(Vec2D(rinkLeft, goalCenter - goalHalf), puckRad, 0.025, 0.0);
//bounceCircle(Vec2D(rinkLeft, goalCenter + goalHalf), puckRad, 0.025, 0.0);
}
else
{
if(spd.x < 0)spd.x *= -wallBounce;
if(spd.x < delta)pos.x += depthFactor * (delta - minDepth);
}
}
if((delta = rinkRight - puckRad - pos.x) < 0)
{
if(abs(pos.y - goalCenter) < goalHalf + eps)
{
if(delta < -puckRad)return 1;
//bounceCircle(Vec2D(rinkRight, goalCenter - goalHalf), puckRad, 0.025, 0.0);
//bounceCircle(Vec2D(rinkRight, goalCenter + goalHalf), puckRad, 0.025, 0.0);
}
else
{
if(spd.x > 0)spd.x *= -wallBounce;
if(spd.x > delta)pos.x += depthFactor * (delta + minDepth);
}
}
if((delta = rinkTop + puckRad - pos.y) > 0)
{
if(spd.y < 0)spd.y *= -wallBounce;
if(spd.y < delta)pos.y += depthFactor * (delta - minDepth);
}
if((delta = rinkBottom - puckRad - pos.y) < 0)
{
if(spd.y > 0)spd.y *= -wallBounce;
if(spd.y > delta)pos.y += depthFactor * (delta + minDepth);
}
if(time <= goalieTime)for(int i = 0; i < 2; i++)
bounceCircle(goalie[i], hockeyistRad + puckRad, hockeyistBounce, goalieFrict);
pos += spd; return 0;
}
};
inline Vec2D puckPos(const HockeyistInfo &info, const HockeyistState &state, int time)
{
return state.pos + hockeyistFrict * state.spd + holdDist * rotate(state.dir, conj(info.rot[time]));
}
struct PathPoint
{
Vec2D pos; PuckStatus status;
Safety intact; double chanceDrop;
void set(const PuckState &state, int time)
{
pos = state.pos; status = FLY; intact = 1;
chanceDrop = ::chanceDrop * state.spd.len();
}
void set(const HockeyistInfo &info, const HockeyistState &state, int time)
{
pos = puckPos(info, state, time); status = HOLD; intact = 1;
chanceDrop = info.defence;
}
};
namespace MoveFlag
{
enum
{
FORW = 0, FIRST_LEFT = 0, SECOND_LEFT = 0,
BACK = 1, FIRST_RIGHT = 2, SECOND_RIGHT = 4,
ROTATE_ONLY = 8, STOP = 16, SUBSTITUTE = 32
};
}
constexpr double stepValue(double val)
{
return val < 0 ? 0 : (val > 1 ? 1 : val);
}
template<typename T, typename State> using AddPointFunc =
void (T::*)(const HockeyistInfo &info, State &state, int time, int flags, double turnTime);
template<typename T, typename State> using ClosePathFunc =
void (T::*)(const HockeyistInfo &info, State &state, int flags, double turnTime);
template<typename T, typename State, AddPointFunc<T, State> addPoint, ClosePathFunc<T, State> closePath> struct Mapper
{
T &obj;
const HockeyistInfo &info;
Mapper(T &obj_, const HockeyistInfo &info_) : obj(obj_), info(info_)
{
}
void fillMapTail(const State &state, int flags, double accel, int time)
{
State cur = state;
for(int i = time + 1; i <= maxLookahead; i++)
{
cur.nextStep(info, accel, Vec2D(1, 0), i); (obj.*addPoint)(info, cur, i, flags, time);
}
(obj.*closePath)(info, cur, flags, time);
}
void fillMapTail(const State &state, int flags, double accel, const Vec2D &turn, const Vec2D &endTurn, int time)
{
double turnTime = time + info.maxTurnSteps;
State cur = state; flags ^= MoveFlag::BACK;
int i = time, end = time + info.maxTurnCount;
for(i++; i <= maxLookahead; i++)
{
if(i > end)
{
cur.nextStep(info, accel, endTurn, i); (obj.*addPoint)(info, cur, i, flags, turnTime); break;
}
cur.nextStep(info, accel, turn, i); (obj.*addPoint)(info, cur, i, flags, i);
}
for(i++; i <= maxLookahead; i++)
{
cur.nextStep(info, accel, Vec2D(1, 0), i); (obj.*addPoint)(info, cur, i, flags, turnTime);
}
(obj.*closePath)(info, cur, flags, turnTime);
}
void fillMap(const State& state, int flags)
{
double accel1 = info.accelMax, accel2 = info.accelMin;
if(flags & MoveFlag::BACK)swap(accel1, accel2);
Vec2D turn = info.turnRot, endTurn = info.endTurnRot;
if(flags & MoveFlag::FIRST_RIGHT)
{
turn.y = -turn.y; endTurn.y = -endTurn.y;
}
State cur = state;
int n = min(maxLookahead, info.knockdown + info.maxTurnCount);
for(int i = info.knockdown + 1; i <= n; i++)
{
cur.nextStep(info, accel1, turn, i); (obj.*addPoint)(info, cur, i, flags, i); if(i % turnStep)continue; // TODO: globalTick ?
fillMapTail(cur, flags, accel1, i); fillMapTail(cur, flags, accel2, turn, endTurn, i);
}
}
void fillMap(const State& state)
{
State cur = state; (obj.*addPoint)(info, cur, 0, 0, 0);
for(int i = 1; i <= info.knockdown; i++)
{
cur.nextStep(info, 0, Vec2D(1, 0), i); (obj.*addPoint)(info, cur, i, 0, 0);
}
fillMapTail(cur, MoveFlag::BACK, info.accelMin, info.knockdown);
fillMapTail(cur, MoveFlag::FORW, info.accelMax, info.knockdown);
fillMap(cur, MoveFlag::BACK | MoveFlag::FIRST_LEFT);
fillMap(cur, MoveFlag::BACK | MoveFlag::FIRST_RIGHT);
fillMap(cur, MoveFlag::FORW | MoveFlag::FIRST_LEFT);
fillMap(cur, MoveFlag::FORW | MoveFlag::FIRST_RIGHT);
}
};
bool checkGoalie(const Vec2D &pos, double rad)
{
double x = min(pos.y - rinkLeft - hockeyistRad, rinkRight + hockeyistRad - pos.y);
double y = max(0.0, abs(pos.y - goalCenter) - goalHalf);
return x * x + y * y < sqr(hockeyistRad + rad);
}
bool validPuckPos(const Vec2D &pos)
{
if(pos.x < rinkLeft + puckRad || pos.x > rinkRight - puckRad)return false;
if(pos.y < rinkTop + puckRad || pos.y > rinkBottom - puckRad)return false;
return !checkGoalie(pos, puckRad);
}
struct EnemyMap
{
vector<double> danger;
vector<int> stick[MAX_ACTIVE];
void init();
void reset()
{
for(int i = 0; i < activeCount; i++)
{
stick[i].resize(gridSize);
for(auto &flag : stick[i])flag = maxLookahead;
}
}
void addMapPoint(const HockeyistInfo &info, HockeyistState &state, int time, int flags, double turnTime)
{
auto &cell = stick[info.index][gridPos(state.pos + effStickLength * state.dir)];
cell = min<int>(cell, max(time, info.cooldown));
}
void closePath(const HockeyistInfo &info, HockeyistState &state, int flags, double turnTime)
{
}
void fillMap(const HockeyistInfo &info, const HockeyistState &state)
{
Mapper<EnemyMap, HockeyistState, &EnemyMap::addMapPoint, &EnemyMap::closePath>(*this, info).fillMap(state);
}
static void filterField(vector<int> &field)
{
constexpr int border = 1;
vector<int> old(gridSize); swap(old, field);
int x, y, cell = 0;
for(y = 0; y < border; y++)
for(x = 0; x < gridLine; x++, cell++)
field[cell] = maxLookahead;
for(; y < gridHeight - border; y++)
{
for(x = 0; x < border; x++, cell++)
field[cell] = maxLookahead;
for(; x < gridLine - border; x++, cell++)
{
int val = old[cell];
val = min<int>(val, old[cell - gridLine - 1]);
val = min<int>(val, old[cell - gridLine + 1]);
val = min<int>(val, old[cell + gridLine - 1]);
val = min<int>(val, old[cell + gridLine + 1]);
val = min<int>(val, old[cell - gridLine]);
val = min<int>(val, old[cell + gridLine]);
val = min<int>(val, old[cell - 1]);
val = min<int>(val, old[cell + 1]);
field[cell] = val;
}
for(; x < gridLine; x++, cell++)
field[cell] = maxLookahead;
}
for(; y < gridHeight; y++)
for(x = 0; x < gridLine; x++, cell++)
field[cell] = maxLookahead;
assert(cell == gridSize);
}
void filter()
{
for(int i = 0; i < activeCount; i++)filterField(stick[i]);
}
double getDanger(const Vec2D &pos)
{
Vec2D offs = (pos - rinkCenter) / cellSize + Vec2D(gridHalfWidth, gridHalfHeight);
int x = int(offs.x), y = int(offs.y), cell = y * gridLine + x; offs -= Vec2D(x, y);
double val1 = danger[cell] + (danger[cell + 1] - danger[cell]) * offs.x; cell += gridLine;
double val2 = danger[cell] + (danger[cell + 1] - danger[cell]) * offs.x;
return val1 + (val2 - val1) * offs.y;
}
void updateSafety(Safety &res, const Vec2D &puck, double spd, int time);
void updateSafety(Safety &res, const HockeyistInfo &info, const HockeyistState &state, int time, bool havePuck);
int enemyTime(int cell) const
{
int res = maxLookahead;
for(int i = 0; i < activeCount; i++)res = min(res, stick[i][cell]);
return res;
}
};
struct TargetPlace
{
Vec2D pos, view;
};
struct PassTarget;
struct EnemyInfo;
struct AllyInfo;
EnemyMap enemyMap;
double currentDanger;
vector<SubstituteInfo> substitutes;
TargetPlace attackPlace, defencePlace;
vector<PassTarget> targets, nextTargets;
PathPoint puckPath[maxLookahead + 1];
int puckPathLen, goalFlag;
EnemyInfo *enemyPuck;
AllyInfo *allyPuck;
template<bool ally> struct StateScore : public HockeyistState
{
double timeFactor;
StateScore(const HockeyistState &state) : HockeyistState(state), timeFactor(1)
{
}
void nextStep(const HockeyistInfo &info, double accel, const Vec2D &turn, int time)
{
HockeyistState::nextStep(info, accel, turn, time); timeFactor *= timeGamma;
}
Safety intact() const
{
return 1;
}
};
template<> struct StateScore<true> : public HockeyistState
{
double timeFactor;
Safety safety;
StateScore(const HockeyistState &state) : HockeyistState(state), timeFactor(1), safety(1)
{
}
void nextStep(const HockeyistInfo &info, double accel, const Vec2D &turn, int time)
{
enemyMap.updateSafety(safety, info, *this, time - 1, !puckPathLen);
HockeyistState::nextStep(info, accel, turn, time); timeFactor *= timeGamma;
}
const Safety &intact() const
{
return safety;
}
};
struct Sector
{
Vec2D dir;
double span, time;
Sector(const Vec2D &dir_ = {0, 0}, double span_ = 0, double time_ = 0) :
dir(dir_), span(span_), time(time_)
{
}
};
struct GoalHelper
{
Vec2D start, bonus, dir;
double rad, power, dist, offs, end, duration;
GoalHelper(const Vec2D &pos, const Vec2D &spd, double power_, bool right) :
start(pos), bonus(spd), rad(hockeyistRad + puckRad), power(power_)
{
if(right)
{
start.x = 2 * rinkCenter.x - start.x; bonus.x = -bonus.x;
}
if(pos.y < goalCenter)
{
start.y = 2 * goalCenter - start.y; bonus.y = -bonus.y;
}
}
bool init()
{
start -= Vec2D(rinkLeft, goalCenter - goalHalf);
dist = start.x - hockeyistRad; if(!(dist > rad))return false;
offs = max(0.0, start.y - goalHalf - goalieRange);
dir = start = normalize(start); return true;
}
void withoutGoalie()
{
start -= Vec2D(rinkLeft, goalCenter - goalHalf);
dir = normalize(Vec2D(start.x, start.y - goalHalf - goalieRange));
double len = start.len(); start /= len;
double end = len / (power - bonus * start);
duration = -log(1 - puckBeta * end) / puckBeta;
}
template<bool first> bool iterate()
{
double invSpd = 1 / (power - bonus * dir), cmp = 0;
end = (dist - rad * dir.y) * invSpd;
if(first)
{
cmp = dist * dir.y - rad; if(!(goalieSpd * end < cmp))return false;
}
double mul = 1 / dir.x, beg = offs * invSpd / dir.y; end *= mul;
if(first)
{
cmp *= mul; if(!(goalieSpd * (end - beg) + offs < cmp))return false;
}
beg = -log(1 - puckBeta * beg) / puckBeta;
end = -log(1 - puckBeta * end) / puckBeta;
double len = goalieSpd * (end - beg) + offs; if(first && !(len < cmp))return false;
double w = dist * dist + len * len, z = sqrt(w - rad * rad);
dir = Vec2D(dist * z - len * rad, len * z + dist * rad) / w;
if(first)duration = end; return true;
}
Sector result(const Vec2D &pos, bool right) const
{
double dot = dir * start, cross = dir % start;
double norm = 1 / sqrt(2 + 2 * dot); Vec2D res = dir + start;
if(!right)res.x = -res.x; if(!(pos.y < goalCenter))res.y = -res.y;
return Sector(res * norm, cross * norm, duration);
}
};
Sector estimateGoalAngle(const Vec2D &pos, const Vec2D &spd, double power, bool right)
{
GoalHelper helper(pos, spd, power, right);
if(goalieTime > 0)
{
if(!helper.init())return Sector();
if(!helper.iterate<true>())return Sector();
for(int i = 0; i < 3; i++)helper.iterate<false>();
}
else helper.withoutGoalie(); return helper.result(pos, right);
}
void interception(Safety &res, const Vec2D &pos, const Vec2D &spd, int time, int duration)
{
SimplePuckState puck(pos, spd);
for(int i = 0; i < duration; i++)
{
enemyMap.updateSafety(res, puck.pos, puck.spdLen, time + i); if(!puck.nextStep())break;
}
}
inline bool inStrikeSector(const HockeyistState &state, const Vec2D &puck)
{
Vec2D delta = puck - state.pos;
if(!(delta.sqr() < sqr(stickLength)))return false;
double dot = delta * state.dir, cross = delta % state.dir;
return abs(cross) < dot * stickSectorTan;
}
inline double probFunc(double val)
{
constexpr double eps = 1e-3;
double val1 = erf(val), val2 = val / (1 + abs(val));
return (val1 + eps * (val2 - val1)) / 2;
}