c0dac65b0c2f3b2cce110b5053f470fbe9e48021
[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     if (idebug) prout("=== ATTACK!");
473
474     if (game.ithere) movetho();
475
476     if (game.neutz) { /* The one chance not to be attacked */
477         game.neutz = 0;
478         return;
479     }
480     if ((((game.comhere || game.ishere) && (game.justin == 0)) || game.skill == SKILL_EMERITUS)&&(torps_ok!=0)) movcom();
481     if (game.nenhere==0 || (game.nenhere==1 && iqhere && iqengry==0)) return;
482     pfac = 1.0/game.inshld;
483     if (game.shldchg == 1) chgfac = 0.25+0.5*Rand();
484     skip(1);
485     if (game.skill <= SKILL_FAIR) i = 2;
486     for_local_enemies(l) {
487         if (game.kpower[l] < 0) continue;       /* too weak to attack */
488         /* compute hit strength and diminsh shield power */
489         r = Rand();
490         /* Increase chance of photon torpedos if docked or enemy energy low */
491         if (game.condit == IHDOCKED) r *= 0.25;
492         if (game.kpower[l] < 500) r *= 0.25; 
493         jay.x = game.ks[l].x;
494         jay.y = game.ks[l].y;
495         iquad = game.quad[jay.x][jay.y];
496         if (iquad==IHT || (iquad==IHQUEST && !iqengry)) continue;
497         itflag = (iquad == IHK && r > 0.0005) || !torps_ok ||
498             (iquad==IHC && r > 0.015) ||
499             (iquad==IHR && r > 0.3) ||
500             (iquad==IHS && r > 0.07) ||
501             (iquad==IHQUEST && r > 0.05);
502         if (itflag) {
503             /* Enemy uses phasers */
504             if (game.condit == IHDOCKED) continue; /* Don't waste the effort! */
505             attempt = 1; /* Attempt to attack */
506             dustfac = 0.8+0.05*Rand();
507             hit = game.kpower[l]*pow(dustfac,game.kavgd[l]);
508             game.kpower[l] *= 0.75;
509         }
510         else { /* Enemy used photon torpedo */
511             double course = 1.90985*atan2((double)game.sector.y-jay.y, (double)jay.x-game.sector.x);
512             hit = 0;
513             proutn(_("***TORPEDO INCOMING"));
514             if (game.damage[DSRSENS] <= 0.0) {
515                 proutn(_(" From "));
516                 crmena(0, iquad, i, jay);
517             }
518             attempt = 1;
519             prout("  ");
520             r = (Rand()+Rand())*0.5 -0.5;
521             r += 0.002*game.kpower[l]*r;
522             torpedo(course, r, jay.x, jay.y, &hit, 1, 1);
523             if (KLINGREM==0) 
524                 finish(FWON); /* Klingons did themselves in! */
525             if (game.state.galaxy[game.quadrant.x][game.quadrant.y].supernova || game.alldone) 
526                 return; /* Supernova or finished */
527             if (hit == 0) continue;
528         }
529         if (game.shldup != 0 || game.shldchg != 0 || game.condit==IHDOCKED) {
530             /* shields will take hits */
531             double absorb, hitsh, propor = pfac*game.shield*(game.condit==IHDOCKED ? 2.1 : 1.0);
532             if(propor < 0.1) propor = 0.1;
533             hitsh = propor*chgfac*hit+1.0;
534             atackd=1;
535             absorb = 0.8*hitsh;
536             if (absorb > game.shield) absorb = game.shield;
537             game.shield -= absorb;
538             hit -= hitsh;
539             if (game.condit==IHDOCKED) dock(0);
540             if (propor > 0.1 && hit < 0.005*game.energy) continue;
541         }
542         /* It's a hit -- print out hit size */
543         atackd = 1; /* We weren't going to check casualties, etc. if
544                        shields were down for some strange reason. This
545                        doesn't make any sense, so I've fw.xed it */
546         ihurt = 1;
547         proutn(_("%d unit hit"), (int)hit);
548         if ((game.damage[DSRSENS] > 0 && itflag) || game.skill<=SKILL_FAIR) {
549             proutn(_(" on the "));
550             crmshp();
551         }
552         if (game.damage[DSRSENS] <= 0.0 && itflag) {
553             proutn(_(" from "));
554             crmena(0, iquad, i, jay);
555         }
556         skip(1);
557         /* Decide if hit is critical */
558         if (hit > hitmax) hitmax = hit;
559         hittot += hit;
560         fry(hit);
561         game.energy -= hit;
562         if (game.condit==IHDOCKED) 
563             dock(0);
564     }
565     if (game.energy <= 0) {
566         /* Returning home upon your shield, not with it... */
567         finish(FBATTLE);
568         return;
569     }
570     if (attempt == 0 && game.condit == IHDOCKED)
571         prout(_("***Enemies decide against attacking your ship."));
572     if (atackd == 0) return;
573     percent = 100.0*pfac*game.shield+0.5;
574     if (ihurt==0) {
575         /* Shields fully protect ship */
576         proutn(_("Enemy attack reduces shield strength to "));
577     }
578     else {
579         /* Print message if starship suffered hit(s) */
580         skip(1);
581         proutn(_("Energy left %2d    shields "), (int)game.energy);
582         if (game.shldup) proutn(_("up "));
583         else if (game.damage[DSHIELD] == 0) proutn(_("down "));
584         else proutn(_("damaged, "));
585     }
586     prout(_("%d%%,   torpedoes left %d"), percent, game.torps);
587     /* Check if anyone was hurt */
588     if (hitmax >= 200 || hittot >= 500) {
589         int icas= hittot*Rand()*0.015;
590         if (icas >= 2) {
591             skip(1);
592             prout(_("Mc Coy-  \"Sickbay to bridge.  We suffered %d casualties"), icas);
593             prout(_("   in that last attack.\""));
594             game.casual += icas;
595         }
596     }
597     /* After attack, reset average distance to enemies */
598     for_local_enemies(l)
599         game.kavgd[l] = game.kdist[l];
600     sortkl();
601     return;
602 }
603                 
604 void deadkl(coord w, int type, int ixx, int iyy) 
605 {
606     /* Added ixx and iyy allow enemy to "move" before dying */
607     coord mv;
608     int i,j;
609
610     mv.x = ixx; mv.y = iyy;
611     skip(1);
612     crmena(1, type, 2, mv);
613     /* Decide what kind of enemy it is and update approriately */
614     if (type == IHR) {
615         /* chalk up a Romulan */
616         game.state.galaxy[game.quadrant.x][game.quadrant.y].romulans--;
617         game.irhere--;
618         game.state.nromrem--;
619     }
620     else if (type == IHT) {
621         /* Killed a Tholian */
622         game.ithere = 0;
623     }
624     else if (type == IHQUEST) {
625         /* Killed a Thingy */
626         iqhere=iqengry=thing.x=thing.y=0;
627     }
628     else {
629         /* Some type of a Klingon */
630         game.state.galaxy[game.quadrant.x][game.quadrant.y].klingons--;
631         game.klhere--;
632         switch (type) {
633         case IHC:
634             game.comhere = 0;
635             for_commanders (i)
636                 if (game.state.kcmdr[i].x==game.quadrant.x && game.state.kcmdr[i].y==game.quadrant.y) break;
637             game.state.kcmdr[i] = game.state.kcmdr[game.state.remcom];
638             game.state.kcmdr[game.state.remcom].x = 0;
639             game.state.kcmdr[game.state.remcom].y = 0;
640             game.state.remcom--;
641             unschedule(FTBEAM);
642             if (game.state.remcom != 0)
643                 schedule(FTBEAM, expran(1.0*game.incom/game.state.remcom));
644             break;
645         case IHK:
646             game.state.remkl--;
647             break;
648         case IHS:
649             game.state.nscrem--;
650             game.ishere = game.state.kscmdr.x = game.state.kscmdr.y = game.isatb = game.iscate = 0;
651             unschedule(FSCMOVE);
652             unschedule(FSCDBAS);
653             break;
654         }
655     }
656
657     /* For each kind of enemy, finish message to player */
658     prout(_(" destroyed."));
659     game.quad[w.x][w.y] = IHDOT;
660     if (KLINGREM==0) return;
661
662     game.state.remtime = game.state.remres/(game.state.remkl + 4*game.state.remcom);
663
664     /* Remove enemy ship from arrays describing local game.conditions */
665     if (is_scheduled(FCDBAS) && game.battle.x==game.quadrant.x && game.battle.y==game.quadrant.y && type==IHC)
666         unschedule(FCDBAS);
667     for_local_enemies(i)
668         if (same(game.ks[i], w)) break;
669     game.nenhere--;
670     if (i <= game.nenhere)  {
671         for (j=i; j<=game.nenhere; j++) {
672             game.ks[j] = game.ks[j+1];
673             game.kpower[j] = game.kpower[j+1];
674             game.kavgd[j] = game.kdist[j] = game.kdist[j+1];
675         }
676     }
677     game.ks[game.nenhere+1].x = 0;
678     game.ks[game.nenhere+1].x = 0;
679     game.kdist[game.nenhere+1] = 0;
680     game.kavgd[game.nenhere+1] = 0;
681     game.kpower[game.nenhere+1] = 0;
682     return;
683 }
684
685 static bool targetcheck(double x, double y, double *course) 
686 {
687     double deltx, delty;
688     /* Return true if target is invalid */
689     if (!VALID_SECTOR(x, y)) {
690         huh();
691         return true;
692     }
693     deltx = 0.1*(y - game.sector.y);
694     delty = 0.1*(game.sector.x - x);
695     if (deltx==0 && delty== 0) {
696         skip(1);
697         prout(_("Spock-  \"Bridge to sickbay.  Dr. McCoy,"));
698         prout(_("  I recommend an immediate review of"));
699         prout(_("  the Captain's psychological profile.\""));
700         chew();
701         return true;
702     }
703     *course = 1.90985932*atan2(deltx, delty);
704     return false;
705 }
706
707 void photon(void) 
708 {
709     double targ[4][3], course[4];
710     double r, dummy;
711     int key, n, i, osuabor;
712
713     game.ididit = 0;
714
715     if (game.damage[DPHOTON]) {
716         prout(_("Photon tubes damaged."));
717         chew();
718         return;
719     }
720     if (game.torps == 0) {
721         prout(_("No torpedoes left."));
722         chew();
723         return;
724     }
725     key = scan();
726     for (;;) {
727         if (key == IHALPHA) {
728             huh();
729             return;
730         }
731         else if (key == IHEOL) {
732             prout(_("%d torpedoes left."), game.torps);
733             proutn(_("Number of torpedoes to fire- "));
734             key = scan();
735         }
736         else /* key == IHREAL */ {
737             n = aaitem + 0.5;
738             if (n <= 0) { /* abort command */
739                 chew();
740                 return;
741             }
742             if (n > 3) {
743                 chew();
744                 prout(_("Maximum of 3 torpedoes per burst."));
745                 key = IHEOL;
746                 return;
747             }
748             if (n <= game.torps) break;
749             chew();
750             key = IHEOL;
751         }
752     }
753     for (i = 1; i <= n; i++) {
754         key = scan();
755         if (i==1 && key == IHEOL) {
756             break;      /* we will try prompting */
757         }
758         if (i==2 && key == IHEOL) {
759             /* direct all torpedoes at one target */
760             while (i <= n) {
761                 targ[i][1] = targ[1][1];
762                 targ[i][2] = targ[1][2];
763                 course[i] = course[1];
764                 i++;
765             }
766             break;
767         }
768         if (key != IHREAL) {
769             huh();
770             return;
771         }
772         targ[i][1] = aaitem;
773         key = scan();
774         if (key != IHREAL) {
775             huh();
776             return;
777         }
778         targ[i][2] = aaitem;
779         if (targetcheck(targ[i][1], targ[i][2], &course[i])) return;
780     }
781     chew();
782     if (i == 1 && key == IHEOL) {
783         /* prompt for each one */
784         for (i = 1; i <= n; i++) {
785             proutn(_("Target sector for torpedo number %d- "), i);
786             key = scan();
787             if (key != IHREAL) {
788                 huh();
789                 return;
790             }
791             targ[i][1] = aaitem;
792             key = scan();
793             if (key != IHREAL) {
794                 huh();
795                 return;
796             }
797             targ[i][2] = aaitem;
798             chew();
799             if (targetcheck(targ[i][1], targ[i][2], &course[i])) return;
800         }
801     }
802     game.ididit = 1;
803     /* Loop for moving <n> torpedoes */
804     osuabor = 0;
805     for (i = 1; i <= n && !osuabor; i++) {
806         if (game.condit != IHDOCKED) game.torps--;
807         r = (Rand()+Rand())*0.5 -0.5;
808         if (fabs(r) >= 0.47) {
809             /* misfire! */
810             r = (Rand()+1.2) * r;
811             if (n>1) {
812                 prouts(_("***TORPEDO NUMBER %d MISFIRES"), i);
813             }
814             else prouts(_("***TORPEDO MISFIRES."));
815             skip(1);
816             if (i < n)
817                 prout(_("  Remainder of burst aborted."));
818             osuabor=1;
819             if (Rand() <= 0.2) {
820                 prout(_("***Photon tubes damaged by misfire."));
821                 game.damage[DPHOTON] = game.damfac*(1.0+2.0*Rand());
822                 break;
823             }
824         }
825         if (game.shldup || game.condit == IHDOCKED) 
826             r *= 1.0 + 0.0001*game.shield;
827         torpedo(course[i], r, game.sector.x, game.sector.y, &dummy, i, n);
828         if (game.alldone || game.state.galaxy[game.quadrant.x][game.quadrant.y].supernova)
829             return;
830     }
831     if (KLINGREM==0) finish(FWON);
832 }
833
834         
835
836 static void overheat(double rpow) 
837 {
838     if (rpow > 1500) {
839         double chekbrn = (rpow-1500.)*0.00038;
840         if (Rand() <= chekbrn) {
841             prout(_("Weapons officer Sulu-  \"Phasers overheated, sir.\""));
842             game.damage[DPHASER] = game.damfac*(1.0 + Rand()) * (1.0+chekbrn);
843         }
844     }
845 }
846
847 static int checkshctrl(double rpow) 
848 {
849     double hit;
850     int icas;
851         
852     skip(1);
853     if (Rand() < .998) {
854         prout(_("Shields lowered."));
855         return 0;
856     }
857     /* Something bad has happened */
858     prouts(_("***RED ALERT!  RED ALERT!"));
859     skip(2);
860     hit = rpow*game.shield/game.inshld;
861     game.energy -= rpow+hit*0.8;
862     game.shield -= hit*0.2;
863     if (game.energy <= 0.0) {
864         prouts(_("Sulu-  \"Captain! Shield malf***********************\""));
865         skip(1);
866         stars();
867         finish(FPHASER);
868         return 1;
869     }
870     prouts(_("Sulu-  \"Captain! Shield malfunction! Phaser fire contained!\""));
871     skip(2);
872     prout(_("Lt. Uhura-  \"Sir, all decks reporting damage.\""));
873     icas = hit*Rand()*0.012;
874     skip(1);
875     fry(0.8*hit);
876     if (icas) {
877         skip(1);
878         prout(_("McCoy to bridge- \"Severe radiation burns, Jim."));
879         prout(_("  %d casualties so far.\""), icas);
880         game.casual -= icas;
881     }
882     skip(1);
883     prout(_("Phaser energy dispersed by shields."));
884     prout(_("Enemy unaffected."));
885     overheat(rpow);
886     return 1;
887 }
888         
889
890 void phasers(void) 
891 {
892     double hits[21], rpow=0, extra, powrem, over, temp;
893     int kz = 0, k=1, i, irec=0; /* Cheating inhibitor */
894     int ifast=0, no=0, ipoop=1, msgflag = 1;
895     enum {NOTSET, MANUAL, FORCEMAN, AUTOMATIC} automode = NOTSET;
896     int key=0;
897
898     skip(1);
899     /* SR sensors and Computer */
900     if (game.damage[DSRSENS]+game.damage[DCOMPTR] > 0) ipoop = 0;
901     if (game.condit == IHDOCKED) {
902         prout(_("Phasers can't be fired through base shields."));
903         chew();
904         return;
905     }
906     if (game.damage[DPHASER] != 0) {
907         prout(_("Phaser control damaged."));
908         chew();
909         return;
910     }
911     if (game.shldup) {
912         if (game.damage[DSHCTRL]) {
913             prout(_("High speed shield control damaged."));
914             chew();
915             return;
916         }
917         if (game.energy <= 200.0) {
918             prout(_("Insufficient energy to activate high-speed shield control."));
919             chew();
920             return;
921         }
922         prout(_("Weapons Officer Sulu-  \"High-speed shield control enabled, sir.\""));
923         ifast = 1;
924                 
925     }
926     /* Original code so convoluted, I re-did it all */
927     while (automode==NOTSET) {
928         key=scan();
929         if (key == IHALPHA) {
930             if (isit("manual")) {
931                 if (game.nenhere==0) {
932                     prout(_("There is no enemy present to select."));
933                     chew();
934                     key = IHEOL;
935                     automode=AUTOMATIC;
936                 }
937                 else {
938                     automode = MANUAL;
939                     key = scan();
940                 }
941             }
942             else if (isit("automatic")) {
943                 if ((!ipoop) && game.nenhere != 0) {
944                     automode = FORCEMAN;
945                 }
946                 else {
947                     if (game.nenhere==0)
948                         prout(_("Energy will be expended into space."));
949                     automode = AUTOMATIC;
950                     key = scan();
951                 }
952             }
953             else if (isit("no")) {
954                 no = 1;
955             }
956             else {
957                 huh();
958                 return;
959             }
960         }
961         else if (key == IHREAL) {
962             if (game.nenhere==0) {
963                 prout(_("Energy will be expended into space."));
964                 automode = AUTOMATIC;
965             }
966             else if (!ipoop)
967                 automode = FORCEMAN;
968             else
969                 automode = AUTOMATIC;
970         }
971         else {
972             /* IHEOL */
973             if (game.nenhere==0) {
974                 prout(_("Energy will be expended into space."));
975                 automode = AUTOMATIC;
976             }
977             else if (!ipoop)
978                 automode = FORCEMAN;
979             else 
980                 proutn(_("Manual or automatic? "));
981         }
982     }
983                                 
984     switch (automode) {
985     case AUTOMATIC:
986         if (key == IHALPHA && isit("no")) {
987             no = 1;
988             key = scan();
989         }
990         if (key != IHREAL && game.nenhere != 0) {
991             prout(_("Phasers locked on target. Energy available: %.2f"),
992                   ifast?game.energy-200.0:game.energy,1,2);
993         }
994         irec=0;
995         do {
996             chew();
997             if (!kz) for_local_enemies(i)
998                 irec+=fabs(game.kpower[i])/(PHASEFAC*pow(0.90,game.kdist[i]))*
999                     (1.01+0.05*Rand()) + 1.0;
1000             kz=1;
1001             proutn(_("(%d) units required. "), irec);
1002             chew();
1003             proutn(_("Units to fire= "));
1004             key = scan();
1005             if (key!=IHREAL) return;
1006             rpow = aaitem;
1007             if (rpow > (ifast?game.energy-200:game.energy)) {
1008                 proutn(_("Energy available= %.2f"),
1009                        ifast?game.energy-200:game.energy);
1010                 skip(1);
1011                 key = IHEOL;
1012             }
1013         } while (rpow > (ifast?game.energy-200:game.energy));
1014         if (rpow<=0) {
1015             /* chicken out */
1016             chew();
1017             return;
1018         }
1019         if ((key=scan()) == IHALPHA && isit("no")) {
1020             no = 1;
1021         }
1022         if (ifast) {
1023             game.energy -= 200; /* Go and do it! */
1024             if (checkshctrl(rpow)) return;
1025         }
1026         chew();
1027         game.energy -= rpow;
1028         extra = rpow;
1029         if (game.nenhere) {
1030             extra = 0.0;
1031             powrem = rpow;
1032             for_local_enemies(i) {
1033                 hits[i] = 0.0;
1034                 if (powrem <= 0) continue;
1035                 hits[i] = fabs(game.kpower[i])/(PHASEFAC*pow(0.90,game.kdist[i]));
1036                 over = (0.01 + 0.05*Rand())*hits[i];
1037                 temp = powrem;
1038                 powrem -= hits[i] + over;
1039                 if (powrem <= 0 && temp < hits[i]) hits[i] = temp;
1040                 if (powrem <= 0) over = 0.0;
1041                 extra += over;
1042             }
1043             if (powrem > 0.0) extra += powrem;
1044             hittem(hits);
1045             game.ididit=1;
1046         }
1047         if (extra > 0 && game.alldone == 0) {
1048             if (game.ithere) {
1049                 proutn(_("*** Tholian web absorbs "));
1050                 if (game.nenhere>0) proutn(_("excess "));
1051                 prout(_("phaser energy."));
1052             }
1053             else {
1054                 prout(_("%d expended on empty space."), (int)extra);
1055             }
1056         }
1057         break;
1058
1059     case FORCEMAN:
1060         chew();
1061         key = IHEOL;
1062         if (game.damage[DCOMPTR]!=0)
1063             prout(_("Battle comuter damaged, manual file only."));
1064         else {
1065             skip(1);
1066             prouts(_("---WORKING---"));
1067             skip(1);
1068             prout(_("Short-range-sensors-damaged"));
1069             prout(_("Insufficient-data-for-automatic-phaser-fire"));
1070             prout(_("Manual-fire-must-be-used"));
1071             skip(1);
1072         }
1073     case MANUAL:
1074         rpow = 0.0;
1075         for (k = 1; k <= game.nenhere;) {
1076             coord aim = game.ks[k];
1077             int ienm = game.quad[aim.x][aim.y];
1078             if (msgflag) {
1079                 proutn(_("Energy available= %.2f"),
1080                        game.energy-.006-(ifast?200:0));
1081                 skip(1);
1082                 msgflag = 0;
1083                 rpow = 0.0;
1084             }
1085             if (game.damage[DSRSENS] && !(abs(game.sector.x-aim.x) < 2 && abs(game.sector.y-aim.y) < 2) &&
1086                 (ienm == IHC || ienm == IHS)) {
1087                 cramen(ienm);
1088                 prout(_(" can't be located without short range scan."));
1089                 chew();
1090                 key = IHEOL;
1091                 hits[k] = 0; /* prevent overflow -- thanks to Alexei Voitenko */
1092                 k++;
1093                 continue;
1094             }
1095             if (key == IHEOL) {
1096                 chew();
1097                 if (ipoop && k > kz)
1098                     irec=(fabs(game.kpower[k])/(PHASEFAC*pow(0.9,game.kdist[k])))*
1099                         (1.01+0.05*Rand()) + 1.0;
1100                 kz = k;
1101                 proutn("(");
1102                 if (game.damage[DCOMPTR]==0) proutn("%d", irec);
1103                 else proutn("??");
1104                 proutn(")  ");
1105                 proutn(_("units to fire at "));
1106                 crmena(0, ienm, 2, aim);
1107                 proutn("-  ");
1108                 key = scan();
1109             }
1110             if (key == IHALPHA && isit("no")) {
1111                 no = 1;
1112                 key = scan();
1113                 continue;
1114             }
1115             if (key == IHALPHA) {
1116                 huh();
1117                 return;
1118             }
1119             if (key == IHEOL) {
1120                 if (k==1) { /* Let me say I'm baffled by this */
1121                     msgflag = 1;
1122                 }
1123                 continue;
1124             }
1125             if (aaitem < 0) {
1126                 /* abort out */
1127                 chew();
1128                 return;
1129             }
1130             hits[k] = aaitem;
1131             rpow += aaitem;
1132             /* If total requested is too much, inform and start over */
1133                                 
1134             if (rpow > (ifast?game.energy-200:game.energy)) {
1135                 prout(_("Available energy exceeded -- try again."));
1136                 chew();
1137                 return;
1138             }
1139             key = scan(); /* scan for next value */
1140             k++;
1141         }
1142         if (rpow == 0.0) {
1143             /* zero energy -- abort */
1144             chew();
1145             return;
1146         }
1147         if (key == IHALPHA && isit("no")) {
1148             no = 1;
1149         }
1150         game.energy -= rpow;
1151         chew();
1152         if (ifast) {
1153             game.energy -= 200.0;
1154             if (checkshctrl(rpow)) return;
1155         }
1156         hittem(hits);
1157         game.ididit=1;
1158     case NOTSET:;       /* avoid gcc warning */
1159     }
1160     /* Say shield raised or malfunction, if necessary */
1161     if (game.alldone) 
1162         return;
1163     if (ifast) {
1164         skip(1);
1165         if (no == 0) {
1166             if (Rand() >= 0.99) {
1167                 prout(_("Sulu-  \"Sir, the high-speed shield control has malfunctioned . . ."));
1168                 prouts(_("         CLICK   CLICK   POP  . . ."));
1169                 prout(_(" No response, sir!"));
1170                 game.shldup = 0;
1171             }
1172             else
1173                 prout(_("Shields raised."));
1174         }
1175         else
1176             game.shldup = 0;
1177     }
1178     overheat(rpow);
1179 }
1180
1181 void hittem(double *hits) 
1182 {
1183     double kp, kpow, wham, hit, dustfac, kpini;
1184     int nenhr2=game.nenhere, k=1, kk=1, ienm;
1185     coord w;
1186
1187     skip(1);
1188
1189     for (; k <= nenhr2; k++, kk++) {
1190         if ((wham = hits[k])==0) continue;
1191         dustfac = 0.9 + 0.01*Rand();
1192         hit = wham*pow(dustfac,game.kdist[kk]);
1193         kpini = game.kpower[kk];
1194         kp = fabs(kpini);
1195         if (PHASEFAC*hit < kp) kp = PHASEFAC*hit;
1196         game.kpower[kk] -= (game.kpower[kk] < 0 ? -kp: kp);
1197         kpow = game.kpower[kk];
1198         w = game.ks[kk];
1199         if (hit > 0.005) {
1200             if (game.damage[DSRSENS]==0)
1201                 boom(w.x, w.y);
1202             proutn(_("%d unit hit on "), (int)hit);
1203         }
1204         else
1205             proutn(_("Very small hit on "));
1206         ienm = game.quad[w.x][w.y];
1207         if (ienm==IHQUEST) iqengry=1;
1208         crmena(0,ienm,2,w);
1209         skip(1);
1210         if (kpow == 0) {
1211             deadkl(w, ienm, w.x, w.y);
1212             if (KLINGREM==0) finish(FWON);
1213             if (game.alldone) return;
1214             kk--; /* don't do the increment */
1215         }
1216         else /* decide whether or not to emasculate klingon */
1217             if (kpow > 0 && Rand() >= 0.9 &&
1218                 kpow <= ((0.4 + 0.4*Rand())*kpini)) {
1219                 prout(_("***Mr. Spock-  \"Captain, the vessel at "),
1220                       cramlc(sector, w));
1221                 prout(_("   has just lost its firepower.\""));
1222                 game.kpower[kk] = -kpow;
1223             }
1224     }
1225     return;
1226 }
1227