Difference between revisions of "Talon/source"

From Robowiki
Jump to navigation Jump to search
(I now have 35 bytes to squeeze wall avoidance in. Still no idea how. :))
m (Made a few concessions. Now have 50 bytes to work with.)
Line 8: Line 8:
  
 
/**
 
/**
* Talon - a robot by Chase
+
* Talon - a robot by Chase Nano Melee bot
* 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: 200
* Codesize: 215
+
*/
*/
 
 
public class Talon extends AdvancedRobot {
 
public class Talon extends AdvancedRobot {
//215 is normal
 
 
private static Point2D.Double[] map = new Point2D.Double[10];
 
private static Point2D.Double[] map = new Point2D.Double[10];
    private static int n = 0;
+
private static int n = 0;
+
 
    public void run() {
+
public void run() {
        double angle, nearestDistance, bestDanger, danger, md;
+
setAdjustGunForRobotTurn(true);
        while(true) {
+
double angle, bestDanger, danger;
        setFire(angle = 360);
+
while(true) {
            nearestDistance = bestDanger = 2000;
+
setAhead(angle = 360);
            while(angle-- > 0) {
+
setFire(bestDanger = angle);
                int t = getOthers();
+
while(angle-- > 0) {
                danger = 0;
+
int t = getOthers();
                try {
+
danger = 0;
                    while(true) {
+
try {
                        //our most recent scans, unfortunately not enough room to project them
+
while(true) {
                        Point2D.Double v = map[--t];
+
// our most recent scans, unfortunately not enough room
                        //md here gets set to the enemies distance
+
// to project them
                        if((md = v.x) < nearestDistance) {
+
Point2D.Double v = map[--t];
                            //nearestDistance is our nearest enemy distance
+
// set our gun to face this nearest enemy
                            nearestDistance = md;
+
setTurnGunRight(Utils.normalRelativeAngleDegrees(v.y - getGunHeading()));
                            //set our gun to face this nearest enemy
+
// the difference between current angle we are
                            setTurnGunRight(Utils.normalRelativeAngleDegrees(v.y - getGunHeading()));
+
// considering and the enemy
                        }
+
// multiply it by how close they are, closer is more
                        //the difference between current angle we are considering and the enemy
+
// dangerous to move towards
                        //multiply it by how close they are, closer is more dangerous to move towards
+
danger += 1 / (Math.abs(Utils.normalRelativeAngleDegrees(angle - v.y)) + 1) * (1 / v.x);
                        danger += 1/(Math.abs(Utils.normalRelativeAngleDegrees(angle-v.y))+1) * (1/md);
+
}
                    }
+
}
                } catch(Exception e) {
+
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
+
// We don't have room for any kind of sanity checks, also
                    //having to have a comparison, saves us some code size
+
// 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;
+
if(danger < bestDanger) {
                    setTurnRight(Utils.normalRelativeAngleDegrees(angle - getHeading()));
+
bestDanger = danger;
                    setAhead(nearestDistance);
+
setTurnRight(Utils.normalRelativeAngleDegrees(angle - getHeading()));
                }
+
}
            }
+
}
            //Turning it by only 1 saves us some code size but in radians this value > PI/4 the maximum
+
// Turning it by only 1 saves us some code size but in radians this
            //turn the radar can turn, meaning it will cover the full arc as long as we call it every turn.
+
// value > PI/4 the maximum
            turnRadarRightRadians(1);
+
// 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
+
public void onScannedRobot(ScannedRobotEvent e) {
        map[--n] = new Point2D.Double(e.getDistance(),getHeading() + e.getBearing());
+
try {
            //map[--n] = new double[]{e.getDistance(),getHeading() + e.getBearing()};
+
// Set the most recent scan into our map of enemies
        } catch(Exception ex) {
+
map[--n] = new Point2D.Double(e.getDistance(), getHeading() + e.getBearing());
            //attempted to scan but it failed, set the number of enemies again
+
// map[--n] = new double[]{e.getDistance(),getHeading() +
            //we miss a enemy scan but we gain some code size here
+
// e.getBearing()};
            n = getOthers();
+
}
        }
+
catch(Exception ex) {
    }
+
// attempted to scan but it failed, set the number of enemies again
 +
// we miss a enemy scan but we gain some code size here
 +
n = getOthers();
 +
}
 +
}
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 23:03, 7 February 2013

package cs;

import java.awt.geom.Point2D;
import robocode.AdvancedRobot;
import robocode.ScannedRobotEvent;
import robocode.util.Utils;

/**
 * Talon - a robot by Chase Nano Melee bot
 * 
 * The idea is to avoid driving toward enemy robots, nearer ones are more
 * dangerous to drive towards.
 * 
 * Codesize: 200
 */
public class Talon extends AdvancedRobot {
	private static Point2D.Double[] map = new Point2D.Double[10];
	private static int n = 0;

	public void run() {
		setAdjustGunForRobotTurn(true);
		double angle, bestDanger, danger;
		while(true) {
			setAhead(angle = 360);
			setFire(bestDanger = angle);
			while(angle-- > 0) {
				int t = getOthers();
				danger = 0;
				try {
					while(true) {
						// our most recent scans, unfortunately not enough room
						// to project them
						Point2D.Double v = map[--t];
						// set our gun to face this nearest enemy
						setTurnGunRight(Utils.normalRelativeAngleDegrees(v.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
						danger += 1 / (Math.abs(Utils.normalRelativeAngleDegrees(angle - v.y)) + 1) * (1 / 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());
			// map[--n] = new double[]{e.getDistance(),getHeading() +
			// e.getBearing()};
		}
		catch(Exception ex) {
			// attempted to scan but it failed, set the number of enemies again
			// we miss a enemy scan but we gain some code size here
			n = getOthers();
		}
	}
}