Advertisement
Guest User

Untitled

a guest
May 24th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.75 KB | None | 0 0
  1. package robo;
  2.  
  3. import robocode.*;
  4. import java.awt.Color;
  5. import java.util.*;
  6. import java.awt.geom.Point2D;
  7. import robocode.util.Utils;
  8.  
  9.  
  10. /**
  11. * A modular bot adhering to the RoboPart Interface.
  12. *
  13. * @author TODO Your Name
  14. * @version TODO Date
  15. *
  16. * @author Period - TODO Your Period
  17. * @author Assignment - PartsBot
  18. *
  19. * @author Sources - TODO list collaborators
  20. */
  21.  
  22. public class PartsBot extends AdvancedRobot
  23. {
  24. Random rand = new Random();
  25.  
  26. private AdvancedEnemyBot enemy = new AdvancedEnemyBot();
  27.  
  28. private RobotPart[] parts = new RobotPart[3]; // make three parts
  29.  
  30. private final static int RADAR = 0;
  31.  
  32. private final static int GUN = 1;
  33.  
  34. private final static int TANK = 2;
  35.  
  36.  
  37. /**
  38. * This runs all the methods in the code.
  39. */
  40. public void run()
  41. {
  42. parts[TANK] = new Tank();
  43. parts[GUN] = new Gun();
  44. parts[RADAR] = new Radar();
  45.  
  46. // initialize each part
  47. for ( int i = 0; i < parts.length; i++ )
  48. {
  49. // behold, the magic of polymorphism
  50. parts[i].init();
  51. }
  52.  
  53. // iterate through each part, moving them as we go
  54. for ( int i = 0; true; i = ( i + 1 ) % parts.length )
  55. {
  56. // polymorphism galore!
  57. parts[i].move();
  58. if ( i == 0 )
  59. {
  60. execute();
  61. }
  62. }
  63. }
  64.  
  65.  
  66. /**
  67. * This is the event depending on if it gets hit by a bullet.
  68. *
  69. * @param e
  70. * This is the situation that it gets hit by a bullet.
  71. */
  72. public void onHitByBullet( HitByBulletEvent e )
  73. {
  74. // This is if the robot is hit by a bullet
  75. this.out.println( "Ouch! You hit me!" );
  76. }
  77.  
  78.  
  79. /**
  80. * This is the event depending on if it scans another robot.
  81. *
  82. * @param e
  83. * This is the situation that it scanned a robot.
  84. */
  85. public void onScannedRobot( ScannedRobotEvent e )
  86. {
  87. Radar radar = (Radar)parts[RADAR];
  88. if ( radar.shouldTrack( e ) )
  89. {
  90. // This looks for a new target
  91. this.out.println( "You are my new target!!" );
  92. enemy.update( e, this );
  93. }
  94. }
  95.  
  96.  
  97. /**
  98. * This is the event depending on if it scans a dead robot.
  99. *
  100. * @param e
  101. * This is the situation that it scanned a dead robot.
  102. */
  103. public void onRobotDeath( RobotDeathEvent e )
  104. {
  105. Radar radar = (Radar)parts[RADAR];
  106. if ( radar.wasTracking( e ) )
  107. {
  108. // This prints out Victory upon killing an enemy :)
  109. this.out.println( "VICTORY!!" );
  110. enemy.reset();
  111. }
  112. }
  113.  
  114.  
  115. /**
  116. * This is the event depending on if it hits the wall.
  117. *
  118. * @param e
  119. * This is the situation that it hits the wall.
  120. */
  121. public void onHitWall( HitWallEvent e )
  122. {
  123. // This is if the robot hits a wall
  124. this.out.println( "Ouch! I hit the wall :(" );
  125. }
  126.  
  127.  
  128. // ... put normalizeBearing and absoluteBearing methods here
  129. /**
  130. * computes the absolute bearing between two points
  131. *
  132. * @param x1
  133. * This is the original x.
  134. * @param y1
  135. * This is the original y.
  136. * @param x2
  137. * This is the next x.
  138. * @param y2
  139. * This is the next y
  140. * @return bearing This is the double that describes the bearing to enemy.
  141. */
  142. public double absoluteBearing( double x1, double y1, double x2, double y2 )
  143. {
  144. double xo = x2 - x1;
  145. double yo = y2 - y1;
  146. double hyp = Point2D.distance( x1, y1, x2, y2 );
  147. double arcSin = Math.toDegrees( Math.asin( xo / hyp ) );
  148. double bearing = 0;
  149.  
  150. if ( xo > 0 && yo > 0 )
  151. { // both pos: lower-Left
  152. bearing = arcSin;
  153. }
  154. else if ( xo < 0 && yo > 0 )
  155. { // x neg, y pos: lower-right
  156. bearing = 360 + arcSin; // arcsin is negative here, actually 360
  157. // -
  158. // ang
  159. }
  160. else if ( xo > 0 && yo < 0 )
  161. { // x pos, y neg: upper-left
  162. bearing = 180 - arcSin;
  163. }
  164. else if ( xo < 0 && yo < 0 )
  165. { // both neg: upper-right
  166. bearing = 180 - arcSin; // arcsin is negative here, actually 180
  167. // +
  168. // ang
  169. }
  170.  
  171. return bearing;
  172. }
  173.  
  174.  
  175. /**
  176. * normalizes a bearing to between +180 and -180
  177. *
  178. * @param angle
  179. * This is the angle to turn the radar.
  180. * @return angle This is the smallest angle change.
  181. */
  182. public double normalizeBearing( double angle )
  183. {
  184. while ( angle > 180 )
  185. {
  186. angle -= 360;
  187. }
  188. while ( angle < -180 )
  189. {
  190. angle += 360;
  191. }
  192. return angle;
  193. }
  194.  
  195.  
  196. /**
  197. * ... declare the RobotPart interface and classes that implement it here
  198. * They will be _inner_ classes.
  199. */
  200. public interface RobotPart
  201. {
  202. /**
  203. * This initializes the bot.
  204. */
  205. public void init();
  206.  
  207.  
  208. /**
  209. * This moves the bot.
  210. */
  211. public void move();
  212.  
  213. }
  214.  
  215.  
  216. /**
  217. * This class controls the radar.
  218. */
  219. public class Radar implements RobotPart
  220. {
  221. /**
  222. * This controls the radar.
  223. */
  224. public void init()
  225. {
  226. // initialize radar operation
  227. setAdjustRadarForGunTurn( true );
  228. }
  229.  
  230.  
  231. /**
  232. * This moves the radar and turns it.
  233. */
  234. public void move()
  235. {
  236. // if there are less than 3 people.
  237. if ( getOthers() < 3 )
  238. {
  239.  
  240. // Absolute angle towards target
  241. double angleToEnemy = getHeadingRadians()
  242. + Math.toRadians( enemy.getBearing() );
  243.  
  244. // Subtract current radar heading to get the turn required to
  245. // face
  246. // the enemy, be sure it is normalized
  247. double radarTurn = Utils.normalRelativeAngle( angleToEnemy
  248. - getRadarHeadingRadians() );
  249.  
  250. // Distance we want to scan from middle of enemy to either side
  251. // The 36.0 is how many units from the center of the enemy
  252. // robot it scans.
  253. double extraTurn = Math.min( Math.atan( 36.0
  254. / enemy.getDistance() ), Rules.RADAR_TURN_RATE_RADIANS );
  255.  
  256. // Adjust the radar turn so it goes that much further in the
  257. // direction it is going to turn
  258. // Basically if we were going to turn it left, turn it even
  259. // more left, if right, turn more right.
  260. // This allows us to overshoot our enemy so that we get a good
  261. // sweep
  262. // that will not slip.
  263. radarTurn += ( radarTurn < 0 ? -extraTurn : extraTurn );
  264.  
  265. // Turn the radar
  266. setTurnRadarRightRadians( radarTurn );
  267. }
  268. // if there is more than 2 robots, do this.
  269. else
  270. {
  271. // Radar sweeps around constantly
  272. setTurnRadarRightRadians( 999999 );
  273. if ( !enemy.none() )
  274. {
  275. // if enemy, absolute bearing is that enemy's bearing
  276. double absoluteBearing = getHeadingRadians()
  277. + Math.toRadians( enemy.getBearing() );
  278. // if enemy is weak
  279. if ( enemy.getEnergy() < 20 )
  280. {
  281. // turn radar towards the enemy and face them
  282. // (predicting movement)
  283. setTurnRadarRightRadians(
  284. 3.5 * Utils.normalRelativeAngle( absoluteBearing
  285. - getRadarHeadingRadians() ) );
  286. }
  287. }
  288. }
  289. }
  290.  
  291.  
  292. /**
  293. * This is the boolean statement
  294. *
  295. * @param e
  296. * This is the event of scanning a robot.
  297. *
  298. * @return ( enemy.none() || e.getDistance() < enemy.getDistance() - 70
  299. * || e.getName().equals( enemy.getName() ) ) This determines
  300. * whether the enemy should be tracked.
  301. */
  302. public boolean shouldTrack( ScannedRobotEvent e )
  303. {
  304. // track if we have no enemy, the one we found is significantly
  305. // closer, or we scanned the one we've been tracking.
  306. return ( enemy.none() || e.getDistance() < enemy.getDistance()
  307. - 100
  308. || e.getName().equals( enemy.getName() ) );
  309. }
  310.  
  311.  
  312. /**
  313. * This controls the radar.
  314. *
  315. * @param e
  316. * This is the event of scanning a dead robot.
  317. *
  318. * @return e.getName().equals( enemy.getName() ) If enemy is dead, the
  319. * robot finds another target.
  320. */
  321. public boolean wasTracking( RobotDeathEvent e )
  322. {
  323. // if enemy is dead, the robot finds another target.
  324. return e.getName().equals( enemy.getName() );
  325. }
  326. }
  327.  
  328.  
  329. /**
  330. * This is the class that controls the gun.
  331. */
  332. public class Gun implements RobotPart
  333. {
  334. /**
  335. * This is the initialization method.
  336. */
  337. public void init()
  338. {
  339. // initialize gun operation
  340. setAdjustGunForRobotTurn( true );
  341. }
  342.  
  343.  
  344. /**
  345. * This moves the gun according to how we want it to.
  346. */
  347. public void move()
  348. {
  349. // don't shoot if I've got no enemy
  350. if ( enemy.none() )
  351. {
  352. return;
  353. }
  354. // if there are less than 3 enemies.
  355. if ( getOthers() < 3 )
  356. {
  357. // if an enemy is nearby
  358. if ( enemy.getDistance() > 20 )
  359. {
  360. // calculate firepower based on distance
  361. double firePower = Math.min( 500
  362. / enemy.getDistance(), 3 );
  363. // calculate speed of bullet
  364. double bulletSpeed = 20 - firePower * 3;
  365. // distance = rate * time, solved for time
  366. long time = (long)( enemy.getDistance() / bulletSpeed );
  367.  
  368. // calculate gun turn to predicted x,y location
  369. double futureX = enemy.getFutureX( time ) - 5;
  370. double futureY = enemy.getFutureY( time ) + 5;
  371. double absDeg = absoluteBearing( getX(), getY(), futureX,
  372. futureY );
  373. // non-predictive firing can be done like this:
  374. // double absDeg = absoluteBearing(getX(), getY(),
  375. // enemy.getX(),
  376. // enemy.getY());
  377.  
  378. // turn the gun to the predicted x,y location
  379. setTurnGunRight( normalizeBearing( absDeg
  380. - getGunHeading() ) );
  381.  
  382. // if the gun is cool and we're pointed in the right
  383. // direction,
  384. // shoot!
  385. if ( getGunHeat() == 0 && Math.abs( getGunTurnRemaining() )
  386. < 10 )
  387. {
  388. setFire( firePower );
  389. }
  390. }
  391. else
  392. {
  393. // if the robot is far away, get its bearing
  394. double absDeg = absoluteBearing( getX(), getY(),
  395. enemy.getX(), enemy.getY() );
  396. // turn gun towards them
  397. setTurnGunRight( normalizeBearing( absDeg
  398. - getGunHeading() ) );
  399. // fire weak bullets
  400. setFire( 10 );
  401. }
  402. }
  403. // if more than 2 enemies:
  404. else
  405. {
  406. // calculate firepower based on distance
  407. double firePower = Math.min( 500
  408. / enemy.getDistance(), 3 );
  409. // calculate speed of bullet
  410. double bulletSpeed = 20 - firePower * 3;
  411. // distance = rate * time, solved for time
  412. long time = (long)( enemy.getDistance() / bulletSpeed );
  413.  
  414. // calculate gun turn to predicted x,y location
  415. double futureX = enemy.getFutureX( time ) - 5;
  416. double futureY = enemy.getFutureY( time ) + 5;
  417. double absDeg = absoluteBearing( getX(), getY(), futureX,
  418. futureY );
  419. // non-predictive firing can be done like this:
  420. // double absDeg = absoluteBearing(getX(), getY(),
  421. // enemy.getX(),
  422. // enemy.getY());
  423.  
  424. // turn the gun to the predicted x,y location
  425. setTurnGunRight( normalizeBearing( absDeg
  426. - getGunHeading() ) );
  427.  
  428. // if the gun is cool and we're pointed in the right
  429. // direction,
  430. // shoot!
  431. if ( getGunHeat() == 0 && Math.abs( getGunTurnRemaining() )
  432. < 10 )
  433. {
  434. setFire( firePower );
  435. }
  436. }
  437. }
  438. }
  439.  
  440.  
  441. /**
  442. * This moves the tanks and turns it.
  443. */
  444. public class Tank implements RobotPart
  445. {
  446. /**
  447. * This is the initialization for the tank.
  448. */
  449. public void init()
  450. {
  451. // this is the initial coloration.
  452. setColors( Color.black, Color.black, Color.black, Color.black, Color.black );
  453. }
  454.  
  455. // this is used when strafing
  456. private byte moveDirection = 1;
  457.  
  458.  
  459. /**
  460. * This moves the tank to spiral around the enemy.
  461. */
  462. public void move()
  463. {
  464. // reverse direction if we stopped
  465. setTurnRight( normalizeBearing( enemy.getBearing() + 90 ) );
  466.  
  467. // strafe by changing direction every 20 ticks
  468. if ( getTime() % 20 == 0 )
  469. {
  470. moveDirection *= -1;
  471. setAhead( 150 * moveDirection );
  472. }
  473. if ( getVelocity() == 0 )
  474. {
  475. setMaxVelocity( 10 );
  476. moveDirection *= -1;
  477. }
  478. {
  479.  
  480. // square off against enemy
  481. setTurnRight( normalizeBearing( enemy.getBearing() + 90 ) );
  482. // checks to see if the robot is near any walls on the
  483. // plane
  484. if ( ( ( getX() < getBattleFieldWidth() - 20 ) || getX() > 20 ) || getY() > 20 )
  485. {
  486. //square off against our enemy
  487. setTurnRight( normalizeBearing( enemy.getBearing() + 110 ) );
  488. setAhead( 1000 * moveDirection );
  489. }
  490. else
  491. {
  492. // turns a random direction if near a wall.
  493. setTurnRight( getHeading() * moveDirection + rand.nextInt( 90 ) );
  494. setAhead( 250 * moveDirection );
  495.  
  496. }
  497. }
  498.  
  499. }
  500. }
  501. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement