Home | History | Annotate | Line # | Download | only in trek
phaser.c revision 1.8
      1  1.8    itojun /*	$NetBSD: phaser.c,v 1.8 2000/07/10 10:19:27 itojun 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.5  christos #include <sys/cdefs.h>
     37  1.1       cgd #ifndef lint
     38  1.3       cgd #if 0
     39  1.3       cgd static char sccsid[] = "@(#)phaser.c	8.1 (Berkeley) 5/31/93";
     40  1.3       cgd #else
     41  1.8    itojun __RCSID("$NetBSD: phaser.c,v 1.8 2000/07/10 10:19:27 itojun Exp $");
     42  1.3       cgd #endif
     43  1.1       cgd #endif /* not lint */
     44  1.1       cgd 
     45  1.5  christos #include <stdio.h>
     46  1.5  christos #include <math.h>
     47  1.5  christos #include "trek.h"
     48  1.5  christos #include "getpar.h"
     49  1.1       cgd 
     50  1.1       cgd /* factors for phaser hits; see description below */
     51  1.1       cgd 
     52  1.1       cgd # define	ALPHA		3.0		/* spread */
     53  1.1       cgd # define	BETA		3.0		/* franf() */
     54  1.1       cgd # define	GAMMA		0.30		/* cos(angle) */
     55  1.1       cgd # define	EPSILON		150.0		/* dist ** 2 */
     56  1.1       cgd # define	OMEGA		10.596		/* overall scaling factor */
     57  1.1       cgd 
     58  1.1       cgd /* OMEGA ~= 100 * (ALPHA + 1) * (BETA + 1) / (EPSILON + 1) */
     59  1.1       cgd 
     60  1.1       cgd /*
     61  1.1       cgd **  Phaser Control
     62  1.1       cgd **
     63  1.1       cgd **	There are up to NBANKS phaser banks which may be fired
     64  1.1       cgd **	simultaneously.  There are two modes, "manual" and
     65  1.1       cgd **	"automatic".  In manual mode, you specify exactly which
     66  1.1       cgd **	direction you want each bank to be aimed, the number
     67  1.1       cgd **	of units to fire, and the spread angle.  In automatic
     68  1.1       cgd **	mode, you give only the total number of units to fire.
     69  1.1       cgd **
     70  1.1       cgd **	The spread is specified as a number between zero and
     71  1.1       cgd **	one, with zero being minimum spread and one being maximum
     72  1.1       cgd **	spread.  You  will normally want zero spread, unless your
     73  1.1       cgd **	short range scanners are out, in which case you probably
     74  1.1       cgd **	don't know exactly where the Klingons are.  In that case,
     75  1.1       cgd **	you really don't have any choice except to specify a
     76  1.1       cgd **	fairly large spread.
     77  1.1       cgd **
     78  1.1       cgd **	Phasers spread slightly, even if you specify zero spread.
     79  1.1       cgd **
     80  1.1       cgd **	Uses trace flag 30
     81  1.1       cgd */
     82  1.1       cgd 
     83  1.1       cgd struct cvntab	Matab[] =
     84  1.1       cgd {
     85  1.5  christos 	{ "m",		"anual",	(cmdfun) 1,	0 },
     86  1.5  christos 	{ "a",		"utomatic",	(cmdfun) 0,	0 },
     87  1.5  christos 	{ NULL,		NULL,		NULL,		0 }
     88  1.1       cgd };
     89  1.1       cgd 
     90  1.1       cgd struct banks
     91  1.1       cgd {
     92  1.1       cgd 	int	units;
     93  1.1       cgd 	double	angle;
     94  1.1       cgd 	double	spread;
     95  1.1       cgd };
     96  1.1       cgd 
     97  1.1       cgd 
     98  1.1       cgd 
     99  1.5  christos /*ARGSUSED*/
    100  1.5  christos void
    101  1.5  christos phaser(v)
    102  1.7       jsm 	int v __attribute__((__unused__));
    103  1.1       cgd {
    104  1.5  christos 	int		i;
    105  1.5  christos 	int		j;
    106  1.5  christos 	struct kling	*k;
    107  1.5  christos 	double		dx, dy;
    108  1.5  christos 	double		anglefactor, distfactor;
    109  1.5  christos 	struct banks	*b;
    110  1.5  christos 	int		manual, flag, extra = 0;
    111  1.5  christos 	int		hit;
    112  1.5  christos 	double		tot;
    113  1.5  christos 	int		n;
    114  1.5  christos 	int		hitreqd[NBANKS];
    115  1.5  christos 	struct banks	bank[NBANKS];
    116  1.6   hubertf 	const struct cvntab	*ptr;
    117  1.5  christos 
    118  1.5  christos 	if (Ship.cond == DOCKED) {
    119  1.5  christos 		printf("Phasers cannot fire through starbase shields\n");
    120  1.5  christos 		return;
    121  1.5  christos 	}
    122  1.5  christos 	if (damaged(PHASER)) {
    123  1.5  christos 		out(PHASER);
    124  1.5  christos 		return;
    125  1.5  christos 	}
    126  1.5  christos 	if (Ship.shldup) {
    127  1.5  christos 		printf("Sulu: Captain, we cannot fire through shields.\n");
    128  1.5  christos 		return;
    129  1.5  christos 	}
    130  1.1       cgd 	if (Ship.cloaked)
    131  1.1       cgd 	{
    132  1.1       cgd 		printf("Sulu: Captain, surely you must realize that we cannot fire\n");
    133  1.1       cgd 		printf("  phasers with the cloaking device up.\n");
    134  1.1       cgd 		return;
    135  1.1       cgd 	}
    136  1.1       cgd 
    137  1.1       cgd 	/* decide if we want manual or automatic mode */
    138  1.1       cgd 	manual = 0;
    139  1.1       cgd 	if (testnl())
    140  1.1       cgd 	{
    141  1.1       cgd 		if (damaged(COMPUTER))
    142  1.1       cgd 		{
    143  1.8    itojun 			printf("%s", Device[COMPUTER].name);
    144  1.1       cgd 			manual++;
    145  1.1       cgd 		}
    146  1.1       cgd 		else
    147  1.1       cgd 			if (damaged(SRSCAN))
    148  1.1       cgd 			{
    149  1.8    itojun 				printf("%s", Device[SRSCAN].name);
    150  1.1       cgd 				manual++;
    151  1.1       cgd 			}
    152  1.1       cgd 		if (manual)
    153  1.1       cgd 			printf(" damaged, manual mode selected\n");
    154  1.1       cgd 	}
    155  1.1       cgd 
    156  1.1       cgd 	if (!manual)
    157  1.1       cgd 	{
    158  1.1       cgd 		ptr = getcodpar("Manual or automatic", Matab);
    159  1.4       cgd 		manual = (long) ptr->value;
    160  1.1       cgd 	}
    161  1.1       cgd 	if (!manual && damaged(COMPUTER))
    162  1.1       cgd 	{
    163  1.1       cgd 		printf("Computer damaged, manual selected\n");
    164  1.1       cgd 		skiptonl(0);
    165  1.1       cgd 		manual++;
    166  1.1       cgd 	}
    167  1.1       cgd 
    168  1.1       cgd 	/* initialize the bank[] array */
    169  1.1       cgd 	flag = 1;
    170  1.1       cgd 	for (i = 0; i < NBANKS; i++)
    171  1.1       cgd 		bank[i].units = 0;
    172  1.1       cgd 	if (manual)
    173  1.1       cgd 	{
    174  1.1       cgd 		/* collect manual mode statistics */
    175  1.1       cgd 		while (flag)
    176  1.1       cgd 		{
    177  1.1       cgd 			printf("%d units available\n", Ship.energy);
    178  1.1       cgd 			extra = 0;
    179  1.1       cgd 			flag = 0;
    180  1.1       cgd 			for (i = 0; i < NBANKS; i++)
    181  1.1       cgd 			{
    182  1.1       cgd 				b = &bank[i];
    183  1.1       cgd 				printf("\nBank %d:\n", i);
    184  1.1       cgd 				hit = getintpar("units");
    185  1.1       cgd 				if (hit < 0)
    186  1.1       cgd 					return;
    187  1.1       cgd 				if (hit == 0)
    188  1.1       cgd 					break;
    189  1.1       cgd 				extra += hit;
    190  1.1       cgd 				if (extra > Ship.energy)
    191  1.1       cgd 				{
    192  1.1       cgd 					printf("available energy exceeded.  ");
    193  1.1       cgd 					skiptonl(0);
    194  1.1       cgd 					flag++;
    195  1.1       cgd 					break;
    196  1.1       cgd 				}
    197  1.1       cgd 				b->units = hit;
    198  1.1       cgd 				hit = getintpar("course");
    199  1.1       cgd 				if (hit < 0 || hit > 360)
    200  1.1       cgd 					return;
    201  1.1       cgd 				b->angle = hit * 0.0174532925;
    202  1.1       cgd 				b->spread = getfltpar("spread");
    203  1.1       cgd 				if (b->spread < 0 || b->spread > 1)
    204  1.1       cgd 					return;
    205  1.1       cgd 			}
    206  1.1       cgd 			Ship.energy -= extra;
    207  1.1       cgd 		}
    208  1.1       cgd 		extra = 0;
    209  1.1       cgd 	}
    210  1.1       cgd 	else
    211  1.1       cgd 	{
    212  1.1       cgd 		/* automatic distribution of power */
    213  1.5  christos 		if (Etc.nkling <= 0) {
    214  1.5  christos 			printf("Sulu: But there are no Klingons in this quadrant\n");
    215  1.5  christos 			return;
    216  1.5  christos 		}
    217  1.1       cgd 		printf("Phasers locked on target.  ");
    218  1.1       cgd 		while (flag)
    219  1.1       cgd 		{
    220  1.1       cgd 			printf("%d units available\n", Ship.energy);
    221  1.1       cgd 			hit = getintpar("Units to fire");
    222  1.1       cgd 			if (hit <= 0)
    223  1.1       cgd 				return;
    224  1.1       cgd 			if (hit > Ship.energy)
    225  1.1       cgd 			{
    226  1.1       cgd 				printf("available energy exceeded.  ");
    227  1.1       cgd 				skiptonl(0);
    228  1.1       cgd 				continue;
    229  1.1       cgd 			}
    230  1.1       cgd 			flag = 0;
    231  1.1       cgd 			Ship.energy -= hit;
    232  1.1       cgd 			extra = hit;
    233  1.1       cgd 			n = Etc.nkling;
    234  1.1       cgd 			if (n > NBANKS)
    235  1.1       cgd 				n = NBANKS;
    236  1.1       cgd 			tot = n * (n + 1) / 2;
    237  1.1       cgd 			for (i = 0; i < n; i++)
    238  1.1       cgd 			{
    239  1.1       cgd 				k = &Etc.klingon[i];
    240  1.1       cgd 				b = &bank[i];
    241  1.1       cgd 				distfactor = k->dist;
    242  1.1       cgd 				anglefactor = ALPHA * BETA * OMEGA / (distfactor * distfactor + EPSILON);
    243  1.1       cgd 				anglefactor *= GAMMA;
    244  1.1       cgd 				distfactor = k->power;
    245  1.1       cgd 				distfactor /= anglefactor;
    246  1.1       cgd 				hitreqd[i] = distfactor + 0.5;
    247  1.1       cgd 				dx = Ship.sectx - k->x;
    248  1.1       cgd 				dy = k->y - Ship.secty;
    249  1.1       cgd 				b->angle = atan2(dy, dx);
    250  1.1       cgd 				b->spread = 0.0;
    251  1.1       cgd 				b->units = ((n - i) / tot) * extra;
    252  1.1       cgd #				ifdef xTRACE
    253  1.1       cgd 				if (Trace)
    254  1.1       cgd 				{
    255  1.1       cgd 					printf("b%d hr%d u%d df%.2f af%.2f\n",
    256  1.1       cgd 						i, hitreqd[i], b->units,
    257  1.1       cgd 						distfactor, anglefactor);
    258  1.1       cgd 				}
    259  1.1       cgd #				endif
    260  1.1       cgd 				extra -= b->units;
    261  1.1       cgd 				hit = b->units - hitreqd[i];
    262  1.1       cgd 				if (hit > 0)
    263  1.1       cgd 				{
    264  1.1       cgd 					extra += hit;
    265  1.1       cgd 					b->units -= hit;
    266  1.1       cgd 				}
    267  1.1       cgd 			}
    268  1.1       cgd 
    269  1.1       cgd 			/* give out any extra energy we might have around */
    270  1.1       cgd 			if (extra > 0)
    271  1.1       cgd 			{
    272  1.1       cgd 				for (i = 0; i < n; i++)
    273  1.1       cgd 				{
    274  1.1       cgd 					b = &bank[i];
    275  1.1       cgd 					hit = hitreqd[i] - b->units;
    276  1.1       cgd 					if (hit <= 0)
    277  1.1       cgd 						continue;
    278  1.1       cgd 					if (hit >= extra)
    279  1.1       cgd 					{
    280  1.1       cgd 						b->units += extra;
    281  1.1       cgd 						extra = 0;
    282  1.1       cgd 						break;
    283  1.1       cgd 					}
    284  1.1       cgd 					b->units = hitreqd[i];
    285  1.1       cgd 					extra -= hit;
    286  1.1       cgd 				}
    287  1.1       cgd 				if (extra > 0)
    288  1.1       cgd 					printf("%d units overkill\n", extra);
    289  1.1       cgd 			}
    290  1.1       cgd 		}
    291  1.1       cgd 	}
    292  1.1       cgd 
    293  1.1       cgd #	ifdef xTRACE
    294  1.1       cgd 	if (Trace)
    295  1.1       cgd 	{
    296  1.1       cgd 		for (i = 0; i < NBANKS; i++)
    297  1.1       cgd 		{
    298  1.1       cgd 			b = &bank[i];
    299  1.1       cgd 			printf("b%d u%d", i, b->units);
    300  1.1       cgd 			if (b->units > 0)
    301  1.1       cgd 				printf(" a%.2f s%.2f\n", b->angle, b->spread);
    302  1.1       cgd 			else
    303  1.1       cgd 				printf("\n");
    304  1.1       cgd 		}
    305  1.1       cgd 	}
    306  1.1       cgd #	endif
    307  1.1       cgd 
    308  1.1       cgd 	/* actually fire the shots */
    309  1.1       cgd 	Move.free = 0;
    310  1.1       cgd 	for (i = 0; i < NBANKS; i++)
    311  1.1       cgd 	{
    312  1.1       cgd 		b = &bank[i];
    313  1.1       cgd 		if (b->units <= 0)
    314  1.1       cgd 		{
    315  1.1       cgd 			continue;
    316  1.1       cgd 		}
    317  1.1       cgd 		printf("\nPhaser bank %d fires:\n", i);
    318  1.1       cgd 		n = Etc.nkling;
    319  1.1       cgd 		k = Etc.klingon;
    320  1.1       cgd 		for (j = 0; j < n; j++)
    321  1.1       cgd 		{
    322  1.1       cgd 			if (b->units <= 0)
    323  1.1       cgd 				break;
    324  1.1       cgd 			/*
    325  1.1       cgd 			** The formula for hit is as follows:
    326  1.1       cgd 			**
    327  1.1       cgd 			**  zap = OMEGA * [(sigma + ALPHA) * (rho + BETA)]
    328  1.1       cgd 			**	/ (dist ** 2 + EPSILON)]
    329  1.1       cgd 			**	* [cos(delta * sigma) + GAMMA]
    330  1.1       cgd 			**	* hit
    331  1.1       cgd 			**
    332  1.1       cgd 			** where sigma is the spread factor,
    333  1.1       cgd 			** rho is a random number (0 -> 1),
    334  1.1       cgd 			** GAMMA is a crud factor for angle (essentially
    335  1.1       cgd 			**	cruds up the spread factor),
    336  1.1       cgd 			** delta is the difference in radians between the
    337  1.1       cgd 			**	angle you are shooting at and the actual
    338  1.1       cgd 			**	angle of the klingon,
    339  1.1       cgd 			** ALPHA scales down the significance of sigma,
    340  1.1       cgd 			** BETA scales down the significance of rho,
    341  1.1       cgd 			** OMEGA is the magic number which makes everything
    342  1.1       cgd 			**	up to "* hit" between zero and one,
    343  1.1       cgd 			** dist is the distance to the klingon
    344  1.1       cgd 			** hit is the number of units in the bank, and
    345  1.1       cgd 			** zap is the amount of the actual hit.
    346  1.1       cgd 			**
    347  1.1       cgd 			** Everything up through dist squared should maximize
    348  1.1       cgd 			** at 1.0, so that the distance factor is never
    349  1.1       cgd 			** greater than one.  Conveniently, cos() is
    350  1.1       cgd 			** never greater than one, but the same restric-
    351  1.1       cgd 			** tion applies.
    352  1.1       cgd 			*/
    353  1.1       cgd 			distfactor = BETA + franf();
    354  1.1       cgd 			distfactor *= ALPHA + b->spread;
    355  1.1       cgd 			distfactor *= OMEGA;
    356  1.1       cgd 			anglefactor = k->dist;
    357  1.1       cgd 			distfactor /= anglefactor * anglefactor + EPSILON;
    358  1.1       cgd 			distfactor *= b->units;
    359  1.1       cgd 			dx = Ship.sectx - k->x;
    360  1.1       cgd 			dy = k->y - Ship.secty;
    361  1.1       cgd 			anglefactor = atan2(dy, dx) - b->angle;
    362  1.1       cgd 			anglefactor = cos((anglefactor * b->spread) + GAMMA);
    363  1.1       cgd 			if (anglefactor < 0.0)
    364  1.1       cgd 			{
    365  1.1       cgd 				k++;
    366  1.1       cgd 				continue;
    367  1.1       cgd 			}
    368  1.1       cgd 			hit = anglefactor * distfactor + 0.5;
    369  1.1       cgd 			k->power -= hit;
    370  1.1       cgd 			printf("%d unit hit on Klingon", hit);
    371  1.1       cgd 			if (!damaged(SRSCAN))
    372  1.1       cgd 				printf(" at %d,%d", k->x, k->y);
    373  1.1       cgd 			printf("\n");
    374  1.1       cgd 			b->units -= hit;
    375  1.1       cgd 			if (k->power <= 0)
    376  1.1       cgd 			{
    377  1.1       cgd 				killk(k->x, k->y);
    378  1.1       cgd 				continue;
    379  1.1       cgd 			}
    380  1.1       cgd 			k++;
    381  1.1       cgd 		}
    382  1.1       cgd 	}
    383  1.1       cgd 
    384  1.1       cgd 	/* compute overkill */
    385  1.1       cgd 	for (i = 0; i < NBANKS; i++)
    386  1.1       cgd 		extra += bank[i].units;
    387  1.1       cgd 	if (extra > 0)
    388  1.1       cgd 		printf("\n%d units expended on empty space\n", extra);
    389  1.1       cgd }
    390