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