[Home]AntiPatternMatcherChallenge2K7/WaylanderGun

Robo Home | AntiPatternMatcherChallenge2K7 | Changes | Preferences | AllPages

 

  import robocode.*;
   import robocode.util.*;
   import java.awt.geom.*;
 
    public class ChallengerBot extends AdvancedRobot{
   
      WaylanderGun waylanderGun;
   
       public void run(){
         waylanderGun = new WaylanderGun(this);
      
      //your code here
      
      
      }
   
       public void onScannedRobot(ScannedRobotEvent e){
      
      //your code here
      
         waylanderGun.onScannedRobot(e);
      }
   
   
   }

    class WaylanderGun
   {
   
      final static double angleScale = 24;
      final static double velocityScale = 1;
      static double lastEnemyHeading;
     
      static boolean firstScan;
      static StringBuilder data = new StringBuilder();
      AdvancedRobot bot;
     
       public WaylanderGun(AdvancedRobot bot){
         this.bot = bot;
         firstScan = true;
         try{
            data.delete(60000, 80000);
         }
             catch(StringIndexOutOfBoundsException e){}
      }
   	
   
       public void onScannedRobot(ScannedRobotEvent e) {
      
         double headingRadians;
         double eDistance ;
         double eHeadingRadians = e.getHeadingRadians();
         double absbearing=e.getBearingRadians()+ (headingRadians = bot.getHeadingRadians());
         
         boolean rammer = (eDistance = e.getDistance()) < 100 
            || bot.getTime() < 20;
        
         Rectangle2D.Double field = new Rectangle2D.Double(17,17,766,566);
            
      		
         if(!firstScan)
            data.insert(0,(char)((eHeadingRadians - lastEnemyHeading )*angleScale))
               .insert(0,(char)(e.getVelocity()*velocityScale));
         
        
         int keyLength  = Math.min(data.length(), Math.min(Math.max(2,(int)bot.getTime()*2 - 8), 256));
         
         int index = -1;
         do{
         
            index = data.indexOf(data.substring(0, keyLength),(int)eDistance/11)/2;
            
         }while(index <= 0 && (keyLength/=2) > 1);
         
         
         double bulletPower = rammer?3:Math.min(2,Math.min(bot.getEnergy()/16, e.getEnergy()/2));
         
           
         double eX=eDistance*Math.sin(absbearing);
         double eY=eDistance*Math.cos(absbearing);
         
         double db=0;
         double ww=eHeadingRadians; 
         double speed = e.getVelocity();
         double w = eHeadingRadians - lastEnemyHeading;
         do
         {
            if( index > 1 ){
               speed = (short)data.charAt(index*2 );
               w = ((short)data.charAt(index--*2 - 1))/angleScale;    
            }
         
         }while ((db+=(20-3*bulletPower))< Point2D.distance(0,0,eX+= (speed*Math.sin(ww+=w)),eY+= (speed*Math.cos(ww))) 
         && field.contains(eX + bot.getX() , eY + bot.getY()));         
         
         
         bot.setTurnGunRightRadians(Utils.normalRelativeAngle(Math.atan2(eX,eY) - bot.getGunHeadingRadians()));
         bot.setFire(bulletPower);
         
         bot.setTurnRadarRightRadians(Math.sin(absbearing - bot.getRadarHeadingRadians())*2);
               
         lastEnemyHeading=eHeadingRadians;
         firstScan = false;
      
      }
           
   
   
   }

Robo Home | AntiPatternMatcherChallenge2K7 | Changes | Preferences | AllPages
Edit text of this page | View other revisions
Last edited October 14, 2007 17:05 EST by Skilgannon (diff)
Search: