SHARE
TWEET

Cymba v0.7

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