You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Robot.java 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. Copyright (c) Xerox Corporation 1998-2002. All rights reserved.
  3. Use and copying of this software and preparation of derivative works based
  4. upon this software are permitted. Any distribution of this software or
  5. derivative works must comply with all applicable United States export control
  6. laws.
  7. This software is made available AS IS, and Xerox Corporation makes no warranty
  8. about the software, its performance or its conformity to any specification.
  9. |<--- this code is formatted to fit into 80 columns --->|
  10. |<--- this code is formatted to fit into 80 columns --->|
  11. |<--- this code is formatted to fit into 80 columns --->|
  12. Robot.java
  13. Part of the Spacewar system.
  14. */
  15. package spacewar;
  16. import java.util.Random;
  17. /**
  18. * Robot is an automatic pilot that now has quite a bit of intelligence.
  19. * So, beware !
  20. */
  21. class Robot extends Pilot implements Runnable {
  22. private static final int FIRE_INTERVAL = 60;
  23. private static final int REBIRTH_DELAY = 900;
  24. private final Random random = new Random();
  25. private Thread runner;
  26. private boolean runnable = true;
  27. Robot(Game theGame, int number) {
  28. super(theGame, number);
  29. }
  30. void start() {
  31. if (runner == null) {
  32. runner = new Thread(this);
  33. runner.start();
  34. }
  35. }
  36. void destroy() {
  37. if (runner != null) {
  38. runnable = false;
  39. runner = null;
  40. }
  41. }
  42. // A Robot tracks User-controlled ships and fires at them
  43. public void run() {
  44. Ship target = null;
  45. while(runnable) {
  46. // find target ship
  47. do {
  48. Ship[] potentials = getGame().getRegistry().getShips();
  49. if(potentials.length != 0)
  50. target = potentials[Math.abs(random.nextInt() % potentials.length)];
  51. sleepForABit(25);
  52. } while (target == ship);
  53. // main loop
  54. int currentRotation = Ship.STOP;
  55. int time;
  56. boolean currentlyAccelerating = false;
  57. double dx, dy, angleA, angleB, theta, dtheta, d,
  58. targetVel, a, b, c, targetXVel, targetYVel;
  59. while(true) {
  60. sleepForABit(FIRE_INTERVAL);
  61. // if my ship is destroyed, give me a new one
  62. if (!ship.isAlive()) {
  63. sleepForABit(REBIRTH_DELAY);
  64. getGame().newShip(this);
  65. }
  66. // find direction and distance from target to me
  67. dx = ship.getXPos() - target.getXPos();
  68. if (dx < - getGame().getWidth() / 2)
  69. dx += getGame().getWidth();
  70. if (dx > getGame().getWidth() / 2)
  71. dx -= getGame().getWidth();
  72. dy = ship.getYPos() - target.getYPos();
  73. if (dy < - getGame().getHeight() / 2)
  74. dy += getGame().getHeight();
  75. if (dy > getGame().getHeight() / 2)
  76. dy -= getGame().getHeight();
  77. d = Math.sqrt(dx * dx + dy * dy);
  78. angleA = Math.atan(dy / dx);
  79. if (dx < 0)
  80. angleA += Math.PI;
  81. // find relative velocity and trajectory of target
  82. targetXVel = target.getXVel() - ship.getXVel();
  83. targetYVel = target.getYVel() - ship.getYVel();
  84. targetVel = Math.sqrt(targetXVel * targetXVel +
  85. targetYVel * targetYVel);
  86. angleB = Math.atan(targetYVel / targetXVel);
  87. if (targetXVel < 0)
  88. angleB+=Math.PI;
  89. // find angle between line to target and taget's direction of travel
  90. theta = (angleA - angleB) % (2 * Math.PI);
  91. if (theta < -Math.PI)
  92. theta += 2 * Math.PI;
  93. if (theta > Math.PI)
  94. theta -= 2 * Math.PI;
  95. // calculate time to bullet impact using law of cosines
  96. a = targetVel * targetVel + Ship.BULLET_SPEED * Ship.BULLET_SPEED;
  97. b = d * targetVel * Math.cos(theta);
  98. c = - d * d;
  99. time = (int)((-b + Math.sqrt(b * b - 4 * a * c)) / 2 / a);
  100. // calculate angle and distance to bullet impact location
  101. dx = targetXVel * time - dx;
  102. dy = targetYVel * time - dy;
  103. theta = Math.atan(dy / dx);
  104. if(dx < 0)
  105. theta += Math.PI;
  106. // find desired change in rotation
  107. dtheta = (theta - ship.getOrientation()) % (2 * Math.PI);
  108. // find the shortest path to the desired orientation;
  109. if(dtheta < - Math.PI)
  110. dtheta += 2 * Math.PI;
  111. if(dtheta > Math.PI)
  112. dtheta -= 2 * Math.PI;
  113. // turn if nessecary
  114. if (dtheta > Ship.DEFAULT_ANGULAR_VELOCITY / 2) {
  115. if (currentRotation != Ship.CLOCKWISE)
  116. ship.rotate(currentRotation = Ship.CLOCKWISE);
  117. }
  118. else if (dtheta < -Ship.DEFAULT_ANGULAR_VELOCITY / 2) {
  119. if (currentRotation != Ship.COUNTERCLOCKWISE)
  120. ship.rotate(currentRotation = Ship.COUNTERCLOCKWISE);
  121. } // otherwise, fire, maybe even a burst
  122. else {
  123. if(currentRotation != Ship.STOP)
  124. ship.rotate(currentRotation = Ship.STOP);
  125. if (random.nextInt() % 40 == 0) {
  126. ship.fire();
  127. }
  128. }
  129. // randomly accelerate
  130. if (currentlyAccelerating && random.nextInt() % 2 == 0)
  131. ship.thrust(currentlyAccelerating = false);
  132. else {
  133. if (ship.getXVel() == 0)
  134. angleA = 0;
  135. else
  136. angleA = Math.atan(ship.getYVel() / ship.getXVel());
  137. if (ship.getXVel() < 0)
  138. angleA+=Math.PI;
  139. angleB = (angleA - ship.getOrientation()) % (2 * Math.PI);
  140. if (angleB < -Math.PI)
  141. angleB += 2 * Math.PI;
  142. if (angleB > Math.PI)
  143. angleB -= 2 * Math.PI;
  144. angleB = Math.abs(angleB);
  145. // angleB now represents the angle between the ship's
  146. // orientation and velocity vector. This will be used to
  147. // determine the probably that the ship will thrust to
  148. // prevent ships from accelerating too much in one direction
  149. if (random.nextInt() % (int)(12 * (Math.PI - angleB) + 1) == 0)
  150. ship.thrust(currentlyAccelerating = true);
  151. }
  152. // switch targets if current one has been destroyed
  153. if (target.getDamage() == 100)
  154. break;
  155. // randomly switch targets
  156. if (random.nextInt() % 4000 == 0)
  157. break;
  158. }
  159. }
  160. }
  161. void sleepForABit (int time) {
  162. try {
  163. runner.sleep(time);
  164. }
  165. catch (InterruptedException e) {}
  166. }
  167. }