Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. #include "sst.h"
  2. #include <math.h>
  3.  
  4. void events(void) {
  5.  
  6.         int ictbeam=0, ipage=0, istract=0, line, i, j, k, l, ixhold, iyhold;
  7.         double fintim = d.date + Time, datemin, xtime, repair, yank;
  8.        
  9.  
  10. #ifdef DEBUG
  11.         if (idebug) prout("EVENTS");
  12. #endif
  13.  
  14.         if (stdamtim == 1e30 && !REPORTS)
  15.         {
  16.                 /* chart will no longer be updated because radio is dead */
  17.                 stdamtim = d.date;
  18.                 for (i=1; i <= 8 ; i++)
  19.                         for (j=1; j <= 8; j++)
  20.                                 if (starch[i][j] == 1) starch[i][j] = d.galaxy[i][j]+1000;
  21.         }
  22.  
  23.         for (;;) {
  24.                 /* Select earliest extraneous event, line==0 if no events */
  25.                 line = FSPY;
  26.                 if (alldone) return;
  27.                 datemin = fintim;
  28.                 for (l=1; l<=NEVENTS; l++)
  29.                         if (future[l] <= datemin) {
  30.                                 line = l;
  31.                                 datemin = future[l];
  32.                         }
  33.                 xtime = datemin-d.date;
  34. #ifdef CLOAKING
  35.                 if (iscloaking) {
  36.                         energy -= xtime*500.0;
  37.                         if (energy <= 0.) {
  38.                                 finish(FNRG);
  39.                                 return;
  40.                         }
  41.                 }
  42. #endif
  43.                 d.date = datemin;
  44.                 /* Decrement Federation resources and recompute remaining time */
  45.                 d.remres -= (d.remkl+4*d.remcom)*xtime;
  46.                 d.remtime = d.remres/(d.remkl+4*d.remcom);
  47.                 if (d.remtime <=0) {
  48.                         finish(FDEPLETE);
  49.                         return;
  50.                 }
  51.                 /* Is life support adequate? */
  52.                 if (damage[DLIFSUP] && condit != IHDOCKED) {
  53.                         if (lsupres < xtime && damage[DLIFSUP] > lsupres) {
  54.                                 finish(FLIFESUP);
  55.                                 return;
  56.                         }
  57.                         lsupres -= xtime;
  58.                         if (damage[DLIFSUP] <= xtime) lsupres = inlsr;
  59.                 }
  60.                 /* Fix devices */
  61.                 repair = xtime;
  62.                 if (condit == IHDOCKED) repair /= docfac;
  63.                 /* Don't fix Deathray here */
  64.                 for (l=1; l<=ndevice; l++)
  65.                         if (damage[l] > 0.0 && l != DDRAY)
  66.                 damage[l] -= (damage[l]-repair > 0.0 ? repair : damage[l]);
  67.         /* Fix Deathray if docked */
  68.         if (damage[DDRAY] > 0.0 && condit == IHDOCKED)
  69.             damage[DDRAY] -= (damage[l] - xtime > 0.0 ? xtime : damage[DDRAY]);
  70.                 /* If radio repaired, update star chart and attack reports */
  71.                 if (stdamtim != 1e30 && REPORTS) {
  72.                         stdamtim = 1e30;
  73.                         prout("Lt. Uhura- \"Captain, the sub-space radio is working and");
  74.                         prout("   surveillance reports are coming in.");
  75.                         skip(1);
  76.                         for (i=1; i <= 8 ; i++)
  77.                                 for (j=1; j <= 8; j++)
  78.                                         if (starch[i][j] > 999) starch[i][j] = 1;
  79.                         if (iseenit==0) {
  80.                                 attakreport();
  81.                                 iseenit = 1;
  82.                         }
  83.                         skip(1);
  84.                         prout("   The star chart is now up to date.\"");
  85.                         skip(1);
  86.                 }
  87.                 /* Cause extraneous event LINE to occur */
  88.                 Time -= xtime;
  89.                 switch (line) {
  90.                         case FSNOVA: /* Supernova */
  91.                                 if (ipage==0) pause(1);
  92.                                 ipage=1;
  93.                                 snova(0,0);
  94.                                 future[FSNOVA] = d.date + expran(0.5*intime);
  95.                                 if (d.galaxy[quadx][quady] == 1000) return;
  96.                                 break;
  97.                         case FSPY: /* Check with spy to see if S.C. should tractor beam */
  98.                 if (d.nscrem == 0 ||
  99. #ifdef CLOAKING
  100.                       iscloaked ||  /* Cannot tractor beam if we can't be seen! */
  101. #endif
  102.                                         ictbeam+istract > 0 ||
  103.                                         condit==IHDOCKED || isatb==1 || iscate==1) return;
  104.                                 if (ientesc ||
  105.                                         (energy < 2000 && torps < 4 && shield < 1250) ||
  106.                                         (damage[DPHASER]>0 && (damage[DPHOTON]>0 || torps < 4)) ||
  107.                                         (damage[DSHIELD] > 0 &&
  108.                                          (energy < 2500 || damage[DPHASER] > 0) &&
  109.                                          (torps < 5 || damage[DPHOTON] > 0))) {
  110.                                         /* Tractor-beam her! */
  111.                                         istract=1;
  112.                                         yank = square(d.isx-quadx) + square(d.isy-quady);
  113.                                         /*********TBEAM CODE***********/
  114.                                 }
  115.                                 else return;
  116.                         case FTBEAM: /* Tractor beam */
  117.                                 if (line==FTBEAM) {
  118.                                         if (d.remcom == 0) {
  119.                                                 future[FTBEAM] = 1e30;
  120.                                                 break;
  121.                                         }
  122.                                         i = Rand()*d.remcom+1.0;
  123.                                         yank = square(d.cx[i]-quadx) + square(d.cy[i]-quady);
  124.                     if (istract || condit == IHDOCKED ||
  125. #ifdef CLOAKING
  126.                           iscloaked || /* cannot tractor beam if we can't be seen */
  127. #endif
  128.                           yank == 0) {
  129.                                                 /* Drats! Have to reschedule */
  130.                                                 future[FTBEAM] = d.date + Time +
  131.                                                                                  expran(1.5*intime/d.remcom);
  132.                                                 break;
  133.                                         }
  134.                                 }
  135.                                 /* tractor beaming cases merge here */
  136.                                 yank = sqrt(yank);
  137.                                 if (ipage==0) pause(1);
  138.                                 ipage=1;
  139.                                 Time = (10.0/(7.5*7.5))*yank; /* 7.5 is yank rate (warp 7.5) */
  140.                                 ictbeam = 1;
  141.                                 skip(1);
  142.                                 proutn("***");
  143.                                 crmshp();
  144.                                 prout(" caught in long range tractor beam--");
  145.                                 /* If Kirk & Co. screwing around on planet, handle */
  146.                                 atover(1); /* atover(1) is Grab */
  147.                                 if (alldone) return;
  148.                                 if (icraft == 1) { /* Caught in Galileo? */
  149.                                         finish(FSTRACTOR);
  150.                                         return;
  151.                                 }
  152.                                 /* Check to see if shuttle is aboard */
  153.                                 if (iscraft==0) {
  154.                                         skip(1);
  155.                                         if (Rand() >0.5) {
  156.                                                 prout("Galileo, left on the planet surface, is captured");
  157.                                                 prout("by aliens and made into a flying McDonald's.");
  158.                                                 damage[DSHUTTL] = -10;
  159.                                                 iscraft = -1;
  160.                                         }
  161.                                         else {
  162.                                                 prout("Galileo, left on the planet surface, is well hidden.");
  163.                                         }
  164.                                 }
  165.                                 if (line==0) {
  166.                                         quadx = d.isx;
  167.                                         quady = d.isy;
  168.                                 }
  169.                                 else {
  170.                                         quadx = d.cx[i];
  171.                                         quady = d.cy[i];
  172.                                 }
  173.                                 iran10(&sectx, &secty);
  174.                                 crmshp();
  175.                                 proutn(" is pulled to");
  176.                                 cramlc(1, quadx, quady);
  177.                                 proutn(", ");
  178.                                 cramlc(2, sectx, secty);
  179.                                 skip(1);
  180.                                 if (resting) {
  181.                                         prout("(Remainder of rest/repair period cancelled.)");
  182.                                         resting = 0;
  183.                                 }
  184.                                 if (shldup==0) {
  185.                                         if (damage[DSHIELD]==0 && shield > 0) {
  186.                                                 sheild(2); /* Shldsup */
  187.                                                 shldchg=0;
  188.                                         }
  189.                                         else prout("(Shields not currently useable.)");
  190.                                 }
  191.                                 newqad(0);
  192.                                 /* Adjust finish time to time of tractor beaming */
  193.                                 fintim = d.date+Time;
  194.                                 if (d.remcom <= 0) future[FTBEAM] = 1e30;
  195.                                 else future[FTBEAM] = d.date+Time+expran(1.5*intime/d.remcom);
  196.                                 break;
  197.                         case FSNAP: /* Snapshot of the universe (for time warp) */
  198.                                 snapsht = d;
  199.                                 d.snap = 1;
  200.                                 future[FSNAP] = d.date + expran(0.5 * intime);
  201.                                 break;
  202.                         case FBATTAK: /* Commander attacks starbase */
  203.                                 if (d.remcom==0 || d.rembase==0) {
  204.                                         /* no can do */
  205.                                         future[FBATTAK] = future[FCDBAS] = 1e30;
  206.                                         break;
  207.                                 }
  208.                                 i = 0;
  209.                                 for (j=1; j<=d.rembase; j++) {
  210.                                         for (k=1; k<=d.remcom; k++)
  211.                                                 if (d.baseqx[j]==d.cx[k] && d.baseqy[j]==d.cy[k] &&
  212.                                                         (d.baseqx[j]!=quadx || d.baseqy[j]!=quady) &&
  213.                                                         (d.baseqx[j]!=d.isx || d.baseqy[j]!=d.isy)) {
  214.                                                         i = 1;
  215.                                                         break;
  216.                                                 }
  217.                                         if (i == 1) break;
  218.                                 }
  219.                                 if (j>d.rembase) {
  220.                                         /* no match found -- try later */
  221.                                         future[FBATTAK] = d.date + expran(0.3*intime);
  222.                                         future[FCDBAS] = 1e30;
  223.                                         break;
  224.                                 }
  225.                                 /* commander + starbase combination found -- launch attack */
  226.                                 batx = d.baseqx[j];
  227.                                 baty = d.baseqy[j];
  228.                                 future[FCDBAS] = d.date+1.0+3.0*Rand();
  229.                                 if (isatb) /* extra time if SC already attacking */
  230.                                         future[FCDBAS] += future[FSCDBAS]-d.date;
  231.                                 future[FBATTAK] = future[FCDBAS] +expran(0.3*intime);
  232.                                 iseenit = 0;
  233.                                 if (!REPORTS)
  234.                                      break; /* No warning :-( */
  235.                                 iseenit = 1;
  236.                                 if (ipage==0) pause(1);
  237.                                 ipage = 1;
  238.                                 skip(1);
  239.                                 proutn("Lt. Uhura-  \"Captain, the starbase in");
  240.                                 cramlc(1, batx, baty);
  241.                                 skip(1);
  242.                                 prout("   reports that it is under atttack and that it can");
  243.                                 proutn("   hold out only until stardate ");
  244.                                 cramf(future[FCDBAS],1,1);
  245.                                 prout(".\"");
  246.                                 if (resting) {
  247.                                         skip(1);
  248.                                         proutn("Mr. Spock-  \"Captain, shall we cancel the rest period?\"");
  249.                                         if (ja()) {
  250.                                                 resting = 0;
  251.                                                 Time = 0.0;
  252.                                                 return;
  253.                                         }
  254.                                 }
  255.                                 break;
  256.                         case FSCDBAS: /* Supercommander destroys base */
  257.                                 future[FSCDBAS] = 1e30;
  258.                                 isatb = 2;
  259.                                 if (d.galaxy[d.isx][d.isy]%100 < 10) break; /* WAS RETURN! */
  260.                                 ixhold = batx;
  261.                                 iyhold = baty;
  262.                                 batx = d.isx;
  263.                                 baty = d.isy;
  264.                         case FCDBAS: /* Commander succeeds in destroying base */
  265.                                 if (line==FCDBAS) {
  266.                                         future[FCDBAS] = 1e30;
  267.                                         /* find the lucky pair */
  268.                                         for (i = 1; i <= d.remcom; i++)
  269.                                                 if (d.cx[i]==batx && d.cy[i]==baty) break;
  270.                                         if (i > d.remcom || d.rembase == 0 ||
  271.                                                 d.galaxy[batx][baty] % 100 < 10) {
  272.                                                 /* No action to take after all */
  273.                                                 batx = baty = 0;
  274.                                                 break;
  275.                                         }
  276.                                 }
  277.                                 /* Code merges here for any commander destroying base */
  278.                                 /* Not perfect, but will have to do */
  279.                                 if (starch[batx][baty] == -1) starch[batx][baty] = 0;
  280.                                 /* Handle case where base is in same quadrant as starship */
  281.                                 if (batx==quadx && baty==quady) {
  282.                                         if (starch[batx][baty] > 999) starch[batx][baty] -= 10;
  283.                                         quad[basex][basey]= IHDOT;
  284.                                         basex=basey=0;
  285.                                         newcnd();
  286.                                         skip(1);
  287.                                         prout("Spock-  \"Captain, I believe the starbase has been destroyed.\"");
  288.                                 }
  289.                                 else if (d.rembase != 1 && REPORTS) {
  290.                                         /* Get word via subspace radio */
  291.                                         if (ipage==0) pause(1);
  292.                                         ipage = 1;
  293.                                         skip(1);
  294.                                         prout("Lt. Uhura-  \"Captain, Starfleet Command reports that");
  295.                                         proutn("   the starbase in");
  296.                                         cramlc(1, batx, baty);
  297.                                         prout(" has been destroyed by");
  298.                                         if (isatb==2) prout("the Klingon Super-Commander");
  299.                                         else prout("a Klingon Commander");
  300.                                 }
  301.                                 /* Remove Starbase from galaxy */
  302.                                 d.galaxy[batx][baty] -= 10;
  303.                                 for (i=1; i <= d.rembase; i++)
  304.                                         if (d.baseqx[i]==batx && d.baseqy[i]==baty) {
  305.                                                 d.baseqx[i]=d.baseqx[d.rembase];
  306.                                                 d.baseqy[i]=d.baseqy[d.rembase];
  307.                                         }
  308.                                 d.rembase--;
  309.                                 if (isatb == 2) {
  310.                                         /* reinstate a commander's base attack */
  311.                                         batx = ixhold;
  312.                                         baty = iyhold;
  313.                                         isatb = 0;
  314.                                 }
  315.                                 else {
  316.                                         batx = baty = 0;
  317.                                 }
  318.                                 break;
  319.                         case FSCMOVE: /* Supercommander moves */
  320.                                 future[FSCMOVE] = d.date+0.2777;
  321.                                 if (ientesc+istract==0 &&
  322.                                         isatb!=1 &&
  323.                                         (iscate!=1 || justin==1)) scom(&ipage);
  324.                                 break;
  325.                         case FDSPROB: /* Move deep space probe */
  326.                                 future[FDSPROB] = d.date + 0.01;
  327.                                 probex += probeinx;
  328.                                 probey += probeiny;
  329.                                 i = (int)(probex/10 +0.05);
  330.                                 j = (int)(probey/10 + 0.05);
  331.                                 if (probecx != i || probecy != j) {
  332.                                         probecx = i;
  333.                                         probecy = j;
  334.                                         if (i < 1 || i > 8 || j < 1 || j > 8 ||
  335.                                                 d.galaxy[probecx][probecy] == 1000) {
  336.                                                 // Left galaxy or ran into supernova
  337.                                                 if (REPORTS) {
  338.                                                         if (ipage==0) pause(1);
  339.                                                         ipage = 1;
  340.                                                         skip(1);
  341.                                                         proutn("Lt. Uhura-  \"The deep space probe ");
  342.                                                         if (i < 1 ||i > 8 || j < 1 || j > 8)
  343.                                                                 proutn("has left the galaxy");
  344.                                                         else
  345.                                                                 proutn("is no longer transmitting");
  346.                                                         prout(".\"");
  347.                                                 }
  348.                                                 future[FDSPROB] = 1e30;
  349.                                                 break;
  350.                                         }
  351.                                         if (REPORTS) {
  352.                                                 if (ipage==0) pause(1);
  353.                                                 ipage = 1;
  354.                                                 skip(1);
  355.                                                 proutn("Lt. Uhura-  \"The deep space probe is now in ");
  356.                                                 cramlc(1, probecx, probecy);
  357.                                                 prout(".\"");
  358.                                         }
  359.                                 }
  360.                                 /* Update star chart if Radio is working or have access to
  361.                                    radio. */
  362.                                 if (REPORTS)
  363.                                         starch[probecx][probecy] = damage[DRADIO] > 0.0 ?
  364.                                                                     d.galaxy[probecx][probecy]+1000 : 1;
  365.                                 proben--; // One less to travel
  366.                                 if (proben == 0 && isarmed &&
  367.                                         d.galaxy[probecx][probecy] % 10 > 0) {
  368.                                         /* lets blow the sucker! */
  369.                                         snova(1,0);
  370.                                         future[FDSPROB] = 1e30;
  371.                                         if (d.galaxy[quadx][quady] == 1000) return;
  372.                                 }
  373.                                 break;
  374.                 }
  375.         }
  376. }
  377.  
  378.                                
  379. void waiting(void) {
  380.         int key;
  381.         double temp, delay, origTime;
  382.  
  383.         ididit = 0;
  384.         for (;;) {
  385.                 key = scan();
  386.                 if (key  != IHEOL) break;
  387.                 proutn("How long? ");
  388.         }
  389.         chew();
  390.         if (key != IHREAL) {
  391.                 huh();
  392.                 return;
  393.         }
  394.         origTime = delay = aaitem;
  395.         if (delay <= 0.0) return;
  396.         if (delay >= d.remtime || nenhere != 0) {
  397.                 prout("Are you sure? ");
  398.                 if (ja() == 0) return;
  399.         }
  400.  
  401.         /* Alternate resting periods (events) with attacks */
  402.  
  403.         resting = 1;
  404.         do {
  405.                 if (delay <= 0) resting = 0;
  406.                 if (resting == 0) {
  407.                         cramf(d.remtime, 0, 2);
  408.                         prout(" stardates left.");
  409.                         return;
  410.                 }
  411.                 temp = Time = delay;
  412.  
  413.                 if (nenhere) {
  414.                         double rtime = 1.0 + Rand();
  415.                         if (rtime < temp) temp = rtime;
  416.                         Time = temp;
  417.                 }
  418.                 if (Time < delay) attack(0);
  419.                 if (nenhere==0) movetho();
  420.                 if (alldone) return;
  421.                 events();
  422.                 ididit = 1;
  423.                 if (alldone) return;
  424.                 delay -= temp;
  425.         } while (d.galaxy[quadx][quady] != 1000); // leave if quadrant supernovas
  426.  
  427.         resting = 0;
  428.         Time = 0;
  429. }
  430.  
  431. void nova(int ix, int iy) {
  432.         static double course[] =
  433.                 {0.0, 10.5, 12.0, 1.5, 9.0, 0.0, 3.0, 7.5, 6.0, 4.5};
  434.         int bot, top, top2, burst, hits[11][3], kount, icx, icy, mm, nn, j;
  435.         int iquad, iquad1, i, ll, newcx, newcy, ii, jj;
  436.         if (Rand() < 0.05) {
  437.                 /* Wow! We've supernova'ed */
  438.                 snova(ix, iy);
  439.                 return;
  440.         }
  441.  
  442.         /* handle initial nova */
  443.         quad[ix][iy] = IHDOT;
  444.         crmena(1, IHSTAR, 2, ix, iy);
  445.         prout(" novas.");
  446.         d.galaxy[quadx][quady] -= 1;
  447.         d.starkl++;
  448.        
  449.         /* Set up stack to recursively trigger adjacent stars */
  450.         bot = top = top2 = 1;
  451.         kount = 0;
  452.         icx = icy = 0;
  453.         hits[1][1] = ix;
  454.         hits[1][2] = iy;
  455.         while (1) {
  456.                 for (mm = bot; mm <= top; mm++)
  457.                 for (nn = 1; nn <= 3; nn++)  /* nn,j represents coordinates around current */
  458.                         for (j = 1; j <= 3; j++) {
  459.                                 if (j==2 && nn== 2) continue;
  460.                                 ii = hits[mm][1]+nn-2;
  461.                                 jj = hits[mm][2]+j-2;
  462.                                 if (ii < 1 || ii > 10 || jj < 1 || jj > 10) continue;
  463.                                 iquad = quad[ii][jj];
  464.                                 switch (iquad) {
  465. //                                      case IHDOT:     /* Empty space ends reaction
  466. //                                      case IHQUEST:
  467. //                                      case IHBLANK:
  468. //                                      case IHT:
  469. //                                      case IHWEB:
  470.                                         default:
  471.                                                 break;
  472.                                         case IHSTAR: /* Affect another star */
  473.                                                 if (Rand() < 0.05) {
  474.                                                         /* This star supernovas */
  475.                                                         snova(ii,jj);
  476.                                                         return;
  477.                                                 }
  478.                                                 top2++;
  479.                                                 hits[top2][1]=ii;
  480.                                                 hits[top2][2]=jj;
  481.                                                 d.galaxy[quadx][quady] -= 1;
  482.                                                 d.starkl++;
  483.                                                 crmena(1, IHSTAR, 2, ii, jj);
  484.                                                 prout(" novas.");
  485.                                                 quad[ii][jj] = IHDOT;
  486.                                                 break;
  487.                                         case IHP: /* Destroy planet */
  488.                                                 d.newstuf[quadx][quady] -= 1;
  489.                                                 d.nplankl++;
  490.                                                 crmena(1, IHP, 2, ii, jj);
  491.                                                 prout(" destroyed.");
  492.                                                 d.plnets[iplnet] = nulplanet;
  493.                                                 iplnet = plnetx = plnety = 0;
  494.                                                 if (landed == 1) {
  495.                                                         finish(FPNOVA);
  496.                                                         return;
  497.                                                 }
  498.                                                 quad[ii][jj] = IHDOT;
  499.                                                 break;
  500.                                         case IHB: /* Destroy base */
  501.                                                 d.galaxy[quadx][quady] -= 10;
  502.                                                 for (i = 1; i <= d.rembase; i++)
  503.                                                         if (d.baseqx[i]==quadx && d.baseqy[i]==quady) break;
  504.                                                 d.baseqx[i] = d.baseqx[d.rembase];
  505.                                                 d.baseqy[i] = d.baseqy[d.rembase];
  506.                                                 d.rembase--;
  507.                                                 basex = basey = 0;
  508.                                                 d.basekl++;
  509.                                                 newcnd();
  510.                                                 crmena(1, IHB, 2, ii, jj);
  511.                                                 prout(" destroyed.");
  512.                                                 quad[ii][jj] = IHDOT;
  513.                                                 break;
  514.                                         case IHE: /* Buffet ship */
  515.                                         case IHF:
  516.                                                 prout("***Starship buffeted by nova.");
  517.                                                 if (shldup) {
  518.                                                         if (shield >= 2000.0) shield -= 2000.0;
  519.                                                         else {
  520.                                                                 double diff = 2000.0 - shield;
  521.                                                                 energy -= diff;
  522.                                                                 shield = 0.0;
  523.                                                                 shldup = 0;
  524.                                                                 prout("***Shields knocked out.");
  525.                                                                 damage[DSHIELD] += 0.005*damfac*Rand()*diff;
  526.                                                         }
  527.                                                 }
  528.                                                 else energy -= 2000.0;
  529.                                                 if (energy <= 0) {
  530.                                                         finish(FNOVA);
  531.                                                         return;
  532.                                                 }
  533.                                                 /* add in course nova contributes to kicking starship*/
  534.                                                 icx += sectx-hits[mm][1];
  535.                                                 icy += secty-hits[mm][2];
  536.                                                 kount++;
  537.                                                 break;
  538.                                         case IHK: /* kill klingon */
  539.                                                 deadkl(ii,jj,iquad, ii, jj);
  540.                                                 break;
  541.                                         case IHC: /* Damage/destroy big enemies */
  542.                                         case IHS:
  543.                                         case IHR:
  544.                                                 for (ll = 1; ll <= nenhere; ll++)
  545.                                                         if (kx[ll]==ii && ky[ll]==jj) break;
  546.                                                 kpower[ll] -= 800.0; /* If firepower is lost, die */
  547.                                                 if (kpower[ll] <= 0.0) {
  548.                                                         deadkl(ii, jj, iquad, ii, jj);
  549.                                                         break;
  550.                                                 }
  551.                                                 newcx = ii + ii - hits[mm][1];
  552.                                                 newcy = jj + jj - hits[mm][2];
  553.                                                 crmena(1, iquad, 2, ii, jj);
  554.                                                 proutn(" damaged");
  555.                                                 if (newcx<1 || newcx>10 || newcy<1 || newcy>10) {
  556.                                                         /* can't leave quadrant */
  557.                                                         skip(1);
  558.                                                         break;
  559.                                                 }
  560.                                                 iquad1 = quad[newcx][newcy];
  561.                                                 if (iquad1 == IHBLANK) {
  562.                                                         proutn(", blasted into ");
  563.                                                         crmena(0, IHBLANK, 2, newcx, newcy);
  564.                                                         skip(1);
  565.                                                         deadkl(ii, jj, iquad, newcx, newcy);
  566.                                                         break;
  567.                                                 }
  568.                                                 if (iquad1 != IHDOT) {
  569.                                                         /* can't move into something else */
  570.                                                         skip(1);
  571.                                                         break;
  572.                                                 }
  573.                                                 proutn(", buffeted to");
  574.                                                 cramlc(2, newcx, newcy);
  575.                                                 quad[ii][jj] = IHDOT;
  576.                                                 quad[newcx][newcy] = iquad;
  577.                                                 kx[ll] = newcx;
  578.                                                 ky[ll] = newcy;
  579.                                                 kavgd[ll] = sqrt(square(sectx-newcx)+square(secty-newcy));
  580.                                                 kdist[ll] = kavgd[ll];
  581.                                                 skip(1);
  582.                                                 break;
  583.                                 }
  584.                         }
  585.                 if (top == top2) break;
  586.                 bot = top + 1;
  587.                 top = top2;
  588.         }
  589.         if (kount==0) return;
  590.  
  591.         /* Starship affected by nova -- kick it away. */
  592.         dist = kount*0.1;
  593.         if (icx) icx = (icx < 0 ? -1 : 1);
  594.         if (icy) icy = (icy < 0 ? -1 : 1);
  595.         direc = course[3*(icx+1)+icy+2];
  596.         if (direc == 0.0) dist = 0.0;
  597.         if (dist == 0.0) return;
  598.         Time = 10.0*dist/16.0;
  599.         skip(1);
  600.         prout("Force of nova displaces starship.");
  601.         iattak=2;       /* Eliminates recursion problem */
  602.         lmove();
  603.         Time = 10.0*dist/16.0;
  604.         return;
  605. }
  606.        
  607.        
  608. void snova(int insx, int insy) {
  609.         int comdead, nqx, nqy, nsx, nsy, num, kldead, iscdead;
  610.         int nrmdead, npdead;
  611.         int insipient=0;
  612.  
  613.         nsx = insx;
  614.         nsy = insy;
  615.  
  616.         if (insy== 0) {
  617.                 if (insx == 1) {
  618.                         /* NOVAMAX being used */
  619.                         nqx = probecx;
  620.                         nqy = probecy;
  621.                 }
  622.                 else {
  623.                         int stars = 0;
  624.                         /* Scheduled supernova -- select star */
  625.                         /* logic changed here so that we won't favor quadrants in top
  626.                         left of universe */
  627.                         for (nqx = 1; nqx<=8; nqx++) {
  628.                                 for (nqy = 1; nqy<=8; nqy++) {
  629.                                         stars += d.galaxy[nqx][nqy] % 10;
  630.                                 }
  631.                         }
  632.                         if (stars == 0) return; /* nothing to supernova exists */
  633.                         num = Rand()*stars + 1;
  634.                         for (nqx = 1; nqx<=8; nqx++) {
  635.                                 for (nqy = 1; nqy<=8; nqy++) {
  636.                                         num -= d.galaxy[nqx][nqy] % 10;
  637.                                         if (num <= 0) break;
  638.                                 }
  639.                                 if (num <=0) break;
  640.                         }
  641. #ifdef DEBUG
  642.                         if (idebug) {
  643.                                 proutn("Super nova here?");
  644.                                 if (ja()==1) {
  645.                                         nqx = quadx;
  646.                                         nqy = quady;
  647.                                 }
  648.                         }
  649. #endif
  650.                 }
  651.  
  652.                 if (nqx != quady || nqy != quady || justin != 0) {
  653.                         /* it isn't here, or we just entered (treat as inroute) */
  654.                         if (REPORTS) {
  655.                                 skip(1);
  656.                                 proutn("Message from Starfleet Command       Stardate ");
  657.                                 cramf(d.date, 0, 1);
  658.                                 skip(1);
  659.                                 proutn("     Supernova in");
  660.                                 cramlc(1, nqx, nqy);
  661.                                 prout("; caution advised.");
  662.                         }
  663.                 }
  664.                 else {
  665.                         /* we are in the quadrant! */
  666.                         insipient = 1;
  667.                         num = Rand()* (d.galaxy[nqx][nqy]%10) + 1;
  668.                         for (nsx=1; nsx < 10; nsx++) {
  669.                                 for (nsy=1; nsy < 10; nsy++) {
  670.                                         if (quad[nsx][nsy]==IHSTAR) {
  671.                                                 num--;
  672.                                                 if (num==0) break;
  673.                                         }
  674.                                 }
  675.                                 if (num==0) break;
  676.                         }
  677.                 }
  678.         }
  679.         else {
  680.                 insipient = 1;
  681.         }
  682.  
  683.         if (insipient) {
  684.                 skip(1);
  685.                 prouts("***RED ALERT!  RED ALERT!");
  686.                 skip(1);
  687.                 proutn("***Incipient supernova detected at");
  688.                 cramlc(2, nsx, nsy);
  689.                 skip(1);
  690.                 nqx = quadx;
  691.                 nqy = quady;
  692.                 if (square(nsx-sectx) + square(nsy-secty) <= 2.1) {
  693.                         proutn("Emergency override attempts t");
  694.                         prouts("***************");
  695.                         skip(1);
  696.                         stars();
  697.                         alldone=1;
  698.                 }
  699.         }
  700.         /* destroy any Klingons in supernovaed quadrant */
  701.         num=d.galaxy[nqx][nqy];
  702.     kldead = num/100;
  703.     d.remkl -= kldead; // Moved here to correctly set remaining Klingon count
  704.         comdead = iscdead = 0;
  705.         if (nqx==d.isx && nqy == d.isy) {
  706.                 /* did in the Supercommander! */
  707.                 d.nscrem = d.isx = d.isy = isatb = iscate = 0;
  708.                 iscdead = 1;
  709.                 kldead--; /* Get proper kill credit */
  710.                 future[FSCMOVE] = future[FSCDBAS] = 1e30;
  711.         }
  712.  
  713.     if (d.remcom) {
  714.                 int maxloop = d.remcom, l;
  715.                 for (l = 1; l <= maxloop; l++) {
  716.                         if (d.cx[l] == nqx && d.cy[l] == nqy) {
  717.                                 d.cx[l] = d.cx[d.remcom];
  718.                                 d.cy[l] = d.cy[d.remcom];
  719.                                 d.cx[d.remcom] = d.cy[d.remcom] = 0;
  720.                                 d.remcom--;
  721.                                 kldead--;
  722.                                 comdead++;
  723.                                 if (d.remcom==0) future[FTBEAM] = 1e30;
  724.                                 break;
  725.                         }
  726.                 }
  727.         }
  728.         /* destroy Romulans and planets in supernovaed quadrant */
  729.         num = d.newstuf[nqx][nqy];
  730.         d.newstuf[nqx][nqy] = 0;
  731.         nrmdead = num/10;
  732.         d.nromrem -= nrmdead;
  733.         npdead = num - nrmdead*10;
  734.         if (npdead) {
  735.                 int l;
  736.                 for (l = 1; l <= inplan; l++)
  737.                         if (d.plnets[l].x == nqx && d.plnets[l].y == nqy) {
  738.                                 d.plnets[l] = nulplanet;
  739.                         }
  740.         }
  741.         /* Destroy any base in supernovaed quadrant */
  742.         if (d.rembase) {
  743.                 int maxloop = d.rembase, l;
  744.                 for (l = 1; l <= maxloop; l++)
  745.                         if (d.baseqx[l]==nqx && d.baseqy[l]==nqy) {
  746.                                 d.baseqx[l] = d.baseqx[d.rembase];
  747.                                 d.baseqy[l] = d.baseqy[d.rembase];
  748.                                 d.baseqx[d.rembase] = d.baseqy[d.rembase] = 0;
  749.                                 d.rembase--;
  750.                                 break;
  751.                         }
  752.         }
  753.         /* If starship caused supernova, tally up destruction */
  754.         if (insx) {
  755.                 num = d.galaxy[nqx][nqy] % 100;
  756.                 d.starkl += num % 10;
  757.                 d.basekl += num/10;
  758.                 d.killk += kldead;
  759.                 d.killc += comdead;
  760.                 d.nromkl += nrmdead;
  761.                 d.nplankl += npdead;
  762.                 d.nsckill += iscdead;
  763.         }
  764.         /* mark supernova in galaxy and in star chart */
  765.         if ((quadx == nqx && quady == nqy) || REPORTS)
  766.                 starch[nqx][nqy] = 1;
  767.         d.galaxy[nqx][nqy] = 1000;
  768.         /* If supernova destroys last klingons give special message */
  769.         if (d.remkl==0 && (nqx != quadx || nqy != quady)) {
  770.                 skip(2);
  771.                 if (insx == 0) prout("Lucky you!");
  772.                 proutn("A supernova in");
  773.                 cramlc(1, nqx, nqy);
  774.                 prout(" has just destroyed the last Klingons.");
  775.                 finish(FWON);
  776.                 return;
  777.         }
  778.         /* if some Klingons remain, continue or die in supernova */
  779.         if (alldone) finish(FSNOVAED);
  780.         return;
  781. }
  782.                
  783.                                
  784.