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