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