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