-
Notifications
You must be signed in to change notification settings - Fork 0
/
MechuEY.java
326 lines (286 loc) · 12.4 KB
/
MechuEY.java
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
package ngs;
import robocode.*;
import robocode.util.Utils;
import java.awt.Color;
import java.util.Random;
// API help : https://robocode.sourceforge.io/docs/robocode/robocode/Robot.html
/**
* MechuEΥ - a robot by (NIKOLAOS GIANNOPOULOS)
*/
public class MechuEY extends AdvancedRobot {
static int direction = 1; // 1= clockwise, -1= counter-clockwise
static double keepDistance = 250; // Target distance from enemy
private double battleFieldHeight; // Min: Down, Max: Up
private double battleFieldWidth; // Min: Left, Max: Right
private double wallDistance = 100;
private double eEnergy = 100; // Enemy's energy
public void run() {
// Initialization of the robot
battleFieldHeight = getBattleFieldHeight();
battleFieldWidth = getBattleFieldWidth();
setColors(Color.white, Color.black, Color.black); // body, gun, radar Colors
setAdjustRadarForRobotTurn(true); // Sets the radar to turn independent from the robot's turn
setAdjustRadarForGunTurn(true); // Sets the radar to turn independent from the gun's turn
setAdjustGunForRobotTurn(true); // Sets the gun to turn independent from the robot's turn
// Robot main loop
while (true) {
if (getRadarTurnRemaining() == 0) {
setTurnRadarRight(360); // turns Radar 360 deg right to scan for a target
}
execute(); // Executes any pending actions, or continues executing actions that are in process
}
}
// Robot scans another robot
public void onScannedRobot(ScannedRobotEvent e) {
// absoluteBearing(angleToEnemy): clockwise angle between north and an scanned robot
double angleToEnemy = getHeadingRadians() + e.getBearingRadians();
// my coordinates
double myX = getX();
double myY = getY();
// enemy's coordinates
double eX = myX + Math.sin(angleToEnemy) * e.getDistance(); // sinθ= opp/hyp, opp=x, hyp=dist
double eY = myY + Math.cos(angleToEnemy) * e.getDistance(); // cosθ= adj/hyp, adj=y, hyp=dist
// radar turn facing enemy
double radarTurn = Utils.normalRelativeAngle(angleToEnemy - getRadarHeadingRadians());
// extra radar turn for wider scanning
double extraTurn = Rules.RADAR_TURN_RATE_RADIANS / 2.5;
if (radarTurn < 0) {
radarTurn -= extraTurn;
} else {
radarTurn += extraTurn;
}
setTurnRadarRightRadians(radarTurn);
// Gun power based on enemy's distance
double properBullet = Math.min((1 - (e.getDistance() / 600)) * 4, getEnergy());
// https://robowiki.net/wiki/Maximum_Escape_Angle (law of sines)
// Enemy's velocity in a direction perpendicular to this
// if enemy moves clockwise lateralVelocity > 0, if counter-clockwise lateralVelocity < 0
// else lateralVelocity == 0
double lateralVelocity = e.getVelocity() * Math.sin(e.getHeadingRadians() - angleToEnemy);
double movementPrediction = Math.asin(lateralVelocity / Rules.getBulletSpeed(properBullet));
double gunTurn = Utils
.normalRelativeAngle(angleToEnemy - getGunHeadingRadians() + movementPrediction);
// Gun turns at the direction that the selected bullet would reach the enemy robot
// as if it had constant lateral velocity
setTurnGunRightRadians(gunTurn);
// Fires a bullet with the previously calculated power
setFire(properBullet);
// atan2: θ= arctan(y/x), where here is y= x, x= y
double robotTurn = Math.atan2(eX - myX, eY - myY); // Robot turns facing scanned robot
// value that turns the robot towards the enemy or away from the enemy based on the keepDistance
double getCloser;
if (myX > wallDistance && myX < battleFieldWidth - wallDistance && myY > wallDistance
&& myY < battleFieldHeight - wallDistance) {
getCloser = (e.getDistance() - keepDistance) / (400 - keepDistance);
} else {
getCloser = 0;
}
// Robot turns 90deg from the enemy robot and then turns towards the enemy or away
// by a max of 45 deg, to maintain the desired keepDistance
robotTurn = Utils.normalRelativeAngle(robotTurn - getHeadingRadians()) - Math.PI / 2
+ Math.PI / 4 * Math.max(-1, Math.min(getCloser, 1)) * direction;
robotTurn = wallTurnSmoothing(robotTurn);
double nearWall = wallSpeedSmoothing();
setTurnRightRadians(robotTurn);
// If enemy fires
if (eEnergy > e.getEnergy()) {
eEnergy = e.getEnergy();
if (Math.random() < .6) {
direction *= -1; // randomly changes direction
}
} else {
eEnergy = e.getEnergy();
}
// Robot moves towards or backwards based on the direction, at maximum speed
// and slows if there is a wall near it
setAhead(Rules.MAX_VELOCITY * (2.5 * nearWall) * direction);
}
// When bullet doesn't hit a robot
public void onBulletMissed(BulletMissedEvent e) {
Random r = new Random();
int low = 25;
int high = 75;
int result = r.nextInt(high - low) + low;
keepDistance -= result; // subtracts from keepDistance a random value between 25 and 75
keepDistance = Math.min(Math.max(50, keepDistance), 400);
}
// When bullet hits another bullet
public void onBulletHitBullet(BulletHitBulletEvent e) {
if (Math.random() < .6) {
direction *= -1;
}
}
// When bullet hits a robot
public void onBulletHit(BulletHitEvent e) {
eEnergy = e.getEnergy();
}
/****************************************************/
/*------------------Custom Functions----------------*/
/****************************************************/
// If wall nearby, robot follows wall or changes direction accordingly
public double wallTurnSmoothing(double robotTurn) {
double myX = getX();
double myY = getY();
double wallSmoothing = 0;
// UP{
if (myY > battleFieldHeight - wallDistance) {
if (direction == 1) {
if (getHeading() >= 0 && getHeading() <= 90) {
wallSmoothing += Utils.normalRelativeAngle(
Math.atan2(100, -100) - getHeadingRadians());
} else if (getHeading() >= 270 && getHeading() < 360) {
direction = -1;
}
} else {
if (getHeading() < 180 && getHeading() >= 90) {
wallSmoothing += Utils.normalRelativeAngle(
Math.atan2(100, 100) - getHeadingRadians());
} else if (getHeading() >= 180 && getHeading() <= 270) {
direction = 1;
}
}
}
// }
// Down{
if (myY < wallDistance) {
if (direction == -1) {
if (getHeading() >= 0 && getHeading() <= 90) {
direction = 1;
} else if (getHeading() >= 270 && getHeading() < 360) {
wallSmoothing += Utils.normalRelativeAngle(
Math.atan2(-100, -100) - getHeadingRadians());
}
} else {
if (getHeading() < 180 && getHeading() >= 90) {
direction = -1;
} else if (getHeading() >= 180 && getHeading() <= 270) {
wallSmoothing += Utils.normalRelativeAngle(
Math.atan2(-100, 100) - getHeadingRadians());
}
}
}
// }
// Left{
if (myX < wallDistance) {
if (direction == 1) {
if (getHeading() > 270 && getHeading() < 360) {
wallSmoothing += Utils.normalRelativeAngle(
Math.atan2(100, 100) - getHeadingRadians());
} else if (getHeading() <= 270 && getHeading() >= 180) {
direction = -1;
}
} else {
if (getHeading() < 180 && getHeading() >= 90) {
direction = 1;
} else if (getHeading() >= 0 && getHeading() < 90) {
wallSmoothing += Utils.normalRelativeAngle(
Math.atan2(-100, 100) - getHeadingRadians());
}
}
}
// }
// Right{
if (myX > battleFieldWidth - wallDistance) {
if (direction == 1) {
if (getHeading() > 90 && getHeading() <= 180) {
wallSmoothing += Utils.normalRelativeAngle(
Math.atan2(-100, -100) - getHeadingRadians());
} else if (getHeading() <= 90 && getHeading() >= 0) {
direction = -1;
}
} else {
if (getHeading() >= 270 && getHeading() < 360) {
direction = 1;
} else if (getHeading() >= 180 && getHeading() < 270) {
wallSmoothing += Utils.normalRelativeAngle(
Math.atan2(100, -100) - getHeadingRadians());
}
}
}
// }
if (wallSmoothing != 0) {
return wallSmoothing;
} else {
return robotTurn;
}
}
// If wall nearby, slows robot's speed
public double wallSpeedSmoothing() {
double myX = getX();
double myY = getY();
double nearWall = 1;
// UP{
if (myY > battleFieldHeight - wallDistance) {
if (direction == 1) {
if (getHeading() >= 0 && getHeading() <= 90) {
nearWall = (2
- (myY - (battleFieldHeight - wallDistance - 100))
/ ((battleFieldHeight) - (battleFieldHeight - wallDistance - 100)))
* (getHeading() / 90);
}
} else {
if (getHeading() < 180 && getHeading() >= 90) {
nearWall = (2
- (myY - (battleFieldHeight - wallDistance - 100))
/ ((battleFieldHeight) - (battleFieldHeight - wallDistance - 100)))
* ((180 - getHeading()) / 90);
}
}
}
// }
// Down{
if (myY < wallDistance) {
if (direction == -1) {
if (getHeading() >= 270 && getHeading() < 360) {
nearWall = (2
- (wallDistance - myY) / (wallDistance))
* ((360 - getHeading()) / 90);
}
} else {
if (getHeading() >= 180 && getHeading() <= 270) {
nearWall = (2
- (wallDistance - myY) / (wallDistance))
* ((getHeading() - 180) / 90);
}
}
}
// }
// Left{
if (myX < wallDistance) {
if (direction == 1) {
if (getHeading() > 270 && getHeading() < 360) {
nearWall = (2
- (wallDistance - myX) / (wallDistance))
* ((getHeading() - 270) / 90);
}
} else {
if (getHeading() >= 0 && getHeading() < 90) {
nearWall = (2
- (wallDistance - myX) / (wallDistance))
* ((90 - getHeading()) / 90);
}
}
}
// }
// Right{
if (myX > battleFieldWidth - wallDistance) {
if (direction == 1) {
if (getHeading() > 90 && getHeading() <= 180) {
nearWall = (2
- (myX - (battleFieldWidth - wallDistance - 100))
/ ((battleFieldWidth) - (battleFieldWidth - wallDistance - 100)))
* ((getHeading() - 90) / 90);
}
} else {
if (getHeading() >= 180 && getHeading() < 270) {
nearWall = (2
- (myX - (battleFieldWidth - wallDistance - 100))
/ ((battleFieldWidth) - (battleFieldWidth - wallDistance - 100)))
* ((270 - getHeading()) / 90);
}
}
}
// }
return nearWall;
}
}