Advertisement
fire219

Cymba v1.9

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