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