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