Advertisement
fire219

cymba v0.8

Feb 23rd, 2015
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.45 KB | None | 0 0
  1. package fire219.cymba;
  2. import robocode.*;
  3. import robocode.util.*;
  4. import java.awt.Color;
  5. import java.awt.Graphics2D;
  6. //import java.awt.Color;
  7.  
  8. /**
  9. * Cymba - a robot by fire219
  10. * version 0.8
  11. * 2-1-15
  12. * Proving simple is better since 2014
  13. */
  14. public class Cymba extends AdvancedRobot
  15. {
  16. int moveDirection = 1; //-1 = left, 1 = right
  17. int turnmodifier = 0; //angle rotation in/out from target
  18. double nprand = 0; //random generator for Linear Gun
  19. double lastenergy = 0; // last target energy
  20. int fliptimer = 0; //timer for scheduled direction change
  21. int antiwalltimer = 0; //"ignore wall-avoidance" timer
  22. int guntype = 0; //0 = Linear, 1 = HOT
  23. int guntypetimer = 0; //timer for gunmode swap
  24. int scannedX = Integer.MIN_VALUE;
  25. int scannedY = Integer.MIN_VALUE;
  26.  
  27. public void run() {
  28. setColors(Color.GRAY, Color.WHITE, Color.GRAY);
  29. out.println("Cymba is initializing.");
  30. setAdjustRadarForRobotTurn(true);
  31.  
  32. do {
  33.  
  34.  
  35. if ( getRadarTurnRemaining() == 0.0 )
  36. setTurnRadarRightRadians( Double.POSITIVE_INFINITY );
  37. execute();
  38. setAhead(150 * moveDirection);
  39. if (Math.random() > .95) {
  40. out.println("Changing direction");
  41. moveDirection = moveDirection * -1;
  42. }
  43. if (antiwalltimer < 60) {
  44. if (getX() < 100 || getY() < 100 || getX() > getBattleFieldWidth()-100 || getY() > getBattleFieldHeight()-100) {
  45. setTurnRight(90);
  46. antiwalltimer = 0;
  47. }
  48. }
  49. antiwalltimer = antiwalltimer + 1;
  50. scan();
  51. }while(true); }
  52.  
  53. public void onScannedRobot(ScannedRobotEvent e) {
  54.  
  55. // Absolute angle towards target
  56. double angleToEnemy = getHeadingRadians() + e.getBearingRadians();
  57.  
  58. // Subtract current radar heading to get the turn required to face the enemy, be sure it is normalized
  59. double radarTurn = Utils.normalRelativeAngle( angleToEnemy - getRadarHeadingRadians() );
  60.  
  61. // Distance we want to scan from middle of enemy to either side
  62. // The 36.0 is how many units from the center of the enemy robot it scans.
  63. double extraTurn = Math.min( Math.atan( 72.0 / e.getDistance() ), Rules.RADAR_TURN_RATE_RADIANS );
  64.  
  65. // Adjust the radar turn so it goes that much further in the direction it is going to turn
  66. // Basically if we were going to turn it left, turn it even more left, if right, turn more right.
  67. // This allows us to overshoot our enemy so that we get a good sweep that will not slip.
  68. radarTurn += (radarTurn < 0 ? -extraTurn : extraTurn);
  69.  
  70. //Turn the radar
  71. setTurnRadarRightRadians(radarTurn);
  72.  
  73.  
  74. //Linear velocity gun
  75. if (guntype == 0){
  76. if (Math.random() > 0.5) {
  77. nprand = -1*Math.random();
  78. }
  79. else {
  80. nprand = Math.random();
  81. }
  82. double bulletPower = 3;
  83. double headOnBearing = getHeadingRadians() + e.getBearingRadians();
  84. double linearBearing = headOnBearing + Math.asin(e.getVelocity()/2 / Rules.getBulletSpeed(bulletPower) * Math.sin(e.getHeadingRadians() - headOnBearing));
  85. setTurnGunRightRadians(Utils.normalRelativeAngle(linearBearing - getGunHeadingRadians()));
  86. if (getGunTurnRemaining() < 10) {
  87. setFire(800/e.getDistance());
  88. }
  89. }
  90.  
  91. //HOT gun
  92. if (guntype == 1){
  93. double absoluteBearing = getHeadingRadians() + e.getBearingRadians();
  94. setTurnGunRightRadians(
  95. robocode.util.Utils.normalRelativeAngle(absoluteBearing - getGunHeadingRadians()));
  96. setFire(800/e.getDistance());
  97. }
  98.  
  99. //Modifies trajectory based on proximity to target.
  100. if (e.getDistance() < 100) {
  101. out.println("too close!" + e.getDistance());
  102. if (moveDirection == 1) {
  103. turnmodifier = -20;
  104. }
  105. if (moveDirection == -1) {
  106. turnmodifier = 20;
  107. }
  108. }
  109. if (e.getDistance() > 100) {
  110. turnmodifier = 0;
  111. }
  112.  
  113. setTurnRight(e.getBearing() + 100 + turnmodifier);
  114.  
  115.  
  116. if (lastenergy != e.getEnergy()){
  117. if (fliptimer > 30){
  118. moveDirection = moveDirection * -1;
  119. fliptimer = 0;
  120. }
  121. }
  122. lastenergy = e.getEnergy();
  123. fliptimer = fliptimer + 1;
  124.  
  125. // The coordinates of the last scanned robot
  126.  
  127.  
  128. // Calculate the angle to the scanned robot
  129. double angle = Math.toRadians((getHeading() + e.getBearing()) % 360);
  130.  
  131. // Calculate the coordinates of the robot
  132. scannedX = (int)(getX() + Math.sin(angle) * e.getDistance());
  133. scannedY = (int)(getY() + Math.cos(angle) * e.getDistance());
  134.  
  135. }
  136.  
  137.  
  138. public void onHitByBullet(HitByBulletEvent e) {
  139.  
  140. execute();
  141. }
  142.  
  143.  
  144. public void onHitWall(HitWallEvent e) {
  145. setTurnRight(e.getBearing() + 90);
  146. moveDirection = moveDirection * -1;
  147. setAhead(150 * moveDirection);
  148. execute();
  149.  
  150. }
  151. public void onBulletHit (BulletHitEvent e) {
  152. guntypetimer = 0;
  153. out.println("Hit! :D");
  154. }
  155. public void onBulletMissed (BulletMissedEvent e) {
  156. if (guntypetimer == 0) {
  157. guntype = guntype == 0 ? 1 : 0;
  158. guntypetimer = 0;
  159. }
  160.  
  161. }
  162.  
  163.  
  164. // Paint a transparent square on top of the last scanned robot
  165. public void onPaint(Graphics2D g) {
  166. // Set the paint color to a red half transparent color
  167. g.setColor(new Color(0xff, 0x00, 0x00, 0x80));
  168.  
  169. // Draw a line from our robot to the scanned robot
  170. g.drawLine(scannedX, scannedY, (int)getX(), (int)getY());
  171.  
  172. // Draw a filled square on top of the scanned robot that covers it
  173. g.fillRect(scannedX - 20, scannedY - 20, 40, 40);
  174.  
  175. g.setColor(new Color(0x00, 0x00, 0xff, 0x80));
  176. g.drawLine(scannedX, scannedY, (int)getX(), (int)getY());
  177. }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement