More BSD features.
[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     game.state.crew -= icas;
162     for (l=0; l < NDEVICES; l++) {
163         if (l == DDRAY) 
164             continue; // Don't damage deathray 
165         if (game.damage[l] < 0) 
166             continue;
167         extradm = (10.0*type*Rand()+1.0)*game.damfac;
168         game.damage[l] += game.optime + extradm; /* Damage for at least time of travel! */
169     }
170     game.shldup = false;
171     if (KLINGREM) {
172         pause_game(2);
173         dreprt();
174     }
175     else finish(FWON);
176     return;
177 }
178
179 void torpedo(double course, double r, int inx, int iny, double *hit, int i, int n)
180 {
181     int l, iquad=0, jx=0, jy=0, shoved=0, ll;
182         
183     double ac=course + 0.25*r;
184     double angle = (15.0-ac)*0.5235988;
185     double bullseye = (15.0 - course)*0.5235988;
186     double deltax=-sin(angle), deltay=cos(angle), x=inx, y=iny, bigger;
187     double ang, temp, xx, yy, kp, h1;
188     coord w;
189
190     w.x = w.y = 0;
191     bigger = fabs(deltax);
192     if (fabs(deltay) > bigger) bigger = fabs(deltay);
193     deltax /= bigger;
194     deltay /= bigger;
195     if (!damaged(DSRSENS) || game.condit==IHDOCKED) 
196         setwnd(srscan_window);
197     else 
198         setwnd(message_window);
199     /* Loop to move a single torpedo */
200     for (l=1; l <= 15; l++) {
201         x += deltax;
202         w.x = x + 0.5;
203         y += deltay;
204         w.y = y + 0.5;
205         if (!VALID_SECTOR(w.x, w.y)) break;
206         iquad=game.quad[w.x][w.y];
207         tracktorpedo(w.x, w.y, l, i, n, iquad);
208         if (iquad==IHDOT) continue;
209         /* hit something */
210         setwnd(message_window);
211         skip(1);        /* start new line after text track */
212         switch(iquad) {
213         case IHE: /* Hit our ship */
214         case IHF:
215             skip(1);
216             proutn(_("Torpedo hits "));
217             crmshp();
218             prout(".");
219             *hit = 700.0 + 100.0*Rand() -
220                 1000.0*sqrt(square(w.x-inx)+square(w.y-iny))*
221                 fabs(sin(bullseye-angle));
222             *hit = fabs(*hit);
223             newcnd(); /* we're blown out of dock */
224             /* We may be displaced. */
225             if (game.landed==1 || game.condit==IHDOCKED) return; /* Cheat if on a planet */
226             ang = angle + 2.5*(Rand()-0.5);
227             temp = fabs(sin(ang));
228             if (fabs(cos(ang)) > temp) temp = fabs(cos(ang));
229             xx = -sin(ang)/temp;
230             yy = cos(ang)/temp;
231             jx=w.x+xx+0.5;
232             jy=w.y+yy+0.5;
233             if (!VALID_SECTOR(jx, jy)) return;
234             if (game.quad[jx][jy]==IHBLANK) {
235                 finish(FHOLE);
236                 return;
237             }
238             if (game.quad[jx][jy]!=IHDOT) {
239                 /* can't move into object */
240                 return;
241             }
242             game.sector.x = jx;
243             game.sector.y = jy;
244             crmshp();
245             shoved = 1;
246             break;
247                                           
248         case IHC: /* Hit a commander */
249         case IHS:
250             if (Rand() <= 0.05) {
251                 crmena(1, iquad, 2, w);
252                 prout(_(" uses anti-photon device;"));
253                 prout(_("   torpedo neutralized."));
254                 return;
255             }
256         case IHR: /* Hit a regular enemy */
257         case IHK:
258             /* find the enemy */
259             for_local_enemies(ll)
260                 if (w.x==game.ks[ll].x && w.y==game.ks[ll].y) break;
261             kp = fabs(game.kpower[ll]);
262             h1 = 700.0 + 100.0*Rand() -
263                 1000.0*sqrt(square(w.x-inx)+square(w.y-iny))*
264                 fabs(sin(bullseye-angle));
265             h1 = fabs(h1);
266             if (kp < h1) h1 = kp;
267             game.kpower[ll] -= (game.kpower[ll]<0 ? -h1 : h1);
268             if (game.kpower[ll] == 0) {
269                 deadkl(w, iquad, w.x, w.y);
270                 return;
271             }
272             crmena(1, iquad, 2, w);
273             /* If enemy damaged but not destroyed, try to displace */
274             ang = angle + 2.5*(Rand()-0.5);
275             temp = fabs(sin(ang));
276             if (fabs(cos(ang)) > temp) temp = fabs(cos(ang));
277             xx = -sin(ang)/temp;
278             yy = cos(ang)/temp;
279             jx=w.x+xx+0.5;
280             jy=w.y+yy+0.5;
281             if (!VALID_SECTOR(jx, jy)) {
282                 prout(_(" damaged but not destroyed."));
283                 return;
284             }
285             if (game.quad[jx][jy]==IHBLANK) {
286                 prout(_(" buffeted into black hole."));
287                 deadkl(w, iquad, jx, jy);
288                 return;
289             }
290             if (game.quad[jx][jy]!=IHDOT) {
291                 /* can't move into object */
292                 prout(_(" damaged but not destroyed."));
293                 return;
294             }
295             proutn(_(" damaged--"));
296             game.ks[ll].x = jx;
297             game.ks[ll].y = jy;
298             shoved = 1;
299             break;
300         case IHB: /* Hit a base */
301             skip(1);
302             prout(_("***STARBASE DESTROYED.."));
303             for_starbases(ll) {
304                 if (game.state.baseq[ll].x==game.quadrant.x && game.state.baseq[ll].y==game.quadrant.y) {
305                     game.state.baseq[ll].x=game.state.baseq[game.state.rembase].x;
306                     game.state.baseq[ll].y=game.state.baseq[game.state.rembase].y;
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             game.state.galaxy[game.quadrant.x][game.quadrant.y].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(1, iquad, 2, w);
320             prout(_(" destroyed."));
321             game.state.nplankl++;
322             game.state.galaxy[game.quadrant.x][game.quadrant.y].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(1, iquad, 2, w);
334             prout(_(" destroyed."));
335             game.state.nworldkl++;
336             game.state.galaxy[game.quadrant.x][game.quadrant.y].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.x, w.y);
351                 return;
352             }
353             crmena(1, IHSTAR, 2, 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.x, w.y);
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=1;
374                 shoved=1;
375             }
376             return;
377         case IHBLANK: /* Black hole */
378             skip(1);
379             crmena(1, IHBLANK, 2, 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*sqrt(square(w.x-inx)+square(w.y-iny))*
389                 fabs(sin(bullseye-angle));
390             h1 = fabs(h1);
391             if (h1 >= 600) {
392                 game.quad[w.x][w.y] = IHDOT;
393                 game.ithere = 0;
394                 game.tholian.x = game.tholian.y = 0;
395                 deadkl(w, iquad, w.x, w.y);
396                 return;
397             }
398             skip(1);
399             crmena(1, IHT, 2, w);
400             if (Rand() > 0.05) {
401                 prout(_(" survives photon blast."));
402                 return;
403             }
404             prout(_(" disappears."));
405             game.quad[w.x][w.y] = IHWEB;
406             game.ithere = game.tholian.x = game.tholian.y = 0;
407             game.nenhere--;
408             {
409                 coord dummy;
410                 dropin(IHBLANK, &dummy);
411             }
412             return;
413                                         
414         default: /* Problem! */
415             skip(1);
416             proutn("Don't know how to handle collision with ");
417             crmena(1, iquad, 2, w);
418             skip(1);
419             return;
420         }
421         break;
422     }
423     if(curwnd!=message_window) {
424         setwnd(message_window);
425     }
426     if (shoved) {
427         game.quad[w.x][w.y]=IHDOT;
428         game.quad[jx][jy]=iquad;
429         prout(_(" displaced by blast to %s "), cramlc(sector, w));
430         for_local_enemies(ll)
431             game.kdist[ll] = game.kavgd[ll] = sqrt(square(game.sector.x-game.ks[ll].x)+square(game.sector.y-game.ks[ll].y));
432         sortkl();
433         return;
434     }
435     skip(1);
436     prout(_("Torpedo missed."));
437     return;
438 }
439
440 static void fry(double hit) 
441 {
442     double ncrit, extradm;
443     int ktr=1, l, ll, j, cdam[NDEVICES];
444
445     /* a critical hit occured */
446     if (hit < (275.0-25.0*game.skill)*(1.0+0.5*Rand())) return;
447
448     ncrit = 1.0 + hit/(500.0+100.0*Rand());
449     proutn(_("***CRITICAL HIT--"));
450     /* Select devices and cause damage */
451     for (l = 0; l < ncrit && 0 < NDEVICES; l++) {
452         do {
453             j = NDEVICES*Rand();
454             /* Cheat to prevent shuttle damage unless on ship */
455         } while 
456               (game.damage[j]<0.0 || (j==DSHUTTL && game.iscraft!=1) || j==DDRAY);
457         cdam[l] = j;
458         extradm = (hit*game.damfac)/(ncrit*(75.0+25.0*Rand()));
459         game.damage[j] += extradm;
460         if (l > 0) {
461             for (ll=2; ll<=l && j != cdam[ll-1]; ll++) ;
462             if (ll<=l) continue;
463             ktr += 1;
464             if (ktr==3) skip(1);
465             proutn(_(" and "));
466         }
467         proutn(device[j]);
468     }
469     prout(_(" damaged."));
470     if (damaged(DSHIELD) && game.shldup) {
471         prout(_("***Shields knocked down."));
472         game.shldup=false;
473     }
474 }
475
476 void attack(int torps_ok) 
477 {
478     /* torps_ok == 0 forces use of phasers in an attack */
479     int percent, ihurt=0, l, i=0, iquad, itflag;
480     int atackd = 0, attempt = 0;
481     double hit;
482     double pfac, dustfac, hitmax=0.0, hittot=0.0, chgfac=1.0, r;
483     coord jay;
484
485     game.iattak = 1;
486     if (game.alldone) return;
487     if (idebug) prout("=== ATTACK!");
488
489     if (game.ithere) movetho();
490
491     if (game.neutz) { /* The one chance not to be attacked */
492         game.neutz = 0;
493         return;
494     }
495     if ((((game.comhere || game.ishere) && (game.justin == 0)) || game.skill == SKILL_EMERITUS)&&(torps_ok!=0)) movcom();
496     if (game.nenhere==0 || (game.nenhere==1 && iqhere && iqengry==0)) return;
497     pfac = 1.0/game.inshld;
498     if (game.shldchg == 1) chgfac = 0.25+0.5*Rand();
499     skip(1);
500     if (game.skill <= SKILL_FAIR) i = 2;
501     for_local_enemies(l) {
502         if (game.kpower[l] < 0) continue;       /* too weak to attack */
503         /* compute hit strength and diminsh shield power */
504         r = Rand();
505         /* Increase chance of photon torpedos if docked or enemy energy low */
506         if (game.condit == IHDOCKED) r *= 0.25;
507         if (game.kpower[l] < 500) r *= 0.25; 
508         jay.x = game.ks[l].x;
509         jay.y = game.ks[l].y;
510         iquad = game.quad[jay.x][jay.y];
511         if (iquad==IHT || (iquad==IHQUEST && !iqengry)) continue;
512         itflag = (iquad == IHK && r > 0.0005) || !torps_ok ||
513             (iquad==IHC && r > 0.015) ||
514             (iquad==IHR && r > 0.3) ||
515             (iquad==IHS && r > 0.07) ||
516             (iquad==IHQUEST && r > 0.05);
517         if (itflag) {
518             /* Enemy uses phasers */
519             if (game.condit == IHDOCKED) continue; /* Don't waste the effort! */
520             attempt = 1; /* Attempt to attack */
521             dustfac = 0.8+0.05*Rand();
522             hit = game.kpower[l]*pow(dustfac,game.kavgd[l]);
523             game.kpower[l] *= 0.75;
524         }
525         else { /* Enemy used photon torpedo */
526             double course = 1.90985*atan2((double)game.sector.y-jay.y, (double)jay.x-game.sector.x);
527             hit = 0;
528             proutn(_("***TORPEDO INCOMING"));
529             if (!damaged(DSRSENS)) {
530                 proutn(_(" From "));
531                 crmena(0, iquad, i, jay);
532             }
533             attempt = 1;
534             prout("  ");
535             r = (Rand()+Rand())*0.5 -0.5;
536             r += 0.002*game.kpower[l]*r;
537             torpedo(course, r, jay.x, jay.y, &hit, 1, 1);
538             if (KLINGREM==0) 
539                 finish(FWON); /* Klingons did themselves in! */
540             if (game.state.galaxy[game.quadrant.x][game.quadrant.y].supernova || game.alldone) 
541                 return; /* Supernova or finished */
542             if (hit == 0) continue;
543         }
544         if (game.shldup || game.shldchg != 0 || game.condit==IHDOCKED) {
545             /* shields will take hits */
546             double absorb, hitsh, propor = pfac*game.shield*(game.condit==IHDOCKED ? 2.1 : 1.0);
547             if(propor < 0.1) propor = 0.1;
548             hitsh = propor*chgfac*hit+1.0;
549             atackd=1;
550             absorb = 0.8*hitsh;
551             if (absorb > game.shield) absorb = game.shield;
552             game.shield -= absorb;
553             hit -= hitsh;
554             if (game.condit==IHDOCKED) dock(false);
555             if (propor > 0.1 && hit < 0.005*game.energy) continue;
556         }
557         /* It's a hit -- print out hit size */
558         atackd = 1; /* We weren't going to check casualties, etc. if
559                        shields were down for some strange reason. This
560                        doesn't make any sense, so I've fixed it */
561         ihurt = 1;
562         proutn(_("%d unit hit"), (int)hit);
563         if ((damaged(DSRSENS) && itflag) || game.skill<=SKILL_FAIR) {
564             proutn(_(" on the "));
565             crmshp();
566         }
567         if (!damaged(DSRSENS) && itflag) {
568             proutn(_(" from "));
569             crmena(0, iquad, i, jay);
570         }
571         skip(1);
572         /* Decide if hit is critical */
573         if (hit > hitmax) hitmax = hit;
574         hittot += hit;
575         fry(hit);
576         game.energy -= hit;
577         if (game.condit==IHDOCKED) 
578             dock(false);
579     }
580     if (game.energy <= 0) {
581         /* Returning home upon your shield, not with it... */
582         finish(FBATTLE);
583         return;
584     }
585     if (attempt == 0 && game.condit == IHDOCKED)
586         prout(_("***Enemies decide against attacking your ship."));
587     if (atackd == 0) return;
588     percent = 100.0*pfac*game.shield+0.5;
589     if (ihurt==0) {
590         /* Shields fully protect ship */
591         proutn(_("Enemy attack reduces shield strength to "));
592     }
593     else {
594         /* Print message if starship suffered hit(s) */
595         skip(1);
596         proutn(_("Energy left %2d    shields "), (int)game.energy);
597         if (game.shldup) proutn(_("up "));
598         else if (!damaged(DSHIELD)) proutn(_("down "));
599         else proutn(_("damaged, "));
600     }
601     prout(_("%d%%,   torpedoes left %d"), percent, game.torps);
602     /* Check if anyone was hurt */
603     if (hitmax >= 200 || hittot >= 500) {
604         int icas= hittot*Rand()*0.015;
605         if (icas >= 2) {
606             skip(1);
607             prout(_("Mc Coy-  \"Sickbay to bridge.  We suffered %d casualties"), icas);
608             prout(_("   in that last attack.\""));
609             game.casual += icas;
610             game.state.crew -= 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 (damaged(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         game.state.crew -= icas;
898     }
899     skip(1);
900     prout(_("Phaser energy dispersed by shields."));
901     prout(_("Enemy unaffected."));
902     overheat(rpow);
903     return 1;
904 }
905         
906
907 void phasers(void) 
908 {
909     double hits[21], rpow=0, extra, powrem, over, temp;
910     int kz = 0, k=1, i, irec=0; /* Cheating inhibitor */
911     int ifast=0, no=0, ipoop=1, msgflag = 1;
912     enum {NOTSET, MANUAL, FORCEMAN, AUTOMATIC} automode = NOTSET;
913     int key=0;
914
915     skip(1);
916     /* SR sensors and Computer */
917     if (damaged(DSRSENS) || damaged(DCOMPTR)) ipoop = 0;
918     if (game.condit == IHDOCKED) {
919         prout(_("Phasers can't be fired through base shields."));
920         chew();
921         return;
922     }
923     if (damaged(DPHASER)) {
924         prout(_("Phaser control damaged."));
925         chew();
926         return;
927     }
928     if (game.shldup) {
929         if (damaged(DSHCTRL)) {
930             prout(_("High speed shield control damaged."));
931             chew();
932             return;
933         }
934         if (game.energy <= 200.0) {
935             prout(_("Insufficient energy to activate high-speed shield control."));
936             chew();
937             return;
938         }
939         prout(_("Weapons Officer Sulu-  \"High-speed shield control enabled, sir.\""));
940         ifast = 1;
941                 
942     }
943     /* Original code so convoluted, I re-did it all */
944     while (automode==NOTSET) {
945         key=scan();
946         if (key == IHALPHA) {
947             if (isit("manual")) {
948                 if (game.nenhere==0) {
949                     prout(_("There is no enemy present to select."));
950                     chew();
951                     key = IHEOL;
952                     automode=AUTOMATIC;
953                 }
954                 else {
955                     automode = MANUAL;
956                     key = scan();
957                 }
958             }
959             else if (isit("automatic")) {
960                 if ((!ipoop) && game.nenhere != 0) {
961                     automode = FORCEMAN;
962                 }
963                 else {
964                     if (game.nenhere==0)
965                         prout(_("Energy will be expended into space."));
966                     automode = AUTOMATIC;
967                     key = scan();
968                 }
969             }
970             else if (isit("no")) {
971                 no = 1;
972             }
973             else {
974                 huh();
975                 return;
976             }
977         }
978         else if (key == IHREAL) {
979             if (game.nenhere==0) {
980                 prout(_("Energy will be expended into space."));
981                 automode = AUTOMATIC;
982             }
983             else if (!ipoop)
984                 automode = FORCEMAN;
985             else
986                 automode = AUTOMATIC;
987         }
988         else {
989             /* IHEOL */
990             if (game.nenhere==0) {
991                 prout(_("Energy will be expended into space."));
992                 automode = AUTOMATIC;
993             }
994             else if (!ipoop)
995                 automode = FORCEMAN;
996             else 
997                 proutn(_("Manual or automatic? "));
998         }
999     }
1000                                 
1001     switch (automode) {
1002     case AUTOMATIC:
1003         if (key == IHALPHA && isit("no")) {
1004             no = 1;
1005             key = scan();
1006         }
1007         if (key != IHREAL && game.nenhere != 0) {
1008             prout(_("Phasers locked on target. Energy available: %.2f"),
1009                   ifast?game.energy-200.0:game.energy,1,2);
1010         }
1011         irec=0;
1012         do {
1013             chew();
1014             if (!kz) for_local_enemies(i)
1015                 irec+=fabs(game.kpower[i])/(PHASEFAC*pow(0.90,game.kdist[i]))*
1016                     (1.01+0.05*Rand()) + 1.0;
1017             kz=1;
1018             proutn(_("(%d) units required. "), irec);
1019             chew();
1020             proutn(_("Units to fire= "));
1021             key = scan();
1022             if (key!=IHREAL) return;
1023             rpow = aaitem;
1024             if (rpow > (ifast?game.energy-200:game.energy)) {
1025                 proutn(_("Energy available= %.2f"),
1026                        ifast?game.energy-200:game.energy);
1027                 skip(1);
1028                 key = IHEOL;
1029             }
1030         } while (rpow > (ifast?game.energy-200:game.energy));
1031         if (rpow<=0) {
1032             /* chicken out */
1033             chew();
1034             return;
1035         }
1036         if ((key=scan()) == IHALPHA && isit("no")) {
1037             no = 1;
1038         }
1039         if (ifast) {
1040             game.energy -= 200; /* Go and do it! */
1041             if (checkshctrl(rpow)) return;
1042         }
1043         chew();
1044         game.energy -= rpow;
1045         extra = rpow;
1046         if (game.nenhere) {
1047             extra = 0.0;
1048             powrem = rpow;
1049             for_local_enemies(i) {
1050                 hits[i] = 0.0;
1051                 if (powrem <= 0) continue;
1052                 hits[i] = fabs(game.kpower[i])/(PHASEFAC*pow(0.90,game.kdist[i]));
1053                 over = (0.01 + 0.05*Rand())*hits[i];
1054                 temp = powrem;
1055                 powrem -= hits[i] + over;
1056                 if (powrem <= 0 && temp < hits[i]) hits[i] = temp;
1057                 if (powrem <= 0) over = 0.0;
1058                 extra += over;
1059             }
1060             if (powrem > 0.0) extra += powrem;
1061             hittem(hits);
1062             game.ididit=1;
1063         }
1064         if (extra > 0 && game.alldone == 0) {
1065             if (game.ithere) {
1066                 proutn(_("*** Tholian web absorbs "));
1067                 if (game.nenhere>0) proutn(_("excess "));
1068                 prout(_("phaser energy."));
1069             }
1070             else {
1071                 prout(_("%d expended on empty space."), (int)extra);
1072             }
1073         }
1074         break;
1075
1076     case FORCEMAN:
1077         chew();
1078         key = IHEOL;
1079         if (damaged(DCOMPTR))
1080             prout(_("Battle comuter damaged, manual file only."));
1081         else {
1082             skip(1);
1083             prouts(_("---WORKING---"));
1084             skip(1);
1085             prout(_("Short-range-sensors-damaged"));
1086             prout(_("Insufficient-data-for-automatic-phaser-fire"));
1087             prout(_("Manual-fire-must-be-used"));
1088             skip(1);
1089         }
1090     case MANUAL:
1091         rpow = 0.0;
1092         for (k = 1; k <= game.nenhere;) {
1093             coord aim = game.ks[k];
1094             int ienm = game.quad[aim.x][aim.y];
1095             if (msgflag) {
1096                 proutn(_("Energy available= %.2f"),
1097                        game.energy-.006-(ifast?200:0));
1098                 skip(1);
1099                 msgflag = 0;
1100                 rpow = 0.0;
1101             }
1102             if (damaged(DSRSENS) && !(abs(game.sector.x-aim.x) < 2 && abs(game.sector.y-aim.y) < 2) &&
1103                 (ienm == IHC || ienm == IHS)) {
1104                 cramen(ienm);
1105                 prout(_(" can't be located without short range scan."));
1106                 chew();
1107                 key = IHEOL;
1108                 hits[k] = 0; /* prevent overflow -- thanks to Alexei Voitenko */
1109                 k++;
1110                 continue;
1111             }
1112             if (key == IHEOL) {
1113                 chew();
1114                 if (ipoop && k > kz)
1115                     irec=(fabs(game.kpower[k])/(PHASEFAC*pow(0.9,game.kdist[k])))*
1116                         (1.01+0.05*Rand()) + 1.0;
1117                 kz = k;
1118                 proutn("(");
1119                 if (!damaged(DCOMPTR)) proutn("%d", irec);
1120                 else proutn("??");
1121                 proutn(")  ");
1122                 proutn(_("units to fire at "));
1123                 crmena(0, ienm, 2, aim);
1124                 proutn("-  ");
1125                 key = scan();
1126             }
1127             if (key == IHALPHA && isit("no")) {
1128                 no = 1;
1129                 key = scan();
1130                 continue;
1131             }
1132             if (key == IHALPHA) {
1133                 huh();
1134                 return;
1135             }
1136             if (key == IHEOL) {
1137                 if (k==1) { /* Let me say I'm baffled by this */
1138                     msgflag = 1;
1139                 }
1140                 continue;
1141             }
1142             if (aaitem < 0) {
1143                 /* abort out */
1144                 chew();
1145                 return;
1146             }
1147             hits[k] = aaitem;
1148             rpow += aaitem;
1149             /* If total requested is too much, inform and start over */
1150                                 
1151             if (rpow > (ifast?game.energy-200:game.energy)) {
1152                 prout(_("Available energy exceeded -- try again."));
1153                 chew();
1154                 return;
1155             }
1156             key = scan(); /* scan for next value */
1157             k++;
1158         }
1159         if (rpow == 0.0) {
1160             /* zero energy -- abort */
1161             chew();
1162             return;
1163         }
1164         if (key == IHALPHA && isit("no")) {
1165             no = 1;
1166         }
1167         game.energy -= rpow;
1168         chew();
1169         if (ifast) {
1170             game.energy -= 200.0;
1171             if (checkshctrl(rpow)) return;
1172         }
1173         hittem(hits);
1174         game.ididit=1;
1175     case NOTSET:;       /* avoid gcc warning */
1176     }
1177     /* Say shield raised or malfunction, if necessary */
1178     if (game.alldone) 
1179         return;
1180     if (ifast) {
1181         skip(1);
1182         if (no == 0) {
1183             if (Rand() >= 0.99) {
1184                 prout(_("Sulu-  \"Sir, the high-speed shield control has malfunctioned . . ."));
1185                 prouts(_("         CLICK   CLICK   POP  . . ."));
1186                 prout(_(" No response, sir!"));
1187                 game.shldup = false;
1188             }
1189             else
1190                 prout(_("Shields raised."));
1191         }
1192         else
1193             game.shldup = false;
1194     }
1195     overheat(rpow);
1196 }
1197
1198 void hittem(double *hits) 
1199 {
1200     double kp, kpow, wham, hit, dustfac, kpini;
1201     int nenhr2=game.nenhere, k=1, kk=1, ienm;
1202     coord w;
1203
1204     skip(1);
1205
1206     for (; k <= nenhr2; k++, kk++) {
1207         if ((wham = hits[k])==0) continue;
1208         dustfac = 0.9 + 0.01*Rand();
1209         hit = wham*pow(dustfac,game.kdist[kk]);
1210         kpini = game.kpower[kk];
1211         kp = fabs(kpini);
1212         if (PHASEFAC*hit < kp) kp = PHASEFAC*hit;
1213         game.kpower[kk] -= (game.kpower[kk] < 0 ? -kp: kp);
1214         kpow = game.kpower[kk];
1215         w = game.ks[kk];
1216         if (hit > 0.005) {
1217             if (!damaged(DSRSENS))
1218                 boom(w.x, w.y);
1219             proutn(_("%d unit hit on "), (int)hit);
1220         }
1221         else
1222             proutn(_("Very small hit on "));
1223         ienm = game.quad[w.x][w.y];
1224         if (ienm==IHQUEST) iqengry=1;
1225         crmena(0,ienm,2,w);
1226         skip(1);
1227         if (kpow == 0) {
1228             deadkl(w, ienm, w.x, w.y);
1229             if (KLINGREM==0) finish(FWON);
1230             if (game.alldone) return;
1231             kk--; /* don't do the increment */
1232         }
1233         else /* decide whether or not to emasculate klingon */
1234             if (kpow > 0 && Rand() >= 0.9 &&
1235                 kpow <= ((0.4 + 0.4*Rand())*kpini)) {
1236                 prout(_("***Mr. Spock-  \"Captain, the vessel at "),
1237                       cramlc(sector, w));
1238                 prout(_("   has just lost its firepower.\""));
1239                 game.kpower[kk] = -kpow;
1240             }
1241     }
1242     return;
1243 }
1244