Difference between revisions of "GravityWave/Code"
Jump to navigation
Jump to search
J Litewski (talk | contribs) m (added navbox) |
RednaxelaBot (talk | contribs) m (Using <syntaxhighlight>.) |
||
(3 intermediate revisions by one other user not shown) | |||
Line 9: | Line 9: | ||
==Scan.java== | ==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. | 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 | + | <syntaxhighlight> |
+ | package Resources.utils; | ||
import robocode.*; //Robocode stuff | import robocode.*; //Robocode stuff | ||
import java.awt.geom.Point2D; //for Point2D stuff | import java.awt.geom.Point2D; //for Point2D stuff | ||
import java.lang.System; //for outputing debug text | import java.lang.System; //for outputing debug text | ||
+ | import java.util.ArrayList; //for ArrayLists | ||
− | //Version 0. | + | //Version 0.6 |
public class Scan { | public class Scan { | ||
//Debug stuff | //Debug stuff | ||
− | private static final boolean debug = | + | private static final boolean debug = false; //set to true for debug text |
//Set up the main varibles for the class | //Set up the main varibles for the class | ||
Line 32: | Line 34: | ||
public static double _velocity; | public static double _velocity; | ||
public static int _tick; //last scan tick | public static int _tick; //last scan tick | ||
+ | |||
+ | //Scan Retention | ||
+ | public static ArrayList<Scanner> lastFiveScans; | ||
//Enemy Stuff | //Enemy Stuff | ||
Line 51: | Line 56: | ||
//Inner class designed to be a placeholder for storing scans | //Inner class designed to be a placeholder for storing scans | ||
− | public class Scanner { | + | public static class Scanner { |
public String name; | public String name; | ||
− | public double bearing, bearingR, distance, energy, heading, headingR; | + | public double bearing, bearingR, distance, energy, heading, headingR, velocity; |
− | public | + | public int tick; |
protected Scanner() { } | 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 | //Functions | ||
public static void init(AdvancedRobot r) { | public static void init(AdvancedRobot r) { | ||
− | + | lastFiveScans = new ArrayList<Scanner>(); //set up the scan retention | |
− | _robot = r; | + | _robot = r; //set up the executing robots varibles |
_tick = (int)(_robot.getTime()); //set the time to correctly reduce ticks | _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) | nextFire = 29; //since guns are hot at the begining of the round for 30 ticks (minus 1 for safety) | ||
Line 100: | Line 122: | ||
//update the scan tick | //update the scan tick | ||
_tick = 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 | //set up some of the advanced functions | ||
Line 113: | Line 140: | ||
if(debug) { System.out.println("Scan.class :: Scan.bulletFired(time, power) called"); } | if(debug) { System.out.println("Scan.class :: Scan.bulletFired(time, power) called"); } | ||
bulletTick = _robot.getTime(); | bulletTick = _robot.getTime(); | ||
− | //calculate the number of ticks until the next fire | + | //correctly calculate the number of ticks until the next fire |
− | nextFire = (int)((1D + power / 5D)*10); | + | nextFire = (int)(((1D + power / 5D)*10)-1); //include the tick you were hit on |
canFire = false; //Set the boolean | canFire = false; //Set the boolean | ||
if(debug) { System.out.println("Scan.class :: "+nextFire+" ticks until enemy can fire again"); } | if(debug) { System.out.println("Scan.class :: "+nextFire+" ticks until enemy can fire again"); } | ||
Line 127: | Line 154: | ||
{ return Math.PI/720d*(40d - 3d*Math.abs(_velocity)); } | { 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() | public final double calcRelativeHeading() | ||
{ return (_heading - (_bearing + _robot.getHeading())); } | { return (_heading - (_bearing + _robot.getHeading())); } | ||
− | } | + | }</syntaxhighlight> |
− | </ | ||
===Features=== | ===Features=== | ||
− | (Version 0. | + | (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 | ||
+ | * 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:
- GravityWave - Versions - Code - Tests
Contents
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.