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