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