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