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