Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2014
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.04 KB | None | 0 0
  1. import java.awt.geom.*;
  2. import java.io.*;
  3. import java.util.zip.*;
  4. import java.util.Random;
  5. import robocode.BattleRules;
  6. import robocode.*;
  7. import robocode.util.Utils;
  8. import java.awt.Color;
  9. // API help : C:\robocode\javadoc\robocode\CSJuniorRobot.html
  10. /**
  11. * Robot2 - a robot by (your name here)
  12. */
  13. public class Robot2 extends CSJuniorRobot
  14. {
  15. private byte moveDirection = 1;
  16. public int shot=1;
  17. /**
  18. * run: Robot2's default behavior
  19. */
  20. public void run() {
  21. setColors(orange, blue, white, yellow, black);
  22. int fw=fieldWidth;
  23. int fh=fieldHeight;
  24. int rxv = robotX;
  25. int ryv =robotY;
  26. while(true) {
  27. // Replace the next 4 lines with any behavior you would like
  28. //scanning();
  29. // ahead(100); // Move ahead 100
  30. //turnGunRight(360); // Spin gun around
  31. //back(100); // Move back 100
  32. rxv =robotX;
  33. ryv =robotY;
  34. ahead(150 * moveDirection);
  35. turnGunRight(40);
  36. for (int i = 0; i < 9; ++i) {
  37. turnGunLeft(40);
  38. }
  39. if (robotY<80){System.out.println("bhit");turnTo(0);ahead(80);};
  40. if (robotX<80){System.out.println("lhit");turnTo(90);ahead(80);};
  41. if (robotX>fieldWidth-80){System.out.println("rhit");turnTo(270);ahead(80);};
  42. if (robotY>fieldHeight-80){System.out.println("thit");turnTo(180);ahead(80);};
  43. ahead (50* -1 * moveDirection);
  44. for (int i = 0; i < 9; ++i) {
  45. turnGunLeft(40);
  46. }
  47. if (robotY<80){System.out.println("bhit");turnTo(0);ahead(80);};
  48. if (robotX<80){System.out.println("lhit");turnTo(90);ahead(80);};
  49. if (robotX>fieldWidth-80){System.out.println("rhit");turnTo(270);ahead(80);};
  50. if (robotY>fieldHeight-80){System.out.println("thit");turnTo(180);ahead(80);};
  51. //if (robotY<80 || robotX<80 || robotX>fieldWidth-80 || robotY>fieldHeight-80) back(160);
  52. }
  53. }
  54. /**
  55. * onScannedRobot: What to do when you see another robot
  56. */
  57. public void onScannedRobot() {
  58. // Replace the next line with any behavior you would like
  59. System.out.println("scanned");
  60. int opp_angle = scannedAngle;
  61. int opp_distance = scannedDistance;
  62. int opp_velocity = scannedVelocity;
  63. int opp_bearing = scannedBearing;
  64. int opp_heading = scannedHeading;
  65.  
  66. int prepared_fire = 1;
  67. if (opp_distance < 200) prepared_fire = 3;
  68. if (opp_distance < 400) prepared_fire = 2;
  69.  
  70. int adjust = gunAdjustment(
  71. heading, gunHeading,
  72. scannedHeading, scannedBearing,
  73. scannedVelocity, scannedAngle,
  74. prepared_fire);
  75. turnGunTo(opp_angle);
  76. // if (true == gunReady) fire(prepared_fire);
  77. if (opp_distance < 200 && energy>12)fire(3);
  78. else if (opp_distance < 250 && energy>15)fire(2.5);
  79. else if (opp_distance < 300 && energy>15)fire(2.2);
  80. else if (opp_distance < 400 && energy>15)fire(2);
  81. else if (opp_distance > 800)fire(0.5);
  82. else fire(1);
  83. ahead (75);
  84. if (robotY<80){turnTo(0);};
  85. if (robotX<80){turnTo(90);};
  86. if (robotX>fieldWidth-80){turnTo(270);};
  87. if (robotY>fieldHeight-80){turnTo(180);};
  88. //turnGunTo(opp_angle);
  89. // if (true == gunReady) fire();
  90. // just skip 1 turn
  91. // guarantee firing without affecting the gun direction
  92. //doNothing();
  93. //turnTo(opp_angle);
  94. //ahead(100);
  95. //turnRight(normalizeBearing(opp_bearing + 90 - (15 * moveDirection)));
  96. /*ahead(150 * moveDirection);
  97. ahead (125* -1 * moveDirection);
  98. if (robotY<80 || robotX<80 || robotX>fieldWidth-80 || robotY>fieldHeight-80){
  99. back(160);
  100. moveDirection*=-1;back(125)
  101.  
  102. }*/
  103. }
  104. /**
  105. * onScannedObstacle: What to do when you see an obstacle
  106. */
  107. public void onScannedObstacle() {
  108. // Replace the next line with any behavior you would like
  109. int d=scannedObstacleDistance;
  110. if (d<50)turnRight(90);
  111. }
  112. /**
  113. * onHitByBullet: What to do when you're hit by a bullet
  114. */
  115. public void onHitByBullet() {
  116. // Replace the next line with any behavior you would like
  117. //back(10);
  118. //moveDirection *= -1;
  119. //turnLeft(90 - hitByBulletBearing);
  120. }
  121.  
  122. /**
  123. * onHitWall: What to do when you hit a wall
  124. */
  125. public void onHitWall() {
  126. // Replace the next line with any behavior you would like
  127. //back(20);
  128. //moveDirection *= -1;
  129. //ahead (200);
  130. }
  131. /**
  132. * onHitObstacle: What to do when you're hit an obstacle
  133. */
  134. public void onHitObstacle() {
  135. // Replace the next line with any behavior you would like
  136. back(20);
  137. turnRight(90);
  138. moveDirection *= -1;
  139. }
  140. int normalizeBearing(int angle) {
  141. while (angle > 180) angle -= 360;
  142. while (angle < -180) angle += 360;
  143. return angle;
  144. }
  145. public int gunAdjustment(
  146. int self_heading, int self_gunHeading,
  147. int opp_heading, int opp_bearing,
  148. int opp_velocity, int opp_angle, int firepower)
  149. {
  150. // reference:
  151. // http://robowiki.net/wiki/Linear_Targeting
  152. int bv = 20 - firepower * 3;
  153. double absBearing = Math.toRadians(self_heading + opp_bearing);
  154. double lv = opp_velocity
  155. * Math.sin(Math.toRadians(opp_heading) -
  156. absBearing);
  157. double predict = robocode.util.Utils.normalRelativeAngle(
  158. absBearing - Math.toRadians(self_gunHeading)
  159. + Math.asin(lv/ bv)
  160. );
  161. if (0 == lv) predict = 0;
  162. return (int) Math.toDegrees(predict);
  163. }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement