More int-to-boolean cleanup. Make the FDISTR event work.
[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 (game.damage[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 (game.damage[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 (game.damage[DSRSENS]==0 || 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         coord w;
427         w.x = jx; w.y = jy;
428         game.quad[jx][jy]=iquad;
429         game.quad[w.x][w.y]=IHDOT;
430         prout(_(" displaced by blast to %s "), cramlc(sector, w));
431         for_local_enemies(ll)
432             game.kdist[ll] = game.kavgd[ll] = sqrt(square(game.sector.x-game.ks[ll].x)+square(game.sector.y-game.ks[ll].y));
433         sortkl();
434         return;
435     }
436     skip(1);
437     prout(_("Torpedo missed."));
438     return;
439 }
440
441 static void fry(double hit) 
442 {
443     double ncrit, extradm;
444     int ktr=1, l, ll, j, cdam[NDEVICES];
445
446     /* a critical hit occured */
447     if (hit < (275.0-25.0*game.skill)*(1.0+0.5*Rand())) return;
448
449     ncrit = 1.0 + hit/(500.0+100.0*Rand());
450     proutn(_("***CRITICAL HIT--"));
451     /* Select devices and cause damage */
452     for (l = 0; l < ncrit && 0 < NDEVICES; l++) {
453         do {
454             j = NDEVICES*Rand();
455             /* Cheat to prevent shuttle damage unless on ship */
456         } while 
457               (game.damage[j]<0.0 || (j==DSHUTTL && game.iscraft!=1) || j==DDRAY);
458         cdam[l] = j;
459         extradm = (hit*game.damfac)/(ncrit*(75.0+25.0*Rand()));
460         game.damage[j] += extradm;
461         if (l > 0) {
462             for (ll=2; ll<=l && j != cdam[ll-1]; ll++) ;
463             if (ll<=l) continue;
464             ktr += 1;
465             if (ktr==3) skip(1);
466             proutn(_(" and "));
467         }
468         proutn(device[j]);
469     }
470     prout(_(" damaged."));
471     if (game.damage[DSHIELD] && game.shldup) {
472         prout(_("***Shields knocked down."));
473         game.shldup=false;
474     }
475 }
476
477 void attack(int torps_ok) 
478 {
479     /* torps_ok == 0 forces use of phasers in an attack */
480     int percent, ihurt=0, l, i=0, iquad, itflag;
481     int atackd = 0, attempt = 0;
482     double hit;
483     double pfac, dustfac, hitmax=0.0, hittot=0.0, chgfac=1.0, r;
484     coord jay;
485
486     game.iattak = 1;
487     if (game.alldone) return;
488     if (idebug) prout("=== ATTACK!");
489
490     if (game.ithere) movetho();
491
492     if (game.neutz) { /* The one chance not to be attacked */
493         game.neutz = 0;
494         return;
495     }
496     if ((((game.comhere || game.ishere) && (game.justin == 0)) || game.skill == SKILL_EMERITUS)&&(torps_ok!=0)) movcom();
497     if (game.nenhere==0 || (game.nenhere==1 && iqhere && iqengry==0)) return;
498     pfac = 1.0/game.inshld;
499     if (game.shldchg == 1) chgfac = 0.25+0.5*Rand();
500     skip(1);
501     if (game.skill <= SKILL_FAIR) i = 2;
502     for_local_enemies(l) {
503         if (game.kpower[l] < 0) continue;       /* too weak to attack */
504         /* compute hit strength and diminsh shield power */
505         r = Rand();
506         /* Increase chance of photon torpedos if docked or enemy energy low */
507         if (game.condit == IHDOCKED) r *= 0.25;
508         if (game.kpower[l] < 500) r *= 0.25; 
509         jay.x = game.ks[l].x;
510         jay.y = game.ks[l].y;
511         iquad = game.quad[jay.x][jay.y];
512         if (iquad==IHT || (iquad==IHQUEST && !iqengry)) continue;
513         itflag = (iquad == IHK && r > 0.0005) || !torps_ok ||
514             (iquad==IHC && r > 0.015) ||
515             (iquad==IHR && r > 0.3) ||
516             (iquad==IHS && r > 0.07) ||
517             (iquad==IHQUEST && r > 0.05);
518         if (itflag) {
519             /* Enemy uses phasers */
520             if (game.condit == IHDOCKED) continue; /* Don't waste the effort! */
521             attempt = 1; /* Attempt to attack */
522             dustfac = 0.8+0.05*Rand();
523             hit = game.kpower[l]*pow(dustfac,game.kavgd[l]);
524             game.kpower[l] *= 0.75;
525         }
526         else { /* Enemy used photon torpedo */
527             double course = 1.90985*atan2((double)game.sector.y-jay.y, (double)jay.x-game.sector.x);
528             hit = 0;
529             proutn(_("***TORPEDO INCOMING"));
530             if (game.damage[DSRSENS] <= 0.0) {
531                 proutn(_(" From "));
532                 crmena(0, iquad, i, jay);
533             }
534             attempt = 1;
535             prout("  ");
536             r = (Rand()+Rand())*0.5 -0.5;
537             r += 0.002*game.kpower[l]*r;
538             torpedo(course, r, jay.x, jay.y, &hit, 1, 1);
539             if (KLINGREM==0) 
540                 finish(FWON); /* Klingons did themselves in! */
541             if (game.state.galaxy[game.quadrant.x][game.quadrant.y].supernova || game.alldone) 
542                 return; /* Supernova or finished */
543             if (hit == 0) continue;
544         }
545         if (game.shldup || game.shldchg != 0 || game.condit==IHDOCKED) {
546             /* shields will take hits */
547             double absorb, hitsh, propor = pfac*game.shield*(game.condit==IHDOCKED ? 2.1 : 1.0);
548             if(propor < 0.1) propor = 0.1;
549             hitsh = propor*chgfac*hit+1.0;
550             atackd=1;
551             absorb = 0.8*hitsh;
552             if (absorb > game.shield) absorb = game.shield;
553             game.shield -= absorb;
554             hit -= hitsh;
555             if (game.condit==IHDOCKED) dock(0);
556             if (propor > 0.1 && hit < 0.005*game.energy) continue;
557         }
558         /* It's a hit -- print out hit size */
559         atackd = 1; /* We weren't going to check casualties, etc. if
560                        shields were down for some strange reason. This
561                        doesn't make any sense, so I've fw.xed it */
562         ihurt = 1;
563         proutn(_("%d unit hit"), (int)hit);
564         if ((game.damage[DSRSENS] > 0 && itflag) || game.skill<=SKILL_FAIR) {
565             proutn(_(" on the "));
566             crmshp();
567         }
568         if (game.damage[DSRSENS] <= 0.0 && itflag) {
569             proutn(_(" from "));
570             crmena(0, iquad, i, jay);
571         }
572         skip(1);
573         /* Decide if hit is critical */
574         if (hit > hitmax) hitmax = hit;
575         hittot += hit;
576         fry(hit);
577         game.energy -= hit;
578         if (game.condit==IHDOCKED) 
579             dock(0);
580     }
581     if (game.energy <= 0) {
582         /* Returning home upon your shield, not with it... */
583         finish(FBATTLE);
584         return;
585     }
586     if (attempt == 0 && game.condit == IHDOCKED)
587         prout(_("***Enemies decide against attacking your ship."));
588     if (atackd == 0) return;
589     percent = 100.0*pfac*game.shield+0.5;
590     if (ihurt==0) {
591         /* Shields fully protect ship */
592         proutn(_("Enemy attack reduces shield strength to "));
593     }
594     else {
595         /* Print message if starship suffered hit(s) */
596         skip(1);
597         proutn(_("Energy left %2d    shields "), (int)game.energy);
598         if (game.shldup) proutn(_("up "));
599         else if (game.damage[DSHIELD] == 0) proutn(_("down "));
600         else proutn(_("damaged, "));
601     }
602     prout(_("%d%%,   torpedoes left %d"), percent, game.torps);
603     /* Check if anyone was hurt */
604     if (hitmax >= 200 || hittot >= 500) {
605         int icas= hittot*Rand()*0.015;
606         if (icas >= 2) {
607             skip(1);
608             prout(_("Mc Coy-  \"Sickbay to bridge.  We suffered %d casualties"), icas);
609             prout(_("   in that last attack.\""));
610             game.casual += icas;
611         }
612     }
613     /* After attack, reset average distance to enemies */
614     for_local_enemies(l)
615         game.kavgd[l] = game.kdist[l];
616     sortkl();
617     return;
618 }
619                 
620 void deadkl(coord w, int type, int ixx, int iyy) 
621 {
622     /* Added ixx and iyy allow enemy to "move" before dying */
623     coord mv;
624     int i,j;
625
626     mv.x = ixx; mv.y = iyy;
627     skip(1);
628     crmena(1, type, 2, mv);
629     /* Decide what kind of enemy it is and update approriately */
630     if (type == IHR) {
631         /* chalk up a Romulan */
632         game.state.galaxy[game.quadrant.x][game.quadrant.y].romulans--;
633         game.irhere--;
634         game.state.nromrem--;
635     }
636     else if (type == IHT) {
637         /* Killed a Tholian */
638         game.ithere = 0;
639     }
640     else if (type == IHQUEST) {
641         /* Killed a Thingy */
642         iqhere=iqengry=thing.x=thing.y=0;
643     }
644     else {
645         /* Some type of a Klingon */
646         game.state.galaxy[game.quadrant.x][game.quadrant.y].klingons--;
647         game.klhere--;
648         switch (type) {
649         case IHC:
650             game.comhere = 0;
651             for_commanders (i)
652                 if (game.state.kcmdr[i].x==game.quadrant.x && game.state.kcmdr[i].y==game.quadrant.y) break;
653             game.state.kcmdr[i] = game.state.kcmdr[game.state.remcom];
654             game.state.kcmdr[game.state.remcom].x = 0;
655             game.state.kcmdr[game.state.remcom].y = 0;
656             game.state.remcom--;
657             unschedule(FTBEAM);
658             if (game.state.remcom != 0)
659                 schedule(FTBEAM, expran(1.0*game.incom/game.state.remcom));
660             break;
661         case IHK:
662             game.state.remkl--;
663             break;
664         case IHS:
665             game.state.nscrem--;
666             game.ishere = game.state.kscmdr.x = game.state.kscmdr.y = game.isatb = game.iscate = 0;
667             unschedule(FSCMOVE);
668             unschedule(FSCDBAS);
669             break;
670         }
671     }
672
673     /* For each kind of enemy, finish message to player */
674     prout(_(" destroyed."));
675     game.quad[w.x][w.y] = IHDOT;
676     if (KLINGREM==0) return;
677
678     game.state.remtime = game.state.remres/(game.state.remkl + 4*game.state.remcom);
679
680     /* Remove enemy ship from arrays describing local game.conditions */
681     if (is_scheduled(FCDBAS) && game.battle.x==game.quadrant.x && game.battle.y==game.quadrant.y && type==IHC)
682         unschedule(FCDBAS);
683     for_local_enemies(i)
684         if (same(game.ks[i], w)) break;
685     game.nenhere--;
686     if (i <= game.nenhere)  {
687         for (j=i; j<=game.nenhere; j++) {
688             game.ks[j] = game.ks[j+1];
689             game.kpower[j] = game.kpower[j+1];
690             game.kavgd[j] = game.kdist[j] = game.kdist[j+1];
691         }
692     }
693     game.ks[game.nenhere+1].x = 0;
694     game.ks[game.nenhere+1].x = 0;
695     game.kdist[game.nenhere+1] = 0;
696     game.kavgd[game.nenhere+1] = 0;
697     game.kpower[game.nenhere+1] = 0;
698     return;
699 }
700
701 static bool targetcheck(double x, double y, double *course) 
702 {
703     double deltx, delty;
704     /* Return true if target is invalid */
705     if (!VALID_SECTOR(x, y)) {
706         huh();
707         return true;
708     }
709     deltx = 0.1*(y - game.sector.y);
710     delty = 0.1*(game.sector.x - x);
711     if (deltx==0 && delty== 0) {
712         skip(1);
713         prout(_("Spock-  \"Bridge to sickbay.  Dr. McCoy,"));
714         prout(_("  I recommend an immediate review of"));
715         prout(_("  the Captain's psychological profile.\""));
716         chew();
717         return true;
718     }
719     *course = 1.90985932*atan2(deltx, delty);
720     return false;
721 }
722
723 void photon(void) 
724 {
725     double targ[4][3], course[4];
726     double r, dummy;
727     int key, n, i, osuabor;
728
729     game.ididit = 0;
730
731     if (game.damage[DPHOTON]) {
732         prout(_("Photon tubes damaged."));
733         chew();
734         return;
735     }
736     if (game.torps == 0) {
737         prout(_("No torpedoes left."));
738         chew();
739         return;
740     }
741     key = scan();
742     for (;;) {
743         if (key == IHALPHA) {
744             huh();
745             return;
746         }
747         else if (key == IHEOL) {
748             prout(_("%d torpedoes left."), game.torps);
749             proutn(_("Number of torpedoes to fire- "));
750             key = scan();
751         }
752         else /* key == IHREAL */ {
753             n = aaitem + 0.5;
754             if (n <= 0) { /* abort command */
755                 chew();
756                 return;
757             }
758             if (n > 3) {
759                 chew();
760                 prout(_("Maximum of 3 torpedoes per burst."));
761                 key = IHEOL;
762                 return;
763             }
764             if (n <= game.torps) break;
765             chew();
766             key = IHEOL;
767         }
768     }
769     for (i = 1; i <= n; i++) {
770         key = scan();
771         if (i==1 && key == IHEOL) {
772             break;      /* we will try prompting */
773         }
774         if (i==2 && key == IHEOL) {
775             /* direct all torpedoes at one target */
776             while (i <= n) {
777                 targ[i][1] = targ[1][1];
778                 targ[i][2] = targ[1][2];
779                 course[i] = course[1];
780                 i++;
781             }
782             break;
783         }
784         if (key != IHREAL) {
785             huh();
786             return;
787         }
788         targ[i][1] = aaitem;
789         key = scan();
790         if (key != IHREAL) {
791             huh();
792             return;
793         }
794         targ[i][2] = aaitem;
795         if (targetcheck(targ[i][1], targ[i][2], &course[i])) return;
796     }
797     chew();
798     if (i == 1 && key == IHEOL) {
799         /* prompt for each one */
800         for (i = 1; i <= n; i++) {
801             proutn(_("Target sector for torpedo number %d- "), i);
802             key = scan();
803             if (key != IHREAL) {
804                 huh();
805                 return;
806             }
807             targ[i][1] = aaitem;
808             key = scan();
809             if (key != IHREAL) {
810                 huh();
811                 return;
812             }
813             targ[i][2] = aaitem;
814             chew();
815             if (targetcheck(targ[i][1], targ[i][2], &course[i])) return;
816         }
817     }
818     game.ididit = 1;
819     /* Loop for moving <n> torpedoes */
820     osuabor = 0;
821     for (i = 1; i <= n && !osuabor; i++) {
822         if (game.condit != IHDOCKED) game.torps--;
823         r = (Rand()+Rand())*0.5 -0.5;
824         if (fabs(r) >= 0.47) {
825             /* misfire! */
826             r = (Rand()+1.2) * r;
827             if (n>1) {
828                 prouts(_("***TORPEDO NUMBER %d MISFIRES"), i);
829             }
830             else prouts(_("***TORPEDO MISFIRES."));
831             skip(1);
832             if (i < n)
833                 prout(_("  Remainder of burst aborted."));
834             osuabor=1;
835             if (Rand() <= 0.2) {
836                 prout(_("***Photon tubes damaged by misfire."));
837                 game.damage[DPHOTON] = game.damfac*(1.0+2.0*Rand());
838                 break;
839             }
840         }
841         if (game.shldup || game.condit == IHDOCKED) 
842             r *= 1.0 + 0.0001*game.shield;
843         torpedo(course[i], r, game.sector.x, game.sector.y, &dummy, i, n);
844         if (game.alldone || game.state.galaxy[game.quadrant.x][game.quadrant.y].supernova)
845             return;
846     }
847     if (KLINGREM==0) finish(FWON);
848 }
849
850         
851
852 static void overheat(double rpow) 
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 int checkshctrl(double rpow) 
864 {
865     double hit;
866     int icas;
867         
868     skip(1);
869     if (Rand() < .998) {
870         prout(_("Shields lowered."));
871         return 0;
872     }
873     /* Something bad has happened */
874     prouts(_("***RED ALERT!  RED ALERT!"));
875     skip(2);
876     hit = rpow*game.shield/game.inshld;
877     game.energy -= rpow+hit*0.8;
878     game.shield -= hit*0.2;
879     if (game.energy <= 0.0) {
880         prouts(_("Sulu-  \"Captain! Shield malf***********************\""));
881         skip(1);
882         stars();
883         finish(FPHASER);
884         return 1;
885     }
886     prouts(_("Sulu-  \"Captain! Shield malfunction! Phaser fire contained!\""));
887     skip(2);
888     prout(_("Lt. Uhura-  \"Sir, all decks reporting damage.\""));
889     icas = hit*Rand()*0.012;
890     skip(1);
891     fry(0.8*hit);
892     if (icas) {
893         skip(1);
894         prout(_("McCoy to bridge- \"Severe radiation burns, Jim."));
895         prout(_("  %d casualties so far.\""), icas);
896         game.casual -= icas;
897     }
898     skip(1);
899     prout(_("Phaser energy dispersed by shields."));
900     prout(_("Enemy unaffected."));
901     overheat(rpow);
902     return 1;
903 }
904         
905
906 void phasers(void) 
907 {
908     double hits[21], rpow=0, extra, powrem, over, temp;
909     int kz = 0, k=1, i, irec=0; /* Cheating inhibitor */
910     int ifast=0, no=0, ipoop=1, msgflag = 1;
911     enum {NOTSET, MANUAL, FORCEMAN, AUTOMATIC} automode = NOTSET;
912     int key=0;
913
914     skip(1);
915     /* SR sensors and Computer */
916     if (game.damage[DSRSENS]+game.damage[DCOMPTR] > 0) ipoop = 0;
917     if (game.condit == IHDOCKED) {
918         prout(_("Phasers can't be fired through base shields."));
919         chew();
920         return;
921     }
922     if (game.damage[DPHASER] != 0) {
923         prout(_("Phaser control damaged."));
924         chew();
925         return;
926     }
927     if (game.shldup) {
928         if (game.damage[DSHCTRL]) {
929             prout(_("High speed shield control damaged."));
930             chew();
931             return;
932         }
933         if (game.energy <= 200.0) {
934             prout(_("Insufficient energy to activate high-speed shield control."));
935             chew();
936             return;
937         }
938         prout(_("Weapons Officer Sulu-  \"High-speed shield control enabled, sir.\""));
939         ifast = 1;
940                 
941     }
942     /* Original code so convoluted, I re-did it all */
943     while (automode==NOTSET) {
944         key=scan();
945         if (key == IHALPHA) {
946             if (isit("manual")) {
947                 if (game.nenhere==0) {
948                     prout(_("There is no enemy present to select."));
949                     chew();
950                     key = IHEOL;
951                     automode=AUTOMATIC;
952                 }
953                 else {
954                     automode = MANUAL;
955                     key = scan();
956                 }
957             }
958             else if (isit("automatic")) {
959                 if ((!ipoop) && game.nenhere != 0) {
960                     automode = FORCEMAN;
961                 }
962                 else {
963                     if (game.nenhere==0)
964                         prout(_("Energy will be expended into space."));
965                     automode = AUTOMATIC;
966                     key = scan();
967                 }
968             }
969             else if (isit("no")) {
970                 no = 1;
971             }
972             else {
973                 huh();
974                 return;
975             }
976         }
977         else if (key == IHREAL) {
978             if (game.nenhere==0) {
979                 prout(_("Energy will be expended into space."));
980                 automode = AUTOMATIC;
981             }
982             else if (!ipoop)
983                 automode = FORCEMAN;
984             else
985                 automode = AUTOMATIC;
986         }
987         else {
988             /* IHEOL */
989             if (game.nenhere==0) {
990                 prout(_("Energy will be expended into space."));
991                 automode = AUTOMATIC;
992             }
993             else if (!ipoop)
994                 automode = FORCEMAN;
995             else 
996                 proutn(_("Manual or automatic? "));
997         }
998     }
999                                 
1000     switch (automode) {
1001     case AUTOMATIC:
1002         if (key == IHALPHA && isit("no")) {
1003             no = 1;
1004             key = scan();
1005         }
1006         if (key != IHREAL && game.nenhere != 0) {
1007             prout(_("Phasers locked on target. Energy available: %.2f"),
1008                   ifast?game.energy-200.0:game.energy,1,2);
1009         }
1010         irec=0;
1011         do {
1012             chew();
1013             if (!kz) for_local_enemies(i)
1014                 irec+=fabs(game.kpower[i])/(PHASEFAC*pow(0.90,game.kdist[i]))*
1015                     (1.01+0.05*Rand()) + 1.0;
1016             kz=1;
1017             proutn(_("(%d) units required. "), irec);
1018             chew();
1019             proutn(_("Units to fire= "));
1020             key = scan();
1021             if (key!=IHREAL) return;
1022             rpow = aaitem;
1023             if (rpow > (ifast?game.energy-200:game.energy)) {
1024                 proutn(_("Energy available= %.2f"),
1025                        ifast?game.energy-200:game.energy);
1026                 skip(1);
1027                 key = IHEOL;
1028             }
1029         } while (rpow > (ifast?game.energy-200:game.energy));
1030         if (rpow<=0) {
1031             /* chicken out */
1032             chew();
1033             return;
1034         }
1035         if ((key=scan()) == IHALPHA && isit("no")) {
1036             no = 1;
1037         }
1038         if (ifast) {
1039             game.energy -= 200; /* Go and do it! */
1040             if (checkshctrl(rpow)) return;
1041         }
1042         chew();
1043         game.energy -= rpow;
1044         extra = rpow;
1045         if (game.nenhere) {
1046             extra = 0.0;
1047             powrem = rpow;
1048             for_local_enemies(i) {
1049                 hits[i] = 0.0;
1050                 if (powrem <= 0) continue;
1051                 hits[i] = fabs(game.kpower[i])/(PHASEFAC*pow(0.90,game.kdist[i]));
1052                 over = (0.01 + 0.05*Rand())*hits[i];
1053                 temp = powrem;
1054                 powrem -= hits[i] + over;
1055                 if (powrem <= 0 && temp < hits[i]) hits[i] = temp;
1056                 if (powrem <= 0) over = 0.0;
1057                 extra += over;
1058             }
1059             if (powrem > 0.0) extra += powrem;
1060             hittem(hits);
1061             game.ididit=1;
1062         }
1063         if (extra > 0 && game.alldone == 0) {
1064             if (game.ithere) {
1065                 proutn(_("*** Tholian web absorbs "));
1066                 if (game.nenhere>0) proutn(_("excess "));
1067                 prout(_("phaser energy."));
1068             }
1069             else {
1070                 prout(_("%d expended on empty space."), (int)extra);
1071             }
1072         }
1073         break;
1074
1075     case FORCEMAN:
1076         chew();
1077         key = IHEOL;
1078         if (game.damage[DCOMPTR]!=0)
1079             prout(_("Battle comuter damaged, manual file only."));
1080         else {
1081             skip(1);
1082             prouts(_("---WORKING---"));
1083             skip(1);
1084             prout(_("Short-range-sensors-damaged"));
1085             prout(_("Insufficient-data-for-automatic-phaser-fire"));
1086             prout(_("Manual-fire-must-be-used"));
1087             skip(1);
1088         }
1089     case MANUAL:
1090         rpow = 0.0;
1091         for (k = 1; k <= game.nenhere;) {
1092             coord aim = game.ks[k];
1093             int ienm = game.quad[aim.x][aim.y];
1094             if (msgflag) {
1095                 proutn(_("Energy available= %.2f"),
1096                        game.energy-.006-(ifast?200:0));
1097                 skip(1);
1098                 msgflag = 0;
1099                 rpow = 0.0;
1100             }
1101             if (game.damage[DSRSENS] && !(abs(game.sector.x-aim.x) < 2 && abs(game.sector.y-aim.y) < 2) &&
1102                 (ienm == IHC || ienm == IHS)) {
1103                 cramen(ienm);
1104                 prout(_(" can't be located without short range scan."));
1105                 chew();
1106                 key = IHEOL;
1107                 hits[k] = 0; /* prevent overflow -- thanks to Alexei Voitenko */
1108                 k++;
1109                 continue;
1110             }
1111             if (key == IHEOL) {
1112                 chew();
1113                 if (ipoop && k > kz)
1114                     irec=(fabs(game.kpower[k])/(PHASEFAC*pow(0.9,game.kdist[k])))*
1115                         (1.01+0.05*Rand()) + 1.0;
1116                 kz = k;
1117                 proutn("(");
1118                 if (game.damage[DCOMPTR]==0) proutn("%d", irec);
1119                 else proutn("??");
1120                 proutn(")  ");
1121                 proutn(_("units to fire at "));
1122                 crmena(0, ienm, 2, aim);
1123                 proutn("-  ");
1124                 key = scan();
1125             }
1126             if (key == IHALPHA && isit("no")) {
1127                 no = 1;
1128                 key = scan();
1129                 continue;
1130             }
1131             if (key == IHALPHA) {
1132                 huh();
1133                 return;
1134             }
1135             if (key == IHEOL) {
1136                 if (k==1) { /* Let me say I'm baffled by this */
1137                     msgflag = 1;
1138                 }
1139                 continue;
1140             }
1141             if (aaitem < 0) {
1142                 /* abort out */
1143                 chew();
1144                 return;
1145             }
1146             hits[k] = aaitem;
1147             rpow += aaitem;
1148             /* If total requested is too much, inform and start over */
1149                                 
1150             if (rpow > (ifast?game.energy-200:game.energy)) {
1151                 prout(_("Available energy exceeded -- try again."));
1152                 chew();
1153                 return;
1154             }
1155             key = scan(); /* scan for next value */
1156             k++;
1157         }
1158         if (rpow == 0.0) {
1159             /* zero energy -- abort */
1160             chew();
1161             return;
1162         }
1163         if (key == IHALPHA && isit("no")) {
1164             no = 1;
1165         }
1166         game.energy -= rpow;
1167         chew();
1168         if (ifast) {
1169             game.energy -= 200.0;
1170             if (checkshctrl(rpow)) return;
1171         }
1172         hittem(hits);
1173         game.ididit=1;
1174     case NOTSET:;       /* avoid gcc warning */
1175     }
1176     /* Say shield raised or malfunction, if necessary */
1177     if (game.alldone) 
1178         return;
1179     if (ifast) {
1180         skip(1);
1181         if (no == 0) {
1182             if (Rand() >= 0.99) {
1183                 prout(_("Sulu-  \"Sir, the high-speed shield control has malfunctioned . . ."));
1184                 prouts(_("         CLICK   CLICK   POP  . . ."));
1185                 prout(_(" No response, sir!"));
1186                 game.shldup = false;
1187             }
1188             else
1189                 prout(_("Shields raised."));
1190         }
1191         else
1192             game.shldup = false;
1193     }
1194     overheat(rpow);
1195 }
1196
1197 void hittem(double *hits) 
1198 {
1199     double kp, kpow, wham, hit, dustfac, kpini;
1200     int nenhr2=game.nenhere, k=1, kk=1, ienm;
1201     coord w;
1202
1203     skip(1);
1204
1205     for (; k <= nenhr2; k++, kk++) {
1206         if ((wham = hits[k])==0) continue;
1207         dustfac = 0.9 + 0.01*Rand();
1208         hit = wham*pow(dustfac,game.kdist[kk]);
1209         kpini = game.kpower[kk];
1210         kp = fabs(kpini);
1211         if (PHASEFAC*hit < kp) kp = PHASEFAC*hit;
1212         game.kpower[kk] -= (game.kpower[kk] < 0 ? -kp: kp);
1213         kpow = game.kpower[kk];
1214         w = game.ks[kk];
1215         if (hit > 0.005) {
1216             if (game.damage[DSRSENS]==0)
1217                 boom(w.x, w.y);
1218             proutn(_("%d unit hit on "), (int)hit);
1219         }
1220         else
1221             proutn(_("Very small hit on "));
1222         ienm = game.quad[w.x][w.y];
1223         if (ienm==IHQUEST) iqengry=1;
1224         crmena(0,ienm,2,w);
1225         skip(1);
1226         if (kpow == 0) {
1227             deadkl(w, ienm, w.x, w.y);
1228             if (KLINGREM==0) finish(FWON);
1229             if (game.alldone) return;
1230             kk--; /* don't do the increment */
1231         }
1232         else /* decide whether or not to emasculate klingon */
1233             if (kpow > 0 && Rand() >= 0.9 &&
1234                 kpow <= ((0.4 + 0.4*Rand())*kpini)) {
1235                 prout(_("***Mr. Spock-  \"Captain, the vessel at "),
1236                       cramlc(sector, w));
1237                 prout(_("   has just lost its firepower.\""));
1238                 game.kpower[kk] = -kpow;
1239             }
1240     }
1241     return;
1242 }
1243