Difference between revisions of "BulletSimBot/Current Code"
Jump to navigation
Jump to search
Awesomeness (talk | contribs) (Showing my messed up code) |
RednaxelaBot (talk | contribs) m (Using <syntaxhighlight>.) |
||
(7 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
− | This is big, but here it is... | + | This is big, but here it is... I had to change it a bit to make it compatible with the newest version of Robocode. |
− | < | + | <syntaxhighlight>package awesomeness; |
− | |||
import robocode.*; | import robocode.*; | ||
import robocode.util.*; | import robocode.util.*; | ||
Line 9: | Line 8: | ||
/** | /** | ||
− | * PwnBot - a robot by | + | * PwnBot - a robot by Awesomenss |
*/ | */ | ||
public class PwnBot extends AdvancedRobot { | public class PwnBot extends AdvancedRobot { | ||
Line 26: | Line 25: | ||
ArrayList<VirtualBullet> bullets = new ArrayList<VirtualBullet>(); | ArrayList<VirtualBullet> bullets = new ArrayList<VirtualBullet>(); | ||
VirtualBullet newBullet, bullet; | VirtualBullet newBullet, bullet; | ||
− | |||
public void run() { | public void run() { | ||
− | |||
− | |||
− | |||
scannedRobotX = -1; | scannedRobotX = -1; | ||
Line 70: | Line 65: | ||
newBullet = new VirtualBullet(scannedRobotX, scannedRobotY, 20 - 3 * changeInEnergy, Utils.normalRelativeAngle(absoluteBearing + Math.PI + Math.asin((Math.sin(e.getBearingRadians()) * getVelocity())/(20 - 3 * changeInEnergy)))); | newBullet = new VirtualBullet(scannedRobotX, scannedRobotY, 20 - 3 * changeInEnergy, Utils.normalRelativeAngle(absoluteBearing + Math.PI + Math.asin((Math.sin(e.getBearingRadians()) * getVelocity())/(20 - 3 * changeInEnergy)))); | ||
− | |||
bullets.add(newBullet); | bullets.add(newBullet); | ||
− | + | ||
+ | |||
} | } | ||
Line 90: | Line 85: | ||
} | } | ||
− | |||
///////////////////End Bullet Code///////////////////// | ///////////////////End Bullet Code///////////////////// | ||
− | + | //Stay at almost right angles | |
− | + | //0setTurnRightRadians(Utils.normalRelativeAngle(absoluteBearing + Math.PI/2 - getHeadingRadians())); | |
− | + | ||
− | + | //setAhead(movementDirection); | |
+ | |||
+ | //if (Math.random() > 0.92) i(); | ||
Line 137: | Line 133: | ||
movementDirection = -movementDirection; | movementDirection = -movementDirection; | ||
} | } | ||
− | + | ||
///////////////////////////Painting//////////////////////// | ///////////////////////////Painting//////////////////////// | ||
Line 150: | Line 146: | ||
bullet = bullets.get(i); | bullet = bullets.get(i); | ||
− | g.fillOval( (int) bullet.getX() - 3, (int) | + | g.fillOval( (int) bullet.getX() - 3, (int) bullet.getY() - 3, 6, 6); |
} | } | ||
Line 163: | Line 159: | ||
g.setColor(new Color(255, 32, 0)); //Draw the target | g.setColor(new Color(255, 32, 0)); //Draw the target | ||
g.drawLine( (int) scannedRobotX, (int) getBattleFieldHeight(), (int) scannedRobotX, 0); | g.drawLine( (int) scannedRobotX, (int) getBattleFieldHeight(), (int) scannedRobotX, 0); | ||
− | g.drawLine( (int) getBattleFieldWidth(), (int) | + | g.drawLine( (int) getBattleFieldWidth(), (int) scannedRobotY, 0, (int) scannedRobotY); |
− | g.drawRect( (int) scannedRobotX-30, (int) | + | g.drawRect( (int) scannedRobotX-30, (int) scannedRobotY-30, 60, 60); |
− | g.drawRect( (int) scannedRobotX-10, (int) | + | g.drawRect( (int) scannedRobotX-10, (int) scannedRobotY-10, 20, 20); |
g.setColor(new Color(255, 32, 0, 64)); | g.setColor(new Color(255, 32, 0, 64)); | ||
− | g.fillRect( (int) scannedRobotX-30, (int) | + | g.fillRect( (int) scannedRobotX-30, (int) scannedRobotY-30, 60, 60); |
} | } | ||
} | } | ||
Line 173: | Line 169: | ||
///////////////////////////Painting//////////////////////// | ///////////////////////////Painting//////////////////////// | ||
− | + | ||
public void onHitByBullet(HitByBulletEvent e) { previousEnergy += 3 * e.getBullet().getPower(); } | public void onHitByBullet(HitByBulletEvent e) { previousEnergy += 3 * e.getBullet().getPower(); } | ||
public void onBulletHit(BulletHitEvent e) { previousEnergy -= 4 * e.getBullet().getPower() + Math.max(0,2 * (e.getBullet().getPower() - 1)); } | public void onBulletHit(BulletHitEvent e) { previousEnergy -= 4 * e.getBullet().getPower() + Math.max(0,2 * (e.getBullet().getPower() - 1)); } | ||
Line 179: | Line 175: | ||
− | + | public class VirtualBullet { //A class to simulate bullets. | |
− | |||
− | |||
− | |||
− | public class VirtualBullet { //A class to simulate bullets. | ||
− | |||
double x, y, speed, angle; | double x, y, speed, angle; | ||
Line 192: | Line 183: | ||
x = startX; | x = startX; | ||
y = startY; | y = startY; | ||
− | speed = velocity | + | speed = velocity; |
angle = trajectory; | angle = trajectory; | ||
− | |||
} | } | ||
Line 228: | Line 218: | ||
} | } | ||
} | } | ||
− | + | ||
− | + | }</syntaxhighlight> | |
− | + | ||
− | + | [[Category:Source Code]] | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− |
Latest revision as of 09:22, 1 July 2010
This is big, but here it is... I had to change it a bit to make it compatible with the newest version of Robocode.
package awesomeness;
import robocode.*;
import robocode.util.*;
import java.util.Random;
import java.util.ArrayList;
import java.awt.*;
/**
* PwnBot - a robot by Awesomenss
*/
public class PwnBot extends AdvancedRobot {
double previousEnergy = 100d;
double changeInEnergy;
double scannedRobotX, scannedRobotY;
int movementDirection = 50;
long lastTime; //The last time known, used to track bullets
Random generator = new Random(); //This makes random numbers
ArrayList<VirtualBullet> bullets = new ArrayList<VirtualBullet>();
VirtualBullet newBullet, bullet;
public void run() {
scannedRobotX = -1;
scannedRobotY = -1;
setBodyColor(Color.red);
setGunColor(Color.darkGray);
setRadarColor(Color.red.darker());
setBulletColor(Color.red);
lastTime = 0;
setAdjustGunForRobotTurn(true);
setAdjustRadarForGunTurn(true);
setAdjustRadarForRobotTurn(true);
while(true)
turnRadarRightRadians(Double.POSITIVE_INFINITY);
}
/**
* onScannedRobot: What to do when you see another robot
*/
public void onScannedRobot(ScannedRobotEvent e) {
//The absolute bearing, this is used a lot
double absoluteBearing = e.getBearingRadians() + getHeadingRadians();
///////////////////////////////////////////////////////
////////////////////Movement Code//////////////////////
/////////////////////Bullet Code///////////////////////
scannedRobotX = getX() + Math.sin(absoluteBearing) * e.getDistance();
scannedRobotY = getY() + Math.cos(absoluteBearing) * e.getDistance();
//If there's a change in energy, it probably fired
if ((changeInEnergy = previousEnergy - (previousEnergy = e.getEnergy())) > 0d && changeInEnergy<=3) {
newBullet = new VirtualBullet(scannedRobotX, scannedRobotY, 20 - 3 * changeInEnergy, Utils.normalRelativeAngle(absoluteBearing + Math.PI + Math.asin((Math.sin(e.getBearingRadians()) * getVelocity())/(20 - 3 * changeInEnergy))));
bullets.add(newBullet);
}
for (int i = 0; i < bullets.size(); i++) {
bullet = bullets.get(i);
bullet.tick(getTime() - lastTime);
bullets.set(i, bullet);
if (bullets.get(i).checkCollide(getX(), getY())) {
// I think this will work
// bullets.remove(i--);
// but usually I do
bullets.remove(i);
i--;
}
}
///////////////////End Bullet Code/////////////////////
//Stay at almost right angles
//0setTurnRightRadians(Utils.normalRelativeAngle(absoluteBearing + Math.PI/2 - getHeadingRadians()));
//setAhead(movementDirection);
//if (Math.random() > 0.92) i();
////////////////////Movement Code//////////////////////
///////////////////////////////////////////////////////
///////////////////////////////////////////////////////
///////////////////////Gun Code////////////////////////
///////////////////////Gun Code////////////////////////
///////////////////////////////////////////////////////
///////////////////////////////////////////////////////
/////////////////////Radar Code////////////////////////
setTurnRadarRightRadians(2 * Utils.normalRelativeAngle(absoluteBearing - getRadarHeadingRadians()));
/////////////////////Radar Code////////////////////////
///////////////////////////////////////////////////////
lastTime = getTime();
}
public void onHitWall(HitWallEvent e) {
i();
}
public void i() {
movementDirection = -movementDirection;
}
///////////////////////////Painting////////////////////////
// Paint a transparent square on top of the last scanned robot
public void onPaint(Graphics2D g) {
g.setColor(new Color(255, 255, 0));
for (int i = 0; i < bullets.size(); i++) { //Draw all the bullets
bullet = bullets.get(i);
g.fillOval( (int) bullet.getX() - 3, (int) bullet.getY() - 3, 6, 6);
}
if(scannedRobotX == -1 && scannedRobotY == -1) { //If we have no target
g.setColor(Color.green); //Draw the target
g.drawString("No target", (int) getBattleFieldWidth()/80, (int) getBattleFieldHeight()/60);
}
else { //If we do have a target
g.setColor(new Color(255, 96, 0)); //Draw the target
g.drawString("Target locked", (int) getBattleFieldWidth()/80, (int) getBattleFieldHeight()/60);
g.setColor(new Color(255, 32, 0)); //Draw the target
g.drawLine( (int) scannedRobotX, (int) getBattleFieldHeight(), (int) scannedRobotX, 0);
g.drawLine( (int) getBattleFieldWidth(), (int) scannedRobotY, 0, (int) scannedRobotY);
g.drawRect( (int) scannedRobotX-30, (int) scannedRobotY-30, 60, 60);
g.drawRect( (int) scannedRobotX-10, (int) scannedRobotY-10, 20, 20);
g.setColor(new Color(255, 32, 0, 64));
g.fillRect( (int) scannedRobotX-30, (int) scannedRobotY-30, 60, 60);
}
}
///////////////////////////Painting////////////////////////
public void onHitByBullet(HitByBulletEvent e) { previousEnergy += 3 * e.getBullet().getPower(); }
public void onBulletHit(BulletHitEvent e) { previousEnergy -= 4 * e.getBullet().getPower() + Math.max(0,2 * (e.getBullet().getPower() - 1)); }
public void onHitRobot(HitRobotEvent e) { previousEnergy -= 0.6; }
public class VirtualBullet { //A class to simulate bullets.
double x, y, speed, angle;
VirtualBullet(double startX, double startY, double velocity, double trajectory) {
x = startX;
y = startY;
speed = velocity;
angle = trajectory;
}
public void tick(long ticks) { //Moves the bullet the amount it would
while(ticks > 0) {
x += Math.sin(angle)*speed;
y += Math.cos(angle)*speed;
ticks --;
}
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public double getDistance(double botX, double botY) {
return Math.sqrt(Math.pow(botX-x, 2)+Math.pow(botY-y, 2));
}
public boolean checkCollide(double botX, double botY) {
if (x < 0 || y < 0 || x > getBattleFieldWidth() || y > getBattleFieldHeight()) {
return true;
}
if(botX-18<x && botX+18>x && botY-18<y && botY+18>y) {
return true;
}
return false;
}
}
}