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