Advertisement
Guest User

Untitled

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