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