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