Subversion Repositories Kolibri OS

Rev

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

  1. #ifndef KOS32
  2. #include <time.h>
  3. #endif
  4. #include "sst.h"
  5.  
  6. void prelim(void) {
  7.         skip(2);
  8.         prout("-SUPER- STAR TREK");
  9.         skip(1);
  10.         prout("Latest update-21 Sept 78");
  11.         skip(1);
  12. }
  13.  
  14. void freeze(int boss) {
  15.         char *x, *y;
  16.         FILE *fp;
  17.         int key;
  18.         if (boss) {
  19.                 strcpy(citem, "emsave.trk");
  20.         }
  21.         else {
  22.                 if ((key = scan()) == IHEOL) {
  23.                         proutn("File name(9 characters maximum): ");
  24.                         key = scan();
  25.                 }
  26.                 if (key != IHALPHA) {
  27.                         huh();
  28.                         return;
  29.                 }
  30.                 chew();
  31.                 if (strchr(citem, '.') == NULL) {
  32.                         strcat(citem, ".trk");
  33.                 }
  34.         }
  35.         if ((fp = fopen(citem, "wb")) == NULL) {
  36.                 proutn("Can't freeze game as file ");
  37.                 proutn(citem);
  38.                 skip(1);
  39.                 return;
  40.         }
  41.         fwrite(&d, sizeof(d), 1, fp);
  42.         fwrite(&snapsht, sizeof(snapsht), 1, fp);
  43.         fwrite(quad, sizeof(quad), 1, fp);
  44.         fwrite(kx, sizeof(kx), 1, fp);
  45.         fwrite(ky, sizeof(ky), 1, fp);
  46.         fwrite(starch, sizeof(starch), 1, fp);
  47.         fwrite(kpower, sizeof(kpower), 1, fp);
  48.         fwrite(kdist, sizeof(kdist), 1, fp);
  49.         fwrite(kavgd, sizeof(kavgd), 1, fp);
  50.         fwrite(damage, sizeof(damage), 1, fp);
  51.         fwrite(future, sizeof(future), 1, fp);
  52.         fwrite(&a, sizeof(a), 1, fp);
  53.         fwrite(passwd, sizeof(passwd), 1, fp);
  54.  
  55.         fclose(fp);
  56.  
  57.         /* I hope that's enough! */
  58. }
  59.  
  60.  
  61. void thaw(void) {
  62.         char *x, *y;
  63.         FILE *fp;
  64.         int key;
  65.  
  66.         passwd[0] = '\0';
  67.         if ((key = scan()) == IHEOL) {
  68.                 proutn("File name: ");
  69.                 key = scan();
  70.         }
  71.         if (key != IHALPHA) {
  72.                 huh();
  73.                 return;
  74.         }
  75.         chew();
  76.         if (strchr(citem, '.') == NULL) {
  77.                 strcat(citem, ".trk");
  78.         }
  79.         if ((fp = fopen(citem, "rb")) == NULL) {
  80.                 proutn("Can't find game file ");
  81.                 proutn(citem);
  82.                 skip(1);
  83.                 return;
  84.         }
  85.         fread(&d, sizeof(d), 1, fp);
  86.         fread(&snapsht, sizeof(snapsht), 1, fp);
  87.         fread(quad, sizeof(quad), 1, fp);
  88.         fread(kx, sizeof(kx), 1, fp);
  89.         fread(ky, sizeof(ky), 1, fp);
  90.         fread(starch, sizeof(starch), 1, fp);
  91.         fread(kpower, sizeof(kpower), 1, fp);
  92.         fread(kdist, sizeof(kdist), 1, fp);
  93.         fread(kavgd, sizeof(kavgd), 1, fp);
  94.         fread(damage, sizeof(damage), 1, fp);
  95.         fread(future, sizeof(future), 1, fp);
  96.         fread(&a, sizeof(a), 1, fp);
  97.         fread(passwd, sizeof(passwd), 1, fp);
  98.  
  99.         fclose(fp);
  100.  
  101.         /* I hope that's enough! */
  102. }
  103.  
  104. void abandn(void) {
  105.         int nb, l;
  106.  
  107.         chew();
  108.         if (condit==IHDOCKED) {
  109.                 if (ship!=IHE) {
  110.                         prout("You cannot abandon Ye Faerie Queene.");
  111.                         return;
  112.                 }
  113.         }
  114.         else {
  115.                 /* Must take shuttle craft to exit */
  116.                 if (damage[DSHUTTL]==-1) {
  117.                         prout("Ye Faerie Queene has no shuttle craft.");
  118.                         return;
  119.                 }
  120.                 if (damage[DSHUTTL]<0) {
  121.                         prout("Shuttle craft now serving Big Mac's.");
  122.                         return;
  123.                 }
  124.                 if (damage[DSHUTTL]>0) {
  125.                         prout("Shuttle craft damaged.");
  126.                         return;
  127.                 }
  128.                 if (landed==1) {
  129.                         prout("You must be aboard the Enterprise.");
  130.                         return;
  131.                 }
  132.                 if (iscraft!=1) {
  133.                         prout("Shuttle craft not currently available.");
  134.                         return;
  135.                 }
  136.                 /* Print abandon ship messages */
  137.                 skip(1);
  138.                 prouts("***ABANDON SHIP!  ABANDON SHIP!");
  139.                 skip(1);
  140.                 prouts("***ALL HANDS ABANDON SHIP!");
  141.                 skip(2);
  142.                 prout("Captain and crew escape in shuttle craft.");
  143.                 prout("Remainder of ship's complement beam down");
  144.                 prout("to nearest habitable planet.");
  145.                 if (d.rembase==0) {
  146.                         /* Ops! no place to go... */
  147.                         finish(FABANDN);
  148.                         return;
  149.                 }
  150.                 /* If at least one base left, give 'em the Faerie Queene */
  151.                 skip(1);
  152.                 icrystl = 0; /* crystals are lost */
  153.                 nprobes = 0; /* No probes */
  154.                 prout("You are captured by Klingons and released to");
  155.                 prout("the Federation in a prisoner-of-war exchange.");
  156.                 nb = Rand()*d.rembase+1;
  157.                 /* Set up quadrant and position FQ adjacient to base */
  158.                 if (quadx!=d.baseqx[nb] || quady!=d.baseqy[nb]) {
  159.                         quadx = d.baseqx[nb];
  160.                         quady = d.baseqy[nb];
  161.                         sectx = secty = 5;
  162.                         newqad(1);
  163.                 }
  164.                 for (;;) {
  165.                         /* position next to base by trial and error */
  166.                         quad[sectx][secty] = IHDOT;
  167.                         for (l = 1; l <= 10; l++) {
  168.                                 sectx = 3.0*Rand() - 1.0 + basex;
  169.                                 secty = 3.0*Rand() - 1.0 + basey;
  170.                                 if (sectx >= 1 && sectx <= 10 &&
  171.                                         secty >= 1 && secty <= 10 &&
  172.                                         quad[sectx][secty] == IHDOT) break;
  173.                         }
  174.                         if (l < 11) break; /* found a spot */
  175.                         sectx=5;
  176.                         secty=5;
  177.                         newqad(1);
  178.                 }
  179.         }
  180.         /* Get new commission */
  181.         quad[sectx][secty] = ship = IHF;
  182.         prout("Starfleet puts you in command of another ship,");
  183.         prout("the Faerie Queene, which is antiquated but,");
  184.         prout("still useable.");
  185.         if (icrystl!=0) prout("The dilithium crystals have been moved.");
  186.         imine=0;
  187.         iscraft=0; /* Gallileo disappears */
  188. #ifdef CAPTURE
  189.         brigcapacity = 300; // Less capacity now
  190.         brigfree = brigcapacity;
  191. #endif
  192. #ifdef CLOAKING
  193.     iscloaked = iscloaking = FALSE;
  194. #endif
  195.         /* Resupply ship */
  196.         condit=IHDOCKED;
  197.         for (l = 1; l <= ndevice; l++) damage[l] = 0.0;
  198.         damage[DSHUTTL] = -1;
  199.         energy = inenrg = 3000.0;
  200.         shield = inshld = 1250.0;
  201.         torps = intorps = 6;
  202.         lsupres=inlsr=3.0;
  203.         shldup=0;
  204.         warpfac=5.0;
  205.         wfacsq=25.0;
  206.         return;
  207. }
  208.        
  209. void setup(void) {
  210.         int i,j, krem, klumper;
  211.         int ix, iy;
  212.         alldone = gamewon = 0;
  213. #ifdef DEBUG
  214.         idebug = 0;
  215. #endif
  216.         //  Decide how many of everything
  217.         if (choose()) return; // frozen game
  218.         // Prepare the Enterprise
  219.         ship = IHE;
  220.         energy = inenrg = 5000.0;
  221.         shield = inshld = 2500.0;
  222.         shldchg = shldup = 0;
  223.         inlsr = 4.0;
  224.         lsupres = 4.0;
  225.         iran8(&quadx, &quady);
  226.         iran10(&sectx, &secty);
  227.         torps = intorps = 10;
  228.         nprobes = (int)(3.0*Rand() + 2.0);      /* Give them 2-4 of these wonders */
  229.         warpfac = 5.0;
  230.         wfacsq = warpfac * warpfac;
  231.         for (i=0; i <= ndevice; i++) damage[i] = 0.0;
  232.         // Set up assorted game parameters
  233.         batx = baty = 0;
  234.         d.date = indate = 100.0*(int)(31.0*Rand()+20.0);
  235.         d.killk = d.killc = nkinks = nhelp = resting = casual = d.nromkl = 0;
  236. // Added d.starkl=0 6/2015
  237.     isatb = iscate = imine = icrystl = icraft = d.nsckill = d.nplankl = d.starkl = 0;
  238.         iscraft = 1;
  239.         landed = -1;
  240.         alive = 1;
  241.         docfac = 0.25;
  242.         for (i = 1; i <= 8; i++)
  243.                 for (j = 1; j <= 8; j++) d.newstuf[i][j] = starch[i][j] = 0;
  244.         // Initialize times for extraneous events
  245.         future[FSNOVA] = d.date + expran(0.5 * intime);
  246.         future[FTBEAM] = d.date + expran(1.5 * (intime / d.remcom));
  247.         future[FSNAP] = d.date + 1.0 + Rand(); // Force an early snapshot
  248.         future[FBATTAK] = d.date + expran(0.3*intime);
  249.         future[FCDBAS] = 1e30;
  250.         future[FSCMOVE] = d.nscrem ? d.date+0.2777 : 1e30;
  251.         future[FSCDBAS] = 1e30;
  252.         future[FDSPROB] = 1e30;
  253.         // Starchart is functional
  254.         stdamtim = 1e30;
  255.         // Put stars in the galaxy
  256.         instar = 0;
  257.         for (i=1; i<=8; i++)
  258.                 for (j=1; j<=8; j++) {
  259.                         int k = Rand()*9.0 + 1.0;
  260.                         instar += k;
  261.                         d.galaxy[i][j] = k;
  262.                 }
  263.         // Locate star bases in galaxy
  264.         for (i = 1; i <= inbase; i++) {
  265.                 int contflag;
  266.                 do {
  267.                         do iran8(&ix, &iy);
  268.                         while (d.galaxy[ix][iy] >= 10);
  269.                         contflag = FALSE;
  270.                         for (j = i-1; j > 0; j--) {
  271.                                 /* Improved placement algorithm to spread out bases */
  272.                                 double distq = square(ix-d.baseqx[j]) + square(iy-d.baseqy[j]);
  273.                                 if (distq < 6.0*(6-inbase) && Rand() < 0.75) {
  274.                                         contflag = TRUE;
  275. #ifdef DEBUG
  276.                                         printf("DEBUG: Abandoning base #%d at %d-%d\n", i, ix, iy);
  277. #endif
  278.                                         break;
  279.                                 }
  280. #ifdef DEBUG
  281.                                 else if (distq < 6.0 * (6-inbase)) {
  282.                                         printf("DEBUG: saving base #%d, close to #%d\n", i, j);
  283.                                 }
  284. #endif
  285.                         }
  286.                 } while (contflag);
  287.                        
  288.                 d.baseqx[i] = ix;
  289.                 d.baseqy[i] = iy;
  290.                 starch[ix][iy] = -1;
  291.                 d.galaxy[ix][iy] += 10;
  292.         }
  293.         // Position ordinary Klingon Battle Cruisers
  294.         krem = inkling - incom - d.nscrem;
  295.         klumper = 0.25*skill*(9.0-length)+1.0;
  296.         if (klumper > 9) klumper = 9; // Can't have more than 9 in quadrant
  297.         do {
  298.                 double r = Rand();
  299.                 int klump = (1.0 - r*r)*klumper;
  300.                 if (klump > krem) klump = krem;
  301.                 krem -= klump;
  302.                 klump *= 100;
  303.                 do iran8(&ix, &iy);
  304.                 while (d.galaxy[ix][iy] + klump >= 1000);
  305.                 d.galaxy[ix][iy] += klump;
  306.         } while (krem > 0);
  307.         // Position Klingon Commander Ships
  308. #ifdef DEBUG
  309.         klumper = 1;
  310. #endif
  311.         for (i = 1; i <= incom; i++) {
  312.                 do {
  313.                         do { /* IF debugging, put commanders by bases, always! */
  314. #ifdef DEBUG
  315.                                 if (idebug && klumper <= inbase) {
  316.                                         ix = d.baseqx[klumper];
  317.                                         iy = d.baseqy[klumper];
  318.                                         klumper++;
  319.                                 }
  320.                                 else
  321. #endif
  322.                                         iran8(&ix, &iy);
  323.                         }
  324.                         while ((d.galaxy[ix][iy] < 99 && Rand() < 0.75)||
  325.                                    d.galaxy[ix][iy]>899);
  326.                         // check for duplicate
  327.                         for (j = 1; j < i; j++)
  328.                                 if (d.cx[j]==ix && d.cy[j]==iy) break;
  329.                 } while (j < i);
  330.                 d.galaxy[ix][iy] += 100;
  331.                 d.cx[i] = ix;
  332.                 d.cy[i] = iy;
  333.         }
  334.         // Locate planets in galaxy
  335.         for (i = 1; i <= inplan; i++) {
  336.                 do iran8(&ix, &iy);
  337.                 while (d.newstuf[ix][iy] > 0);
  338.                 d.newstuf[ix][iy] = 1;
  339.                 d.plnets[i].x = ix;
  340.                 d.plnets[i].y = iy;
  341.                 d.plnets[i].pclass = Rand()*3.0 + 1.0; // Planet class M N or O
  342.                 d.plnets[i].crystals = 1.5*Rand();              // 1 in 3 chance of crystals
  343.                 d.plnets[i].known = 0;
  344.         }
  345.         // Locate Romulans
  346.         for (i = 1; i <= d.nromrem; i++) {
  347.                 iran8(&ix, &iy);
  348.                 d.newstuf[ix][iy] += 10;
  349.         }
  350.         // Locate the Super Commander
  351.         if (d.nscrem > 0) {
  352.                 do iran8(&ix, &iy);
  353.                 while (d.galaxy[ix][iy] >= 900);
  354.                 d.isx = ix;
  355.                 d.isy = iy;
  356.                 d.galaxy[ix][iy] += 100;
  357.         }
  358.         // Place thing (in tournament game, thingx == -1, don't want one!)
  359.         if (Rand() < 0.1 && thingx != -1) {
  360.                 iran8(&thingx, &thingy);
  361.         }
  362.         else {
  363.                 thingx = thingy = 0;
  364.         }
  365.  
  366. //      idate = date;
  367.         skip(3);
  368.         d.snap = 0;
  369.                
  370.         if (skill == SNOVICE) {
  371.                 printf("It is stardate %d. The Federation is being attacked by\n",
  372.                            (int)d.date);
  373.                 printf("a deadly Klingon invasion force. As captain of the United\n"
  374.                            "Starship U.S.S. Enterprise, it is your mission to seek out\n"
  375.                            "and destroy this invasion force of %d battle cruisers.\n",
  376.                            inkling);
  377.                 printf("You have an initial allotment of %d stardates to complete\n"
  378.                            "your mission.  As you proceed you may be given more time.\n\n"
  379.                            "You will have %d supporting starbases.\n"
  380.                            "Starbase locations-  ",
  381.                            (int)intime, inbase);
  382.         }
  383.         else {
  384.                 printf("Stardate %d.\n\n"
  385.                            "%d Klingons,\nan unknown number of Romulans\n",
  386.                            (int)d.date, inkling);
  387.                 if (d.nscrem) printf("and one (GULP) Super-Commander.\n");
  388.                 printf("%d stardates\n%d starbases in  ",(int)intime, inbase);
  389.         }
  390.         for (i = 1; i <= inbase; i++) {
  391.                 cramlc(0, d.baseqx[i], d.baseqy[i]);
  392.                 if (i < inbase) proutn("  ");
  393.         }
  394.         skip(2);
  395.         proutn("The Enterprise is currently in");
  396.         cramlc(1, quadx, quady);
  397.         proutn(" ");
  398.         cramlc(2, sectx, secty);
  399.         skip(2);
  400.         prout("Good Luck!");
  401.         if (d.nscrem) proutn("  YOU'LL NEED IT.");
  402.         skip(1);
  403.         newqad(0);
  404.         if (nenhere) shldup=1.0;
  405.         if (neutz) attack(0);   // bad luck to start in a Romulan Neutral Zone
  406. }
  407.  
  408. int choose(void) {
  409.         tourn = 0;
  410.         thawed = 0;
  411.         skill = 0;
  412.         length = 0;
  413.         while (TRUE) {
  414.                 if (fromcommandline) /* Can start with command line options */
  415.                         fromcommandline = 0;
  416.                 else
  417.                         proutn("Would you like a regular, tournament, or frozen game?");
  418.                 scan();
  419.                 if (strlen(citem)==0) continue; // Try again
  420.                 if (isit("tournament")) {
  421.                         while (scan() == IHEOL) {
  422.                                 proutn("Type in tournament number-");
  423.                         }
  424.                         if (aaitem == 0) {
  425.                                 chew();
  426.                                 continue; // We don't want a blank entry
  427.                         }
  428.                         tourn = (int)aaitem;
  429.                         thingx = -1;
  430.                         srand((unsigned int)(int)aaitem);
  431.                         break;
  432.                 }
  433.                 if (isit("frozen")) {
  434.                         thaw();
  435.                         chew();
  436.                         if (*passwd==0) continue;
  437.                         randomize();
  438.                         Rand(); Rand(); Rand(); Rand();
  439.                         if (!alldone) thawed = 1; // No plaque if not finished
  440.                         report(1);
  441.                         return TRUE;
  442.                 }
  443.                 if (isit("regular")) {
  444.                         skip(2);
  445.                         randomize();
  446.                         Rand(); Rand(); Rand(); Rand();
  447.                         break;
  448.                 }
  449.                 proutn("What is \"");
  450.                 proutn(citem);
  451.                 prout("\"?");
  452.                 chew();
  453.         }
  454.         while (length==0 || skill==0) {
  455.                 if (scan() == IHALPHA) {
  456.                         if (isit("short")) length = 1;
  457.                         else if (isit("medium")) length = 2;
  458.                         else if (isit("long")) length = 4;
  459.                         else if (isit("novice")) skill = SNOVICE;
  460.                         else if (isit("fair")) skill = SFAIR;
  461.                         else if (isit("good")) skill = SGOOD;
  462.                         else if (isit("expert")) skill = SEXPERT;
  463.                         else if (isit("emeritus")) skill = SEMERITUS;
  464.                         else {
  465.                                 proutn("What is \"");
  466.                                 proutn(citem);
  467.                                 prout("\"?");
  468.                         }
  469.                 }
  470.                 else {
  471.                         chew();
  472.                         if (length==0) proutn("Would you like a Short, Medium, or Long game? ");
  473.                         else if (skill == 0) proutn("Are you a Novice, Fair, Good, Expert, or Emeritus player?");
  474.                 }
  475.         }
  476.         while (TRUE) {
  477.                 scan();
  478.                 strcpy(passwd, citem);
  479.                 chew();
  480.                 if (*passwd != 0) break;
  481.                 proutn("Please type in a secret password (9 characters maximum)-");
  482.         }
  483. #ifdef DEBUG
  484.         if (strcmp(passwd, "debug")==0) idebug = 1;
  485. #endif
  486.  
  487.         // Use parameters to generate initial values of things
  488.         damfac = 0.5 * skill;
  489.         d.rembase = 3.0*Rand()+2.0;
  490.         inbase = d.rembase;
  491.         inplan = (PLNETMAX/2) + (PLNETMAX/2+1)*Rand();
  492.         d.nromrem = (2.0+Rand())*skill;
  493.         d.nscrem = (skill > SFAIR? 1 : 0);
  494.         d.remtime = 7.0 * length;
  495.         intime = d.remtime;
  496.         d.remkl = 2.0*intime*((skill+1 - 2*Rand())*skill*0.1+.15); // d.remkl and inkling includes commanders and SC
  497.         inkling = d.remkl;
  498.         incom = skill + 0.0625*inkling*Rand();
  499.         d.remcom= min(10, incom);
  500.         incom = d.remcom;
  501.         d.remres = (inkling+4*incom)*intime;
  502.         inresor = d.remres;
  503.         if (inkling > 50) {
  504.                 inbase = (d.rembase += 1);
  505.     }
  506. #ifdef CAPTURE
  507.         brigcapacity = 400;
  508.     brigfree = brigcapacity;
  509.     kcaptured = 0; // TAA fix 6/2015
  510. #endif
  511. #ifdef CLOAKING
  512.     ncviol = 0; // TAA fix 6/2015
  513.     iscloaked = FALSE;
  514.     iscloaking = FALSE;
  515. #endif
  516.         return FALSE;
  517. }
  518.  
  519. void dropin(int iquad, int *ix, int *iy) {
  520.         do iran10(ix, iy);
  521.         while (quad[*ix][*iy] != IHDOT);
  522.         quad[*ix][*iy] = iquad;
  523. }
  524.  
  525. void newcnd(void) {
  526.         condit = IHGREEN;
  527.         if (energy < 1000.0) condit = IHYELLOW;
  528.         if (d.galaxy[quadx][quady] > 99 || d.newstuf[quadx][quady] > 9)
  529.                 condit = IHRED;
  530. }
  531.  
  532.  
  533. void newqad(int shutup) {
  534.         int quadnum = d.galaxy[quadx][quady];
  535.         int newnum = d.newstuf[quadx][quady];
  536.         int i, j, ix, iy, nplan;
  537.  
  538.         iattak = 1;
  539.         justin = 1;
  540.         basex = basey = 0;
  541.         klhere = 0;
  542.         comhere = 0;
  543.         plnetx = plnety = 0;
  544.         ishere = 0;
  545.         irhere = 0;
  546.         iplnet = 0;
  547.         nenhere = 0;
  548.         neutz = 0;
  549.         inorbit = 0;
  550.         landed = -1;
  551.         ientesc = 0;
  552.         ithere = 0;
  553.     iseenit = 0;
  554.  
  555. #ifdef CLOAKING
  556.     isviolreported = FALSE;
  557. #endif
  558.         if (iscate) {
  559.                 // Attempt to escape Super-commander, so tbeam back!
  560.                 iscate = 0;
  561.                 ientesc = 1;
  562.         }
  563.         // Clear quadrant
  564.         for (i=1; i <= 10; i++)
  565.                 for (j=1; j <= 10; j++) quad[i][j] = IHDOT;
  566.         // cope with supernova
  567.         if (quadnum > 999) {
  568.                 return;
  569.         }
  570.         klhere = quadnum/100;
  571.         irhere = newnum/10;
  572.         nplan = newnum%10;
  573.         nenhere = klhere + irhere;
  574.  
  575.         // Position Starship
  576.         quad[sectx][secty] = ship;
  577.  
  578.         // Decide if quadrant needs a Tholian
  579.         if ((skill <  SGOOD && Rand() <= 0.02) ||   /* Lighten up if skill is low */
  580.                 (skill == SGOOD && Rand() <= 0.05) ||
  581.                 (skill > SGOOD && Rand() <= 0.08)
  582. #ifdef DEBUG
  583.                 || strcmp(passwd, "tholianx")==0
  584. #endif
  585.                 ) {
  586.                 do {
  587.                         ithx = Rand() > 0.5 ? 10 : 1;
  588.                         ithy = Rand() > 0.5 ? 10 : 1;
  589.                 } while (quad[ithx][ithy] != IHDOT);
  590.                 quad[ithx][ithy] = IHT;
  591.                 ithere = 1;
  592.                 /* Reserve unocupied corners */
  593.                 if (quad[1][1]==IHDOT) quad[1][1] = 'X';
  594.                 if (quad[1][10]==IHDOT) quad[1][10] = 'X';
  595.                 if (quad[10][1]==IHDOT) quad[10][1] = 'X';
  596.                 if (quad[10][10]==IHDOT) quad[10][10] = 'X';
  597.         }
  598.  
  599.         if (quadnum >= 100) {
  600.                 // Position ordinary Klingons
  601.                 quadnum -= 100*klhere;
  602.                 for (i = 1; i <= klhere; i++) {
  603.                         dropin(IHK, &ix, &iy);
  604.                         kx[i] = ix;
  605.                         ky[i] = iy;
  606.                         kdist[i] = kavgd[i] = sqrt(square(sectx-ix) + square(secty-iy));
  607.                         kpower[i] = Rand()*150.0 +300.0 +25.0*skill;
  608.                 }
  609.                 // If we need a commander, promote a Klingon
  610.                 for (i = 1; i <= d.remcom ; i++)
  611.                         if (d.cx[i]==quadx && d.cy[i]==quady) break;
  612.                        
  613.                 if (i <= d.remcom) {
  614.                         quad[ix][iy] = IHC;
  615.                         kpower[klhere] = 950.0+400.0*Rand()+50.0*skill;
  616.                         comhere = 1;
  617.                 }
  618.  
  619.                 // If we need a super-commander, promote a Klingon
  620.                 if (quadx == d.isx && quady == d.isy) {
  621.                         quad[kx[1]][ky[1]] = IHS;
  622.                         kpower[1] = 1175.0 + 400.0*Rand() + 125.0*skill;
  623.                         iscate = 1;
  624.                         ishere = 1;
  625.                 }
  626.         }
  627.         // Put in Romulans if needed
  628.         for (i = klhere+1; i <= nenhere; i++) {
  629.                 dropin(IHR, &ix, &iy);
  630.                 kx[i] = ix;
  631.                 ky[i] = iy;
  632.                 kdist[i] = kavgd[i] = sqrt(square(sectx-ix) + square(secty-iy));
  633.                 kpower[i] = Rand()*400.0 + 450.0 + 50.0*skill;
  634.         }
  635.         sortkl();
  636.         // If quadrant needs a starbase, put it in
  637.         if (quadnum >= 10) {
  638.                 quadnum -= 10;
  639.                 dropin(IHB, &basex, &basey);
  640.         }
  641.        
  642.         if (nplan) {
  643.                 // If quadrant needs a planet, put it in
  644.                 for (i=1; i <= inplan; i++)
  645.                         if (d.plnets[i].x == quadx && d.plnets[i].y == quady) break;
  646.                 if (i <= inplan) {
  647.                         iplnet = i;
  648.                         dropin(IHP, &plnetx, &plnety);
  649.                 }
  650.         }
  651.         // Check for condition
  652.         newcnd();
  653.         // And finally the stars
  654.         for (i = 1; i <= quadnum; i++) dropin(IHSTAR, &ix, &iy);
  655.  
  656.         // Check for RNZ
  657.         if (irhere > 0 && klhere == 0 && basex == 0) {
  658.                 neutz = 1;
  659.                 if (REPORTS) {
  660.                         skip(1);
  661.                         prout("LT. UHURA- \"Captain, an urgent message.");
  662.                         prout("  I'll put it on audio.\"  CLICK");
  663.                         skip(1);
  664.                         prout("INTRUDER! YOU HAVE VIOLATED THE ROMULAN NEUTRAL ZONE.");
  665.                         prout("LEAVE AT ONCE, OR YOU WILL BE DESTROYED!");
  666.                 }
  667.         }
  668.  
  669.         if (shutup==0) {
  670.                 // Put in THING if needed
  671.                 if (thingx == quadx && thingy == quady) {
  672.                         dropin(IHQUEST, &ix, &iy);
  673.                         thingx = thingy = 0; // Transient
  674.                         if (damage[DSRSENS] == 0.0) {
  675.                                 skip(1);
  676.                                 prout("MR. SPOCK- \"Captain, this is most unusual.");
  677.                                 prout("    Please examine your short-range scan.\"");
  678.                         }
  679.                 }
  680.         }
  681.  
  682.         // Put in a few black holes
  683.         for (i = 1; i <= 3; i++)
  684.                 if (Rand() > 0.5) dropin(IHBLANK, &ix, &iy);
  685.  
  686.         // Take out X's in corners if Tholian present
  687.         if (ithere) {
  688.                 if (quad[1][1]=='X') quad[1][1] = IHDOT;
  689.                 if (quad[1][10]=='X') quad[1][10] = IHDOT;
  690.                 if (quad[10][1]=='X') quad[10][1] = IHDOT;
  691.                 if (quad[10][10]=='X') quad[10][10] = IHDOT;
  692.         }              
  693. }
  694.  
  695. void sortkl(void) {
  696.         double t;
  697.         int sw, j, k;
  698.  
  699.         // The author liked bubble sort. So we will use it. :-(
  700.  
  701.         if (nenhere < 2) return;
  702.  
  703.         do {
  704.                 sw = FALSE;
  705.                 for (j = 1; j < nenhere; j++)
  706.                         if (kdist[j] > kdist[j+1]) {
  707.                                 sw = TRUE;
  708.                                 t = kdist[j];
  709.                                 kdist[j] = kdist[j+1];
  710.                                 kdist[j+1] = t;
  711.                                 t = kavgd[j];
  712.                                 kavgd[j] = kavgd[j+1];
  713.                                 kavgd[j+1] = t;
  714.                                 k = kx[j];
  715.                                 kx[j] = kx[j+1];
  716.                                 kx[j+1] = k;
  717.                                 k = ky[j];
  718.                                 ky[j] = ky[j+1];
  719.                                 ky[j+1] = k;
  720.                                 t = kpower[j];
  721.                                 kpower[j] = kpower[j+1];
  722.                                 kpower[j+1] = t;
  723.                         }
  724.         } while (sw);
  725. }
  726.