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