Difference between revisions of "Talon/source"

From Robowiki
Jump to navigation Jump to search
(move the -- to the end, gives us our 0 angle)
(I now have 35 bytes to squeeze wall avoidance in. Still no idea how. :))
Line 1: Line 1:
 
<syntaxhighlight>
 
<syntaxhighlight>
 
package cs;
 
package cs;
 +
 +
import java.awt.geom.Point2D;
 +
import robocode.AdvancedRobot;
 +
import robocode.ScannedRobotEvent;
 
import robocode.util.Utils;
 
import robocode.util.Utils;
import robocode.*;
 
  
 
/**
 
/**
Line 10: Line 13:
 
* The idea is to avoid driving toward enemy robots,
 
* The idea is to avoid driving toward enemy robots,
 
* nearer ones are more dangerous to drive towards
 
* nearer ones are more dangerous to drive towards
 +
*
 +
* Codesize: 215
 
*/
 
*/
public final class Talon extends AdvancedRobot {
+
public class Talon extends AdvancedRobot {
    private static double[][] map = new double[10][2];
+
//215 is normal
 +
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;
 
         double angle, nearestDistance, bestDanger, danger, md;
 
        //Some nice red colors, cause were Talon... y
 
        setAllColors(java.awt.Color.red);
 
 
        setAdjustGunForRobotTurn(true);
 
 
 
         while(true) {
 
         while(true) {
            angle = 360;
+
        setFire(angle = 360);
 
             nearestDistance = bestDanger = 2000;
 
             nearestDistance = bestDanger = 2000;
 
             while(angle-- > 0) {
 
             while(angle-- > 0) {
Line 32: Line 32:
 
                     while(true) {
 
                     while(true) {
 
                         //our most recent scans, unfortunately not enough room to project them
 
                         //our most recent scans, unfortunately not enough room to project them
                         double[] v = map[--t];
+
                         Point2D.Double v = map[--t];
 
                         //md here gets set to the enemies distance
 
                         //md here gets set to the enemies distance
                         if((md = v[0]) < nearestDistance) {
+
                         if((md = v.x) < nearestDistance) {
 
                             //nearestDistance is our nearest enemy distance
 
                             //nearestDistance is our nearest enemy distance
 
                             nearestDistance = md;
 
                             nearestDistance = md;
 
                             //set our gun to face this nearest enemy
 
                             //set our gun to face this nearest enemy
                             setTurnGunRight(Utils.normalRelativeAngleDegrees(v[1] - getGunHeading()));
+
                             setTurnGunRight(Utils.normalRelativeAngleDegrees(v.y - getGunHeading()));
 
                         }
 
                         }
 
                         //the difference between current angle we are considering and the enemy
 
                         //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
 
                         //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);
+
                         danger += 1/(Math.abs(Utils.normalRelativeAngleDegrees(angle-v.y))+1) * (1/md);
 
                     }
 
                     }
 
                 } catch(Exception e) {
 
                 } catch(Exception e) {
Line 54: Line 54:
 
                 }
 
                 }
 
             }
 
             }
            setFire(nearestDistance);
 
 
             //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 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.
 
             //turn the radar can turn, meaning it will cover the full arc as long as we call it every turn.
Line 60: Line 59:
 
         }
 
         }
 
     }
 
     }
 
+
 
     public void onScannedRobot(ScannedRobotEvent e) {
 
     public void onScannedRobot(ScannedRobotEvent e) {
 
         try {
 
         try {
 
             //Set the most recent scan into our map of enemies
 
             //Set the most recent scan into our map of enemies
             map[--n] = new double[]{e.getDistance(),getHeading() + e.getBearing()};
+
        map[--n] = new Point2D.Double(e.getDistance(),getHeading() + e.getBearing());
 +
             //map[--n] = new double[]{e.getDistance(),getHeading() + e.getBearing()};
 
         } catch(Exception ex) {
 
         } catch(Exception ex) {
 
             //attempted to scan but it failed, set the number of enemies again
 
             //attempted to scan but it failed, set the number of enemies again

Revision as of 21:22, 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: 215
*/
public class Talon extends AdvancedRobot {
	//215 is normal
	private static Point2D.Double[] map = new Point2D.Double[10];
    private static int n = 0;
 
    public void run() {
        double angle, nearestDistance, bestDanger, danger, md;
        while(true) {
        	setFire(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
                        Point2D.Double v = map[--t];
                        //md here gets set to the enemies distance
                        if((md = v.x) < nearestDistance) {
                            //nearestDistance is our nearest enemy distance
                            nearestDistance = md;
                            //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/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);
                }
            }
            //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();
        }
    }
}