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