More magic-number chasing.
[super-star-trek.git] / setup.c
diff --git a/setup.c b/setup.c
index 1d7dd0153679b77abc4e5d21520c216fb44bb48e..fb12ed86bc1e445f30e6971767a593aeb3a66360 100644 (file)
--- a/setup.c
+++ b/setup.c
-#include <time.h>\r
-#include "sst.h"\r
-\r
-void prelim(void) {\r
-       skip(2);\r
-       prout("-SUPER- STAR TREK");\r
-       skip(1);\r
-       prout("Latest update-21 Sept 78");\r
-       skip(1);\r
-}\r
-\r
-void freeze(int boss) {\r
-       char *x, *y;\r
-       FILE *fp;\r
-       int key;\r
-       if (boss) {\r
-               strcpy(citem, "emsave.trk");\r
-       }\r
-       else {\r
-               if ((key = scan()) == IHEOL) {\r
-                       proutn("File name: ");\r
-                       key = scan();\r
-               }\r
-               if (key != IHALPHA) {\r
-                       huh();\r
-                       return;\r
-               }\r
-               chew();\r
-               if (strchr(citem, '.') == NULL) {\r
-                       strcat(citem, ".trk");\r
-               }\r
-       }\r
-       if ((fp = fopen(citem, "wb")) == NULL) {\r
-               proutn("Can't freeze game as file ");\r
-               proutn(citem);\r
-               skip(1);\r
-               return;\r
-       }\r
-       strcpy(game.magic, SSTMAGIC);\r
-       fwrite(&game, sizeof(game), 1, fp);\r
-\r
-       fclose(fp);\r
-\r
-       /* I hope that's enough! */\r
-}\r
-\r
-\r
-int thaw(void) {\r
-       FILE *fp;\r
-        int key;\r
-\r
-       game.passwd[0] = '\0';\r
-       if ((key = scan()) == IHEOL) {\r
-               proutn("File name: ");\r
-               key = scan();\r
-       }\r
-       if (key != IHALPHA) {\r
-               huh();\r
-               return 1;\r
-       }\r
-       chew();\r
-       if (strchr(citem, '.') == NULL) {\r
-               strcat(citem, ".trk");\r
-       }\r
-       if ((fp = fopen(citem, "rb")) == NULL) {\r
-               proutn("Can't find game file ");\r
-               proutn(citem);\r
-               skip(1);\r
-               return 1;\r
-       }\r
-       fread(&game, sizeof(game), 1, fp);\r
-       if (strcmp(game.magic, SSTMAGIC)) {\r
-               prout("Game file format is bad, should begin with " SSTMAGIC);\r
-               skip(1);\r
-               fclose(fp);\r
-               return 1;\r
-       }\r
-\r
-       fclose(fp);\r
-\r
-        return 0;\r
-}\r
-\r
-void abandn(void) {\r
-       int nb, l;\r
-\r
-       chew();\r
-       if (condit==IHDOCKED) {\r
-               if (ship!=IHE) {\r
-                       prout("You cannot abandon Ye Faerie Queene.");\r
-                       return;\r
-               }\r
-       }\r
-       else {\r
-               /* Must take shuttle craft to exit */\r
-               if (game.damage[DSHUTTL]==-1) {\r
-                       prout("Ye Faerie Queene has no shuttle craft.");\r
-                       return;\r
-               }\r
-               if (game.damage[DSHUTTL]<0) {\r
-                       prout("Shuttle craft now serving Big Mac's.");\r
-                       return;\r
-               }\r
-               if (game.damage[DSHUTTL]>0) {\r
-                       prout("Shuttle craft damaged.");\r
-                       return;\r
-               }\r
-               if (landed==1) {\r
-                       prout("You must be aboard the Enterprise.");\r
-                       return;\r
-               }\r
-               if (iscraft!=1) {\r
-                       prout("Shuttle craft not currently available.");\r
-                       return;\r
-               }\r
-               /* Print abandon ship messages */\r
-               skip(1);\r
-               prouts("***ABANDON SHIP!  ABANDON SHIP!");\r
-               skip(1);\r
-               prouts("***ALL HANDS ABANDON SHIP!");\r
-               skip(2);\r
-               prout("Captain and crew escape in shuttle craft.");\r
-               prout("Remainder of ship's complement beam down");\r
-               prout("to nearest habitable planet.");\r
-               if (game.state.rembase==0) {\r
-                       /* Ops! no place to go... */\r
-                       finish(FABANDN);\r
-                       return;\r
-               }\r
-               /* If at least one base left, give 'em the Faerie Queene */\r
-               skip(1);\r
-               icrystl = 0; /* crystals are lost */\r
-               nprobes = 0; /* No probes */\r
-               prout("You are captured by Klingons and released to");\r
-               prout("the Federation in a prisoner-of-war exchange.");\r
-               nb = Rand()*game.state.rembase+1;\r
-               /* Set up quadrant and position FQ adjacient to base */\r
-               if (quadx!=game.state.baseqx[nb] || quady!=game.state.baseqy[nb]) {\r
-                       quadx = game.state.baseqx[nb];\r
-                       quady = game.state.baseqy[nb];\r
-                       sectx = secty = 5;\r
-                       newqad(1);\r
-               }\r
-               for (;;) {\r
-                       /* position next to base by trial and error */\r
-                       game.quad[sectx][secty] = IHDOT;\r
-                       for (l = 1; l <= 10; l++) {\r
-                               sectx = 3.0*Rand() - 1.0 + basex;\r
-                               secty = 3.0*Rand() - 1.0 + basey;\r
-                               if (sectx >= 1 && sectx <= 10 &&\r
-                                       secty >= 1 && secty <= 10 &&\r
-                                       game.quad[sectx][secty] == IHDOT) break;\r
-                       }\r
-                       if (l < 11) break; /* found a spot */\r
-                       sectx=5;\r
-                       secty=5;\r
-                       newqad(1);\r
-               }\r
-       }\r
-       /* Get new commission */\r
-       game.quad[sectx][secty] = ship = IHF;\r
-       prout("Starfleet puts you in command of another ship,");\r
-       prout("the Faerie Queene, which is antiquated but,");\r
-       prout("still useable.");\r
-       if (icrystl!=0) prout("The dilithium crystals have been moved.");\r
-       imine=0;\r
-       iscraft=0; /* Gallileo disappears */\r
-       /* Resupply ship */\r
-       condit=IHDOCKED;\r
-       for (l = 1; l <= NDEVICES; l++) game.damage[l] = 0.0;\r
-       game.damage[DSHUTTL] = -1;\r
-       energy = inenrg = 3000.0;\r
-       shield = inshld = 1250.0;\r
-       torps = intorps = 6;\r
-       lsupres=inlsr=3.0;\r
-       shldup=0;\r
-       warpfac=5.0;\r
-       wfacsq=25.0;\r
-       return;\r
-}\r
-       \r
-void setup(int needprompt) {\r
-       int i,j, krem, klumper;\r
-       int ix, iy;\r
-#ifdef DEBUG\r
-       idebug = 0;\r
-#endif\r
-       //  Decide how many of everything\r
-       if (choose(needprompt)) return; // frozen game\r
-       // Prepare the Enterprise\r
-        alldone = gamewon = 0;\r
-       ship = IHE;\r
-       energy = inenrg = 5000.0;\r
-       shield = inshld = 2500.0;\r
-       shldchg = shldup = 0;\r
-       inlsr = 4.0;\r
-       lsupres = 4.0;\r
-       iran8(&quadx, &quady);\r
-       iran10(&sectx, &secty);\r
-       torps = intorps = 10;\r
-       nprobes = (int)(3.0*Rand() + 2.0);      /* Give them 2-4 of these wonders */\r
-       warpfac = 5.0;\r
-       wfacsq = warpfac * warpfac;\r
-       for (i=0; i <= NDEVICES; i++) game.damage[i] = 0.0;\r
-       // Set up assorted game parameters\r
-       batx = baty = 0;\r
-       game.state.date = indate = 100.0*(int)(31.0*Rand()+20.0);\r
-       game.state.killk = game.state.killc = nkinks = nhelp = resting = casual = game.state.nromkl = 0;\r
-       isatb = iscate = imine = icrystl = icraft = game.state.nsckill = game.state.nplankl = 0;\r
-        game.state.starkl = game.state.basekl = 0;\r
-       iscraft = 1;\r
-       landed = -1;\r
-       alive = 1;\r
-       docfac = 0.25;\r
-       for (i = 1; i <= 8; i++)\r
-               for (j = 1; j <= 8; j++) game.state.newstuf[i][j] = game.starch[i][j] = 0;\r
-       // Initialize times for extraneous events\r
-       game.future[FSNOVA] = game.state.date + expran(0.5 * intime);\r
-       game.future[FTBEAM] = game.state.date + expran(1.5 * (intime / game.state.remcom));\r
-       game.future[FSNAP] = game.state.date + 1.0 + Rand(); // Force an early snapshot\r
-       game.future[FBATTAK] = game.state.date + expran(0.3*intime);\r
-       game.future[FCDBAS] = 1e30;\r
-       game.future[FSCMOVE] = game.state.nscrem ? game.state.date+0.2777 : 1e30;\r
-       game.future[FSCDBAS] = 1e30;\r
-       game.future[FDSPROB] = 1e30;\r
-       // Starchart is functional\r
-       stdamtim = 1e30;\r
-       // Put stars in the galaxy\r
-       instar = 0;\r
-       for (i=1; i<=8; i++)\r
-               for (j=1; j<=8; j++) {\r
-                       int k = Rand()*9.0 + 1.0;\r
-                       instar += k;\r
-                       game.state.galaxy[i][j] = k;\r
-               }\r
-       // Locate star bases in galaxy\r
-       for (i = 1; i <= inbase; i++) {\r
-               int contflag;\r
-               do {\r
-                       do iran8(&ix, &iy);\r
-                       while (game.state.galaxy[ix][iy] >= 10);\r
-                       contflag = FALSE;\r
-                       for (j = i-1; j > 0; j--) {\r
-                               /* Improved placement algorithm to spread out bases */\r
-                               double distq = square(ix-game.state.baseqx[j]) + square(iy-game.state.baseqy[j]);\r
-                               if (distq < 6.0*(6-inbase) && Rand() < 0.75) {\r
-                                       contflag = TRUE;\r
-#ifdef DEBUG\r
-                                       proutn("DEBUG: Abandoning base #%d at %d-%d\n", i, ix, iy);\r
-#endif\r
-                                       break;\r
-                               }\r
-#ifdef DEBUG\r
-                               else if (distq < 6.0 * (6-inbase)) {\r
-                                       proutn("DEBUG: saving base #%d, close to #%d\n", i, j);\r
-                               }\r
-#endif\r
-                       }\r
-               } while (contflag);\r
-                       \r
-               game.state.baseqx[i] = ix;\r
-               game.state.baseqy[i] = iy;\r
-               game.starch[ix][iy] = -1;\r
-               game.state.galaxy[ix][iy] += 10;\r
-       }\r
-       // Position ordinary Klingon Battle Cruisers\r
-       krem = inkling - incom - game.state.nscrem;\r
-       klumper = 0.25*skill*(9.0-length)+1.0;\r
-       if (klumper > 9) klumper = 9; // Can't have more than 9 in quadrant\r
-       do {\r
-               double r = Rand();\r
-               int klump = (1.0 - r*r)*klumper;\r
-               if (klump > krem) klump = krem;\r
-               krem -= klump;\r
-               klump *= 100;\r
-               do iran8(&ix, &iy);\r
-               while (game.state.galaxy[ix][iy] + klump >= 1000);\r
-               game.state.galaxy[ix][iy] += klump;\r
-       } while (krem > 0);\r
-       // Position Klingon Commander Ships\r
-#ifdef DEBUG\r
-       klumper = 1;\r
-#endif\r
-       for (i = 1; i <= incom; i++) {\r
-               do {\r
-                       do { /* IF debugging, put commanders by bases, always! */\r
-#ifdef DEBUG\r
-                               if (idebug && klumper <= inbase) {\r
-                                       ix = game.state.baseqx[klumper];\r
-                                       iy = game.state.baseqy[klumper];\r
-                                       klumper++;\r
-                               }\r
-                               else\r
-#endif\r
-                                       iran8(&ix, &iy);\r
-                       }\r
-                       while ((game.state.galaxy[ix][iy] < 99 && Rand() < 0.75)||\r
-                                  game.state.galaxy[ix][iy]>899);\r
-                       // check for duplicate\r
-                       for (j = 1; j < i; j++)\r
-                               if (game.state.cx[j]==ix && game.state.cy[j]==iy) break;\r
-               } while (j < i);\r
-               game.state.galaxy[ix][iy] += 100;\r
-               game.state.cx[i] = ix;\r
-               game.state.cy[i] = iy;\r
-       }\r
-       // Locate planets in galaxy\r
-       for (i = 0; i < inplan; i++) {\r
-               do iran8(&ix, &iy);\r
-               while (game.state.newstuf[ix][iy] > 0);\r
-               game.state.newstuf[ix][iy] = 1;\r
-               game.state.plnets[i].x = ix;\r
-               game.state.plnets[i].y = iy;\r
-               game.state.plnets[i].pclass = Rand()*3.0; // Planet class M N or O\r
-               game.state.plnets[i].crystals = 1.5*Rand();             // 1 in 3 chance of crystals\r
-               game.state.plnets[i].known = unknown;\r
-       }\r
-       // Locate Romulans\r
-       for (i = 1; i <= game.state.nromrem; i++) {\r
-               iran8(&ix, &iy);\r
-               game.state.newstuf[ix][iy] += 10;\r
-       }\r
-       // Locate the Super Commander\r
-       if (game.state.nscrem > 0) {\r
-               do iran8(&ix, &iy);\r
-               while (game.state.galaxy[ix][iy] >= 900);\r
-               game.state.isx = ix;\r
-               game.state.isy = iy;\r
-               game.state.galaxy[ix][iy] += 100;\r
-       }\r
-       // Place thing (in tournament game, thingx == -1, don't want one!)\r
-       if (thingx != -1) {\r
-               iran8(&thingx, &thingy);\r
-       }\r
-       else {\r
-               thingx = thingy = 0;\r
-       }\r
-\r
-//     idate = date;\r
-       skip(2);\r
-       game.state.snap = 0;\r
-               \r
-       if (skill == 1) {\r
-               prout("It is stardate %d. The Federation is being attacked by",\r
-                          (int)game.state.date);\r
-               prout("a deadly Klingon invasion force. As captain of the United");\r
-               prout("Starship U.S.S. Enterprise, it is your mission to seek out");\r
-               prout("and destroy this invasion force of %d battle cruisers.",\r
-                          inkling);\r
-               prout("You have an initial allotment of %d stardates to complete", (int)intime);\r
-               prout("your mission.  As you proceed you may be given more time.");\r
-               prout("");\r
-               prout("You will have %d supporting starbases.", inbase);\r
-               proutn("Starbase locations-  ");\r
-       }\r
-       else {\r
-               prout("Stardate %d.", (int)game.state.date);\r
-               prout("");\r
-               prout("%d Klingons.", inkling);\r
-               prout("An unknown number of Romulans.");\r
-               if (game.state.nscrem) prout("and one (GULP) Super-Commander.");\r
-                       prout("%d stardates.",(int)intime);\r
-                       proutn("%d starbases in ", inbase);\r
-       }\r
-       for (i = 1; i <= inbase; i++) {\r
-               proutn(cramlc(0, game.state.baseqx[i], game.state.baseqy[i]));\r
-               proutn("  ");\r
-       }\r
-       skip(2);\r
-       proutn("The Enterprise is currently in ");\r
-       proutn(cramlc(quadrant, quadx, quady));\r
-       proutn(" ");\r
-        proutn(cramlc(sector, sectx, secty));\r
-       skip(2);\r
-       prout("Good Luck!");\r
-       if (game.state.nscrem) prout("  YOU'LL NEED IT.");\r
-       newqad(0);\r
-        if (nenhere-iqhere-ithere) shldup=1.0;\r
-       if (neutz) attack(0);   // bad luck to start in a Romulan Neutral Zone\r
-}\r
-\r
-void randomize(void) {\r
-       srand((int)time(NULL));\r
-}\r
-\r
-int choose(int needprompt) {\r
-       tourn = 0;\r
-       thawed = 0;\r
-       skill = 0;\r
-       length = 0;\r
-       while (TRUE) {\r
-               if (needprompt) /* Can start with command line options */\r
-                   proutn("Would you like a regular, tournament, or frozen game?");\r
-               scan();\r
-               if (strlen(citem)==0) continue; // Try again\r
-               if (isit("tournament")) {\r
-                       while (scan() == IHEOL) {\r
-                               proutn("Type in tournament number-");\r
-                       }\r
-                       if (aaitem == 0) {\r
-                               chew();\r
-                               continue; // We don't want a blank entry\r
-                       }\r
-                       tourn = (int)aaitem;\r
-                       thingx = -1;\r
-                       srand((unsigned int)(int)aaitem);\r
-                       break;\r
-               }\r
-               if (isit("frozen")) {\r
-                       thaw();\r
-                       chew();\r
-                       if (*game.passwd==0) continue;\r
-                       randomize();\r
-                       Rand(); Rand(); Rand(); Rand();\r
-                       if (!alldone) thawed = 1; // No plaque if not finished\r
-                       report(1);\r
-                       return TRUE;\r
-               }\r
-               if (isit("regular")) {\r
-                       skip(2);\r
-                       randomize();\r
-                       Rand(); Rand(); Rand(); Rand();\r
-                       break;\r
-               }\r
-               proutn("What is \"");\r
-               proutn(citem);\r
-               prout("\"?");\r
-               chew();\r
-       }\r
-       while (length==0 || skill==0) {\r
-               if (scan() == IHALPHA) {\r
-                       if (isit("short")) length = 1;\r
-                       else if (isit("medium")) length = 2;\r
-                       else if (isit("long")) length = 4;\r
-                       else if (isit("novice")) skill = 1;\r
-                       else if (isit("fair")) skill = 2;\r
-                       else if (isit("good")) skill = 3;\r
-                       else if (isit("expert")) skill = 4;\r
-                       else if (isit("emeritus")) skill = 5;\r
-                       else {\r
-                               proutn("What is \"");\r
-                               proutn(citem);\r
-                               prout("\"?");\r
-                       }\r
-               }\r
-               else {\r
-                       chew();\r
-                       if (length==0) proutn("Would you like a Short, Medium, or Long game? ");\r
-                       else if (skill == 0) proutn("Are you a Novice, Fair, Good, Expert, or Emeritus player?");\r
-               }\r
-       }\r
-       while (TRUE) {\r
-               scan();\r
-               strcpy(game.passwd, citem);\r
-               chew();\r
-               if (*game.passwd != 0) break;\r
-               proutn("Please type in a secret password-");\r
-       }\r
-#ifdef DEBUG\r
-       if (strcmp(game.passwd, "debug")==0) idebug = 1;\r
-#endif\r
-\r
-       // Use parameters to generate initial values of things\r
-       damfac = 0.5 * skill;\r
-       game.state.rembase = 3.0*Rand()+2.0;\r
-       inbase = game.state.rembase;\r
-       inplan = (PLNETMAX/2) + (PLNETMAX/2+1)*Rand();\r
-       game.state.nromrem = (2.0+Rand())*skill;\r
-       game.state.nscrem = (skill > 2? 1 : 0);\r
-       game.state.remtime = 7.0 * length;\r
-       intime = game.state.remtime;\r
-       game.state.remkl = 2.0*intime*((skill+1 - 2*Rand())*skill*0.1+.15);\r
-       inkling = game.state.remkl;\r
-       incom = skill + 0.0625*inkling*Rand();\r
-       game.state.remcom= min(10, incom);\r
-       incom = game.state.remcom;\r
-       game.state.remres = (inkling+4*incom)*intime;\r
-       inresor = game.state.remres;\r
-       if (inkling > 50) {\r
-               inbase = (game.state.rembase += 1);\r
-       }\r
-       return FALSE;\r
-}\r
-\r
-void dropin(int iquad, int *ix, int *iy) {\r
-       do iran10(ix, iy);\r
-       while (game.quad[*ix][*iy] != IHDOT);\r
-       game.quad[*ix][*iy] = iquad;\r
-}\r
-\r
-void newcnd(void) {\r
-       condit = IHGREEN;\r
-       if (energy < 1000.0) condit = IHYELLOW;\r
-       if (game.state.galaxy[quadx][quady] > 99 || game.state.newstuf[quadx][quady] > 9)\r
-               condit = IHRED;\r
-}\r
-\r
-\r
-void newqad(int shutup) {\r
-       int quadnum = game.state.galaxy[quadx][quady];\r
-       int newnum = game.state.newstuf[quadx][quady];\r
-       int i, j, ix, iy, nplan;\r
-\r
-       iattak = 1;\r
-       justin = 1;\r
-       basex = basey = 0;\r
-       klhere = 0;\r
-       comhere = 0;\r
-       plnetx = plnety = 0;\r
-       ishere = 0;\r
-       irhere = 0;\r
-       iplnet = 0;\r
-       nenhere = 0;\r
-       neutz = 0;\r
-       inorbit = 0;\r
-       landed = -1;\r
-       ientesc = 0;\r
-       ithere = 0;\r
-        iqhere=0;\r
-        iqengry=0;\r
-       iseenit = 0;\r
-       if (iscate) {\r
-               // Attempt to escape Super-commander, so tbeam back!\r
-               iscate = 0;\r
-               ientesc = 1;\r
-       }\r
-       // Clear quadrant\r
-       for (i=1; i <= 10; i++)\r
-               for (j=1; j <= 10; j++) game.quad[i][j] = IHDOT;\r
-       // cope with supernova\r
-       if (quadnum > 999) {\r
-               return;\r
-       }\r
-       klhere = quadnum/100;\r
-       irhere = newnum/10;\r
-       nplan = newnum%10;\r
-       nenhere = klhere + irhere;\r
-\r
-       // Position Starship\r
-       game.quad[sectx][secty] = ship;\r
-\r
-       if (quadnum >= 100) {\r
-               // Position ordinary Klingons\r
-               quadnum -= 100*klhere;\r
-               for (i = 1; i <= klhere; i++) {\r
-                       dropin(IHK, &ix, &iy);\r
-                       game.kx[i] = ix;\r
-                       game.ky[i] = iy;\r
-                       game.kdist[i] = game.kavgd[i] = sqrt(square(sectx-ix) + square(secty-iy));\r
-                       game.kpower[i] = Rand()*150.0 +300.0 +25.0*skill;\r
-               }\r
-               // If we need a commander, promote a Klingon\r
-               for (i = 1; i <= game.state.remcom ; i++) \r
-                       if (game.state.cx[i]==quadx && game.state.cy[i]==quady) break;\r
-                       \r
-               if (i <= game.state.remcom) {\r
-                       game.quad[ix][iy] = IHC;\r
-                       game.kpower[klhere] = 950.0+400.0*Rand()+50.0*skill;\r
-                       comhere = 1;\r
-               }\r
-\r
-               // If we need a super-commander, promote a Klingon\r
-               if (quadx == game.state.isx && quady == game.state.isy) {\r
-                       game.quad[game.kx[1]][game.ky[1]] = IHS;\r
-                       game.kpower[1] = 1175.0 + 400.0*Rand() + 125.0*skill;\r
-                        iscate = game.state.remkl>1;\r
-                       ishere = 1;\r
-               }\r
-       }\r
-       // Put in Romulans if needed\r
-       for (i = klhere+1; i <= nenhere; i++) {\r
-               dropin(IHR, &ix, &iy);\r
-               game.kx[i] = ix;\r
-               game.ky[i] = iy;\r
-               game.kdist[i] = game.kavgd[i] = sqrt(square(sectx-ix) + square(secty-iy));\r
-               game.kpower[i] = Rand()*400.0 + 450.0 + 50.0*skill;\r
-       }\r
-       sortkl();\r
-       // If quadrant needs a starbase, put it in\r
-       if (quadnum >= 10) {\r
-               quadnum -= 10;\r
-               dropin(IHB, &basex, &basey);\r
-       }\r
-       \r
-       if (nplan) {\r
-               // If quadrant needs a planet, put it in\r
-               for (i=0; i < inplan; i++)\r
-                       if (game.state.plnets[i].x == quadx && game.state.plnets[i].y == quady) break;\r
-               if (i < inplan) {\r
-                       iplnet = i;\r
-                       dropin(IHP, &plnetx, &plnety);\r
-               }\r
-       }\r
-       // Check for condition\r
-       newcnd();\r
-       // And finally the stars\r
-       for (i = 1; i <= quadnum; i++) dropin(IHSTAR, &ix, &iy);\r
-\r
-       // Check for RNZ\r
-       if (irhere > 0 && klhere == 0) {\r
-               neutz = 1;\r
-               if (game.damage[DRADIO] <= 0.0) {\r
-                       skip(1);\r
-                       prout("LT. Uhura- \"Captain, an urgent message.");\r
-                       prout("  I'll put it on audio.\"  CLICK");\r
-                       skip(1);\r
-                       prout("INTRUDER! YOU HAVE VIOLATED THE ROMULAN NEUTRAL ZONE.");\r
-                       prout("LEAVE AT ONCE, OR YOU WILL BE DESTROYED!");\r
-               }\r
-       }\r
-\r
-       if (shutup==0) {\r
-               // Put in THING if needed\r
-               if (thingx == quadx && thingy == quady) {\r
-                       dropin(IHQUEST, &ix, &iy);\r
-                        iran8(&thingx, &thingy);\r
-                        nenhere++;\r
-                        iqhere=1;\r
-                        game.kx[nenhere] = ix;\r
-                        game.ky[nenhere] = iy;\r
-                        game.kdist[nenhere] = game.kavgd[nenhere] =\r
-                           sqrt(square(sectx-ix) + square(secty-iy));\r
-                        game.kpower[nenhere] = Rand()*6000.0 +500.0 +250.0*skill;\r
-                       if (game.damage[DSRSENS] == 0.0) {\r
-                               skip(1);\r
-                               prout("MR. SPOCK- \"Captain, this is most unusual.");\r
-                               prout("    Please examine your short-range scan.\"");\r
-                       }\r
-               }\r
-       }\r
-\r
-        // Decide if quadrant needs a Tholian\r
-        if ((skill < 3 && Rand() <= 0.02) ||   /* Lighten up if skill is low */\r
-                (skill == 3 && Rand() <= 0.05) ||\r
-                (skill > 3 && Rand() <= 0.08)\r
-#ifdef DEBUG\r
-                || strcmp(passwd, "tholianx")==0\r
-#endif\r
-                ) {\r
-                do {\r
-                        ithx = Rand() > 0.5 ? 10 : 1;\r
-                        ithy = Rand() > 0.5 ? 10 : 1;\r
-                } while (game.quad[ithx][ithy] != IHDOT);\r
-                game.quad[ithx][ithy] = IHT;\r
-                ithere = 1;\r
-                nenhere++;\r
-                game.kx[nenhere] = ithx;\r
-                game.ky[nenhere] = ithy;\r
-                game.kdist[nenhere] = game.kavgd[nenhere] =\r
-                   sqrt(square(sectx-ithx) + square(secty-ithy));\r
-                game.kpower[nenhere] = Rand()*400.0 +100.0 +25.0*skill;\r
-                /* Reserve unocupied corners */\r
-                if (game.quad[1][1]==IHDOT) game.quad[1][1] = 'X';\r
-                if (game.quad[1][10]==IHDOT) game.quad[1][10] = 'X';\r
-                if (game.quad[10][1]==IHDOT) game.quad[10][1] = 'X';\r
-                if (game.quad[10][10]==IHDOT) game.quad[10][10] = 'X';\r
-        }\r
-        sortkl();\r
-\r
-       // Put in a few black holes\r
-       for (i = 1; i <= 3; i++)\r
-               if (Rand() > 0.5) dropin(IHBLANK, &ix, &iy);\r
-\r
-       // Take out X's in corners if Tholian present\r
-       if (ithere) {\r
-               if (game.quad[1][1]=='X') game.quad[1][1] = IHDOT;\r
-               if (game.quad[1][10]=='X') game.quad[1][10] = IHDOT;\r
-               if (game.quad[10][1]=='X') game.quad[10][1] = IHDOT;\r
-               if (game.quad[10][10]=='X') game.quad[10][10] = IHDOT;\r
-       }               \r
-}\r
-\r
-void sortkl(void) {\r
-       double t;\r
-       int sw, j, k;\r
-\r
-       // The author liked bubble sort. So we will use it. :-(\r
-\r
-        if (nenhere-iqhere-ithere < 2) return;\r
-\r
-       do {\r
-               sw = FALSE;\r
-               for (j = 1; j < nenhere; j++)\r
-                       if (game.kdist[j] > game.kdist[j+1]) {\r
-                               sw = TRUE;\r
-                               t = game.kdist[j];\r
-                               game.kdist[j] = game.kdist[j+1];\r
-                               game.kdist[j+1] = t;\r
-                               t = game.kavgd[j];\r
-                               game.kavgd[j] = game.kavgd[j+1];\r
-                               game.kavgd[j+1] = t;\r
-                               k = game.kx[j];\r
-                               game.kx[j] = game.kx[j+1];\r
-                               game.kx[j+1] = k;\r
-                               k = game.ky[j];\r
-                               game.ky[j] = game.ky[j+1];\r
-                               game.ky[j+1] = k;\r
-                               t = game.kpower[j];\r
-                               game.kpower[j] = game.kpower[j+1];\r
-                               game.kpower[j+1] = t;\r
-                       }\r
-       } while (sw);\r
-}\r
+#include <time.h>
+#include <sys/stat.h>
+#include "sst.h"
+
+#ifdef __linux__
+static long filelength(int fd) {
+struct stat buf;
+    fstat(fd, &buf);
+    return buf.st_size;
+}
+#endif
+
+void prelim(void) {
+       skip(2);
+       prout("-SUPER- STAR TREK");
+       skip(1);
+#ifdef __HISTORICAL__
+       prout("Latest update-21 Sept 78");
+       skip(1);
+#endif /* __HISTORICAL__ */
+}
+
+void freeze(int boss) {
+       FILE *fp;
+       int key;
+       if (boss) {
+               strcpy(citem, "emsave.trk");
+       }
+       else {
+               if ((key = scan()) == IHEOL) {
+                       proutn("File name: ");
+                       key = scan();
+               }
+               if (key != IHALPHA) {
+                       huh();
+                       return;
+               }
+               chew();
+               if (strchr(citem, '.') == NULL) {
+                       strcat(citem, ".trk");
+               }
+       }
+       if ((fp = fopen(citem, "wb")) == NULL) {
+               proutn("Can't freeze game as file ");
+               proutn(citem);
+               skip(1);
+               return;
+       }
+       strcpy(game.magic, SSTMAGIC);
+       fwrite(&game, sizeof(game), 1, fp);
+
+       fclose(fp);
+
+       /* I hope that's enough! */
+}
+
+
+int thaw(void) {
+       FILE *fp;
+        int key;
+
+       game.passwd[0] = '\0';
+       if ((key = scan()) == IHEOL) {
+               proutn("File name: ");
+               key = scan();
+       }
+       if (key != IHALPHA) {
+               huh();
+               return 1;
+       }
+       chew();
+       if (strchr(citem, '.') == NULL) {
+               strcat(citem, ".trk");
+       }
+       if ((fp = fopen(citem, "rb")) == NULL) {
+               proutn("Can't find game file ");
+               proutn(citem);
+               skip(1);
+               return 1;
+       }
+       fread(&game, sizeof(game), 1, fp);
+       if (feof(fp) || ftell(fp) != filelength(fileno(fp)) || strcmp(game.magic, SSTMAGIC)) {
+               prout("Game file format is bad, should begin with " SSTMAGIC);
+               skip(1);
+               fclose(fp);
+               return 1;
+       }
+
+       fclose(fp);
+
+        return 0;
+}
+
+void abandn(void) {
+       int nb, l;
+
+       chew();
+       if (condit==IHDOCKED) {
+               if (ship!=IHE) {
+                       prout("You cannot abandon Ye Faerie Queene.");
+                       return;
+               }
+       }
+       else {
+               /* Must take shuttle craft to exit */
+               if (game.damage[DSHUTTL]==-1) {
+                       prout("Ye Faerie Queene has no shuttle craft.");
+                       return;
+               }
+               if (game.damage[DSHUTTL]<0) {
+                       prout("Shuttle craft now serving Big Mac's.");
+                       return;
+               }
+               if (game.damage[DSHUTTL]>0) {
+                       prout("Shuttle craft damaged.");
+                       return;
+               }
+               if (landed==1) {
+                       prout("You must be aboard the Enterprise.");
+                       return;
+               }
+               if (iscraft!=1) {
+                       prout("Shuttle craft not currently available.");
+                       return;
+               }
+               /* Print abandon ship messages */
+               skip(1);
+               prouts("***ABANDON SHIP!  ABANDON SHIP!");
+               skip(1);
+               prouts("***ALL HANDS ABANDON SHIP!");
+               skip(2);
+               prout("Captain and crew escape in shuttle craft.");
+               prout("Remainder of ship's complement beam down");
+               prout("to nearest habitable planet.");
+               if (game.state.rembase==0) {
+                       /* Ops! no place to go... */
+                       finish(FABANDN);
+                       return;
+               }
+               /* If at least one base left, give 'em the Faerie Queene */
+               skip(1);
+               icrystl = 0; /* crystals are lost */
+               nprobes = 0; /* No probes */
+               prout("You are captured by Klingons and released to");
+               prout("the Federation in a prisoner-of-war exchange.");
+               nb = Rand()*game.state.rembase+1;
+               /* Set up quadrant and position FQ adjacient to base */
+               if (quadx!=game.state.baseqx[nb] || quady!=game.state.baseqy[nb]) {
+                       quadx = game.state.baseqx[nb];
+                       quady = game.state.baseqy[nb];
+                       sectx = secty = 5;
+                       newqad(1);
+               }
+               for (;;) {
+                       /* position next to base by trial and error */
+                       game.quad[sectx][secty] = IHDOT;
+                       for (l = 1; l <= QUADSIZE; l++) {
+                               sectx = 3.0*Rand() - 1.0 + basex;
+                               secty = 3.0*Rand() - 1.0 + basey;
+                               if (sectx >= 1 && sectx <= QUADSIZE &&
+                                       secty >= 1 && secty <= QUADSIZE &&
+                                       game.quad[sectx][secty] == IHDOT) break;
+                       }
+                       if (l < QUADSIZE+1) break; /* found a spot */
+                       sectx=QUADSIZE/2;
+                       secty=QUADSIZE/2;
+                       newqad(1);
+               }
+       }
+       /* Get new commission */
+       game.quad[sectx][secty] = ship = IHF;
+       prout("Starfleet puts you in command of another ship,");
+       prout("the Faerie Queene, which is antiquated but,");
+       prout("still useable.");
+       if (icrystl!=0) prout("The dilithium crystals have been moved.");
+       imine=0;
+       iscraft=0; /* Gallileo disappears */
+       /* Resupply ship */
+       condit=IHDOCKED;
+       for (l = 1; l <= NDEVICES; l++) game.damage[l] = 0.0;
+       game.damage[DSHUTTL] = -1;
+       energy = inenrg = 3000.0;
+       shield = inshld = 1250.0;
+       torps = intorps = 6;
+       lsupres=inlsr=3.0;
+       shldup=0;
+       warpfac=5.0;
+       wfacsq=25.0;
+       return;
+}
+       
+void setup(int needprompt) {
+       int i,j, krem, klumper;
+       int ix, iy;
+#ifdef DEBUG
+       idebug = 0;
+#endif
+       //  Decide how many of everything
+       if (choose(needprompt)) return; // frozen game
+       // Prepare the Enterprise
+        alldone = gamewon = 0;
+       ship = IHE;
+       energy = inenrg = 5000.0;
+       shield = inshld = 2500.0;
+       shldchg = shldup = 0;
+       inlsr = 4.0;
+       lsupres = 4.0;
+       iran8(&quadx, &quady);
+       iran10(&sectx, &secty);
+       torps = intorps = 10;
+       nprobes = (int)(3.0*Rand() + 2.0);      /* Give them 2-4 of these wonders */
+       warpfac = 5.0;
+       wfacsq = warpfac * warpfac;
+       for (i=0; i <= NDEVICES; i++) game.damage[i] = 0.0;
+       // Set up assorted game parameters
+       batx = baty = 0;
+       game.state.date = indate = 100.0*(int)(31.0*Rand()+20.0);
+       game.state.killk = game.state.killc = nkinks = nhelp = resting = casual = game.state.nromkl = 0;
+       isatb = iscate = imine = icrystl = icraft = game.state.nsckill = game.state.nplankl = 0;
+        game.state.starkl = game.state.basekl = 0;
+       iscraft = 1;
+       landed = -1;
+       alive = 1;
+       docfac = 0.25;
+       for (i = 1; i <= GALSIZE; i++)
+               for (j = 1; j <= GALSIZE; j++) game.state.newstuf[i][j] = game.starch[i][j] = 0;
+       // Initialize times for extraneous events
+       game.future[FSNOVA] = game.state.date + expran(0.5 * intime);
+       game.future[FTBEAM] = game.state.date + expran(1.5 * (intime / game.state.remcom));
+       game.future[FSNAP] = game.state.date + 1.0 + Rand(); // Force an early snapshot
+       game.future[FBATTAK] = game.state.date + expran(0.3*intime);
+       game.future[FCDBAS] = 1e30;
+       game.future[FSCMOVE] = game.state.nscrem ? game.state.date+0.2777 : 1e30;
+       game.future[FSCDBAS] = 1e30;
+       game.future[FDSPROB] = 1e30;
+       // Starchart is functional
+       stdamtim = 1e30;
+       // Put stars in the galaxy
+       instar = 0;
+       for (i=1; i<=GALSIZE; i++)
+               for (j=1; j<=GALSIZE; j++) {
+                       int k = Rand()*9.0 + 1.0;
+                       instar += k;
+                       game.state.galaxy[i][j] = k;
+               }
+       // Locate star bases in galaxy
+       for (i = 1; i <= inbase; i++) {
+               int contflag;
+               do {
+                       do iran8(&ix, &iy);
+                       while (game.state.galaxy[ix][iy] >= BASE_PLACE);
+                       contflag = FALSE;
+                       for (j = i-1; j > 0; j--) {
+                               /* Improved placement algorithm to spread out bases */
+                               double distq = square(ix-game.state.baseqx[j]) + square(iy-game.state.baseqy[j]);
+                               if (distq < 6.0*(BASEMAX-inbase) && Rand() < 0.75) {
+                                       contflag = TRUE;
+#ifdef DEBUG
+                                       proutn("DEBUG: Abandoning base #%d at %d-%d\n", i, ix, iy);
+#endif
+                                       break;
+                               }
+#ifdef DEBUG
+                               else if (distq < 6.0 * (BASEMAX-inbase)) {
+                                       proutn("DEBUG: saving base #%d, close to #%d\n", i, j);
+                               }
+#endif
+                       }
+               } while (contflag);
+                       
+               game.state.baseqx[i] = ix;
+               game.state.baseqy[i] = iy;
+               game.starch[ix][iy] = -1;
+               game.state.galaxy[ix][iy] += BASE_PLACE;
+       }
+       // Position ordinary Klingon Battle Cruisers
+       krem = inkling - incom - game.state.nscrem;
+       klumper = 0.25*skill*(9.0-length)+1.0;
+       if (klumper > 9) klumper = 9; // Can't have more than 9 in quadrant
+       do {
+               double r = Rand();
+               int klump = (1.0 - r*r)*klumper;
+               if (klump > krem) klump = krem;
+               krem -= klump;
+               klump *= 100;
+               do iran8(&ix, &iy);
+               while (game.state.galaxy[ix][iy] + klump >= SUPERNOVA_PLACE);
+               game.state.galaxy[ix][iy] += klump;
+       } while (krem > 0);
+       // Position Klingon Commander Ships
+#ifdef DEBUG
+       klumper = 1;
+#endif
+       for (i = 1; i <= incom; i++) {
+               do {
+                       do { /* IF debugging, put commanders by bases, always! */
+#ifdef DEBUG
+                               if (idebug && klumper <= inbase) {
+                                       ix = game.state.baseqx[klumper];
+                                       iy = game.state.baseqy[klumper];
+                                       klumper++;
+                               }
+                               else
+#endif
+                                       iran8(&ix, &iy);
+                       }
+                       while ((game.state.galaxy[ix][iy] < 99 && Rand() < 0.75)||
+                                  game.state.galaxy[ix][iy]>899);
+                       // check for duplicate
+                       for (j = 1; j < i; j++)
+                               if (game.state.cx[j]==ix && game.state.cy[j]==iy) break;
+               } while (j < i);
+               game.state.galaxy[ix][iy] += ENEMY_PLACE;
+               game.state.cx[i] = ix;
+               game.state.cy[i] = iy;
+       }
+       // Locate planets in galaxy
+       for (i = 0; i < inplan; i++) {
+               do iran8(&ix, &iy);
+               while (game.state.newstuf[ix][iy] > 0);
+               game.state.newstuf[ix][iy] = 1;
+               game.state.plnets[i].x = ix;
+               game.state.plnets[i].y = iy;
+               game.state.plnets[i].pclass = Rand()*3.0; // Planet class M N or O
+               game.state.plnets[i].crystals = 1.5*Rand();             // 1 in 3 chance of crystals
+               game.state.plnets[i].known = unknown;
+       }
+       // Locate Romulans
+       for (i = 1; i <= game.state.nromrem; i++) {
+               iran8(&ix, &iy);
+               game.state.newstuf[ix][iy] += ROMULAN_PLACE;
+       }
+       // Locate the Super Commander
+       if (game.state.nscrem > 0) {
+               do iran8(&ix, &iy);
+               while (game.state.galaxy[ix][iy] >= 900);
+               game.state.isx = ix;
+               game.state.isy = iy;
+               game.state.galaxy[ix][iy] += ENEMY_PLACE;
+       }
+       // Place thing (in tournament game, thingx == -1, don't want one!)
+       if (thingx != -1) {
+               iran8(&thingx, &thingy);
+       }
+       else {
+               thingx = thingy = 0;
+       }
+
+//     idate = date;
+       skip(2);
+       game.state.snap = 0;
+               
+       if (skill == 1) {
+               prout("It is stardate %d. The Federation is being attacked by",
+                          (int)game.state.date);
+               prout("a deadly Klingon invasion force. As captain of the United");
+               prout("Starship U.S.S. Enterprise, it is your mission to seek out");
+               prout("and destroy this invasion force of %d battle cruisers.",
+                          inkling);
+               prout("You have an initial allotment of %d stardates to complete", (int)intime);
+               prout("your mission.  As you proceed you may be given more time.");
+               prout("");
+               prout("You will have %d supporting starbases.", inbase);
+               proutn("Starbase locations-  ");
+       }
+       else {
+               prout("Stardate %d.", (int)game.state.date);
+               prout("");
+               prout("%d Klingons.", inkling);
+               prout("An unknown number of Romulans.");
+               if (game.state.nscrem) prout("and one (GULP) Super-Commander.");
+                       prout("%d stardates.",(int)intime);
+                       proutn("%d starbases in ", inbase);
+       }
+       for (i = 1; i <= inbase; i++) {
+               proutn(cramlc(0, game.state.baseqx[i], game.state.baseqy[i]));
+               proutn("  ");
+       }
+       skip(2);
+       proutn("The Enterprise is currently in ");
+       proutn(cramlc(quadrant, quadx, quady));
+       proutn(" ");
+        proutn(cramlc(sector, sectx, secty));
+       skip(2);
+       prout("Good Luck!");
+       if (game.state.nscrem) prout("  YOU'LL NEED IT.");
+        waitfor();
+       newqad(0);
+        if (nenhere-iqhere-ithere) shldup=1.0;
+       if (neutz) attack(0);   // bad luck to start in a Romulan Neutral Zone
+}
+
+void randomize(void) {
+       srand((int)time(NULL));
+}
+
+int choose(int needprompt) {
+        while (TRUE) {
+       tourn = 0;
+       thawed = 0;
+       skill = 0;
+       length = 0;
+               if (needprompt) /* Can start with command line options */
+                        proutn("Would you like a regular, tournament, or saved game? ");
+               scan();
+               if (strlen(citem)==0) continue; // Try again
+               if (isit("tournament")) {
+                       while (scan() == IHEOL) {
+                               proutn("Type in tournament number-");
+                       }
+                       if (aaitem == 0) {
+                               chew();
+                               continue; // We don't want a blank entry
+                       }
+                       tourn = (int)aaitem;
+                       thingx = -1;
+                       srand((unsigned int)(int)aaitem);
+                       break;
+               }
+               if (isit("saved") || isit("frozen")) {
+                        if (thaw()) continue;
+                       chew();
+                       if (*game.passwd==0) continue;
+                       if (!alldone) thawed = 1; // No plaque if not finished
+                        report();
+                        waitfor();
+                       return TRUE;
+               }
+                if (isit("regular")) break;
+               proutn("What is \"");
+               proutn(citem);
+               prout("\"?");
+               chew();
+       }
+       while (length==0 || skill==0) {
+               if (scan() == IHALPHA) {
+                       if (isit("short")) length = 1;
+                       else if (isit("medium")) length = 2;
+                       else if (isit("long")) length = 4;
+                       else if (isit("novice")) skill = 1;
+                       else if (isit("fair")) skill = 2;
+                       else if (isit("good")) skill = 3;
+                       else if (isit("expert")) skill = 4;
+                       else if (isit("emeritus")) skill = 5;
+                       else {
+                               proutn("What is \"");
+                               proutn(citem);
+                               prout("\"?");
+                       }
+               }
+               else {
+                       chew();
+                       if (length==0) proutn("Would you like a Short, Medium, or Long game? ");
+                       else if (skill == 0) proutn("Are you a Novice, Fair, Good, Expert, or Emeritus player? ");
+               }
+       }
+       setpassword();
+#ifdef DEBUG
+       if (strcmp(game.passwd, "debug")==0) idebug = 1;
+#endif
+
+       // Use parameters to generate initial values of things
+       damfac = 0.5 * skill;
+       game.state.rembase = 3.0*Rand()+2.0;
+       inbase = game.state.rembase;
+       inplan = (PLNETMAX/2) + (PLNETMAX/2+1)*Rand();
+       game.state.nromrem = (2.0+Rand())*skill;
+       game.state.nscrem = (skill > 2? 1 : 0);
+       game.state.remtime = 7.0 * length;
+       intime = game.state.remtime;
+       game.state.remkl = 2.0*intime*((skill+1 - 2*Rand())*skill*0.1+.15);
+       inkling = game.state.remkl;
+       incom = skill + 0.0625*inkling*Rand();
+       game.state.remcom= min(10, incom);
+       incom = game.state.remcom;
+       game.state.remres = (inkling+4*incom)*intime;
+       inresor = game.state.remres;
+       if (inkling > 50) {
+               inbase = (game.state.rembase += 1);
+       }
+       return FALSE;
+}
+
+void dropin(int iquad, int *ix, int *iy) {
+       do iran10(ix, iy);
+       while (game.quad[*ix][*iy] != IHDOT);
+       game.quad[*ix][*iy] = iquad;
+}
+
+void newcnd(void) {
+       condit = IHGREEN;
+       if (energy < 1000.0) condit = IHYELLOW;
+       if (game.state.galaxy[quadx][quady] >= ENEMY_PLACE || game.state.newstuf[quadx][quady] > 9)
+               condit = IHRED;
+        if (!alive) condit=IHDEAD;
+}
+
+
+void newqad(int shutup) {
+       int quadnum = game.state.galaxy[quadx][quady];
+       int newnum = game.state.newstuf[quadx][quady];
+       int i, j, ix, iy, nplan;
+
+       iattak = 1;
+       justin = 1;
+       basex = basey = 0;
+       klhere = 0;
+       comhere = 0;
+       plnetx = plnety = 0;
+       ishere = 0;
+       irhere = 0;
+       iplnet = 0;
+       nenhere = 0;
+       neutz = 0;
+       inorbit = 0;
+       landed = -1;
+       ientesc = 0;
+       ithere = 0;
+        iqhere=0;
+        iqengry=0;
+       iseenit = 0;
+       if (iscate) {
+               // Attempt to escape Super-commander, so tbeam back!
+               iscate = 0;
+               ientesc = 1;
+       }
+       // Clear quadrant
+       for (i=1; i <= QUADSIZE; i++)
+               for (j=1; j <= QUADSIZE; j++) game.quad[i][j] = IHDOT;
+       // cope with supernova
+       if (quadnum > 999) {
+               return;
+       }
+       klhere = quadnum/ENEMY_PLACE;
+       irhere = newnum/ROMULAN_PLACE;
+       nplan = newnum%10;
+       nenhere = klhere + irhere;
+
+       // Position Starship
+       game.quad[sectx][secty] = ship;
+
+       if (quadnum >= ENEMY_PLACE) {
+               // Position ordinary Klingons
+               quadnum -= ENEMY_PLACE*klhere;
+               for (i = 1; i <= klhere; i++) {
+                       dropin(IHK, &ix, &iy);
+                       game.kx[i] = ix;
+                       game.ky[i] = iy;
+                       game.kdist[i] = game.kavgd[i] = sqrt(square(sectx-ix) + square(secty-iy));
+                       game.kpower[i] = Rand()*150.0 +300.0 +25.0*skill;
+               }
+               // If we need a commander, promote a Klingon
+               for (i = 1; i <= game.state.remcom ; i++) 
+                       if (game.state.cx[i]==quadx && game.state.cy[i]==quady) break;
+                       
+               if (i <= game.state.remcom) {
+                       game.quad[ix][iy] = IHC;
+                       game.kpower[klhere] = 950.0+400.0*Rand()+50.0*skill;
+                       comhere = 1;
+               }
+
+               // If we need a super-commander, promote a Klingon
+               if (quadx == game.state.isx && quady == game.state.isy) {
+                       game.quad[game.kx[1]][game.ky[1]] = IHS;
+                       game.kpower[1] = 1175.0 + 400.0*Rand() + 125.0*skill;
+                        iscate = game.state.remkl>1;
+                       ishere = 1;
+               }
+       }
+       // Put in Romulans if needed
+       for (i = klhere+1; i <= nenhere; i++) {
+               dropin(IHR, &ix, &iy);
+               game.kx[i] = ix;
+               game.ky[i] = iy;
+               game.kdist[i] = game.kavgd[i] = sqrt(square(sectx-ix) + square(secty-iy));
+               game.kpower[i] = Rand()*400.0 + 450.0 + 50.0*skill;
+       }
+       // If quadrant needs a starbase, put it in
+       if (quadnum >= BASE_PLACE) {
+               quadnum -= BASE_PLACE;
+               dropin(IHB, &basex, &basey);
+       }
+       
+       if (nplan) {
+               // If quadrant needs a planet, put it in
+               for (i=0; i < inplan; i++)
+                       if (game.state.plnets[i].x == quadx && game.state.plnets[i].y == quady) break;
+               if (i < inplan) {
+                       iplnet = i;
+                       dropin(IHP, &plnetx, &plnety);
+               }
+       }
+       // Check for condition
+       newcnd();
+       // And finally the stars
+       for (i = 1; i <= quadnum; i++) dropin(IHSTAR, &ix, &iy);
+
+       // Check for RNZ
+       if (irhere > 0 && klhere == 0) {
+               neutz = 1;
+               if (game.damage[DRADIO] <= 0.0) {
+                       skip(1);
+                       prout("LT. Uhura- \"Captain, an urgent message.");
+                       prout("  I'll put it on audio.\"  CLICK");
+                       skip(1);
+                       prout("INTRUDER! YOU HAVE VIOLATED THE ROMULAN NEUTRAL ZONE.");
+                       prout("LEAVE AT ONCE, OR YOU WILL BE DESTROYED!");
+               }
+       }
+
+       if (shutup==0) {
+               // Put in THING if needed
+               if (thingx == quadx && thingy == quady) {
+                       dropin(IHQUEST, &ix, &iy);
+                        iran8(&thingx, &thingy);
+                        nenhere++;
+                        iqhere=1;
+                        game.kx[nenhere] = ix;
+                        game.ky[nenhere] = iy;
+                        game.kdist[nenhere] = game.kavgd[nenhere] =
+                           sqrt(square(sectx-ix) + square(secty-iy));
+                        game.kpower[nenhere] = Rand()*6000.0 +500.0 +250.0*skill;
+                       if (game.damage[DSRSENS] == 0.0) {
+                               skip(1);
+                               prout("MR. SPOCK- \"Captain, this is most unusual.");
+                               prout("    Please examine your short-range scan.\"");
+                       }
+               }
+       }
+
+        // Decide if quadrant needs a Tholian
+        if ((skill < 3 && Rand() <= 0.02) ||   /* Lighten up if skill is low */
+                (skill == 3 && Rand() <= 0.05) ||
+                (skill > 3 && Rand() <= 0.08)
+#ifdef DEBUG
+                || strcmp(passwd, "tholianx")==0
+#endif
+                ) {
+                do {
+                        ithx = Rand() > 0.5 ? QUADSIZE : 1;
+                        ithy = Rand() > 0.5 ? QUADSIZE : 1;
+                } while (game.quad[ithx][ithy] != IHDOT);
+                game.quad[ithx][ithy] = IHT;
+                ithere = 1;
+                nenhere++;
+                game.kx[nenhere] = ithx;
+                game.ky[nenhere] = ithy;
+                game.kdist[nenhere] = game.kavgd[nenhere] =
+                   sqrt(square(sectx-ithx) + square(secty-ithy));
+                game.kpower[nenhere] = Rand()*400.0 +100.0 +25.0*skill;
+                /* Reserve unocupied corners */
+                if (game.quad[1][1]==IHDOT) game.quad[1][1] = 'X';
+                if (game.quad[1][QUADSIZE]==IHDOT) game.quad[1][QUADSIZE] = 'X';
+                if (game.quad[QUADSIZE][1]==IHDOT) game.quad[QUADSIZE][1] = 'X';
+                if (game.quad[QUADSIZE][QUADSIZE]==IHDOT) game.quad[QUADSIZE][QUADSIZE] = 'X';
+        }
+        sortkl();
+
+       // Put in a few black holes
+       for (i = 1; i <= 3; i++)
+               if (Rand() > 0.5) dropin(IHBLANK, &ix, &iy);
+
+       // Take out X's in corners if Tholian present
+       if (ithere) {
+               if (game.quad[1][1]=='X') game.quad[1][1] = IHDOT;
+               if (game.quad[1][QUADSIZE]=='X') game.quad[1][QUADSIZE] = IHDOT;
+               if (game.quad[QUADSIZE][1]=='X') game.quad[QUADSIZE][1] = IHDOT;
+               if (game.quad[QUADSIZE][QUADSIZE]=='X') game.quad[QUADSIZE][QUADSIZE] = IHDOT;
+       }               
+}
+
+void sortkl(void) {
+       double t;
+       int sw, j, k;
+
+       // The author liked bubble sort. So we will use it. :-(
+
+        if (nenhere-iqhere-ithere < 2) return;
+
+       do {
+               sw = FALSE;
+               for (j = 1; j < nenhere; j++)
+                       if (game.kdist[j] > game.kdist[j+1]) {
+                               sw = TRUE;
+                               t = game.kdist[j];
+                               game.kdist[j] = game.kdist[j+1];
+                               game.kdist[j+1] = t;
+                               t = game.kavgd[j];
+                               game.kavgd[j] = game.kavgd[j+1];
+                               game.kavgd[j+1] = t;
+                               k = game.kx[j];
+                               game.kx[j] = game.kx[j+1];
+                               game.kx[j+1] = k;
+                               k = game.ky[j];
+                               game.ky[j] = game.ky[j+1];
+                               game.ky[j+1] = k;
+                               t = game.kpower[j];
+                               game.kpower[j] = game.kpower[j+1];
+                               game.kpower[j+1] = t;
+                       }
+       } while (sw);
+}