Get rid of obnoxious visible "have we paused?" state.
[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 (KLINGREM) {
243         pause_game(true);
244         dreprt();
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_local_enemies(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_starbases(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             DESTROY(&game.state.planets[game.iplnet]);
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             DESTROY(&game.state.planets[game.iplnet]);
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_local_enemies(ll)
505             game.kdist[ll] = game.kavgd[ll] = distance(game.sector,game.ks[ll]);
506         sortkl();
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) skip(1);
542             proutn(_(" and "));
543         }
544         proutn(device[j]);
545     }
546     prout(_(" damaged."));
547     if (damaged(DSHIELD) && game.shldup) {
548         prout(_("***Shields knocked down."));
549         game.shldup=false;
550     }
551 }
552
553 void attack(bool torps_ok) 
554 /* bad guy attacks us */
555 {
556     /* torps_ok == false forces use of phasers in an attack */
557     int percent, loop, iquad;
558     bool usephasers, atackd = false, attempt = false, ihurt = false;
559     double hit, pfac, dustfac, hitmax=0.0, hittot=0.0, chgfac=1.0, r;
560     coord jay;
561     enum loctype where = neither;
562
563     /* game could be over at this point, check */
564     if (game.alldone) 
565         return;
566
567     if (idebug) 
568         prout("=== ATTACK!");
569
570     /* Tholian gewts to move before attacking */
571     if (game.ithere) 
572         movetho();
573
574     /* if you have just entered the RNZ, you'll get a warning */
575     if (game.neutz) { /* The one chance not to be attacked */
576         game.neutz = false;
577         return;
578     }
579
580     /* commanders get a chance to tac-move towards you */
581     if ((((game.comhere || game.ishere) && !game.justin) || game.skill == SKILL_EMERITUS) && torps_ok) 
582         moveklings();
583
584     /* if no enemies remain after movement, we're done */
585     if (game.nenhere==0 || (game.nenhere==1 && iqhere && !iqengry)) 
586         return;
587
588     /* set up partial hits if attack happens during shield status change */
589     pfac = 1.0/game.inshld;
590     if (game.shldchg)
591         chgfac = 0.25+0.5*Rand();
592
593     skip(1);
594
595     /* message verbosity control */
596     if (game.skill <= SKILL_FAIR) 
597         where = sector;
598
599     for_local_enemies(loop) {
600         if (game.kpower[loop] < 0) continue;    /* too weak to attack */
601         /* compute hit strength and diminish shield power */
602         r = Rand();
603         /* Increase chance of photon torpedos if docked or enemy energy low */
604         if (game.condition == docked)
605             r *= 0.25;
606         if (game.kpower[loop] < 500)
607             r *= 0.25; 
608         jay = game.ks[loop];
609         iquad = game.quad[jay.x][jay.y];
610         if (iquad==IHT || (iquad==IHQUEST && !iqengry))
611             continue;
612         /* different enemies have different probabilities of throwing a torp */
613         usephasers = !torps_ok || \
614             (iquad == IHK && r > 0.0005) || 
615             (iquad==IHC && r > 0.015) ||
616             (iquad==IHR && r > 0.3) ||
617             (iquad==IHS && r > 0.07) ||
618             (iquad==IHQUEST && r > 0.05);
619         if (usephasers) {           /* Enemy uses phasers */
620             if (game.condition == docked) continue; /* Don't waste the effort! */
621             attempt = true; /* Attempt to attack */
622             dustfac = 0.8+0.05*Rand();
623             hit = game.kpower[loop]*pow(dustfac,game.kavgd[loop]);
624             game.kpower[loop] *= 0.75;
625         }
626         else { /* Enemy uses photon torpedo */
627             double course = 1.90985*atan2((double)game.sector.y-jay.y, (double)jay.x-game.sector.x);
628             hit = 0;
629             proutn(_("***TORPEDO INCOMING"));
630             if (!damaged(DSRSENS)) {
631                 proutn(_(" From "));
632                 crmena(false, iquad, where, jay);
633             }
634             attempt = true;
635             prout("  ");
636             r = (Rand()+Rand())*0.5 -0.5;
637             r += 0.002*game.kpower[loop]*r;
638             torpedo(course, r, jay, &hit, 1, 1);
639             if (KLINGREM==0) 
640                 finish(FWON); /* Klingons did themselves in! */
641             if (game.state.galaxy[game.quadrant.x][game.quadrant.y].supernova || game.alldone) 
642                 return; /* Supernova or finished */
643             if (hit == 0)
644                 continue;
645         }
646         /* incoming phaser or torpedo, shields may dissipate it */
647         if (game.shldup || game.shldchg || game.condition==docked) {
648             /* shields will take hits */
649             double absorb, hitsh, propor = pfac*game.shield*(game.condition==docked ? 2.1 : 1.0);
650             if (propor < 0.1)
651                 propor = 0.1;
652             hitsh = propor*chgfac*hit+1.0;
653             absorb = 0.8*hitsh;
654             if (absorb > game.shield)
655                 absorb = game.shield;
656             game.shield -= absorb;
657             hit -= hitsh;
658             /* taking a hit blasts us out of a starbase dock */
659             if (game.condition == docked)
660                 dock(false);
661             /* but the shields may take care of it */
662             if (propor > 0.1 && hit < 0.005*game.energy) 
663                 continue;
664         }
665         /* hit from this opponent got through shields, so take damage */
666         ihurt = true;
667         proutn(_("%d unit hit"), (int)hit);
668         if ((damaged(DSRSENS) && usephasers) || game.skill<=SKILL_FAIR) {
669             proutn(_(" on the "));
670             crmshp();
671         }
672         if (!damaged(DSRSENS) && usephasers) {
673             proutn(_(" from "));
674             crmena(false, iquad, where, jay);
675         }
676         skip(1);
677         /* Decide if hit is critical */
678         if (hit > hitmax)
679             hitmax = hit;
680         hittot += hit;
681         fry(hit);
682         game.energy -= hit;
683     }
684     if (game.energy <= 0) {
685         /* Returning home upon your shield, not with it... */
686         finish(FBATTLE);
687         return;
688     }
689     if (!attempt && game.condition == docked)
690         prout(_("***Enemies decide against attacking your ship."));
691     if (!atackd)
692         return;
693     percent = 100.0*pfac*game.shield+0.5;
694     if (!ihurt) {
695         /* Shields fully protect ship */
696         proutn(_("Enemy attack reduces shield strength to "));
697     }
698     else {
699         /* Print message if starship suffered hit(s) */
700         skip(1);
701         proutn(_("Energy left %2d    shields "), (int)game.energy);
702         if (game.shldup)
703             proutn(_("up "));
704         else if (!damaged(DSHIELD))
705             proutn(_("down "));
706         else
707             proutn(_("damaged, "));
708     }
709     prout(_("%d%%,   torpedoes left %d"), percent, game.torps);
710     /* Check if anyone was hurt */
711     if (hitmax >= 200 || hittot >= 500) {
712         int icas= hittot*Rand()*0.015;
713         if (icas >= 2) {
714             skip(1);
715             prout(_("Mc Coy-  \"Sickbay to bridge.  We suffered %d casualties"), icas);
716             prout(_("   in that last attack.\""));
717             game.casual += icas;
718             game.state.crew -= icas;
719         }
720     }
721     /* After attack, reset average distance to enemies */
722     for_local_enemies(loop)
723         game.kavgd[loop] = game.kdist[loop];
724     sortkl();
725     return;
726 }
727                 
728 void deadkl(coord w, feature type, coord mv)
729 /* kill a Klingon, Tholian, Romulan, or Thingy */
730 {
731     /* Added mv to allow enemy to "move" before dying */
732     int i,j;
733
734     crmena(true, type, sector, mv);
735     /* Decide what kind of enemy it is and update appropriately */
736     if (type == IHR) {
737         /* chalk up a Romulan */
738         game.state.galaxy[game.quadrant.x][game.quadrant.y].romulans--;
739         game.irhere--;
740         game.state.nromrem--;
741     }
742     else if (type == IHT) {
743         /* Killed a Tholian */
744         game.ithere = false;
745     }
746     else if (type == IHQUEST) {
747         /* Killed a Thingy */
748         iqhere = iqengry = false;
749         invalidate(thing);
750     }
751     else {
752         /* Some type of a Klingon */
753         game.state.galaxy[game.quadrant.x][game.quadrant.y].klingons--;
754         game.klhere--;
755         switch (type) {
756         case IHC:
757             game.comhere = false;
758             for_commanders (i)
759                 if (same(game.state.kcmdr[i], game.quadrant)) 
760                     break;
761             game.state.kcmdr[i] = game.state.kcmdr[game.state.remcom];
762             game.state.kcmdr[game.state.remcom].x = 0;
763             game.state.kcmdr[game.state.remcom].y = 0;
764             game.state.remcom--;
765             unschedule(FTBEAM);
766             if (game.state.remcom != 0)
767                 schedule(FTBEAM, expran(1.0*game.incom/game.state.remcom));
768             break;
769         case IHK:
770             game.state.remkl--;
771             break;
772         case IHS:
773             game.state.nscrem--;
774             game.ishere = false;
775             game.state.kscmdr.x = game.state.kscmdr.y = game.isatb = 0;
776             game.iscate = false;
777             unschedule(FSCMOVE);
778             unschedule(FSCDBAS);
779             break;
780         default:        /* avoids a gcc warning */
781             prout("*** Internal error, deadkl() called on %c\n", type);
782             break;
783         }
784     }
785
786     /* For each kind of enemy, finish message to player */
787     prout(_(" destroyed."));
788     game.quad[w.x][w.y] = IHDOT;
789     if (KLINGREM==0)
790         return;
791
792     game.state.remtime = game.state.remres/(game.state.remkl + 4*game.state.remcom);
793
794     /* Remove enemy ship from arrays describing local conditions */
795     if (is_scheduled(FCDBAS) && same(game.battle, game.quadrant) && type==IHC)
796         unschedule(FCDBAS);
797     for_local_enemies(i)
798         if (same(game.ks[i], w))
799             break;
800     game.nenhere--;
801     if (i <= game.nenhere)  {
802         for (j=i; j<=game.nenhere; j++) {
803             game.ks[j] = game.ks[j+1];
804             game.kpower[j] = game.kpower[j+1];
805             game.kavgd[j] = game.kdist[j] = game.kdist[j+1];
806         }
807     }
808     game.ks[game.nenhere+1].x = 0;
809     game.ks[game.nenhere+1].x = 0;
810     game.kdist[game.nenhere+1] = 0;
811     game.kavgd[game.nenhere+1] = 0;
812     game.kpower[game.nenhere+1] = 0;
813     return;
814 }
815
816 static bool targetcheck(double x, double y, double *course) 
817 {
818     double deltx, delty;
819     /* Return true if target is invalid */
820     if (!VALID_SECTOR(x, y)) {
821         huh();
822         return true;
823     }
824     deltx = 0.1*(y - game.sector.y);
825     delty = 0.1*(game.sector.x - x);
826     if (deltx==0 && delty== 0) {
827         skip(1);
828         prout(_("Spock-  \"Bridge to sickbay.  Dr. McCoy,"));
829         prout(_("  I recommend an immediate review of"));
830         prout(_("  the Captain's psychological profile.\""));
831         chew();
832         return true;
833     }
834     *course = 1.90985932*atan2(deltx, delty);
835     return false;
836 }
837
838 void photon(void) 
839 /* launch photon torpedo */
840 {
841     double targ[4][3], course[4];
842     double r, dummy;
843     int key, n, i;
844
845     game.ididit = false;
846
847     if (damaged(DPHOTON)) {
848         prout(_("Photon tubes damaged."));
849         chew();
850         return;
851     }
852     if (game.torps == 0) {
853         prout(_("No torpedoes left."));
854         chew();
855         return;
856     }
857     key = scan();
858     for (;;) {
859         if (key == IHALPHA) {
860             huh();
861             return;
862         }
863         else if (key == IHEOL) {
864             prout(_("%d torpedoes left."), game.torps);
865             proutn(_("Number of torpedoes to fire- "));
866             key = scan();
867         }
868         else /* key == IHREAL */ {
869             n = aaitem + 0.5;
870             if (n <= 0) { /* abort command */
871                 chew();
872                 return;
873             }
874             if (n > 3) {
875                 chew();
876                 prout(_("Maximum of 3 torpedoes per burst."));
877                 key = IHEOL;
878                 return;
879             }
880             if (n <= game.torps)
881                 break;
882             chew();
883             key = IHEOL;
884         }
885     }
886     for (i = 1; i <= n; i++) {
887         key = scan();
888         if (i==1 && key == IHEOL) {
889             break;      /* we will try prompting */
890         }
891         if (i==2 && key == IHEOL) {
892             /* direct all torpedoes at one target */
893             while (i <= n) {
894                 targ[i][1] = targ[1][1];
895                 targ[i][2] = targ[1][2];
896                 course[i] = course[1];
897                 i++;
898             }
899             break;
900         }
901         if (key != IHREAL) {
902             huh();
903             return;
904         }
905         targ[i][1] = aaitem;
906         key = scan();
907         if (key != IHREAL) {
908             huh();
909             return;
910         }
911         targ[i][2] = aaitem;
912         if (targetcheck(targ[i][1], targ[i][2], &course[i]))
913             return;
914     }
915     chew();
916     if (i == 1 && key == IHEOL) {
917         /* prompt for each one */
918         for (i = 1; i <= n; i++) {
919             proutn(_("Target sector for torpedo number %d- "), i);
920             key = scan();
921             if (key != IHREAL) {
922                 huh();
923                 return;
924             }
925             targ[i][1] = aaitem;
926             key = scan();
927             if (key != IHREAL) {
928                 huh();
929                 return;
930             }
931             targ[i][2] = aaitem;
932             chew();
933             if (targetcheck(targ[i][1], targ[i][2], &course[i]))
934                 return;
935         }
936     }
937     game.ididit = true;
938     /* Loop for moving <n> torpedoes */
939     for (i = 1; i <= n; i++) {
940         if (game.condition != docked)
941             game.torps--;
942         r = (Rand()+Rand())*0.5 -0.5;
943         if (fabs(r) >= 0.47) {
944             /* misfire! */
945             r = (Rand()+1.2) * r;
946             if (n>1) {
947                 prouts(_("***TORPEDO NUMBER %d MISFIRES"), i);
948             }
949             else
950                 prouts(_("***TORPEDO MISFIRES."));
951             skip(1);
952             if (i < n)
953                 prout(_("  Remainder of burst aborted."));
954             if (Rand() <= 0.2) {
955                 prout(_("***Photon tubes damaged by misfire."));
956                 game.damage[DPHOTON] = game.damfac*(1.0+2.0*Rand());
957             }
958             break;
959         }
960         if (game.shldup || game.condition == docked) 
961             r *= 1.0 + 0.0001*game.shield;
962         torpedo(course[i], r, game.sector, &dummy, i, n);
963         if (game.alldone || game.state.galaxy[game.quadrant.x][game.quadrant.y].supernova)
964             return;
965     }
966     if (KLINGREM==0)
967         finish(FWON);
968 }
969
970         
971
972 static void overheat(double rpow)
973 /* check for phasers overheating */
974 {
975     if (rpow > 1500) {
976         double chekbrn = (rpow-1500.)*0.00038;
977         if (Rand() <= chekbrn) {
978             prout(_("Weapons officer Sulu-  \"Phasers overheated, sir.\""));
979             game.damage[DPHASER] = game.damfac*(1.0 + Rand()) * (1.0+chekbrn);
980         }
981     }
982 }
983
984 static bool checkshctrl(double rpow) 
985 /* check shield control */
986 {
987     double hit;
988     int icas;
989         
990     skip(1);
991     if (Rand() < 0.998) {
992         prout(_("Shields lowered."));
993         return false;
994     }
995     /* Something bad has happened */
996     prouts(_("***RED ALERT!  RED ALERT!"));
997     skip(2);
998     hit = rpow*game.shield/game.inshld;
999     game.energy -= rpow+hit*0.8;
1000     game.shield -= hit*0.2;
1001     if (game.energy <= 0.0) {
1002         prouts(_("Sulu-  \"Captain! Shield malf***********************\""));
1003         skip(1);
1004         stars();
1005         finish(FPHASER);
1006         return true;
1007     }
1008     prouts(_("Sulu-  \"Captain! Shield malfunction! Phaser fire contained!\""));
1009     skip(2);
1010     prout(_("Lt. Uhura-  \"Sir, all decks reporting damage.\""));
1011     icas = hit*Rand()*0.012;
1012     skip(1);
1013     fry(0.8*hit);
1014     if (icas) {
1015         skip(1);
1016         prout(_("McCoy to bridge- \"Severe radiation burns, Jim."));
1017         prout(_("  %d casualties so far.\""), icas);
1018         game.casual += icas;
1019         game.state.crew -= icas;
1020     }
1021     skip(1);
1022     prout(_("Phaser energy dispersed by shields."));
1023     prout(_("Enemy unaffected."));
1024     overheat(rpow);
1025     return true;
1026 }
1027         
1028
1029 void phasers(void) 
1030 /* fire phasers */
1031 {
1032     double hits[21], rpow=0, extra, powrem, over, temp;
1033     int kz = 0, k=1, i, irec=0; /* Cheating inhibitor */
1034     bool ifast = false, no = false, itarg = true, msgflag = true;
1035     enum {NOTSET, MANUAL, FORCEMAN, AUTOMATIC} automode = NOTSET;
1036     int key=0;
1037
1038     skip(1);
1039     /* SR sensors and Computer are needed fopr automode */
1040     if (damaged(DSRSENS) || damaged(DCOMPTR)) 
1041         itarg = false;
1042     if (game.condition == docked) {
1043         prout(_("Phasers can't be fired through base shields."));
1044         chew();
1045         return;
1046     }
1047     if (damaged(DPHASER)) {
1048         prout(_("Phaser control damaged."));
1049         chew();
1050         return;
1051     }
1052     if (game.shldup) {
1053         if (damaged(DSHCTRL)) {
1054             prout(_("High speed shield control damaged."));
1055             chew();
1056             return;
1057         }
1058         if (game.energy <= 200.0) {
1059             prout(_("Insufficient energy to activate high-speed shield control."));
1060             chew();
1061             return;
1062         }
1063         prout(_("Weapons Officer Sulu-  \"High-speed shield control enabled, sir.\""));
1064         ifast = true;
1065                 
1066     }
1067     /* Original code so convoluted, I re-did it all */
1068     while (automode==NOTSET) {
1069         key=scan();
1070         if (key == IHALPHA) {
1071             if (isit("manual")) {
1072                 if (game.nenhere==0) {
1073                     prout(_("There is no enemy present to select."));
1074                     chew();
1075                     key = IHEOL;
1076                     automode=AUTOMATIC;
1077                 }
1078                 else {
1079                     automode = MANUAL;
1080                     key = scan();
1081                 }
1082             }
1083             else if (isit("automatic")) {
1084                 if ((!itarg) && game.nenhere != 0) {
1085                     automode = FORCEMAN;
1086                 }
1087                 else {
1088                     if (game.nenhere==0)
1089                         prout(_("Energy will be expended into space."));
1090                     automode = AUTOMATIC;
1091                     key = scan();
1092                 }
1093             }
1094             else if (isit("no")) {
1095                 no = true;
1096             }
1097             else {
1098                 huh();
1099                 return;
1100             }
1101         }
1102         else if (key == IHREAL) {
1103             if (game.nenhere==0) {
1104                 prout(_("Energy will be expended into space."));
1105                 automode = AUTOMATIC;
1106             }
1107             else if (!itarg)
1108                 automode = FORCEMAN;
1109             else
1110                 automode = AUTOMATIC;
1111         }
1112         else {
1113             /* IHEOL */
1114             if (game.nenhere==0) {
1115                 prout(_("Energy will be expended into space."));
1116                 automode = AUTOMATIC;
1117             }
1118             else if (!itarg)
1119                 automode = FORCEMAN;
1120             else 
1121                 proutn(_("Manual or automatic? "));
1122         }
1123     }
1124                                 
1125     switch (automode) {
1126     case AUTOMATIC:
1127         if (key == IHALPHA && isit("no")) {
1128             no = true;
1129             key = scan();
1130         }
1131         if (key != IHREAL && game.nenhere != 0) {
1132             prout(_("Phasers locked on target. Energy available: %.2f"),
1133                   ifast?game.energy-200.0:game.energy);
1134         }
1135         irec=0;
1136         do {
1137             chew();
1138             if (!kz) for_local_enemies(i)
1139                 irec+=fabs(game.kpower[i])/(PHASEFAC*pow(0.90,game.kdist[i]))*
1140                     (1.01+0.05*Rand()) + 1.0;
1141             kz=1;
1142             proutn(_("%d units required. "), irec);
1143             chew();
1144             proutn(_("Units to fire= "));
1145             key = scan();
1146             if (key!=IHREAL)
1147                 return;
1148             rpow = aaitem;
1149             if (rpow > (ifast?game.energy-200:game.energy)) {
1150                 proutn(_("Energy available= %.2f"),
1151                        ifast?game.energy-200:game.energy);
1152                 skip(1);
1153                 key = IHEOL;
1154             }
1155         } while (rpow > (ifast?game.energy-200:game.energy));
1156         if (rpow<=0) {
1157             /* chicken out */
1158             chew();
1159             return;
1160         }
1161         if ((key=scan()) == IHALPHA && isit("no")) {
1162             no = true;
1163         }
1164         if (ifast) {
1165             game.energy -= 200; /* Go and do it! */
1166             if (checkshctrl(rpow))
1167                 return;
1168         }
1169         chew();
1170         game.energy -= rpow;
1171         extra = rpow;
1172         if (game.nenhere) {
1173             extra = 0.0;
1174             powrem = rpow;
1175             for_local_enemies(i) {
1176                 hits[i] = 0.0;
1177                 if (powrem <= 0)
1178                     continue;
1179                 hits[i] = fabs(game.kpower[i])/(PHASEFAC*pow(0.90,game.kdist[i]));
1180                 over = (0.01 + 0.05*Rand())*hits[i];
1181                 temp = powrem;
1182                 powrem -= hits[i] + over;
1183                 if (powrem <= 0 && temp < hits[i])
1184                     hits[i] = temp;
1185                 if (powrem <= 0)
1186                     over = 0.0;
1187                 extra += over;
1188             }
1189             if (powrem > 0.0)
1190                 extra += powrem;
1191             hittem(hits);
1192             game.ididit = true;
1193         }
1194         if (extra > 0 && !game.alldone) {
1195             if (game.ithere) {
1196                 proutn(_("*** Tholian web absorbs "));
1197                 if (game.nenhere>0)
1198                     proutn(_("excess "));
1199                 prout(_("phaser energy."));
1200             }
1201             else {
1202                 prout(_("%d expended on empty space."), (int)extra);
1203             }
1204         }
1205         break;
1206
1207     case FORCEMAN:
1208         chew();
1209         key = IHEOL;
1210         if (damaged(DCOMPTR))
1211             prout(_("Battle computer damaged, manual fire only."));
1212         else {
1213             skip(1);
1214             prouts(_("---WORKING---"));
1215             skip(1);
1216             prout(_("Short-range-sensors-damaged"));
1217             prout(_("Insufficient-data-for-automatic-phaser-fire"));
1218             prout(_("Manual-fire-must-be-used"));
1219             skip(1);
1220         }
1221     case MANUAL:
1222         rpow = 0.0;
1223         for (k = 1; k <= game.nenhere;) {
1224             coord aim = game.ks[k];
1225             int ienm = game.quad[aim.x][aim.y];
1226             if (msgflag) {
1227                 proutn(_("Energy available= %.2f"),
1228                        game.energy-.006-(ifast?200:0));
1229                 skip(1);
1230                 msgflag = false;
1231                 rpow = 0.0;
1232             }
1233             if (damaged(DSRSENS) && !(abs(game.sector.x-aim.x) < 2 && abs(game.sector.y-aim.y) < 2) &&
1234                 (ienm == IHC || ienm == IHS)) {
1235                 cramen(ienm);
1236                 prout(_(" can't be located without short range scan."));
1237                 chew();
1238                 key = IHEOL;
1239                 hits[k] = 0; /* prevent overflow -- thanks to Alexei Voitenko */
1240                 k++;
1241                 continue;
1242             }
1243             if (key == IHEOL) {
1244                 chew();
1245                 if (itarg && k > kz)
1246                     irec=(fabs(game.kpower[k])/(PHASEFAC*pow(0.9,game.kdist[k])))*
1247                         (1.01+0.05*Rand()) + 1.0;
1248                 kz = k;
1249                 proutn("(");
1250                 if (!damaged(DCOMPTR))
1251                     proutn("%d", irec);
1252                 else
1253                     proutn("??");
1254                 proutn(")  ");
1255                 proutn(_("units to fire at "));
1256                 crmena(false, ienm, sector, aim);
1257                 proutn("-  ");
1258                 key = scan();
1259             }
1260             if (key == IHALPHA && isit("no")) {
1261                 no = true;
1262                 key = scan();
1263                 continue;
1264             }
1265             if (key == IHALPHA) {
1266                 huh();
1267                 return;
1268             }
1269             if (key == IHEOL) {
1270                 if (k==1) { /* Let me say I'm baffled by this */
1271                     msgflag = true;
1272                 }
1273                 continue;
1274             }
1275             if (aaitem < 0) {
1276                 /* abort out */
1277                 chew();
1278                 return;
1279             }
1280             hits[k] = aaitem;
1281             rpow += aaitem;
1282             /* If total requested is too much, inform and start over */
1283                                 
1284             if (rpow > (ifast?game.energy-200:game.energy)) {
1285                 prout(_("Available energy exceeded -- try again."));
1286                 chew();
1287                 return;
1288             }
1289             key = scan(); /* scan for next value */
1290             k++;
1291         }
1292         if (rpow == 0.0) {
1293             /* zero energy -- abort */
1294             chew();
1295             return;
1296         }
1297         if (key == IHALPHA && isit("no")) {
1298             no = true;
1299         }
1300         game.energy -= rpow;
1301         chew();
1302         if (ifast) {
1303             game.energy -= 200.0;
1304             if (checkshctrl(rpow))
1305                 return;
1306         }
1307         hittem(hits);
1308         game.ididit = true;
1309     case NOTSET:;       /* avoid gcc warning */
1310     }
1311     /* Say shield raised or malfunction, if necessary */
1312     if (game.alldone) 
1313         return;
1314     if (ifast) {
1315         skip(1);
1316         if (no == 0) {
1317             if (Rand() >= 0.99) {
1318                 prout(_("Sulu-  \"Sir, the high-speed shield control has malfunctioned . . ."));
1319                 prouts(_("         CLICK   CLICK   POP  . . ."));
1320                 prout(_(" No response, sir!"));
1321                 game.shldup = false;
1322             }
1323             else
1324                 prout(_("Shields raised."));
1325         }
1326         else
1327             game.shldup = false;
1328     }
1329     overheat(rpow);
1330 }
1331
1332 void hittem(double *hits) 
1333 /* register a phaser hit on Klingons and Romulans */
1334 {
1335     double kp, kpow, wham, hit, dustfac, kpini;
1336     int nenhr2=game.nenhere, k=1, kk=1, ienm;
1337     coord w;
1338
1339     skip(1);
1340
1341     for (; k <= nenhr2; k++, kk++) {
1342         if ((wham = hits[k])==0)
1343             continue;
1344         dustfac = 0.9 + 0.01*Rand();
1345         hit = wham*pow(dustfac,game.kdist[kk]);
1346         kpini = game.kpower[kk];
1347         kp = fabs(kpini);
1348         if (PHASEFAC*hit < kp)
1349             kp = PHASEFAC*hit;
1350         game.kpower[kk] -= (game.kpower[kk] < 0 ? -kp: kp);
1351         kpow = game.kpower[kk];
1352         w = game.ks[kk];
1353         if (hit > 0.005) {
1354             if (!damaged(DSRSENS))
1355                 boom(w);
1356             proutn(_("%d unit hit on "), (int)hit);
1357         }
1358         else
1359             proutn(_("Very small hit on "));
1360         ienm = game.quad[w.x][w.y];
1361         if (ienm==IHQUEST)
1362             iqengry = true;
1363         crmena(false,ienm,sector,w);
1364         skip(1);
1365         if (kpow == 0) {
1366             deadkl(w, ienm, w);
1367             if (KLINGREM==0)
1368                 finish(FWON);           
1369             if (game.alldone)
1370                 return;
1371             kk--; /* don't do the increment */
1372         }
1373         else /* decide whether or not to emasculate klingon */
1374             if (kpow > 0 && Rand() >= 0.9 &&
1375                 kpow <= ((0.4 + 0.4*Rand())*kpini)) {
1376                 prout(_("***Mr. Spock-  \"Captain, the vessel at %s"),
1377                       cramlc(sector, w));
1378                 prout(_("   has just lost its firepower.\""));
1379                 game.kpower[kk] = -kpow;
1380             }
1381     }
1382     return;
1383 }
1384