Preparing for release.
[super-star-trek.git] / c-version / 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 /* issue a historically correct banner */
13 {
14     skip(2);
15     prout(_("-SUPER- STAR TREK"));
16     skip(1);
17 #ifdef __HISTORICAL__
18     prout(_("Latest update-21 Sept 78"));
19     skip(1);
20 #endif /* __HISTORICAL__ */
21 }
22
23 void freeze(bool boss)
24 /* save game */
25 {
26     FILE *fp;
27     int key;
28     if (boss) {
29         strcpy(citem, "emsave.trk");
30     }
31     else {
32         if ((key = scan()) == IHEOL) {
33             proutn(_("File name: "));
34             key = scan();
35         }
36         if (key != IHALPHA) {
37             huh();
38             return;
39         }
40         chew();
41         if (strchr(citem, '.') == NULL) {
42             strcat(citem, ".trk");
43         }
44     }
45     if ((fp = fopen(citem, "wb")) == NULL) {
46         proutn(_("Can't freeze game as file "));
47         proutn(citem);
48         skip(1);
49         return;
50     }
51     strcpy(game.magic, SSTMAGIC);
52     fwrite(&game, sizeof(game), 1, fp);
53
54     fclose(fp);
55 }
56
57
58 bool thaw(void) 
59 /* retrieve saved game */
60 {
61     FILE *fp;
62     int key;
63
64     game.passwd[0] = '\0';
65     if ((key = scan()) == IHEOL) {
66         proutn(_("File name: "));
67         key = scan();
68     }
69     if (key != IHALPHA) {
70         huh();
71         return true;
72     }
73     chew();
74     if (strchr(citem, '.') == NULL) {
75         strcat(citem, ".trk");
76     }
77     if ((fp = fopen(citem, "rb")) == NULL) {
78         proutn(_("Can't find game file "));
79         proutn(citem);
80         skip(1);
81         return 1;
82     }
83     fread(&game, sizeof(game), 1, fp);
84     if (feof(fp) || ftell(fp) != filelength(fileno(fp)) || strcmp(game.magic, SSTMAGIC)) {
85         proutn(_("Game file format is bad, should begin with "));
86         prout(SSTMAGIC);
87         skip(1);
88         fclose(fp);
89         return 1;
90     }
91
92     fclose(fp);
93
94     return false;
95 }
96
97 #define SYSTEM_NAMES \
98     { \
99         /* \
100          * I used <http://www.memory-alpha.org> to find planets \
101          * with references in ST:TOS.  Eath and the Alpha Centauri \
102          * Colony have been omitted. \
103          * \
104          * Some planets marked Class G and P here will be displayed as class M \
105          * because of the way planets are generated. This is a known bug. \
106          */ \
107         /* Federation Worlds */ \
108         _("Andoria (Fesoan)"),  /* several episodes */ \
109         _("Tellar Prime (Miracht)"),    /* TOS: "Journey to Babel" */ \
110         _("Vulcan (T'Khasi)"),  /* many episodes */ \
111         _("Medusa"),            /* TOS: "Is There in Truth No Beauty?" */ \
112         _("Argelius II (Nelphia)"),/* TOS: "Wolf in the Fold" ("IV" in BSD) */ \
113         _("Ardana"),            /* TOS: "The Cloud Minders" */ \
114         _("Catulla (Cendo-Prae)"),      /* TOS: "The Way to Eden" */ \
115         _("Gideon"),            /* TOS: "The Mark of Gideon" */ \
116         _("Aldebaran III"),     /* TOS: "The Deadly Years" */ \
117         _("Alpha Majoris I"),   /* TOS: "Wolf in the Fold" */ \
118         _("Altair IV"),         /* TOS: "Amok Time */ \
119         _("Ariannus"),          /* TOS: "Let That Be Your Last Battlefield" */ \
120         _("Benecia"),           /* TOS: "The Conscience of the King" */ \
121         _("Beta Niobe I (Sarpeidon)"),  /* TOS: "All Our Yesterdays" */ \
122         _("Alpha Carinae II"),  /* TOS: "The Ultimate Computer" */ \
123         _("Capella IV (Kohath)"),       /* TOS: "Friday's Child" (Class G) */ \
124         _("Daran V"),           /* TOS: "For the World is Hollow and I Have Touched the Sky" */ \
125         _("Deneb II"),          /* TOS: "Wolf in the Fold" ("IV" in BSD) */ \
126         _("Eminiar VII"),               /* TOS: "A Taste of Armageddon" */ \
127         _("Gamma Canaris IV"),  /* TOS: "Metamorphosis" */ \
128         _("Gamma Tranguli VI (Vaalel)"),        /* TOS: "The Apple" */ \
129         _("Ingraham B"),                /* TOS: "Operation: Annihilate" */ \
130         _("Janus IV"),          /* TOS: "The Devil in the Dark" */ \
131         _("Makus III"),         /* TOS: "The Galileo Seven" */ \
132         _("Marcos XII"),                /* TOS: "And the Children Shall Lead", */ \
133         _("Omega IV"),          /* TOS: "The Omega Glory" */ \
134         _("Regulus V"),         /* TOS: "Amok Time */ \
135         _("Deneva"),            /* TOS: "Operation -- Annihilate!" */ \
136         /* Worlds from BSD Trek */ \
137         _("Rigel II"),          /* TOS: "Shore Leave" ("III" in BSD) */ \
138         _("Beta III"),          /* TOS: "The Return of the Archons" */ \
139         _("Triacus"),           /* TOS: "And the Children Shall Lead", */ \
140         _("Exo III"),           /* TOS: "What Are Little Girls Made Of?" (Class P) */ \
141     }
142 #if 0   /* Others */
143         _("Hansen's Planet"),   /* TOS: "The Galileo Seven" */
144         _("Taurus IV"),         /* TOS: "The Galileo Seven" (class G) */
145         _("Antos IV (Doraphane)"),      /* TOS: "Whom Gods Destroy", "Who Mourns for Adonais?" */
146         _("Izar"),                      /* TOS: "Whom Gods Destroy" */
147         _("Tiburon"),           /* TOS: "The Way to Eden" */
148         _("Merak II"),          /* TOS: "The Cloud Minders" */
149         _("Coridan (Desotriana)"),      /* TOS: "Journey to Babel" */
150         _("Iotia"),             /* TOS: "A Piece of the Action" */
151 #endif
152
153 #define DEVICE_NAMES \
154     { \
155         _("S. R. Sensors"), \
156         _("L. R. Sensors"), \
157         _("Phasers"), \
158         _("Photon Tubes"), \
159         _("Life Support"), \
160         _("Warp Engines"), \
161         _("Impulse Engines"), \
162         _("Shields"), \
163         _("Subspace Radio"), \
164         _("Shuttle Craft"), \
165         _("Computer"), \
166         _("Navigation System"), \
167         _("Transporter"), \
168         _("Shield Control"), \
169         _("Death Ray"), \
170         _("D. S. Probe") \
171     }
172
173 static void setup_names(void)
174 /* Sets up some arrays with localized names.
175  * Must be done after iostart() for localization to work. */
176 {
177     char *tmp1[ARRAY_SIZE(systnames)] = SYSTEM_NAMES;
178     char *tmp2[ARRAY_SIZE(device)] = DEVICE_NAMES;
179
180     memcpy(systnames, tmp1, sizeof(systnames));
181     memcpy(device, tmp2, sizeof(device));
182 }
183         
184 void setup(bool needprompt) 
185 /* prepare to play, set up cosmos */
186 {
187     int i,j, krem, klumper;
188     coord w;
189
190     /* call the setup hooks here */
191     setup_names();
192
193     //  Decide how many of everything
194     if (choose(needprompt))
195         return; // frozen game
196     // Prepare the Enterprise
197     game.alldone = game.gamewon = false;
198     game.ship = IHE;
199     game.state.crew = FULLCREW;
200     game.energy = game.inenrg = 5000.0;
201     game.shield = game.inshld = 2500.0;
202     game.shldchg = false;
203     game.shldup = false;
204     game.inlsr = 4.0;
205     game.lsupres = 4.0;
206     game.quadrant = randplace(GALSIZE);
207     game.sector = randplace(QUADSIZE);
208     game.torps = game.intorps = 10;
209     game.nprobes = (int)(3.0*Rand() + 2.0);     /* Give them 2-4 of these wonders */
210     game.warpfac = 5.0;
211     game.wfacsq = game.warpfac * game.warpfac;
212     for (i=0; i < NDEVICES; i++) 
213         game.damage[i] = 0.0;
214     // Set up assorted game parameters
215     invalidate(game.battle);
216     game.state.date = game.indate = 100.0*(int)(31.0*Rand()+20.0);
217     game.nkinks = game.nhelp = game.casual = game.abandoned = 0;
218     game.iscate = game.resting = game.imine = game.icrystl = game.icraft = false;
219     game.isatb = game.state.nplankl = 0;
220     game.state.starkl = game.state.basekl = 0;
221     game.iscraft = onship;
222     game.landed = false;
223     game.alive = true;
224     game.docfac = 0.25;
225     for (i = 1; i <= GALSIZE; i++)
226         for (j = 1; j <= GALSIZE; j++) {
227             struct quadrant *quad = &game.state.galaxy[i][j];
228             quad->charted = 0;
229             quad->planet = NOPLANET;
230             quad->romulans = 0;
231             quad->klingons = 0;
232             quad->starbase = false;
233             quad->supernova = false;
234             quad->status = secure;
235         }
236     // Initialize times for extraneous events
237     schedule(FSNOVA, expran(0.5 * game.intime));
238     schedule(FTBEAM, expran(1.5 * (game.intime / game.state.remcom)));
239     schedule(FSNAP, 1.0 + Rand()); // Force an early snapshot
240     schedule(FBATTAK, expran(0.3*game.intime));
241     unschedule(FCDBAS);
242     if (game.state.nscrem)
243         schedule(FSCMOVE, 0.2777);
244     else
245         unschedule(FSCMOVE);
246     unschedule(FSCDBAS);
247     unschedule(FDSPROB);
248     if ((game.options & OPTION_WORLDS) && game.skill >= SKILL_GOOD)
249         schedule(FDISTR, expran(1.0 + game.intime));
250     else
251         unschedule(FDISTR);
252     unschedule(FENSLV);
253     unschedule(FREPRO);
254     // Starchart is functional but we've never seen it
255     game.lastchart = FOREVER;
256     // Put stars in the galaxy
257     game.instar = 0;
258     for (i = 1; i <= GALSIZE; i++)
259         for (j = 1; j <= GALSIZE; j++) {
260             int k = Rand()*9.0 + 1.0;
261             game.instar += k;
262             game.state.galaxy[i][j].stars = k;
263         }
264     // Locate star bases in galaxy
265     for (i = 1; i <= game.inbase; i++) {
266         bool contflag;
267         do {
268             do w = randplace(GALSIZE);
269             while (game.state.galaxy[w.x][w.y].starbase);
270             contflag = false;
271             for (j = i-1; j > 0; j--) {
272                 /* Improved placement algorithm to spread out bases */
273                 double distq = square(w.x-game.state.baseq[j].x) + square(w.y-game.state.baseq[j].y);
274                 if (distq < 6.0*(BASEMAX+1-game.inbase) && Rand() < 0.75) {
275                     contflag = true;
276                     if (idebug)
277                         prout("=== Abandoning base #%d at %d-%d", i, w.x, w.y);
278                     break;
279                 }
280                 else if (distq < 6.0 * (BASEMAX+1-game.inbase)) {
281                     if (idebug)
282                         prout("=== Saving base #%d, close to #%d", i, j);
283                 }
284             }
285         } while (contflag);
286                         
287         game.state.baseq[i] = w;
288         game.state.galaxy[w.x][w.y].starbase = true;
289         game.state.chart[w.x][w.y].starbase = true;
290     }
291     // Position ordinary Klingon Battle Cruisers
292     krem = game.inkling;
293     klumper = 0.25*game.skill*(9.0-game.length)+1.0;
294     if (klumper > MAXKLQUAD) 
295         klumper = MAXKLQUAD;
296     do {
297         double r = Rand();
298         int klump = (1.0 - r*r)*klumper;
299         if (klump > krem)
300             klump = krem;
301         krem -= klump;
302         do w = randplace(GALSIZE);
303         while (game.state.galaxy[w.x][w.y].supernova ||
304                 game.state.galaxy[w.x][w.y].klingons + klump > 9);
305         game.state.galaxy[w.x][w.y].klingons += klump;
306     } while (krem > 0);
307     // Position Klingon Commander Ships
308 #ifdef ODEBUG
309     klumper = 1;
310 #endif /* ODEBUG */
311     for (i = 1; i <= game.incom; i++) {
312         do {
313             do { /* IF debugging, put commanders by bases, always! */
314 #ifdef ODEBUG
315                 if (game.idebug && klumper <= game.inbase) {
316                     w = game.state.baseq[klumper];
317                     klumper++;
318                 }
319                 else
320 #endif /* ODEBUG */
321                     w = randplace(GALSIZE);
322             }
323             while ((!game.state.galaxy[w.x][w.y].klingons && Rand() < 0.75)||
324                    game.state.galaxy[w.x][w.y].supernova||
325                    game.state.galaxy[w.x][w.y].klingons > 8);
326             // check for duplicate
327             for (j = 1; j < i; j++)
328                 if (same(game.state.kcmdr[j], w))
329                     break;
330         } while (j < i);
331         game.state.galaxy[w.x][w.y].klingons++;
332         game.state.kcmdr[i] = w;
333     }
334     // Locate planets in galaxy
335     for (i = 0; i < game.inplan; i++) {
336         do w = randplace(GALSIZE); 
337         while (game.state.galaxy[w.x][w.y].planet != NOPLANET);
338         game.state.planets[i].w = w;
339         if ((game.options & OPTION_WORLDS) && i < NINHAB) {
340             game.state.planets[i].pclass = M;   // All inhabited planets are class M
341             game.state.planets[i].crystals = absent;
342             game.state.planets[i].known = known;
343             game.state.planets[i].inhabited = i;
344         } else {
345             game.state.planets[i].pclass = Rand()*3.0; // Planet class M N or O
346             game.state.planets[i].crystals = Rand()*1.5;                // 1 in 3 chance of crystals
347             game.state.planets[i].known = unknown;
348             game.state.planets[i].inhabited = UNINHABITED;
349         }
350         game.state.galaxy[w.x][w.y].planet = i;
351     }
352     // Locate Romulans
353     for (i = 1; i <= game.state.nromrem; i++) {
354         w = randplace(GALSIZE);
355         game.state.galaxy[w.x][w.y].romulans = 1;
356     }
357     // Locate the Super Commander
358     if (game.state.nscrem > 0) {
359         do w = randplace(GALSIZE);
360         while (game.state.galaxy[w.x][w.y].supernova || game.state.galaxy[w.x][w.y].klingons > 8);
361         game.state.kscmdr = w;
362         game.state.galaxy[w.x][w.y].klingons++;
363     }
364     // Place thing (in tournament game, thingx == -1, don't want one!)
365     if (thing.x != -1) {
366         thing = randplace(GALSIZE);
367     }
368     else
369         invalidate(thing);
370
371     skip(2);
372     game.state.snap = false;
373                 
374     if (game.skill == SKILL_NOVICE) {
375         prout(_("It is stardate %d. The Federation is being attacked by"),
376               (int)game.state.date);
377         prout(_("a deadly Klingon invasion force. As captain of the United"));
378         prout(_("Starship U.S.S. Enterprise, it is your mission to seek out"));
379         prout(_("and destroy this invasion force of %d battle cruisers."),
380               (game.inkling + game.incom + game.inscom));
381         prout(_("You have an initial allotment of %d stardates to complete"), (int)game.intime);
382         prout(_("your mission.  As you proceed you may be given more time."));
383         skip(1);
384         prout(_("You will have %d supporting starbases."), game.inbase);
385         proutn(_("Starbase locations-  "));
386     }
387     else {
388         prout(_("Stardate %d."), (int)game.state.date);
389         skip(1);
390         prout(_("%d Klingons."), game.inkling + game.incom + game.inscom);
391         prout(_("An unknown number of Romulans."));
392         if (game.state.nscrem)
393             prout(_("And one (GULP) Super-Commander."));
394         prout(_("%d stardates."),(int)game.intime);
395         proutn(_("%d starbases in "), game.inbase);
396     }
397     for (i = 1; i <= game.inbase; i++) {
398         proutn(cramlc(0, game.state.baseq[i]));
399         proutn("  ");
400     }
401     skip(2);
402     proutn(_("The Enterprise is currently in "));
403     proutn(cramlc(quadrant, game.quadrant));
404     proutn(" ");
405     proutn(cramlc(sector, game.sector));
406     skip(2);
407     prout(_("Good Luck!"));
408     if (game.state.nscrem)
409         prout(_("  YOU'LL NEED IT."));
410     waitfor();
411     newqad(false);
412     if (game.nenhere-iqhere-game.ithere)
413         game.shldup = true;
414     if (game.neutz)     // bad luck to start in a Romulan Neutral Zone
415         attack(false);
416 }
417
418 bool choose(bool needprompt) 
419 /* choose your game type */
420 {
421     for(;;) {
422         game.tourn = 0;
423         game.thawed = false;
424         game.skill = SKILL_NONE;
425         game.length = 0;
426         if (needprompt) /* Can start with command line options */
427             proutn(_("Would you like a regular, tournament, or saved game? "));
428         scan();
429         if (strlen(citem)==0) // Try again
430             continue;
431         if (isit("tournament")) {
432             while (scan() == IHEOL) {
433                 proutn(_("Type in tournament number-"));
434             }
435             if (aaitem == 0) {
436                 chew();
437                 continue; // We don't want a blank entry
438             }
439             game.tourn = (int)aaitem;
440             thing.x = -1;
441             srand((unsigned int)(int)aaitem);
442             break;
443         }
444         if (isit("saved") || isit("frozen")) {
445             if (thaw())
446                 continue;
447             chew();
448             if (*game.passwd==0)
449                 continue;
450             if (!game.alldone)
451                 game.thawed = true; // No plaque if not finished
452             report();
453             waitfor();
454             return true;
455         }
456         if (isit("regular"))
457             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"))
466                 game.length = 1;
467             else if (isit("medium"))
468                 game.length = 2;
469             else if (isit("long"))
470                 game.length = 4;
471             else if (isit("novice"))
472                 game.skill = SKILL_NOVICE;
473             else if (isit("fair"))
474                 game.skill = SKILL_FAIR;
475             else if (isit("good"))
476                 game.skill = SKILL_GOOD;
477             else if (isit("expert"))
478                 game.skill = SKILL_EXPERT;
479             else if (isit("emeritus"))
480                 game.skill = SKILL_EMERITUS;
481             else {
482                 proutn(_("What is \""));
483                 proutn(citem);
484                 prout("\"?");
485             }
486         }
487         else {
488             chew();
489             if (game.length==0)
490                 proutn(_("Would you like a Short, Medium, or Long game? "));
491             else if (game.skill == SKILL_NONE)
492                 proutn(_("Are you a Novice, Fair, Good, Expert, or Emeritus player? "));
493         }
494     }
495     // Choose game options -- added by ESR for SST2K
496     if (scan() != IHALPHA) {
497         chew();
498         proutn(_("Choose your game style (plain, almy, fancy or just press enter): "));
499         scan();
500     }
501     if (isit("plain")) {
502         // Approximates the UT FORTRAN version.
503         game.options &=~ (OPTION_THOLIAN | OPTION_PLANETS | OPTION_THINGY | OPTION_PROBE | OPTION_RAMMING | OPTION_MVBADDY | OPTION_BLKHOLE | OPTION_BASE | OPTION_WORLDS);
504         game.options |= OPTION_PLAIN;
505     } 
506     else if (isit("almy")) {
507         // Approximates Tom Almy's version.
508         game.options &=~ (OPTION_THINGY | OPTION_BLKHOLE | OPTION_BASE | OPTION_WORLDS);
509         game.options |= OPTION_ALMY;
510     }
511     else if (isit("fancy"))
512         /* do nothing */;
513     else if (strlen(citem)) {
514             proutn(_("What is \""));
515             proutn(citem);
516             prout("\"?");
517     }
518     setpassword();
519     if (strcmp(game.passwd, "debug")==0) {
520         idebug = true;
521         fputs("=== Debug mode enabled\n", stdout);
522     }
523
524     // Use parameters to generate initial values of things
525     game.damfac = 0.5 * game.skill;
526     game.state.rembase = 2.0 + Rand()*(BASEMAX-2.0);
527     game.inbase = game.state.rembase;
528     game.inplan = 0;
529     if (game.options & OPTION_PLANETS)
530         game.inplan += (MAXUNINHAB/2) + (MAXUNINHAB/2+1)*Rand();
531     if (game.options & OPTION_WORLDS)
532         game.inplan += NINHAB;
533     game.state.nromrem = game.inrom = (2.0+Rand())*game.skill;
534     game.state.nscrem = game.inscom = (game.skill > SKILL_FAIR ? 1 : 0);
535     game.state.remtime = 7.0 * game.length;
536     game.intime = game.state.remtime;
537     game.state.remkl = game.inkling = 2.0*game.intime*((game.skill+1 - 2*Rand())*game.skill*0.1+.15);
538     game.incom = game.skill + 0.0625*game.inkling*Rand();
539     game.state.remcom = min(10, game.incom);
540     game.incom = game.state.remcom;
541     game.state.remres = (game.inkling+4*game.incom)*game.intime;
542     game.inresor = game.state.remres;
543     if (game.inkling > 50) {
544         game.inbase = (game.state.rembase += 1);
545     }
546     return false;
547 }
548
549 coord dropin(feature iquad)
550 /* drop a feature on a random dot in the current quadrant */
551 {
552     coord w;
553     do w = randplace(QUADSIZE);
554     while (game.quad[w.x][w.y] != IHDOT);
555     game.quad[w.x][w.y] = iquad;
556     return w;
557 }
558
559 void newcnd(void)
560 /* update our alert status */
561 {
562     game.condition = green;
563     if (game.energy < 1000.0)
564         game.condition = yellow;
565     if (game.state.galaxy[game.quadrant.x][game.quadrant.y].klingons || game.state.galaxy[game.quadrant.x][game.quadrant.y].romulans)
566         game.condition = red;
567     if (!game.alive)
568         game.condition=dead;
569 }
570
571 coord newkling(int i)
572 /* drop new Klingon into current quadrant */
573 {
574     coord pi = dropin(IHK);
575     game.ks[i] = pi;
576     game.kdist[i] = game.kavgd[i] = distance(game.sector, pi);
577     game.kpower[i] = Rand()*150.0 +300.0 +25.0*game.skill;
578     return pi;
579 }
580
581 void newqad(bool shutup)
582 /* set up a new state of quadrant, for when we enter or re-enter it */
583 {
584     int i, j;
585     coord w;
586     struct quadrant *q;
587
588     game.justin = true;
589     invalidate(game.base);
590     game.klhere = 0;
591     game.comhere = false;
592     invalidate(game.plnet);
593     game.ishere = false;
594     game.irhere = 0;
595     game.iplnet = 0;
596     game.nenhere = 0;
597     game.neutz = false;
598     game.inorbit = false;
599     game.landed = false;
600     game.ientesc = false;
601     game.ithere = false;
602     iqhere = false;
603     iqengry = false;
604     game.iseenit = false;
605     if (game.iscate) {
606         // Attempt to escape Super-commander, so tbeam back!
607         game.iscate = false;
608         game.ientesc = true;
609     }
610     // Clear quadrant
611     for (i = 1; i <= QUADSIZE; i++)
612         for (j = 1; j <= QUADSIZE; j++)
613             game.quad[i][j] = IHDOT;
614     q = &game.state.galaxy[game.quadrant.x][game.quadrant.y];
615     // cope with supernova
616     if (q->supernova)
617         return;
618     game.klhere = q->klingons;
619     game.irhere = q->romulans;
620     game.nenhere = game.klhere + game.irhere;
621
622     // Position Starship
623     game.quad[game.sector.x][game.sector.y] = game.ship;
624
625     if (q->klingons) {
626         w.x = w.y = 0;  /* quiet a gcc warning */
627         // Position ordinary Klingons
628         for (i = 1; i <= game.klhere; i++)
629             w = newkling(i);
630         // If we need a commander, promote a Klingon
631         for (i = 1; i <= game.state.remcom; i++)
632             if (same(game.state.kcmdr[i], game.quadrant))
633                 break;
634                         
635         if (i <= game.state.remcom) {
636             game.quad[w.x][w.y] = IHC;
637             game.kpower[game.klhere] = 950.0+400.0*Rand()+50.0*game.skill;
638             game.comhere = true;
639         }
640
641         // If we need a super-commander, promote a Klingon
642         if (same(game.quadrant, game.state.kscmdr)) {
643             game.quad[game.ks[1].x][game.ks[1].y] = IHS;
644             game.kpower[1] = 1175.0 + 400.0*Rand() + 125.0*game.skill;
645             game.iscate = (game.state.remkl > 1);
646             game.ishere = true;
647         }
648     }
649     // Put in Romulans if needed
650     for (i = game.klhere+1; i <= game.nenhere; i++) {
651         w = dropin(IHR);
652         game.ks[i] = w;
653         game.kdist[i] = game.kavgd[i] = distance(game.sector, w);
654         game.kpower[i] = Rand()*400.0 + 450.0 + 50.0*game.skill;
655     }
656     // If quadrant needs a starbase, put it in
657     if (q->starbase)
658         game.base = dropin(IHB);
659         
660     // If quadrant needs a planet, put it in
661     if (q->planet != NOPLANET) {
662         game.iplnet = q->planet;
663         if (game.state.planets[q->planet].inhabited == UNINHABITED)
664             game.plnet = dropin(IHP);
665         else
666             game.plnet = dropin(IHW);
667     }
668     // Check for condition
669     newcnd();
670     // And finally the stars
671     for (i = 1; i <= q->stars; i++) 
672         dropin(IHSTAR);
673
674     // Check for RNZ
675     if (game.irhere > 0 && game.klhere == 0) {
676         game.neutz = true;
677         if (!damaged(DRADIO)) {
678             skip(1);
679             prout(_("LT. Uhura- \"Captain, an urgent message."));
680             prout(_("  I'll put it on audio.\"  CLICK"));
681             skip(1);
682             prout(_("INTRUDER! YOU HAVE VIOLATED THE ROMULAN NEUTRAL ZONE."));
683             prout(_("LEAVE AT ONCE, OR YOU WILL BE DESTROYED!"));
684         }
685     }
686
687     if (shutup==0) {
688         // Put in THING if needed
689         if (same(thing, game.quadrant)) {
690             w = dropin(IHQUEST);
691             thing = randplace(GALSIZE);
692             game.nenhere++;
693             iqhere = true;
694             game.ks[game.nenhere] = w;
695             game.kdist[game.nenhere] = game.kavgd[game.nenhere] =
696                 distance(game.sector, w);
697             game.kpower[game.nenhere] = Rand()*6000.0 +500.0 +250.0*game.skill;
698             if (!damaged(DSRSENS)) {
699                 skip(1);
700                 prout(_("Mr. Spock- \"Captain, this is most unusual."));
701                 prout(_("    Please examine your short-range scan.\""));
702             }
703         }
704     }
705
706     // Decide if quadrant needs a Tholian
707     if (game.options & OPTION_THOLIAN) {
708         if ((game.skill < SKILL_GOOD && Rand() <= 0.02) ||   /* Lighten up if skill is low */
709             (game.skill == SKILL_GOOD && Rand() <= 0.05) ||
710             (game.skill > SKILL_GOOD && Rand() <= 0.08)
711             ) {
712             do {
713                 game.tholian.x = Rand() > 0.5 ? QUADSIZE : 1;
714                 game.tholian.y = Rand() > 0.5 ? QUADSIZE : 1;
715             } while (game.quad[game.tholian.x][game.tholian.y] != IHDOT);
716             game.quad[game.tholian.x][game.tholian.y] = IHT;
717             game.ithere = true;
718             game.nenhere++;
719             game.ks[game.nenhere] = game.tholian;
720             game.kdist[game.nenhere] = game.kavgd[game.nenhere] =
721                 distance(game.sector, game.tholian);
722             game.kpower[game.nenhere] = Rand()*400.0 +100.0 +25.0*game.skill;
723             /* Reserve unocupied corners */
724             if (game.quad[1][1]==IHDOT)
725                 game.quad[1][1] = 'X';
726             if (game.quad[1][QUADSIZE]==IHDOT)
727                 game.quad[1][QUADSIZE] = 'X';
728             if (game.quad[QUADSIZE][1]==IHDOT)
729                 game.quad[QUADSIZE][1] = 'X';
730             if (game.quad[QUADSIZE][QUADSIZE]==IHDOT)
731                 game.quad[QUADSIZE][QUADSIZE] = 'X';
732         }
733     }
734
735     sortklings();
736
737     // Put in a few black holes
738     for (i = 1; i <= 3; i++)
739         if (Rand() > 0.5) 
740             dropin(IHBLANK);
741
742     // Take out X's in corners if Tholian present
743     if (game.ithere) {
744         if (game.quad[1][1]=='X')
745             game.quad[1][1] = IHDOT;
746         if (game.quad[1][QUADSIZE]=='X')
747             game.quad[1][QUADSIZE] = IHDOT;
748         if (game.quad[QUADSIZE][1]=='X')
749             game.quad[QUADSIZE][1] = IHDOT;
750         if (game.quad[QUADSIZE][QUADSIZE]=='X')
751             game.quad[QUADSIZE][QUADSIZE] = IHDOT;
752     }           
753 }
754
755 void sortklings(void) 
756 /* sort Klingons by distance from us */
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)
765         return;
766
767     do {
768         sw = false;
769         for (j = 1; j < game.nenhere; j++)
770             if (game.kdist[j] > game.kdist[j+1]) {
771                 sw = true;
772                 t = game.kdist[j];
773                 game.kdist[j] = game.kdist[j+1];
774                 game.kdist[j+1] = t;
775                 t = game.kavgd[j];
776                 game.kavgd[j] = game.kavgd[j+1];
777                 game.kavgd[j+1] = t;
778                 k = game.ks[j].x;
779                 game.ks[j].x = game.ks[j+1].x;
780                 game.ks[j+1].x = k;
781                 k = game.ks[j].y;
782                 game.ks[j].y = game.ks[j+1].y;
783                 game.ks[j+1].y = k;
784                 t = game.kpower[j];
785                 game.kpower[j] = game.kpower[j+1];
786                 game.kpower[j+1] = t;
787             }
788     } while (sw);
789 }
790
791 void setpassword(void)
792 /* set the self-destruct password */
793 {
794     if (game.options & OPTION_PLAIN) {
795         while (TRUE) {
796             chew();
797             proutn(_("Please type in a secret password- "));
798             scan();
799             strcpy(game.passwd, citem);
800             if (*game.passwd != 0)
801                 break;
802         }
803     } else {
804         int i;
805         for(i=0;i<3;i++)
806             game.passwd[i]=(char)(97+(int)(Rand()*25));
807         game.passwd[3]=0;
808     }
809 }