BSD-Trek-like critical hits with weighting.
[super-star-trek.git] / src / battle.c
1 #include "sst.h"
2
3 void doshield(bool raise) 
4 /* change shield status */
5 {
6     int key;
7     enum {NONE, SHUP, SHDN, NRG} action = NONE;
8
9     game.ididit = false;
10
11     if (raise) 
12         action = SHUP;
13     else {
14         key = scan();
15         if (key == IHALPHA) {
16             if (isit("transfer"))
17                 action = NRG;
18             else {
19                 chew();
20                 if (damaged(DSHIELD)) {
21                     prout(_("Shields damaged and down."));
22                     return;
23                 }
24                 if (isit("up"))
25                     action = SHUP;
26                 else if (isit("down"))
27                     action = SHDN;
28             }
29         }
30         if (action==NONE) {
31             proutn(_("Do you wish to change shield energy? "));
32             if (ja() == true) {
33                 proutn(_("Energy to transfer to shields- "));
34                 action = NRG;
35             }
36             else if (damaged(DSHIELD)) {
37                 prout(_("Shields damaged and down."));
38                 return;
39             }
40             else if (game.shldup) {
41                 proutn(_("Shields are up. Do you want them down? "));
42                 if (ja() == true) action = SHDN;
43                 else {
44                     chew();
45                     return;
46                 }
47             }
48             else {
49                 proutn(_("Shields are down. Do you want them up? "));
50                 if (ja() == true) action = SHUP;
51                 else {
52                     chew();
53                     return;
54                 }
55             }
56         }
57     }
58     switch (action) {
59     case SHUP: /* raise shields */
60         if (game.shldup) {
61             prout(_("Shields already up."));
62             return;
63         }
64         game.shldup = true;
65         game.shldchg = 1;
66         if (game.condition != docked) game.energy -= 50.0;
67         prout(_("Shields raised."));
68         if (game.energy <= 0) {
69             skip(1);
70             prout(_("Shields raising uses up last of energy."));
71             finish(FNRG);
72             return;
73         }
74         game.ididit=true;
75         return;
76     case SHDN:
77         if (!game.shldup) {
78             prout(_("Shields already down."));
79             return;
80         }
81         game.shldup=false;
82         game.shldchg=1;
83         prout(_("Shields lowered."));
84         game.ididit = true;
85         return;
86     case NRG:
87         while (scan() != IHREAL) {
88             chew();
89             proutn(_("Energy to transfer to shields- "));
90         }
91         chew();
92         if (aaitem==0) return;
93         if (aaitem > game.energy) {
94             prout(_("Insufficient ship energy."));
95             return;
96         }
97         game.ididit = true;
98         if (game.shield+aaitem >= game.inshld) {
99             prout(_("Shield energy maximized."));
100             if (game.shield+aaitem > game.inshld) {
101                 prout(_("Excess energy requested returned to ship energy"));
102             }
103             game.energy -= game.inshld-game.shield;
104             game.shield = game.inshld;
105             return;
106         }
107         if (aaitem < 0.0 && game.energy-aaitem > game.inenrg) {
108             /* Prevent shield drain loophole */
109             skip(1);
110             prout(_("Engineering to bridge--"));
111             prout(_("  Scott here. Power circuit problem, Captain."));
112             prout(_("  I can't drain the shields."));
113             game.ididit = false;
114             return;
115         }
116         if (game.shield+aaitem < 0) {
117             prout(_("All shield energy transferred to ship."));
118             game.energy += game.shield;
119             game.shield = 0.0;
120             return;
121         }
122         proutn(_("Scotty- \""));
123         if (aaitem > 0)
124             prout(_("Transferring energy to shields.\""));
125         else
126             prout(_("Draining energy from shields.\""));
127         game.shield += aaitem;
128         game.energy -= aaitem;
129         return;
130     case NONE:; /* avoid gcc warning */
131     }
132 }
133
134 static int randdevice(void)
135 /* choose a device to damage, at random. */
136 {
137     /*
138      * Quoth Eric Allman in the code of BSD-Trek:
139      * "Under certain conditions you can get a critical hit.  This
140      * sort of hit damages devices.  The probability that a given
141      * device is damaged depends on the device.  Well protected
142      * devices (such as the computer, which is in the core of the
143      * ship and has considerable redundancy) almost never get
144      * damaged, whereas devices which are exposed (such as the
145      * warp engines) or which are particularly delicate (such as
146      * the transporter) have a much higher probability of being
147      * damaged."
148      *
149      * This is one place where OPTION_PLAIN does not restore the
150      * original behavior, which was equiprobable damage across
151      * all devices.  If we wanted that, we'd return NDEVICES*Rand()
152      * and have done with it.  Also, in the original game, DNAVYS
153      * and DCOMPTR were the same device. 
154      *
155      * Instead, we use a table of weights similar to the one from BSD Trek.
156      * BSD doesn't have the shuttle, shield controller, death ray, or probes. 
157      * We don't have a cloaking device.  The shuttle got the allocation
158      * for the cloaking device, then we shaved a half-percent off
159      * everything to have some weight to give DSHCTRL/DDRAY/DDSP.
160      */
161     static int weights[NDEVICES] = {
162         105,    /* DSRSENS: short range scanners        10.5% */
163         105,    /* DLRSENS: long range scanners         10.5% */
164         120,    /* DPHASER: phasers                     12.0% */
165         120,    /* DPHOTON: photon torpedoes            12.0% */
166         25,     /* DLIFSUP: life support                 2.5% */
167         65,     /* DWARPEN: warp drive                   6.5% */
168         70,     /* DIMPULS: impulse engines              6.5% */
169         145,    /* DSHIELD: deflector shields           14.5% */
170         30,     /* DRADIO:  subspace radio               3.0% */
171         45,     /* DSHUTTL: shuttle                      4.5% */
172         15,     /* DCOMPTR: computer                     1.5% */
173         20,     /* NAVCOMP: navigation system            2.0% */
174         75,     /* DTRANSP: transporter                  7.5% */
175         20,     /* DSHCTRL: high-speed shield controller 2.0% */
176         10,     /* DDRAY: death ray                      1.0% */
177         30,     /* DDSP: deep-space probes               3.0% */
178     };
179     int sum, i, idx = Rand() * 1000.0;  /* weights must sum to 1000 */
180
181     for (i = sum = 0; i < NDEVICES; i++) {
182         sum += weights[i];
183         if (idx < sum)
184             return i;
185     }
186     return -1;  /* we should never get here, but this quiets GCC */
187 }
188
189 void ram(bool ibumpd, feature ienm, coord w)
190 /* make our ship ram something */
191 {
192     double hardness, extradm;
193     int icas, m, ncrits;
194         
195     prouts(_("***RED ALERT!  RED ALERT!"));
196     skip(1);
197     prout(_("***COLLISION IMMINENT."));
198     skip(2);
199     proutn("***");
200     crmshp();
201     switch (ienm) {
202     case IHR: hardness = 1.5; break;
203     case IHC: hardness = 2.0; break;
204     case IHS: hardness = 2.5; break;
205     case IHT: hardness = 0.5; break;
206     case IHQUEST: hardness = 4.0; break;
207     default: hardness = 1.0; break;
208     }
209     proutn(ibumpd ? _(" rammed by ") : _(" rams "));
210     crmena(false, ienm, sector, w);
211     if (ibumpd) proutn(_(" (original position)"));
212     skip(1);
213     deadkl(w, ienm, game.sector);
214     proutn("***");
215     crmshp();
216     prout(_(" heavily damaged."));
217     icas = 10.0+20.0*Rand();
218     prout(_("***Sickbay reports %d casualties"), icas);
219     game.casual += icas;
220     game.state.crew -= icas;
221     /*
222      * In the pre-SST2K version, all devices got equiprobably damaged,
223      * which was silly.  Instead, pick up to half the devices at
224      * random according to our weighting table,
225      */
226     ncrits = Rand() * (NDEVICES/2);
227     for (m=0; m < ncrits; m++) {
228         int dev = randdevice();
229         if (game.damage[dev] < 0) 
230             continue;
231         extradm = (10.0*hardness*Rand()+1.0)*game.damfac;
232         /* Damage for at least time of travel! */
233         game.damage[dev] += game.optime + extradm;
234     }
235     game.shldup = false;
236     prout(_("***Shields are down."));
237     if (KLINGREM) {
238         pause_game(true);
239         dreprt();
240     }
241     else finish(FWON);
242     return;
243 }
244
245 void torpedo(double course, double r, coord in, double *hit, int i, int n)
246 /* let a photon torpedo fly */
247 {
248     int l, iquad=0, ll;
249     bool shoved = false;
250     double ac=course + 0.25*r;
251     double angle = (15.0-ac)*0.5235988;
252     double bullseye = (15.0 - course)*0.5235988;
253     double deltax=-sin(angle), deltay=cos(angle), x=in.x, y=in.y, bigger;
254     double ang, temp, xx, yy, kp, h1;
255     struct quadrant *q = &game.state.galaxy[game.quadrant.x][game.quadrant.y];
256     coord w, jw;
257
258     w.x = w.y = jw.x = jw.y = 0;
259     bigger = fabs(deltax);
260     if (fabs(deltay) > bigger) bigger = fabs(deltay);
261     deltax /= bigger;
262     deltay /= bigger;
263     if (!damaged(DSRSENS) || game.condition==docked) 
264         setwnd(srscan_window);
265     else 
266         setwnd(message_window);
267     /* Loop to move a single torpedo */
268     for (l=1; l <= 15; l++) {
269         x += deltax;
270         w.x = x + 0.5;
271         y += deltay;
272         w.y = y + 0.5;
273         if (!VALID_SECTOR(w.x, w.y)) break;
274         iquad=game.quad[w.x][w.y];
275         tracktorpedo(w, l, i, n, iquad);
276         if (iquad==IHDOT) continue;
277         /* hit something */
278         setwnd(message_window);
279         skip(1);        /* start new line after text track */
280         switch(iquad) {
281         case IHE: /* Hit our ship */
282         case IHF:
283             skip(1);
284             proutn(_("Torpedo hits "));
285             crmshp();
286             prout(".");
287             *hit = 700.0 + 100.0*Rand() -
288                 1000.0 * distance(w, in) * fabs(sin(bullseye-angle));
289             *hit = fabs(*hit);
290             newcnd(); /* we're blown out of dock */
291             /* We may be displaced. */
292             if (game.landed==1 || game.condition==docked) return; /* Cheat if on a planet */
293             ang = angle + 2.5*(Rand()-0.5);
294             temp = fabs(sin(ang));
295             if (fabs(cos(ang)) > temp) temp = fabs(cos(ang));
296             xx = -sin(ang)/temp;
297             yy = cos(ang)/temp;
298             jw.x=w.x+xx+0.5;
299             jw.y=w.y+yy+0.5;
300             if (!VALID_SECTOR(jw.x, jw.y)) return;
301             if (game.quad[jw.x][jw.y]==IHBLANK) {
302                 finish(FHOLE);
303                 return;
304             }
305             if (game.quad[jw.x][jw.y]!=IHDOT) {
306                 /* can't move into object */
307                 return;
308             }
309             game.sector = jw;
310             crmshp();
311             shoved = true;
312             break;
313                                           
314         case IHC: /* Hit a commander */
315         case IHS:
316             if (Rand() <= 0.05) {
317                 crmena(true, iquad, sector, w);
318                 prout(_(" uses anti-photon device;"));
319                 prout(_("   torpedo neutralized."));
320                 return;
321             }
322         case IHR: /* Hit a regular enemy */
323         case IHK:
324             /* find the enemy */
325             for_local_enemies(ll)
326                 if (same(w, game.ks[ll]))
327                     break;
328             kp = fabs(game.kpower[ll]);
329             h1 = 700.0 + 100.0*Rand() -
330                 1000.0 * distance(w, in) * fabs(sin(bullseye-angle));
331             h1 = fabs(h1);
332             if (kp < h1) h1 = kp;
333             game.kpower[ll] -= (game.kpower[ll]<0 ? -h1 : h1);
334             if (game.kpower[ll] == 0) {
335                 deadkl(w, iquad, w);
336                 return;
337             }
338             crmena(true, iquad, sector, w);
339             /* If enemy damaged but not destroyed, try to displace */
340             ang = angle + 2.5*(Rand()-0.5);
341             temp = fabs(sin(ang));
342             if (fabs(cos(ang)) > temp) temp = fabs(cos(ang));
343             xx = -sin(ang)/temp;
344             yy = cos(ang)/temp;
345             jw.x=w.x+xx+0.5;
346             jw.y=w.y+yy+0.5;
347             if (!VALID_SECTOR(jw.x, jw.y)) {
348                 prout(_(" damaged but not destroyed."));
349                 return;
350             }
351             if (game.quad[jw.x][jw.y]==IHBLANK) {
352                 prout(_(" buffeted into black hole."));
353                 deadkl(w, iquad, jw);
354                 return;
355             }
356             if (game.quad[jw.x][jw.y]!=IHDOT) {
357                 /* can't move into object */
358                 prout(_(" damaged but not destroyed."));
359                 return;
360             }
361             proutn(_(" damaged--"));
362             game.ks[ll] = jw;
363             shoved = true;
364             break;
365         case IHB: /* Hit a base */
366             skip(1);
367             prout(_("***STARBASE DESTROYED.."));
368             for_starbases(ll) {
369                 if (same(game.state.baseq[ll], game.quadrant)) {
370                     game.state.baseq[ll]=game.state.baseq[game.state.rembase];
371                     break;
372                 }
373             }
374             game.quad[w.x][w.y]=IHDOT;
375             game.state.rembase--;
376             game.base.x=game.base.y=0;
377             q->starbase--;
378             game.state.chart[game.quadrant.x][game.quadrant.y].starbase--;
379             game.state.basekl++;
380             newcnd();
381             return;
382         case IHP: /* Hit a planet */
383             crmena(true, iquad, sector, w);
384             prout(_(" destroyed."));
385             game.state.nplankl++;
386             q->planet = NOPLANET;
387             DESTROY(&game.state.plnets[game.iplnet]);
388             game.iplnet = 0;
389             game.plnet.x = game.plnet.y = 0;
390             game.quad[w.x][w.y] = IHDOT;
391             if (game.landed==1) {
392                 /* captain perishes on planet */
393                 finish(FDPLANET);
394             }
395             return;
396         case IHW: /* Hit an inhabited world -- very bad! */
397             crmena(true, iquad, sector, w);
398             prout(_(" destroyed."));
399             game.state.nworldkl++;
400             q->planet = NOPLANET;
401             DESTROY(&game.state.plnets[game.iplnet]);
402             game.iplnet = 0;
403             game.plnet.x = game.plnet.y = 0;
404             game.quad[w.x][w.y] = IHDOT;
405             if (game.landed==1) {
406                 /* captain perishes on planet */
407                 finish(FDPLANET);
408             }
409             prout("You have just destroyed an inhabited planet.");
410             prout("Celebratory rallies are being held on the Klingon homeworld.");
411             return;
412         case IHSTAR: /* Hit a star */
413             if (Rand() > 0.10) {
414                 nova(w);
415                 return;
416             }
417             crmena(true, IHSTAR, sector, w);
418             prout(_(" unaffected by photon blast."));
419             return;
420         case IHQUEST: /* Hit a thingy */
421             if (!(game.options & OPTION_THINGY) || Rand()>0.7) {
422                 skip(1);
423                 prouts(_("AAAAIIIIEEEEEEEEAAAAAAAAUUUUUGGGGGHHHHHHHHHHHH!!!"));
424                 skip(1);
425                 prouts(_("    HACK!     HACK!    HACK!        *CHOKE!*  "));
426                 skip(1);
427                 proutn(_("Mr. Spock-"));
428                 prouts(_("  \"Fascinating!\""));
429                 skip(1);
430                 deadkl(w, iquad, w);
431             } else {
432                 /*
433                  * Stas Sergeev added the possibility that
434                  * you can shove the Thingy and piss it off.
435                  * It then becomes an enemy and may fire at you.
436                  */
437                 iqengry = true;
438                 shoved = true;
439             }
440             return;
441         case IHBLANK: /* Black hole */
442             skip(1);
443             crmena(true, IHBLANK, sector, w);
444             prout(_(" swallows torpedo."));
445             return;
446         case IHWEB: /* hit the web */
447             skip(1);
448             prout(_("***Torpedo absorbed by Tholian web."));
449             return;
450         case IHT:  /* Hit a Tholian */
451             h1 = 700.0 + 100.0*Rand() -
452                 1000.0 * distance(w, in) * fabs(sin(bullseye-angle));
453             h1 = fabs(h1);
454             if (h1 >= 600) {
455                 game.quad[w.x][w.y] = IHDOT;
456                 game.ithere = false;
457                 deadkl(w, iquad, w);
458                 return;
459             }
460             skip(1);
461             crmena(true, IHT, sector, w);
462             if (Rand() > 0.05) {
463                 prout(_(" survives photon blast."));
464                 return;
465             }
466             prout(_(" disappears."));
467             game.quad[w.x][w.y] = IHWEB;
468             game.ithere = false;
469             game.nenhere--;
470             dropin(IHBLANK);
471             return;
472                                         
473         default: /* Problem! */
474             skip(1);
475             proutn("Don't know how to handle collision with ");
476             crmena(true, iquad, sector, w);
477             skip(1);
478             return;
479         }
480         break;
481     }
482     if(curwnd!=message_window) {
483         setwnd(message_window);
484     }
485     if (shoved) {
486         game.quad[w.x][w.y]=IHDOT;
487         game.quad[jw.x][jw.y]=iquad;
488         prout(_(" displaced by blast to %s "), cramlc(sector, jw));
489         for_local_enemies(ll)
490             game.kdist[ll] = game.kavgd[ll] = distance(game.sector,game.ks[ll]);
491         sortkl();
492         return;
493     }
494     skip(1);
495     prout(_("Torpedo missed."));
496     return;
497 }
498
499 static void fry(double hit)
500 /* critical-hit resolution */
501 {
502     double ncrit, extradm;
503     int ktr=1, loop1, loop2, j, cdam[NDEVICES];
504
505     /* a critical hit occured */
506     if (hit < (275.0-25.0*game.skill)*(1.0+0.5*Rand())) return;
507
508     ncrit = 1.0 + hit/(500.0+100.0*Rand());
509     proutn(_("***CRITICAL HIT--"));
510     /* Select devices and cause damage */
511     for (loop1 = 0; loop1 < ncrit; loop1++) {
512         do {
513             j = randdevice();
514             /* Cheat to prevent shuttle damage unless on ship */
515         } while 
516               (game.damage[j]<0.0 || (j==DSHUTTL && game.iscraft!=1));
517         cdam[loop1] = j;
518         extradm = (hit*game.damfac)/(ncrit*(75.0+25.0*Rand()));
519         game.damage[j] += extradm;
520         if (loop1 > 0) {
521             for (loop2=2; loop2<=loop1 && j != cdam[loop2-1]; loop2++) ;
522             if (loop2<=loop1) continue;
523             ktr += 1;
524             if (ktr==3) skip(1);
525             proutn(_(" and "));
526         }
527         proutn(device[j]);
528     }
529     prout(_(" damaged."));
530     if (damaged(DSHIELD) && game.shldup) {
531         prout(_("***Shields knocked down."));
532         game.shldup=false;
533     }
534 }
535
536 void attack(bool torps_ok) 
537 /* bad guy attacks us */
538 {
539     /* torps_ok == false forces use of phasers in an attack */
540     int percent, loop, iquad;
541     bool itflag, atackd = false, attempt = false, ihurt = false;
542     double hit, pfac, dustfac, hitmax=0.0, hittot=0.0, chgfac=1.0, r;
543     coord jay;
544     enum loctype where = neither;
545
546     game.iattak = 1;
547     if (game.alldone) return;
548     if (idebug) prout("=== ATTACK!");
549
550     if (game.ithere) movetho();
551
552     if (game.neutz) { /* The one chance not to be attacked */
553         game.neutz = false;
554         return;
555     }
556     if ((((game.comhere || game.ishere) && !game.justin) || game.skill == SKILL_EMERITUS) && torps_ok) movcom();
557     if (game.nenhere==0 || (game.nenhere==1 && iqhere && !iqengry)) return;
558     pfac = 1.0/game.inshld;
559     if (game.shldchg == 1) chgfac = 0.25+0.5*Rand();
560     skip(1);
561     if (game.skill <= SKILL_FAIR) where = sector;
562     for_local_enemies(loop) {
563         if (game.kpower[loop] < 0) continue;    /* too weak to attack */
564         /* compute hit strength and diminsh shield power */
565         r = Rand();
566         /* Increase chance of photon torpedos if docked or enemy energy low */
567         if (game.condition == docked) r *= 0.25;
568         if (game.kpower[loop] < 500) r *= 0.25; 
569         jay = game.ks[loop];
570         iquad = game.quad[jay.x][jay.y];
571         if (iquad==IHT || (iquad==IHQUEST && !iqengry)) continue;
572         itflag = (iquad == IHK && r > 0.0005) || !torps_ok ||
573             (iquad==IHC && r > 0.015) ||
574             (iquad==IHR && r > 0.3) ||
575             (iquad==IHS && r > 0.07) ||
576             (iquad==IHQUEST && r > 0.05);
577         if (itflag) {
578             /* Enemy uses phasers */
579             if (game.condition == docked) continue; /* Don't waste the effort! */
580             attempt = true; /* Attempt to attack */
581             dustfac = 0.8+0.05*Rand();
582             hit = game.kpower[loop]*pow(dustfac,game.kavgd[loop]);
583             game.kpower[loop] *= 0.75;
584         }
585         else { /* Enemy used photon torpedo */
586             double course = 1.90985*atan2((double)game.sector.y-jay.y, (double)jay.x-game.sector.x);
587             hit = 0;
588             proutn(_("***TORPEDO INCOMING"));
589             if (!damaged(DSRSENS)) {
590                 proutn(_(" From "));
591                 crmena(false, iquad, where, jay);
592             }
593             attempt = true;
594             prout("  ");
595             r = (Rand()+Rand())*0.5 -0.5;
596             r += 0.002*game.kpower[loop]*r;
597             torpedo(course, r, jay, &hit, 1, 1);
598             if (KLINGREM==0) 
599                 finish(FWON); /* Klingons did themselves in! */
600             if (game.state.galaxy[game.quadrant.x][game.quadrant.y].supernova || game.alldone) 
601                 return; /* Supernova or finished */
602             if (hit == 0) continue;
603         }
604         if (game.shldup || game.shldchg != 0 || game.condition==docked) {
605             /* shields will take hits */
606             double absorb, hitsh, propor = pfac*game.shield*(game.condition==docked ? 2.1 : 1.0);
607             if(propor < 0.1) propor = 0.1;
608             hitsh = propor*chgfac*hit+1.0;
609             atackd = true;
610             absorb = 0.8*hitsh;
611             if (absorb > game.shield) absorb = game.shield;
612             game.shield -= absorb;
613             hit -= hitsh;
614             if (game.condition==docked) dock(false);
615             if (propor > 0.1 && hit < 0.005*game.energy) continue;
616         }
617         /* It's a hit -- print out hit size */
618         atackd = true; /* We weren't going to check casualties, etc. if
619                        shields were down for some strange reason. This
620                        doesn't make any sense, so I've fixed it */
621         ihurt = true;
622         proutn(_("%d unit hit"), (int)hit);
623         if ((damaged(DSRSENS) && itflag) || game.skill<=SKILL_FAIR) {
624             proutn(_(" on the "));
625             crmshp();
626         }
627         if (!damaged(DSRSENS) && itflag) {
628             proutn(_(" from "));
629             crmena(false, iquad, where, jay);
630         }
631         skip(1);
632         /* Decide if hit is critical */
633         if (hit > hitmax) hitmax = hit;
634         hittot += hit;
635         fry(hit);
636         game.energy -= hit;
637         if (game.condition==docked) 
638             dock(false);
639     }
640     if (game.energy <= 0) {
641         /* Returning home upon your shield, not with it... */
642         finish(FBATTLE);
643         return;
644     }
645     if (!attempt && game.condition == docked)
646         prout(_("***Enemies decide against attacking your ship."));
647     if (!atackd) return;
648     percent = 100.0*pfac*game.shield+0.5;
649     if (!ihurt) {
650         /* Shields fully protect ship */
651         proutn(_("Enemy attack reduces shield strength to "));
652     }
653     else {
654         /* Print message if starship suffered hit(s) */
655         skip(1);
656         proutn(_("Energy left %2d    shields "), (int)game.energy);
657         if (game.shldup) proutn(_("up "));
658         else if (!damaged(DSHIELD)) proutn(_("down "));
659         else proutn(_("damaged, "));
660     }
661     prout(_("%d%%,   torpedoes left %d"), percent, game.torps);
662     /* Check if anyone was hurt */
663     if (hitmax >= 200 || hittot >= 500) {
664         int icas= hittot*Rand()*0.015;
665         if (icas >= 2) {
666             skip(1);
667             prout(_("Mc Coy-  \"Sickbay to bridge.  We suffered %d casualties"), icas);
668             prout(_("   in that last attack.\""));
669             game.casual += icas;
670             game.state.crew -= icas;
671         }
672     }
673     /* After attack, reset average distance to enemies */
674     for_local_enemies(loop)
675         game.kavgd[loop] = game.kdist[loop];
676     sortkl();
677     return;
678 }
679                 
680 void deadkl(coord w, feature type, coord mv)
681 /* kill a Klingon, Tholian, Romulan, or Thingy */
682 {
683     /* Added mv to allow enemy to "move" before dying */
684     int i,j;
685
686     skip(1);
687     crmena(true, type, sector, mv);
688     /* Decide what kind of enemy it is and update approriately */
689     if (type == IHR) {
690         /* chalk up a Romulan */
691         game.state.galaxy[game.quadrant.x][game.quadrant.y].romulans--;
692         game.irhere--;
693         game.state.nromrem--;
694     }
695     else if (type == IHT) {
696         /* Killed a Tholian */
697         game.ithere = false;
698     }
699     else if (type == IHQUEST) {
700         /* Killed a Thingy */
701         iqhere = iqengry = false;
702         thing.x =thing.y = 0;
703     }
704     else {
705         /* Some type of a Klingon */
706         game.state.galaxy[game.quadrant.x][game.quadrant.y].klingons--;
707         game.klhere--;
708         switch (type) {
709         case IHC:
710             game.comhere = false;
711             for_commanders (i)
712                 if (game.state.kcmdr[i].x==game.quadrant.x && game.state.kcmdr[i].y==game.quadrant.y) break;
713             game.state.kcmdr[i] = game.state.kcmdr[game.state.remcom];
714             game.state.kcmdr[game.state.remcom].x = 0;
715             game.state.kcmdr[game.state.remcom].y = 0;
716             game.state.remcom--;
717             unschedule(FTBEAM);
718             if (game.state.remcom != 0)
719                 schedule(FTBEAM, expran(1.0*game.incom/game.state.remcom));
720             break;
721         case IHK:
722             game.state.remkl--;
723             break;
724         case IHS:
725             game.state.nscrem--;
726             game.ishere = false;
727             game.state.kscmdr.x = game.state.kscmdr.y = game.isatb = game.iscate = 0;
728             unschedule(FSCMOVE);
729             unschedule(FSCDBAS);
730             break;
731         default:        /* avoids a gcc warning */
732             prout("*** Internal error, deadkl() called on %c\n", type);
733             break;
734         }
735     }
736
737     /* For each kind of enemy, finish message to player */
738     prout(_(" destroyed."));
739     game.quad[w.x][w.y] = IHDOT;
740     if (KLINGREM==0) return;
741
742     game.state.remtime = game.state.remres/(game.state.remkl + 4*game.state.remcom);
743
744     /* Remove enemy ship from arrays describing local conditions */
745     if (is_scheduled(FCDBAS) && game.battle.x==game.quadrant.x && game.battle.y==game.quadrant.y && type==IHC)
746         unschedule(FCDBAS);
747     for_local_enemies(i)
748         if (same(game.ks[i], w)) break;
749     game.nenhere--;
750     if (i <= game.nenhere)  {
751         for (j=i; j<=game.nenhere; j++) {
752             game.ks[j] = game.ks[j+1];
753             game.kpower[j] = game.kpower[j+1];
754             game.kavgd[j] = game.kdist[j] = game.kdist[j+1];
755         }
756     }
757     game.ks[game.nenhere+1].x = 0;
758     game.ks[game.nenhere+1].x = 0;
759     game.kdist[game.nenhere+1] = 0;
760     game.kavgd[game.nenhere+1] = 0;
761     game.kpower[game.nenhere+1] = 0;
762     return;
763 }
764
765 static bool targetcheck(double x, double y, double *course) 
766 {
767     double deltx, delty;
768     /* Return true if target is invalid */
769     if (!VALID_SECTOR(x, y)) {
770         huh();
771         return true;
772     }
773     deltx = 0.1*(y - game.sector.y);
774     delty = 0.1*(game.sector.x - x);
775     if (deltx==0 && delty== 0) {
776         skip(1);
777         prout(_("Spock-  \"Bridge to sickbay.  Dr. McCoy,"));
778         prout(_("  I recommend an immediate review of"));
779         prout(_("  the Captain's psychological profile.\""));
780         chew();
781         return true;
782     }
783     *course = 1.90985932*atan2(deltx, delty);
784     return false;
785 }
786
787 void photon(void) 
788 /* launch photon torpedo */
789 {
790     double targ[4][3], course[4];
791     double r, dummy;
792     int key, n, i;
793     bool osuabor;
794
795     game.ididit = false;
796
797     if (damaged(DPHOTON)) {
798         prout(_("Photon tubes damaged."));
799         chew();
800         return;
801     }
802     if (game.torps == 0) {
803         prout(_("No torpedoes left."));
804         chew();
805         return;
806     }
807     key = scan();
808     for (;;) {
809         if (key == IHALPHA) {
810             huh();
811             return;
812         }
813         else if (key == IHEOL) {
814             prout(_("%d torpedoes left."), game.torps);
815             proutn(_("Number of torpedoes to fire- "));
816             key = scan();
817         }
818         else /* key == IHREAL */ {
819             n = aaitem + 0.5;
820             if (n <= 0) { /* abort command */
821                 chew();
822                 return;
823             }
824             if (n > 3) {
825                 chew();
826                 prout(_("Maximum of 3 torpedoes per burst."));
827                 key = IHEOL;
828                 return;
829             }
830             if (n <= game.torps) break;
831             chew();
832             key = IHEOL;
833         }
834     }
835     for (i = 1; i <= n; i++) {
836         key = scan();
837         if (i==1 && key == IHEOL) {
838             break;      /* we will try prompting */
839         }
840         if (i==2 && key == IHEOL) {
841             /* direct all torpedoes at one target */
842             while (i <= n) {
843                 targ[i][1] = targ[1][1];
844                 targ[i][2] = targ[1][2];
845                 course[i] = course[1];
846                 i++;
847             }
848             break;
849         }
850         if (key != IHREAL) {
851             huh();
852             return;
853         }
854         targ[i][1] = aaitem;
855         key = scan();
856         if (key != IHREAL) {
857             huh();
858             return;
859         }
860         targ[i][2] = aaitem;
861         if (targetcheck(targ[i][1], targ[i][2], &course[i])) return;
862     }
863     chew();
864     if (i == 1 && key == IHEOL) {
865         /* prompt for each one */
866         for (i = 1; i <= n; i++) {
867             proutn(_("Target sector for torpedo number %d- "), i);
868             key = scan();
869             if (key != IHREAL) {
870                 huh();
871                 return;
872             }
873             targ[i][1] = aaitem;
874             key = scan();
875             if (key != IHREAL) {
876                 huh();
877                 return;
878             }
879             targ[i][2] = aaitem;
880             chew();
881             if (targetcheck(targ[i][1], targ[i][2], &course[i])) return;
882         }
883     }
884     game.ididit = true;
885     /* Loop for moving <n> torpedoes */
886     osuabor = false;
887     for (i = 1; i <= n && !osuabor; i++) {
888         if (game.condition != docked) game.torps--;
889         r = (Rand()+Rand())*0.5 -0.5;
890         if (fabs(r) >= 0.47) {
891             /* misfire! */
892             r = (Rand()+1.2) * r;
893             if (n>1) {
894                 prouts(_("***TORPEDO NUMBER %d MISFIRES"), i);
895             }
896             else prouts(_("***TORPEDO MISFIRES."));
897             skip(1);
898             if (i < n)
899                 prout(_("  Remainder of burst aborted."));
900             osuabor = true;
901             if (Rand() <= 0.2) {
902                 prout(_("***Photon tubes damaged by misfire."));
903                 game.damage[DPHOTON] = game.damfac*(1.0+2.0*Rand());
904                 break;
905             }
906         }
907         if (game.shldup || game.condition == docked) 
908             r *= 1.0 + 0.0001*game.shield;
909         torpedo(course[i], r, game.sector, &dummy, i, n);
910         if (game.alldone || game.state.galaxy[game.quadrant.x][game.quadrant.y].supernova)
911             return;
912     }
913     if (KLINGREM==0) finish(FWON);
914 }
915
916         
917
918 static void overheat(double rpow)
919 /* check for phasers overheating */
920 {
921     if (rpow > 1500) {
922         double chekbrn = (rpow-1500.)*0.00038;
923         if (Rand() <= chekbrn) {
924             prout(_("Weapons officer Sulu-  \"Phasers overheated, sir.\""));
925             game.damage[DPHASER] = game.damfac*(1.0 + Rand()) * (1.0+chekbrn);
926         }
927     }
928 }
929
930 static bool checkshctrl(double rpow) 
931 /* check shield control */
932 {
933     double hit;
934     int icas;
935         
936     skip(1);
937     if (Rand() < 0.998) {
938         prout(_("Shields lowered."));
939         return false;
940     }
941     /* Something bad has happened */
942     prouts(_("***RED ALERT!  RED ALERT!"));
943     skip(2);
944     hit = rpow*game.shield/game.inshld;
945     game.energy -= rpow+hit*0.8;
946     game.shield -= hit*0.2;
947     if (game.energy <= 0.0) {
948         prouts(_("Sulu-  \"Captain! Shield malf***********************\""));
949         skip(1);
950         stars();
951         finish(FPHASER);
952         return true;
953     }
954     prouts(_("Sulu-  \"Captain! Shield malfunction! Phaser fire contained!\""));
955     skip(2);
956     prout(_("Lt. Uhura-  \"Sir, all decks reporting damage.\""));
957     icas = hit*Rand()*0.012;
958     skip(1);
959     fry(0.8*hit);
960     if (icas) {
961         skip(1);
962         prout(_("McCoy to bridge- \"Severe radiation burns, Jim."));
963         prout(_("  %d casualties so far.\""), icas);
964         game.casual += icas;
965         game.state.crew -= icas;
966     }
967     skip(1);
968     prout(_("Phaser energy dispersed by shields."));
969     prout(_("Enemy unaffected."));
970     overheat(rpow);
971     return true;
972 }
973         
974
975 void phasers(void) 
976 /* fire phasers */
977 {
978     double hits[21], rpow=0, extra, powrem, over, temp;
979     int kz = 0, k=1, i, irec=0; /* Cheating inhibitor */
980     bool ifast = false, no = false, itarg = true, msgflag = true;
981     enum {NOTSET, MANUAL, FORCEMAN, AUTOMATIC} automode = NOTSET;
982     int key=0;
983
984     skip(1);
985     /* SR sensors and Computer are needed fopr automode */
986     if (damaged(DSRSENS) || damaged(DCOMPTR)) 
987         itarg = false;
988     if (game.condition == docked) {
989         prout(_("Phasers can't be fired through base shields."));
990         chew();
991         return;
992     }
993     if (damaged(DPHASER)) {
994         prout(_("Phaser control damaged."));
995         chew();
996         return;
997     }
998     if (game.shldup) {
999         if (damaged(DSHCTRL)) {
1000             prout(_("High speed shield control damaged."));
1001             chew();
1002             return;
1003         }
1004         if (game.energy <= 200.0) {
1005             prout(_("Insufficient energy to activate high-speed shield control."));
1006             chew();
1007             return;
1008         }
1009         prout(_("Weapons Officer Sulu-  \"High-speed shield control enabled, sir.\""));
1010         ifast = true;
1011                 
1012     }
1013     /* Original code so convoluted, I re-did it all */
1014     while (automode==NOTSET) {
1015         key=scan();
1016         if (key == IHALPHA) {
1017             if (isit("manual")) {
1018                 if (game.nenhere==0) {
1019                     prout(_("There is no enemy present to select."));
1020                     chew();
1021                     key = IHEOL;
1022                     automode=AUTOMATIC;
1023                 }
1024                 else {
1025                     automode = MANUAL;
1026                     key = scan();
1027                 }
1028             }
1029             else if (isit("automatic")) {
1030                 if ((!itarg) && game.nenhere != 0) {
1031                     automode = FORCEMAN;
1032                 }
1033                 else {
1034                     if (game.nenhere==0)
1035                         prout(_("Energy will be expended into space."));
1036                     automode = AUTOMATIC;
1037                     key = scan();
1038                 }
1039             }
1040             else if (isit("no")) {
1041                 no = true;
1042             }
1043             else {
1044                 huh();
1045                 return;
1046             }
1047         }
1048         else if (key == IHREAL) {
1049             if (game.nenhere==0) {
1050                 prout(_("Energy will be expended into space."));
1051                 automode = AUTOMATIC;
1052             }
1053             else if (!itarg)
1054                 automode = FORCEMAN;
1055             else
1056                 automode = AUTOMATIC;
1057         }
1058         else {
1059             /* IHEOL */
1060             if (game.nenhere==0) {
1061                 prout(_("Energy will be expended into space."));
1062                 automode = AUTOMATIC;
1063             }
1064             else if (!itarg)
1065                 automode = FORCEMAN;
1066             else 
1067                 proutn(_("Manual or automatic? "));
1068         }
1069     }
1070                                 
1071     switch (automode) {
1072     case AUTOMATIC:
1073         if (key == IHALPHA && isit("no")) {
1074             no = true;
1075             key = scan();
1076         }
1077         if (key != IHREAL && game.nenhere != 0) {
1078             prout(_("Phasers locked on target. Energy available: %.2f"),
1079                   ifast?game.energy-200.0:game.energy,1,2);
1080         }
1081         irec=0;
1082         do {
1083             chew();
1084             if (!kz) for_local_enemies(i)
1085                 irec+=fabs(game.kpower[i])/(PHASEFAC*pow(0.90,game.kdist[i]))*
1086                     (1.01+0.05*Rand()) + 1.0;
1087             kz=1;
1088             proutn(_("(%d) units required. "), irec);
1089             chew();
1090             proutn(_("Units to fire= "));
1091             key = scan();
1092             if (key!=IHREAL) return;
1093             rpow = aaitem;
1094             if (rpow > (ifast?game.energy-200:game.energy)) {
1095                 proutn(_("Energy available= %.2f"),
1096                        ifast?game.energy-200:game.energy);
1097                 skip(1);
1098                 key = IHEOL;
1099             }
1100         } while (rpow > (ifast?game.energy-200:game.energy));
1101         if (rpow<=0) {
1102             /* chicken out */
1103             chew();
1104             return;
1105         }
1106         if ((key=scan()) == IHALPHA && isit("no")) {
1107             no = true;
1108         }
1109         if (ifast) {
1110             game.energy -= 200; /* Go and do it! */
1111             if (checkshctrl(rpow)) return;
1112         }
1113         chew();
1114         game.energy -= rpow;
1115         extra = rpow;
1116         if (game.nenhere) {
1117             extra = 0.0;
1118             powrem = rpow;
1119             for_local_enemies(i) {
1120                 hits[i] = 0.0;
1121                 if (powrem <= 0) continue;
1122                 hits[i] = fabs(game.kpower[i])/(PHASEFAC*pow(0.90,game.kdist[i]));
1123                 over = (0.01 + 0.05*Rand())*hits[i];
1124                 temp = powrem;
1125                 powrem -= hits[i] + over;
1126                 if (powrem <= 0 && temp < hits[i]) hits[i] = temp;
1127                 if (powrem <= 0) over = 0.0;
1128                 extra += over;
1129             }
1130             if (powrem > 0.0) extra += powrem;
1131             hittem(hits);
1132             game.ididit = true;
1133         }
1134         if (extra > 0 && !game.alldone) {
1135             if (game.ithere) {
1136                 proutn(_("*** Tholian web absorbs "));
1137                 if (game.nenhere>0) proutn(_("excess "));
1138                 prout(_("phaser energy."));
1139             }
1140             else {
1141                 prout(_("%d expended on empty space."), (int)extra);
1142             }
1143         }
1144         break;
1145
1146     case FORCEMAN:
1147         chew();
1148         key = IHEOL;
1149         if (damaged(DCOMPTR))
1150             prout(_("Battle computer damaged, manual file only."));
1151         else {
1152             skip(1);
1153             prouts(_("---WORKING---"));
1154             skip(1);
1155             prout(_("Short-range-sensors-damaged"));
1156             prout(_("Insufficient-data-for-automatic-phaser-fire"));
1157             prout(_("Manual-fire-must-be-used"));
1158             skip(1);
1159         }
1160     case MANUAL:
1161         rpow = 0.0;
1162         for (k = 1; k <= game.nenhere;) {
1163             coord aim = game.ks[k];
1164             int ienm = game.quad[aim.x][aim.y];
1165             if (msgflag) {
1166                 proutn(_("Energy available= %.2f"),
1167                        game.energy-.006-(ifast?200:0));
1168                 skip(1);
1169                 msgflag = false;
1170                 rpow = 0.0;
1171             }
1172             if (damaged(DSRSENS) && !(abs(game.sector.x-aim.x) < 2 && abs(game.sector.y-aim.y) < 2) &&
1173                 (ienm == IHC || ienm == IHS)) {
1174                 cramen(ienm);
1175                 prout(_(" can't be located without short range scan."));
1176                 chew();
1177                 key = IHEOL;
1178                 hits[k] = 0; /* prevent overflow -- thanks to Alexei Voitenko */
1179                 k++;
1180                 continue;
1181             }
1182             if (key == IHEOL) {
1183                 chew();
1184                 if (itarg && k > kz)
1185                     irec=(fabs(game.kpower[k])/(PHASEFAC*pow(0.9,game.kdist[k])))*
1186                         (1.01+0.05*Rand()) + 1.0;
1187                 kz = k;
1188                 proutn("(");
1189                 if (!damaged(DCOMPTR)) proutn("%d", irec);
1190                 else proutn("??");
1191                 proutn(")  ");
1192                 proutn(_("units to fire at "));
1193                 crmena(false, ienm, sector, aim);
1194                 proutn("-  ");
1195                 key = scan();
1196             }
1197             if (key == IHALPHA && isit("no")) {
1198                 no = 1;
1199                 key = scan();
1200                 continue;
1201             }
1202             if (key == IHALPHA) {
1203                 huh();
1204                 return;
1205             }
1206             if (key == IHEOL) {
1207                 if (k==1) { /* Let me say I'm baffled by this */
1208                     msgflag = true;
1209                 }
1210                 continue;
1211             }
1212             if (aaitem < 0) {
1213                 /* abort out */
1214                 chew();
1215                 return;
1216             }
1217             hits[k] = aaitem;
1218             rpow += aaitem;
1219             /* If total requested is too much, inform and start over */
1220                                 
1221             if (rpow > (ifast?game.energy-200:game.energy)) {
1222                 prout(_("Available energy exceeded -- try again."));
1223                 chew();
1224                 return;
1225             }
1226             key = scan(); /* scan for next value */
1227             k++;
1228         }
1229         if (rpow == 0.0) {
1230             /* zero energy -- abort */
1231             chew();
1232             return;
1233         }
1234         if (key == IHALPHA && isit("no")) {
1235             no = true;
1236         }
1237         game.energy -= rpow;
1238         chew();
1239         if (ifast) {
1240             game.energy -= 200.0;
1241             if (checkshctrl(rpow)) return;
1242         }
1243         hittem(hits);
1244         game.ididit = true;
1245     case NOTSET:;       /* avoid gcc warning */
1246     }
1247     /* Say shield raised or malfunction, if necessary */
1248     if (game.alldone) 
1249         return;
1250     if (ifast) {
1251         skip(1);
1252         if (no == 0) {
1253             if (Rand() >= 0.99) {
1254                 prout(_("Sulu-  \"Sir, the high-speed shield control has malfunctioned . . ."));
1255                 prouts(_("         CLICK   CLICK   POP  . . ."));
1256                 prout(_(" No response, sir!"));
1257                 game.shldup = false;
1258             }
1259             else
1260                 prout(_("Shields raised."));
1261         }
1262         else
1263             game.shldup = false;
1264     }
1265     overheat(rpow);
1266 }
1267
1268 void hittem(double *hits) 
1269 /* register a phaser hit on Klingons and Romulans */
1270 {
1271     double kp, kpow, wham, hit, dustfac, kpini;
1272     int nenhr2=game.nenhere, k=1, kk=1, ienm;
1273     coord w;
1274
1275     skip(1);
1276
1277     for (; k <= nenhr2; k++, kk++) {
1278         if ((wham = hits[k])==0) continue;
1279         dustfac = 0.9 + 0.01*Rand();
1280         hit = wham*pow(dustfac,game.kdist[kk]);
1281         kpini = game.kpower[kk];
1282         kp = fabs(kpini);
1283         if (PHASEFAC*hit < kp) kp = PHASEFAC*hit;
1284         game.kpower[kk] -= (game.kpower[kk] < 0 ? -kp: kp);
1285         kpow = game.kpower[kk];
1286         w = game.ks[kk];
1287         if (hit > 0.005) {
1288             if (!damaged(DSRSENS))
1289                 boom(w);
1290             proutn(_("%d unit hit on "), (int)hit);
1291         }
1292         else
1293             proutn(_("Very small hit on "));
1294         ienm = game.quad[w.x][w.y];
1295         if (ienm==IHQUEST) iqengry = true;
1296         crmena(false,ienm,sector,w);
1297         skip(1);
1298         if (kpow == 0) {
1299             deadkl(w, ienm, w);
1300             if (KLINGREM==0) finish(FWON);
1301             if (game.alldone) return;
1302             kk--; /* don't do the increment */
1303         }
1304         else /* decide whether or not to emasculate klingon */
1305             if (kpow > 0 && Rand() >= 0.9 &&
1306                 kpow <= ((0.4 + 0.4*Rand())*kpini)) {
1307                 prout(_("***Mr. Spock-  \"Captain, the vessel at "),
1308                       cramlc(sector, w));
1309                 prout(_("   has just lost its firepower.\""));
1310                 game.kpower[kk] = -kpow;
1311             }
1312     }
1313     return;
1314 }
1315