Pull this loose from the autoconf goo.
[super-star-trek.git] / c-version / src / events.c
1 /*
2  * events.c -- event-queue handling
3  *
4  * This isn't a real event queue a la BSD Trek yet -- you can only have one 
5  * event of each type active at any given time.  Mostly these means we can 
6  * only have one FDISTR/FENSLV/FREPRO sequence going at any given time;
7  * BSD Trek, from which we swiped the idea, can have up to 5.
8  */
9 #include "sst.h"
10 #include <math.h>
11
12 event *unschedule(int evtype)
13 /* remove an event from the schedule */
14 {
15     game.future[evtype].date = FOREVER;
16     return &game.future[evtype];
17 }
18
19 int is_scheduled(int evtype)
20 /* is an event of specified type scheduled */
21 {
22     return game.future[evtype].date != FOREVER;
23 }
24
25 double scheduled(int evtype)
26 /* when will this event happen? */
27 {
28     return game.future[evtype].date;
29 }
30
31 event *schedule(int evtype, double offset)
32 /* schedule an event of specified type */
33 {
34     game.future[evtype].date = game.state.date + offset;
35     return &game.future[evtype];
36 }
37
38 void postpone(int evtype, double offset)
39 /* postpone a scheduled event */
40 {
41     game.future[evtype].date += offset;
42 }
43
44 static bool cancelrest(void)
45 /* rest period is interrupted by event */
46 {
47     if (game.resting) {
48         skip(1);
49         proutn(_("Mr. Spock-  \"Captain, shall we cancel the rest period?\""));
50         if (ja() == true) {
51             game.resting = false;
52             game.optime = 0.0;
53             return true;
54         }
55     }
56
57     return false;
58 }
59
60 void events(void) 
61 /* run through the event queue looking for things to do */
62 {
63     int evcode, i=0, j, k, l;
64     double fintim = game.state.date + game.optime, datemin, xtime, repair, yank=0;
65     bool ictbeam = false, istract = false;
66     struct quadrant *pdest, *q;
67     coord w, hold;
68     event *ev, *ev2;
69     bool fixed_dev[NDEVICES];
70
71     if (idebug) {
72         prout("=== EVENTS from %.2f to %.2f:", game.state.date, fintim);
73         for (i = 1; i < NEVENTS; i++) {
74             switch (i) {
75             case FSNOVA:  proutn("=== Supernova       "); break;
76             case FTBEAM:  proutn("=== T Beam          "); break;
77             case FSNAP:   proutn("=== Snapshot        "); break;
78             case FBATTAK: proutn("=== Base Attack     "); break;
79             case FCDBAS:  proutn("=== Base Destroy    "); break;
80             case FSCMOVE: proutn("=== SC Move         "); break;
81             case FSCDBAS: proutn("=== SC Base Destroy "); break;
82             case FDSPROB: proutn("=== Probe Move      "); break;
83             case FDISTR:  proutn("=== Distress Call   "); break;
84             case FENSLV:  proutn("=== Enslavement     "); break;
85             case FREPRO:  proutn("=== Klingon Build   "); break;
86             }
87             if (is_scheduled(i))
88                 prout("%.2f", scheduled(i));
89             else
90                 prout("never");
91
92         }
93     }
94
95     hold.x = hold.y = 0;
96     for (;;) {
97         /* Select earliest extraneous event, evcode==0 if no events */
98         evcode = FSPY;
99         if (game.alldone)
100             return;
101         datemin = fintim;
102         for (l = 1; l < NEVENTS; l++)
103             if (game.future[l].date < datemin) {
104                 evcode = l;
105                 if (idebug)
106                     prout("== Event %d fires", evcode);
107                 datemin = game.future[l].date;
108             }
109         xtime = datemin-game.state.date;
110         game.state.date = datemin;
111         /* Decrement Federation resources and recompute remaining time */
112         game.state.remres -= (game.state.remkl+4*game.state.remcom)*xtime;
113         game.state.remtime = game.state.remkl + game.state.remcom > 0 ?
114                 game.state.remres/(game.state.remkl + 4*game.state.remcom) : 99;
115         if (game.state.remtime <=0) {
116             finish(FDEPLETE);
117             return;
118         }
119         /* Any crew left alive? */
120         if (game.state.crew <=0) {
121             finish(FCREW);
122             return;
123         }
124         /* Is life support adequate? */
125         if (damaged(DLIFSUP) && game.condition != docked) {
126             if (game.lsupres < xtime && game.damage[DLIFSUP] > game.lsupres) {
127                 finish(FLIFESUP);
128                 return;
129             }
130             game.lsupres -= xtime;
131             if (game.damage[DLIFSUP] <= xtime)
132                 game.lsupres = game.inlsr;
133         }
134         /* Fix devices */
135         repair = xtime;
136         if (game.condition == docked)
137             repair /= game.docfac;
138         /* Don't fix Deathray here */
139         for (l=0; l<NDEVICES; l++) {
140             fixed_dev[l] = false;
141             if (game.damage[l] > 0.0 && l != DDRAY) {
142                 double reminder = (game.damage[l] > repair ?
143                         game.damage[l] - repair : .0);
144                 game.damage[l] = reminder;
145                 if (!(reminder > 0))
146                     fixed_dev[l] = true;
147             }
148         }
149         /* If radio repaired, update star chart and attack reports */
150         if (fixed_dev[DRADIO]) {
151             prout(_("Lt. Uhura- \"Captain, the sub-space radio is working and"));
152             prout(_("   surveillance reports are coming in."));
153             skip(1);
154             if (!game.iseenit) {
155                 attackreport(false);
156                 game.iseenit = true;
157             }
158             prout(_("   The star chart is now up to date.\""));
159             skip(1);
160         }
161         if (fixed_dev[DRADIO] || fixed_dev[DLRSENS] || fixed_dev[DSRSENS])
162             rechart();
163         /* Cause extraneous event EVCODE to occur */
164         game.optime -= xtime;
165         switch (evcode) {
166         case FSNOVA: /* Supernova */
167             announce();
168             supernova(false, NULL);
169             schedule(FSNOVA, expran(0.5*game.intime));
170             if (game.state.galaxy[game.quadrant.x][game.quadrant.y].supernova)
171                 return;
172             break;
173         case FSPY: /* Check with spy to see if S.C. should tractor beam */
174             if (game.state.nscrem == 0 ||
175                 ictbeam || istract ||
176                 game.condition==docked || game.isatb==1 || game.iscate)
177                 return;
178             if (game.ientesc ||
179                 (game.energy < 2000 && game.torps < 4 && game.shield < 1250) ||
180                 (damaged(DPHASER) && (damaged(DPHOTON) || game.torps < 4)) ||
181                 (damaged(DSHIELD) &&
182                  (game.energy < 2500 || damaged(DPHASER)) &&
183                  (game.torps < 5 || damaged(DPHOTON)))) {
184                 /* Tractor-beam her! */
185                 istract = true;
186                 yank = distance(game.state.kscmdr, game.quadrant);
187                 /********* fall through to FTBEAM code ***********/
188             }
189             else
190                 return;
191         case FTBEAM: /* Tractor beam */
192             if (evcode==FTBEAM) {
193                 if (game.state.remcom == 0) {
194                     unschedule(FTBEAM);
195                     break;
196                 }
197                 i = Rand()*game.state.remcom+1.0;
198                 yank = square(game.state.kcmdr[i].x-game.quadrant.x) + square(game.state.kcmdr[i].y-game.quadrant.y);
199                 if (istract || game.condition == docked || yank == 0) {
200                     /* Drats! Have to reschedule */
201                     schedule(FTBEAM, 
202                              game.optime + expran(1.5*game.intime/game.state.remcom));
203                     break;
204                 }
205             }
206             /* tractor beaming cases merge here */
207             yank = sqrt(yank);
208             announce();
209             game.optime = (10.0/(7.5*7.5))*yank; /* 7.5 is yank rate (warp 7.5) */
210             ictbeam = true;
211             skip(1);
212             proutn("***");
213             crmshp();
214             prout(_(" caught in long range tractor beam--"));
215             /* If Kirk & Co. screwing around on planet, handle */
216             atover(true); /* atover(true) is Grab */
217             if (game.alldone)
218                 return;
219             if (game.icraft) { /* Caught in Galileo? */
220                 finish(FSTRACTOR);
221                 return;
222             }
223             /* Check to see if shuttle is aboard */
224             if (game.iscraft == offship) {
225                 skip(1);
226                 if (Rand() > 0.5) {
227                     prout(_("Galileo, left on the planet surface, is captured"));
228                     prout(_("by aliens and made into a flying McDonald's."));
229                     game.damage[DSHUTTL] = -10;
230                     game.iscraft = removed;
231                 }
232                 else {
233                     prout(_("Galileo, left on the planet surface, is well hidden."));
234                 }
235             }
236             if (evcode==0)
237                 game.quadrant = game.state.kscmdr;
238             else
239                 game.quadrant = game.state.kcmdr[i];
240             game.sector = randplace(QUADSIZE);
241             crmshp();
242             proutn(_(" is pulled to "));
243             proutn(cramlc(quadrant, game.quadrant));
244             proutn(", ");
245             prout(cramlc(sector, game.sector));
246             if (game.resting) {
247                 prout(_("(Remainder of rest/repair period cancelled.)"));
248                 game.resting = false;
249             }
250             if (!game.shldup) {
251                 if (!damaged(DSHIELD) && game.shield > 0) {
252                     doshield(true); /* raise shields */
253                     game.shldchg=false;
254                 }
255                 else
256                     prout(_("(Shields not currently useable.)"));
257             }
258             newqad(false);
259             /* Adjust finish time to time of tractor beaming */
260             fintim = game.state.date+game.optime;
261             attack(false);
262             if (game.state.remcom <= 0)
263                 unschedule(FTBEAM);
264             else 
265                 schedule(FTBEAM, game.optime+expran(1.5*game.intime/game.state.remcom));
266             break;
267         case FSNAP: /* Snapshot of the universe (for time warp) */
268             game.snapsht = game.state;
269             game.state.snap = true;
270             schedule(FSNAP, expran(0.5 * game.intime));
271             break;
272         case FBATTAK: /* Commander attacks starbase */
273             if (game.state.remcom==0 || game.state.rembase==0) {
274                 /* no can do */
275                 unschedule(FBATTAK);
276                 unschedule(FCDBAS);
277                 break;
278             }
279             i = 0;
280             for (j = 1; j <= game.state.rembase; j++) {
281                 for (k = 1; k <= game.state.remcom; k++)
282                     if (same(game.state.baseq[j], game.state.kcmdr[k]) &&
283                         !same(game.state.baseq[j], game.quadrant) &&
284                         !same(game.state.baseq[j], game.state.kscmdr)) {
285                         i = 1;
286                         break;
287                     }
288                 if (i == 1)
289                     break;
290             }
291             if (j>game.state.rembase) {
292                 /* no match found -- try later */
293                 schedule(FBATTAK, expran(0.3*game.intime));
294                 unschedule(FCDBAS);
295                 break;
296             }
297             /* commander + starbase combination found -- launch attack */
298             game.battle = game.state.baseq[j];
299             schedule(FCDBAS, 1.0+3.0*Rand());
300             if (game.isatb) /* extra time if SC already attacking */
301                 postpone(FCDBAS, scheduled(FSCDBAS)-game.state.date);
302             game.future[FBATTAK].date = game.future[FCDBAS].date + expran(0.3*game.intime);
303             game.iseenit = false;
304             if (!damaged(DRADIO) && game.condition != docked) 
305                 break; /* No warning :-( */
306             game.iseenit = true;
307             announce();
308             skip(1);
309             proutn(_("Lt. Uhura-  \"Captain, the starbase in "));
310             prout(cramlc(quadrant, game.battle));
311             prout(_("   reports that it is under attack and that it can"));
312             proutn(_("   hold out only until stardate %d"),
313                    (int)scheduled(FCDBAS));
314             prout(".\"");
315             if (cancelrest())
316                 return;
317             break;
318         case FSCDBAS: /* Supercommander destroys base */
319             unschedule(FSCDBAS);
320             game.isatb = 2;
321             if (!game.state.galaxy[game.state.kscmdr.x][game.state.kscmdr.y].starbase) 
322                 break; /* WAS RETURN! */
323             hold = game.battle;
324             game.battle = game.state.kscmdr;
325             /* FALL THROUGH */
326         case FCDBAS: /* Commander succeeds in destroying base */
327             if (evcode==FCDBAS) {
328                 unschedule(FCDBAS);
329                 /* find the lucky pair */
330                 for (i = 1; i <= game.state.remcom; i++)
331                     if (same(game.state.kcmdr[i], game.battle)) 
332                         break;
333                 if (i > game.state.remcom || game.state.rembase == 0 ||
334                     !game.state.galaxy[game.battle.x][game.battle.y].starbase) {
335                     /* No action to take after all */
336                     invalidate(game.battle);
337                     break;
338                 }
339             }
340             /* Code merges here for any commander destroying base */
341             /* Not perfect, but will have to do */
342             /* Handle case where base is in same quadrant as starship */
343             if (same(game.battle, game.quadrant)) {
344                 game.state.chart[game.battle.x][game.battle.y].starbase = false;
345                 game.quad[game.base.x][game.base.y] = IHDOT;
346                 game.base.x=game.base.y=0;
347                 newcnd();
348                 skip(1);
349                 prout(_("Spock-  \"Captain, I believe the starbase has been destroyed.\""));
350             }
351             else if (game.state.rembase != 1 &&
352                      (!damaged(DRADIO) || game.condition == docked)) {
353                 /* Get word via subspace radio */
354                 announce();
355                 skip(1);
356                 prout(_("Lt. Uhura-  \"Captain, Starfleet Command reports that"));
357                 proutn(_("   the starbase in "));
358                 proutn(cramlc(quadrant, game.battle));
359                 prout(_(" has been destroyed by"));
360                 if (game.isatb == 2) 
361                     prout(_("the Klingon Super-Commander"));
362                 else
363                     prout(_("a Klingon Commander"));
364                 game.state.chart[game.battle.x][game.battle.y].starbase = false;
365             }
366             /* Remove Starbase from galaxy */
367             game.state.galaxy[game.battle.x][game.battle.y].starbase = false;
368             for (i = 1; i <= game.state.rembase; i++)
369                 if (same(game.state.baseq[i], game.battle))
370                     game.state.baseq[i] = game.state.baseq[game.state.rembase];
371             game.state.rembase--;
372             if (game.isatb == 2) {
373                 /* reinstate a commander's base attack */
374                 game.battle = hold;
375                 game.isatb = 0;
376             }
377             else
378                 invalidate(game.battle);
379             break;
380         case FSCMOVE: /* Supercommander moves */
381             schedule(FSCMOVE, 0.2777);
382             if (!game.ientesc && !istract && game.isatb != 1 &&
383                         (!game.iscate || !game.justin)) 
384                 supercommander();
385             break;
386         case FDSPROB: /* Move deep space probe */
387             schedule(FDSPROB, 0.01);
388             game.probex += game.probeinx;
389             game.probey += game.probeiny;
390             i = (int)(game.probex/QUADSIZE +0.05);
391             j = (int)(game.probey/QUADSIZE + 0.05);
392             if (game.probec.x != i || game.probec.y != j) {
393                 game.probec.x = i;
394                 game.probec.y = j;
395                 if (!VALID_QUADRANT(i, j) ||
396                     game.state.galaxy[game.probec.x][game.probec.y].supernova) {
397                     // Left galaxy or ran into supernova
398                     if (!damaged(DRADIO) || game.condition == docked) {
399                         announce();
400                         skip(1);
401                         proutn(_("Lt. Uhura-  \"The deep space probe "));
402                         if (!VALID_QUADRANT(j, i))
403                             proutn(_("has left the galaxy"));
404                         else
405                             proutn(_("is no longer transmitting"));
406                         prout(".\"");
407                     }
408                     unschedule(FDSPROB);
409                     break;
410                 }
411                 if (!damaged(DRADIO) || game.condition == docked) {
412                     announce();
413                     skip(1);
414                     proutn(_("Lt. Uhura-  \"The deep space probe is now in "));
415                     proutn(cramlc(quadrant, game.probec));
416                     prout(".\"");
417                 }
418             }
419             pdest = &game.state.galaxy[game.probec.x][game.probec.y];
420             /* Update star chart if Radio is working or have access to
421                radio. */
422             if (!damaged(DRADIO) || game.condition == docked) {
423                 struct page *chp = &game.state.chart[game.probec.x][game.probec.y];
424
425                 chp->klingons = pdest->klingons;
426                 chp->starbase = pdest->starbase;
427                 chp->stars = pdest->stars;
428                 pdest->charted = true;
429             }
430             game.proben--; // One less to travel
431             if (game.proben == 0 && game.isarmed && pdest->stars) {
432                 /* lets blow the sucker! */
433                 supernova(true, &game.probec);
434                 unschedule(FDSPROB);
435                 if (game.state.galaxy[game.quadrant.x][game.quadrant.y].supernova) 
436                     return;
437             }
438             break;
439         case FDISTR: /* inhabited system issues distress call */
440             unschedule(FDISTR);
441             /* try a whole bunch of times to find something suitable */
442             i = 100;
443             do {
444                 // need a quadrant which is not the current one,
445                 // which has some stars which are inhabited and
446                 // not already under attack, which is not
447                 // supernova'ed, and which has some Klingons in it
448                 w = randplace(GALSIZE);
449                 q = &game.state.galaxy[w.x][w.y];
450             } while (--i &&
451                      (same(game.quadrant, w) || q->planet == NOPLANET ||
452                       game.state.planets[q->planet].inhabited == UNINHABITED ||
453                       q->supernova || q->status!=secure || q->klingons<=0));
454             if (i == 0) {
455                 /* can't seem to find one; ignore this call */
456                 if (idebug)
457                     prout("=== Couldn't find location for distress event.");
458                 break;
459             }
460
461             /* got one!!  Schedule its enslavement */
462             ev = schedule(FENSLV, expran(game.intime));
463             ev->quadrant = w;
464             q->status = distressed;
465
466             /* tell the captain about it if we can */
467             if (!damaged(DRADIO) || game.condition == docked)
468             {
469                 prout(_("Uhura- Captain, %s in %s reports it is under attack"),
470                       systnames[q->planet], cramlc(quadrant, w));
471                 prout(_("by a Klingon invasion fleet."));
472                 if (cancelrest())
473                     return;
474             }
475             break;
476         case FENSLV:            /* starsystem is enslaved */
477             ev = unschedule(FENSLV);
478             /* see if current distress call still active */
479             q = &game.state.galaxy[ev->quadrant.x][ev->quadrant.y];
480             if (q->klingons <= 0) {
481                 q->status = secure;
482                 break;
483             }
484             q->status = enslaved;
485
486             /* play stork and schedule the first baby */
487             ev2 = schedule(FREPRO, expran(2.0 * game.intime));
488             ev2->quadrant = ev->quadrant;
489
490             /* report the disaster if we can */
491             if (!damaged(DRADIO) || game.condition == docked)
492             {
493                 prout(_("Uhura- We've lost contact with starsystem %s"),
494                       systnames[q->planet]);
495                 prout(_("in %s.\n"), cramlc(quadrant, ev->quadrant));
496             }
497             break;
498         case FREPRO:            /* Klingon reproduces */
499             // If we ever switch to a real event queue, we'll need to
500             // explicitly retrieve and restore the x and y.
501             ev = schedule(FREPRO, expran(1.0 * game.intime));
502             /* see if current distress call still active */
503             q = &game.state.galaxy[ev->quadrant.x][ev->quadrant.y];
504             if (q->klingons <= 0) {
505                 q->status = secure;
506                 break;
507             }
508             if (game.state.remkl >=MAXKLGAME)
509                 break;          /* full right now */
510             /* reproduce one Klingon */
511             w = ev->quadrant;
512             if (game.klhere >= MAXKLQUAD) {
513                 /* this quadrant not ok, pick an adjacent one */
514                 for (i = w.x - 1; i <= w.x + 1; i++)
515                 {
516                     for (j = w.y - 1; j <= w.y + 1; j++)
517                     {
518                         if (!VALID_QUADRANT(i, j))
519                             continue;
520                         q = &game.state.galaxy[w.x][w.y];
521                         /* check for this quad ok (not full & no snova) */
522                         if (q->klingons >= MAXKLQUAD || q->supernova)
523                             continue;
524                         goto foundit;
525                     }
526                 }
527                 break;  /* search for eligible quadrant failed */
528             foundit:
529                 w.x = i;
530                 w.y = j;
531             }
532
533             /* deliver the child */
534             game.state.remkl++;
535             q->klingons++;
536             if (same(game.quadrant, w))
537                 newkling(++game.klhere);
538
539             /* recompute time left */
540             game.state.remtime = game.state.remkl + game.state.remcom > 0 ?
541                     game.state.remres/(game.state.remkl + 4*game.state.remcom) : 99;
542             /* report the disaster if we can */
543             if (!damaged(DRADIO) || game.condition == docked)
544             {
545                 if (same(game.quadrant, w)) {
546                     prout(_("Spock- sensors indicate the Klingons have"));
547                     prout(_("launched a warship from %s."), systnames[q->planet]);
548                 } else {
549                     prout(_("Uhura- Starfleet reports increased Klingon activity"));
550                     if (q->planet != NOPLANET)
551                         proutn(_("near %s "), systnames[q->planet]);
552                     prout(_("in %s.\n"), cramlc(quadrant, w));
553                 }
554             }
555             break;
556         }
557     }
558 }
559
560                                 
561 void wait(void) 
562 /* wait on events */
563 {
564     int key;
565     double temp, delay, origTime;
566
567     game.ididit = false;
568     for (;;) {
569         key = scan();
570         if (key  != IHEOL)
571             break;
572         proutn(_("How long? "));
573     }
574     chew();
575     if (key != IHREAL) {
576         huh();
577         return;
578     }
579     origTime = delay = aaitem;
580     if (delay <= 0.0)
581         return;
582     if (delay >= game.state.remtime || game.nenhere != 0) {
583         proutn(_("Are you sure? "));
584         if (ja() == false)
585             return;
586     }
587
588     /* Alternate resting periods (events) with attacks */
589
590     game.resting = true;
591     do {
592         if (delay <= 0)
593             game.resting = false;
594         if (!game.resting) {
595             prout(_("%d stardates left."), (int)game.state.remtime);
596             return;
597         }
598         temp = game.optime = delay;
599
600         if (game.nenhere) {
601             double rtime = 1.0 + Rand();
602             if (rtime < temp)
603                 temp = rtime;
604             game.optime = temp;
605         }
606         if (game.optime < delay)
607             attack(false);
608         if (game.alldone)
609             return;
610         events();
611         game.ididit = true;
612         if (game.alldone)
613             return;
614         delay -= temp;
615         /* Repair Deathray if long rest at starbase */
616         if (origTime-delay >= 9.99 && game.condition == docked)
617             game.damage[DDRAY] = 0.0;
618     } while 
619         // leave if quadrant supernovas
620         (!game.state.galaxy[game.quadrant.x][game.quadrant.y].supernova);
621
622     game.resting = false;
623     game.optime = 0;
624 }
625
626 /*
627  *      A nova occurs.  It is the result of having a star hit with a
628  *      photon torpedo, or possibly of a probe warhead going off.
629  *      Stars that go nova cause stars which surround them to undergo
630  *      the same probabilistic process.  Klingons next to them are
631  *      destroyed.  And if the starship is next to it, it gets zapped.
632  *      If the zap is too much, it gets destroyed.
633  */
634 void nova(coord nov) 
635 /* star goes nova */
636 {
637     static double course[] =
638         {0.0, 10.5, 12.0, 1.5, 9.0, 0.0, 3.0, 7.5, 6.0, 4.5};
639     int bot, top, top2, hits[QUADSIZE+1][3], kount, icx, icy, mm, nn, j;
640     int iquad, iquad1, i, ll;
641     coord newc, scratch;
642
643     if (Rand() < 0.05) {
644         /* Wow! We've supernova'ed */
645         supernova(false, &nov);
646         return;
647     }
648
649     /* handle initial nova */
650     game.quad[nov.x][nov.y] = IHDOT;
651     crmena(false, IHSTAR, sector, nov);
652     prout(_(" novas."));
653     game.state.galaxy[game.quadrant.x][game.quadrant.y].stars--;
654     game.state.starkl++;
655         
656     /* Set up stack to recursively trigger adjacent stars */
657     bot = top = top2 = 1;
658     kount = 0;
659     icx = icy = 0;
660     hits[1][1] = nov.x;
661     hits[1][2] = nov.y;
662     while (1) {
663         for (mm = bot; mm <= top; mm++) 
664             for (nn = 1; nn <= 3; nn++)  /* nn,j represents coordinates around current */
665                 for (j = 1; j <= 3; j++) {
666                     if (j==2 && nn== 2)
667                         continue;
668                     scratch.x = hits[mm][1]+nn-2;
669                     scratch.y = hits[mm][2]+j-2;
670                     if (!VALID_SECTOR(scratch.y, scratch.x))
671                         continue;
672                     iquad = game.quad[scratch.x][scratch.y];
673                     switch (iquad) {
674                     // case IHDOT:      /* Empty space ends reaction
675                     // case IHQUEST:
676                     // case IHBLANK:
677                     // case IHT:
678                     // case IHWEB:
679                     default:
680                         break;
681                     case IHSTAR: /* Affect another star */
682                         if (Rand() < 0.05) {
683                             /* This star supernovas */
684                             supernova(false, &scratch);
685                             return;
686                         }
687                         top2++;
688                         hits[top2][1]=scratch.x;
689                         hits[top2][2]=scratch.y;
690                         game.state.galaxy[game.quadrant.x][game.quadrant.y].stars -= 1;
691                         game.state.starkl++;
692                         crmena(true, IHSTAR, sector, scratch);
693                         prout(_(" novas."));
694                         game.quad[scratch.x][scratch.y] = IHDOT;
695                         break;
696                     case IHP: /* Destroy planet */
697                         game.state.galaxy[game.quadrant.x][game.quadrant.y].planet = NOPLANET;
698                         game.state.nplankl++;
699                         crmena(true, IHP, sector, scratch);
700                         prout(_(" destroyed."));
701                         game.state.planets[game.iplnet].pclass = destroyed;
702                         game.iplnet = 0;
703                         invalidate(game.plnet);
704                         if (game.landed) {
705                             finish(FPNOVA);
706                             return;
707                         }
708                         game.quad[scratch.x][scratch.y] = IHDOT;
709                         break;
710                     case IHB: /* Destroy base */
711                         game.state.galaxy[game.quadrant.x][game.quadrant.y].starbase = false;
712                         for (i = 1; i <= game.state.rembase; i++)
713                             if (same(game.state.baseq[i], game.quadrant)) 
714                                 break;
715                         game.state.baseq[i] = game.state.baseq[game.state.rembase];
716                         game.state.rembase--;
717                         invalidate(game.base);
718                         game.state.basekl++;
719                         newcnd();
720                         crmena(true, IHB, sector, scratch);
721                         prout(_(" destroyed."));
722                         game.quad[scratch.x][scratch.y] = IHDOT;
723                         break;
724                     case IHE: /* Buffet ship */
725                     case IHF:
726                         prout(_("***Starship buffeted by nova."));
727                         if (game.shldup) {
728                             if (game.shield >= 2000.0)
729                                 game.shield -= 2000.0;
730                             else {
731                                 double diff = 2000.0 - game.shield;
732                                 game.energy -= diff;
733                                 game.shield = 0.0;
734                                 game.shldup = false;
735                                 prout(_("***Shields knocked out."));
736                                 game.damage[DSHIELD] += 0.005*game.damfac*Rand()*diff;
737                             }
738                         }
739                         else
740                             game.energy -= 2000.0;
741                         if (game.energy <= 0) {
742                             finish(FNOVA);
743                             return;
744                         }
745                         /* add in course nova contributes to kicking starship*/
746                         icx += game.sector.x-hits[mm][1];
747                         icy += game.sector.y-hits[mm][2];
748                         kount++;
749                         break;
750                     case IHK: /* kill klingon */
751                         deadkl(scratch,iquad, scratch);
752                         break;
753                     case IHC: /* Damage/destroy big enemies */
754                     case IHS:
755                     case IHR:
756                         for (ll = 1; ll <= game.nenhere; ll++)
757                             if (same(game.ks[ll], scratch))
758                                 break;
759                         game.kpower[ll] -= 800.0; /* If firepower is lost, die */
760                         if (game.kpower[ll] <= 0.0) {
761                             deadkl(scratch, iquad, scratch);
762                             break;
763                         }
764                         newc.x = scratch.x + scratch.x - hits[mm][1];
765                         newc.y = scratch.y + scratch.y - hits[mm][2];
766                         crmena(true, iquad, sector, scratch);
767                         proutn(_(" damaged"));
768                         if (!VALID_SECTOR(newc.x, newc.y)) {
769                             /* can't leave quadrant */
770                             skip(1);
771                             break;
772                         }
773                         iquad1 = game.quad[newc.x][newc.y];
774                         if (iquad1 == IHBLANK) {
775                             proutn(_(", blasted into "));
776                             crmena(false, IHBLANK, sector, newc);
777                             skip(1);
778                             deadkl(scratch, iquad, newc);
779                             break;
780                         }
781                         if (iquad1 != IHDOT) {
782                             /* can't move into something else */
783                             skip(1);
784                             break;
785                         }
786                         proutn(_(", buffeted to "));
787                         proutn(cramlc(sector, newc));
788                         game.quad[scratch.x][scratch.y] = IHDOT;
789                         game.quad[newc.x][newc.y] = iquad;
790                         game.ks[ll] = newc;
791                         game.kdist[ll] = game.kavgd[ll] = distance(game.sector, newc);
792                         skip(1);
793                         break;
794                     }
795                 }
796         if (top == top2) 
797             break;
798         bot = top + 1;
799         top = top2;
800     }
801     if (kount==0) 
802         return;
803
804     /* Starship affected by nova -- kick it away. */
805     game.dist = kount*0.1;
806     if (icx)
807         icx = (icx < 0 ? -1 : 1);
808     if (icy)
809         icy = (icy < 0 ? -1 : 1);
810     game.direc = course[3*(icx+1)+icy+2];
811     if (game.direc == 0.0)
812         game.dist = 0.0;
813     if (game.dist == 0.0)
814         return;
815     game.optime = 10.0*game.dist/16.0;
816     skip(1);
817     prout(_("Force of nova displaces starship."));
818     imove(true);
819     game.optime = 10.0*game.dist/16.0;
820     return;
821 }
822         
823         
824 void supernova(bool induced, coord *w) 
825 /* star goes supernova */
826 {
827     int num = 0, nrmdead, npdead = 0, kldead, loop;
828     coord nq;
829
830     if (w != NULL) 
831         nq = *w;
832     else {
833         int stars = 0;
834         /* Scheduled supernova -- select star */
835         /* logic changed here so that we won't favor quadrants in top
836            left of universe */
837         for (nq.x = 1; nq.x <= GALSIZE; nq.x++)
838             for (nq.y = 1; nq.y <= GALSIZE; nq.y++)
839                 stars += game.state.galaxy[nq.x][nq.y].stars;
840         if (stars == 0)
841             return; /* nothing to supernova exists */
842         num = Rand()*stars + 1;
843         for (nq.x = 1; nq.x <= GALSIZE; nq.x++) {
844             for (nq.y = 1; nq.y <= GALSIZE; nq.y++) {
845                 num -= game.state.galaxy[nq.x][nq.y].stars;
846                 if (num <= 0)
847                     break;
848             }
849             if (num <=0)
850                 break;
851         }
852         if (idebug) {
853             proutn("=== Super nova here?");
854             if (ja() == true)
855                 nq = game.quadrant;
856         }
857     }
858
859     if (!same(nq, game.quadrant) || game.justin) {
860         /* it isn't here, or we just entered (treat as enroute) */
861         if (!damaged(DRADIO) || game.condition == docked) {
862             skip(1);
863             prout(_("Message from Starfleet Command       Stardate %.2f"), game.state.date);
864             prout(_("     Supernova in %s; caution advised."),
865                   cramlc(quadrant, nq));
866         }
867     }
868     else {
869         coord ns;
870         /* we are in the quadrant! */
871         num = Rand()* game.state.galaxy[nq.x][nq.y].stars + 1;
872         for (ns.x = 1; ns.x <= QUADSIZE; ns.x++) {
873             for (ns.y = 1; ns.y <= QUADSIZE; ns.y++) {
874                 if (game.quad[ns.x][ns.y]==IHSTAR) {
875                     num--;
876                     if (num==0)
877                         break;
878                 }
879             }
880             if (num==0)
881                 break;
882         }
883
884         skip(1);
885         prouts(_("***RED ALERT!  RED ALERT!"));
886         skip(1);
887         prout(_("***Incipient supernova detected at %s"), cramlc(sector, ns));
888         if (square(ns.x-game.sector.x) + square(ns.y-game.sector.y) <= 2.1) {
889             proutn(_("Emergency override attempts t"));
890             prouts("***************");
891             skip(1);
892             stars();
893             game.alldone = true;
894         }
895     }
896
897     /* destroy any Klingons in supernovaed quadrant */
898     kldead = game.state.galaxy[nq.x][nq.y].klingons;
899     game.state.galaxy[nq.x][nq.y].klingons = 0;
900     if (same(nq, game.state.kscmdr)) {
901         /* did in the Supercommander! */
902         game.state.nscrem = game.state.kscmdr.x = game.state.kscmdr.y = game.isatb =  0;
903         game.iscate = false;
904         unschedule(FSCMOVE);
905     }
906     if (same(nq, game.battle)) {
907         unschedule(FSCDBAS);
908         unschedule(FCDBAS);
909         invalidate(game.battle);
910     }
911     if (game.state.remcom) {
912         int maxloop = game.state.remcom, l;
913         for (l = 1; l <= maxloop; l++) {
914             if (same(game.state.kcmdr[l], nq)) {
915                 game.state.kcmdr[l] = game.state.kcmdr[game.state.remcom];
916                 invalidate(game.state.kcmdr[game.state.remcom]);
917                 game.state.remcom--;
918                 kldead--;
919                 if (game.state.remcom==0)
920                     unschedule(FTBEAM);
921                 break;
922             }
923         }
924     }
925     game.state.remkl -= kldead;
926     /* destroy Romulans and planets in supernovaed quadrant */
927     nrmdead = game.state.galaxy[nq.x][nq.y].romulans;
928     game.state.galaxy[nq.x][nq.y].romulans = 0;
929     game.state.nromrem -= nrmdead;
930     /* Destroy planets */
931     for (loop = 0; loop < game.inplan; loop++) {
932         if (same(game.state.planets[loop].w, nq)) {
933             game.state.planets[loop].pclass = destroyed;
934             npdead++;
935         }
936     }
937     /* Destroy any base in supernovaed quadrant */
938     if (game.state.rembase) {
939         int maxloop = game.state.rembase, loop;
940         for (loop = 1; loop <= maxloop; loop++)
941             if (same(game.state.baseq[loop], nq)) {
942                 game.state.baseq[loop] = game.state.baseq[game.state.rembase];
943                 invalidate(game.state.baseq[game.state.rembase]);
944                 game.state.rembase--;
945                 break;
946             }
947     }
948     /* If starship caused supernova, tally up destruction */
949     if (induced) {
950         game.state.starkl += game.state.galaxy[nq.x][nq.y].stars;
951         game.state.basekl += game.state.galaxy[nq.x][nq.y].starbase;
952         game.state.nplankl += npdead;
953     }
954     /* mark supernova in galaxy and in star chart */
955     if (same(game.quadrant, nq) || !damaged(DRADIO) || game.condition == docked)
956         game.state.galaxy[nq.x][nq.y].supernova = true;
957     /* If supernova destroys last Klingons give special message */
958     if ((game.state.remkl + game.state.remcom + game.state.nscrem)==0 && !same(nq, game.quadrant)) {
959         skip(2);
960         if (!induced)
961             prout(_("Lucky you!"));
962         proutn(_("A supernova in %s has just destroyed the last Klingons."),
963                cramlc(quadrant, nq));
964         finish(FWON);
965         return;
966     }
967     /* if some Klingons remain, continue or die in supernova */
968     if (game.alldone)
969         finish(FSNOVAED);
970     return;
971 }