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