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