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