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