-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathActualStrategy.cs
1693 lines (1527 loc) · 41.6 KB
/
ActualStrategy.cs
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
using System;
using System.Collections.Generic;
using Com.CodeGame.CodeTanks2012.DevKit.CSharpCgdk.Model;
using System.IO;
using System.Linq;
abstract class ActualStrategy
{
protected const double inf = 1e20;
protected const int medikitVal = 35;
protected const int repairVal = 50;
public const double regularBulletFriction = 0.995;
public const double premiumBulletFriction = 0.99;
public const double regularBulletStartSpeed = 16.500438538620 / regularBulletFriction;
public const double premiumBulletStartSpeed = 13.068000645325 / premiumBulletFriction;
const double premiumShotDistance = 850;
protected const double ricochetAngle = Math.PI / 3;
protected const string myName = "Hohol";
protected readonly double diagonalLen = Math.Sqrt(1280 * 1280 + 800 * 800);
protected const double stayPerpendicularDistance = 800 * 3/4;
public const double bulletWidth = 22.5;
public const double bulletHeight = 7.5;
const int dangerFactorTestTickCnt = 100;
const double targetDist = 30;
const int stuckDetectTickCnt = 100;
bool stuckEscapeForward;
const double stuckDist = 10;
const int stuckAvoidTime = 35;
protected int runToCornerTime = 300;
protected MoveType prevMove;
//int dummy;
public double[] historyX = new double[10000];
public double[] historyY = new double[10000];
int stuckDetectedTick = -1;
protected double cornerX, cornerY;
protected double targetX, targetY;
public static List<MoveType> moveTypes = new List<MoveType>();
protected Tank self;
static protected World world;
protected Move move;
protected List<Tank> teammates, enemies;
#if TEDDY_BEARS
public static StreamWriter file;//, teorFile, realFile;
#endif
public abstract void Move(Tank self, World world, Move move);
public void CommonMove(Tank self, World world, Move move)
{
this.self = self;
ActualStrategy.world = world;
this.move = move;
enemies = new List<Tank>();
foreach (Tank tank in world.Tanks)
if (!IsDead(tank) && !tank.IsTeammate)
enemies.Add(tank);
historyX[world.Tick] = self.X;
historyY[world.Tick] = self.Y;
teammates = new List<Tank>();
foreach (Tank tank in world.Tanks)
if (tank.IsTeammate && tank.Id != self.Id)
teammates.Add(tank);
/////////////////
Move(self, world, move);
/////////////////
ManageStuck();
AvoidBullets();
prevMove = new MoveType(move.LeftTrackPower, move.RightTrackPower);
}
bool TeammateNeedsMore(Bonus bonus, Tank teammate)
{
if (bonus.Type == BonusType.AmmoCrate)
{
if (self.PremiumShellCount != teammate.PremiumShellCount)
return self.PremiumShellCount > teammate.PremiumShellCount;
}
else if (bonus.Type == BonusType.Medikit)
{
if (self.CrewHealth != teammate.CrewHealth)
return self.CrewHealth > teammate.CrewHealth;
}
else
{
if (self.HullDurability != teammate.HullDurability)
return self.HullDurability > teammate.HullDurability;
}
return teammate.GetDistanceTo(bonus) < self.GetDistanceTo(bonus);
}
protected Bonus GetBonus(out bool forward)
{
var forbidden = new HashSet<long>();
foreach (Tank tm in teammates)
{
if (IsDead(tm))
continue;
bool dummy;
Bonus b = GetBonus(tm, out dummy, null);
if (b != null && TeammateNeedsMore(b, tm))
forbidden.Add(b.Id);
}
return GetBonus(self, out forward, forbidden);
}
static protected int TeamSize(Tank tank)
{
int r = 0;
foreach (Tank t in world.Tanks)
if (tank.PlayerName == t.PlayerName)
r++;
return r;
}
static protected bool MoreSweet(Tank a, Tank b)
{
int s1 = TeamSize(a), s2 = TeamSize(b);
if (s1 != s2)
return s1 > s2;
return Math.Min(a.CrewHealth, a.HullDurability) < Math.Min(b.CrewHealth, b.HullDurability);
}
protected void TryShoot(Tank victim, bool shootOnlyToVictim)
{
if (self.RemainingReloadingTime > 0)
return;
int dummy, resTick;
Unit aimPrem = self.PremiumShellCount > 0 ? EmulateShot(true, out dummy) : null;
Unit aimReg = EmulateShot(false, out resTick);
if (BadAim(aimReg, victim, shootOnlyToVictim, ShellType.Regular))
aimReg = null;
if (BadAim(aimPrem, victim, shootOnlyToVictim, ShellType.Premium))
aimPrem = null;
if (aimPrem != null && ((Tank)aimPrem).HullDurability > 20)
move.FireType = FireType.Premium;
else if (aimReg != null)
{
move.FireType = FireType.Regular;
//double angle = GetCollisionAngle((Tank)aimReg, resTick);
//if (double.IsNaN(angle) || angle < ricochetAngle - Math.PI / 10)
}
}
protected bool BadAim(Unit aim, Unit victim, bool shootOnlyToVictim, ShellType bulletType)
{
if (aim == null)
return true;
if (!(aim is Tank))
return true;
Tank tank = (Tank)aim;
if (IsDead(tank) || shootOnlyToVictim && victim != null && tank.Id != victim.Id)
return true;
if (tank.IsTeammate)
return true;
if (self.GetDistanceTo(aim) < self.Width * 3 && bulletType == ShellType.Regular && self.PremiumShellCount > 0)
return true;
if (bulletType == ShellType.Premium)
return CanEscape(tank, bulletType);
if (self.GetDistanceTo(aim) > world.Height)
{
return false;
}
else
{
return CanEscape(tank, bulletType);
}
//return false;
}
protected void MoveToVert(double x, double y)
{
targetX = x;
targetY = y;
double tox = 10 * Math.Cos(self.Angle);
double toy = 10 * Math.Sin(self.Angle);
double a = Math.Atan2(self.Y-y,self.X-x);
if (self.GetDistanceTo(x, y) <= targetDist)
{
if (toy > 0)
TurnTo(self.X, self.Y+1);
else
TurnTo(self.X, self.Y-1);
}
else if (self.GetDistanceTo(x, y) <= 2 * targetDist && (AngleDiff(a, 0) < Math.PI / 4 || AngleDiff(a, Math.PI) < Math.PI / 4))
{
if (toy > 0)
MoveTo(x, y + self.Width, true);
else
MoveTo(x, y + self.Width, false);
}
else
{
bool forward = (tox * (x - self.X) + toy * (y - self.Y) > 0);
MoveTo(x, y, forward);
}
}
protected void MoveToHor(double x, double y)
{
targetX = x;
targetY = y;
double tox = 10 * Math.Cos(self.Angle);
double toy = 10 * Math.Sin(self.Angle);
double a = Math.Atan2(self.Y - y, self.X - x);
if (self.GetDistanceTo(x, y) <= targetDist)
{
if (tox > 0)
TurnTo(self.X+1, self.Y);
else
TurnTo(self.X-1, self.Y);
}
else if (self.GetDistanceTo(x, y) <= 2 * targetDist && (AngleDiff(a, Math.PI/2) < Math.PI / 4 || AngleDiff(a, -Math.PI/2) < Math.PI / 4))
{
if (tox > 0)
MoveTo(x+self.Width, y, true);
else
MoveTo(x + self.Width, y, false);
}
else
{
bool forward = (tox * (x - self.X) + toy * (y - self.Y) > 0);
MoveTo(x, y, forward);
}
}
protected void MoveTo(double x, double y, double dx, double dy)
{
targetX = x;
targetY = y;
double tox = 10 * Math.Cos(self.Angle);
double toy = 10 * Math.Sin(self.Angle);
if (self.GetDistanceTo(x, y) <= self.Height / 2)
{
if (tox * dx + toy * dy > 0)
TurnTo(self.X + dx, self.Y + dy);
else
TurnTo(self.X - dx, self.Y - dy);
}
else
{
bool forward = (tox * (x - self.X) + toy * (y - self.Y) > 0);
MoveTo(x, y, forward);
}
}/**/
/*protected void MoveTo(double x, double y, double dx, double dy)
{
targetX = x;
targetY = y;
if (self.GetDistanceTo(x, y) <= self.Height / 2)
TurnTo(self.X+dx,self.Y+dy);
else
{
Point o = new Point(x, y);
Point me = new Point(self.X, self.Y);
Point d = new Point(dx, dy);
Point oMe = o - me;
if (Point.scalar(oMe, d) > 0)
{
MoveTo(x, y, true);
}
else
MoveTo(x, y, false);
}
}*/
protected int AliveTeammateCnt()
{
return world.Tanks.Count(tank => tank.IsTeammate && !IsDead(tank));
}
protected void StayPerpendicular(Tank tank)
{
if (self.GetDistanceTo(tank) < stayPerpendicularDistance)
return;
if (Math.Abs(tank.GetTurretAngleTo(self)) > Math.PI / 5)
return;
//if (tank.RemainingReloadingTime > self.ReloadingTime / 2)
// return;
if (ObstacleBetween(self,tank,false))
return;
double angle = self.GetAngleTo(tank);
const double allowedAngle = Math.PI / 4;
if (angle > 0)
{
if (angle < Math.PI / 2 - allowedAngle)
RotateCCW();
else if (angle > Math.PI + allowedAngle)
RotateCW();
}
else
{
if (angle > -Math.PI / 2 + allowedAngle)
RotateCW();
else if (angle < -Math.PI / 2 - allowedAngle)
RotateCCW();
}
}
void RotateCW()
{
move.LeftTrackPower = self.EngineRearPowerFactor;
move.RightTrackPower = -1;
}
void RotateCCW()
{
move.LeftTrackPower = -1;
move.RightTrackPower = self.EngineRearPowerFactor;
}
protected Tank GetWeakest()
{
double mi = inf;
Tank res = null;
foreach (var tank in world.Tanks)
{
if (tank.IsTeammate || IsDead(tank))
continue;
double test;
if (ObstacleBetween(self, tank, true))
test = inf / 2;
else
test = Math.Min(tank.CrewHealth, tank.HullDurability);
//test = Math.Min(Math.Abs(tank.GetAngleTo(self)), angleDiff(tank.GetAngleTo(self), Math.PI));
if (test < mi)
{
mi = test;
res = tank;
}
}
return res;
}
protected Tank PickEnemy()
{
return world.Tanks.FirstOrDefault(tank => !IsDead(tank) && !tank.IsTeammate);
}
protected void RotateForSafety()
{
if (cornerX == -1 || self.GetDistanceTo(cornerX, cornerY) > self.Width)
return;
Tank tank = GetMostAngryEnemy();
if (tank == null)
return;
if (cornerX < world.Width / 2 && cornerY < world.Height / 2)
{
if (tank.X < tank.Y)
TurnTo(world.Width, self.Y);
else
TurnTo(self.X, world.Height);
}
else if (cornerX < world.Width / 2 && cornerY > world.Height / 2)
{
if (tank.X + tank.Y < world.Height)
TurnTo(world.Width, self.Y);
else
TurnTo(self.X, 0);
}
else if (cornerX > world.Width / 2 && cornerY < world.Height / 2)
{
if (tank.X + tank.Y < world.Width)
TurnTo(self.X, world.Height);
else
TurnTo(0, self.Y);
}
else
{
if (tank.X < tank.Y + world.Width - world.Height)
TurnTo(self.X, 0);
else
TurnTo(0, self.Y);
}
}
protected Tank GetMostAngryEnemy()
{
double mi = inf;
Tank r = null;
foreach (var tank in world.Tanks)
{
if (tank.IsTeammate || IsDead(tank) || self.GetDistanceTo(tank) <= stayPerpendicularDistance)
continue;
if (ObstacleBetween(self, tank, false))
continue;
double test = Math.Abs(tank.GetTurretAngleTo(self));
if (test < mi)
{
mi = test;
r = tank;
}
}
return r;
}
protected static bool IsMovingBackward(Unit unit)
{
double tx = Math.Cos(unit.Angle);
double ty = Math.Sin(unit.Angle);
return tx * unit.SpeedX + ty * unit.SpeedY < 0;
}
protected bool CanEscape(Tank tank, ShellType bulletType)
{
double bulletSpeed;
if (bulletType == ShellType.Premium)
bulletSpeed = premiumBulletStartSpeed;
else
bulletSpeed = regularBulletStartSpeed;
double angle = self.Angle + self.TurretRelativeAngle;
double cosa = Math.Cos(angle);
double sina = Math.Sin(angle);
double bulletX = self.X + self.VirtualGunLength * cosa;
double bulletY = self.Y + self.VirtualGunLength * sina;
double bulletSpeedX = bulletSpeed * cosa;
double bulletSpeedY = bulletSpeed * sina;
Point[] bounds = GetBounds(bulletX, bulletY, angle, bulletWidth, bulletHeight);
var ar = new List<MoveType>
{
new MoveType(1, 1),
new MoveType(-1, -1),
new MoveType(-1, 0.75),
new MoveType(0.75, -1)
};
foreach (var moveType in ar)
{
bool can = true;
foreach (var p in bounds)
{
if (DangerFactor(tank, p.x, p.y, bulletSpeedX, bulletSpeedY, bulletType, moveType,true) == inf)
{
can = false;
break;
}
}
if (can)
return true;
}
return false;
}
static double DangerFactor(Tank self, double bulletX, double bulletY, double bulletSpeedX, double bulletSpeedY,
ShellType bulletType, MoveType moveType, bool shouldTestCollision)
{
const double maxDanger = 20;
double friction;
if (bulletType == ShellType.Regular)
friction = regularBulletFriction;
else
friction = premiumBulletFriction;
double startX = bulletX, startY = bulletY;
var me = new MutableTank(self);
double minDist = inf;
for (int tick = 0; tick < dangerFactorTestTickCnt; tick++)
{
bulletSpeedX *= friction;
bulletSpeedY *= friction;
bulletX += bulletSpeedX;
bulletY += bulletSpeedY;
me.Move(moveType, world);
double precision = 0;
if (!self.IsTeammate)
precision = -1;
double anglePrecision = Math.PI/10;
if (!self.IsTeammate)
anglePrecision *= -1;
if (Inside(me, bulletX, bulletY, precision))
{
double collisionAngle = GetCollisionAngle(me, bulletX, bulletY, startX, startY);
if (bulletType == ShellType.Premium || double.IsNaN(collisionAngle)
|| collisionAngle < ricochetAngle + anglePrecision)
return inf;
else
return maxDanger - 5;
}
if (shouldTestCollision)
{
if (self.IsTeammate)
{
if(TestCollision(bulletX, bulletY, tick, -10, -7, -10, self) != null)
return Math.Max(0, maxDanger - minDist);
}
else
{
if (TestCollision(bulletX, bulletY, tick, -10, 1, -10, self) != null)
return Math.Max(0, maxDanger - minDist);
}
}
foreach (var p in me.GetBounds())
{
minDist = Math.Min(minDist,Point.Dist(p.x,p.y,bulletX,bulletY));
}
}
return Math.Max(0, maxDanger - minDist);
}
static DangerType DangerFactor2(Tank self, List<Shell> bullets, MoveType moveType, bool shouldTestCollision,
out List<Shell> dangerBullets)
{
dangerBullets = new List<Shell>();
DangerType res = new DangerType();
foreach (var bullet in bullets)
{
Point[] bounds = GetBounds(bullet);
double ma = 0;
foreach (var p in bounds)
{
double test = DangerFactor(self, p.x, p.y, bullet.SpeedX, bullet.SpeedY, bullet.Type, moveType, shouldTestCollision);
ma = Math.Max(ma, test);
}
if (ma == inf)
{
if (bullet.Type == ShellType.Premium)
res.Cnt += 2;
else
res.Cnt++;
dangerBullets.Add(bullet);
}
res.dangerFactor = Math.Max(res.dangerFactor, ma);
}
return res;
}
/*public class BulletComparer : IComparer<Shell>
{
Tank tank;
public BulletComparer(Tank tank)
{
this.tank = tank;
}
public int Compare(Shell a, Shell b)
{
if (a.Type != b.Type)
{
if (a.Type == ShellType.Premium)
return -1;
else
return 1;
}
if (tank.GetDistanceTo(a) < tank.GetDistanceTo(b))
return -1;
return 1;
}
}*/
class DangerType
{
public int Cnt;
public double dangerFactor;
public static bool operator <(DangerType a, DangerType b)
{
if (a.Cnt != b.Cnt)
return a.Cnt < b.Cnt;
return a.dangerFactor < b.dangerFactor;
}
public static bool operator >(DangerType a, DangerType b)
{
if (a.Cnt != b.Cnt)
return a.Cnt > b.Cnt;
return a.dangerFactor > b.dangerFactor;
}
}
bool MayBeDanger(Shell bullet)
{
return self.GetDistanceTo(bullet) < 2 * self.Width ||
bullet.SpeedX * (self.X - bullet.X) + bullet.SpeedY * (self.Y - bullet.Y) > 0;
}
protected void AvoidBullets()
{
var bullets = new List<Shell>();
foreach (var bullet in world.Shells)
if (MayBeDanger(bullet))
bullets.Add(bullet);
//bullets.Sort(new BulletComparer(self));
List<MoveType> curMoves;// = new List<MoveType>();
curMoves = new List<MoveType>(moveTypes);
if(prevMove != null)
curMoves.Add(prevMove);
curMoves = curMoves.OrderBy(
m => Math.Abs(m.LeftTrackPower - move.LeftTrackPower)
+ Math.Abs(m.RightTrackPower - move.RightTrackPower)).ToList();/**/
for (int i = 0; i < dangerFactorTestTickCnt - 1; i += dangerFactorTestTickCnt/10)
{
curMoves.Add(new MoveType2(-1, 0.75, i, 1, 1));
curMoves.Add(new MoveType2(-1, 0.75, i, -1, -1));
curMoves.Add(new MoveType2(0.75, -1, i, 1, 1));
curMoves.Add(new MoveType2(0.75, -1, i, -1, -1));
}/**/
var bestMove = new MoveType(move.LeftTrackPower,move.RightTrackPower);
//Shell dangerBullet = null;
List<Shell> dangerBullets;
DangerType danger = DangerFactor2(self, bullets, bestMove, true, out dangerBullets);
if(danger.dangerFactor > 0)
{
foreach(var curMove in curMoves)
{
DangerType test = DangerFactor2(self, bullets, curMove, false, out dangerBullets);
if(test < danger)
{
danger = test;
bestMove = curMove;
}
if (danger.dangerFactor == 0)
break;
}
if (danger.Cnt > 0 && self.RemainingReloadingTime == 0)
{
DangerFactor2(self, bullets, bestMove, false, out dangerBullets);
foreach(var bullet in dangerBullets)
{
if (ShootSaves(bullet))
{
move.FireType = FireType.Regular;
break;
}
}
}
}
move.LeftTrackPower = bestMove.LeftTrackPower;
move.RightTrackPower = bestMove.RightTrackPower;
}
private bool ShootSaves(Shell bullet)
{
MutableBullet a = new MutableBullet(bullet);
MutableBullet b = new MutableBullet(self, ShellType.Regular);
for (int i = 0; i < 100; i++)
{
if (Collide(a, b, 0))
return true;
a.Move();
b.Move();
foreach (var p in b.GetBounds())
if (TestCollision(p.x, p.y, 0,1,1,1,null) != null)
return false;
}
return false;
}
bool Piece()
{
return Math.Abs(self.SpeedX) < 1e-5 && Math.Abs(self.SpeedY) < 1e-5
&& Math.Abs(self.AngularSpeed) < 1e-5;
}
#if TEDDY_BEARS
int experimentTick;
bool experimentStarted;
protected void Experiment()
{
if (!experimentStarted)
{
if (self.GetDistanceTo(world.Width / 2, world.Height / 2) > self.Width)
{
MoveTo(world.Width / 2, world.Height / 2, true);
return;
}
/*if (Math.Abs(self.GetAngleTo(world.Width/2,world.Height/2)) > 1e-1)
{
TurnTo(world.Width / 2, world.Height / 2);
return;
}*/
/*if (Math.Abs(self.TurretRelativeAngle-Math.PI/2) > 1e-4)
{
move.TurretTurn = -(self.TurretRelativeAngle-Math.PI/2);
return;
}*/
experimentStarted = Piece();
}
//const int rotateTickCnt = 300;
if (experimentStarted)
{
move.LeftTrackPower = -0.66;
move.RightTrackPower = 0.256;
//if (experimentTick == 0)
// file.WriteLine(move.LeftTrackPower + " " + move.RightTrackPower + " " + self.CrewHealth);
//file.WriteLine(self.Angle + " " + self.SpeedX + " " + self.SpeedY + " " + self.X + " " + self.Y);
experimentTick++;
}
}
#endif
static protected bool BonusSaves(Tank self, Bonus bonus)
{
return bonus != null &&
(bonus.Type == BonusType.RepairKit && self.HullDurability <= 40 || bonus.Type == BonusType.Medikit && self.CrewHealth <= 40);
}
public static Point[] GetBounds(Unit unit)
{
return GetBounds(new MutableUnit(unit));
}
public static Point[] GetBounds(MutableUnit unit)
{
double tx = unit.X;
double ty = unit.Y;
double alpha = unit.Angle;
return GetBounds(tx, ty, alpha, unit.Width, unit.Height);
}
static Point[] GetBounds(double tx, double ty, double alpha, double width, double height)
{
double beta = Math.Atan2(height / 2, width / 2);
double D = Math.Sqrt(Util.Sqr(height / 2) + Util.Sqr(width / 2));
Point t = new Point(tx, ty);
Point a = new Point(D * Math.Cos(alpha + beta), D * Math.Sin(alpha + beta));
Point b = new Point(D * Math.Cos(alpha - beta), D * Math.Sin(alpha - beta));
Point c = new Point(D * Math.Cos(alpha - Math.PI + beta), D * Math.Sin(alpha - Math.PI + beta));
Point d = new Point(D * Math.Cos(alpha + Math.PI - beta), D * Math.Sin(alpha + Math.PI - beta));
a = a + t;
b = b + t;
c = c + t;
d = d + t;
Point[] res = new Point[4];
res[0] = a; // front right
res[1] = b; // front left
res[2] = c; // back left
res[3] = d; // back right
return res;
}
/*protected double GetCollisionAngle(Tank tank, int resTick) //always regular bullet
{
//double bulletSpeed = regularBulletStartSpeed;
double angle = self.Angle + self.TurretRelativeAngle;
double cosa = Math.Cos(angle);
double sina = Math.Sin(angle);
double bulletX = self.X + self.VirtualGunLength * cosa;
double bulletY = self.Y + self.VirtualGunLength * sina;
double startX = bulletX;
double startY = bulletY;
double dx = regularBulletStartSpeed * cosa;
double dy = regularBulletStartSpeed * sina;
for (int tick = 0; tick < resTick; tick++)
{
dx *= regularBulletFriction;
dy *= regularBulletFriction;
bulletX += dx;
bulletY += dy;
//bulletSpeed *= regularBulletFriction;
}
return GetCollisionAngle(new MutableUnit(tank), bulletX, bulletY, startX, startY);
}*/
static protected double GetCollisionAngle(MutableUnit tank, double bulletX, double bulletY, double startX, double startY)
{
double dx = bulletX - startX;
double dy = bulletY - startY;
double dd = Point.Dist(0, 0, dx, dy);
dx /= dd;
dy /= dd;
bool found = false;
for (int i = 0; i < 50; i++)
{
if (Inside(tank, bulletX, bulletY, -1))
{
found = true;
break;
}
bulletX += dx;
bulletY += dy;
}
if (!found)
return double.NaN;
Point bullet = new Point(bulletX, bulletY);
Point[] ar = tank.GetBounds();
Point a = ar[0], b = ar[1], c = ar[2], d = ar[3];
Point me = new Point(startX, startY);
double va = Math.Atan2(me.y - bullet.y, me.x - bullet.x);
if (Point.Intersect(a, b, bullet, me))
return AngleDiff(va, tank.Angle);
if (Point.Intersect(b, c, bullet, me))
return AngleDiff(va, tank.Angle - Math.PI / 2);
if (Point.Intersect(c, d, bullet, me))
return AngleDiff(va, tank.Angle - Math.PI);
if (Point.Intersect(d, a, bullet, me))
return AngleDiff(va, tank.Angle + Math.PI / 2);
//throw new Exception();
return double.NaN;
}
protected static double AngleDiff(double a, double b)
{
while (a < b - Math.PI)
a += 2 * Math.PI;
while (b < a - Math.PI)
b += 2 * Math.PI;
return Math.Abs(a - b);
}
bool WillUsePremiumBullet(Tank tank) //бред...
{
return self.PremiumShellCount > 0 && self.GetDistanceTo(tank) <= premiumShotDistance;
}
protected Tank GetNearest(double x, double y)
{
Tank res = null;
double mi = inf;
foreach (Tank tank in world.Tanks)
{
if (tank.IsTeammate || IsDead(tank))
continue;
double dist = tank.GetDistanceTo(x, y);
if (ObstacleBetween(self,tank,true))
{
dist = inf/2;
}
if (dist < mi)
{
mi = dist;
res = tank;
}
}
return res;
}
protected bool HaveTimeToTurn(Unit unit)
{
return self.RemainingReloadingTime >= TimeToTurn(self, unit) - 5;
}
protected void TurnToMovingTank(Tank tank, bool mustRotateTrucks)
{
double t = self.GetDistanceTo(tank) / (WillUsePremiumBullet(tank) ? premiumBulletStartSpeed : regularBulletStartSpeed);
double victimX = tank.X + tank.SpeedX * t;
double victimY = tank.Y + tank.SpeedY * t;
double angleDiff = self.GetTurretAngleTo(victimX, victimY);
//double allowedAngle = Math.Atan2(tank.Height, self.GetDistanceTo(tank))/4;
if (angleDiff > 0)
{
move.TurretTurn = inf;
if (mustRotateTrucks)
{
move.LeftTrackPower = 1;
move.RightTrackPower = -1;
}
}
else
{
move.TurretTurn = -inf;
if (mustRotateTrucks)
{
move.LeftTrackPower = -1;
move.RightTrackPower = 1;
}
}
}
bool AlmostDead(Tank tank, int damage)
{
if (tank.HullDurability <= damage)
return true;
if (tank.CrewHealth <= damage)
return self.PremiumShellCount > 0 || /*Math.Abs(tank.GetAngleTo(self)) > Math.PI / 2 ||*/
self.GetDistanceTo(tank) < world.Height / 2;
return false;
}
protected Tank GetAlmostDead()
{
int damage = (self.PremiumShellCount > 0 ? 35 : 20);
Tank res = null;
double mi = inf;
foreach (Tank tank in world.Tanks)
{
if (tank.IsTeammate || IsDead(tank))
continue;
if (!AlmostDead(tank, damage))
continue;
if (self.GetDistanceTo(tank) > world.Height)
continue;
if (ObstacleBetween(self, tank, true))
continue;
double angle = Math.Abs(self.GetTurretAngleTo(tank));
if (angle < mi)
{
mi = angle;
res = tank;
}
}
if (mi < Math.PI / 2)
return res;
return null;
}
bool Collide(Unit a, Unit b, double precision)
{
return Collide(new MutableUnit(a), new MutableUnit(b), precision);
}
bool Collide(MutableUnit a, MutableUnit b, double precision)
{
Point[] aBounds = a.GetBounds();
Point[] bBounds = b.GetBounds();
foreach (var p in aBounds)
if (Inside(b, p.x, p.y, precision))
return true;
foreach (var p in bBounds)
if (Inside(a, p.x, p.y, precision))
return true;
return false;
}
static bool Inside(Unit unit, double x, double y, double precision)
{
double w = unit.Width / 2 + precision;
double h = unit.Height / 2 + precision;
#if TEDDY_BEARS
if (w < h)
throw new Exception();
#endif
if (x < unit.X - w || x > unit.X + w ||
y < unit.Y - w || y > unit.Y + w)
return false;
double d = unit.GetDistanceTo(x, y);
double angle = unit.GetAngleTo(x, y);
x = d * Math.Cos(angle);
y = d * Math.Sin(angle);
return x >= -w && x <= w &&
y >= -h && y <= h;
}
static bool Inside(MutableUnit unit, double x, double y, double precision)
{
double w = unit.Width / 2 + precision;
double h = unit.Height / 2 + precision;
#if TEDDY_BEARS
if (w < h)
throw new Exception();
#endif
if (x < unit.X - w || x > unit.X + w ||
y < unit.Y - w || y > unit.Y + w)
return false;
double d = unit.GetDistanceTo(x, y);
double angle = unit.GetAngleTo(x, y);
x = d * Math.Cos(angle);
y = d * Math.Sin(angle);