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