Make the "crystals' member into an enumeration.
[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         skip(1);        /* start new line after text track */
280         switch(iquad) {
281         case IHE: /* Hit our ship */
282         case IHF:
283             skip(1);
284             proutn(_("Torpedo hits "));
285             crmshp();
286             prout(".");
287             *hit = 700.0 + 100.0*Rand() -
288                 1000.0 * distance(w, in) * fabs(sin(bullseye-angle));
289             *hit = fabs(*hit);
290             newcnd(); /* we're blown out of dock */
291             /* We may be displaced. */
292             if (game.landed || game.condition==docked) 
293                 return; /* Cheat if on a planet */
294             ang = angle + 2.5*(Rand()-0.5);
295             temp = fabs(sin(ang));
296             if (fabs(cos(ang)) > temp) temp = fabs(cos(ang));
297             xx = -sin(ang)/temp;
298             yy = cos(ang)/temp;
299             jw.x=w.x+xx+0.5;
300             jw.y=w.y+yy+0.5;
301             if (!VALID_SECTOR(jw.x, jw.y)) return;
302             if (game.quad[jw.x][jw.y]==IHBLANK) {
303                 finish(FHOLE);
304                 return;
305             }
306             if (game.quad[jw.x][jw.y]!=IHDOT) {
307                 /* can't move into object */
308                 return;
309             }
310             game.sector = jw;
311             crmshp();
312             shoved = true;
313             break;
314                                           
315         case IHC: /* Hit a commander */
316         case IHS:
317             if (Rand() <= 0.05) {
318                 crmena(true, iquad, sector, w);
319                 prout(_(" uses anti-photon device;"));
320                 prout(_("   torpedo neutralized."));
321                 return;
322             }
323         case IHR: /* Hit a regular enemy */
324         case IHK:
325             /* find the enemy */
326             for_local_enemies(ll)
327                 if (same(w, game.ks[ll]))
328                     break;
329             kp = fabs(game.kpower[ll]);
330             h1 = 700.0 + 100.0*Rand() -
331                 1000.0 * distance(w, in) * fabs(sin(bullseye-angle));
332             h1 = fabs(h1);
333             if (kp < h1) h1 = kp;
334             game.kpower[ll] -= (game.kpower[ll]<0 ? -h1 : h1);
335             if (game.kpower[ll] == 0) {
336                 deadkl(w, iquad, w);
337                 return;
338             }
339             crmena(true, iquad, sector, w);
340             /* If enemy damaged but not destroyed, try to displace */
341             ang = angle + 2.5*(Rand()-0.5);
342             temp = fabs(sin(ang));
343             if (fabs(cos(ang)) > temp) temp = fabs(cos(ang));
344             xx = -sin(ang)/temp;
345             yy = cos(ang)/temp;
346             jw.x=w.x+xx+0.5;
347             jw.y=w.y+yy+0.5;
348             if (!VALID_SECTOR(jw.x, jw.y)) {
349                 prout(_(" damaged but not destroyed."));
350                 return;
351             }
352             if (game.quad[jw.x][jw.y]==IHBLANK) {
353                 prout(_(" buffeted into black hole."));
354                 deadkl(w, iquad, jw);
355                 return;
356             }
357             if (game.quad[jw.x][jw.y]!=IHDOT) {
358                 /* can't move into object */
359                 prout(_(" damaged but not destroyed."));
360                 return;
361             }
362             proutn(_(" damaged--"));
363             game.ks[ll] = jw;
364             shoved = true;
365             break;
366         case IHB: /* Hit a base */
367             skip(1);
368             prout(_("***STARBASE DESTROYED.."));
369             for_starbases(ll) {
370                 if (same(game.state.baseq[ll], game.quadrant)) {
371                     game.state.baseq[ll]=game.state.baseq[game.state.rembase];
372                     break;
373                 }
374             }
375             game.quad[w.x][w.y]=IHDOT;
376             game.state.rembase--;
377             game.base.x=game.base.y=0;
378             q->starbase--;
379             game.state.chart[game.quadrant.x][game.quadrant.y].starbase--;
380             game.state.basekl++;
381             newcnd();
382             return;
383         case IHP: /* Hit a planet */
384             crmena(true, iquad, sector, w);
385             prout(_(" destroyed."));
386             game.state.nplankl++;
387             q->planet = NOPLANET;
388             DESTROY(&game.state.plnets[game.iplnet]);
389             game.iplnet = 0;
390             game.plnet.x = game.plnet.y = 0;
391             game.quad[w.x][w.y] = IHDOT;
392             if (game.landed) {
393                 /* captain perishes on planet */
394                 finish(FDPLANET);
395             }
396             return;
397         case IHW: /* Hit an inhabited world -- very bad! */
398             crmena(true, iquad, sector, w);
399             prout(_(" destroyed."));
400             game.state.nworldkl++;
401             q->planet = NOPLANET;
402             DESTROY(&game.state.plnets[game.iplnet]);
403             game.iplnet = 0;
404             game.plnet.x = game.plnet.y = 0;
405             game.quad[w.x][w.y] = IHDOT;
406             if (game.landed) {
407                 /* captain perishes on planet */
408                 finish(FDPLANET);
409             }
410             prout(_("You have just destroyed an inhabited planet."));
411             prout(_("Celebratory rallies are being held on the Klingon homeworld."));
412             return;
413         case IHSTAR: /* Hit a star */
414             if (Rand() > 0.10) {
415                 nova(w);
416                 return;
417             }
418             crmena(true, IHSTAR, sector, w);
419             prout(_(" unaffected by photon blast."));
420             return;
421         case IHQUEST: /* Hit a thingy */
422             if (!(game.options & OPTION_THINGY) || Rand()>0.7) {
423                 skip(1);
424                 prouts(_("AAAAIIIIEEEEEEEEAAAAAAAAUUUUUGGGGGHHHHHHHHHHHH!!!"));
425                 skip(1);
426                 prouts(_("    HACK!     HACK!    HACK!        *CHOKE!*  "));
427                 skip(1);
428                 proutn(_("Mr. Spock-"));
429                 prouts(_("  \"Fascinating!\""));
430                 skip(1);
431                 deadkl(w, iquad, w);
432             } else {
433                 /*
434                  * Stas Sergeev added the possibility that
435                  * you can shove the Thingy and piss it off.
436                  * It then becomes an enemy and may fire at you.
437                  */
438                 iqengry = true;
439                 shoved = true;
440             }
441             return;
442         case IHBLANK: /* Black hole */
443             skip(1);
444             crmena(true, IHBLANK, sector, w);
445             prout(_(" swallows torpedo."));
446             return;
447         case IHWEB: /* hit the web */
448             skip(1);
449             prout(_("***Torpedo absorbed by Tholian web."));
450             return;
451         case IHT:  /* Hit a Tholian */
452             h1 = 700.0 + 100.0*Rand() -
453                 1000.0 * distance(w, in) * fabs(sin(bullseye-angle));
454             h1 = fabs(h1);
455             if (h1 >= 600) {
456                 game.quad[w.x][w.y] = IHDOT;
457                 game.ithere = false;
458                 deadkl(w, iquad, w);
459                 return;
460             }
461             skip(1);
462             crmena(true, IHT, sector, w);
463             if (Rand() > 0.05) {
464                 prout(_(" survives photon blast."));
465                 return;
466             }
467             prout(_(" disappears."));
468             game.quad[w.x][w.y] = IHWEB;
469             game.ithere = false;
470             game.nenhere--;
471             dropin(IHBLANK);
472             return;
473                                         
474         default: /* Problem! */
475             skip(1);
476             proutn("Don't know how to handle collision with ");
477             crmena(true, iquad, sector, w);
478             skip(1);
479             return;
480         }
481         break;
482     }
483     if(curwnd!=message_window) {
484         setwnd(message_window);
485     }
486     if (shoved) {
487         game.quad[w.x][w.y]=IHDOT;
488         game.quad[jw.x][jw.y]=iquad;
489         prout(_(" displaced by blast to %s "), cramlc(sector, jw));
490         for_local_enemies(ll)
491             game.kdist[ll] = game.kavgd[ll] = distance(game.sector,game.ks[ll]);
492         sortkl();
493         return;
494     }
495     skip(1);
496     prout(_("Torpedo missed."));
497     return;
498 }
499
500 static void fry(double hit)
501 /* critical-hit resolution */
502 {
503     double ncrit, extradm;
504     int ktr=1, loop1, loop2, j, cdam[NDEVICES];
505
506     /* a critical hit occured */
507     if (hit < (275.0-25.0*game.skill)*(1.0+0.5*Rand())) return;
508
509     ncrit = 1.0 + hit/(500.0+100.0*Rand());
510     proutn(_("***CRITICAL HIT--"));
511     /* Select devices and cause damage */
512     for (loop1 = 0; loop1 < ncrit; loop1++) {
513         do {
514             j = randdevice();
515             /* Cheat to prevent shuttle damage unless on ship */
516         } while 
517               (game.damage[j]<0.0 || (j==DSHUTTL && game.iscraft != onship));
518         cdam[loop1] = j;
519         extradm = (hit*game.damfac)/(ncrit*(75.0+25.0*Rand()));
520         game.damage[j] += extradm;
521         if (loop1 > 0) {
522             for (loop2=2; loop2<=loop1 && j != cdam[loop2-1]; loop2++) ;
523             if (loop2<=loop1) continue;
524             ktr += 1;
525             if (ktr==3) skip(1);
526             proutn(_(" and "));
527         }
528         proutn(device[j]);
529     }
530     prout(_(" damaged."));
531     if (damaged(DSHIELD) && game.shldup) {
532         prout(_("***Shields knocked down."));
533         game.shldup=false;
534     }
535 }
536
537 void attack(bool torps_ok) 
538 /* bad guy attacks us */
539 {
540     /* torps_ok == false forces use of phasers in an attack */
541     int percent, loop, iquad;
542     bool usephasers, atackd = false, attempt = false, ihurt = false;
543     double hit, pfac, dustfac, hitmax=0.0, hittot=0.0, chgfac=1.0, r;
544     coord jay;
545     enum loctype where = neither;
546
547     /* game could be over at this point, check */
548     if (game.alldone) 
549         return;
550
551     if (idebug) 
552         prout("=== ATTACK!");
553
554     /* Tholian gewts to move before attacking */
555     if (game.ithere) 
556         movetho();
557
558     /* if you have just entered the RNZ, you'll get a warning */
559     if (game.neutz) { /* The one chance not to be attacked */
560         game.neutz = false;
561         return;
562     }
563
564     /* commanders get a chance to tac-move towards you */
565     if ((((game.comhere || game.ishere) && !game.justin) || game.skill == SKILL_EMERITUS) && torps_ok) 
566         movcom();
567
568     /* if no enemies remain after movement, we're done */
569     if (game.nenhere==0 || (game.nenhere==1 && iqhere && !iqengry)) 
570         return;
571
572     /* set up partial hits if attack happens during shield status change */
573     pfac = 1.0/game.inshld;
574     if (game.shldchg) chgfac = 0.25+0.5*Rand();
575
576     skip(1);
577
578     /* message verbosity control */
579     if (game.skill <= SKILL_FAIR) 
580         where = sector;
581
582     for_local_enemies(loop) {
583         if (game.kpower[loop] < 0) continue;    /* too weak to attack */
584         /* compute hit strength and diminish shield power */
585         r = Rand();
586         /* Increase chance of photon torpedos if docked or enemy energy low */
587         if (game.condition == docked) r *= 0.25;
588         if (game.kpower[loop] < 500) r *= 0.25; 
589         jay = game.ks[loop];
590         iquad = game.quad[jay.x][jay.y];
591         if (iquad==IHT || (iquad==IHQUEST && !iqengry)) continue;
592         /* different enemies have different probabilities of throwing a torp */
593         usephasers = !torps_ok || \
594             (iquad == IHK && r > 0.0005) || 
595             (iquad==IHC && r > 0.015) ||
596             (iquad==IHR && r > 0.3) ||
597             (iquad==IHS && r > 0.07) ||
598             (iquad==IHQUEST && r > 0.05);
599         if (usephasers) {           /* Enemy uses phasers */
600             if (game.condition == docked) continue; /* Don't waste the effort! */
601             attempt = true; /* Attempt to attack */
602             dustfac = 0.8+0.05*Rand();
603             hit = game.kpower[loop]*pow(dustfac,game.kavgd[loop]);
604             game.kpower[loop] *= 0.75;
605         }
606         else { /* Enemy uses photon torpedo */
607             double course = 1.90985*atan2((double)game.sector.y-jay.y, (double)jay.x-game.sector.x);
608             hit = 0;
609             proutn(_("***TORPEDO INCOMING"));
610             if (!damaged(DSRSENS)) {
611                 proutn(_(" From "));
612                 crmena(false, iquad, where, jay);
613             }
614             attempt = true;
615             prout("  ");
616             r = (Rand()+Rand())*0.5 -0.5;
617             r += 0.002*game.kpower[loop]*r;
618             torpedo(course, r, jay, &hit, 1, 1);
619             if (KLINGREM==0) 
620                 finish(FWON); /* Klingons did themselves in! */
621             if (game.state.galaxy[game.quadrant.x][game.quadrant.y].supernova || game.alldone) 
622                 return; /* Supernova or finished */
623             if (hit == 0) continue;
624         }
625         /* incoming phaser or torpedo, shields may dissipate it */
626         if (game.shldup || game.shldchg || game.condition==docked) {
627             /* shields will take hits */
628             double absorb, hitsh, propor = pfac*game.shield*(game.condition==docked ? 2.1 : 1.0);
629             if (propor < 0.1) propor = 0.1;
630             hitsh = propor*chgfac*hit+1.0;
631             absorb = 0.8*hitsh;
632             if (absorb > game.shield) absorb = game.shield;
633             game.shield -= absorb;
634             hit -= hitsh;
635             /* taking a hit blasts us out of a starbase dock */
636             if (game.condition == docked)
637                 dock(false);
638             /* but the shields may take care of it */
639             if (propor > 0.1 && hit < 0.005*game.energy) 
640                 continue;
641         }
642         /* hit from this opponent got through shields, so take damage */
643         ihurt = true;
644         proutn(_("%d unit hit"), (int)hit);
645         if ((damaged(DSRSENS) && usephasers) || game.skill<=SKILL_FAIR) {
646             proutn(_(" on the "));
647             crmshp();
648         }
649         if (!damaged(DSRSENS) && usephasers) {
650             proutn(_(" from "));
651             crmena(false, iquad, where, jay);
652         }
653         skip(1);
654         /* Decide if hit is critical */
655         if (hit > hitmax) hitmax = hit;
656         hittot += hit;
657         fry(hit);
658         game.energy -= hit;
659     }
660     if (game.energy <= 0) {
661         /* Returning home upon your shield, not with it... */
662         finish(FBATTLE);
663         return;
664     }
665     if (!attempt && game.condition == docked)
666         prout(_("***Enemies decide against attacking your ship."));
667     if (!atackd) return;
668     percent = 100.0*pfac*game.shield+0.5;
669     if (!ihurt) {
670         /* Shields fully protect ship */
671         proutn(_("Enemy attack reduces shield strength to "));
672     }
673     else {
674         /* Print message if starship suffered hit(s) */
675         skip(1);
676         proutn(_("Energy left %2d    shields "), (int)game.energy);
677         if (game.shldup) proutn(_("up "));
678         else if (!damaged(DSHIELD)) proutn(_("down "));
679         else proutn(_("damaged, "));
680     }
681     prout(_("%d%%,   torpedoes left %d"), percent, game.torps);
682     /* Check if anyone was hurt */
683     if (hitmax >= 200 || hittot >= 500) {
684         int icas= hittot*Rand()*0.015;
685         if (icas >= 2) {
686             skip(1);
687             prout(_("Mc Coy-  \"Sickbay to bridge.  We suffered %d casualties"), icas);
688             prout(_("   in that last attack.\""));
689             game.casual += icas;
690             game.state.crew -= icas;
691         }
692     }
693     /* After attack, reset average distance to enemies */
694     for_local_enemies(loop)
695         game.kavgd[loop] = game.kdist[loop];
696     sortkl();
697     return;
698 }
699                 
700 void deadkl(coord w, feature type, coord mv)
701 /* kill a Klingon, Tholian, Romulan, or Thingy */
702 {
703     /* Added mv to allow enemy to "move" before dying */
704     int i,j;
705
706     skip(1);
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         thing.x =thing.y = 0;
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     bool osuabor;
816
817     game.ididit = false;
818
819     if (damaged(DPHOTON)) {
820         prout(_("Photon tubes damaged."));
821         chew();
822         return;
823     }
824     if (game.torps == 0) {
825         prout(_("No torpedoes left."));
826         chew();
827         return;
828     }
829     key = scan();
830     for (;;) {
831         if (key == IHALPHA) {
832             huh();
833             return;
834         }
835         else if (key == IHEOL) {
836             prout(_("%d torpedoes left."), game.torps);
837             proutn(_("Number of torpedoes to fire- "));
838             key = scan();
839         }
840         else /* key == IHREAL */ {
841             n = aaitem + 0.5;
842             if (n <= 0) { /* abort command */
843                 chew();
844                 return;
845             }
846             if (n > 3) {
847                 chew();
848                 prout(_("Maximum of 3 torpedoes per burst."));
849                 key = IHEOL;
850                 return;
851             }
852             if (n <= game.torps) break;
853             chew();
854             key = IHEOL;
855         }
856     }
857     for (i = 1; i <= n; i++) {
858         key = scan();
859         if (i==1 && key == IHEOL) {
860             break;      /* we will try prompting */
861         }
862         if (i==2 && key == IHEOL) {
863             /* direct all torpedoes at one target */
864             while (i <= n) {
865                 targ[i][1] = targ[1][1];
866                 targ[i][2] = targ[1][2];
867                 course[i] = course[1];
868                 i++;
869             }
870             break;
871         }
872         if (key != IHREAL) {
873             huh();
874             return;
875         }
876         targ[i][1] = aaitem;
877         key = scan();
878         if (key != IHREAL) {
879             huh();
880             return;
881         }
882         targ[i][2] = aaitem;
883         if (targetcheck(targ[i][1], targ[i][2], &course[i])) return;
884     }
885     chew();
886     if (i == 1 && key == IHEOL) {
887         /* prompt for each one */
888         for (i = 1; i <= n; i++) {
889             proutn(_("Target sector for torpedo number %d- "), i);
890             key = scan();
891             if (key != IHREAL) {
892                 huh();
893                 return;
894             }
895             targ[i][1] = aaitem;
896             key = scan();
897             if (key != IHREAL) {
898                 huh();
899                 return;
900             }
901             targ[i][2] = aaitem;
902             chew();
903             if (targetcheck(targ[i][1], targ[i][2], &course[i])) return;
904         }
905     }
906     game.ididit = true;
907     /* Loop for moving <n> torpedoes */
908     osuabor = false;
909     for (i = 1; i <= n && !osuabor; i++) {
910         if (game.condition != docked) game.torps--;
911         r = (Rand()+Rand())*0.5 -0.5;
912         if (fabs(r) >= 0.47) {
913             /* misfire! */
914             r = (Rand()+1.2) * r;
915             if (n>1) {
916                 prouts(_("***TORPEDO NUMBER %d MISFIRES"), i);
917             }
918             else prouts(_("***TORPEDO MISFIRES."));
919             skip(1);
920             if (i < n)
921                 prout(_("  Remainder of burst aborted."));
922             osuabor = true;
923             if (Rand() <= 0.2) {
924                 prout(_("***Photon tubes damaged by misfire."));
925                 game.damage[DPHOTON] = game.damfac*(1.0+2.0*Rand());
926                 break;
927             }
928         }
929         if (game.shldup || game.condition == docked) 
930             r *= 1.0 + 0.0001*game.shield;
931         torpedo(course[i], r, game.sector, &dummy, i, n);
932         if (game.alldone || game.state.galaxy[game.quadrant.x][game.quadrant.y].supernova)
933             return;
934     }
935     if (KLINGREM==0) finish(FWON);
936 }
937
938         
939
940 static void overheat(double rpow)
941 /* check for phasers overheating */
942 {
943     if (rpow > 1500) {
944         double chekbrn = (rpow-1500.)*0.00038;
945         if (Rand() <= chekbrn) {
946             prout(_("Weapons officer Sulu-  \"Phasers overheated, sir.\""));
947             game.damage[DPHASER] = game.damfac*(1.0 + Rand()) * (1.0+chekbrn);
948         }
949     }
950 }
951
952 static bool checkshctrl(double rpow) 
953 /* check shield control */
954 {
955     double hit;
956     int icas;
957         
958     skip(1);
959     if (Rand() < 0.998) {
960         prout(_("Shields lowered."));
961         return false;
962     }
963     /* Something bad has happened */
964     prouts(_("***RED ALERT!  RED ALERT!"));
965     skip(2);
966     hit = rpow*game.shield/game.inshld;
967     game.energy -= rpow+hit*0.8;
968     game.shield -= hit*0.2;
969     if (game.energy <= 0.0) {
970         prouts(_("Sulu-  \"Captain! Shield malf***********************\""));
971         skip(1);
972         stars();
973         finish(FPHASER);
974         return true;
975     }
976     prouts(_("Sulu-  \"Captain! Shield malfunction! Phaser fire contained!\""));
977     skip(2);
978     prout(_("Lt. Uhura-  \"Sir, all decks reporting damage.\""));
979     icas = hit*Rand()*0.012;
980     skip(1);
981     fry(0.8*hit);
982     if (icas) {
983         skip(1);
984         prout(_("McCoy to bridge- \"Severe radiation burns, Jim."));
985         prout(_("  %d casualties so far.\""), icas);
986         game.casual += icas;
987         game.state.crew -= icas;
988     }
989     skip(1);
990     prout(_("Phaser energy dispersed by shields."));
991     prout(_("Enemy unaffected."));
992     overheat(rpow);
993     return true;
994 }
995         
996
997 void phasers(void) 
998 /* fire phasers */
999 {
1000     double hits[21], rpow=0, extra, powrem, over, temp;
1001     int kz = 0, k=1, i, irec=0; /* Cheating inhibitor */
1002     bool ifast = false, no = false, itarg = true, msgflag = true;
1003     enum {NOTSET, MANUAL, FORCEMAN, AUTOMATIC} automode = NOTSET;
1004     int key=0;
1005
1006     skip(1);
1007     /* SR sensors and Computer are needed fopr automode */
1008     if (damaged(DSRSENS) || damaged(DCOMPTR)) 
1009         itarg = false;
1010     if (game.condition == docked) {
1011         prout(_("Phasers can't be fired through base shields."));
1012         chew();
1013         return;
1014     }
1015     if (damaged(DPHASER)) {
1016         prout(_("Phaser control damaged."));
1017         chew();
1018         return;
1019     }
1020     if (game.shldup) {
1021         if (damaged(DSHCTRL)) {
1022             prout(_("High speed shield control damaged."));
1023             chew();
1024             return;
1025         }
1026         if (game.energy <= 200.0) {
1027             prout(_("Insufficient energy to activate high-speed shield control."));
1028             chew();
1029             return;
1030         }
1031         prout(_("Weapons Officer Sulu-  \"High-speed shield control enabled, sir.\""));
1032         ifast = true;
1033                 
1034     }
1035     /* Original code so convoluted, I re-did it all */
1036     while (automode==NOTSET) {
1037         key=scan();
1038         if (key == IHALPHA) {
1039             if (isit("manual")) {
1040                 if (game.nenhere==0) {
1041                     prout(_("There is no enemy present to select."));
1042                     chew();
1043                     key = IHEOL;
1044                     automode=AUTOMATIC;
1045                 }
1046                 else {
1047                     automode = MANUAL;
1048                     key = scan();
1049                 }
1050             }
1051             else if (isit("automatic")) {
1052                 if ((!itarg) && game.nenhere != 0) {
1053                     automode = FORCEMAN;
1054                 }
1055                 else {
1056                     if (game.nenhere==0)
1057                         prout(_("Energy will be expended into space."));
1058                     automode = AUTOMATIC;
1059                     key = scan();
1060                 }
1061             }
1062             else if (isit("no")) {
1063                 no = true;
1064             }
1065             else {
1066                 huh();
1067                 return;
1068             }
1069         }
1070         else if (key == IHREAL) {
1071             if (game.nenhere==0) {
1072                 prout(_("Energy will be expended into space."));
1073                 automode = AUTOMATIC;
1074             }
1075             else if (!itarg)
1076                 automode = FORCEMAN;
1077             else
1078                 automode = AUTOMATIC;
1079         }
1080         else {
1081             /* IHEOL */
1082             if (game.nenhere==0) {
1083                 prout(_("Energy will be expended into space."));
1084                 automode = AUTOMATIC;
1085             }
1086             else if (!itarg)
1087                 automode = FORCEMAN;
1088             else 
1089                 proutn(_("Manual or automatic? "));
1090         }
1091     }
1092                                 
1093     switch (automode) {
1094     case AUTOMATIC:
1095         if (key == IHALPHA && isit("no")) {
1096             no = true;
1097             key = scan();
1098         }
1099         if (key != IHREAL && game.nenhere != 0) {
1100             prout(_("Phasers locked on target. Energy available: %.2f"),
1101                   ifast?game.energy-200.0:game.energy,1,2);
1102         }
1103         irec=0;
1104         do {
1105             chew();
1106             if (!kz) for_local_enemies(i)
1107                 irec+=fabs(game.kpower[i])/(PHASEFAC*pow(0.90,game.kdist[i]))*
1108                     (1.01+0.05*Rand()) + 1.0;
1109             kz=1;
1110             proutn(_("%d units required. "), irec);
1111             chew();
1112             proutn(_("Units to fire= "));
1113             key = scan();
1114             if (key!=IHREAL) return;
1115             rpow = aaitem;
1116             if (rpow > (ifast?game.energy-200:game.energy)) {
1117                 proutn(_("Energy available= %.2f"),
1118                        ifast?game.energy-200:game.energy);
1119                 skip(1);
1120                 key = IHEOL;
1121             }
1122         } while (rpow > (ifast?game.energy-200:game.energy));
1123         if (rpow<=0) {
1124             /* chicken out */
1125             chew();
1126             return;
1127         }
1128         if ((key=scan()) == IHALPHA && isit("no")) {
1129             no = true;
1130         }
1131         if (ifast) {
1132             game.energy -= 200; /* Go and do it! */
1133             if (checkshctrl(rpow)) return;
1134         }
1135         chew();
1136         game.energy -= rpow;
1137         extra = rpow;
1138         if (game.nenhere) {
1139             extra = 0.0;
1140             powrem = rpow;
1141             for_local_enemies(i) {
1142                 hits[i] = 0.0;
1143                 if (powrem <= 0) continue;
1144                 hits[i] = fabs(game.kpower[i])/(PHASEFAC*pow(0.90,game.kdist[i]));
1145                 over = (0.01 + 0.05*Rand())*hits[i];
1146                 temp = powrem;
1147                 powrem -= hits[i] + over;
1148                 if (powrem <= 0 && temp < hits[i]) hits[i] = temp;
1149                 if (powrem <= 0) over = 0.0;
1150                 extra += over;
1151             }
1152             if (powrem > 0.0) extra += powrem;
1153             hittem(hits);
1154             game.ididit = true;
1155         }
1156         if (extra > 0 && !game.alldone) {
1157             if (game.ithere) {
1158                 proutn(_("*** Tholian web absorbs "));
1159                 if (game.nenhere>0) proutn(_("excess "));
1160                 prout(_("phaser energy."));
1161             }
1162             else {
1163                 prout(_("%d expended on empty space."), (int)extra);
1164             }
1165         }
1166         break;
1167
1168     case FORCEMAN:
1169         chew();
1170         key = IHEOL;
1171         if (damaged(DCOMPTR))
1172             prout(_("Battle computer damaged, manual file only."));
1173         else {
1174             skip(1);
1175             prouts(_("---WORKING---"));
1176             skip(1);
1177             prout(_("Short-range-sensors-damaged"));
1178             prout(_("Insufficient-data-for-automatic-phaser-fire"));
1179             prout(_("Manual-fire-must-be-used"));
1180             skip(1);
1181         }
1182     case MANUAL:
1183         rpow = 0.0;
1184         for (k = 1; k <= game.nenhere;) {
1185             coord aim = game.ks[k];
1186             int ienm = game.quad[aim.x][aim.y];
1187             if (msgflag) {
1188                 proutn(_("Energy available= %.2f"),
1189                        game.energy-.006-(ifast?200:0));
1190                 skip(1);
1191                 msgflag = false;
1192                 rpow = 0.0;
1193             }
1194             if (damaged(DSRSENS) && !(abs(game.sector.x-aim.x) < 2 && abs(game.sector.y-aim.y) < 2) &&
1195                 (ienm == IHC || ienm == IHS)) {
1196                 cramen(ienm);
1197                 prout(_(" can't be located without short range scan."));
1198                 chew();
1199                 key = IHEOL;
1200                 hits[k] = 0; /* prevent overflow -- thanks to Alexei Voitenko */
1201                 k++;
1202                 continue;
1203             }
1204             if (key == IHEOL) {
1205                 chew();
1206                 if (itarg && k > kz)
1207                     irec=(fabs(game.kpower[k])/(PHASEFAC*pow(0.9,game.kdist[k])))*
1208                         (1.01+0.05*Rand()) + 1.0;
1209                 kz = k;
1210                 proutn("(");
1211                 if (!damaged(DCOMPTR)) proutn("%d", irec);
1212                 else proutn("??");
1213                 proutn(")  ");
1214                 proutn(_("units to fire at "));
1215                 crmena(false, ienm, sector, aim);
1216                 proutn("-  ");
1217                 key = scan();
1218             }
1219             if (key == IHALPHA && isit("no")) {
1220                 no = true;
1221                 key = scan();
1222                 continue;
1223             }
1224             if (key == IHALPHA) {
1225                 huh();
1226                 return;
1227             }
1228             if (key == IHEOL) {
1229                 if (k==1) { /* Let me say I'm baffled by this */
1230                     msgflag = true;
1231                 }
1232                 continue;
1233             }
1234             if (aaitem < 0) {
1235                 /* abort out */
1236                 chew();
1237                 return;
1238             }
1239             hits[k] = aaitem;
1240             rpow += aaitem;
1241             /* If total requested is too much, inform and start over */
1242                                 
1243             if (rpow > (ifast?game.energy-200:game.energy)) {
1244                 prout(_("Available energy exceeded -- try again."));
1245                 chew();
1246                 return;
1247             }
1248             key = scan(); /* scan for next value */
1249             k++;
1250         }
1251         if (rpow == 0.0) {
1252             /* zero energy -- abort */
1253             chew();
1254             return;
1255         }
1256         if (key == IHALPHA && isit("no")) {
1257             no = true;
1258         }
1259         game.energy -= rpow;
1260         chew();
1261         if (ifast) {
1262             game.energy -= 200.0;
1263             if (checkshctrl(rpow)) return;
1264         }
1265         hittem(hits);
1266         game.ididit = true;
1267     case NOTSET:;       /* avoid gcc warning */
1268     }
1269     /* Say shield raised or malfunction, if necessary */
1270     if (game.alldone) 
1271         return;
1272     if (ifast) {
1273         skip(1);
1274         if (no == 0) {
1275             if (Rand() >= 0.99) {
1276                 prout(_("Sulu-  \"Sir, the high-speed shield control has malfunctioned . . ."));
1277                 prouts(_("         CLICK   CLICK   POP  . . ."));
1278                 prout(_(" No response, sir!"));
1279                 game.shldup = false;
1280             }
1281             else
1282                 prout(_("Shields raised."));
1283         }
1284         else
1285             game.shldup = false;
1286     }
1287     overheat(rpow);
1288 }
1289
1290 void hittem(double *hits) 
1291 /* register a phaser hit on Klingons and Romulans */
1292 {
1293     double kp, kpow, wham, hit, dustfac, kpini;
1294     int nenhr2=game.nenhere, k=1, kk=1, ienm;
1295     coord w;
1296
1297     skip(1);
1298
1299     for (; k <= nenhr2; k++, kk++) {
1300         if ((wham = hits[k])==0) continue;
1301         dustfac = 0.9 + 0.01*Rand();
1302         hit = wham*pow(dustfac,game.kdist[kk]);
1303         kpini = game.kpower[kk];
1304         kp = fabs(kpini);
1305         if (PHASEFAC*hit < kp) kp = PHASEFAC*hit;
1306         game.kpower[kk] -= (game.kpower[kk] < 0 ? -kp: kp);
1307         kpow = game.kpower[kk];
1308         w = game.ks[kk];
1309         if (hit > 0.005) {
1310             if (!damaged(DSRSENS))
1311                 boom(w);
1312             proutn(_("%d unit hit on "), (int)hit);
1313         }
1314         else
1315             proutn(_("Very small hit on "));
1316         ienm = game.quad[w.x][w.y];
1317         if (ienm==IHQUEST) iqengry = true;
1318         crmena(false,ienm,sector,w);
1319         skip(1);
1320         if (kpow == 0) {
1321             deadkl(w, ienm, w);
1322             if (KLINGREM==0) finish(FWON);
1323             if (game.alldone) return;
1324             kk--; /* don't do the increment */
1325         }
1326         else /* decide whether or not to emasculate klingon */
1327             if (kpow > 0 && Rand() >= 0.9 &&
1328                 kpow <= ((0.4 + 0.4*Rand())*kpini)) {
1329                 prout(_("***Mr. Spock-  \"Captain, the vessel at "),
1330                       cramlc(sector, w));
1331                 prout(_("   has just lost its firepower.\""));
1332                 game.kpower[kk] = -kpow;
1333             }
1334     }
1335     return;
1336 }
1337