Difference between revisions of "GravityWave/Code"

From Robowiki
Jump to navigation Jump to search
m (→‎Scan.java: bug fix)
m (Using <syntaxhighlight>.)
 
(One intermediate revision by one other user not shown)
Line 10: Line 10:
 
This is GravityWave's Scan class. It's still being expanded and debugged, but I feel it's good enough to release so newer people to Robocode have a nice Scan class with little effort on using it.
 
This is GravityWave's Scan class. It's still being expanded and debugged, but I feel it's good enough to release so newer people to Robocode have a nice Scan class with little effort on using it.
 
===Code===
 
===Code===
<pre>
+
<syntaxhighlight>
package hackhalotwo.utils;
+
package Resources.utils;
  
 
import robocode.*; //Robocode stuff
 
import robocode.*; //Robocode stuff
Line 18: Line 18:
 
import java.util.ArrayList; //for ArrayLists
 
import java.util.ArrayList; //for ArrayLists
  
//Version 0.5.5
+
//Version 0.6
  
 
public class Scan {
 
public class Scan {
Line 84: Line 84:
 
//Functions
 
//Functions
 
public static void init(AdvancedRobot r) {
 
public static void init(AdvancedRobot r) {
 
 
lastFiveScans = new ArrayList<Scanner>(); //set up the scan retention
 
lastFiveScans = new ArrayList<Scanner>(); //set up the scan retention
 
_robot = r; //set up the executing robots varibles
 
_robot = r; //set up the executing robots varibles
Line 155: Line 154:
 
{ return Math.PI/720d*(40d - 3d*Math.abs(_velocity)); }
 
{ return Math.PI/720d*(40d - 3d*Math.abs(_velocity)); }
  
// public final double calcHeading(double heading, double _heading)
+
public final double calcHeading(int history) {
// { return (heading - _heading); }
+
if(history < (lastFiveScans.size()-1)) {
 +
Scanner s = lastFiveScans.get(history);
 +
return (_heading - s.heading);
 +
} else {
 +
System.out.println("Scan.Java :: ERROR: History Out of Bounds\n"+
 +
  "Scan.Java :: Defaulting to earliest scan");
 +
Scanner s = lastFiveScans.get(lastFiveScans.size()-1);
 +
return (_heading - s.heading);
 +
}
 +
}
 
 
 
public final double calcRelativeHeading()
 
public final double calcRelativeHeading()
 
{ return (_heading - (_bearing + _robot.getHeading())); }
 
{ return (_heading - (_bearing + _robot.getHeading())); }
}</pre>
+
}</syntaxhighlight>
 
===Features===
 
===Features===
(Version 0.5.5)
+
(Version 0.6)
 
* Gun Heat Tracker using a simple and quick math equation
 
* Gun Heat Tracker using a simple and quick math equation
 
* Internal ''Scanner'' Class to store scans
 
* Internal ''Scanner'' Class to store scans
 
* Scanner Populate function to ease use
 
* Scanner Populate function to ease use
 
* Scan Retainer (the last five scans)
 
* Scan Retainer (the last five scans)
 +
* calcHeading() function biased on last scans (currently lastFiveScans)
 +
** 1 = Last Scan
 +
** (lastFiveScans-1) = Earliest scan in ArrayList
 
* Fast Execution Time
 
* Fast Execution Time
 
* Plug and Play
 
* Plug and Play
 +
===Drawbacks===
 +
* Only 1v1. After all the bugs and features are worked out, I will add melee support.

Latest revision as of 09:28, 1 July 2010

GravityWave pages:
GravityWaveVersions - Code - Tests

Scan.java

This is GravityWave's Scan class. It's still being expanded and debugged, but I feel it's good enough to release so newer people to Robocode have a nice Scan class with little effort on using it.

Code

package Resources.utils;

import robocode.*; //Robocode stuff
import java.awt.geom.Point2D; //for Point2D stuff
import java.lang.System; //for outputing debug text
import java.util.ArrayList; //for ArrayLists

//Version 0.6

public class Scan {
	//Debug stuff
	private static final boolean debug = false; //set to true for debug text
	
	//Set up the main varibles for the class
	public static String _name;
	public static double _bearing;
	public static double _bearingR;
	public static double _distance;
	public static double _energy;
	public static double _heading;
	public static double _headingR;
	public static double _velocity;
	public static int _tick; //last scan tick
	
	//Scan Retention
	public static ArrayList<Scanner> lastFiveScans;
	
	//Enemy Stuff
	public static long bulletTick; //tick the bullet was fired on
	public static int nextFire; //when the enemy could fire again
	public static boolean canFire = false; //Can the Enemy fire?
	
	//Your Robot
	public static AdvancedRobot _robot; //Your robot
	
	//Location Stuff
	public static Point2D.Double _location; //Last Scan Location
	public static Point2D.Double _enemyLocation; //Enemy Location
	
	//Helper Varibles
	public static double absBearing; //Absolute Bearing
	public static double latVelocity; //Lateral Velocity
	public static double bulletPower; //Bullet Power and Distance Traveled
	
	//Inner class designed to be a placeholder for storing scans
	public static class Scanner {
		public String name;
		public double bearing, bearingR, distance, energy, heading, headingR, velocity;
		public int tick;
		
		protected Scanner() { }
		
	}
	
	//Helper classes for the Inner Class
	public static Scanner populate() {
		Scanner s = new Scanner();
		s.name = _name;
		s.bearing = _bearing;
		s.bearingR = _bearingR;
		s.distance = _distance;
		s.energy = _energy;
		s.heading = _heading;
		s.headingR = _headingR;
		s.velocity = _velocity;
		//set the tick the scan was registered on
		s.tick = (int)(_robot.getTime());
		
		return s;
	}
	
	//Functions
	public static void init(AdvancedRobot r) {
		lastFiveScans = new ArrayList<Scanner>(); //set up the scan retention
		_robot = r; //set up the executing robots varibles
		_tick = (int)(_robot.getTime()); //set the time to correctly reduce ticks
		nextFire = 29; //since guns are hot at the begining of the round for 30 ticks (minus 1 for safety)
		canFire = false; //make sure it's set correctly
		//debug stuff
		if(debug) { System.out.println("Scan.class :: Scan.init(AdvancedRobot) called"); }
	}
	
	public static void update(AdvancedRobot r, ScannedRobotEvent e) {
		//debug stuff
		if(debug) { System.out.println("Scan.class :: Scan.update(AdvancedRobot, ScannedRobotEvent) called"); }
		
		//update the varibles
		_robot = r;
		_name = e.getName();
		_bearing = e.getBearing();
		_bearingR = e.getBearingRadians();
		_distance = e.getDistance();
		_energy = e.getEnergy();
		_heading = e.getHeading();
		_headingR = e.getHeadingRadians();
		_velocity = e.getVelocity();
		
		//get the current tick
		int tick = (int)(_robot.getTime());
		
		//check to see if the enemy can fire
		if(!canFire) { //he can't fire
			if(nextFire > 0) { //check to see how many ticks to take away
				nextFire -= (tick - _tick); //correctly reduce the number of ticks remaining
				if(nextFire < 0) { nextFire = 0; } //just in case
				if(debug) { System.out.println("Scan.class :: Time till enemy can fire again: "+ nextFire); }
			} else { canFire = true; }
		}
		
		//update the scan tick
		_tick = tick;
		
		//Save this scan and trim off old ones
		lastFiveScans.add(0, populate());
		
		if(lastFiveScans.size() > 5) { lastFiveScans.remove(lastFiveScans.size() - 1); }
		
		//set up some of the advanced functions
		//updateAdv();
		
	}
	
	public static void updateAdv() {
		
	}
	
	public static void bulletFired(double power) {
		if(debug) { System.out.println("Scan.class :: Scan.bulletFired(time, power) called"); }
		bulletTick = _robot.getTime();
		//correctly calculate the number of ticks until the next fire
		nextFire = (int)(((1D + power / 5D)*10)-1); //include the tick you were hit on
		canFire = false; //Set the boolean
		if(debug) { System.out.println("Scan.class :: "+nextFire+" ticks until enemy can fire again"); }
	}

	public static boolean canFire() {
		if(canFire) { return true; }
		else { return false; }
	}
	
	public static double calcTurn()
		{ return Math.PI/720d*(40d - 3d*Math.abs(_velocity)); }

	public final double calcHeading(int history) {
		if(history < (lastFiveScans.size()-1)) {
			Scanner s = lastFiveScans.get(history);
			return (_heading - s.heading);
		} else {
			System.out.println("Scan.Java :: ERROR: History Out of Bounds\n"+
							   "Scan.Java :: Defaulting to earliest scan");
			Scanner s = lastFiveScans.get(lastFiveScans.size()-1);
			return (_heading - s.heading);
		}
	}
	
	public final double calcRelativeHeading()
		{ return (_heading - (_bearing + _robot.getHeading())); }		
}

Features

(Version 0.6)

  • Gun Heat Tracker using a simple and quick math equation
  • Internal Scanner Class to store scans
  • Scanner Populate function to ease use
  • Scan Retainer (the last five scans)
  • calcHeading() function biased on last scans (currently lastFiveScans)
    • 1 = Last Scan
    • (lastFiveScans-1) = Earliest scan in ArrayList
  • Fast Execution Time
  • Plug and Play

Drawbacks

  • Only 1v1. After all the bugs and features are worked out, I will add melee support.