-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyStrategy.cpp
More file actions
2137 lines (1876 loc) · 75.5 KB
/
MyStrategy.cpp
File metadata and controls
2137 lines (1876 loc) · 75.5 KB
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"
#define PI 3.14159265358979323846
#define _USE_MATH_DEFINES
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <chrono>
#include <ratio>
#include <tuple>
#include <unordered_map>
#include <queue>
using namespace model;
using namespace std;
using namespace std::chrono;
using std::priority_queue;
#include <iomanip>
#include <unordered_set>
#include <array>
#include <utility>
using std::unordered_map;
using std::unordered_set;
using std::array;
using std::queue;
using std::pair;
using std::tuple;
using std::tie;
using std::string;
#include <fstream>
using std::ofstream;
#include <functional>
#include "tileadaptor.hpp"
// quick and dirty file: variables
const int debug = 0;
const int timeDebug = 0;
const int fileDebug = 0;
int tick = 0;
vector<Building> enemyBuildingsGlobal;
Building homeBase;
const double spacingBuffer = 1;
const double attackRangeThreshold = 0.5;
vector<Bonus> bonuses;
const double lowLifeFactor = 0.63;
const double maxOverallStep = 8.1;
const double stepBackFactor = 0.7;
const double minimumSaveDistance = 92;
bool ignoreEverything = false;
bool bonusTarget = false;
int timeToNextBonusAppearance = 2501;
int activeSkillLevel = 0;
int activeSkills[25] = { 0 };
//vector<int> skillLevels;
high_resolution_clock::time_point allTime;
#define SIZEMAP 100
int scale = 4000. / SIZEMAP;
char map[SIZEMAP][SIZEMAP];
bool mapUpdated = false;
//Instantiating our path adaptor
//passing the map size and a lambda that return false if the tile is a wall
TileAdaptor adaptor({ SIZEMAP, SIZEMAP }, [](const Vectori& vec) {return map[vec.x][vec.y] == ' '; });
//This is a bit of an exageration here for the weight, but it did make my performance test go from 8s to 2s
Pathfinder pathfinder(adaptor, 100.f /*weight*/);
enum TargetType {
_TARGET_UNKNOWN_ = -1,
TARGET_ENEMY_BASE = 0,
TARGET_BONUS = 1,
TARGET_TOWER = 2,
TARGET_WIZARD = 3,
TARGET_MINION = 4,
TARGET_HOME_BASE = 5,
TARGET_DANGER_ZONE = 6,
TARGET_PROJECTILE = 7
};
class Point : public Unit {
public:
Point() : Unit(-1, 0.0, 0.0, 0.0, 0.0, 0.0, _FACTION_UNKNOWN_) { };
Point(double x, double y) : Unit(-1, x, y, 0.0, 0.0, 0.0, _FACTION_UNKNOWN_) { };
Point(double x, double y, double angle) : Unit(-1, x, y, 0.0, 0.0, angle, _FACTION_UNKNOWN_) { };
Point(const Unit& unit) : Unit(unit) { };
double getAbsoluteAngleTo(double x, double y) const {
return atan2(y - this->getY(), x - this->getX());
}
double getAbsoluteAngleTo(const Unit& unit) const {
return getAbsoluteAngleTo(unit.getX(), unit.getY());
}
double getAngleGrad() const {
return (this->getAngle() / PI * 180);
}
};
class AStarPoint : public CircularUnit {
public:
AStarPoint() : CircularUnit(-1, 0.0, 0.0, 0.0, 0.0, 0.0, _FACTION_UNKNOWN_, 0) { };
AStarPoint(const Point& p, double radius) : CircularUnit(p.getId(), p.getX(), p.getY(), p.getSpeedX(), p.getSpeedY(), p.getAngle(), p.getFaction(), radius) { };
AStarPoint(const CircularUnit& unit) : CircularUnit(unit) { };
bool operator==(const AStarPoint& other) const {
int x1 = (int)(this->getX() * 1000);
int y1 = (int)(this->getY() * 1000);
int x2 = (int)(other.getX() * 1000);
int y2 = (int)(other.getY() * 1000);
return (x1 == x2 && y1 == y2);
}
bool operator==(const Point& other) const {
int x1 = (int)(this->getX() * 1000);
int y1 = (int)(this->getY() * 1000);
int x2 = (int)(other.getX() * 1000);
int y2 = (int)(other.getY() * 1000);
return (x1 == x2 && y1 == y2);
}
bool operator<(const AStarPoint& other) const {
return ((int)(this->getX()*1000)<(int)(other.getX() * 1000) || (!((int)(other.getX() * 1000)<(int)(this->getX() * 1000)) && (int)(this->getY() * 1000)<(int)(other.getY() * 1000)));
}
};
class MovePoint : public Point {
private:
double speed;
double strafe;
double angleRelative;
public:
MovePoint() : Point(), speed(-1), strafe(-1) { };
MovePoint(double x, double y, double speed, double strafe, double angle, double dist, double angleRelative) :
Point(x, y, angle), speed(speed), strafe(strafe), angleRelative(angleRelative){ };
double getSpeed() const {
return speed;
}
double getStrafe() const {
return strafe;
}
double getAngleRelative() const {
return angleRelative;
}
};
template<class T>
class UnitDistance {
public:
T unit;
double distanceToSelf;
UnitDistance(const T& unit, double distanceToSelf) : unit(unit), distanceToSelf(distanceToSelf) {
}
bool operator < (const UnitDistance& p) const {
return (this->distanceToSelf < p.distanceToSelf);
}
};
class Action {
public:
TargetType actionTarget;
Point actionPoint;
Wizard actionWizard;
Minion actionMinion;
Building actionBuilding;
bool actionInAttackRange;
bool actionInStaffRange;
string actionDescription;
TargetType moveTarget;
Point movePoint;
string moveDescription;
Action() : actionTarget(_TARGET_UNKNOWN_), actionPoint(), actionInAttackRange(false), actionInStaffRange(false), actionDescription(""), moveTarget(_TARGET_UNKNOWN_), movePoint(), moveDescription("") {};
};
int globalLane = 0;
//vector<vector<Point>> globalPath = {
// { Point(100,3900), Point(50,2693.26), Point(350,1656.75), Point(400,800), Point(4000 - 2312.13,4000 - 3950), Point(4000 - 1370.66,4000 - 3650), Point(3600,400) },
// { Point(100,3900), Point(902.613,2768.1), Point(1929.29,2400), Point(4000 - 1929.29,4000 - 2400), Point(4000 - 902.613,4000 - 2768.1), Point(3600,400) },
// { Point(100,3900), Point(1370.66,3650), Point(2312.13,3950), Point(3200,3600), Point(4000 - 350,4000 - 1656.75), Point(4000 - 50,4000 - 2693.26), Point(3600,400) } };
// TODO Enemy Base final Step
vector<vector<Point>> globalPath = {
{ Point(100,3900), Point(200,2693.26), Point(200,1656.75), Point(200,1200), Point(200,1000), Point(200,800), Point(235,600), Point(360,390), Point(540,260), Point(800,200), Point(4000 - 2312.13,200), Point(4000 - 1370.66,200),Point(3100,200) },
{ Point(100,3900), Point(3200,800) },
{ Point(100,3900), Point(1370.66,3800), Point(2312.13,3800), Point(2800,3800), Point(3000,3800), Point(3200,3800), Point(3400,3765), Point(3610,3640), Point(3740,3460), Point(3800,3200), Point(3800,4000 - 1656.75), Point(3800,4000 - 2693.26), Point(3800,900) } };
TargetType getTargetTypeGlobalPath(int index) {
if (index == 0) return TARGET_HOME_BASE;
if (index == 1 || index == 2 || index == 3 || index == 4) return TARGET_TOWER;
if (index == 5) return TARGET_ENEMY_BASE;
}
int nextGlobalPathIndex(const Wizard& self, int lane) {
int lastWaypointIndex = globalPath[lane].size() - 1;
Point lastWaypoint = globalPath[lane][lastWaypointIndex];
for (int waypointIndex = 0; waypointIndex < lastWaypointIndex; ++waypointIndex) {
Point waypoint = globalPath[lane][waypointIndex];
if (waypoint.getDistanceTo(self) <= 150) {
return (waypointIndex + 1);
}
if (lastWaypoint.getDistanceTo(waypoint) < lastWaypoint.getDistanceTo(self)) {
return waypointIndex;
}
}
return lastWaypointIndex;
}
int previousGlobalPathIndex(const Wizard& self, int lane) {
Point firstWaypoint = globalPath[lane][0];
for (int waypointIndex = globalPath[lane].size() - 1; waypointIndex > 0; --waypointIndex) {
Point waypoint = globalPath[lane][waypointIndex];
if (waypoint.getDistanceTo(self) <= 150) {
return waypointIndex - 1;
}
if (firstWaypoint.getDistanceTo(waypoint) < firstWaypoint.getDistanceTo(self)) {
return waypointIndex;
}
}
return 0;
}
void checkGlobalLane(const Wizard& self, const World& world) {
if (self.getId() == 3 || self.getId() == 8) {
int wizardsPerLane[3] = { 0,0,0 };
auto getLane = [](int angle) {
if (angle > 165 / 180 * PI) {
return 0;
} else if (angle < 105 / 180 * PI) {
return 2;
} else {
return 1;
}
};
for (const auto& w : world.getWizards()) {
if (w.getFaction() != self.getFaction()) {
wizardsPerLane[getLane(w.getAngle())] += 1;
}
}
if (wizardsPerLane[1] > 3) {
globalLane = 1;
} else {
globalLane = 0;
}
}
}
void setGlobalLane(const Wizard& self, const World& world) {
if (self.getId() == 1 || self.getId() == 6)
globalLane = 0;
else if (self.getId() == 2 || self.getId() == 7)
globalLane = 1;
else if (self.getId() == 3 || self.getId() == 8)
globalLane = 1;
else if (self.getId() == 4 || self.getId() == 9)
globalLane = 1;
else if (self.getId() == 5 || self.getId() == 10)
globalLane = 2;
else
globalLane = 1;
return;
}
// helper functions
//-----------------
// angle in game sense, clock wise
Point getPoint(double x, double y, double angle, double distance) {
double xp = x + distance * cos(-angle);
double yp = y - distance * sin(-angle);
xp = max(min(xp, 3960.), 40.);
yp = max(min(yp, 3960.), 40.);
return Point(xp, yp);
}
Point getPoint(const Unit& unit, double angle, double distance) {
return getPoint(unit.getX(), unit.getY(), angle, distance);
}
Point getPoint(const Unit& unit, double distance) {
return getPoint(unit.getX(), unit.getY(), unit.getAngle(), distance);
}
// geometry
bool isLineWithinCircle(const Unit& a, const Unit& center, double radius) {
double d = a.getDistanceTo(center);
if (d <= radius) {
return true;
}
return false;
}
bool isLineCircleIntersectionPoint(const Unit& a, const Unit& b, const Unit& center, double radius) {
// http://math.stackexchange.com/questions/228841/how-do-i-calculate-the-intersections-of-a-straight-line-and-a-circle
double m = (b.getY() - a.getY()) / (b.getX() - a.getX());
double c = a.getY() - m*a.getX();
double p = center.getX();
double q = center.getY();
double A = m*m + 1;
double B = 2 * (m*c - m*q - p);
double C = q*q - radius*radius + p*p - 2 * c*q + c*c;
double root = (B*B - 4 * A*C);
if (root < 0) {
return false;
}
else if (root == 0) {
double x = -B / (2 * A);
double d = b.getX() - a.getX();
double dx = x - a.getX();
if (d < 0) {
d *= -1;
dx *= -1;
}
if (d >= dx && dx >= 0) {
return true;
}
return false;
}
else {
double x = -B / (2 * A);
double sqrtroot = sqrt(root) / (2 * A);
double x1 = x + sqrtroot;
double x2 = x - sqrtroot;
double d = b.getX() - a.getX();
double dx1 = x1 - a.getX();
double dx2 = x2 - a.getX();
if (d < 0) {
d *= -1;
dx1 *= -1;
dx2 *= -1;
}
if ((d >= dx1 && dx1 >= 0) || (d >= dx2 && dx2 >= 0)) {
return true;
}
return false;
}
}
// output
ostream &operator<<(ostream &stream, const Point& point) {
return stream << "x:" << point.getX() << " y:" << point.getY() << " angle:" << point.getAngleGrad();
}
ostream &operator<<(ostream &stream, const MovePoint& point) {
return stream << "x:" << point.getX() << " y:" << point.getY() << " speed:" << point.getSpeed() << " strafe:" << point.getStrafe() << " angle:" << point.getAngleGrad();
}
ostream &operator<<(ostream &stream, const Action& action) {
return stream << "actionTarget " << action.actionTarget << " actionPoint " << action.actionPoint << " actionDescription " << action.actionDescription << " moveTarget " << action.moveTarget << " movePoint " << action.movePoint << " moveDescription " << action.moveDescription;
}
ostream &operator<<(ostream &stream, const Unit& unit) {
return stream << "id:" << unit.getId() << " x:" << unit.getX() << " y:" << unit.getY() << " angle:" << (unit.getAngle() / PI * 180);
}
ostream &operator<<(ostream &stream, const Move& move) {
return stream << "speed:" << move.getSpeed() << " strafeSpeed:" << move.getStrafeSpeed() << " turn:" << (move.getTurn() / PI * 180) <<
" action:" << move.getAction() << " castAngle:" << move.getCastAngle() << " minCastDist:" << move.getMinCastDistance() << " maxCastDist:" << move.getMaxCastDistance();
}
ostream &operator<<(ostream &stream, const Wizard& self) {
return stream << "id:" << self.getId() << " getX:" << self.getX() << " getY:" << self.getY() << " getTurn:" << (self.getAngle() / PI * 180) << " getSpeedX:" << self.getSpeedX() << " getSpeedY:" << self.getSpeedY();
}
// inits and tick updates
// enemy buildings
void initEnemyBuildings(const Wizard& self, const World& world) {
Faction oppFaction = FACTION_ACADEMY;
if (self.getFaction() == FACTION_ACADEMY) {
oppFaction = FACTION_RENEGADES;
}
for (const auto &building : world.getBuildings()) {
if (building.getFaction() == self.getFaction()) {
Building b = { (long long)0, 4000-building.getX(), 4000-building.getY(), 0, 0, 0, oppFaction,
building.getRadius(), building.getLife(), building.getMaxLife(), vector<Status>(), building.getType(),
building.getVisionRange(), building.getAttackRange(),
-1, -1, 0 };
enemyBuildingsGlobal.push_back(b);
}
}
}
bool inVisionFriendlyArea(const Wizard& self, const World& world, const Building& building) {
bool inVision = false;
for (const auto& w : world.getWizards()) {
if (w.getFaction() == self.getFaction()) {
double dist = w.getDistanceTo(building);
if (dist < w.getVisionRange()) {
inVision = true;
break;
}
}
}
if (!inVision) {
for (const auto& m : world.getMinions()) {
if (m.getFaction() == self.getFaction()) {
double dist = m.getDistanceTo(building);
if (dist < m.getVisionRange()) {
inVision = true;
break;
}
}
}
}
return inVision;
}
void updateEnemyBuildings(const Wizard& self, const World& world) {
// decrease cool down ticks
for (auto& b : enemyBuildingsGlobal) {
b = Building ( b.getId(), b.getX(), b.getY(), 0, 0, 0, b.getFaction(),
b.getRadius(), b.getLife(), b.getMaxLife(), b.getStatuses(), b.getType(),
b.getVisionRange(), b.getAttackRange(),
b.getDamage(), b.getCooldownTicks(), max(b.getRemainingActionCooldownTicks()-1,0) );
}
// update dummy with current data
for (const auto& building : world.getBuildings()) {
if (building.getFaction() != self.getFaction()) {
// find building and erase
unsigned int i = 0;
for (; i < (unsigned int)enemyBuildingsGlobal.size(); i++) {
if ((int)enemyBuildingsGlobal[i].getX() == (int)building.getX() && (int)enemyBuildingsGlobal[i].getY() == (int)building.getY()) {
break;
}
}
enemyBuildingsGlobal.erase(enemyBuildingsGlobal.begin() + i);
// add original
enemyBuildingsGlobal.push_back(building);
}
}
// update remove destroyed one
auto isNotThereAnymore = [&self, &world](const Building& building) {
bool inVision = inVisionFriendlyArea(self, world, building);
if (inVision) {
bool found = false;
for (const auto& b : world.getBuildings()) {
if ((int)b.getX() == (int)building.getX() && (int)b.getY() == (int)building.getY()) {
found = true;
if (found) break;
}
}
return !found;
}
else {
return false;
}
};
enemyBuildingsGlobal.erase(remove_if(enemyBuildingsGlobal.begin(), enemyBuildingsGlobal.end(), isNotThereAnymore), enemyBuildingsGlobal.end());
}
// skills
void initActiveSkills() {
for (int i = 0; i < 25; i++) {
activeSkills[i] = 0;
}
}
void updateActiveSkills(const Wizard& self, const World& world, const Game& game) {
initActiveSkills();
vector<SkillType> mySkills = self.getSkills();
for (const auto& skill : mySkills) {
activeSkills[skill] = 1;
}
for (const auto& wizard : world.getWizards()) {
if (wizard.getFaction() == self.getFaction()) {
double dist = self.getDistanceTo(wizard);
if (dist <= game.getAuraSkillRange()) {
for (const auto& skill : wizard.getSkills()) {
if (skill == SKILL_RANGE_BONUS_AURA_1 || skill == SKILL_RANGE_BONUS_AURA_2 ||
skill == SKILL_MAGICAL_DAMAGE_BONUS_AURA_1 || skill == SKILL_MAGICAL_DAMAGE_BONUS_AURA_2 ||
skill == SKILL_STAFF_DAMAGE_BONUS_AURA_1 || skill == SKILL_STAFF_DAMAGE_BONUS_AURA_2 ||
skill == SKILL_MOVEMENT_BONUS_FACTOR_AURA_1 || skill == SKILL_MOVEMENT_BONUS_FACTOR_AURA_2 ||
skill == SKILL_MAGICAL_DAMAGE_ABSORPTION_AURA_1 || skill == SKILL_MAGICAL_DAMAGE_ABSORPTION_AURA_2) {
activeSkills[skill] = 1;
}
}
}
}
}
}
bool isNextSkillPossible(const Wizard& self) {
int currentLevel = self.getLevel();
if (activeSkillLevel < currentLevel) return true;
return false;
}
SkillType learnNextSkill(const Wizard& self) {
SkillType skillPath[] = {
SKILL_RANGE_BONUS_PASSIVE_1,
SKILL_RANGE_BONUS_AURA_1,
SKILL_RANGE_BONUS_PASSIVE_2,
SKILL_RANGE_BONUS_AURA_2,
SKILL_ADVANCED_MAGIC_MISSILE,
SKILL_MAGICAL_DAMAGE_BONUS_PASSIVE_1,
SKILL_MAGICAL_DAMAGE_BONUS_AURA_1,
SKILL_MAGICAL_DAMAGE_BONUS_PASSIVE_2,
SKILL_MAGICAL_DAMAGE_BONUS_AURA_2,
SKILL_FROST_BOLT,
SKILL_MOVEMENT_BONUS_FACTOR_PASSIVE_1,
SKILL_MOVEMENT_BONUS_FACTOR_AURA_1,
SKILL_MOVEMENT_BONUS_FACTOR_PASSIVE_2,
SKILL_MOVEMENT_BONUS_FACTOR_AURA_2,
SKILL_HASTE,
SKILL_STAFF_DAMAGE_BONUS_PASSIVE_1,
SKILL_STAFF_DAMAGE_BONUS_AURA_1,
SKILL_STAFF_DAMAGE_BONUS_PASSIVE_2,
SKILL_STAFF_DAMAGE_BONUS_AURA_2,
SKILL_FIREBALL,
SKILL_MAGICAL_DAMAGE_ABSORPTION_PASSIVE_1,
SKILL_MAGICAL_DAMAGE_ABSORPTION_AURA_1,
SKILL_MAGICAL_DAMAGE_ABSORPTION_PASSIVE_2,
SKILL_MAGICAL_DAMAGE_ABSORPTION_AURA_2,
SKILL_SHIELD};
//SkillType skillPath[] = {
// SKILL_MAGICAL_DAMAGE_BONUS_PASSIVE_1,
// SKILL_MAGICAL_DAMAGE_BONUS_AURA_1,
// SKILL_MAGICAL_DAMAGE_BONUS_PASSIVE_2,
// SKILL_MAGICAL_DAMAGE_BONUS_AURA_2,
// SKILL_FROST_BOLT,
// SKILL_RANGE_BONUS_PASSIVE_1,
// SKILL_RANGE_BONUS_AURA_1,
// SKILL_RANGE_BONUS_PASSIVE_2,
// SKILL_RANGE_BONUS_AURA_2,
// SKILL_ADVANCED_MAGIC_MISSILE,
// SKILL_MOVEMENT_BONUS_FACTOR_PASSIVE_1,
// SKILL_MOVEMENT_BONUS_FACTOR_AURA_1,
// SKILL_MOVEMENT_BONUS_FACTOR_PASSIVE_2,
// SKILL_MOVEMENT_BONUS_FACTOR_AURA_2,
// SKILL_HASTE,
// SKILL_STAFF_DAMAGE_BONUS_PASSIVE_1,
// SKILL_STAFF_DAMAGE_BONUS_AURA_1,
// SKILL_STAFF_DAMAGE_BONUS_PASSIVE_2,
// SKILL_STAFF_DAMAGE_BONUS_AURA_2,
// SKILL_FIREBALL,
// SKILL_MAGICAL_DAMAGE_ABSORPTION_PASSIVE_1,
// SKILL_MAGICAL_DAMAGE_ABSORPTION_AURA_1,
// SKILL_MAGICAL_DAMAGE_ABSORPTION_PASSIVE_2,
// SKILL_MAGICAL_DAMAGE_ABSORPTION_AURA_2,
// SKILL_SHIELD};
return skillPath[activeSkillLevel];
}
// projectiles
double getProjectileMaxRange(const Projectile& projectile, const World& world, const Game& game) {
double castRange = 600; // maximum for high leveled wizards
for (const auto& wizard : world.getWizards()) {
if (wizard.getId() == projectile.getOwnerUnitId()) {
castRange = wizard.getCastRange();
}
}
if (projectile.getType() == PROJECTILE_DART) {
castRange = game.getFetishBlowdartAttackRange();
}
return castRange;
}
double getRemainingProjectileDistance(const Projectile& p, const Game& game, double projectileMaxRange, int firstSeenTick) {
double speed = 40;
if (p.getType() == PROJECTILE_DART) {
speed = game.getDartSpeed();
}
if (p.getType() == PROJECTILE_FIREBALL) {
speed = game.getFireballSpeed();
}
if (p.getType() == PROJECTILE_FROST_BOLT) {
speed = game.getFrostBoltSpeed();
}
return projectileMaxRange - speed * (tick - firstSeenTick + 1);
}
class ProjectileData {
public:
Projectile projectile;
int firstSeenTick;
Point origin;
double minAngle; // for retreat
double maxAngle; // for retreat
double projectileMaxRange;
bool targetingMe; // Im the target, but maybe out of range
bool isOutOfRange; // Im on the save side, dont step towards
double remainingProjectileDistance;
double distanceToSelf;
ProjectileData(const Wizard& self, const World& world, const Game& game, const Projectile& p) : projectile(p), firstSeenTick(tick) {
double angle = p.getAngle();
origin = Point(p.getX() - p.getSpeedX(), p.getY() - p.getSpeedY(), angle);
if (angle < 0) {
angle += 2 * PI;
}
minAngle = angle - PI / 30.;
maxAngle = angle + PI / 30.;
projectileMaxRange = getProjectileMaxRange(p, world, game);
targetingMe = false;
isOutOfRange = true;
remainingProjectileDistance = getRemainingProjectileDistance(p, game, projectileMaxRange, firstSeenTick);
distanceToSelf = self.getDistanceTo(p);
if (self.getId() != p.getOwnerPlayerId()) {
targetingMe = isLineCircleIntersectionPoint(origin, getPoint(origin, projectileMaxRange + p.getRadius()), self, self.getRadius());
isOutOfRange = !targetingMe;
}
};
void update(const Wizard& self, const Game& game) {
if (self.getId() != projectile.getOwnerPlayerId()) {
remainingProjectileDistance = getRemainingProjectileDistance(projectile, game, projectileMaxRange, firstSeenTick);
distanceToSelf = self.getDistanceTo(Point(projectile.getX() + (tick - firstSeenTick)*projectile.getSpeedX(), projectile.getY() + (tick - firstSeenTick)*projectile.getSpeedY(), projectile.getAngle()));
isOutOfRange = !isLineCircleIntersectionPoint(origin, getPoint(origin, projectileMaxRange), self, self.getRadius());
}
}
};
vector<ProjectileData> projectiles;
void updateProjectileData(const Wizard& self, const World& world, const Game& game) {
// update all old projectiles
for (auto& projectile : projectiles) {
projectile.update(self, game);
}
// insert new
auto isNewProjectile = [](const Projectile& projectile) {
for (const auto& p : projectiles) {
if (projectile.getId() == p.projectile.getId()) return false;
}
return true;
};
for (const auto& projectile : world.getProjectiles()) {
if (isNewProjectile(projectile)) {
projectiles.push_back(ProjectileData(self, world, game, projectile));
}
}
// remove old projectiles
auto isProjectileNotInGame = [&world](const ProjectileData& projectileData) {
for (const auto& p : world.getProjectiles()) {
if (projectileData.projectile.getId() == p.getId()) return false;
}
return true;
};
projectiles.erase(remove_if(projectiles.begin(), projectiles.end(), isProjectileNotInGame), projectiles.end());
}
// TODO stimmt targetingMe so
bool computeProjectileRetreatAngles(double& minAngle, double& maxAngle) {
minAngle = 0;
maxAngle = 2 * PI;
bool meTarget = false;
for (const auto& p : projectiles) {
if (p.targetingMe) {
meTarget = true;
if (p.minAngle > minAngle) {
minAngle = p.minAngle;
}
if (p.maxAngle < maxAngle) {
maxAngle = p.maxAngle;
}
}
}
return meTarget;
}
bool computeProjectileRetreatAngle(double& angle) {
bool meTarget = false;
for (const auto& p : projectiles) {
if (p.targetingMe) {
meTarget = true;
if (p.minAngle > angle) {
angle = p.minAngle;
}
if (p.maxAngle < angle) {
angle = p.maxAngle;
}
}
}
return meTarget;
}
// bases
Building getEnemyBase() {
for (const auto &building : enemyBuildingsGlobal) {
if (building.getType() == BUILDING_FACTION_BASE) {
return building;
}
}
return Building();
}
Building getHomeBase(const LivingUnit& unit, const World& world) {
for (auto const &building : world.getBuildings()) {
if (building.getType() == BUILDING_FACTION_BASE && building.getFaction() == unit.getFaction()) {
return building;
}
}
return Building();
}
// remember fog of war
template<typename T>
vector<LivingUnit> getLivingUnitsWithinDistance(const CircularUnit& origin, double distance, const vector<T>& units) {
vector<LivingUnit> returnUnits;
for (auto const &unit : units) {
double dist = origin.getDistanceTo(unit);
if (dist > 1) {
if (dist - origin.getRadius() - unit.getRadius() < distance) {
returnUnits.push_back(unit);
}
}
}
return returnUnits;
}
vector<LivingUnit> getStaticObstaclesWithinDistance(const CircularUnit& origin, double distance, const World& world) {
//vector<LivingUnit> obstacles = getLivingUnitsWithinDistance(origin, distance, world.getTrees());
//vector<LivingUnit> buildings = getLivingUnitsWithinDistance(origin, distance, world.getBuildings());
vector<LivingUnit> obstacles;
obstacles.insert(obstacles.end(), world.getTrees().begin(), world.getTrees().end());
obstacles.insert(obstacles.end(), world.getBuildings().begin(), world.getBuildings().end());
for (auto const &unit : world.getMinions()) {
if (unit.getFaction() == FACTION_NEUTRAL && unit.getSpeedX() == 0 && unit.getSpeedY() == 0) {
obstacles.push_back(unit);
}
}
return obstacles;
}
vector<LivingUnit> getAllObstaclesWithinDistance(const CircularUnit& origin, double distance, const World& world) {
vector<LivingUnit> obstacles = getStaticObstaclesWithinDistance(origin, distance, world);
//vector<LivingUnit> minions = getLivingUnitsWithinDistance(origin, distance, world.getMinions());
//obstacles.insert(obstacles.end(), minions.begin(), minions.end());
//vector<LivingUnit> wizards = getLivingUnitsWithinDistance(origin, distance, world.getWizards());
//obstacles.insert(obstacles.end(), wizards.begin(), wizards.end());
obstacles.insert(obstacles.end(), world.getMinions().begin(), world.getMinions().end());
//obstacles.insert(obstacles.end(), world.getWizards().begin(), world.getWizards().end());
for (auto const &unit : world.getWizards()) {
if (unit.isMe()) continue;
obstacles.push_back(unit);
}
return obstacles;
}
vector<LivingUnit> getMovingStaticObstaclesWithinDistance(const CircularUnit& origin, double distance, const World& world) {
vector<LivingUnit> obstacles = getStaticObstaclesWithinDistance(origin, distance, world);
for (auto const &unit : world.getMinions()) {
if (origin.getDistanceTo(unit) - origin.getRadius() - unit.getRadius() < distance && unit.getSpeedX() == 0 && unit.getSpeedY() == 0) {
obstacles.push_back(unit);
}
}
for (auto const &unit : world.getWizards()) {
if (origin.getDistanceTo(unit) - origin.getRadius() - unit.getRadius() < distance && unit.getSpeedX() == 0 && unit.getSpeedY() == 0) {
obstacles.push_back(unit);
}
}
return obstacles;
}
// type: 1 ... All, 2 ... Moving+Static, 3 ... Only static obstacles
vector<LivingUnit> getObstaclesWithinDistance(const CircularUnit& origin, double distance, const World& world, int type) {
if (type == 1) {
return getAllObstaclesWithinDistance(origin, distance, world);
} else if (type == 2) {
return getMovingStaticObstaclesWithinDistance(origin, distance, world);
} else {
return getStaticObstaclesWithinDistance(origin, distance, world);
}
}
// move functions
//-----------------
bool isHastened(const LivingUnit& self) {
for (const auto& s : self.getStatuses()) {
if (s.getType() == STATUS_HASTENED) {
return true;
}
}
return false;
}
bool isShielded(const LivingUnit& self) {
for (const auto& s : self.getStatuses()) {
if (s.getType() == STATUS_SHIELDED) {
return true;
}
}
return false;
}
double getBonusSpeedFactor(const LivingUnit& self, const Game& game) {
double bonusSpeedFactor = 1;
if (activeSkills[SKILL_MOVEMENT_BONUS_FACTOR_PASSIVE_1]) bonusSpeedFactor += game.getMovementBonusFactorPerSkillLevel();
if (activeSkills[SKILL_MOVEMENT_BONUS_FACTOR_AURA_1]) bonusSpeedFactor += game.getMovementBonusFactorPerSkillLevel();
if (activeSkills[SKILL_MOVEMENT_BONUS_FACTOR_PASSIVE_2]) bonusSpeedFactor += game.getMovementBonusFactorPerSkillLevel();
if (activeSkills[SKILL_MOVEMENT_BONUS_FACTOR_AURA_2]) bonusSpeedFactor += game.getMovementBonusFactorPerSkillLevel();
if (isHastened(self)) bonusSpeedFactor += game.getHastenedMovementBonusFactor();
return bonusSpeedFactor;
}
double getForwardSpeed(const LivingUnit& self, const Game& game) {
double speed = game.getWizardForwardSpeed();
double bonusSpeed = getBonusSpeedFactor(self, game);
return speed * bonusSpeed;
}
double getBackwardSpeed(const LivingUnit& self, const Game& game) {
double speed = game.getWizardBackwardSpeed();
double bonusSpeed = getBonusSpeedFactor(self, game);
return speed * bonusSpeed;
}
double getStrafeSpeed(const LivingUnit& self, const Game& game) {
double speed = game.getWizardStrafeSpeed();
double bonusSpeed = getBonusSpeedFactor(self, game);
return speed * bonusSpeed;
}
double getTurnAngle(const LivingUnit& self, const Game& game) {
double turnAngle = game.getWizardMaxTurnAngle();
double bonus = 1;
if (isHastened(self)) bonus += game.getHastenedRotationBonusFactor();
return turnAngle * bonus;
}
MovePoint getMovePoint(const LivingUnit& self, const Game& game, double angleRelative, double dist) {
double phi_r = angleRelative;
double phi_g = angleRelative + self.getAngle();
if (abs(phi_r) < PI / 2) { // ellipse
double a = getForwardSpeed(self, game);
double b = getStrafeSpeed(self, game);
double cosphi_r = cos(-phi_r);
double sinphi_r = sin(-phi_r);
double r = a*b / (sqrt(b*b*cosphi_r*cosphi_r + a*a*sinphi_r*sinphi_r));
r = dist < r ? dist : r;
return MovePoint(self.getX() + r*cos(-phi_g), self.getY() - r*sin(-phi_g), r * cosphi_r, -r * sinphi_r, phi_g, r, phi_r);
} else { // circle
double r = dist < getBackwardSpeed(self, game) ? dist : getBackwardSpeed(self, game);
return MovePoint(self.getX() + r*cos(-phi_g), self.getY() - r*sin(-phi_g), r * cos(-phi_r), -r * sin(-phi_r), phi_g, r, phi_r);
}
}
// remember fog of war
bool isDirectWayToTargetWithoutObstacles(const CircularUnit& self, const World& world, const Game& game, const Point& target, int type) {
if (target.getX() < 40 || target.getX() > 3960 || target.getY() < 40 || target.getY() > 3960) {
return false;
}
double dist = self.getDistanceTo(target);
vector<LivingUnit> obstacles = getObstaclesWithinDistance(self, dist + spacingBuffer, world, type);
// any obstacles within searchDistance
bool intersection = false;
double radadd = game.getWizardRadius() + spacingBuffer;
Point start = getPoint(self, self.getAngle() + self.getAngleTo(target), spacingBuffer);
for (auto const &obstacle : obstacles) {
if (target.getDistanceTo(obstacle) <= obstacle.getRadius()) continue;
intersection = isLineCircleIntersectionPoint(start, target, obstacle, obstacle.getRadius() + radadd);
if (intersection) {
return false;
}
}
return true;
}
// type: 1 ... All, 2 ... Moving, 3 ... Only static obstacles
double nextMoveTowardsTargetSearch(const Wizard& self, const World& world, const Game& game, double targetAngle, double searchDistance, int type) {
vector<LivingUnit> obstacles = getObstaclesWithinDistance(self, searchDistance + spacingBuffer, world, type);
if (obstacles.size() == 0) return targetAngle;
// find new angle without obstacles
double angleCounterClockWise = targetAngle;
double angleClockWise = targetAngle;
// counter clock wise
unsigned int circlePoints = 180;
double rad = PI / circlePoints;
double radadd = game.getWizardRadius() + spacingBuffer;
bool intersection1 = true;
for (unsigned int i = 1; i <= circlePoints; i++) {
angleCounterClockWise = targetAngle - i * rad;
Point start = getPoint(self, angleCounterClockWise, spacingBuffer);
Point end = getPoint(self, angleCounterClockWise, searchDistance);
if (end.getX() < 40 || end.getX() > 3960 || end.getY() < 40 || end.getY() > 3960) {
continue;
}
for (auto const &obstacle : obstacles) {
intersection1 = isLineWithinCircle(start, obstacle, obstacle.getRadius() + radadd);
if (intersection1) break;
intersection1 = isLineCircleIntersectionPoint(start, end, obstacle, obstacle.getRadius() + radadd);
if (intersection1) {
break;
} // sobald ein Hindernis entdeckt wird, weiter zum nächsten Winkel
}
if (!intersection1) {
break;
}
}
// clock wise
bool intersection2 = true;
for (unsigned int i = 0; i < circlePoints; i++) {
angleClockWise = targetAngle + i * rad;
Point start = getPoint(self, angleClockWise, spacingBuffer);
Point end = getPoint(self, angleClockWise, searchDistance);
if (end.getX() < 40 || end.getX() > 3960 || end.getY() < 40 || end.getY() > 3960) {
continue;
}
for (auto const &obstacle : obstacles) {
intersection2 = isLineWithinCircle(start, obstacle, obstacle.getRadius() + radadd);
if (intersection2) break;
intersection2 = isLineCircleIntersectionPoint(start, end, obstacle, obstacle.getRadius() + radadd);
if (intersection2) {
break;
} // sobald ein Hindernis entdeckt wird, weiter zum nächsten Winkel
}
if (!intersection2) {
break;
}
}
// es gibt nun zwei Winkel ohne Hindernisse
// wähle jenen Winkel mit geringer Differenz zum Ziel
double d1 = angleCounterClockWise - targetAngle;
double d2 = angleClockWise - targetAngle;
double angleNew = targetAngle;
if (abs(d1) < abs(d2)) {
angleNew = angleCounterClockWise;
} else {
angleNew = angleClockWise;
}
if (debug) cout << "search distance:" << searchDistance << " obstacle size:" << obstacles.size() << " angleCounterClockWise:" << angleCounterClockWise / PI * 180 << " angleClockWise:" << angleClockWise / PI * 180 << " angleNew:" << angleNew / PI * 180 << endl;
return angleNew;
}
bool isEnemyUnitAttackedByMinions(const LivingUnit& unit, const World& world, const Game& game) {
for (auto const &minion : world.getMinions()) {
if (unit.getFaction() != minion.getFaction() && minion.getFaction() != FACTION_NEUTRAL) {
double dist = unit.getDistanceTo(minion);
if ((minion.getType() == MINION_ORC_WOODCUTTER && dist <= (game.getOrcWoodcutterAttackRange() + unit.getRadius())) ||
(minion.getType() == MINION_FETISH_BLOWDART && dist <= (game.getFetishBlowdartAttackRange() + unit.getRadius()))) {
return true;
}
}
}
return false;
}
double getNonMovingDistanceToObstacle(const LivingUnit& obstacle, const World& world, const Game& game) {
if (isEnemyUnitAttackedByMinions(obstacle, world, game)) {
if (obstacle.getRadius() == 50) return 600 - 50;
if (obstacle.getRadius() == 35) return 545 - 36;
if (obstacle.getRadius() == 25) return game.getFetishBlowdartAttackRange();
} else {
if (obstacle.getRadius() == 50) return game.getGuardianTowerAttackRange();
if (obstacle.getRadius() == 35) return game.getWizardCastRange() + game.getMagicMissileRadius() + game.getWizardRadius();
if (obstacle.getRadius() == 25) return game.getFetishBlowdartAttackRange() + game.getDartRadius() + game.getWizardRadius();
}
return obstacle.getRadius();
}
// Theta*
void updateThetaStarMap(const Wizard& self, const World& world) {
auto makeCircle = [](const Vectorf& center, float radius, int scale) {
Vectori lt((center.x - radius - 0.00001f) / scale, (center.y - radius - 0.00001f) / scale);
Vectori rb((center.x + radius) / scale, (center.y + radius) / scale);
for (int x = lt.x + 1; x <= rb.x; x++) {
for (int y = lt.y + 1; y <= rb.y; y++) {
float dist = (x*scale - center.x)*(x*scale - center.x) + (y*scale - center.y)*(y*scale - center.y);
if (dist <= radius*radius) {
if (map[x][y] == ' ') {
map[x][y] = '0';
}
else {
map[x][y] += 1;
}
}
}
}
};
auto makeWall = [](const Vectori& pos, const Vectori& size) {