Get rid of FORTRANisms.
[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 (l = 1; l <= QUADSIZE; l++) {
160                 sectx = 3.0*Rand() - 1.0 + basex;
161                 secty = 3.0*Rand() - 1.0 + basey;
162                 if (sectx >= 1 && sectx <= QUADSIZE &&
163                     secty >= 1 && secty <= QUADSIZE &&
164                     game.quad[sectx][secty] == IHDOT) break;
165             }
166             if (l < QUADSIZE+1) break; /* found a spot */
167             sectx=QUADSIZE/2;
168             secty=QUADSIZE/2;
169             newqad(1);
170         }
171     }
172     /* Get new commission */
173     game.quad[sectx][secty] = ship = IHF;
174     prout("Starfleet puts you in command of another ship,");
175     prout("the Faerie Queene, which is antiquated but,");
176     prout("still useable.");
177     if (icrystl!=0) prout("The dilithium crystals have been moved.");
178     imine=0;
179     iscraft=0; /* Gallileo disappears */
180     /* Resupply ship */
181     condit=IHDOCKED;
182     for (l = 1; l <= NDEVICES; l++) 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++) game.damage[i] = 0.0;
218     // Set up assorted game parameters
219     batx = baty = 0;
220     game.state.date = indate = 100.0*(int)(31.0*Rand()+20.0);
221     game.state.killk = game.state.killc = nkinks = nhelp = resting = casual = game.state.nromkl = 0;
222     isatb = iscate = imine = icrystl = icraft = game.state.nsckill = game.state.nplankl = 0;
223     game.state.starkl = game.state.basekl = 0;
224     iscraft = 1;
225     landed = -1;
226     alive = 1;
227     docfac = 0.25;
228     for (i = 1; i <= GALSIZE; i++)
229         for (j = 1; j <= GALSIZE; j++) game.state.newstuf[i][j] = game.starch[i][j] = 0;
230     // Initialize times for extraneous events
231     game.future[FSNOVA] = game.state.date + expran(0.5 * intime);
232     game.future[FTBEAM] = game.state.date + expran(1.5 * (intime / game.state.remcom));
233     game.future[FSNAP] = game.state.date + 1.0 + Rand(); // Force an early snapshot
234     game.future[FBATTAK] = game.state.date + expran(0.3*intime);
235     game.future[FCDBAS] = 1e30;
236     game.future[FSCMOVE] = game.state.nscrem ? game.state.date+0.2777 : 1e30;
237     game.future[FSCDBAS] = 1e30;
238     game.future[FDSPROB] = 1e30;
239     // Starchart is functional
240     stdamtim = 1e30;
241     // Put stars in the galaxy
242     instar = 0;
243     for (i=1; i<=GALSIZE; i++)
244         for (j=1; j<=GALSIZE; j++) {
245             int k = Rand()*9.0 + 1.0;
246             instar += k;
247             game.state.galaxy[i][j] = k * STAR_PLACE;
248         }
249     // Locate star bases in galaxy
250     for (i = 1; i <= inbase; i++) {
251         int contflag;
252         do {
253             do iran(GALSIZE, &ix, &iy);
254             while (BASES(game.state.galaxy[ix][iy]));
255             contflag = FALSE;
256             for (j = i-1; j > 0; j--) {
257                 /* Improved placement algorithm to spread out bases */
258                 double distq = square(ix-game.state.baseqx[j]) + square(iy-game.state.baseqy[j]);
259                 if (distq < 6.0*(BASEMAX-inbase) && Rand() < 0.75) {
260                     contflag = TRUE;
261 #ifdef DEBUG
262                     proutn("DEBUG: Abandoning base #%d at %d-%d\n", i, ix, iy);
263 #endif
264                     break;
265                 }
266 #ifdef DEBUG
267                 else if (distq < 6.0 * (BASEMAX-inbase)) {
268                     proutn("DEBUG: saving base #%d, close to #%d\n", i, j);
269                 }
270 #endif
271             }
272         } while (contflag);
273                         
274         game.state.baseqx[i] = ix;
275         game.state.baseqy[i] = iy;
276         game.starch[ix][iy] = CHART_UNKNOWN;
277         game.state.galaxy[ix][iy] += BASE_PLACE;
278     }
279     // Position ordinary Klingon Battle Cruisers
280     krem = inkling - incom - game.state.nscrem;
281     klumper = 0.25*skill*(9.0-length)+1.0;
282     if (klumper > 9) klumper = 9; // Can't have more than 9 in quadrant
283     do {
284         double r = Rand();
285         int klump = (1.0 - r*r)*klumper;
286         if (klump > krem) klump = krem;
287         krem -= klump;
288         klump *= KLINGON_PLACE;
289         do iran(GALSIZE, &ix, &iy);
290         while (game.state.galaxy[ix][iy] + klump >= SUPERNOVA_PLACE);
291         game.state.galaxy[ix][iy] += klump;
292     } while (krem > 0);
293     // Position Klingon Commander Ships
294 #ifdef DEBUG
295     klumper = 1;
296 #endif
297     for (i = 1; i <= incom; i++) {
298         do {
299             do { /* IF debugging, put commanders by bases, always! */
300 #ifdef DEBUG
301                 if (idebug && klumper <= inbase) {
302                     ix = game.state.baseqx[klumper];
303                     iy = game.state.baseqy[klumper];
304                     klumper++;
305                 }
306                 else
307 #endif
308                     iran(GALSIZE, &ix, &iy);
309             }
310             while ((game.state.galaxy[ix][iy] <= KLINGON_PLACE && Rand() < 0.75)||
311                    NOEXIT(game.state.galaxy[ix][iy]));
312             // check for duplicate
313             for (j = 1; j < i; j++)
314                 if (game.state.cx[j]==ix && game.state.cy[j]==iy) break;
315         } while (j < i);
316         game.state.galaxy[ix][iy] += KLINGON_PLACE;
317         game.state.cx[i] = ix;
318         game.state.cy[i] = iy;
319     }
320     // Locate planets in galaxy
321     for (i = 0; i < inplan; i++) {
322         do iran(GALSIZE, &ix, &iy);
323         while (game.state.newstuf[ix][iy] > 0);
324         game.state.newstuf[ix][iy] = 1;
325         game.state.plnets[i].x = ix;
326         game.state.plnets[i].y = iy;
327         game.state.plnets[i].pclass = Rand()*3.0; // Planet class M N or O
328         game.state.plnets[i].crystals = 1.5*Rand();             // 1 in 3 chance of crystals
329         game.state.plnets[i].known = unknown;
330     }
331     // Locate Romulans
332     for (i = 1; i <= game.state.nromrem; i++) {
333         iran(GALSIZE, &ix, &iy);
334         game.state.newstuf[ix][iy] += ROMULAN_PLACE;
335     }
336     // Locate the Super Commander
337     if (game.state.nscrem > 0) {
338         do iran(GALSIZE, &ix, &iy);
339         while (game.state.galaxy[ix][iy] >= 900);
340         game.state.isx = ix;
341         game.state.isy = iy;
342         game.state.galaxy[ix][iy] += KLINGON_PLACE;
343     }
344     // Place thing (in tournament game, thingx == -1, don't want one!)
345     if (thingx != -1) {
346         iran(GALSIZE, &thingx, &thingy);
347     }
348     else {
349         thingx = thingy = 0;
350     }
351
352 //      idate = date;
353     skip(2);
354     game.state.snap = 0;
355                 
356     if (skill == SKILL_NOVICE) {
357         prout("It is stardate %d. The Federation is being attacked by",
358               (int)game.state.date);
359         prout("a deadly Klingon invasion force. As captain of the United");
360         prout("Starship U.S.S. Enterprise, it is your mission to seek out");
361         prout("and destroy this invasion force of %d battle cruisers.",
362               inkling);
363         prout("You have an initial allotment of %d stardates to complete", (int)intime);
364         prout("your mission.  As you proceed you may be given more time.");
365         prout("");
366         prout("You will have %d supporting starbases.", inbase);
367         proutn("Starbase locations-  ");
368     }
369     else {
370         prout("Stardate %d.", (int)game.state.date);
371         prout("");
372         prout("%d Klingons.", inkling);
373         prout("An unknown number of Romulans.");
374         if (game.state.nscrem) prout("and one (GULP) Super-Commander.");
375         prout("%d stardates.",(int)intime);
376         proutn("%d starbases in ", inbase);
377     }
378     for (i = 1; i <= inbase; i++) {
379         proutn(cramlc(0, game.state.baseqx[i], game.state.baseqy[i]));
380         proutn("  ");
381     }
382     skip(2);
383     proutn("The Enterprise is currently in ");
384     proutn(cramlc(quadrant, quadx, quady));
385     proutn(" ");
386     proutn(cramlc(sector, sectx, secty));
387     skip(2);
388     prout("Good Luck!");
389     if (game.state.nscrem) prout("  YOU'LL NEED IT.");
390     waitfor();
391     newqad(0);
392     if (nenhere-iqhere-ithere) shldup=1.0;
393     if (neutz) attack(0);       // bad luck to start in a Romulan Neutral Zone
394 }
395
396 void randomize(void) 
397 {
398     srand((int)time(NULL));
399 }
400
401 int choose(int needprompt) 
402 {
403     while (TRUE) {
404         tourn = 0;
405         thawed = 0;
406         skill = SKILL_NONE;
407         length = 0;
408         if (needprompt) /* Can start with command line options */
409             proutn("Would you like a regular, tournament, or saved game? ");
410         scan();
411         if (strlen(citem)==0) continue; // Try again
412         if (isit("tournament")) {
413             while (scan() == IHEOL) {
414                 proutn("Type in tournament number-");
415             }
416             if (aaitem == 0) {
417                 chew();
418                 continue; // We don't want a blank entry
419             }
420             tourn = (int)aaitem;
421             thingx = -1;
422             srand((unsigned int)(int)aaitem);
423             break;
424         }
425         if (isit("saved") || isit("frozen")) {
426             if (thaw()) continue;
427             chew();
428             if (*game.passwd==0) continue;
429             if (!alldone) thawed = 1; // No plaque if not finished
430             report();
431             waitfor();
432             return TRUE;
433         }
434         if (isit("regular")) break;
435         proutn("What is \"");
436         proutn(citem);
437         prout("\"?");
438         chew();
439     }
440     while (length==0 || skill==SKILL_NONE) {
441         if (scan() == IHALPHA) {
442             if (isit("short")) length = 1;
443             else if (isit("medium")) length = 2;
444             else if (isit("long")) length = 4;
445             else if (isit("novice")) skill = SKILL_NOVICE;
446             else if (isit("fair")) skill = SKILL_FAIR;
447             else if (isit("good")) skill = SKILL_GOOD;
448             else if (isit("expert")) skill = SKILL_EXPERT;
449             else if (isit("emeritus")) skill = SKILL_EMERITUS;
450             else {
451                 proutn("What is \"");
452                 proutn(citem);
453                 prout("\"?");
454             }
455         }
456         else {
457             chew();
458             if (length==0) proutn("Would you like a Short, Medium, or Long game? ");
459             else if (skill == SKILL_NONE) proutn("Are you a Novice, Fair, Good, Expert, or Emeritus player? ");
460         }
461     }
462     setpassword();
463 #ifdef DEBUG
464     if (strcmp(game.passwd, "debug")==0) idebug = 1;
465 #endif
466
467     // Use parameters to generate initial values of things
468     damfac = 0.5 * skill;
469     game.state.rembase = 3.0*Rand()+2.0;
470     inbase = game.state.rembase;
471     inplan = (PLNETMAX/2) + (PLNETMAX/2+1)*Rand();
472     game.state.nromrem = (2.0+Rand())*skill;
473     game.state.nscrem = (skill > SKILL_FAIR ? 1 : 0);
474     game.state.remtime = 7.0 * length;
475     intime = game.state.remtime;
476     game.state.remkl = 2.0*intime*((skill+1 - 2*Rand())*skill*0.1+.15);
477     inkling = game.state.remkl;
478     incom = skill + 0.0625*inkling*Rand();
479     game.state.remcom= min(10, incom);
480     incom = game.state.remcom;
481     game.state.remres = (inkling+4*incom)*intime;
482     inresor = game.state.remres;
483     if (inkling > 50) {
484         inbase = (game.state.rembase += 1);
485     }
486     return FALSE;
487 }
488
489 void dropin(int iquad, int *ix, int *iy) 
490 {
491     do iran(QUADSIZE, ix, iy);
492     while (game.quad[*ix][*iy] != IHDOT);
493     game.quad[*ix][*iy] = iquad;
494 }
495
496 void newcnd(void) 
497 {
498     condit = IHGREEN;
499     if (energy < 1000.0) condit = IHYELLOW;
500     if (game.state.galaxy[quadx][quady] >= KLINGON_PLACE || game.state.newstuf[quadx][quady] >= ROMULAN_PLACE)
501         condit = IHRED;
502     if (!alive) condit=IHDEAD;
503 }
504
505
506 void newqad(int shutup) 
507 {
508     int quadnum = game.state.galaxy[quadx][quady];
509     int newnum = game.state.newstuf[quadx][quady];
510     int i, j, ix, iy, nplan;
511
512     iattak = 1;
513     justin = 1;
514     basex = basey = 0;
515     klhere = 0;
516     comhere = 0;
517     plnetx = plnety = 0;
518     ishere = 0;
519     irhere = 0;
520     iplnet = 0;
521     nenhere = 0;
522     neutz = 0;
523     inorbit = 0;
524     landed = -1;
525     ientesc = 0;
526     ithere = 0;
527     iqhere=0;
528     iqengry=0;
529     iseenit = 0;
530     if (iscate) {
531         // Attempt to escape Super-commander, so tbeam back!
532         iscate = 0;
533         ientesc = 1;
534     }
535     // Clear quadrant
536     for (i=1; i <= QUADSIZE; i++)
537         for (j=1; j <= QUADSIZE; j++) game.quad[i][j] = IHDOT;
538     // cope with supernova
539     if (quadnum >= SUPERNOVA_PLACE) {
540         return;
541     }
542     klhere = KLINGONS(quadnum);
543     irhere = ROMULANS(newnum);
544     nplan = newnum%10;
545     nenhere = klhere + irhere;
546
547     // Position Starship
548     game.quad[sectx][secty] = ship;
549
550     if (quadnum >= KLINGON_PLACE) {
551         // Position ordinary Klingons
552         quadnum -= KLINGON_PLACE*klhere;
553         for (i = 1; i <= klhere; i++) {
554             dropin(IHK, &ix, &iy);
555             game.kx[i] = ix;
556             game.ky[i] = iy;
557             game.kdist[i] = game.kavgd[i] = sqrt(square(sectx-ix) + square(secty-iy));
558             game.kpower[i] = Rand()*150.0 +300.0 +25.0*skill;
559         }
560         // If we need a commander, promote a Klingon
561         for (i = 1; i <= game.state.remcom ; i++) 
562             if (game.state.cx[i]==quadx && game.state.cy[i]==quady) break;
563                         
564         if (i <= game.state.remcom) {
565             game.quad[ix][iy] = IHC;
566             game.kpower[klhere] = 950.0+400.0*Rand()+50.0*skill;
567             comhere = 1;
568         }
569
570         // If we need a super-commander, promote a Klingon
571         if (quadx == game.state.isx && quady == game.state.isy) {
572             game.quad[game.kx[1]][game.ky[1]] = IHS;
573             game.kpower[1] = 1175.0 + 400.0*Rand() + 125.0*skill;
574             iscate = game.state.remkl>1;
575             ishere = 1;
576         }
577     }
578     // Put in Romulans if needed
579     for (i = klhere+1; i <= nenhere; i++) {
580         dropin(IHR, &ix, &iy);
581         game.kx[i] = ix;
582         game.ky[i] = iy;
583         game.kdist[i] = game.kavgd[i] = sqrt(square(sectx-ix) + square(secty-iy));
584         game.kpower[i] = Rand()*400.0 + 450.0 + 50.0*skill;
585     }
586     // If quadrant needs a starbase, put it in
587     if (quadnum >= BASE_PLACE) {
588         quadnum -= BASE_PLACE;
589         dropin(IHB, &basex, &basey);
590     }
591         
592     if (nplan) {
593         // If quadrant needs a planet, put it in
594         for (i=0; i < inplan; i++)
595             if (game.state.plnets[i].x == quadx && game.state.plnets[i].y == quady) break;
596         if (i < inplan) {
597             iplnet = i;
598             dropin(IHP, &plnetx, &plnety);
599         }
600     }
601     // Check for condition
602     newcnd();
603     // And finally the stars
604     for (i = 1; i <= quadnum; i++) dropin(IHSTAR, &ix, &iy);
605
606     // Check for RNZ
607     if (irhere > 0 && klhere == 0) {
608         neutz = 1;
609         if (game.damage[DRADIO] <= 0.0) {
610             skip(1);
611             prout("LT. Uhura- \"Captain, an urgent message.");
612             prout("  I'll put it on audio.\"  CLICK");
613             skip(1);
614             prout("INTRUDER! YOU HAVE VIOLATED THE ROMULAN NEUTRAL ZONE.");
615             prout("LEAVE AT ONCE, OR YOU WILL BE DESTROYED!");
616         }
617     }
618
619     if (shutup==0) {
620         // Put in THING if needed
621         if (thingx == quadx && thingy == quady) {
622             dropin(IHQUEST, &ix, &iy);
623             iran(GALSIZE, &thingx, &thingy);
624             nenhere++;
625             iqhere=1;
626             game.kx[nenhere] = ix;
627             game.ky[nenhere] = iy;
628             game.kdist[nenhere] = game.kavgd[nenhere] =
629                 sqrt(square(sectx-ix) + square(secty-iy));
630             game.kpower[nenhere] = Rand()*6000.0 +500.0 +250.0*skill;
631             if (game.damage[DSRSENS] == 0.0) {
632                 skip(1);
633                 prout("MR. SPOCK- \"Captain, this is most unusual.");
634                 prout("    Please examine your short-range scan.\"");
635             }
636         }
637     }
638
639     // Decide if quadrant needs a Tholian
640     if ((skill < SKILL_GOOD && Rand() <= 0.02) ||   /* Lighten up if skill is low */
641         (skill == SKILL_GOOD && Rand() <= 0.05) ||
642         (skill > SKILL_GOOD && Rand() <= 0.08)
643 #ifdef DEBUG
644         || strcmp(passwd, "tholianx")==0
645 #endif
646         ) {
647         do {
648             ithx = Rand() > 0.5 ? QUADSIZE : 1;
649             ithy = Rand() > 0.5 ? QUADSIZE : 1;
650         } while (game.quad[ithx][ithy] != IHDOT);
651         game.quad[ithx][ithy] = IHT;
652         ithere = 1;
653         nenhere++;
654         game.kx[nenhere] = ithx;
655         game.ky[nenhere] = ithy;
656         game.kdist[nenhere] = game.kavgd[nenhere] =
657             sqrt(square(sectx-ithx) + square(secty-ithy));
658         game.kpower[nenhere] = Rand()*400.0 +100.0 +25.0*skill;
659         /* Reserve unocupied corners */
660         if (game.quad[1][1]==IHDOT) game.quad[1][1] = 'X';
661         if (game.quad[1][QUADSIZE]==IHDOT) game.quad[1][QUADSIZE] = 'X';
662         if (game.quad[QUADSIZE][1]==IHDOT) game.quad[QUADSIZE][1] = 'X';
663         if (game.quad[QUADSIZE][QUADSIZE]==IHDOT) game.quad[QUADSIZE][QUADSIZE] = 'X';
664     }
665     sortkl();
666
667     // Put in a few black holes
668     for (i = 1; i <= 3; i++)
669         if (Rand() > 0.5) dropin(IHBLANK, &ix, &iy);
670
671     // Take out X's in corners if Tholian present
672     if (ithere) {
673         if (game.quad[1][1]=='X') game.quad[1][1] = IHDOT;
674         if (game.quad[1][QUADSIZE]=='X') game.quad[1][QUADSIZE] = IHDOT;
675         if (game.quad[QUADSIZE][1]=='X') game.quad[QUADSIZE][1] = IHDOT;
676         if (game.quad[QUADSIZE][QUADSIZE]=='X') game.quad[QUADSIZE][QUADSIZE] = IHDOT;
677     }           
678 }
679
680 void sortkl(void) 
681 {
682     double t;
683     int sw, j, k;
684
685     // The author liked bubble sort. So we will use it. :-(
686
687     if (nenhere-iqhere-ithere < 2) return;
688
689     do {
690         sw = FALSE;
691         for (j = 1; j < nenhere; j++)
692             if (game.kdist[j] > game.kdist[j+1]) {
693                 sw = TRUE;
694                 t = game.kdist[j];
695                 game.kdist[j] = game.kdist[j+1];
696                 game.kdist[j+1] = t;
697                 t = game.kavgd[j];
698                 game.kavgd[j] = game.kavgd[j+1];
699                 game.kavgd[j+1] = t;
700                 k = game.kx[j];
701                 game.kx[j] = game.kx[j+1];
702                 game.kx[j+1] = k;
703                 k = game.ky[j];
704                 game.ky[j] = game.ky[j+1];
705                 game.ky[j+1] = k;
706                 t = game.kpower[j];
707                 game.kpower[j] = game.kpower[j+1];
708                 game.kpower[j+1] = t;
709             }
710     } while (sw);
711 }