[Home]Galatea

Robo Home | Changes | Preferences | AllPages

A test Robot of mine, an attempt to try and impliment a StatGun?. The first class is the gun, the second is the wave class that compliments it. The RobocodeUtils? calls are mostly availible in Hanji/RobocodeUtils, others are scattered over the wiki. I'll update this with comments soon.--Dan

 package dans;

 import java.awt.geom.Point2D;
 import java.awt.geom.Rectangle2D;
 import java.util.Vector;

 public class Galatea extends AdvancedRobot {
 static Vector WaveBin=new Vector(100);
 static double bearingOffsets[]= new double[91];
 static int current=0;
 static int connects=0;
 static int hits=0;
 static int shots=0;
 static int misses=0;
 static int hitBullet=0;
 static final double degreesToGF=1.036888887852;
	
	public void run(){
		setAdjustRadarForRobotTurn(true);
		setAdjustGunForRobotTurn(true);
		setAdjustRadarForGunTurn(true);
		setTurnRadarRight(Double.POSITIVE_INFINITY);	
		do{
                execute();
		}while(true);
	}
	public void onScannedRobot(ScannedRobotEvent e){
		double curBearing=getHeading()+e.getBearing();
		Point2D enemy = new Point2D.Double(RobocodeUtils.enemyX(curBearing,getX(),e.getDistance()) RobocodeUtils.enemyY(curBearing,getY(),e.getDistance()));
		setTurnRadarLeft(getRadarTurnRemaining());
		for(int r=0; r<=WaveBin.size(); r++){
			try{
			if(WaveBin.elementAt(r) instanceof Wave){
				Wave f=new Wave();
				f=(Wave)WaveBin.elementAt(r);
				f.Update();
				if(f.checkIntercept(enemy)==true){
					double k=Math.round(f.deltaBearing(enemy)/degreesToGF);
					if(k<0){
						k=(k*-1)*2;
					}
					bearingOffsets[(int)Math.round(k)]+=1;
					WaveBin.remove(f);
				}
			}
		}
		catch(NullPointerException s){}
		catch(ArrayIndexOutOfBoundsException vs){}
		}
		int highest=0;
		int last=0;
		int secondbest=0;
		for(int g=90; g>=0;g--){
			int a=last;
			highest=Math.max(highest,(int)bearingOffsets[g]);
			if(highest==bearingOffsets[g]){
				last=g;
				secondbest=a;
			}
		}
		if(last>44){
			last=(last*-1)/2;
		}
		if(secondbest>44){
			secondbest=(secondbest*-1)/2;
		}
		double n=e.getDistance()/11;
		double offset=(last+secondbest*degreesToGF)/2;
		out.println(offset);
		setTurnGunRightRadians(RobocodeUtils.normalizeBearing(Math.toRadians((curBearing+offset)
                -getGunHeading())));
		if(getEnergy()>Math.min(e.getEnergy()/4,3)&&getGunHeat()==0){ 
			setFire(e.getEnergy()/4);
			Wave w= new Wave(new Point2D.Double(getX(),getY()),curBearing,e.getDistance());
			WaveBin.add(w);
		}
		
	}
	public void onBulletMissed(BulletMissedEvent e){
		misses++;
		shots++;
	}
	public void onBulletHitBullet(BulletHitBulletEvent e){
		misses++;
		hitBullet++;
		shots++;
	} 
	public void onDeath(DeathEvent e){
		double hitPercent=Math.round((hits/shots)*100);	
		double bulletToBulletPer=Math.round((hitBullet/shots)*100);
		double missPercent=Math.round((misses/shots)*100);
		out.println("We shot "+shots+" times.");
		out.println("We hit "+hits+" times.");
		out.println("We missed "+misses+" times.");
		out.println("We hit other bullets "+hitBullet+" times.");
		out.println("We hit " + hitPercent+" % of the time.");
		out.println("We missed " + missPercent+" % of the time.");
		out.println("We hit other bullets "+bulletToBulletPer+" % of the time.");
		out.println("We had a "+hits/misses+" hit/miss ratio.");
	}
	public void onWin(WinEvent e){
		double hitPercent=Math.round((hits/shots)*100);	
		double bulletToBulletPer=Math.round((hitBullet/shots)*100);
		double missPercent=Math.round((misses/shots)*100);
		out.println("We shot "+shots+" times.");
		out.println("We hit "+hits+" times.");
		out.println("We missed "+misses+" times.");
		out.println("We hit other bullets "+hitBullet+" times.");
		out.println("We hit " + hitPercent+" % of the time.");
		out.println("We missed " + missPercent+" % of the time.");
		out.println("We hit other bullets "+bulletToBulletPer+" % of the time.");
		out.println("We had a "+hits/misses+" hit/miss ratio.");
		
	}
 }

 package dans;

 import java.awt.geom.Point2D;
 public class Wave {
	Point2D startPos= new Point2D.Double();
	double tarDistance;
	double tarBearing;
	double radius=0;
	public Wave(){
	}
	public Wave(Point2D start, double absBearing, double distance){
		this.startPos=start;
		this.tarBearing=absBearing;
		this.radius+=11;
		this.tarDistance=distance;	
	}
	public void Update(){
		this.radius+=11;
	}
	public boolean checkIntercept(Point2D tarPoint){
		if(this.radius>=tarPoint.distance(this.startPos)){
			return true;
		}
		else{
			return false;
		}
	}
	public double deltaBearing(Point2D enemy){
		double a= this.tarBearing-Math.toDegrees(RobocodeUtils.getBearing(startPos,enemy));
		return a;
	}
	public double getRadius(){
		return this.radius;
	}
	public double getX(){
		return this.startPos.getX();
	}
	public double getY(){
		return this.startPos.getY();
	}
	public double getDistance(){
		return this.tarDistance;
	}
 }

Questions, Comments?

Robo Home | Changes | Preferences | AllPages
Edit text of this page | View other revisions
Last edited February 6, 2004 2:55 EST by Dan (diff)
Search: