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