SHARE
TWEET

cymba v0.6

fire219 Jan 5th, 2015 163 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package fire219.cymba;
  2. import robocode.*;
  3. import robocode.util.*;
  4. //import java.awt.Color;
  5.  
  6. /**
  7.  * Cymba - a robot by fire219
  8.  * version 1-5-15
  9.  * Proving simple is better since 2014
  10.  */
  11. public class Cymba extends AdvancedRobot
  12. {
  13.         int moveDirection = 1; //-1 = left, 1 = right  
  14.         int turnmodifier = 0;
  15.         double targetdist1 = 0;
  16.         double targetingmod = 0;
  17.         double nprand = 0;
  18.         double lastenergy = 0;
  19.     int fliptimer = 0;
  20.        
  21.         public void run() {
  22.  
  23.            
  24.            setAdjustRadarForRobotTurn(true);
  25.           // setAdjustRadarForGunTurn(true);
  26.  
  27.            do {
  28.                // ...
  29.               // Turn the radar if we have no more turn, starts it if it stops and at the start of round
  30.        
  31.               if ( getRadarTurnRemaining() == 0.0 )
  32.                   setTurnRadarRightRadians( Double.POSITIVE_INFINITY );
  33.                   execute();
  34.                   setAhead(150 * moveDirection);
  35.                   if (Math.random() > .9) {
  36.                         out.println("Changing dir");
  37.                         moveDirection = moveDirection * -1;    
  38.                         }
  39.                   if (getX() < 20 || getY() < 20 || getX() > getBattleFieldWidth()-20 || getY() > getBattleFieldHeight()-20) {
  40.             moveDirection *= -1;       
  41.                         }      
  42.                   scan();
  43.          } while ( true );
  44.        
  45.           // ...
  46.         }
  47.  
  48.         public void onScannedRobot(ScannedRobotEvent e) {
  49.            // ...
  50.  
  51.            // Absolute angle towards target
  52.            double angleToEnemy = getHeadingRadians() + e.getBearingRadians();
  53.  
  54.           // Subtract current radar heading to get the turn required to face the enemy, be sure it is normalized
  55.            double radarTurn = Utils.normalRelativeAngle( angleToEnemy - getRadarHeadingRadians() );
  56.  
  57.             // Distance we want to scan from middle of enemy to either side
  58.            // The 36.0 is how many units from the center of the enemy robot it scans.
  59.            double extraTurn = Math.min( Math.atan( 72.0 / e.getDistance() ), Rules.RADAR_TURN_RATE_RADIANS );
  60.  
  61.           // Adjust the radar turn so it goes that much further in the direction it is going to turn
  62.            // Basically if we were going to turn it left, turn it even more left, if right, turn more right.
  63.            // This allows us to overshoot our enemy so that we get a good sweep that will not slip.
  64.           radarTurn += (radarTurn < 0 ? -extraTurn : extraTurn);
  65.  
  66.           //Turn the radar
  67.           setTurnRadarRightRadians(radarTurn);
  68.           if (Math.random() > 0.5) {
  69.         nprand = -1*Math.random();
  70.                 }
  71.           else {
  72.                 nprand = Math.random();
  73.                 }
  74.           double bulletPower = 3;
  75.       double headOnBearing = getHeadingRadians() + e.getBearingRadians();
  76.       double linearBearing = headOnBearing + Math.asin(e.getVelocity() / Rules.getBulletSpeed(bulletPower) * Math.sin(e.getHeadingRadians() - headOnBearing));
  77.       setTurnGunRightRadians(Utils.normalRelativeAngle(linearBearing - getGunHeadingRadians()) + .1*nprand);
  78.           if (getGunTurnRemaining() < 2) {
  79.                 setFire(3);
  80.           }    
  81.                
  82.                 //Modifies trajectory based on proximity to target.
  83.                 if (e.getDistance() < 100) {
  84.                         out.println("too close!" + e.getDistance());
  85.                         if (moveDirection == 1) {
  86.                                 turnmodifier = -20;
  87.                         }
  88.                         if (moveDirection == -1) {
  89.                                 turnmodifier = 20;
  90.                         }                      
  91.                 }
  92.                 if (e.getDistance() > 100) {
  93.                         turnmodifier = 0;
  94.                 }
  95.                 setTurnRight(e.getBearing() + 100 + turnmodifier);
  96.                        
  97.        
  98.                 if (getGunTurnRemaining() < 5) {
  99.                         setFire(3);
  100.                 }
  101.                 if (lastenergy != e.getEnergy()){
  102.                         if (fliptimer > 50){
  103.                                 moveDirection = moveDirection * -1;
  104.                                 fliptimer = 0; 
  105.                         }
  106.                 }      
  107.                 lastenergy = e.getEnergy();
  108.                 fliptimer = fliptimer + 1;
  109. }
  110.  
  111.  
  112.         public void onHitByBullet(HitByBulletEvent e) {
  113.  
  114.                 execute();
  115.         }
  116.        
  117.  
  118.         public void onHitWall(HitWallEvent e) {
  119.                 setTurnRight(e.getBearing() + 180);
  120.                 moveDirection = moveDirection * -1;    
  121.                 setAhead(150 * moveDirection);
  122.                 execute();
  123.        
  124.         }      
  125. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top