import robocode.*;
import java.awt.geom.*;
import java.awt.Graphics2D;
import java.awt.Color;
public class Knowledge extends AdvancedRobot
{
static StringBuffer headDelta;
static StringBuffer velocity;
double lastHeading=0;
Point2D.Double predictedPos=new Point2D.Double();
/**
* run: Knowledge's default behavior
*/
public void run() {
if(getRoundNum()==0){
headDelta=new StringBuffer();
velocity=new StringBuffer();
}
setTurnRadarRightRadians(Double.POSITIVE_INFINITY);
// After trying out your robot, try uncommenting the import at the top,
// and the next line:
setColors(Color.WHITE,Color.BLACK,new Color(0,255,0));
setAdjustGunForRobotTurn(true);
setAdjustRadarForGunTurn(true);
doNothing();
}
public void onScannedRobot(ScannedRobotEvent e) {
double myX=getX();
double myY=getY();
double myHeading=getHeadingRadians();
double myGunHeading=getGunHeadingRadians();
headDelta.append( (char)(int)(e.getHeadingRadians()-lastHeading) );
velocity.append( (char)(int)(e.getVelocity()) );
lastHeading=e.getHeadingRadians();
if(headDelta.length()>10 && getGunHeat()/getGunCoolingRate()<5){
double bulletPower=3;
String currentH=headDelta.toString().substring(headDelta.length()-7);
String currentV=velocity.toString().substring(velocity.length()-7);
int startIndex=-1;
for(int i=0;i+7<=headDelta.length();i++){
if(headDelta.toString().substring(i,i+7).equals(currentH) &&
velocity.toString().substring(i,i+7).equals(currentV)){
startIndex=i+7;
break;
}
}
if(startIndex==-1){
setTurnGunRightRadians(normalRelativeAngle(e.getBearingRadians()+myHeading-myGunHeading));
}else{
double bD=0;
double eX=myX+Math.sin(e.getBearingRadians()+myHeading)*e.getDistance();
double eY=myY+Math.cos(e.getBearingRadians()+myHeading)*e.getDistance();
double eH=e.getHeadingRadians();
int index=startIndex;
double battleFieldWidth=getBattleFieldWidth();
double battleFieldHeight=getBattleFieldHeight();
while((++index)<headDelta.length()-1){
eH+=((double)(int)headDelta.toString().charAt(index));
double dX=Math.sin(eH) * ((double)(int)velocity.toString().charAt(index));
double dY=Math.cos(eH) * ((double)(int)velocity.toString().charAt(index));
eX+=dX;
eY+=dY;
if(distance(myX,myY,eX,eY)<=bD){
setTurnGunRightRadians(normalRelativeAngle(Math.atan2(eX-myX,eY-myY)-myGunHeading));
out.println(":)");
break;
}
bD+=20-(3*bulletPower);
}
predictedPos.setLocation(eX,eY);
}
if(getGunHeat()==0d){
setFire(bulletPower);
}
}else{
setTurnGunRightRadians(normalRelativeAngle(e.getBearingRadians()+myHeading-myGunHeading));
}
setTurnRadarRightRadians(normalRelativeAngle(e.getBearingRadians()+myHeading-getRadarHeadingRadians())*2);
scan();
execute();
}
public void onPaint(Graphics2D g){
g.setColor(Color.GREEN);
g.fillOval((int)predictedPos.getX(),(int)(getBattleFieldHeight()-predictedPos.getY()),10,10);
}
public double normalRelativeAngle(double angle) {
//taken from OddBot
return ((angle + 15.7079633) % 6.2831853) - Math.PI;
}
public double distance( double x1,double y1, double x2,double y2 ) {
double xo = x2-x1;
double yo = y2-y1;
double h = Math.sqrt( xo*xo + yo*yo );
return h;
}
}
The onPaint and out.println(":)"); are for debugging. --
Starrynte