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