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