Guest User

ERTYUJKL

a guest
Oct 1st, 2014
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.43 KB | None | 0 0
  1. package Cy;
  2. import robocode.*;
  3. import java.awt.*;
  4. import java.awt.geom.Point2D;
  5. import java.awt.Color;
  6. import robocode.util.*;
  7. //import java.awt.Color;
  8.  
  9. // API help : http://robocode.sourceforge.net/docs/robocode/robocode/Robot.html
  10.  
  11. /**
  12. * Quasarmint - a robot by (your name here)
  13. */
  14. public class Quasarmint extends AdvancedRobot
  15. {
  16. static int moveDirection=1;
  17. //The amount of velocities we will store at one time.
  18. setColors( new Color(150, 0, 150), new Color(150, 0, 150), new Color(150, 0, 150), new Color(150, 0, 150), new Color(150, 0, 150));
  19. static int depth=200;
  20.  
  21. //Whether or not we changed our gun's depth.
  22. static boolean depthChange=true;
  23.  
  24. //The list of data we store our enemy's movements in.
  25. static double enemyVelocities[][];
  26.  
  27. //Our enemy's current velocity.
  28. static int currentEnemyVelocity;
  29.  
  30. //The velocity we are currently keeping track of.
  31. static int aimingEnemyVelocity;
  32.  
  33. //The averaged velocity.
  34. double velocityToAimAt;
  35.  
  36.  
  37. //Other
  38. boolean fired;
  39. double oldTime;
  40. int count;
  41. int averageCount;
  42.  
  43.  
  44. //The middle of our field, we'll divide it into four quadrants.
  45. static double fieldXMid;
  46. static double fieldYMid;
  47.  
  48. //Enemy's X and Y.
  49. static double EX;
  50. static double EY;
  51.  
  52. //Our bulletpower and turn direction.
  53. static double maxBP;
  54. static double turn;
  55.  
  56. //Our direction and distance to stay away from the enemy.
  57. int dir=1;
  58. double dist=1000;
  59.  
  60. //The X and Y of the corner we are trying to push them into.
  61. double cornerX,cornerY;
  62.  
  63. //The amount to modify our angle by.
  64. static double firingAngleMod=1.0;
  65.  
  66. //The closest distance we want to be from our enemy.
  67. static double closestDist=100;
  68.  
  69. //The old heading of the enemy, used to find turn rate.
  70. double oldEnemyHeading;
  71.  
  72. static boolean firstTime=true;
  73.  
  74. /**
  75. * run: Quasarmint's default behavior
  76. */
  77. public void run() {
  78. if(firstTime){
  79. velocityToAimAt=4;
  80. currentEnemyVelocity=1;
  81. }
  82. firstTime=false;
  83.  
  84. if(depthChange){
  85. enemyVelocities=new double[depth][4];
  86. double assumedVelocity=velocityToAimAt;
  87. count=0;
  88. while(count<4){
  89. if(count==0||count==2){
  90. if(currentEnemyVelocity==0||currentEnemyVelocity==2){
  91. assumedVelocity=velocityToAimAt;
  92. }
  93. else{
  94. assumedVelocity=-velocityToAimAt;
  95. }
  96. }
  97. else{
  98. if(currentEnemyVelocity==1||currentEnemyVelocity==3){
  99. assumedVelocity=velocityToAimAt;
  100. }
  101. else{
  102. assumedVelocity=-velocityToAimAt;
  103. }
  104.  
  105. }
  106. averageCount=0;
  107. while(averageCount<depth){
  108. enemyVelocities[averageCount][count]=assumedVelocity;
  109. averageCount++;
  110. }
  111. count++;
  112. }
  113. count=0;
  114. }
  115. depthChange=false;
  116.  
  117.  
  118. //Find the middle of the field.
  119. fieldYMid=getBattleFieldHeight()/2;
  120. fieldXMid=getBattleFieldWidth()/2;
  121. setAdjustGunForRobotTurn(true);
  122. setAdjustRadarForGunTurn(true);
  123. do{
  124. turnRadarRightRadians(Double.POSITIVE_INFINITY);
  125. }while(true);
  126. }
  127.  
  128. /**
  129. * onScannedRobot: What to do when you see another robot
  130. */
  131. public void onScannedRobot(ScannedRobotEvent e) {
  132. // Replace the next line with any behavior you would like
  133. maxBP=3;
  134. if(e.getDistance()>200){
  135. maxBP=2.4;
  136. }
  137. double bulletPower=Math.min(maxBP,Math.min(getEnergy()/10,e.getEnergy()/4));
  138. double bulletSpeed=20-3*bulletPower;
  139.  
  140. //Find our enemy's absolute bearing and makes a thing used for painting.
  141. double absBearing=e.getBearingRadians()+getHeadingRadians();
  142. Graphics2D g=getGraphics();
  143.  
  144. //This finds our enemies X and Y (note: it can be used to find the X and Y of anything by switching the distance and bearing).
  145. EX=getX()+e.getDistance()*Math.sin(absBearing);
  146. EY=getY()+e.getDistance()*Math.cos(absBearing);
  147.  
  148.  
  149.  
  150. //Find out which velocity segment our enemy is at right now. Note that I use two segments for when their velocity is zero,
  151. //because it is often important to consider where they are coming from.
  152. if(e.getVelocity()<-2){
  153. currentEnemyVelocity=0;
  154. }
  155. else if(e.getVelocity()>2){
  156. currentEnemyVelocity=1;
  157. }
  158. else if(e.getVelocity()<=2&&e.getVelocity()>=-2){
  159. if(currentEnemyVelocity==0){
  160. currentEnemyVelocity=2;
  161. }
  162. else if(currentEnemyVelocity==1){
  163. currentEnemyVelocity=3;
  164. }
  165. }
  166.  
  167. //We update our aiming segment each time enough time has passed for a bullet to hit them and we have fired;
  168. if(getTime()-oldTime>e.getDistance()/12.8&&fired==true){
  169. aimingEnemyVelocity=currentEnemyVelocity;
  170. }
  171. else{
  172. fired=false;
  173. }
  174.  
  175. //Record a new enemy velocity and raise the count.
  176. enemyVelocities[count][aimingEnemyVelocity]=e.getVelocity();
  177. count++;
  178. if(count==depth){
  179. count=0;
  180. }
  181.  
  182.  
  183. //Calculate our average velocity for our the list of velocities at our current segment.
  184. averageCount=0;
  185. velocityToAimAt=0;
  186. while(averageCount<depth){
  187. velocityToAimAt+=enemyVelocities[averageCount][currentEnemyVelocity];
  188. averageCount++;
  189. }
  190. velocityToAimAt/=depth;
  191.  
  192.  
  193.  
  194.  
  195. //find our enemy's heading and heading change.
  196. double enemyHeading = e.getHeadingRadians();
  197. double enemyHeadingChange = enemyHeading - oldEnemyHeading;
  198. oldEnemyHeading = enemyHeading;
  199.  
  200. /*This method of targeting is know as circular targeting; you assume your enemy will keep moving with the same speed and turn rate that he is using at
  201. *fire time. This particular version of it will not aim outside the walls, important for when your enemy is trapped in a corner.
  202. *The base code comes from the wiki, however, it has been improved by keeping track of more than just the current state of the enemy robot.
  203. */
  204. double deltaTime = 0;
  205. double predictedX = EX, predictedY = EY;
  206. while((++deltaTime) * bulletSpeed < Point2D.Double.distance(getX(), getY(), predictedX, predictedY)){
  207.  
  208. //Add the movement we think our enemy will make to our enemy's current X and Y(note that it is modified by the average of our enemy's velocities
  209. //at the current segment, instead of their current velocity.
  210. predictedX += Math.sin(enemyHeading) * velocityToAimAt;
  211. predictedY += Math.cos(enemyHeading) * velocityToAimAt;
  212.  
  213.  
  214. //Find our enemy's heading changes.
  215. enemyHeading += enemyHeadingChange;
  216.  
  217. //Paint the path we think our enemy will take(turn on paint in the robot console to see).
  218. g.setColor(Color.red);
  219. g.fillOval((int)predictedX-2, (int)predictedY-2, 4, 4);
  220.  
  221. //If our predicted coordinates are outside the walls, put them 18 distance units away from the walls as we know
  222. //that that is the closest they can get to the wall (bots are non-rotating 36*36 squares).
  223. predictedX=Math.max(predictedX,18);
  224. predictedY=Math.max(predictedY,18);
  225. predictedX=Math.min(predictedX,getBattleFieldWidth()-18);
  226. predictedY=Math.min(predictedY,getBattleFieldHeight()-18);
  227.  
  228. }
  229. //Find the bearing of our predicted coordinates from us.
  230. double aim = Utils.normalAbsoluteAngle(Math.atan2( predictedX - getX(), predictedY - getY()));
  231.  
  232. //Aim and fire.
  233. setTurnGunRightRadians(Utils.normalRelativeAngle(aim - getGunHeadingRadians()));
  234. setFire(bulletPower);
  235.  
  236. //Find which corner we want to trap them in, based on their distance from each corner.
  237. if(EX>fieldXMid){
  238. cornerX=getBattleFieldWidth();
  239. }
  240. else{
  241. cornerX=0;
  242. }
  243. if(EY>fieldYMid){
  244. cornerY=getBattleFieldHeight();
  245. }
  246. else{
  247. cornerY=0;
  248. }
  249.  
  250. //Yay, math!!
  251.  
  252. //This finds the enemy's distance from the current corner. Note that you can (and should, it is much easier)
  253. //use the Point2D.Double.distance(X1, Y1, X2, Y2) method instead. I do it this way just for the heck of it.
  254. double enemyDistanceFromCorner=Math.sqrt(Math.pow((EX-cornerX),2)+Math.pow((EY-cornerY),2));
  255.  
  256. //This finds the enemy's bearing from the current corner,
  257. double enemyBearingFromCorner=Utils.normalAbsoluteAngle(Math.atan2(EX-cornerX,EY-cornerY));
  258.  
  259. /*This assigns coordinates for where we want to move. Note that this is the same formula used to find the enemies X and Y,
  260. *except that the variables are replaced. This is equivalent to graphing a line from the corner through the enemy and picking a
  261. *spot that is "dist"(the distance we want to be from the enemy) down the line.
  262. */
  263. double targetX=cornerX+(enemyDistanceFromCorner+dist)*Math.sin(enemyBearingFromCorner);
  264. double targetY=cornerY+(enemyDistanceFromCorner+dist)*Math.cos(enemyBearingFromCorner);
  265.  
  266.  
  267. //We don't want to aim at a point outside the battlefield, do we?
  268. targetX=Math.max(18, targetX);
  269. targetX=Math.min(getBattleFieldWidth()-20, targetX);
  270. targetY=Math.max(18, targetY);
  271. targetY=Math.min(getBattleFieldHeight()-20, targetY);
  272.  
  273.  
  274. //This part is for determining which direction to go in order to best get to the point.
  275. //There are much easier ways to do this, but this works and it is an interesting example.
  276.  
  277. //What we are doing here is making two points, one directly in front of us and one behind.....
  278. double cushion1X=getX()+100*Math.sin(getHeadingRadians());
  279. double cushion1Y=getY()+100*Math.cos(getHeadingRadians());
  280. double cushion2X=getX()-100*Math.sin(getHeadingRadians());
  281. double cushion2Y=getY()-100*Math.cos(getHeadingRadians());
  282.  
  283. //...Then finding the distance from those points to the place we want to go.
  284. double cushion1Dist=Math.sqrt(Math.pow((cushion1X-targetX),2)+Math.pow((cushion1Y-targetY),2));
  285. double cushion2Dist=Math.sqrt(Math.pow((cushion2X-targetX),2)+Math.pow((cushion2Y-targetY),2));
  286.  
  287. //Here we assign our direction based on which one is closer.
  288. if(cushion1Dist>cushion2Dist){
  289. dir=-1;
  290. }
  291. else{
  292. dir=1;
  293. }
  294.  
  295. //Graphics that paint a target at the location we are trying to get to. To see them, press "paint" in the robot console.
  296. g.setColor(Color.red);
  297. g.fillOval((int)targetX-5, (int)targetY-5, 10, 10);
  298. g.setColor(Color.white);
  299. g.fillOval((int)targetX-4, (int)targetY-4, 8, 8);
  300. g.setColor(Color.red);
  301. g.fillOval((int)targetX-3, (int)targetY-3, 6, 6);
  302.  
  303.  
  304. //if we reach our target, make the distance closer
  305. if(getX()>targetX-18&&getX()<targetX+18&&getY()>targetY-18&&getY()<targetY+18){
  306. dist-=50;
  307. }
  308.  
  309. //It would be counterproductive to move away from the enemy, so we make the maximum distance equivalent to our distance from the enemy robot.
  310. dist=Math.min(dist,e.getDistance());
  311.  
  312. //If they aren't moving away, then we don't want to hit them. If we did that, we'd be a Rambot *shudder*
  313. //Instead, we'll stay at close quarters, which will help our targeting a lot, as linear targeting can actually be decent at close range.
  314. dist=Math.max(dist, closestDist);
  315.  
  316. //This finds the bearing of the point from us. Really this does the same thing as the turn,
  317. //but it helps us figure out where to go when we change directions.
  318. double inverseTurn=Utils.normalAbsoluteAngle(Math.atan2(targetX-getX(),targetY-getY()));
  319.  
  320. //If we are going backwards, we want to reflect our target points over our robot for the purposes of turning.
  321. if(dir==-1){
  322. targetX=getX()-100*Math.sin(inverseTurn);
  323. targetY=getY()-100*Math.cos(inverseTurn);
  324. }
  325.  
  326. //This finds the bearing of our target coordinates from us.
  327. turn=Utils.normalAbsoluteAngle(Math.atan2(targetX-getX(),targetY-getY()));
  328.  
  329. //.... If you've read this whole thing and you don't know what this does, you've just wasted 20 minutes of your life.
  330. setAhead(100*dir);
  331. setTurnRight(e.getBearing() + 90);
  332.  
  333. // strafe by changing direction every 20 ticks
  334. if (getTime() % 20 == 0) {
  335. moveDirection *= -1;
  336. setAhead(150 * moveDirection);
  337. }
  338. setTurnRadarRightRadians(robocode.util.Utils.normalRelativeAngle(absBearing-getRadarHeadingRadians())*2);
  339.  
  340. }
  341. public void onDeath(DeathEvent e){
  342.  
  343. /*There are vast differences in movement among robots, and what works for one may not work for another. Changing the closest
  344. *distance we will go to our enemy robot greatly helps our robot to adapt to different enemies. The idea behind changing it when the robot dies is that
  345. *if the distance works, the robot will be less likely to die and it will continue to use that movement. Note that it is not effective to have
  346. *multiple things set this way, because that dramatically decreases your chances of finding the optimal values.
  347. */
  348. closestDist=Math.max(50, 600*Math.random());
  349. if(Math.random()>.5){
  350. closestDist=Math.max(50,150*Math.random());
  351. }
  352.  
  353. //Same basic idea for this, which will change the depth of our gun when we die.
  354. depth=(int)(400*Math.random());
  355. depthChange=true;
  356. }
  357. public void onHitWalls(){
  358. setAhead(150 * moveDirection);}
  359. }
Add Comment
Please, Sign In to add comment