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