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