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