torped.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /* $NetBSD: torped.c,v 1.8 2004/01/27 20:30:31 jsm Exp $ */
  2. /*
  3. * Copyright (c) 1980, 1993
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the University nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. */
  30. #include <sys/cdefs.h>
  31. #ifndef lint
  32. #if 0
  33. static char sccsid[] = "@(#)torped.c 8.1 (Berkeley) 5/31/93";
  34. #else
  35. __RCSID("$NetBSD: torped.c,v 1.8 2004/01/27 20:30:31 jsm Exp $");
  36. #endif
  37. #endif /* not lint */
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <math.h>
  41. #include "trek.h"
  42. #include "getpar.h"
  43. /*
  44. ** PHOTON TORPEDO CONTROL
  45. **
  46. ** Either one or three photon torpedoes are fired. If three
  47. ** are fired, it is called a "burst" and you also specify
  48. ** a spread angle.
  49. **
  50. ** Torpedoes are never 100% accurate. There is always a random
  51. ** cludge factor in their course which is increased if you have
  52. ** your shields up. Hence, you will find that they are more
  53. ** accurate at close range. However, they have the advantage that
  54. ** at long range they don't lose any of their power as phasers
  55. ** do, i.e., a hit is a hit is a hit, by any other name.
  56. **
  57. ** When the course spreads too much, you get a misfire, and the
  58. ** course is randomized even more. You also have the chance that
  59. ** the misfire damages your torpedo tubes.
  60. */
  61. static int randcourse(int);
  62. /*ARGSUSED*/
  63. void
  64. torped(v)
  65. int v __attribute__((__unused__));
  66. {
  67. int ix, iy;
  68. double x, y, dx, dy;
  69. double angle;
  70. int course, course2;
  71. int k;
  72. double bigger;
  73. double sectsize;
  74. int burst;
  75. int n;
  76. if (Ship.cloaked)
  77. {
  78. printf("Federation regulations do not permit attack while cloaked.\n");
  79. return;
  80. }
  81. if (check_out(TORPED))
  82. return;
  83. if (Ship.torped <= 0)
  84. {
  85. printf("All photon torpedos expended\n");
  86. return;
  87. }
  88. /* get the course */
  89. course = getintpar("Torpedo course");
  90. if (course < 0 || course > 360)
  91. return;
  92. burst = -1;
  93. /* need at least three torpedoes for a burst */
  94. if (Ship.torped < 3)
  95. {
  96. printf("No-burst mode selected\n");
  97. burst = 0;
  98. }
  99. else
  100. {
  101. /* see if the user wants one */
  102. if (!testnl())
  103. {
  104. k = ungetc(cgetc(0), stdin);
  105. if (k >= '0' && k <= '9')
  106. burst = 1;
  107. }
  108. }
  109. if (burst < 0)
  110. {
  111. burst = getynpar("Do you want a burst");
  112. }
  113. if (burst)
  114. {
  115. burst = getintpar("burst angle");
  116. if (burst <= 0)
  117. return;
  118. if (burst > 15) {
  119. printf("Maximum burst angle is 15 degrees\n");
  120. return;
  121. }
  122. }
  123. sectsize = NSECTS;
  124. n = -1;
  125. if (burst)
  126. {
  127. n = 1;
  128. course -= burst;
  129. }
  130. for (; n && n <= 3; n++)
  131. {
  132. /* select a nice random course */
  133. course2 = course + randcourse(n);
  134. angle = course2 * 0.0174532925; /* convert to radians */
  135. dx = -cos(angle);
  136. dy = sin(angle);
  137. bigger = fabs(dx);
  138. x = fabs(dy);
  139. if (x > bigger)
  140. bigger = x;
  141. dx /= bigger;
  142. dy /= bigger;
  143. x = Ship.sectx + 0.5;
  144. y = Ship.secty + 0.5;
  145. if (Ship.cond != DOCKED)
  146. Ship.torped -= 1;
  147. printf("Torpedo track");
  148. if (n > 0)
  149. printf(", torpedo number %d", n);
  150. printf(":\n%6.1f\t%4.1f\n", x, y);
  151. while (1)
  152. {
  153. ix = x += dx;
  154. iy = y += dy;
  155. if (x < 0.0 || x >= sectsize || y < 0.0 || y >= sectsize)
  156. {
  157. printf("Torpedo missed\n");
  158. break;
  159. }
  160. printf("%6.1f\t%4.1f\n", x, y);
  161. switch (Sect[ix][iy])
  162. {
  163. case EMPTY:
  164. continue;
  165. case HOLE:
  166. printf("Torpedo disappears into a black hole\n");
  167. break;
  168. case KLINGON:
  169. for (k = 0; k < Etc.nkling; k++)
  170. {
  171. if (Etc.klingon[k].x != ix || Etc.klingon[k].y != iy)
  172. continue;
  173. Etc.klingon[k].power -= 500 + ranf(501);
  174. if (Etc.klingon[k].power > 0)
  175. {
  176. printf("*** Hit on Klingon at %d,%d: extensive damages\n",
  177. ix, iy);
  178. break;
  179. }
  180. killk(ix, iy);
  181. break;
  182. }
  183. break;
  184. case STAR:
  185. nova(ix, iy);
  186. break;
  187. case INHABIT:
  188. kills(ix, iy, -1);
  189. break;
  190. case BASE:
  191. killb(Ship.quadx, Ship.quady);
  192. Game.killb += 1;
  193. break;
  194. default:
  195. printf("Unknown object %c at %d,%d destroyed\n",
  196. Sect[ix][iy], ix, iy);
  197. Sect[ix][iy] = EMPTY;
  198. break;
  199. }
  200. break;
  201. }
  202. if (damaged(TORPED) || Quad[Ship.quadx][Ship.quady].stars < 0)
  203. break;
  204. course += burst;
  205. }
  206. Move.free = 0;
  207. }
  208. /*
  209. ** RANDOMIZE COURSE
  210. **
  211. ** This routine randomizes the course for torpedo number 'n'.
  212. ** Other things handled by this routine are misfires, damages
  213. ** to the tubes, etc.
  214. */
  215. static int
  216. randcourse(n)
  217. int n;
  218. {
  219. double r;
  220. int d;
  221. d = ((franf() + franf()) - 1.0) * 20;
  222. if (abs(d) > 12)
  223. {
  224. printf("Photon tubes misfire");
  225. if (n < 0)
  226. printf("\n");
  227. else
  228. printf(" on torpedo %d\n", n);
  229. if (ranf(2))
  230. {
  231. damage(TORPED, 0.2 * abs(d) * (franf() + 1.0));
  232. }
  233. d *= 1.0 + 2.0 * franf();
  234. }
  235. if (Ship.shldup || Ship.cond == DOCKED)
  236. {
  237. r = Ship.shield;
  238. r = 1.0 + r / Param.shield;
  239. if (Ship.cond == DOCKED)
  240. r = 2.0;
  241. d *= r;
  242. }
  243. return (d);
  244. }