xref: /netbsd-src/games/trek/torped.c (revision edfa83365254b6d7c6cdaa0d30b214319daeee7f)
1 /*
2  * Copyright (c) 1980 Regents of the University of California.
3  * 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 /*static char sccsid[] = "from: @(#)torped.c	5.4 (Berkeley) 6/1/90";*/
36 static char rcsid[] = "$Id: torped.c,v 1.2 1993/08/01 18:49:55 mycroft Exp $";
37 #endif /* not lint */
38 
39 # include	<stdio.h>
40 # include	"trek.h"
41 
42 /*
43 **  PHOTON TORPEDO CONTROL
44 **
45 **	Either one or three photon torpedoes are fired.  If three
46 **	are fired, it is called a "burst" and you also specify
47 **	a spread angle.
48 **
49 **	Torpedoes are never 100% accurate.  There is always a random
50 **	cludge factor in their course which is increased if you have
51 **	your shields up.  Hence, you will find that they are more
52 **	accurate at close range.  However, they have the advantage that
53 **	at long range they don't lose any of their power as phasers
54 **	do, i.e., a hit is a hit is a hit, by any other name.
55 **
56 **	When the course spreads too much, you get a misfire, and the
57 **	course is randomized even more.  You also have the chance that
58 **	the misfire damages your torpedo tubes.
59 */
60 
61 
62 torped()
63 {
64 	register int		ix, iy;
65 	double			x, y, dx, dy;
66 	double			angle;
67 	int			course, course2;
68 	register int		k;
69 	double			bigger;
70 	double			sectsize;
71 	int			burst;
72 	int			n;
73 
74 	if (Ship.cloaked)
75 	{
76 		return (printf("Federation regulations do not permit attack while cloaked.\n"));
77 	}
78 	if (check_out(TORPED))
79 		return;
80 	if (Ship.torped <= 0)
81 	{
82 		return (printf("All photon torpedos expended\n"));
83 	}
84 
85 	/* get the course */
86 	course = getintpar("Torpedo course");
87 	if (course < 0 || course > 360)
88 		return;
89 	burst = -1;
90 
91 	/* need at least three torpedoes for a burst */
92 	if (Ship.torped < 3)
93 	{
94 		printf("No-burst mode selected\n");
95 		burst = 0;
96 	}
97 	else
98 	{
99 		/* see if the user wants one */
100 		if (!testnl())
101 		{
102 			k = ungetc(cgetc(0), stdin);
103 			if (k >= '0' && k <= '9')
104 				burst = 1;
105 		}
106 	}
107 	if (burst < 0)
108 	{
109 		burst = getynpar("Do you want a burst");
110 	}
111 	if (burst)
112 	{
113 		burst = getintpar("burst angle");
114 		if (burst <= 0)
115 			return;
116 		if (burst > 15)
117 			return (printf("Maximum burst angle is 15 degrees\n"));
118 	}
119 	sectsize = NSECTS;
120 	n = -1;
121 	if (burst)
122 	{
123 		n = 1;
124 		course -= burst;
125 	}
126 	for (; n && n <= 3; n++)
127 	{
128 		/* select a nice random course */
129 		course2 = course + randcourse(n);
130 		angle = course2 * 0.0174532925;			/* convert to radians */
131 		dx = -cos(angle);
132 		dy =  sin(angle);
133 		bigger = fabs(dx);
134 		x = fabs(dy);
135 		if (x > bigger)
136 			bigger = x;
137 		dx /= bigger;
138 		dy /= bigger;
139 		x = Ship.sectx + 0.5;
140 		y = Ship.secty + 0.5;
141 		if (Ship.cond != DOCKED)
142 			Ship.torped -= 1;
143 		printf("Torpedo track");
144 		if (n > 0)
145 			printf(", torpedo number %d", n);
146 		printf(":\n%6.1f\t%4.1f\n", x, y);
147 		while (1)
148 		{
149 			ix = x += dx;
150 			iy = y += dy;
151 			if (x < 0.0 || x >= sectsize || y < 0.0 || y >= sectsize)
152 			{
153 				printf("Torpedo missed\n");
154 				break;
155 			}
156 			printf("%6.1f\t%4.1f\n", x, y);
157 			switch (Sect[ix][iy])
158 			{
159 			  case EMPTY:
160 				continue;
161 
162 			  case HOLE:
163 				printf("Torpedo disappears into a black hole\n");
164 				break;
165 
166 			  case KLINGON:
167 				for (k = 0; k < Etc.nkling; k++)
168 				{
169 					if (Etc.klingon[k].x != ix || Etc.klingon[k].y != iy)
170 						continue;
171 					Etc.klingon[k].power -= 500 + ranf(501);
172 					if (Etc.klingon[k].power > 0)
173 					{
174 						printf("*** Hit on Klingon at %d,%d: extensive damages\n",
175 							ix, iy);
176 						break;
177 					}
178 					killk(ix, iy);
179 					break;
180 				}
181 				break;
182 
183 			  case STAR:
184 				nova(ix, iy);
185 				break;
186 
187 			  case INHABIT:
188 				kills(ix, iy, -1);
189 				break;
190 
191 			  case BASE:
192 				killb(Ship.quadx, Ship.quady);
193 				Game.killb += 1;
194 				break;
195 			  default:
196 				printf("Unknown object %c at %d,%d destroyed\n",
197 					Sect[ix][iy], ix, iy);
198 				Sect[ix][iy] = EMPTY;
199 				break;
200 			}
201 			break;
202 		}
203 		if (damaged(TORPED) || Quad[Ship.quadx][Ship.quady].stars < 0)
204 			break;
205 		course += burst;
206 	}
207 	Move.free = 0;
208 }
209 
210 
211 /*
212 **  RANDOMIZE COURSE
213 **
214 **	This routine randomizes the course for torpedo number 'n'.
215 **	Other things handled by this routine are misfires, damages
216 **	to the tubes, etc.
217 */
218 
219 randcourse(n)
220 int	n;
221 {
222 	double			r;
223 	register int		d;
224 
225 	d = ((franf() + franf()) - 1.0) * 20;
226 	if (abs(d) > 12)
227 	{
228 		printf("Photon tubes misfire");
229 		if (n < 0)
230 			printf("\n");
231 		else
232 			printf(" on torpedo %d\n", n);
233 		if (ranf(2))
234 		{
235 			damage(TORPED, 0.2 * abs(d) * (franf() + 1.0));
236 		}
237 		d *= 1.0 + 2.0 * franf();
238 	}
239 	if (Ship.shldup || Ship.cond == DOCKED)
240 	{
241 		r = Ship.shield;
242 		r = 1.0 + r / Param.shield;
243 		if (Ship.cond == DOCKED)
244 			r = 2.0;
245 		d *= r;
246 	}
247 	return (d);
248 }
249