From Robowiki
Jump to navigation Jump to search
GravityWave pages:
GravityWaveVersions - Code - Tests

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.


package hackhalotwo.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.5.5

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(); = _name;
		s.bearing = _bearing;
		s.bearingR = _bearingR;
		s.distance = _distance; = _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;
	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
	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(double heading, double _heading)
//		{ return (heading - _heading); }
	public final double calcRelativeHeading()
		{ return (_heading - (_bearing + _robot.getHeading())); }		


(Version 0.5.5)

  • 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)
  • Fast Execution Time
  • Plug and Play