attack.c revision 1.7 1 1.7 dholland /* $NetBSD: attack.c,v 1.7 2009/05/24 21:44:56 dholland Exp $ */
2 1.3 cgd
3 1.1 cgd /*
4 1.3 cgd * Copyright (c) 1980, 1993
5 1.3 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.5 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.4 christos #include <sys/cdefs.h>
33 1.1 cgd #ifndef lint
34 1.3 cgd #if 0
35 1.3 cgd static char sccsid[] = "@(#)attack.c 8.1 (Berkeley) 5/31/93";
36 1.3 cgd #else
37 1.7 dholland __RCSID("$NetBSD: attack.c,v 1.7 2009/05/24 21:44:56 dholland Exp $");
38 1.3 cgd #endif
39 1.1 cgd #endif /* not lint */
40 1.1 cgd
41 1.4 christos #include <stdio.h>
42 1.4 christos #include <math.h>
43 1.4 christos #include "trek.h"
44 1.1 cgd
45 1.1 cgd /*
46 1.1 cgd ** Klingon Attack Routine
47 1.1 cgd **
48 1.1 cgd ** This routine performs the Klingon attack provided that
49 1.1 cgd ** (1) Something happened this move (i.e., not free), and
50 1.1 cgd ** (2) You are not cloaked. Note that if you issue the
51 1.1 cgd ** cloak command, you are not considered cloaked until you
52 1.1 cgd ** expend some time.
53 1.1 cgd **
54 1.1 cgd ** Klingons are permitted to move both before and after the
55 1.1 cgd ** attack. They will tend to move toward you before the
56 1.1 cgd ** attack and away from you after the attack.
57 1.1 cgd **
58 1.1 cgd ** Under certain conditions you can get a critical hit. This
59 1.1 cgd ** sort of hit damages devices. The probability that a given
60 1.1 cgd ** device is damaged depends on the device. Well protected
61 1.1 cgd ** devices (such as the computer, which is in the core of the
62 1.1 cgd ** ship and has considerable redundancy) almost never get
63 1.1 cgd ** damaged, whereas devices which are exposed (such as the
64 1.1 cgd ** warp engines) or which are particularly delicate (such as
65 1.1 cgd ** the transporter) have a much higher probability of being
66 1.1 cgd ** damaged.
67 1.1 cgd **
68 1.1 cgd ** The actual amount of damage (i.e., how long it takes to fix
69 1.1 cgd ** it) depends on the amount of the hit and the "damfac[]"
70 1.1 cgd ** entry for the particular device.
71 1.1 cgd **
72 1.1 cgd ** Casualties can also occur.
73 1.6 dholland **
74 1.6 dholland ** resting -- set if attack while resting
75 1.1 cgd */
76 1.1 cgd
77 1.4 christos void
78 1.6 dholland attack(int resting)
79 1.1 cgd {
80 1.4 christos int hit, i, l;
81 1.4 christos int maxhit, tothit, shldabsb;
82 1.4 christos double chgfac, propor, extradm;
83 1.4 christos double dustfac, tothe;
84 1.4 christos int cas;
85 1.4 christos int hitflag;
86 1.1 cgd
87 1.1 cgd if (Move.free)
88 1.1 cgd return;
89 1.1 cgd if (Etc.nkling <= 0 || Quad[Ship.quadx][Ship.quady].stars < 0)
90 1.1 cgd return;
91 1.1 cgd if (Ship.cloaked && Ship.cloakgood)
92 1.1 cgd return;
93 1.1 cgd /* move before attack */
94 1.1 cgd klmove(0);
95 1.7 dholland if (Ship.cond == DOCKED) {
96 1.1 cgd if (!resting)
97 1.1 cgd printf("Starbase shields protect the %s\n", Ship.shipname);
98 1.1 cgd return;
99 1.1 cgd }
100 1.1 cgd /* setup shield effectiveness */
101 1.1 cgd chgfac = 1.0;
102 1.1 cgd if (Move.shldchg)
103 1.1 cgd chgfac = 0.25 + 0.50 * franf();
104 1.1 cgd maxhit = tothit = 0;
105 1.1 cgd hitflag = 0;
106 1.1 cgd
107 1.1 cgd /* let each Klingon do his damndest */
108 1.7 dholland for (i = 0; i < Etc.nkling; i++) {
109 1.1 cgd /* if he's low on power he won't attack */
110 1.1 cgd if (Etc.klingon[i].power < 20)
111 1.1 cgd continue;
112 1.7 dholland if (!hitflag) {
113 1.1 cgd printf("\nStardate %.2f: Klingon attack:\n",
114 1.1 cgd Now.date);
115 1.1 cgd hitflag++;
116 1.1 cgd }
117 1.1 cgd /* complete the hit */
118 1.1 cgd dustfac = 0.90 + 0.01 * franf();
119 1.1 cgd tothe = Etc.klingon[i].avgdist;
120 1.1 cgd hit = Etc.klingon[i].power * pow(dustfac, tothe) * Param.hitfac;
121 1.1 cgd /* deplete his energy */
122 1.1 cgd dustfac = Etc.klingon[i].power;
123 1.1 cgd Etc.klingon[i].power = dustfac * Param.phasfac * (1.0 + (franf() - 0.5) * 0.2);
124 1.1 cgd /* see how much of hit shields will absorb */
125 1.1 cgd shldabsb = 0;
126 1.7 dholland if (Ship.shldup || Move.shldchg) {
127 1.1 cgd propor = Ship.shield;
128 1.1 cgd propor /= Param.shield;
129 1.1 cgd shldabsb = propor * chgfac * hit;
130 1.1 cgd if (shldabsb > Ship.shield)
131 1.1 cgd shldabsb = Ship.shield;
132 1.1 cgd Ship.shield -= shldabsb;
133 1.1 cgd }
134 1.1 cgd /* actually do the hit */
135 1.1 cgd printf("HIT: %d units", hit);
136 1.1 cgd if (!damaged(SRSCAN))
137 1.1 cgd printf(" from %d,%d", Etc.klingon[i].x, Etc.klingon[i].y);
138 1.1 cgd cas = (shldabsb * 100) / hit;
139 1.1 cgd hit -= shldabsb;
140 1.1 cgd if (shldabsb > 0)
141 1.1 cgd printf(", shields absorb %d%%, effective hit %d\n",
142 1.1 cgd cas, hit);
143 1.1 cgd else
144 1.1 cgd printf("\n");
145 1.1 cgd tothit += hit;
146 1.1 cgd if (hit > maxhit)
147 1.1 cgd maxhit = hit;
148 1.1 cgd Ship.energy -= hit;
149 1.1 cgd /* see if damages occurred */
150 1.7 dholland if (hit >= (15 - Game.skill) * (25 - ranf(12))) {
151 1.1 cgd printf("CRITICAL HIT!!!\n");
152 1.1 cgd /* select a device from probability vector */
153 1.1 cgd cas = ranf(1000);
154 1.1 cgd for (l = 0; cas >= 0; l++)
155 1.1 cgd cas -= Param.damprob[l];
156 1.1 cgd l -= 1;
157 1.1 cgd /* compute amount of damage */
158 1.1 cgd extradm = (hit * Param.damfac[l]) / (75 + ranf(25)) + 0.5;
159 1.1 cgd /* damage the device */
160 1.1 cgd damage(l, extradm);
161 1.7 dholland if (damaged(SHIELD)) {
162 1.1 cgd if (Ship.shldup)
163 1.1 cgd printf("Sulu: Shields knocked down, captain.\n");
164 1.1 cgd Ship.shldup = 0;
165 1.1 cgd Move.shldchg = 0;
166 1.1 cgd }
167 1.1 cgd }
168 1.1 cgd if (Ship.energy <= 0)
169 1.1 cgd lose(L_DSTRYD);
170 1.1 cgd }
171 1.1 cgd
172 1.1 cgd /* see what our casualities are like */
173 1.7 dholland if (maxhit >= 200 || tothit >= 500) {
174 1.1 cgd cas = tothit * 0.015 * franf();
175 1.7 dholland if (cas >= 2) {
176 1.1 cgd printf("McCoy: we suffered %d casualties in that attack.\n",
177 1.1 cgd cas);
178 1.1 cgd Game.deaths += cas;
179 1.1 cgd Ship.crew -= cas;
180 1.1 cgd }
181 1.1 cgd }
182 1.1 cgd
183 1.1 cgd /* allow Klingons to move after attacking */
184 1.1 cgd klmove(1);
185 1.1 cgd
186 1.1 cgd return;
187 1.1 cgd }
188