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