-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.txt
114 lines (95 loc) · 2.95 KB
/
utilities.txt
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
/* Ideas
* Don't circle targets if we are really close to them
* better verticle aim on kill
* check recharge level based on distance to power station
* when repairing fly higher to avoid damage
*/
extern public void Utilities() {}
public bool object::isAlive() {
return search(category, position) == this;
}
public class SpotChecker {
public bool check(point p) {
message("check not implemented", DisplayError);
throw 1;
}
}
public point findSpot(SpotChecker checker, point origin, float minDist, float stride) {
point p = origin;
int start = minDist / stride;
for (int side = start; side < 50; side++) {
p.y = origin.y - stride * side;
for (p.x = origin.x - stride * side; p.x < origin.x + 0.1 + stride * side; p.x += stride) {
if (checker.check(p)) return p;
}
p.x = origin.x + stride * side;
for (p.y = origin.y - stride * side; p.y < origin.y + 0.1 + stride * side; p.y += stride) {
if (checker.check(p)) return p;
}
p.y = origin.y + stride * side;
for (p.x = origin.x + stride * side; p.x > origin.x - 0.1 - stride * side; p.x -= stride) {
if (checker.check(p)) return p;
}
p.x = origin.x - stride * side;
for (p.y = origin.y + stride * side; p.y > origin.y - 0.1 - stride * side; p.y -= stride) {
if (checker.check(p)) return p;
}
}
}
public point nearestSafeSpot(point origin, float minDist) {
// FIX: put this back to inline construction when melex fixes his code
SpotChecker c = new SafeSpotChecker(minDist);
return findSpot(c, origin, 0, 10);
}
public class SafeSpotChecker extends SpotChecker {
static Const const = new Const();
float minDistToEnemy;
void SafeSpotChecker(float _minDistToEnemy) {
minDistToEnemy = _minDistToEnemy;
}
public bool check(point p) {
if (topo(p) < 0.5) return false;
if (flatground(p, 2) < 1) return false;
object o = search(const.ALL_ALIENS, p);
if (o != null && distance2d(p, o.position) < minDistToEnemy) return false;
return true;
}
}
public point findShipSpot() {
object o = radar(SpaceShip);
// FIX: put this back to inline construction when melex fixes his code
SpotChecker c = new ShipSpotChecker();
return findSpot(c, o.position, 0, 1);
}
public class ShipSpotChecker extends SpotChecker {
public bool check(point p) {
object o = search(Any, p);
return (distance2d(p, o.position) > 3);
}
}
public float angle(point pointA, point pointB)
{
if (pointB.x - pointA.x == 0) {
if (pointB.y - pointA.y > 0) {
return 90;
} else {
return 270;
}
}
float offset = 0;
if ((pointB.x - pointA.x) < 0) {
offset = 180;
}
return offset + atan((pointB.y - pointA.y) / (pointB.x - pointA.x));
}
public float normalize(float a) {
while (a < 0) a += 360;
while (a > 360) a -= 360;
return a;
}
public point alongDirection(point origin, float angle, float dis) {
point p;
p.x = origin.x + cos(angle) * dis;
p.y = origin.y + sin(angle) * dis;
return p;
}