-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRobocodeRobot.java
More file actions
174 lines (147 loc) · 5.78 KB
/
RobocodeRobot.java
File metadata and controls
174 lines (147 loc) · 5.78 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
package jc;
import robocode.*;
import static robocode.util.Utils.normalRelativeAngleDegrees;
import static robocode.util.Utils.normalRelativeAngle;
import java.awt.*;
public class RobocodeRobot extends AdvancedRobot {
boolean forward; // Whether the robot is moving forward or backwards.
boolean nearWall; // True when the robot is near the wall.
int numOfTurns; // The number of turns/ticks deciding how often the robot strafes.
double energyChange; // This stores the change in energy for the currently scanned enemy robot.
double previousEnergy = 100; // The previous energy of the robot we are scanning. Allows us to calculate the change in energy.
double bulletPower; // The power of the bullet that the robot will use.
public void run() {
// Allow each part of the robot to move freely from the others.
setAdjustRadarForRobotTurn(true);
setAdjustGunForRobotTurn(true);
setAdjustRadarForGunTurn(true);
// Move the robot forward by 100,000 pixels.
setAhead(100000);
forward = true;
// Turn the radar right by 360 degrees to try and find an enemy.
setTurnRadarRight(360);
// Set the robots colours
setGunColor(new Color(00, 00, 00)); // Black
setRadarColor(new Color(255, 00, 00)); // Red
setBodyColor(new Color(255, 00, 00)); // Red
setBulletColor(new Color(220, 00, 255)); // Pink
// Wall avoidance
// Check if the robot is closer than 60px from the wall.
if (getX() <= 60 || getY() <= 60 || getBattleFieldWidth() - getX() <= 60 || getBattleFieldHeight() - getY() <= 60) {
nearWall = true;
} else {
nearWall = false;
}
while (true) {
// More wall avoidance
// If the robot is away from all walls, set nearWall to false.
if (getX() > 60 && getY() > 60 && getBattleFieldWidth() - getX() > 60 && getBattleFieldHeight() - getY() > 60 && nearWall == true) {
nearWall = false;
}
// If the robot is near a wall and nearWall is set to false, set it to true and move in the opposite direction.
if (getX() <= 60 || getY() <= 60 || getBattleFieldWidth() - getX() <= 60 || getBattleFieldHeight() - getY() <= 60 ) {
if ( nearWall == false){
changeDirection();
nearWall = true;
}
}
// Strafing
// Every 40/30 turns/ticks of a round, the robot will change direction and therefore strafe.
if (forward == true) {
numOfTurns = 40;
} else {
numOfTurns = 30;
}
if (getTime() % numOfTurns == 0) {
changeDirection();
}
// If the radar stops turning, turn the radar right by 360 degrees to try and find a new enemy.
if (getRadarTurnRemaining() == 0.0){
setTurnRadarRight(360);
}
// Executes all of the set commands. e.g. setAhead.
execute();
}
}
public void onScannedRobot(ScannedRobotEvent e) {
// Work out the fire power based on distance from enemy.
if (e.getDistance() > 350) {
bulletPower = 1;
} else if (e.getDistance() > 100) {
bulletPower = 2;
} else {
bulletPower = 3;
}
// Linear predictive targeting
double bulletVelocity = 20 - 3 * bulletPower; // The speed of the robots bullet.
double enemyAbsoluteBearingRadians = getHeadingRadians() + e.getBearingRadians(); // Angle between north and enemy observed from the robot in radians.
double enemyLateralVelocity = e.getVelocity() * Math.sin(e.getHeadingRadians() - enemyAbsoluteBearingRadians); // The enemy robots velocity perpendicular to the robot.
setTurnGunRightRadians(normalRelativeAngle((enemyAbsoluteBearingRadians - getGunHeadingRadians()) + (enemyLateralVelocity / bulletVelocity))); // Turns the gun ahead of the enemy, to the point where they will be when the bullet reaches them.
double enemyAbsoluteBearing = getHeading() + e.getBearing(); // Angle between north and enemy observed from the robot in degrees.
double enemyDegreesFromRadar = normalRelativeAngleDegrees(enemyAbsoluteBearing - getRadarHeading()); // Angel between enemy robots absolute bearing and the heading of the radar.
setTurnRadarRight(enemyDegreesFromRadar); // Keeps the radar focused on the enemy.
// Fire the gun if gun heat is 0 so we won't get fired into disability.
if (getGunHeat() == 0 && (getEnergy() - bulletPower) >= 0.1) {
fire(bulletPower);
}
// Circling
// Circle around the enemy robot and move closer.
if (forward == true){
setTurnRight(normalRelativeAngleDegrees(e.getBearing() + 80));
} else {
setTurnRight(normalRelativeAngleDegrees(e.getBearing() + 100));
}
// Evading
// If close to enemy
if (e.getDistance() < 100) {
// And if enemy is infront of the robot then move back
if (e.getBearing() > -90 && e.getBearing() < 90) {
setBack(120);
// Else if behind the robot, move forward
} else {
setAhead(120);
}
}
// Dodgeing bullets
// If the enemy robots energy change is small, change direction to avoid bullet.
energyChange = previousEnergy - e.getEnergy();
if (energyChange > 0 && energyChange <= 3) {
changeDirection();
}
// Calls the OnScannedRobot method if a robot has been seen.
if (enemyDegreesFromRadar == 0) {
scan();
}
// Store the enemies current energy in previous energy.
previousEnergy = e.getEnergy();
}
// If the avoiding walls code doesn't work and the robot hits a wall, reverse the direction of the robot.
public void onHitWall(HitWallEvent e) {
changeDirection();
}
public void onHitRobot(HitRobotEvent e) {
// If we hit another robot, then reverse.
if (e.isMyFault()) {
changeDirection();
}
}
// Reverses the direction of the robot.
public void changeDirection() {
if (forward == true) {
setBack(100000);
forward = false;
} else {
setAhead(100000);
forward = true;
}
}
// If the robot has wn the game, then stop and do a victory dance.
public void onWin(WinEvent e) {
for (int i = 0; i < 60; i++) {
setAhead(0);
turnRight(30);
turnLeft(30);
fire(3);
}
}
}