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