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