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