59d2d9340b23791e033804244024156fb0fff526
[super-star-trek.git] / src / battle.c
1 #include "sst.h"
2
3 void doshield(bool raise) 
4 /* change shield status */
5 {
6     int key;
7     enum {NONE, SHUP, SHDN, NRG} action = NONE;
8
9     game.ididit = false;
10
11     if (raise) 
12         action = SHUP;
13     else {
14         key = scan();
15         if (key == IHALPHA) {
16             if (isit("transfer"))
17                 action = NRG;
18             else {
19                 chew();
20                 if (damaged(DSHIELD)) {
21                     prout(_("Shields damaged and down."));
22                     return;
23                 }
24                 if (isit("up"))
25                     action = SHUP;
26                 else if (isit("down"))
27                     action = SHDN;
28             }
29         }
30         if (action==NONE) {
31             proutn(_("Do you wish to change shield energy? "));
32             if (ja() == true) {
33                 proutn(_("Energy to transfer to shields- "));
34                 action = NRG;
35             }
36             else if (damaged(DSHIELD)) {
37                 prout(_("Shields damaged and down."));
38                 return;
39             }
40             else if (game.shldup) {
41                 proutn(_("Shields are up. Do you want them down? "));
42                 if (ja() == true) action = SHDN;
43                 else {
44                     chew();
45                     return;
46                 }
47             }
48             else {
49                 proutn(_("Shields are down. Do you want them up? "));
50                 if (ja() == true) action = SHUP;
51                 else {
52                     chew();
53                     return;
54                 }
55             }
56         }
57     }
58     switch (action) {
59     case SHUP: /* raise shields */
60         if (game.shldup) {
61             prout(_("Shields already up."));
62             return;
63         }
64         game.shldup = true;
65         game.shldchg = 1;
66         if (game.condition != docked) game.energy -= 50.0;
67         prout(_("Shields raised."));
68         if (game.energy <= 0) {
69             skip(1);
70             prout(_("Shields raising uses up last of energy."));
71             finish(FNRG);
72             return;
73         }
74         game.ididit=true;
75         return;
76     case SHDN:
77         if (!game.shldup) {
78             prout(_("Shields already down."));
79             return;
80         }
81         game.shldup=false;
82         game.shldchg=1;
83         prout(_("Shields lowered."));
84         game.ididit = true;
85         return;
86     case NRG:
87         while (scan() != IHREAL) {
88             chew();
89             proutn(_("Energy to transfer to shields- "));
90         }
91         chew();
92         if (aaitem==0) return;
93         if (aaitem > game.energy) {
94             prout(_("Insufficient ship energy."));
95             return;
96         }
97         game.ididit = true;
98         if (game.shield+aaitem >= game.inshld) {
99             prout(_("Shield energy maximized."));
100             if (game.shield+aaitem > game.inshld) {
101                 prout(_("Excess energy requested returned to ship energy"));
102             }
103             game.energy -= game.inshld-game.shield;
104             game.shield = game.inshld;
105             return;
106         }
107         if (aaitem < 0.0 && game.energy-aaitem > game.inenrg) {
108             /* Prevent shield drain loophole */
109             skip(1);
110             prout(_("Engineering to bridge--"));
111             prout(_("  Scott here. Power circuit problem, Captain."));
112             prout(_("  I can't drain the shields."));
113             game.ididit = false;
114             return;
115         }
116         if (game.shield+aaitem < 0) {
117             prout(_("All shield energy transferred to ship."));
118             game.energy += game.shield;
119             game.shield = 0.0;
120             return;
121         }
122         proutn(_("Scotty- \""));
123         if (aaitem > 0)
124             prout(_("Transferring energy to shields.\""));
125         else
126             prout(_("Draining energy from shields.\""));
127         game.shield += aaitem;
128         game.energy -= aaitem;
129         return;
130     case NONE:; /* avoid gcc warning */
131     }
132 }
133
134 void ram(bool ibumpd, feature ienm, coord w)
135 /* make our ship ram something */
136 {
137     double hardness, extradm;
138     int icas, m;
139         
140     prouts(_("***RED ALERT!  RED ALERT!"));
141     skip(1);
142     prout(_("***COLLISION IMMINENT."));
143     skip(2);
144     proutn("***");
145     crmshp();
146     switch (ienm) {
147     case IHR: hardness = 1.5; break;
148     case IHC: hardness = 2.0; break;
149     case IHS: hardness = 2.5; break;
150     case IHT: hardness = 0.5; break;
151     case IHQUEST: hardness = 4.0; break;
152     default: hardness = 1.0; break;
153     }
154     proutn(ibumpd ? _(" rammed by ") : _(" rams "));
155     crmena(false, ienm, sector, w);
156     if (ibumpd) proutn(_(" (original position)"));
157     skip(1);
158     deadkl(w, ienm, game.sector);
159     proutn("***");
160     crmshp();
161     prout(_(" heavily damaged."));
162     icas = 10.0+20.0*Rand();
163     prout(_("***Sickbay reports %d casualties"), icas);
164     game.casual += icas;
165     game.state.crew -= icas;
166     for (m=0; m < NDEVICES; m++) {
167         if (m == DDRAY) 
168             continue; // Don't damage deathray 
169         if (game.damage[m] < 0) 
170             continue;
171         extradm = (10.0*hardness*Rand()+1.0)*game.damfac;
172         game.damage[m] += game.optime + extradm; /* Damage for at least time of travel! */
173     }
174     game.shldup = false;
175     if (KLINGREM) {
176         pause_game(true);
177         dreprt();
178     }
179     else finish(FWON);
180     return;
181 }
182
183 void torpedo(double course, double r, coord in, double *hit, int i, int n)
184 /* let a photon torpedo fly */
185 {
186     int l, iquad=0, ll;
187     bool shoved = false;
188     double ac=course + 0.25*r;
189     double angle = (15.0-ac)*0.5235988;
190     double bullseye = (15.0 - course)*0.5235988;
191     double deltax=-sin(angle), deltay=cos(angle), x=in.x, y=in.y, bigger;
192     double ang, temp, xx, yy, kp, h1;
193     struct quadrant *q = &game.state.galaxy[game.quadrant.x][game.quadrant.y];
194     coord w, jw;
195
196     w.x = w.y = jw.x = jw.y = 0;
197     bigger = fabs(deltax);
198     if (fabs(deltay) > bigger) bigger = fabs(deltay);
199     deltax /= bigger;
200     deltay /= bigger;
201     if (!damaged(DSRSENS) || game.condition==docked) 
202         setwnd(srscan_window);
203     else 
204         setwnd(message_window);
205     /* Loop to move a single torpedo */
206     for (l=1; l <= 15; l++) {
207         x += deltax;
208         w.x = x + 0.5;
209         y += deltay;
210         w.y = y + 0.5;
211         if (!VALID_SECTOR(w.x, w.y)) break;
212         iquad=game.quad[w.x][w.y];
213         tracktorpedo(w, l, i, n, iquad);
214         if (iquad==IHDOT) continue;
215         /* hit something */
216         setwnd(message_window);
217         skip(1);        /* start new line after text track */
218         switch(iquad) {
219         case IHE: /* Hit our ship */
220         case IHF:
221             skip(1);
222             proutn(_("Torpedo hits "));
223             crmshp();
224             prout(".");
225             *hit = 700.0 + 100.0*Rand() -
226                 1000.0 * distance(w, in) * fabs(sin(bullseye-angle));
227             *hit = fabs(*hit);
228             newcnd(); /* we're blown out of dock */
229             /* We may be displaced. */
230             if (game.landed==1 || game.condition==docked) return; /* Cheat if on a planet */
231             ang = angle + 2.5*(Rand()-0.5);
232             temp = fabs(sin(ang));
233             if (fabs(cos(ang)) > temp) temp = fabs(cos(ang));
234             xx = -sin(ang)/temp;
235             yy = cos(ang)/temp;
236             jw.x=w.x+xx+0.5;
237             jw.y=w.y+yy+0.5;
238             if (!VALID_SECTOR(jw.x, jw.y)) return;
239             if (game.quad[jw.x][jw.y]==IHBLANK) {
240                 finish(FHOLE);
241                 return;
242             }
243             if (game.quad[jw.x][jw.y]!=IHDOT) {
244                 /* can't move into object */
245                 return;
246             }
247             game.sector = jw;
248             crmshp();
249             shoved = true;
250             break;
251                                           
252         case IHC: /* Hit a commander */
253         case IHS:
254             if (Rand() <= 0.05) {
255                 crmena(true, iquad, sector, w);
256                 prout(_(" uses anti-photon device;"));
257                 prout(_("   torpedo neutralized."));
258                 return;
259             }
260         case IHR: /* Hit a regular enemy */
261         case IHK:
262             /* find the enemy */
263             for_local_enemies(ll)
264                 if (same(w, game.ks[ll]))
265                     break;
266             kp = fabs(game.kpower[ll]);
267             h1 = 700.0 + 100.0*Rand() -
268                 1000.0 * distance(w, in) * fabs(sin(bullseye-angle));
269             h1 = fabs(h1);
270             if (kp < h1) h1 = kp;
271             game.kpower[ll] -= (game.kpower[ll]<0 ? -h1 : h1);
272             if (game.kpower[ll] == 0) {
273                 deadkl(w, iquad, w);
274                 return;
275             }
276             crmena(true, iquad, sector, w);
277             /* If enemy damaged but not destroyed, try to displace */
278             ang = angle + 2.5*(Rand()-0.5);
279             temp = fabs(sin(ang));
280             if (fabs(cos(ang)) > temp) temp = fabs(cos(ang));
281             xx = -sin(ang)/temp;
282             yy = cos(ang)/temp;
283             jw.x=w.x+xx+0.5;
284             jw.y=w.y+yy+0.5;
285             if (!VALID_SECTOR(jw.x, jw.y)) {
286                 prout(_(" damaged but not destroyed."));
287                 return;
288             }
289             if (game.quad[jw.x][jw.y]==IHBLANK) {
290                 prout(_(" buffeted into black hole."));
291                 deadkl(w, iquad, jw);
292                 return;
293             }
294             if (game.quad[jw.x][jw.y]!=IHDOT) {
295                 /* can't move into object */
296                 prout(_(" damaged but not destroyed."));
297                 return;
298             }
299             proutn(_(" damaged--"));
300             game.ks[ll] = jw;
301             shoved = true;
302             break;
303         case IHB: /* Hit a base */
304             skip(1);
305             prout(_("***STARBASE DESTROYED.."));
306             for_starbases(ll) {
307                 if (same(game.state.baseq[ll], game.quadrant)) {
308                     game.state.baseq[ll]=game.state.baseq[game.state.rembase];
309                     break;
310                 }
311             }
312             game.quad[w.x][w.y]=IHDOT;
313             game.state.rembase--;
314             game.base.x=game.base.y=0;
315             q->starbase--;
316             game.state.chart[game.quadrant.x][game.quadrant.y].starbase--;
317             game.state.basekl++;
318             newcnd();
319             return;
320         case IHP: /* Hit a planet */
321             crmena(true, iquad, sector, w);
322             prout(_(" destroyed."));
323             game.state.nplankl++;
324             q->planet = NOPLANET;
325             DESTROY(&game.state.plnets[game.iplnet]);
326             game.iplnet = 0;
327             game.plnet.x = game.plnet.y = 0;
328             game.quad[w.x][w.y] = IHDOT;
329             if (game.landed==1) {
330                 /* captain perishes on planet */
331                 finish(FDPLANET);
332             }
333             return;
334         case IHW: /* Hit an inhabited world -- very bad! */
335             crmena(true, iquad, sector, w);
336             prout(_(" destroyed."));
337             game.state.nworldkl++;
338             q->planet = NOPLANET;
339             DESTROY(&game.state.plnets[game.iplnet]);
340             game.iplnet = 0;
341             game.plnet.x = game.plnet.y = 0;
342             game.quad[w.x][w.y] = IHDOT;
343             if (game.landed==1) {
344                 /* captain perishes on planet */
345                 finish(FDPLANET);
346             }
347             prout("You have just destroyed an inhabited planet.");
348             prout("Celebratory rallies are being held on the Klingon homeworld.");
349             return;
350         case IHSTAR: /* Hit a star */
351             if (Rand() > 0.10) {
352                 nova(w);
353                 return;
354             }
355             crmena(true, IHSTAR, sector, w);
356             prout(_(" unaffected by photon blast."));
357             return;
358         case IHQUEST: /* Hit a thingy */
359             if (!(game.options & OPTION_THINGY) || Rand()>0.7) {
360                 skip(1);
361                 prouts(_("AAAAIIIIEEEEEEEEAAAAAAAAUUUUUGGGGGHHHHHHHHHHHH!!!"));
362                 skip(1);
363                 prouts(_("    HACK!     HACK!    HACK!        *CHOKE!*  "));
364                 skip(1);
365                 proutn(_("Mr. Spock-"));
366                 prouts(_("  \"Fascinating!\""));
367                 skip(1);
368                 deadkl(w, iquad, w);
369             } else {
370                 /*
371                  * Stas Sergeev added the possibility that
372                  * you can shove the Thingy and piss it off.
373                  * It then becomes an enemy and may fire at you.
374                  */
375                 iqengry = true;
376                 shoved = true;
377             }
378             return;
379         case IHBLANK: /* Black hole */
380             skip(1);
381             crmena(true, IHBLANK, sector, w);
382             prout(_(" swallows torpedo."));
383             return;
384         case IHWEB: /* hit the web */
385             skip(1);
386             prout(_("***Torpedo absorbed by Tholian web."));
387             return;
388         case IHT:  /* Hit a Tholian */
389             h1 = 700.0 + 100.0*Rand() -
390                 1000.0 * distance(w, in) * fabs(sin(bullseye-angle));
391             h1 = fabs(h1);
392             if (h1 >= 600) {
393                 game.quad[w.x][w.y] = IHDOT;
394                 game.ithere = false;
395                 deadkl(w, iquad, w);
396                 return;
397             }
398             skip(1);
399             crmena(true, IHT, sector, w);
400             if (Rand() > 0.05) {
401                 prout(_(" survives photon blast."));
402                 return;
403             }
404             prout(_(" disappears."));
405             game.quad[w.x][w.y] = IHWEB;
406             game.ithere = false;
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 = false;
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.condition == docked) 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.condition == docked) 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.condition==docked) {
543             /* shields will take hits */
544             double absorb, hitsh, propor = pfac*game.shield*(game.condition==docked ? 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.condition==docked) 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.condition==docked) 
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.condition == docked)
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, feature 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 = false;
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         default:        /* avoids a gcc warning */
670             prout("*** Internal error, deadkl() called on %c\n", type);
671             break;
672         }
673     }
674
675     /* For each kind of enemy, finish message to player */
676     prout(_(" destroyed."));
677     game.quad[w.x][w.y] = IHDOT;
678     if (KLINGREM==0) return;
679
680     game.state.remtime = game.state.remres/(game.state.remkl + 4*game.state.remcom);
681
682     /* Remove enemy ship from arrays describing local conditions */
683     if (is_scheduled(FCDBAS) && game.battle.x==game.quadrant.x && game.battle.y==game.quadrant.y && type==IHC)
684         unschedule(FCDBAS);
685     for_local_enemies(i)
686         if (same(game.ks[i], w)) break;
687     game.nenhere--;
688     if (i <= game.nenhere)  {
689         for (j=i; j<=game.nenhere; j++) {
690             game.ks[j] = game.ks[j+1];
691             game.kpower[j] = game.kpower[j+1];
692             game.kavgd[j] = game.kdist[j] = game.kdist[j+1];
693         }
694     }
695     game.ks[game.nenhere+1].x = 0;
696     game.ks[game.nenhere+1].x = 0;
697     game.kdist[game.nenhere+1] = 0;
698     game.kavgd[game.nenhere+1] = 0;
699     game.kpower[game.nenhere+1] = 0;
700     return;
701 }
702
703 static bool targetcheck(double x, double y, double *course) 
704 {
705     double deltx, delty;
706     /* Return true if target is invalid */
707     if (!VALID_SECTOR(x, y)) {
708         huh();
709         return true;
710     }
711     deltx = 0.1*(y - game.sector.y);
712     delty = 0.1*(game.sector.x - x);
713     if (deltx==0 && delty== 0) {
714         skip(1);
715         prout(_("Spock-  \"Bridge to sickbay.  Dr. McCoy,"));
716         prout(_("  I recommend an immediate review of"));
717         prout(_("  the Captain's psychological profile.\""));
718         chew();
719         return true;
720     }
721     *course = 1.90985932*atan2(deltx, delty);
722     return false;
723 }
724
725 void photon(void) 
726 /* launch photon torpedo */
727 {
728     double targ[4][3], course[4];
729     double r, dummy;
730     int key, n, i;
731     bool osuabor;
732
733     game.ididit = false;
734
735     if (damaged(DPHOTON)) {
736         prout(_("Photon tubes damaged."));
737         chew();
738         return;
739     }
740     if (game.torps == 0) {
741         prout(_("No torpedoes left."));
742         chew();
743         return;
744     }
745     key = scan();
746     for (;;) {
747         if (key == IHALPHA) {
748             huh();
749             return;
750         }
751         else if (key == IHEOL) {
752             prout(_("%d torpedoes left."), game.torps);
753             proutn(_("Number of torpedoes to fire- "));
754             key = scan();
755         }
756         else /* key == IHREAL */ {
757             n = aaitem + 0.5;
758             if (n <= 0) { /* abort command */
759                 chew();
760                 return;
761             }
762             if (n > 3) {
763                 chew();
764                 prout(_("Maximum of 3 torpedoes per burst."));
765                 key = IHEOL;
766                 return;
767             }
768             if (n <= game.torps) break;
769             chew();
770             key = IHEOL;
771         }
772     }
773     for (i = 1; i <= n; i++) {
774         key = scan();
775         if (i==1 && key == IHEOL) {
776             break;      /* we will try prompting */
777         }
778         if (i==2 && key == IHEOL) {
779             /* direct all torpedoes at one target */
780             while (i <= n) {
781                 targ[i][1] = targ[1][1];
782                 targ[i][2] = targ[1][2];
783                 course[i] = course[1];
784                 i++;
785             }
786             break;
787         }
788         if (key != IHREAL) {
789             huh();
790             return;
791         }
792         targ[i][1] = aaitem;
793         key = scan();
794         if (key != IHREAL) {
795             huh();
796             return;
797         }
798         targ[i][2] = aaitem;
799         if (targetcheck(targ[i][1], targ[i][2], &course[i])) return;
800     }
801     chew();
802     if (i == 1 && key == IHEOL) {
803         /* prompt for each one */
804         for (i = 1; i <= n; i++) {
805             proutn(_("Target sector for torpedo number %d- "), i);
806             key = scan();
807             if (key != IHREAL) {
808                 huh();
809                 return;
810             }
811             targ[i][1] = aaitem;
812             key = scan();
813             if (key != IHREAL) {
814                 huh();
815                 return;
816             }
817             targ[i][2] = aaitem;
818             chew();
819             if (targetcheck(targ[i][1], targ[i][2], &course[i])) return;
820         }
821     }
822     game.ididit = true;
823     /* Loop for moving <n> torpedoes */
824     osuabor = false;
825     for (i = 1; i <= n && !osuabor; i++) {
826         if (game.condition != docked) game.torps--;
827         r = (Rand()+Rand())*0.5 -0.5;
828         if (fabs(r) >= 0.47) {
829             /* misfire! */
830             r = (Rand()+1.2) * r;
831             if (n>1) {
832                 prouts(_("***TORPEDO NUMBER %d MISFIRES"), i);
833             }
834             else prouts(_("***TORPEDO MISFIRES."));
835             skip(1);
836             if (i < n)
837                 prout(_("  Remainder of burst aborted."));
838             osuabor = true;
839             if (Rand() <= 0.2) {
840                 prout(_("***Photon tubes damaged by misfire."));
841                 game.damage[DPHOTON] = game.damfac*(1.0+2.0*Rand());
842                 break;
843             }
844         }
845         if (game.shldup || game.condition == docked) 
846             r *= 1.0 + 0.0001*game.shield;
847         torpedo(course[i], r, game.sector, &dummy, i, n);
848         if (game.alldone || game.state.galaxy[game.quadrant.x][game.quadrant.y].supernova)
849             return;
850     }
851     if (KLINGREM==0) finish(FWON);
852 }
853
854         
855
856 static void overheat(double rpow)
857 /* check for phasers overheating */
858 {
859     if (rpow > 1500) {
860         double chekbrn = (rpow-1500.)*0.00038;
861         if (Rand() <= chekbrn) {
862             prout(_("Weapons officer Sulu-  \"Phasers overheated, sir.\""));
863             game.damage[DPHASER] = game.damfac*(1.0 + Rand()) * (1.0+chekbrn);
864         }
865     }
866 }
867
868 static bool checkshctrl(double rpow) 
869 /* check shield control */
870 {
871     double hit;
872     int icas;
873         
874     skip(1);
875     if (Rand() < .998) {
876         prout(_("Shields lowered."));
877         return false;
878     }
879     /* Something bad has happened */
880     prouts(_("***RED ALERT!  RED ALERT!"));
881     skip(2);
882     hit = rpow*game.shield/game.inshld;
883     game.energy -= rpow+hit*0.8;
884     game.shield -= hit*0.2;
885     if (game.energy <= 0.0) {
886         prouts(_("Sulu-  \"Captain! Shield malf***********************\""));
887         skip(1);
888         stars();
889         finish(FPHASER);
890         return true;
891     }
892     prouts(_("Sulu-  \"Captain! Shield malfunction! Phaser fire contained!\""));
893     skip(2);
894     prout(_("Lt. Uhura-  \"Sir, all decks reporting damage.\""));
895     icas = hit*Rand()*0.012;
896     skip(1);
897     fry(0.8*hit);
898     if (icas) {
899         skip(1);
900         prout(_("McCoy to bridge- \"Severe radiation burns, Jim."));
901         prout(_("  %d casualties so far.\""), icas);
902         game.casual += icas;
903         game.state.crew -= icas;
904     }
905     skip(1);
906     prout(_("Phaser energy dispersed by shields."));
907     prout(_("Enemy unaffected."));
908     overheat(rpow);
909     return true;
910 }
911         
912
913 void phasers(void) 
914 /* fire phasers */
915 {
916     double hits[21], rpow=0, extra, powrem, over, temp;
917     int kz = 0, k=1, i, irec=0; /* Cheating inhibitor */
918     bool ifast = false, no = false, ipoop = true, msgflag = true;
919     enum {NOTSET, MANUAL, FORCEMAN, AUTOMATIC} automode = NOTSET;
920     int key=0;
921
922     skip(1);
923     /* SR sensors and Computer */
924     if (damaged(DSRSENS) || damaged(DCOMPTR)) ipoop = false;
925     if (game.condition == docked) {
926         prout(_("Phasers can't be fired through base shields."));
927         chew();
928         return;
929     }
930     if (damaged(DPHASER)) {
931         prout(_("Phaser control damaged."));
932         chew();
933         return;
934     }
935     if (game.shldup) {
936         if (damaged(DSHCTRL)) {
937             prout(_("High speed shield control damaged."));
938             chew();
939             return;
940         }
941         if (game.energy <= 200.0) {
942             prout(_("Insufficient energy to activate high-speed shield control."));
943             chew();
944             return;
945         }
946         prout(_("Weapons Officer Sulu-  \"High-speed shield control enabled, sir.\""));
947         ifast = true;
948                 
949     }
950     /* Original code so convoluted, I re-did it all */
951     while (automode==NOTSET) {
952         key=scan();
953         if (key == IHALPHA) {
954             if (isit("manual")) {
955                 if (game.nenhere==0) {
956                     prout(_("There is no enemy present to select."));
957                     chew();
958                     key = IHEOL;
959                     automode=AUTOMATIC;
960                 }
961                 else {
962                     automode = MANUAL;
963                     key = scan();
964                 }
965             }
966             else if (isit("automatic")) {
967                 if ((!ipoop) && game.nenhere != 0) {
968                     automode = FORCEMAN;
969                 }
970                 else {
971                     if (game.nenhere==0)
972                         prout(_("Energy will be expended into space."));
973                     automode = AUTOMATIC;
974                     key = scan();
975                 }
976             }
977             else if (isit("no")) {
978                 no = true;
979             }
980             else {
981                 huh();
982                 return;
983             }
984         }
985         else if (key == IHREAL) {
986             if (game.nenhere==0) {
987                 prout(_("Energy will be expended into space."));
988                 automode = AUTOMATIC;
989             }
990             else if (!ipoop)
991                 automode = FORCEMAN;
992             else
993                 automode = AUTOMATIC;
994         }
995         else {
996             /* IHEOL */
997             if (game.nenhere==0) {
998                 prout(_("Energy will be expended into space."));
999                 automode = AUTOMATIC;
1000             }
1001             else if (!ipoop)
1002                 automode = FORCEMAN;
1003             else 
1004                 proutn(_("Manual or automatic? "));
1005         }
1006     }
1007                                 
1008     switch (automode) {
1009     case AUTOMATIC:
1010         if (key == IHALPHA && isit("no")) {
1011             no = true;
1012             key = scan();
1013         }
1014         if (key != IHREAL && game.nenhere != 0) {
1015             prout(_("Phasers locked on target. Energy available: %.2f"),
1016                   ifast?game.energy-200.0:game.energy,1,2);
1017         }
1018         irec=0;
1019         do {
1020             chew();
1021             if (!kz) for_local_enemies(i)
1022                 irec+=fabs(game.kpower[i])/(PHASEFAC*pow(0.90,game.kdist[i]))*
1023                     (1.01+0.05*Rand()) + 1.0;
1024             kz=1;
1025             proutn(_("(%d) units required. "), irec);
1026             chew();
1027             proutn(_("Units to fire= "));
1028             key = scan();
1029             if (key!=IHREAL) return;
1030             rpow = aaitem;
1031             if (rpow > (ifast?game.energy-200:game.energy)) {
1032                 proutn(_("Energy available= %.2f"),
1033                        ifast?game.energy-200:game.energy);
1034                 skip(1);
1035                 key = IHEOL;
1036             }
1037         } while (rpow > (ifast?game.energy-200:game.energy));
1038         if (rpow<=0) {
1039             /* chicken out */
1040             chew();
1041             return;
1042         }
1043         if ((key=scan()) == IHALPHA && isit("no")) {
1044             no = true;
1045         }
1046         if (ifast) {
1047             game.energy -= 200; /* Go and do it! */
1048             if (checkshctrl(rpow)) return;
1049         }
1050         chew();
1051         game.energy -= rpow;
1052         extra = rpow;
1053         if (game.nenhere) {
1054             extra = 0.0;
1055             powrem = rpow;
1056             for_local_enemies(i) {
1057                 hits[i] = 0.0;
1058                 if (powrem <= 0) continue;
1059                 hits[i] = fabs(game.kpower[i])/(PHASEFAC*pow(0.90,game.kdist[i]));
1060                 over = (0.01 + 0.05*Rand())*hits[i];
1061                 temp = powrem;
1062                 powrem -= hits[i] + over;
1063                 if (powrem <= 0 && temp < hits[i]) hits[i] = temp;
1064                 if (powrem <= 0) over = 0.0;
1065                 extra += over;
1066             }
1067             if (powrem > 0.0) extra += powrem;
1068             hittem(hits);
1069             game.ididit = true;
1070         }
1071         if (extra > 0 && !game.alldone) {
1072             if (game.ithere) {
1073                 proutn(_("*** Tholian web absorbs "));
1074                 if (game.nenhere>0) proutn(_("excess "));
1075                 prout(_("phaser energy."));
1076             }
1077             else {
1078                 prout(_("%d expended on empty space."), (int)extra);
1079             }
1080         }
1081         break;
1082
1083     case FORCEMAN:
1084         chew();
1085         key = IHEOL;
1086         if (damaged(DCOMPTR))
1087             prout(_("Battle comuter damaged, manual file only."));
1088         else {
1089             skip(1);
1090             prouts(_("---WORKING---"));
1091             skip(1);
1092             prout(_("Short-range-sensors-damaged"));
1093             prout(_("Insufficient-data-for-automatic-phaser-fire"));
1094             prout(_("Manual-fire-must-be-used"));
1095             skip(1);
1096         }
1097     case MANUAL:
1098         rpow = 0.0;
1099         for (k = 1; k <= game.nenhere;) {
1100             coord aim = game.ks[k];
1101             int ienm = game.quad[aim.x][aim.y];
1102             if (msgflag) {
1103                 proutn(_("Energy available= %.2f"),
1104                        game.energy-.006-(ifast?200:0));
1105                 skip(1);
1106                 msgflag = false;
1107                 rpow = 0.0;
1108             }
1109             if (damaged(DSRSENS) && !(abs(game.sector.x-aim.x) < 2 && abs(game.sector.y-aim.y) < 2) &&
1110                 (ienm == IHC || ienm == IHS)) {
1111                 cramen(ienm);
1112                 prout(_(" can't be located without short range scan."));
1113                 chew();
1114                 key = IHEOL;
1115                 hits[k] = 0; /* prevent overflow -- thanks to Alexei Voitenko */
1116                 k++;
1117                 continue;
1118             }
1119             if (key == IHEOL) {
1120                 chew();
1121                 if (ipoop && k > kz)
1122                     irec=(fabs(game.kpower[k])/(PHASEFAC*pow(0.9,game.kdist[k])))*
1123                         (1.01+0.05*Rand()) + 1.0;
1124                 kz = k;
1125                 proutn("(");
1126                 if (!damaged(DCOMPTR)) proutn("%d", irec);
1127                 else proutn("??");
1128                 proutn(")  ");
1129                 proutn(_("units to fire at "));
1130                 crmena(false, ienm, sector, aim);
1131                 proutn("-  ");
1132                 key = scan();
1133             }
1134             if (key == IHALPHA && isit("no")) {
1135                 no = 1;
1136                 key = scan();
1137                 continue;
1138             }
1139             if (key == IHALPHA) {
1140                 huh();
1141                 return;
1142             }
1143             if (key == IHEOL) {
1144                 if (k==1) { /* Let me say I'm baffled by this */
1145                     msgflag = true;
1146                 }
1147                 continue;
1148             }
1149             if (aaitem < 0) {
1150                 /* abort out */
1151                 chew();
1152                 return;
1153             }
1154             hits[k] = aaitem;
1155             rpow += aaitem;
1156             /* If total requested is too much, inform and start over */
1157                                 
1158             if (rpow > (ifast?game.energy-200:game.energy)) {
1159                 prout(_("Available energy exceeded -- try again."));
1160                 chew();
1161                 return;
1162             }
1163             key = scan(); /* scan for next value */
1164             k++;
1165         }
1166         if (rpow == 0.0) {
1167             /* zero energy -- abort */
1168             chew();
1169             return;
1170         }
1171         if (key == IHALPHA && isit("no")) {
1172             no = true;
1173         }
1174         game.energy -= rpow;
1175         chew();
1176         if (ifast) {
1177             game.energy -= 200.0;
1178             if (checkshctrl(rpow)) return;
1179         }
1180         hittem(hits);
1181         game.ididit = true;
1182     case NOTSET:;       /* avoid gcc warning */
1183     }
1184     /* Say shield raised or malfunction, if necessary */
1185     if (game.alldone) 
1186         return;
1187     if (ifast) {
1188         skip(1);
1189         if (no == 0) {
1190             if (Rand() >= 0.99) {
1191                 prout(_("Sulu-  \"Sir, the high-speed shield control has malfunctioned . . ."));
1192                 prouts(_("         CLICK   CLICK   POP  . . ."));
1193                 prout(_(" No response, sir!"));
1194                 game.shldup = false;
1195             }
1196             else
1197                 prout(_("Shields raised."));
1198         }
1199         else
1200             game.shldup = false;
1201     }
1202     overheat(rpow);
1203 }
1204
1205 void hittem(double *hits) 
1206 /* register a phaser hit on Klingons and Romulans */
1207 {
1208     double kp, kpow, wham, hit, dustfac, kpini;
1209     int nenhr2=game.nenhere, k=1, kk=1, ienm;
1210     coord w;
1211
1212     skip(1);
1213
1214     for (; k <= nenhr2; k++, kk++) {
1215         if ((wham = hits[k])==0) continue;
1216         dustfac = 0.9 + 0.01*Rand();
1217         hit = wham*pow(dustfac,game.kdist[kk]);
1218         kpini = game.kpower[kk];
1219         kp = fabs(kpini);
1220         if (PHASEFAC*hit < kp) kp = PHASEFAC*hit;
1221         game.kpower[kk] -= (game.kpower[kk] < 0 ? -kp: kp);
1222         kpow = game.kpower[kk];
1223         w = game.ks[kk];
1224         if (hit > 0.005) {
1225             if (!damaged(DSRSENS))
1226                 boom(w);
1227             proutn(_("%d unit hit on "), (int)hit);
1228         }
1229         else
1230             proutn(_("Very small hit on "));
1231         ienm = game.quad[w.x][w.y];
1232         if (ienm==IHQUEST) iqengry = true;
1233         crmena(false,ienm,sector,w);
1234         skip(1);
1235         if (kpow == 0) {
1236             deadkl(w, ienm, w);
1237             if (KLINGREM==0) finish(FWON);
1238             if (game.alldone) return;
1239             kk--; /* don't do the increment */
1240         }
1241         else /* decide whether or not to emasculate klingon */
1242             if (kpow > 0 && Rand() >= 0.9 &&
1243                 kpow <= ((0.4 + 0.4*Rand())*kpini)) {
1244                 prout(_("***Mr. Spock-  \"Captain, the vessel at "),
1245                       cramlc(sector, w));
1246                 prout(_("   has just lost its firepower.\""));
1247                 game.kpower[kk] = -kpow;
1248             }
1249     }
1250     return;
1251 }
1252