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