Difference between revisions of "Talon/source"

From Robowiki
Jump to navigation Jump to search
m (2 more bytes squeezed)
(move the -- to the end, gives us our 0 angle)
Line 26: Line 26:
 
             angle = 360;
 
             angle = 360;
 
             nearestDistance = bestDanger = 2000;
 
             nearestDistance = bestDanger = 2000;
             while(--angle > 0) {
+
             while(angle-- > 0) {
 
                 int t = getOthers();
 
                 int t = getOthers();
 
                 danger = 0;
 
                 danger = 0;

Revision as of 20:49, 2 July 2011

package cs;
import robocode.util.Utils;
import robocode.*;

/**
* 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
*/
public final class Talon extends AdvancedRobot {
    private static double[][] map = new double[10][2];
    private static int n = 0;

    public void run() {
        double angle, nearestDistance, bestDanger, danger, md;

        //Some nice red colors, cause were Talon... y
        setAllColors(java.awt.Color.red);

        setAdjustGunForRobotTurn(true);

        while(true) {
            angle = 360;
            nearestDistance = bestDanger = 2000;
            while(angle-- > 0) {
                int t = getOthers();
                danger = 0;
                try {
                    while(true) {
                        //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) {
        try {
            //Set the most recent scan into our map of enemies
            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();
        }
    }
}