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