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