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