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