Parallel cleanups in the C and scratch Python code.
[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 /* 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         "ERROR", \
108         /* Federation Worlds */ \
109         _("Andoria (Fesoan)"),  /* several episodes */ \
110         _("Tellar Prime (Miracht)"),    /* TOS: "Journey to Babel" */ \
111         _("Vulcan (T'Khasi)"),  /* many episodes */ \
112         _("Medusa"),            /* TOS: "Is There in Truth No Beauty?" */ \
113         _("Argelius II (Nelphia)"),/* TOS: "Wolf in the Fold" ("IV" in BSD) */ \
114         _("Ardana"),            /* TOS: "The Cloud Minders" */ \
115         _("Catulla (Cendo-Prae)"),      /* TOS: "The Way to Eden" */ \
116         _("Gideon"),            /* TOS: "The Mark of Gideon" */ \
117         _("Aldebaran III"),     /* TOS: "The Deadly Years" */ \
118         _("Alpha Majoris I"),   /* TOS: "Wolf in the Fold" */ \
119         _("Altair IV"),         /* TOS: "Amok Time */ \
120         _("Ariannus"),          /* TOS: "Let That Be Your Last Battlefield" */ \
121         _("Benecia"),           /* TOS: "The Conscience of the King" */ \
122         _("Beta Niobe I (Sarpeidon)"),  /* TOS: "All Our Yesterdays" */ \
123         _("Alpha Carinae II"),  /* TOS: "The Ultimate Computer" */ \
124         _("Capella IV (Kohath)"),       /* TOS: "Friday's Child" (Class G) */ \
125         _("Daran V"),           /* TOS: "For the World is Hollow and I Have Touched the Sky" */ \
126         _("Deneb II"),          /* TOS: "Wolf in the Fold" ("IV" in BSD) */ \
127         _("Eminiar VII"),               /* TOS: "A Taste of Armageddon" */ \
128         _("Gamma Canaris IV"),  /* TOS: "Metamorphosis" */ \
129         _("Gamma Tranguli VI (Vaalel)"),        /* TOS: "The Apple" */ \
130         _("Ingraham B"),                /* TOS: "Operation: Annihilate" */ \
131         _("Janus IV"),          /* TOS: "The Devil in the Dark" */ \
132         _("Makus III"),         /* TOS: "The Galileo Seven" */ \
133         _("Marcos XII"),                /* TOS: "And the Children Shall Lead", */ \
134         _("Omega IV"),          /* TOS: "The Omega Glory" */ \
135         _("Regulus V"),         /* TOS: "Amok Time */ \
136         _("Deneva"),            /* TOS: "Operation -- Annihilate!" */ \
137         /* Worlds from BSD Trek */ \
138         _("Rigel II"),          /* TOS: "Shore Leave" ("III" in BSD) */ \
139         _("Beta III"),          /* TOS: "The Return of the Archons" */ \
140         _("Triacus"),           /* TOS: "And the Children Shall Lead", */ \
141         _("Exo III"),           /* TOS: "What Are Little Girls Made Of?" (Class P) */ \
142     }
143 #if 0   /* Others */
144         _("Hansen's Planet"),   /* TOS: "The Galileo Seven" */
145         _("Taurus IV"),         /* TOS: "The Galileo Seven" (class G) */
146         _("Antos IV (Doraphane)"),      /* TOS: "Whom Gods Destroy", "Who Mourns for Adonais?" */
147         _("Izar"),                      /* TOS: "Whom Gods Destroy" */
148         _("Tiburon"),           /* TOS: "The Way to Eden" */
149         _("Merak II"),          /* TOS: "The Cloud Minders" */
150         _("Coridan (Desotriana)"),      /* TOS: "Journey to Babel" */
151         _("Iotia"),             /* TOS: "A Piece of the Action" */
152 #endif
153
154 #define DEVICE_NAMES \
155     { \
156         _("S. R. Sensors"), \
157         _("L. R. Sensors"), \
158         _("Phasers"), \
159         _("Photon Tubes"), \
160         _("Life Support"), \
161         _("Warp Engines"), \
162         _("Impulse Engines"), \
163         _("Shields"), \
164         _("Subspace Radio"), \
165         _("Shuttle Craft"), \
166         _("Computer"), \
167         _("Navigation System"), \
168         _("Transporter"), \
169         _("Shield Control"), \
170         _("Death Ray"), \
171         _("D. S. Probe") \
172     }
173
174 static void setup_names(void)
175 /* Sets up some arrays with localized names.
176  * Must be done after iostart() for localization to work. */
177 {
178     char *tmp1[ARRAY_SIZE(systnames)] = SYSTEM_NAMES;
179     char *tmp2[ARRAY_SIZE(device)] = DEVICE_NAMES;
180
181     memcpy(systnames, tmp1, sizeof(systnames));
182     memcpy(device, tmp2, sizeof(device));
183 }
184         
185 void setup(bool needprompt) 
186 /* prepare to play, set up cosmos */
187 {
188     int i,j, krem, klumper;
189     coord w;
190
191     /* call the setup hooks here */
192     setup_names();
193
194     //  Decide how many of everything
195     if (choose(needprompt)) 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_quadrants(i)
226         for_quadrants(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_quadrants(i)
259         for_quadrants(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) klump = krem;
300         krem -= klump;
301         do w = randplace(GALSIZE);
302         while (game.state.galaxy[w.x][w.y].supernova ||
303                 game.state.galaxy[w.x][w.y].klingons + klump > 9);
304         game.state.galaxy[w.x][w.y].klingons += klump;
305     } while (krem > 0);
306     // Position Klingon Commander Ships
307 #ifdef ODEBUG
308     klumper = 1;
309 #endif /* ODEBUG */
310     for (i = 1; i <= game.incom; i++) {
311         do {
312             do { /* IF debugging, put commanders by bases, always! */
313 #ifdef ODEBUG
314                 if (game.idebug && klumper <= game.inbase) {
315                     w = game.state.baseq[klumper];
316                     klumper++;
317                 }
318                 else
319 #endif /* ODEBUG */
320                     w = randplace(GALSIZE);
321             }
322             while ((!game.state.galaxy[w.x][w.y].klingons && Rand() < 0.75)||
323                    game.state.galaxy[w.x][w.y].supernova||
324                    game.state.galaxy[w.x][w.y].klingons > 8);
325             // check for duplicate
326             for (j = 1; j < i; j++)
327                 if (same(game.state.kcmdr[j], w))
328                     break;
329         } while (j < i);
330         game.state.galaxy[w.x][w.y].klingons++;
331         game.state.kcmdr[i] = w;
332     }
333     // Locate planets in galaxy
334     for (i = 0; i < game.inplan; i++) {
335         do w = randplace(GALSIZE); 
336         while (game.state.galaxy[w.x][w.y].planet != NOPLANET);
337         game.state.plnets[i].w = w;
338         if (i < NINHAB) {
339             game.state.plnets[i].pclass = M;    // All inhabited planets are class M
340             game.state.plnets[i].crystals = absent;
341             game.state.plnets[i].known = known;
342             game.state.plnets[i].inhabited = i;
343         } else {
344             game.state.plnets[i].pclass = Rand()*3.0; // Planet class M N or O
345             game.state.plnets[i].crystals = Rand()*1.5;         // 1 in 3 chance of crystals
346             game.state.plnets[i].known = unknown;
347             game.state.plnets[i].inhabited = UNINHABITED;
348         }
349         if ((game.options & OPTION_WORLDS) || i >= NINHAB)
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               INKLINGTOT);
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."), INKLINGTOT);
391         prout(_("An unknown number of Romulans."));
392         if (game.state.nscrem) prout(_("And one (GULP) Super-Commander."));
393         prout(_("%d stardates."),(int)game.intime);
394         proutn(_("%d starbases in "), game.inbase);
395     }
396     for (i = 1; i <= game.inbase; i++) {
397         proutn(cramlc(0, game.state.baseq[i]));
398         proutn("  ");
399     }
400     skip(2);
401     proutn(_("The Enterprise is currently in "));
402     proutn(cramlc(quadrant, game.quadrant));
403     proutn(" ");
404     proutn(cramlc(sector, game.sector));
405     skip(2);
406     prout(_("Good Luck!"));
407     if (game.state.nscrem) prout(_("  YOU'LL NEED IT."));
408     waitfor();
409     newqad(false);
410     if (game.nenhere-iqhere-game.ithere) game.shldup = true;
411     if (game.neutz) attack(false);      // bad luck to start in a Romulan Neutral Zone
412 }
413
414 bool choose(bool needprompt) 
415 /* choose your game type */
416 {
417     for(;;) {
418         game.tourn = 0;
419         game.thawed = false;
420         game.skill = SKILL_NONE;
421         game.length = 0;
422         if (needprompt) /* Can start with command line options */
423             proutn(_("Would you like a regular, tournament, or saved game? "));
424         scan();
425         if (strlen(citem)==0) continue; // Try again
426         if (isit("tournament")) {
427             while (scan() == IHEOL) {
428                 proutn(_("Type in tournament number-"));
429             }
430             if (aaitem == 0) {
431                 chew();
432                 continue; // We don't want a blank entry
433             }
434             game.tourn = (int)aaitem;
435             thing.x = -1;
436             srand((unsigned int)(int)aaitem);
437             break;
438         }
439         if (isit("saved") || isit("frozen")) {
440             if (thaw()) continue;
441             chew();
442             if (*game.passwd==0) continue;
443             if (!game.alldone) game.thawed = 1; // No plaque if not finished
444             report();
445             waitfor();
446             return true;
447         }
448         if (isit("regular")) break;
449         proutn(_("What is \""));
450         proutn(citem);
451         prout("\"?");
452         chew();
453     }
454     while (game.length==0 || game.skill==SKILL_NONE) {
455         if (scan() == IHALPHA) {
456             if (isit("short")) game.length = 1;
457             else if (isit("medium")) game.length = 2;
458             else if (isit("long")) game.length = 4;
459             else if (isit("novice")) game.skill = SKILL_NOVICE;
460             else if (isit("fair")) game.skill = SKILL_FAIR;
461             else if (isit("good")) game.skill = SKILL_GOOD;
462             else if (isit("expert")) game.skill = SKILL_EXPERT;
463             else if (isit("emeritus")) game.skill = SKILL_EMERITUS;
464             else {
465                 proutn(_("What is \""));
466                 proutn(citem);
467                 prout("\"?");
468             }
469         }
470         else {
471             chew();
472             if (game.length==0) proutn(_("Would you like a Short, Medium, or Long game? "));
473             else if (game.skill == SKILL_NONE) proutn(_("Are you a Novice, Fair, Good, Expert, or Emeritus player? "));
474         }
475     }
476     // Choose game options -- added by ESR for SST2K
477     if (scan() != IHALPHA) {
478         chew();
479         proutn(_("Choose your game style (or just press enter): "));
480         scan();
481     }
482     if (isit("plain")) {
483         // Approximates the UT FORTRAN version.
484         game.options &=~ (OPTION_THOLIAN | OPTION_PLANETS | OPTION_THINGY | OPTION_PROBE | OPTION_RAMMING | OPTION_MVBADDY | OPTION_BLKHOLE | OPTION_BASE | OPTION_WORLDS);
485         game.options |= OPTION_PLAIN;
486     } 
487     else if (isit("almy")) {
488         // Approximates Tom Almy's version.
489         game.options &=~ (OPTION_THINGY | OPTION_BLKHOLE | OPTION_BASE | OPTION_WORLDS);
490         game.options |= OPTION_ALMY;
491     }
492     else if (isit("fancy"))
493         /* do nothing */;
494     else if (strlen(citem)) {
495             proutn(_("What is \""));
496             proutn(citem);
497             prout("\"?");
498     }
499     setpassword();
500     if (strcmp(game.passwd, "debug")==0) {
501         idebug = true;
502         fputs("=== Debug mode enabled\n", stdout);
503     }
504
505     // Use parameters to generate initial values of things
506     game.damfac = 0.5 * game.skill;
507     game.state.rembase = 2.0 + Rand()*(BASEMAX-2.0);
508     game.inbase = game.state.rembase;
509     if (game.options & OPTION_PLANETS)
510         game.inplan = NINHAB + (MAXUNINHAB/2) + (MAXUNINHAB/2+1)*Rand();
511     game.state.nromrem = game.inrom = (2.0+Rand())*game.skill;
512     game.state.nscrem = game.inscom = (game.skill > SKILL_FAIR ? 1 : 0);
513     game.state.remtime = 7.0 * game.length;
514     game.intime = game.state.remtime;
515     game.state.remkl = game.inkling = 2.0*game.intime*((game.skill+1 - 2*Rand())*game.skill*0.1+.15);
516     game.incom = game.skill + 0.0625*game.inkling*Rand();
517     game.state.remcom = min(10, game.incom);
518     game.incom = game.state.remcom;
519     game.state.remres = (game.inkling+4*game.incom)*game.intime;
520     game.inresor = game.state.remres;
521     if (game.inkling > 50) {
522         game.inbase = (game.state.rembase += 1);
523     }
524     return false;
525 }
526
527 coord dropin(feature iquad)
528 /* drop a feature on a random dot in the current quadrant */
529 {
530     coord w;
531     do w = randplace(QUADSIZE);
532     while (game.quad[w.x][w.y] != IHDOT);
533     game.quad[w.x][w.y] = iquad;
534     return w;
535 }
536
537 void newcnd(void)
538 /* update our alert status */
539 {
540     game.condition = green;
541     if (game.energy < 1000.0) game.condition = yellow;
542     if (game.state.galaxy[game.quadrant.x][game.quadrant.y].klingons || game.state.galaxy[game.quadrant.x][game.quadrant.y].romulans)
543         game.condition = red;
544     if (!game.alive) game.condition=dead;
545 }
546
547 coord newkling(int i)
548 /* drop new Klingon into current quadrant */
549 {
550     coord pi = dropin(IHK);
551     game.ks[i] = pi;
552     game.kdist[i] = game.kavgd[i] = distance(game.sector, pi);
553     game.kpower[i] = Rand()*150.0 +300.0 +25.0*game.skill;
554     return pi;
555 }
556
557 void newqad(bool shutup)
558 /* set up a new state of quadrant, for when we enter or re-enter it */
559 {
560     int i, j;
561     coord w;
562     struct quadrant *q;
563
564     game.justin = true;
565     invalidate(game.base);
566     game.klhere = 0;
567     game.comhere = false;
568     invalidate(game.plnet);
569     game.ishere = false;
570     game.irhere = 0;
571     game.iplnet = 0;
572     game.nenhere = 0;
573     game.neutz = false;
574     game.inorbit = false;
575     game.landed = false;
576     game.ientesc = false;
577     game.ithere = false;
578     iqhere = false;
579     iqengry = false;
580     game.iseenit = false;
581     if (game.iscate) {
582         // Attempt to escape Super-commander, so tbeam back!
583         game.iscate = false;
584         game.ientesc = true;
585     }
586     // Clear quadrant
587     for_sectors(i)
588         for_sectors(j) 
589             game.quad[i][j] = IHDOT;
590     q = &game.state.galaxy[game.quadrant.x][game.quadrant.y];
591     // cope with supernova
592     if (q->supernova)
593         return;
594     game.klhere = q->klingons;
595     game.irhere = q->romulans;
596     game.nenhere = game.klhere + game.irhere;
597
598     // Position Starship
599     game.quad[game.sector.x][game.sector.y] = game.ship;
600
601     if (q->klingons) {
602         w.x = w.y = 0;  /* quiet a gcc warning */
603         // Position ordinary Klingons
604         for (i = 1; i <= game.klhere; i++)
605             w = newkling(i);
606         // If we need a commander, promote a Klingon
607         for_commanders(i)
608             if (same(game.state.kcmdr[i], game.quadrant))
609                 break;
610                         
611         if (i <= game.state.remcom) {
612             game.quad[w.x][w.y] = IHC;
613             game.kpower[game.klhere] = 950.0+400.0*Rand()+50.0*game.skill;
614             game.comhere = true;
615         }
616
617         // If we need a super-commander, promote a Klingon
618         if (same(game.quadrant, game.state.kscmdr)) {
619             game.quad[game.ks[1].x][game.ks[1].y] = IHS;
620             game.kpower[1] = 1175.0 + 400.0*Rand() + 125.0*game.skill;
621             game.iscate = (game.state.remkl > 1);
622             game.ishere = true;
623         }
624     }
625     // Put in Romulans if needed
626     for (i = game.klhere+1; i <= game.nenhere; i++) {
627         w = dropin(IHR);
628         game.ks[i] = w;
629         game.kdist[i] = game.kavgd[i] = distance(game.sector, w);
630         game.kpower[i] = Rand()*400.0 + 450.0 + 50.0*game.skill;
631     }
632     // If quadrant needs a starbase, put it in
633     if (q->starbase)
634         game.base = dropin(IHB);
635         
636     // If quadrant needs a planet, put it in
637     if (q->planet != NOPLANET) {
638         game.iplnet = q->planet;
639         if (game.state.plnets[q->planet].inhabited == UNINHABITED)
640             game.plnet = dropin(IHP);
641         else
642             game.plnet = dropin(IHW);
643     }
644     // Check for condition
645     newcnd();
646     // And finally the stars
647     for (i = 1; i <= q->stars; i++) 
648         dropin(IHSTAR);
649
650     // Check for RNZ
651     if (game.irhere > 0 && game.klhere == 0 && (q->planet == NOPLANET || game.state.plnets[q->planet].inhabited == UNINHABITED)) {
652         game.neutz = true;
653         if (!damaged(DRADIO)) {
654             skip(1);
655             prout(_("LT. Uhura- \"Captain, an urgent message."));
656             prout(_("  I'll put it on audio.\"  CLICK"));
657             skip(1);
658             prout(_("INTRUDER! YOU HAVE VIOLATED THE ROMULAN NEUTRAL ZONE."));
659             prout(_("LEAVE AT ONCE, OR YOU WILL BE DESTROYED!"));
660         }
661     }
662
663     if (shutup==0) {
664         // Put in THING if needed
665         if (same(thing, game.quadrant)) {
666             w = dropin(IHQUEST);
667             thing = randplace(GALSIZE);
668             game.nenhere++;
669             iqhere = true;
670             game.ks[game.nenhere] = w;
671             game.kdist[game.nenhere] = game.kavgd[game.nenhere] =
672                 distance(game.sector, w);
673             game.kpower[game.nenhere] = Rand()*6000.0 +500.0 +250.0*game.skill;
674             if (!damaged(DSRSENS)) {
675                 skip(1);
676                 prout(_("Mr. Spock- \"Captain, this is most unusual."));
677                 prout(_("    Please examine your short-range scan.\""));
678             }
679         }
680     }
681
682     // Decide if quadrant needs a Tholian
683     if (game.options & OPTION_THOLIAN) {
684         if ((game.skill < SKILL_GOOD && Rand() <= 0.02) ||   /* Lighten up if skill is low */
685             (game.skill == SKILL_GOOD && Rand() <= 0.05) ||
686             (game.skill > SKILL_GOOD && Rand() <= 0.08)
687             ) {
688             do {
689                 game.tholian.x = Rand() > 0.5 ? QUADSIZE : 1;
690                 game.tholian.y = Rand() > 0.5 ? QUADSIZE : 1;
691             } while (game.quad[game.tholian.x][game.tholian.y] != IHDOT);
692             game.quad[game.tholian.x][game.tholian.y] = IHT;
693             game.ithere = true;
694             game.nenhere++;
695             game.ks[game.nenhere] = game.tholian;
696             game.kdist[game.nenhere] = game.kavgd[game.nenhere] =
697                 distance(game.sector, game.tholian);
698             game.kpower[game.nenhere] = Rand()*400.0 +100.0 +25.0*game.skill;
699             /* Reserve unocupied corners */
700             if (game.quad[1][1]==IHDOT) game.quad[1][1] = 'X';
701             if (game.quad[1][QUADSIZE]==IHDOT) game.quad[1][QUADSIZE] = 'X';
702             if (game.quad[QUADSIZE][1]==IHDOT) game.quad[QUADSIZE][1] = 'X';
703             if (game.quad[QUADSIZE][QUADSIZE]==IHDOT) game.quad[QUADSIZE][QUADSIZE] = 'X';
704         }
705     }
706
707     sortkl();
708
709     // Put in a few black holes
710     for (i = 1; i <= 3; i++)
711         if (Rand() > 0.5) 
712             dropin(IHBLANK);
713
714     // Take out X's in corners if Tholian present
715     if (game.ithere) {
716         if (game.quad[1][1]=='X') game.quad[1][1] = IHDOT;
717         if (game.quad[1][QUADSIZE]=='X') game.quad[1][QUADSIZE] = IHDOT;
718         if (game.quad[QUADSIZE][1]=='X') game.quad[QUADSIZE][1] = IHDOT;
719         if (game.quad[QUADSIZE][QUADSIZE]=='X') game.quad[QUADSIZE][QUADSIZE] = IHDOT;
720     }           
721 }
722
723 void sortkl(void) 
724 /* sort Klingons by distance from us */
725 {
726     double t;
727     int j, k;
728     bool sw;
729
730     // The author liked bubble sort. So we will use it. :-(
731
732     if (game.nenhere-iqhere-game.ithere < 2) return;
733
734     do {
735         sw = false;
736         for (j = 1; j < game.nenhere; j++)
737             if (game.kdist[j] > game.kdist[j+1]) {
738                 sw = true;
739                 t = game.kdist[j];
740                 game.kdist[j] = game.kdist[j+1];
741                 game.kdist[j+1] = t;
742                 t = game.kavgd[j];
743                 game.kavgd[j] = game.kavgd[j+1];
744                 game.kavgd[j+1] = t;
745                 k = game.ks[j].x;
746                 game.ks[j].x = game.ks[j+1].x;
747                 game.ks[j+1].x = k;
748                 k = game.ks[j].y;
749                 game.ks[j].y = game.ks[j+1].y;
750                 game.ks[j+1].y = k;
751                 t = game.kpower[j];
752                 game.kpower[j] = game.kpower[j+1];
753                 game.kpower[j+1] = t;
754             }
755     } while (sw);
756 }
757
758 void setpassword(void)
759 /* set the self-destruct password */
760 {
761     if (game.options & OPTION_PLAIN) {
762         while (TRUE) {
763             chew();
764             proutn(_("Please type in a secret password- "));
765             scan();
766             strcpy(game.passwd, citem);
767             if (*game.passwd != 0) break;
768         }
769     } else {
770         int i;
771         for(i=0;i<3;i++) game.passwd[i]=(char)(97+(int)(Rand()*25));
772         game.passwd[3]=0;
773     }
774 }