Advertisement
Guest User

Untitled

a guest
May 24th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.87 KB | None | 0 0
  1. package robo;
  2. import robocode.*;
  3.  
  4. import java.util.*;
  5. import robocode.util.Utils;
  6.  
  7. import java.awt.Color;
  8. import java.awt.geom.Point2D;
  9.  
  10.  
  11. public class PartsBot extends AdvancedRobot
  12. {
  13. Random rand = new Random();
  14.  
  15. private AdvancedEnemyBot enemy = new AdvancedEnemyBot();
  16.  
  17. private RobotPart[] parts = new RobotPart[3]; // make three parts
  18.  
  19. private final static int RADAR = 0;
  20.  
  21. private final static int GUN = 1;
  22.  
  23. private final static int TANK = 2;
  24.  
  25.  
  26. /**
  27. * This runs all the methods in the code.
  28. */
  29. public void run()
  30. {
  31. parts[TANK] = new Tank();
  32. parts[GUN] = new Gun();
  33. parts[RADAR] = new Radar();
  34.  
  35. // initialize each part
  36. for ( int i = 0; i < parts.length; i++ )
  37. {
  38. // behold, the magic of polymorphism
  39. parts[i].init();
  40. }
  41.  
  42. // iterate through each part, moving them as we go
  43. for ( int i = 0; true; i = ( i + 1 ) % parts.length )
  44. {
  45. // polymorphism galore!
  46. parts[i].move();
  47. if ( i == 0 )
  48. {
  49. execute();
  50. }
  51. }
  52. }
  53.  
  54.  
  55. /**
  56. * This is the event depending on if it gets hit by a bullet.
  57. *
  58. * @param e
  59. * This is the situation that it gets hit by a bullet.
  60. */
  61. public void onHitByBullet( HitByBulletEvent e )
  62. {
  63.  
  64. }
  65.  
  66.  
  67. /**
  68. * This is the event depending on if it scans another robot.
  69. *
  70. * @param e
  71. * This is the situation that it scanned a robot.
  72. */
  73. public void onScannedRobot( ScannedRobotEvent e )
  74. {
  75. Radar radar = (Radar)parts[RADAR];
  76. if ( radar.shouldTrack( e ) )
  77. {
  78. // This looks for a new target
  79. enemy.update( e, this );
  80. }
  81. }
  82.  
  83.  
  84. /**
  85. * This is the event depending on if it scans a dead robot.
  86. *
  87. * @param e
  88. * This is the situation that it scanned a dead robot.
  89. */
  90. public void onRobotDeath( RobotDeathEvent e )
  91. {
  92. Radar radar = (Radar)parts[RADAR];
  93. if ( radar.wasTracking( e ) )
  94. {
  95. // This prints out Victory upon killing an enemy :)
  96. this.out.println( "VICTORY!!" );
  97. enemy.reset();
  98. }
  99. }
  100.  
  101.  
  102. /**
  103. * This is the event depending on if it hits the wall.
  104. *
  105. * @param e
  106. * This is the situation that it hits the wall.
  107. */
  108. public void onHitWall( HitWallEvent e )
  109. {
  110. // This is if the robot hits a wall
  111. Tank tank = (Tank)parts[TANK];
  112. tank.hitWalls();
  113. }
  114.  
  115.  
  116. // ... put normalizeBearing and absoluteBearing methods here
  117. /**
  118. * computes the absolute bearing between two points
  119. *
  120. * @param x1
  121. * This is the original x.
  122. * @param y1
  123. * This is the original y.
  124. * @param x2
  125. * This is the next x.
  126. * @param y2
  127. * This is the next y
  128. * @return bearing This is the double that describes the bearing to enemy.
  129. */
  130. public double absoluteBearing( double x1, double y1, double x2, double y2 )
  131. {
  132. double xo = x2 - x1;
  133. double yo = y2 - y1;
  134. double hyp = Point2D.distance( x1, y1, x2, y2 );
  135. double arcSin = Math.toDegrees( Math.asin( xo / hyp ) );
  136. double bearing = 0;
  137.  
  138. if ( xo > 0 && yo > 0 )
  139. { // both pos: lower-Left
  140. bearing = arcSin;
  141. }
  142. else if ( xo < 0 && yo > 0 )
  143. { // x neg, y pos: lower-right
  144. bearing = 360 + arcSin; // arcsin is negative here, actually 360
  145. // -
  146. // ang
  147. }
  148. else if ( xo > 0 && yo < 0 )
  149. { // x pos, y neg: upper-left
  150. bearing = 180 - arcSin;
  151. }
  152. else if ( xo < 0 && yo < 0 )
  153. { // both neg: upper-right
  154. bearing = 180 - arcSin; // arcsin is negative here, actually 180
  155. // +
  156. // ang
  157. }
  158.  
  159. return bearing;
  160. }
  161.  
  162.  
  163. /**
  164. * normalizes a bearing to between +180 and -180
  165. *
  166. * @param angle
  167. * This is the angle to turn the radar.
  168. * @return angle This is the smallest angle change.
  169. */
  170. public double normalizeBearing( double angle )
  171. {
  172. while ( angle > 180 )
  173. {
  174. angle -= 360;
  175. }
  176. while ( angle < -180 )
  177. {
  178. angle += 360;
  179. }
  180. return angle;
  181. }
  182.  
  183.  
  184. /**
  185. * ... declare the RobotPart interface and classes that implement it here
  186. * They will be _inner_ classes.
  187. */
  188. public interface RobotPart
  189. {
  190. /**
  191. * This initializes the bot.
  192. */
  193. public void init();
  194.  
  195.  
  196. /**
  197. * This moves the bot.
  198. */
  199. public void move();
  200.  
  201. }
  202.  
  203.  
  204. /**
  205. * This class controls the radar.
  206. */
  207. public class Radar implements RobotPart
  208. {
  209. /**
  210. * This controls the radar.
  211. */
  212. public void init()
  213. {
  214. // initialize radar operation
  215. setAdjustRadarForGunTurn( true );
  216. }
  217.  
  218.  
  219. /**
  220. * This moves the radar and turns it.
  221. */
  222. public void move()
  223. {
  224.  
  225. // Absolute angle towards target
  226. double angleToEnemy = getHeading() + enemy.getBearing();
  227.  
  228. // Subtract current radar heading to get the turn required to
  229. // face
  230. // the enemy, be sure it is normalized
  231. double radarTurn = normalizeBearing(angleToEnemy - getRadarHeading() );
  232.  
  233. // Distance we want to scan from middle of enemy to either side
  234. // The 36.0 is how many units from the center of the enemy
  235. // robot it scans.
  236. if (radarTurn > 0) {
  237. radarTurn += 10.0;
  238. }
  239. else {
  240. radarTurn -= 10.0;
  241. }
  242.  
  243. // Adjust the radar turn so it goes that much further in the
  244. // direction it is going to turn
  245. // Basically if we were going to turn it left, turn it even
  246. // more left, if right, turn more right.
  247. // This allows us to overshoot our enemy so that we get a good
  248. // sweep
  249. // that will not slip.
  250.  
  251.  
  252. // Turn the radar
  253. setTurnRadarRightRadians( radarTurn );
  254.  
  255.  
  256. }
  257.  
  258.  
  259. /**
  260. * This is the boolean statement
  261. *
  262. * @param e
  263. * This is the event of scanning a robot.
  264. *
  265. * @return ( enemy.none() || e.getDistance() < enemy.getDistance() - 70
  266. * || e.getName().equals( enemy.getName() ) ) This determines
  267. * whether the enemy should be tracked.
  268. */
  269. public boolean shouldTrack( ScannedRobotEvent e )
  270. {
  271. // track if we have no enemy, the one we found is significantly
  272. // closer, or we scanned the one we've been tracking.
  273. return ( enemy.none() || e.getDistance() < enemy.getDistance()
  274. - 100
  275. || e.getName().equals( enemy.getName() ) );
  276. }
  277.  
  278.  
  279. /**
  280. * This controls the radar.
  281. *
  282. * @param e
  283. * This is the event of scanning a dead robot.
  284. *
  285. * @return e.getName().equals( enemy.getName() ) If enemy is dead, the
  286. * robot finds another target.
  287. */
  288. public boolean wasTracking( RobotDeathEvent e )
  289. {
  290. // if enemy is dead, the robot finds another target.
  291. return e.getName().equals( enemy.getName() );
  292. }
  293. }
  294.  
  295.  
  296. /**
  297. * This is the class that controls the gun.
  298. */
  299. public class Gun implements RobotPart
  300. {
  301. /**
  302. * This is the initialization method.
  303. */
  304. public void init()
  305. {
  306. // initialize gun operation
  307. setAdjustGunForRobotTurn( true );
  308. }
  309.  
  310. public void onScannedRobot( ScannedRobotEvent e )
  311. {
  312. // track if we have no enemy, the one we found is significantly
  313. // closer, or we scanned the one we've been tracking.
  314. if ( enemy.none() || e.getDistance() < enemy.getDistance() - 70
  315. || e.getName().equals( enemy.getName() ) )
  316. {
  317.  
  318. // track him using the NEW update method
  319. enemy.update( e );
  320. }
  321. }
  322.  
  323. public void move()
  324. {
  325. // TODO: gun implementation
  326. if ( enemy.none() )
  327. return;
  328.  
  329. // calculate firepower based on distance
  330. double firePower = Math.min( 500 / enemy.getDistance(), 3 );
  331. // calculate speed of bullet
  332. double bulletSpeed = 20 - firePower * 3;
  333. // distance = rate * time, solved for time
  334. long time = (long)( enemy.getDistance() / bulletSpeed );
  335.  
  336. // calculate gun turn to predicted x,y location
  337. double futureX = enemy.getFutureX( time );
  338. double futureY = enemy.getFutureY( time );
  339. double absDeg = absoluteBearing( getX(), getY(), futureX, futureY );
  340. // non-predictive firing can be done like this:
  341. // double absDeg = absoluteBearing(getX(), getY(), enemy.getX(),
  342. // enemy.getY());
  343.  
  344. // turn the gun to the predicted x,y location
  345. setTurnGunRight( normalizeBearing( absDeg - getGunHeading() ) );
  346.  
  347. // if the gun is cool and we're pointed in the right direction, shoot!
  348. if ( getGunHeat() == 0 && Math.abs( getGunTurnRemaining() ) < 10 )
  349. {
  350. setFire( firePower );
  351. }
  352.  
  353. }
  354.  
  355.  
  356. }
  357.  
  358.  
  359. /**
  360. * This moves the tanks and turns it.
  361. */
  362. public class Tank implements RobotPart
  363. {
  364. /**
  365. * This is the initialization for the tank.
  366. */
  367. public void init()
  368. {
  369. // this is the initial color.
  370. setColors( Color.white, Color.white, Color.red, Color.white,
  371. Color.white );
  372. }
  373.  
  374. // this is used when strafing
  375. private byte moveDirection = 1;
  376.  
  377. public void move()
  378. {
  379. setTurnRight( normalizeBearing( enemy.getBearing() + 90 ) );
  380. setAhead(100 * moveDirection);
  381.  
  382. }
  383.  
  384.  
  385. public void hitWalls( ) {
  386. setMaxVelocity( 10 );
  387. moveDirection *= -1;
  388. }
  389.  
  390. }
  391. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement