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