Difference between revisions of "Talon/source"

From Robowiki
Jump to navigation Jump to search
(move the -- to the end, gives us our 0 angle)
m (To be fair, it is already a mess (need to update Talon to the getOthers() version).)
 
(7 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
<syntaxhighlight>
 
<syntaxhighlight>
package cs;
+
package cs.sheldor;
 +
 +
import java.awt.geom.Point2D;
 +
import robocode.AdvancedRobot;
 +
import robocode.ScannedRobotEvent;
 
import robocode.util.Utils;
 
import robocode.util.Utils;
import robocode.*;
+
 
 
 
/**
 
/**
* Talon - a robot by Chase
+
* Talon - a Nano Melee robot by Chase and Sheldor
* Nano Melee bot
+
*  
*
+
* The idea is to avoid driving toward enemy robots, nearer ones are more
* The idea is to avoid driving toward enemy robots,
+
* dangerous to drive towards.
* nearer ones are more dangerous to drive towards
+
*
*/
+
* Codesize: 248
public final class Talon extends AdvancedRobot {
+
*/
    private static double[][] map = new double[10][2];
+
public class Talon extends AdvancedRobot {
    private static int n = 0;
+
private static Point2D.Double[] map = new Point2D.Double[10];
 
+
private static int n;
    public void run() {
+
        double angle, nearestDistance, bestDanger, danger, md;
+
public void run() {
  
        //Some nice red colors, cause were Talon... y
+
//Sheldor: In melee this doesn't really matter.
        setAllColors(java.awt.Color.red);
+
//setAdjustGunForRobotTurn(true);
 +
 +
//Sheldor: I'm very sorry. :(
 +
//setAllColors(Color.RED);
  
        setAdjustGunForRobotTurn(true);
+
double angle, bestDanger, danger;
  
        while(true) {
+
while(true) {
            angle = 360;
+
            nearestDistance = bestDanger = 2000;
+
//Sheldor: Slowing down before making big turns makes the wall avoidance much better.
            while(angle-- > 0) {
+
setAhead((bestDanger = angle = 360) - (5 * getTurnRemaining()));
                int t = getOthers();
+
                danger = 0;
+
while(angle-- > 0) {
                try {
+
int t;
                    while(true) {
+
setFire(t = getOthers());
                        //our most recent scans, unfortunately not enough room to project them
 
                        double[] v = map[--t];
 
                        //md here gets set to the enemies distance
 
                        if((md = v[0]) < nearestDistance) {
 
                            //nearestDistance is our nearest enemy distance
 
                            nearestDistance = md;
 
                            //set our gun to face this nearest enemy
 
                            setTurnGunRight(Utils.normalRelativeAngleDegrees(v[1] - getGunHeading()));
 
                        }
 
                        //the difference between current angle we are considering and the enemy
 
                        //multiply it by how close they are, closer is more dangerous to move towards
 
                        danger += 1/(Math.abs(Utils.normalRelativeAngleDegrees(angle-v[1]))+1) * (1/md);
 
                    }
 
                } catch(Exception e) {
 
                    //We don't have room for any kind of sanity checks, also the only way to get out of the loop without
 
                    //having to have a comparison, saves us some code size
 
                }
 
                if(danger < bestDanger) {
 
                    bestDanger = danger;
 
                    setTurnRight(Utils.normalRelativeAngleDegrees(angle - getHeading()));
 
                    setAhead(nearestDistance);
 
                }
 
            }
 
            setFire(nearestDistance);
 
            //Turning it by only 1 saves us some code size but in radians this value > PI/4 the maximum
 
            //turn the radar can turn, meaning it will cover the full arc as long as we call it every turn.
 
            turnRadarRightRadians(1);
 
        }
 
    }
 
  
    public void onScannedRobot(ScannedRobotEvent e) {
+
//Sheldor: Be careful when using counters to initialize other counters.
        try {
+
//Chase: uses less energy when there are less robots on the map, makes up for our poor targeting
            //Set the most recent scan into our map of enemies
+
//setFire(t = n);
            map[--n] = new double[]{e.getDistance(),getHeading() + e.getBearing()};
+
        } catch(Exception ex) {
+
//Sheldor: Drive toward the center of the field when the bot gets near a wall.
            //attempted to scan but it failed, set the number of enemies again
+
//This will only work properly on a 1000/1000 pixel field.
            //we miss a enemy scan but we gain some code size here
+
danger = -Math.abs(Utils.normalRelativeAngleDegrees(angle - Math.toDegrees(Math.atan2(getX() - 500, getY() - 500)))) *
            n = getOthers();
+
(table.charAt((int)getX()) + table.charAt((int)getY()));
        }
+
    }
+
try {
 +
while(true) {
 +
// our most recent scans, unfortunately not enough room to project them
 +
Point2D.Double v;
 +
// set our gun to face this nearest enemy
 +
setTurnGunRight(Utils.normalRelativeAngleDegrees((v = map[--t]).y - getGunHeading()));
 +
// the difference between current angle we are considering and the enemy multiply it
 +
// by how close they are, closer is more dangerous to move towards
 +
//Sheldor: "X / Y" has exactly the same effect as "X * (1 / Y)" but is ~3 bytes smaller.
 +
danger += 1 / (Math.abs(Utils.normalRelativeAngleDegrees(angle - v.y))) / v.x; 
 +
}
 +
} catch(Exception e) {
 +
// We don't have room for any kind of sanity checks, also the only way to get out of
 +
// the loop without having to have a comparison, saves us some code size
 +
}
 +
if(danger < bestDanger) {
 +
bestDanger = danger;
 +
setTurnRight(Utils.normalRelativeAngleDegrees(angle - getHeading()));
 +
}
 +
}
 +
// Turning it by only 1 saves us some code size but in radians this
 +
// value > PI/4 the maximum turn the radar can turn, meaning it will
 +
// cover the full arc as long as we call it every turn.
 +
turnRadarRightRadians(1);
 +
}
 +
}
 +
 +
public void onScannedRobot(ScannedRobotEvent e) {
 +
try {
 +
// Set the most recent scan into our map of enemies
 +
map[--n] = new Point2D.Double(e.getDistance(), getHeading() + e.getBearing());
 +
}
 +
catch(Exception ex) {
 +
// attempted to scan but it failed, set the number of enemies again
 +
// we miss an enemy scan but we gain some code size here
 +
n = getOthers();
 +
}
 +
}
 +
 +
static final String table = "\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064" +
 +
"\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0001" +
 +
"\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001" +
 +
"\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" +
 +
"\0\0\0\0\0\0\0\0\0\0\0\0\0\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0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" +
 +
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" +
 +
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" +
 +
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" +
 +
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\u0001\u0001\u0001\u0001\u0001" +
 +
"\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001" +
 +
"\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064" +
 +
"\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064" +
 +
"\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064";
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>

Latest revision as of 02:22, 23 February 2013

package cs.sheldor;
 
import java.awt.geom.Point2D;
import robocode.AdvancedRobot;
import robocode.ScannedRobotEvent;
import robocode.util.Utils;
 
/**
 * Talon - a Nano Melee robot by Chase and Sheldor
 * 
 * The idea is to avoid driving toward enemy robots, nearer ones are more
 * dangerous to drive towards.
 * 
 * Codesize: 248
 */
public class Talon extends AdvancedRobot {
	private static Point2D.Double[] map = new Point2D.Double[10];
	private static int n;
 
	public void run() {

		//Sheldor: In melee this doesn't really matter.
		//setAdjustGunForRobotTurn(true);
		
		//Sheldor: I'm very sorry. :(
		//setAllColors(Color.RED);

		double angle, bestDanger, danger;

		while(true) {
 
			//Sheldor: Slowing down before making big turns makes the wall avoidance much better.
			setAhead((bestDanger = angle = 360) - (5 * getTurnRemaining()));
 
			while(angle-- > 0) {
				int t;
				setFire(t = getOthers());

				//Sheldor: Be careful when using counters to initialize other counters.
				//Chase: uses less energy when there are less robots on the map, makes up for our poor targeting
				//setFire(t = n); 
 
				//Sheldor: Drive toward the center of the field when the bot gets near a wall.
				//This will only work properly on a 1000/1000 pixel field.
				danger = -Math.abs(Utils.normalRelativeAngleDegrees(angle - Math.toDegrees(Math.atan2(getX() - 500, getY() - 500)))) *
					(table.charAt((int)getX()) + table.charAt((int)getY()));
 
				try {
					while(true) {
						// our most recent scans, unfortunately not enough room to project them
						Point2D.Double v;
						// set our gun to face this nearest enemy
						setTurnGunRight(Utils.normalRelativeAngleDegrees((v = map[--t]).y - getGunHeading()));
						// the difference between current angle we are considering and the enemy multiply it
						// by how close they are, closer is more dangerous to move towards
						//Sheldor: "X / Y" has exactly the same effect as "X * (1 / Y)" but is ~3 bytes smaller.
						danger += 1 / (Math.abs(Utils.normalRelativeAngleDegrees(angle - v.y))) / v.x;  
					}
				} catch(Exception e) {
					// We don't have room for any kind of sanity checks, also the only way to get out of
					// the loop without having to have a comparison, saves us some code size
				}
				if(danger < bestDanger) {
					bestDanger = danger;
					setTurnRight(Utils.normalRelativeAngleDegrees(angle - getHeading()));
				}
			}
			// Turning it by only 1 saves us some code size but in radians this
			// value > PI/4 the maximum turn the radar can turn, meaning it will
			// cover the full arc as long as we call it every turn.
			turnRadarRightRadians(1);
		}
	}
 
	public void onScannedRobot(ScannedRobotEvent e) {
		try {
			// Set the most recent scan into our map of enemies
			map[--n] = new Point2D.Double(e.getDistance(), getHeading() + e.getBearing());
		}
		catch(Exception ex) {
			// attempted to scan but it failed, set the number of enemies again
			// we miss an enemy scan but we gain some code size here
			n = getOthers();
		}
	}
 
	static final String table = "\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064" +
			"\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0001" +
			"\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001" +
			"\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" +
			"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" +
			"\0\0\0\0\0\0\0\0\0\0\0\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0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" +
			"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" +
			"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" +
			"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\u0001\u0001\u0001\u0001\u0001" +
			"\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001" +
			"\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064" +
			"\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064" +
			"\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064\u0064";
}