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