attack.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* $NetBSD: attack.c,v 1.5 2003/08/07 09:37:49 agc 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[] = "@(#)attack.c 8.1 (Berkeley) 5/31/93";
  34. #else
  35. __RCSID("$NetBSD: attack.c,v 1.5 2003/08/07 09:37:49 agc Exp $");
  36. #endif
  37. #endif /* not lint */
  38. #include <stdio.h>
  39. #include <math.h>
  40. #include "trek.h"
  41. /*
  42. ** Klingon Attack Routine
  43. **
  44. ** This routine performs the Klingon attack provided that
  45. ** (1) Something happened this move (i.e., not free), and
  46. ** (2) You are not cloaked. Note that if you issue the
  47. ** cloak command, you are not considered cloaked until you
  48. ** expend some time.
  49. **
  50. ** Klingons are permitted to move both before and after the
  51. ** attack. They will tend to move toward you before the
  52. ** attack and away from you after the attack.
  53. **
  54. ** Under certain conditions you can get a critical hit. This
  55. ** sort of hit damages devices. The probability that a given
  56. ** device is damaged depends on the device. Well protected
  57. ** devices (such as the computer, which is in the core of the
  58. ** ship and has considerable redundancy) almost never get
  59. ** damaged, whereas devices which are exposed (such as the
  60. ** warp engines) or which are particularly delicate (such as
  61. ** the transporter) have a much higher probability of being
  62. ** damaged.
  63. **
  64. ** The actual amount of damage (i.e., how long it takes to fix
  65. ** it) depends on the amount of the hit and the "damfac[]"
  66. ** entry for the particular device.
  67. **
  68. ** Casualties can also occur.
  69. */
  70. void
  71. attack(resting)
  72. int resting; /* set if attack while resting */
  73. {
  74. int hit, i, l;
  75. int maxhit, tothit, shldabsb;
  76. double chgfac, propor, extradm;
  77. double dustfac, tothe;
  78. int cas;
  79. int hitflag;
  80. if (Move.free)
  81. return;
  82. if (Etc.nkling <= 0 || Quad[Ship.quadx][Ship.quady].stars < 0)
  83. return;
  84. if (Ship.cloaked && Ship.cloakgood)
  85. return;
  86. /* move before attack */
  87. klmove(0);
  88. if (Ship.cond == DOCKED)
  89. {
  90. if (!resting)
  91. printf("Starbase shields protect the %s\n", Ship.shipname);
  92. return;
  93. }
  94. /* setup shield effectiveness */
  95. chgfac = 1.0;
  96. if (Move.shldchg)
  97. chgfac = 0.25 + 0.50 * franf();
  98. maxhit = tothit = 0;
  99. hitflag = 0;
  100. /* let each Klingon do his damndest */
  101. for (i = 0; i < Etc.nkling; i++)
  102. {
  103. /* if he's low on power he won't attack */
  104. if (Etc.klingon[i].power < 20)
  105. continue;
  106. if (!hitflag)
  107. {
  108. printf("\nStardate %.2f: Klingon attack:\n",
  109. Now.date);
  110. hitflag++;
  111. }
  112. /* complete the hit */
  113. dustfac = 0.90 + 0.01 * franf();
  114. tothe = Etc.klingon[i].avgdist;
  115. hit = Etc.klingon[i].power * pow(dustfac, tothe) * Param.hitfac;
  116. /* deplete his energy */
  117. dustfac = Etc.klingon[i].power;
  118. Etc.klingon[i].power = dustfac * Param.phasfac * (1.0 + (franf() - 0.5) * 0.2);
  119. /* see how much of hit shields will absorb */
  120. shldabsb = 0;
  121. if (Ship.shldup || Move.shldchg)
  122. {
  123. propor = Ship.shield;
  124. propor /= Param.shield;
  125. shldabsb = propor * chgfac * hit;
  126. if (shldabsb > Ship.shield)
  127. shldabsb = Ship.shield;
  128. Ship.shield -= shldabsb;
  129. }
  130. /* actually do the hit */
  131. printf("HIT: %d units", hit);
  132. if (!damaged(SRSCAN))
  133. printf(" from %d,%d", Etc.klingon[i].x, Etc.klingon[i].y);
  134. cas = (shldabsb * 100) / hit;
  135. hit -= shldabsb;
  136. if (shldabsb > 0)
  137. printf(", shields absorb %d%%, effective hit %d\n",
  138. cas, hit);
  139. else
  140. printf("\n");
  141. tothit += hit;
  142. if (hit > maxhit)
  143. maxhit = hit;
  144. Ship.energy -= hit;
  145. /* see if damages occurred */
  146. if (hit >= (15 - Game.skill) * (25 - ranf(12)))
  147. {
  148. printf("CRITICAL HIT!!!\n");
  149. /* select a device from probability vector */
  150. cas = ranf(1000);
  151. for (l = 0; cas >= 0; l++)
  152. cas -= Param.damprob[l];
  153. l -= 1;
  154. /* compute amount of damage */
  155. extradm = (hit * Param.damfac[l]) / (75 + ranf(25)) + 0.5;
  156. /* damage the device */
  157. damage(l, extradm);
  158. if (damaged(SHIELD))
  159. {
  160. if (Ship.shldup)
  161. printf("Sulu: Shields knocked down, captain.\n");
  162. Ship.shldup = 0;
  163. Move.shldchg = 0;
  164. }
  165. }
  166. if (Ship.energy <= 0)
  167. lose(L_DSTRYD);
  168. }
  169. /* see what our casualities are like */
  170. if (maxhit >= 200 || tothit >= 500)
  171. {
  172. cas = tothit * 0.015 * franf();
  173. if (cas >= 2)
  174. {
  175. printf("McCoy: we suffered %d casualties in that attack.\n",
  176. cas);
  177. Game.deaths += cas;
  178. Ship.crew -= cas;
  179. }
  180. }
  181. /* allow Klingons to move after attacking */
  182. klmove(1);
  183. return;
  184. }