get rid of sibuf; requires change to user interface because of
[unix-history] / usr / src / games / trek / attack.c
CommitLineData
b6f0a7e4
DF
1/*
2 * Copyright (c) 1980 Regents of the University of California.
e9fb6bea
KB
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
6 * provided that this notice is preserved and that due credit is given
7 * to the University of California at Berkeley. The name of the University
8 * may not be used to endorse or promote products derived from this
9 * software without specific prior written permission. This software
10 * is provided ``as is'' without express or implied warranty.
b6f0a7e4
DF
11 */
12
d98c7187 13#ifndef lint
e9fb6bea
KB
14static char sccsid[] = "@(#)attack.c 5.2 (Berkeley) %G%";
15#endif /* not lint */
d98c7187
KM
16
17# include "trek.h"
18
19/*
20** Klingon Attack Routine
21**
22** This routine performs the Klingon attack provided that
23** (1) Something happened this move (i.e., not free), and
24** (2) You are not cloaked. Note that if you issue the
25** cloak command, you are not considered cloaked until you
26** expend some time.
27**
28** Klingons are permitted to move both before and after the
29** attack. They will tend to move toward you before the
30** attack and away from you after the attack.
31**
32** Under certain conditions you can get a critical hit. This
33** sort of hit damages devices. The probability that a given
34** device is damaged depends on the device. Well protected
35** devices (such as the computer, which is in the core of the
36** ship and has considerable redundancy) almost never get
37** damaged, whereas devices which are exposed (such as the
38** warp engines) or which are particularly delicate (such as
39** the transporter) have a much higher probability of being
40** damaged.
41**
42** The actual amount of damage (i.e., how long it takes to fix
43** it) depends on the amount of the hit and the "damfac[]"
44** entry for the particular device.
45**
46** Casualties can also occur.
47*/
48
49attack(resting)
50int resting; /* set if attack while resting */
51{
52 register int hit, i, l;
53 int maxhit, tothit, shldabsb;
54 double chgfac, propor, extradm;
55 double dustfac, tothe;
56 int cas;
57 int hitflag;
58
59 if (Move.free)
60 return;
61 if (Etc.nkling <= 0 || Quad[Ship.quadx][Ship.quady].stars < 0)
62 return;
63 if (Ship.cloaked && Ship.cloakgood)
64 return;
65 /* move before attack */
66 klmove(0);
67 if (Ship.cond == DOCKED)
68 {
69 if (!resting)
70 printf("Starbase shields protect the %s\n", Ship.shipname);
71 return;
72 }
73 /* setup shield effectiveness */
74 chgfac = 1.0;
75 if (Move.shldchg)
76 chgfac = 0.25 + 0.50 * franf();
77 maxhit = tothit = 0;
78 hitflag = 0;
79
80 /* let each Klingon do his damndest */
81 for (i = 0; i < Etc.nkling; i++)
82 {
83 /* if he's low on power he won't attack */
84 if (Etc.klingon[i].power < 20)
85 continue;
86 if (!hitflag)
87 {
88 printf("\nStardate %.2f: Klingon attack:\n",
89 Now.date);
90 hitflag++;
91 }
92 /* complete the hit */
93 dustfac = 0.90 + 0.01 * franf();
94 tothe = Etc.klingon[i].avgdist;
95 hit = Etc.klingon[i].power * pow(dustfac, tothe) * Param.hitfac;
96 /* deplete his energy */
97 dustfac = Etc.klingon[i].power;
98 Etc.klingon[i].power = dustfac * Param.phasfac * (1.0 + (franf() - 0.5) * 0.2);
99 /* see how much of hit shields will absorb */
100 shldabsb = 0;
101 if (Ship.shldup || Move.shldchg)
102 {
103 propor = Ship.shield;
35b95499 104 propor /= Param.shield;
d98c7187
KM
105 shldabsb = propor * chgfac * hit;
106 if (shldabsb > Ship.shield)
107 shldabsb = Ship.shield;
35b95499 108 Ship.shield -= shldabsb;
d98c7187
KM
109 }
110 /* actually do the hit */
111 printf("\aHIT: %d units", hit);
112 if (!damaged(SRSCAN))
113 printf(" from %d,%d", Etc.klingon[i].x, Etc.klingon[i].y);
114 cas = (shldabsb * 100) / hit;
35b95499 115 hit -= shldabsb;
d98c7187
KM
116 if (shldabsb > 0)
117 printf(", shields absorb %d%%, effective hit %d\n",
118 cas, hit);
119 else
120 printf("\n");
35b95499 121 tothit += hit;
d98c7187
KM
122 if (hit > maxhit)
123 maxhit = hit;
35b95499 124 Ship.energy -= hit;
d98c7187
KM
125 /* see if damages occurred */
126 if (hit >= (15 - Game.skill) * (25 - ranf(12)))
127 {
128 printf("\aCRITICAL HIT!!!\a\n");
129 /* select a device from probability vector */
130 cas = ranf(1000);
131 for (l = 0; cas >= 0; l++)
35b95499
KL
132 cas -= Param.damprob[l];
133 l -= 1;
d98c7187
KM
134 /* compute amount of damage */
135 extradm = (hit * Param.damfac[l]) / (75 + ranf(25)) + 0.5;
136 /* damage the device */
137 damage(l, extradm);
138 if (damaged(SHIELD))
139 {
140 if (Ship.shldup)
141 printf("Sulu: Shields knocked down, captain.\n");
142 Ship.shldup = 0;
143 Move.shldchg = 0;
144 }
145 }
146 if (Ship.energy <= 0)
147 lose(L_DSTRYD);
148 }
149
150 /* see what our casualities are like */
151 if (maxhit >= 200 || tothit >= 500)
152 {
153 cas = tothit * 0.015 * franf();
154 if (cas >= 2)
155 {
156 printf("McCoy: we suffered %d casualties in that attack.\n",
157 cas);
35b95499
KL
158 Game.deaths += cas;
159 Ship.crew -= cas;
d98c7187
KM
160 }
161 }
162
163 /* allow Klingons to move after attacking */
164 klmove(1);
165
166 return;
167}