Make the internal pager actually work. In the process, remove getch()
[super-star-trek.git] / setup.c
1 #include <time.h>\r
2 #include "sst.h"\r
3 \r
4 void prelim(void) {\r
5         skip(2);\r
6         prout("-SUPER- STAR TREK");\r
7         skip(1);\r
8         prout("Latest update-21 Sept 78");\r
9         skip(1);\r
10 }\r
11 \r
12 void freeze(int boss) {\r
13         char *x, *y;\r
14         FILE *fp;\r
15         int key;\r
16         if (boss) {\r
17                 strcpy(citem, "emsave.trk");\r
18         }\r
19         else {\r
20                 if ((key = scan()) == IHEOL) {\r
21                         proutn("File name: ");\r
22                         key = scan();\r
23                 }\r
24                 if (key != IHALPHA) {\r
25                         huh();\r
26                         return;\r
27                 }\r
28                 chew();\r
29                 if (strchr(citem, '.') == NULL) {\r
30                         strcat(citem, ".trk");\r
31                 }\r
32         }\r
33         if ((fp = fopen(citem, "wb")) == NULL) {\r
34                 proutn("Can't freeze game as file ");\r
35                 proutn(citem);\r
36                 skip(1);\r
37                 return;\r
38         }\r
39         strcpy(game.magic, SSTMAGIC);\r
40         fwrite(&game, sizeof(game), 1, fp);\r
41 \r
42         fclose(fp);\r
43 \r
44         /* I hope that's enough! */\r
45 }\r
46 \r
47 \r
48 void thaw(void) {\r
49         char *x, *y;\r
50         FILE *fp;\r
51         int key;\r
52 \r
53         game.passwd[0] = '\0';\r
54         if ((key = scan()) == IHEOL) {\r
55                 proutn("File name: ");\r
56                 key = scan();\r
57         }\r
58         if (key != IHALPHA) {\r
59                 huh();\r
60                 return;\r
61         }\r
62         chew();\r
63         if (strchr(citem, '.') == NULL) {\r
64                 strcat(citem, ".trk");\r
65         }\r
66         if ((fp = fopen(citem, "rb")) == NULL) {\r
67                 proutn("Can't find game file ");\r
68                 proutn(citem);\r
69                 skip(1);\r
70                 return;\r
71         }\r
72         fread(&game, sizeof(game), 1, fp);\r
73         if (strcmp(game.magic, SSTMAGIC)) {\r
74                 prout("Game file format is bad, should begin with " SSTMAGIC);\r
75                 skip(1);\r
76                 return;\r
77         }\r
78 \r
79         fclose(fp);\r
80 \r
81         /* I hope that's enough! */\r
82 }\r
83 \r
84 void abandn(void) {\r
85         int nb, l;\r
86 \r
87         chew();\r
88         if (condit==IHDOCKED) {\r
89                 if (ship!=IHE) {\r
90                         prout("You cannot abandon Ye Faerie Queene.");\r
91                         return;\r
92                 }\r
93         }\r
94         else {\r
95                 /* Must take shuttle craft to exit */\r
96                 if (game.damage[DSHUTTL]==-1) {\r
97                         prout("Ye Faerie Queene has no shuttle craft.");\r
98                         return;\r
99                 }\r
100                 if (game.damage[DSHUTTL]<0) {\r
101                         prout("Shuttle craft now serving Big Mac's.");\r
102                         return;\r
103                 }\r
104                 if (game.damage[DSHUTTL]>0) {\r
105                         prout("Shuttle craft damaged.");\r
106                         return;\r
107                 }\r
108                 if (landed==1) {\r
109                         prout("You must be aboard the Enterprise.");\r
110                         return;\r
111                 }\r
112                 if (iscraft!=1) {\r
113                         prout("Shuttle craft not currently available.");\r
114                         return;\r
115                 }\r
116                 /* Print abandon ship messages */\r
117                 skip(1);\r
118                 prouts("***ABANDON SHIP!  ABANDON SHIP!");\r
119                 skip(1);\r
120                 prouts("***ALL HANDS ABANDON SHIP!");\r
121                 skip(2);\r
122                 prout("Captain and crew escape in shuttle craft.");\r
123                 prout("Remainder of ship's complement beam down");\r
124                 prout("to nearest habitable planet.");\r
125                 if (game.state.rembase==0) {\r
126                         /* Ops! no place to go... */\r
127                         finish(FABANDN);\r
128                         return;\r
129                 }\r
130                 /* If at least one base left, give 'em the Faerie Queene */\r
131                 skip(1);\r
132                 icrystl = 0; /* crystals are lost */\r
133                 nprobes = 0; /* No probes */\r
134                 prout("You are captured by Klingons and released to");\r
135                 prout("the Federation in a prisoner-of-war exchange.");\r
136                 nb = Rand()*game.state.rembase+1;\r
137                 /* Set up quadrant and position FQ adjacient to base */\r
138                 if (quadx!=game.state.baseqx[nb] || quady!=game.state.baseqy[nb]) {\r
139                         quadx = game.state.baseqx[nb];\r
140                         quady = game.state.baseqy[nb];\r
141                         sectx = secty = 5;\r
142                         newqad(1);\r
143                 }\r
144                 for (;;) {\r
145                         /* position next to base by trial and error */\r
146                         game.quad[sectx][secty] = IHDOT;\r
147                         for (l = 1; l <= 10; l++) {\r
148                                 sectx = 3.0*Rand() - 1.0 + basex;\r
149                                 secty = 3.0*Rand() - 1.0 + basey;\r
150                                 if (sectx >= 1 && sectx <= 10 &&\r
151                                         secty >= 1 && secty <= 10 &&\r
152                                         game.quad[sectx][secty] == IHDOT) break;\r
153                         }\r
154                         if (l < 11) break; /* found a spot */\r
155                         sectx=5;\r
156                         secty=5;\r
157                         newqad(1);\r
158                 }\r
159         }\r
160         /* Get new commission */\r
161         game.quad[sectx][secty] = ship = IHF;\r
162         prout("Starfleet puts you in command of another ship,");\r
163         prout("the Faerie Queene, which is antiquated but,");\r
164         prout("still useable.");\r
165         if (icrystl!=0) prout("The dilithium crystals have been moved.");\r
166         imine=0;\r
167         iscraft=0; /* Gallileo disappears */\r
168         /* Resupply ship */\r
169         condit=IHDOCKED;\r
170         for (l = 1; l <= NDEVICES; l++) game.damage[l] = 0.0;\r
171         game.damage[DSHUTTL] = -1;\r
172         energy = inenrg = 3000.0;\r
173         shield = inshld = 1250.0;\r
174         torps = intorps = 6;\r
175         lsupres=inlsr=3.0;\r
176         shldup=0;\r
177         warpfac=5.0;\r
178         wfacsq=25.0;\r
179         return;\r
180 }\r
181         \r
182 void setup(int needprompt) {\r
183         int i,j, krem, klumper;\r
184         int ix, iy;\r
185         alldone = gamewon = 0;\r
186 #ifdef DEBUG\r
187         idebug = 0;\r
188 #endif\r
189         //  Decide how many of everything\r
190         if (choose(needprompt)) return; // frozen game\r
191         // Prepare the Enterprise\r
192         ship = IHE;\r
193         energy = inenrg = 5000.0;\r
194         shield = inshld = 2500.0;\r
195         shldchg = shldup = 0;\r
196         inlsr = 4.0;\r
197         lsupres = 4.0;\r
198         iran8(&quadx, &quady);\r
199         iran10(&sectx, &secty);\r
200         torps = intorps = 10;\r
201         nprobes = (int)(3.0*Rand() + 2.0);      /* Give them 2-4 of these wonders */\r
202         warpfac = 5.0;\r
203         wfacsq = warpfac * warpfac;\r
204         for (i=0; i <= NDEVICES; i++) game.damage[i] = 0.0;\r
205         // Set up assorted game parameters\r
206         batx = baty = 0;\r
207         game.state.date = indate = 100.0*(int)(31.0*Rand()+20.0);\r
208         game.state.killk = game.state.killc = nkinks = nhelp = resting = casual = game.state.nromkl = 0;\r
209         isatb = iscate = imine = icrystl = icraft = game.state.nsckill = game.state.nplankl = 0;\r
210         iscraft = 1;\r
211         landed = -1;\r
212         alive = 1;\r
213         docfac = 0.25;\r
214         for (i = 1; i <= 8; i++)\r
215                 for (j = 1; j <= 8; j++) game.state.newstuf[i][j] = game.starch[i][j] = 0;\r
216         // Initialize times for extraneous events\r
217         game.future[FSNOVA] = game.state.date + expran(0.5 * intime);\r
218         game.future[FTBEAM] = game.state.date + expran(1.5 * (intime / game.state.remcom));\r
219         game.future[FSNAP] = game.state.date + 1.0 + Rand(); // Force an early snapshot\r
220         game.future[FBATTAK] = game.state.date + expran(0.3*intime);\r
221         game.future[FCDBAS] = 1e30;\r
222         game.future[FSCMOVE] = game.state.nscrem ? game.state.date+0.2777 : 1e30;\r
223         game.future[FSCDBAS] = 1e30;\r
224         game.future[FDSPROB] = 1e30;\r
225         // Starchart is functional\r
226         stdamtim = 1e30;\r
227         // Put stars in the galaxy\r
228         instar = 0;\r
229         for (i=1; i<=8; i++)\r
230                 for (j=1; j<=8; j++) {\r
231                         int k = Rand()*9.0 + 1.0;\r
232                         instar += k;\r
233                         game.state.galaxy[i][j] = k;\r
234                 }\r
235         // Locate star bases in galaxy\r
236         for (i = 1; i <= inbase; i++) {\r
237                 int contflag;\r
238                 do {\r
239                         do iran8(&ix, &iy);\r
240                         while (game.state.galaxy[ix][iy] >= 10);\r
241                         contflag = FALSE;\r
242                         for (j = i-1; j > 0; j--) {\r
243                                 /* Improved placement algorithm to spread out bases */\r
244                                 double distq = square(ix-game.state.baseqx[j]) + square(iy-game.state.baseqy[j]);\r
245                                 if (distq < 6.0*(6-inbase) && Rand() < 0.75) {\r
246                                         contflag = TRUE;\r
247 #ifdef DEBUG\r
248                                         proutn("DEBUG: Abandoning base #%d at %d-%d\n", i, ix, iy);\r
249 #endif\r
250                                         break;\r
251                                 }\r
252 #ifdef DEBUG\r
253                                 else if (distq < 6.0 * (6-inbase)) {\r
254                                         proutn("DEBUG: saving base #%d, close to #%d\n", i, j);\r
255                                 }\r
256 #endif\r
257                         }\r
258                 } while (contflag);\r
259                         \r
260                 game.state.baseqx[i] = ix;\r
261                 game.state.baseqy[i] = iy;\r
262                 game.starch[ix][iy] = -1;\r
263                 game.state.galaxy[ix][iy] += 10;\r
264         }\r
265         // Position ordinary Klingon Battle Cruisers\r
266         krem = inkling - incom - game.state.nscrem;\r
267         klumper = 0.25*skill*(9.0-length)+1.0;\r
268         if (klumper > 9) klumper = 9; // Can't have more than 9 in quadrant\r
269         do {\r
270                 double r = Rand();\r
271                 int klump = (1.0 - r*r)*klumper;\r
272                 if (klump > krem) klump = krem;\r
273                 krem -= klump;\r
274                 klump *= 100;\r
275                 do iran8(&ix, &iy);\r
276                 while (game.state.galaxy[ix][iy] + klump >= 1000);\r
277                 game.state.galaxy[ix][iy] += klump;\r
278         } while (krem > 0);\r
279         // Position Klingon Commander Ships\r
280 #ifdef DEBUG\r
281         klumper = 1;\r
282 #endif\r
283         for (i = 1; i <= incom; i++) {\r
284                 do {\r
285                         do { /* IF debugging, put commanders by bases, always! */\r
286 #ifdef DEBUG\r
287                                 if (idebug && klumper <= inbase) {\r
288                                         ix = game.state.baseqx[klumper];\r
289                                         iy = game.state.baseqy[klumper];\r
290                                         klumper++;\r
291                                 }\r
292                                 else\r
293 #endif\r
294                                         iran8(&ix, &iy);\r
295                         }\r
296                         while ((game.state.galaxy[ix][iy] < 99 && Rand() < 0.75)||\r
297                                    game.state.galaxy[ix][iy]>899);\r
298                         // check for duplicate\r
299                         for (j = 1; j < i; j++)\r
300                                 if (game.state.cx[j]==ix && game.state.cy[j]==iy) break;\r
301                 } while (j < i);\r
302                 game.state.galaxy[ix][iy] += 100;\r
303                 game.state.cx[i] = ix;\r
304                 game.state.cy[i] = iy;\r
305         }\r
306         // Locate planets in galaxy\r
307         for (i = 0; i < inplan; i++) {\r
308                 do iran8(&ix, &iy);\r
309                 while (game.state.newstuf[ix][iy] > 0);\r
310                 game.state.newstuf[ix][iy] = 1;\r
311                 game.state.plnets[i].x = ix;\r
312                 game.state.plnets[i].y = iy;\r
313                 game.state.plnets[i].pclass = Rand()*3.0; // Planet class M N or O\r
314                 game.state.plnets[i].crystals = 1.5*Rand();             // 1 in 3 chance of crystals\r
315                 game.state.plnets[i].known = unknown;\r
316         }\r
317         // Locate Romulans\r
318         for (i = 1; i <= game.state.nromrem; i++) {\r
319                 iran8(&ix, &iy);\r
320                 game.state.newstuf[ix][iy] += 10;\r
321         }\r
322         // Locate the Super Commander\r
323         if (game.state.nscrem > 0) {\r
324                 do iran8(&ix, &iy);\r
325                 while (game.state.galaxy[ix][iy] >= 900);\r
326                 game.state.isx = ix;\r
327                 game.state.isy = iy;\r
328                 game.state.galaxy[ix][iy] += 100;\r
329         }\r
330         // Place thing (in tournament game, thingx == -1, don't want one!)\r
331         if (Rand() < 0.1 && thingx != -1) {\r
332                 iran8(&thingx, &thingy);\r
333         }\r
334         else {\r
335                 thingx = thingy = 0;\r
336         }\r
337 \r
338 //      idate = date;\r
339         skip(3);\r
340         game.state.snap = 0;\r
341                 \r
342         if (skill == 1) {\r
343                 prout("It is stardate %d. The Federation is being attacked by",\r
344                            (int)game.state.date);\r
345                 prout("a deadly Klingon invasion force. As captain of the United");\r
346                 prout("Starship U.S.S. Enterprise, it is your mission to seek out");\r
347                 prout("and destroy this invasion force of %d battle cruisers.",\r
348                            inkling);\r
349                 prout("You have an initial allotment of %d stardates to complete", (int)intime);\r
350                 prout("your mission.  As you proceed you may be given more time.");\r
351                 prout("");\r
352                 prout("You will have %d supporting starbases.", inbase);\r
353                 proutn("Starbase locations-  ");\r
354         }\r
355         else {\r
356                 prout("Stardate %d.", (int)game.state.date);\r
357                 prout("");\r
358                 prout("%d Klingons.", inkling);\r
359                 prout("An unknown number of Romulans.");\r
360                 if (game.state.nscrem) prout("and one (GULP) Super-Commander.");\r
361                         prout("%d stardates.",(int)intime);\r
362                         proutn("%d starbases in ", inbase);\r
363         }\r
364         for (i = 1; i <= inbase; i++) {\r
365                 proutn(cramlc(0, game.state.baseqx[i], game.state.baseqy[i]));\r
366                 proutn("  ");\r
367         }\r
368         skip(2);\r
369         proutn("The Enterprise is currently in ");\r
370         proutn(cramlc(quadrant, quadx, quady));\r
371         proutn(" ");\r
372         proutn(cramlc(sector, sectx, secty));\r
373         skip(2);\r
374         prout("Good Luck!");\r
375         if (game.state.nscrem) prout("  YOU'LL NEED IT.");\r
376         newqad(0);\r
377         if (nenhere) shldup=1.0;\r
378         if (neutz) attack(0);   // bad luck to start in a Romulan Neutral Zone\r
379 }\r
380 \r
381 int choose(int needprompt) {\r
382         tourn = 0;\r
383         thawed = 0;\r
384         skill = 0;\r
385         length = 0;\r
386         while (TRUE) {\r
387                 if (needprompt) /* Can start with command line options */\r
388                     proutn("Would you like a regular, tournament, or frozen game?");\r
389                 scan();\r
390                 if (strlen(citem)==0) continue; // Try again\r
391                 if (isit("tournament")) {\r
392                         while (scan() == IHEOL) {\r
393                                 proutn("Type in tournament number-");\r
394                         }\r
395                         if (aaitem == 0) {\r
396                                 chew();\r
397                                 continue; // We don't want a blank entry\r
398                         }\r
399                         tourn = (int)aaitem;\r
400                         thingx = -1;\r
401                         srand((unsigned int)(int)aaitem);\r
402                         break;\r
403                 }\r
404                 if (isit("frozen")) {\r
405                         thaw();\r
406                         chew();\r
407                         if (*game.passwd==0) continue;\r
408                         randomize();\r
409                         Rand(); Rand(); Rand(); Rand();\r
410                         if (!alldone) thawed = 1; // No plaque if not finished\r
411                         report(1);\r
412                         return TRUE;\r
413                 }\r
414                 if (isit("regular")) {\r
415                         skip(2);\r
416                         randomize();\r
417                         Rand(); Rand(); Rand(); Rand();\r
418                         break;\r
419                 }\r
420                 proutn("What is \"");\r
421                 proutn(citem);\r
422                 prout("\"?");\r
423                 chew();\r
424         }\r
425         while (length==0 || skill==0) {\r
426                 if (scan() == IHALPHA) {\r
427                         if (isit("short")) length = 1;\r
428                         else if (isit("medium")) length = 2;\r
429                         else if (isit("long")) length = 4;\r
430                         else if (isit("novice")) skill = 1;\r
431                         else if (isit("fair")) skill = 2;\r
432                         else if (isit("good")) skill = 3;\r
433                         else if (isit("expert")) skill = 4;\r
434                         else if (isit("emeritus")) skill = 5;\r
435                         else {\r
436                                 proutn("What is \"");\r
437                                 proutn(citem);\r
438                                 prout("\"?");\r
439                         }\r
440                 }\r
441                 else {\r
442                         chew();\r
443                         if (length==0) proutn("Would you like a Short, Medium, or Long game? ");\r
444                         else if (skill == 0) proutn("Are you a Novice, Fair, Good, Expert, or Emeritus player?");\r
445                 }\r
446         }\r
447         while (TRUE) {\r
448                 scan();\r
449                 strcpy(game.passwd, citem);\r
450                 chew();\r
451                 if (*game.passwd != 0) break;\r
452                 proutn("Please type in a secret password-");\r
453         }\r
454 #ifdef DEBUG\r
455         if (strcmp(game.passwd, "debug")==0) idebug = 1;\r
456 #endif\r
457 \r
458         // Use parameters to generate initial values of things\r
459         damfac = 0.5 * skill;\r
460         game.state.rembase = 3.0*Rand()+2.0;\r
461         inbase = game.state.rembase;\r
462         inplan = (PLNETMAX/2) + (PLNETMAX/2+1)*Rand();\r
463         game.state.nromrem = (2.0+Rand())*skill;\r
464         game.state.nscrem = (skill > 2? 1 : 0);\r
465         game.state.remtime = 7.0 * length;\r
466         intime = game.state.remtime;\r
467         game.state.remkl = 2.0*intime*((skill+1 - 2*Rand())*skill*0.1+.15);\r
468         inkling = game.state.remkl;\r
469         incom = skill + 0.0625*inkling*Rand();\r
470         game.state.remcom= min(10, incom);\r
471         incom = game.state.remcom;\r
472         game.state.remres = (inkling+4*incom)*intime;\r
473         inresor = game.state.remres;\r
474         if (inkling > 50) {\r
475                 inbase = (game.state.rembase += 1);\r
476         }\r
477         return FALSE;\r
478 }\r
479 \r
480 void dropin(int iquad, int *ix, int *iy) {\r
481         do iran10(ix, iy);\r
482         while (game.quad[*ix][*iy] != IHDOT);\r
483         game.quad[*ix][*iy] = iquad;\r
484 }\r
485 \r
486 void newcnd(void) {\r
487         condit = IHGREEN;\r
488         if (energy < 1000.0) condit = IHYELLOW;\r
489         if (game.state.galaxy[quadx][quady] > 99 || game.state.newstuf[quadx][quady] > 9)\r
490                 condit = IHRED;\r
491 }\r
492 \r
493 \r
494 void newqad(int shutup) {\r
495         int quadnum = game.state.galaxy[quadx][quady];\r
496         int newnum = game.state.newstuf[quadx][quady];\r
497         int i, j, ix, iy, nplan;\r
498 \r
499         iattak = 1;\r
500         justin = 1;\r
501         basex = basey = 0;\r
502         klhere = 0;\r
503         comhere = 0;\r
504         plnetx = plnety = 0;\r
505         ishere = 0;\r
506         irhere = 0;\r
507         iplnet = 0;\r
508         nenhere = 0;\r
509         neutz = 0;\r
510         inorbit = 0;\r
511         landed = -1;\r
512         ientesc = 0;\r
513         ithere = 0;\r
514         iseenit = 0;\r
515         if (iscate) {\r
516                 // Attempt to escape Super-commander, so tbeam back!\r
517                 iscate = 0;\r
518                 ientesc = 1;\r
519         }\r
520         // Clear quadrant\r
521         for (i=1; i <= 10; i++)\r
522                 for (j=1; j <= 10; j++) game.quad[i][j] = IHDOT;\r
523         // cope with supernova\r
524         if (quadnum > 999) {\r
525                 return;\r
526         }\r
527         klhere = quadnum/100;\r
528         irhere = newnum/10;\r
529         nplan = newnum%10;\r
530         nenhere = klhere + irhere;\r
531 \r
532         // Position Starship\r
533         game.quad[sectx][secty] = ship;\r
534 \r
535         // Decide if quadrant needs a Tholian\r
536         if ((skill < 3 && Rand() <= 0.02) ||   /* Lighten up if skill is low */\r
537                 (skill == 3 && Rand() <= 0.05) ||\r
538                 (skill > 3 && Rand() <= 0.08)\r
539 #ifdef DEBUG\r
540                 || strcmp(game.passwd, "tholianx")==0\r
541 #endif\r
542                 ) {\r
543                 do {\r
544                         ithx = Rand() > 0.5 ? 10 : 1;\r
545                         ithy = Rand() > 0.5 ? 10 : 1;\r
546                 } while (game.quad[ithx][ithy] != IHDOT);\r
547                 game.quad[ithx][ithy] = IHT;\r
548                 ithere = 1;\r
549                 /* Reserve unocupied corners */\r
550                 if (game.quad[1][1]==IHDOT) game.quad[1][1] = 'X';\r
551                 if (game.quad[1][10]==IHDOT) game.quad[1][10] = 'X';\r
552                 if (game.quad[10][1]==IHDOT) game.quad[10][1] = 'X';\r
553                 if (game.quad[10][10]==IHDOT) game.quad[10][10] = 'X';\r
554         }\r
555 \r
556         if (quadnum >= 100) {\r
557                 // Position ordinary Klingons\r
558                 quadnum -= 100*klhere;\r
559                 for (i = 1; i <= klhere; i++) {\r
560                         dropin(IHK, &ix, &iy);\r
561                         game.kx[i] = ix;\r
562                         game.ky[i] = iy;\r
563                         game.kdist[i] = game.kavgd[i] = sqrt(square(sectx-ix) + square(secty-iy));\r
564                         game.kpower[i] = Rand()*150.0 +300.0 +25.0*skill;\r
565                 }\r
566                 // If we need a commander, promote a Klingon\r
567                 for (i = 1; i <= game.state.remcom ; i++) \r
568                         if (game.state.cx[i]==quadx && game.state.cy[i]==quady) break;\r
569                         \r
570                 if (i <= game.state.remcom) {\r
571                         game.quad[ix][iy] = IHC;\r
572                         game.kpower[klhere] = 950.0+400.0*Rand()+50.0*skill;\r
573                         comhere = 1;\r
574                 }\r
575 \r
576                 // If we need a super-commander, promote a Klingon\r
577                 if (quadx == game.state.isx && quady == game.state.isy) {\r
578                         game.quad[game.kx[1]][game.ky[1]] = IHS;\r
579                         game.kpower[1] = 1175.0 + 400.0*Rand() + 125.0*skill;\r
580                         iscate = 1;\r
581                         ishere = 1;\r
582                 }\r
583         }\r
584         // Put in Romulans if needed\r
585         for (i = klhere+1; i <= nenhere; i++) {\r
586                 dropin(IHR, &ix, &iy);\r
587                 game.kx[i] = ix;\r
588                 game.ky[i] = iy;\r
589                 game.kdist[i] = game.kavgd[i] = sqrt(square(sectx-ix) + square(secty-iy));\r
590                 game.kpower[i] = Rand()*400.0 + 450.0 + 50.0*skill;\r
591         }\r
592         sortkl();\r
593         // If quadrant needs a starbase, put it in\r
594         if (quadnum >= 10) {\r
595                 quadnum -= 10;\r
596                 dropin(IHB, &basex, &basey);\r
597         }\r
598         \r
599         if (nplan) {\r
600                 // If quadrant needs a planet, put it in\r
601                 for (i=0; i < inplan; i++)\r
602                         if (game.state.plnets[i].x == quadx && game.state.plnets[i].y == quady) break;\r
603                 if (i < inplan) {\r
604                         iplnet = i;\r
605                         dropin(IHP, &plnetx, &plnety);\r
606                 }\r
607         }\r
608         // Check for condition\r
609         newcnd();\r
610         // And finally the stars\r
611         for (i = 1; i <= quadnum; i++) dropin(IHSTAR, &ix, &iy);\r
612 \r
613         // Check for RNZ\r
614         if (irhere > 0 && klhere == 0 && basex == 0) {\r
615                 neutz = 1;\r
616                 if (game.damage[DRADIO] <= 0.0) {\r
617                         skip(1);\r
618                         prout("LT. Uhura- \"Captain, an urgent message.");\r
619                         prout("  I'll put it on audio.\"  CLICK");\r
620                         skip(1);\r
621                         prout("INTRUDER! YOU HAVE VIOLATED THE ROMULAN NEUTRAL ZONE.");\r
622                         prout("LEAVE AT ONCE, OR YOU WILL BE DESTROYED!");\r
623                 }\r
624         }\r
625 \r
626         if (shutup==0) {\r
627                 // Put in THING if needed\r
628                 if (thingx == quadx && thingy == quady) {\r
629                         dropin(IHQUEST, &ix, &iy);\r
630                         thingx = thingy = 0; // Transient\r
631                         if (game.damage[DSRSENS] == 0.0) {\r
632                                 skip(1);\r
633                                 prout("MR. SPOCK- \"Captain, this is most unusual.");\r
634                                 prout("    Please examine your short-range scan.\"");\r
635                         }\r
636                 }\r
637         }\r
638 \r
639         // Put in a few black holes\r
640         for (i = 1; i <= 3; i++)\r
641                 if (Rand() > 0.5) dropin(IHBLANK, &ix, &iy);\r
642 \r
643         // Take out X's in corners if Tholian present\r
644         if (ithere) {\r
645                 if (game.quad[1][1]=='X') game.quad[1][1] = IHDOT;\r
646                 if (game.quad[1][10]=='X') game.quad[1][10] = IHDOT;\r
647                 if (game.quad[10][1]=='X') game.quad[10][1] = IHDOT;\r
648                 if (game.quad[10][10]=='X') game.quad[10][10] = IHDOT;\r
649         }               \r
650 }\r
651 \r
652 void sortkl(void) {\r
653         double t;\r
654         int sw, j, k;\r
655 \r
656         // The author liked bubble sort. So we will use it. :-(\r
657 \r
658         if (nenhere < 2) return;\r
659 \r
660         do {\r
661                 sw = FALSE;\r
662                 for (j = 1; j < nenhere; j++)\r
663                         if (game.kdist[j] > game.kdist[j+1]) {\r
664                                 sw = TRUE;\r
665                                 t = game.kdist[j];\r
666                                 game.kdist[j] = game.kdist[j+1];\r
667                                 game.kdist[j+1] = t;\r
668                                 t = game.kavgd[j];\r
669                                 game.kavgd[j] = game.kavgd[j+1];\r
670                                 game.kavgd[j+1] = t;\r
671                                 k = game.kx[j];\r
672                                 game.kx[j] = game.kx[j+1];\r
673                                 game.kx[j+1] = k;\r
674                                 k = game.ky[j];\r
675                                 game.ky[j] = game.ky[j+1];\r
676                                 game.ky[j+1] = k;\r
677                                 t = game.kpower[j];\r
678                                 game.kpower[j] = game.kpower[j+1];\r
679                                 game.kpower[j+1] = t;\r
680                         }\r
681         } while (sw);\r
682 }\r