attack.c revision 1.5 1 1.5 agc /* $NetBSD: attack.c,v 1.5 2003/08/07 09:37:49 agc 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.5 agc __RCSID("$NetBSD: attack.c,v 1.5 2003/08/07 09:37:49 agc 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.1 cgd */
74 1.1 cgd
75 1.4 christos void
76 1.1 cgd attack(resting)
77 1.1 cgd int resting; /* set if attack while resting */
78 1.1 cgd {
79 1.4 christos int hit, i, l;
80 1.4 christos int maxhit, tothit, shldabsb;
81 1.4 christos double chgfac, propor, extradm;
82 1.4 christos double dustfac, tothe;
83 1.4 christos int cas;
84 1.4 christos int hitflag;
85 1.1 cgd
86 1.1 cgd if (Move.free)
87 1.1 cgd return;
88 1.1 cgd if (Etc.nkling <= 0 || Quad[Ship.quadx][Ship.quady].stars < 0)
89 1.1 cgd return;
90 1.1 cgd if (Ship.cloaked && Ship.cloakgood)
91 1.1 cgd return;
92 1.1 cgd /* move before attack */
93 1.1 cgd klmove(0);
94 1.1 cgd if (Ship.cond == DOCKED)
95 1.1 cgd {
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.1 cgd for (i = 0; i < Etc.nkling; i++)
109 1.1 cgd {
110 1.1 cgd /* if he's low on power he won't attack */
111 1.1 cgd if (Etc.klingon[i].power < 20)
112 1.1 cgd continue;
113 1.1 cgd if (!hitflag)
114 1.1 cgd {
115 1.1 cgd printf("\nStardate %.2f: Klingon attack:\n",
116 1.1 cgd Now.date);
117 1.1 cgd hitflag++;
118 1.1 cgd }
119 1.1 cgd /* complete the hit */
120 1.1 cgd dustfac = 0.90 + 0.01 * franf();
121 1.1 cgd tothe = Etc.klingon[i].avgdist;
122 1.1 cgd hit = Etc.klingon[i].power * pow(dustfac, tothe) * Param.hitfac;
123 1.1 cgd /* deplete his energy */
124 1.1 cgd dustfac = Etc.klingon[i].power;
125 1.1 cgd Etc.klingon[i].power = dustfac * Param.phasfac * (1.0 + (franf() - 0.5) * 0.2);
126 1.1 cgd /* see how much of hit shields will absorb */
127 1.1 cgd shldabsb = 0;
128 1.1 cgd if (Ship.shldup || Move.shldchg)
129 1.1 cgd {
130 1.1 cgd propor = Ship.shield;
131 1.1 cgd propor /= Param.shield;
132 1.1 cgd shldabsb = propor * chgfac * hit;
133 1.1 cgd if (shldabsb > Ship.shield)
134 1.1 cgd shldabsb = Ship.shield;
135 1.1 cgd Ship.shield -= shldabsb;
136 1.1 cgd }
137 1.1 cgd /* actually do the hit */
138 1.1 cgd printf("HIT: %d units", hit);
139 1.1 cgd if (!damaged(SRSCAN))
140 1.1 cgd printf(" from %d,%d", Etc.klingon[i].x, Etc.klingon[i].y);
141 1.1 cgd cas = (shldabsb * 100) / hit;
142 1.1 cgd hit -= shldabsb;
143 1.1 cgd if (shldabsb > 0)
144 1.1 cgd printf(", shields absorb %d%%, effective hit %d\n",
145 1.1 cgd cas, hit);
146 1.1 cgd else
147 1.1 cgd printf("\n");
148 1.1 cgd tothit += hit;
149 1.1 cgd if (hit > maxhit)
150 1.1 cgd maxhit = hit;
151 1.1 cgd Ship.energy -= hit;
152 1.1 cgd /* see if damages occurred */
153 1.1 cgd if (hit >= (15 - Game.skill) * (25 - ranf(12)))
154 1.1 cgd {
155 1.1 cgd printf("CRITICAL HIT!!!\n");
156 1.1 cgd /* select a device from probability vector */
157 1.1 cgd cas = ranf(1000);
158 1.1 cgd for (l = 0; cas >= 0; l++)
159 1.1 cgd cas -= Param.damprob[l];
160 1.1 cgd l -= 1;
161 1.1 cgd /* compute amount of damage */
162 1.1 cgd extradm = (hit * Param.damfac[l]) / (75 + ranf(25)) + 0.5;
163 1.1 cgd /* damage the device */
164 1.1 cgd damage(l, extradm);
165 1.1 cgd if (damaged(SHIELD))
166 1.1 cgd {
167 1.1 cgd if (Ship.shldup)
168 1.1 cgd printf("Sulu: Shields knocked down, captain.\n");
169 1.1 cgd Ship.shldup = 0;
170 1.1 cgd Move.shldchg = 0;
171 1.1 cgd }
172 1.1 cgd }
173 1.1 cgd if (Ship.energy <= 0)
174 1.1 cgd lose(L_DSTRYD);
175 1.1 cgd }
176 1.1 cgd
177 1.1 cgd /* see what our casualities are like */
178 1.1 cgd if (maxhit >= 200 || tothit >= 500)
179 1.1 cgd {
180 1.1 cgd cas = tothit * 0.015 * franf();
181 1.1 cgd if (cas >= 2)
182 1.1 cgd {
183 1.1 cgd printf("McCoy: we suffered %d casualties in that attack.\n",
184 1.1 cgd cas);
185 1.1 cgd Game.deaths += cas;
186 1.1 cgd Ship.crew -= cas;
187 1.1 cgd }
188 1.1 cgd }
189 1.1 cgd
190 1.1 cgd /* allow Klingons to move after attacking */
191 1.1 cgd klmove(1);
192 1.1 cgd
193 1.1 cgd return;
194 1.1 cgd }
195