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