Difference between revisions of "BulletSimBot/Current Code"

From Robowiki
Jump to navigation Jump to search
(Finally!)
m (add category)
Line 220: Line 220:
 
 
 
}</pre>
 
}</pre>
 +
 +
[[Category:Source Code]]

Revision as of 15:21, 14 August 2009

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;
		}
	}
			
}