Home | History | Annotate | Line # | Download | only in trek
attack.c revision 1.2
      1  1.1      cgd /*
      2  1.1      cgd  * Copyright (c) 1980 Regents of the University of California.
      3  1.1      cgd  * All rights reserved.
      4  1.1      cgd  *
      5  1.1      cgd  * Redistribution and use in source and binary forms, with or without
      6  1.1      cgd  * modification, are permitted provided that the following conditions
      7  1.1      cgd  * are met:
      8  1.1      cgd  * 1. Redistributions of source code must retain the above copyright
      9  1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     10  1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     12  1.1      cgd  *    documentation and/or other materials provided with the distribution.
     13  1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     14  1.1      cgd  *    must display the following acknowledgement:
     15  1.1      cgd  *	This product includes software developed by the University of
     16  1.1      cgd  *	California, Berkeley and its contributors.
     17  1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     18  1.1      cgd  *    may be used to endorse or promote products derived from this software
     19  1.1      cgd  *    without specific prior written permission.
     20  1.1      cgd  *
     21  1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1      cgd  * SUCH DAMAGE.
     32  1.1      cgd  */
     33  1.1      cgd 
     34  1.1      cgd #ifndef lint
     35  1.2  mycroft /*static char sccsid[] = "from: @(#)attack.c	5.4 (Berkeley) 6/1/90";*/
     36  1.2  mycroft static char rcsid[] = "$Id: attack.c,v 1.2 1993/08/01 18:50:53 mycroft Exp $";
     37  1.1      cgd #endif /* not lint */
     38  1.1      cgd 
     39  1.1      cgd # include	"trek.h"
     40  1.1      cgd 
     41  1.1      cgd /*
     42  1.1      cgd **  Klingon Attack Routine
     43  1.1      cgd **
     44  1.1      cgd **	This routine performs the Klingon attack provided that
     45  1.1      cgd **	(1) Something happened this move (i.e., not free), and
     46  1.1      cgd **	(2) You are not cloaked.  Note that if you issue the
     47  1.1      cgd **	cloak command, you are not considered cloaked until you
     48  1.1      cgd **	expend some time.
     49  1.1      cgd **
     50  1.1      cgd **	Klingons are permitted to move both before and after the
     51  1.1      cgd **	attack.  They will tend to move toward you before the
     52  1.1      cgd **	attack and away from you after the attack.
     53  1.1      cgd **
     54  1.1      cgd **	Under certain conditions you can get a critical hit.  This
     55  1.1      cgd **	sort of hit damages devices.  The probability that a given
     56  1.1      cgd **	device is damaged depends on the device.  Well protected
     57  1.1      cgd **	devices (such as the computer, which is in the core of the
     58  1.1      cgd **	ship and has considerable redundancy) almost never get
     59  1.1      cgd **	damaged, whereas devices which are exposed (such as the
     60  1.1      cgd **	warp engines) or which are particularly delicate (such as
     61  1.1      cgd **	the transporter) have a much higher probability of being
     62  1.1      cgd **	damaged.
     63  1.1      cgd **
     64  1.1      cgd **	The actual amount of damage (i.e., how long it takes to fix
     65  1.1      cgd **	it) depends on the amount of the hit and the "damfac[]"
     66  1.1      cgd **	entry for the particular device.
     67  1.1      cgd **
     68  1.1      cgd **	Casualties can also occur.
     69  1.1      cgd */
     70  1.1      cgd 
     71  1.1      cgd attack(resting)
     72  1.1      cgd int	resting;	/* set if attack while resting */
     73  1.1      cgd {
     74  1.1      cgd 	register int		hit, i, l;
     75  1.1      cgd 	int			maxhit, tothit, shldabsb;
     76  1.1      cgd 	double			chgfac, propor, extradm;
     77  1.1      cgd 	double			dustfac, tothe;
     78  1.1      cgd 	int			cas;
     79  1.1      cgd 	int			hitflag;
     80  1.1      cgd 
     81  1.1      cgd 	if (Move.free)
     82  1.1      cgd 		return;
     83  1.1      cgd 	if (Etc.nkling <= 0 || Quad[Ship.quadx][Ship.quady].stars < 0)
     84  1.1      cgd 		return;
     85  1.1      cgd 	if (Ship.cloaked && Ship.cloakgood)
     86  1.1      cgd 		return;
     87  1.1      cgd 	/* move before attack */
     88  1.1      cgd 	klmove(0);
     89  1.1      cgd 	if (Ship.cond == DOCKED)
     90  1.1      cgd 	{
     91  1.1      cgd 		if (!resting)
     92  1.1      cgd 			printf("Starbase shields protect the %s\n", Ship.shipname);
     93  1.1      cgd 		return;
     94  1.1      cgd 	}
     95  1.1      cgd 	/* setup shield effectiveness */
     96  1.1      cgd 	chgfac = 1.0;
     97  1.1      cgd 	if (Move.shldchg)
     98  1.1      cgd 		chgfac = 0.25 + 0.50 * franf();
     99  1.1      cgd 	maxhit = tothit = 0;
    100  1.1      cgd 	hitflag = 0;
    101  1.1      cgd 
    102  1.1      cgd 	/* let each Klingon do his damndest */
    103  1.1      cgd 	for (i = 0; i < Etc.nkling; i++)
    104  1.1      cgd 	{
    105  1.1      cgd 		/* if he's low on power he won't attack */
    106  1.1      cgd 		if (Etc.klingon[i].power < 20)
    107  1.1      cgd 			continue;
    108  1.1      cgd 		if (!hitflag)
    109  1.1      cgd 		{
    110  1.1      cgd 			printf("\nStardate %.2f: Klingon attack:\n",
    111  1.1      cgd 				Now.date);
    112  1.1      cgd 			hitflag++;
    113  1.1      cgd 		}
    114  1.1      cgd 		/* complete the hit */
    115  1.1      cgd 		dustfac = 0.90 + 0.01 * franf();
    116  1.1      cgd 		tothe = Etc.klingon[i].avgdist;
    117  1.1      cgd 		hit = Etc.klingon[i].power * pow(dustfac, tothe) * Param.hitfac;
    118  1.1      cgd 		/* deplete his energy */
    119  1.1      cgd 		dustfac = Etc.klingon[i].power;
    120  1.1      cgd 		Etc.klingon[i].power = dustfac * Param.phasfac * (1.0 + (franf() - 0.5) * 0.2);
    121  1.1      cgd 		/* see how much of hit shields will absorb */
    122  1.1      cgd 		shldabsb = 0;
    123  1.1      cgd 		if (Ship.shldup || Move.shldchg)
    124  1.1      cgd 		{
    125  1.1      cgd 			propor = Ship.shield;
    126  1.1      cgd 			propor /= Param.shield;
    127  1.1      cgd 			shldabsb = propor * chgfac * hit;
    128  1.1      cgd 			if (shldabsb > Ship.shield)
    129  1.1      cgd 				shldabsb = Ship.shield;
    130  1.1      cgd 			Ship.shield -= shldabsb;
    131  1.1      cgd 		}
    132  1.1      cgd 		/* actually do the hit */
    133  1.1      cgd 		printf("HIT: %d units", hit);
    134  1.1      cgd 		if (!damaged(SRSCAN))
    135  1.1      cgd 			printf(" from %d,%d", Etc.klingon[i].x, Etc.klingon[i].y);
    136  1.1      cgd 		cas = (shldabsb * 100) / hit;
    137  1.1      cgd 		hit -= shldabsb;
    138  1.1      cgd 		if (shldabsb > 0)
    139  1.1      cgd 			printf(", shields absorb %d%%, effective hit %d\n",
    140  1.1      cgd 				cas, hit);
    141  1.1      cgd 		else
    142  1.1      cgd 			printf("\n");
    143  1.1      cgd 		tothit += hit;
    144  1.1      cgd 		if (hit > maxhit)
    145  1.1      cgd 			maxhit = hit;
    146  1.1      cgd 		Ship.energy -= hit;
    147  1.1      cgd 		/* see if damages occurred */
    148  1.1      cgd 		if (hit >= (15 - Game.skill) * (25 - ranf(12)))
    149  1.1      cgd 		{
    150  1.1      cgd 			printf("CRITICAL HIT!!!\n");
    151  1.1      cgd 			/* select a device from probability vector */
    152  1.1      cgd 			cas = ranf(1000);
    153  1.1      cgd 			for (l = 0; cas >= 0; l++)
    154  1.1      cgd 				cas -= Param.damprob[l];
    155  1.1      cgd 			l -= 1;
    156  1.1      cgd 			/* compute amount of damage */
    157  1.1      cgd 			extradm = (hit * Param.damfac[l]) / (75 + ranf(25)) + 0.5;
    158  1.1      cgd 			/* damage the device */
    159  1.1      cgd 			damage(l, extradm);
    160  1.1      cgd 			if (damaged(SHIELD))
    161  1.1      cgd 			{
    162  1.1      cgd 				if (Ship.shldup)
    163  1.1      cgd 					printf("Sulu: Shields knocked down, captain.\n");
    164  1.1      cgd 				Ship.shldup = 0;
    165  1.1      cgd 				Move.shldchg = 0;
    166  1.1      cgd 			}
    167  1.1      cgd 		}
    168  1.1      cgd 		if (Ship.energy <= 0)
    169  1.1      cgd 			lose(L_DSTRYD);
    170  1.1      cgd 	}
    171  1.1      cgd 
    172  1.1      cgd 	/* see what our casualities are like */
    173  1.1      cgd 	if (maxhit >= 200 || tothit >= 500)
    174  1.1      cgd 	{
    175  1.1      cgd 		cas = tothit * 0.015 * franf();
    176  1.1      cgd 		if (cas >= 2)
    177  1.1      cgd 		{
    178  1.1      cgd 			printf("McCoy: we suffered %d casualties in that attack.\n",
    179  1.1      cgd 				cas);
    180  1.1      cgd 			Game.deaths += cas;
    181  1.1      cgd 			Ship.crew -= cas;
    182  1.1      cgd 		}
    183  1.1      cgd 	}
    184  1.1      cgd 
    185  1.1      cgd 	/* allow Klingons to move after attacking */
    186  1.1      cgd 	klmove(1);
    187  1.1      cgd 
    188  1.1      cgd 	return;
    189  1.1      cgd }
    190