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