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