More merging of SERGEEV changes.
[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         FILE *fp;\r
14         int key;\r
15         if (boss) {\r
16                 strcpy(citem, "emsave.trk");\r
17         }\r
18         else {\r
19                 if ((key = scan()) == IHEOL) {\r
20                         proutn("File name: ");\r
21                         key = scan();\r
22                 }\r
23                 if (key != IHALPHA) {\r
24                         huh();\r
25                         return;\r
26                 }\r
27                 chew();\r
28                 if (strchr(citem, '.') == NULL) {\r
29                         strcat(citem, ".trk");\r
30                 }\r
31         }\r
32         if ((fp = fopen(citem, "wb")) == NULL) {\r
33                 proutn("Can't freeze game as file ");\r
34                 proutn(citem);\r
35                 skip(1);\r
36                 return;\r
37         }\r
38         strcpy(game.magic, SSTMAGIC);\r
39         fwrite(&game, sizeof(game), 1, fp);\r
40 \r
41         fclose(fp);\r
42 \r
43         /* I hope that's enough! */\r
44 }\r
45 \r
46 \r
47 int thaw(void) {\r
48         FILE *fp;\r
49         int key;\r
50 \r
51         game.passwd[0] = '\0';\r
52         if ((key = scan()) == IHEOL) {\r
53                 proutn("File name: ");\r
54                 key = scan();\r
55         }\r
56         if (key != IHALPHA) {\r
57                 huh();\r
58                 return 1;\r
59         }\r
60         chew();\r
61         if (strchr(citem, '.') == NULL) {\r
62                 strcat(citem, ".trk");\r
63         }\r
64         if ((fp = fopen(citem, "rb")) == NULL) {\r
65                 proutn("Can't find game file ");\r
66                 proutn(citem);\r
67                 skip(1);\r
68                 return 1;\r
69         }\r
70         fread(&game, sizeof(game), 1, fp);\r
71         if (strcmp(game.magic, SSTMAGIC)) {\r
72                 prout("Game file format is bad, should begin with " SSTMAGIC);\r
73                 skip(1);\r
74                 fclose(fp);\r
75                 return 1;\r
76         }\r
77 \r
78         fclose(fp);\r
79 \r
80         return 0;\r
81 }\r
82 \r
83 void abandn(void) {\r
84         int nb, l;\r
85 \r
86         chew();\r
87         if (condit==IHDOCKED) {\r
88                 if (ship!=IHE) {\r
89                         prout("You cannot abandon Ye Faerie Queene.");\r
90                         return;\r
91                 }\r
92         }\r
93         else {\r
94                 /* Must take shuttle craft to exit */\r
95                 if (game.damage[DSHUTTL]==-1) {\r
96                         prout("Ye Faerie Queene has no shuttle craft.");\r
97                         return;\r
98                 }\r
99                 if (game.damage[DSHUTTL]<0) {\r
100                         prout("Shuttle craft now serving Big Mac's.");\r
101                         return;\r
102                 }\r
103                 if (game.damage[DSHUTTL]>0) {\r
104                         prout("Shuttle craft damaged.");\r
105                         return;\r
106                 }\r
107                 if (landed==1) {\r
108                         prout("You must be aboard the Enterprise.");\r
109                         return;\r
110                 }\r
111                 if (iscraft!=1) {\r
112                         prout("Shuttle craft not currently available.");\r
113                         return;\r
114                 }\r
115                 /* Print abandon ship messages */\r
116                 skip(1);\r
117                 prouts("***ABANDON SHIP!  ABANDON SHIP!");\r
118                 skip(1);\r
119                 prouts("***ALL HANDS ABANDON SHIP!");\r
120                 skip(2);\r
121                 prout("Captain and crew escape in shuttle craft.");\r
122                 prout("Remainder of ship's complement beam down");\r
123                 prout("to nearest habitable planet.");\r
124                 if (game.state.rembase==0) {\r
125                         /* Ops! no place to go... */\r
126                         finish(FABANDN);\r
127                         return;\r
128                 }\r
129                 /* If at least one base left, give 'em the Faerie Queene */\r
130                 skip(1);\r
131                 icrystl = 0; /* crystals are lost */\r
132                 nprobes = 0; /* No probes */\r
133                 prout("You are captured by Klingons and released to");\r
134                 prout("the Federation in a prisoner-of-war exchange.");\r
135                 nb = Rand()*game.state.rembase+1;\r
136                 /* Set up quadrant and position FQ adjacient to base */\r
137                 if (quadx!=game.state.baseqx[nb] || quady!=game.state.baseqy[nb]) {\r
138                         quadx = game.state.baseqx[nb];\r
139                         quady = game.state.baseqy[nb];\r
140                         sectx = secty = 5;\r
141                         newqad(1);\r
142                 }\r
143                 for (;;) {\r
144                         /* position next to base by trial and error */\r
145                         game.quad[sectx][secty] = IHDOT;\r
146                         for (l = 1; l <= 10; l++) {\r
147                                 sectx = 3.0*Rand() - 1.0 + basex;\r
148                                 secty = 3.0*Rand() - 1.0 + basey;\r
149                                 if (sectx >= 1 && sectx <= 10 &&\r
150                                         secty >= 1 && secty <= 10 &&\r
151                                         game.quad[sectx][secty] == IHDOT) break;\r
152                         }\r
153                         if (l < 11) break; /* found a spot */\r
154                         sectx=5;\r
155                         secty=5;\r
156                         newqad(1);\r
157                 }\r
158         }\r
159         /* Get new commission */\r
160         game.quad[sectx][secty] = ship = IHF;\r
161         prout("Starfleet puts you in command of another ship,");\r
162         prout("the Faerie Queene, which is antiquated but,");\r
163         prout("still useable.");\r
164         if (icrystl!=0) prout("The dilithium crystals have been moved.");\r
165         imine=0;\r
166         iscraft=0; /* Gallileo disappears */\r
167         /* Resupply ship */\r
168         condit=IHDOCKED;\r
169         for (l = 1; l <= NDEVICES; l++) game.damage[l] = 0.0;\r
170         game.damage[DSHUTTL] = -1;\r
171         energy = inenrg = 3000.0;\r
172         shield = inshld = 1250.0;\r
173         torps = intorps = 6;\r
174         lsupres=inlsr=3.0;\r
175         shldup=0;\r
176         warpfac=5.0;\r
177         wfacsq=25.0;\r
178         return;\r
179 }\r
180         \r
181 void setup(int needprompt) {\r
182         int i,j, krem, klumper;\r
183         int ix, iy;\r
184 #ifdef DEBUG\r
185         idebug = 0;\r
186 #endif\r
187         //  Decide how many of everything\r
188         if (choose(needprompt)) return; // frozen game\r
189         // Prepare the Enterprise\r
190         alldone = gamewon = 0;\r
191         ship = IHE;\r
192         energy = inenrg = 5000.0;\r
193         shield = inshld = 2500.0;\r
194         shldchg = shldup = 0;\r
195         inlsr = 4.0;\r
196         lsupres = 4.0;\r
197         iran8(&quadx, &quady);\r
198         iran10(&sectx, &secty);\r
199         torps = intorps = 10;\r
200         nprobes = (int)(3.0*Rand() + 2.0);      /* Give them 2-4 of these wonders */\r
201         warpfac = 5.0;\r
202         wfacsq = warpfac * warpfac;\r
203         for (i=0; i <= NDEVICES; i++) game.damage[i] = 0.0;\r
204         // Set up assorted game parameters\r
205         batx = baty = 0;\r
206         game.state.date = indate = 100.0*(int)(31.0*Rand()+20.0);\r
207         game.state.killk = game.state.killc = nkinks = nhelp = resting = casual = game.state.nromkl = 0;\r
208         isatb = iscate = imine = icrystl = icraft = game.state.nsckill = game.state.nplankl = 0;\r
209         game.state.starkl = game.state.basekl = 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 (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(2);\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 #ifdef SERGEEV\r
377         getche();\r
378 #endif /* SERGEEV */\r
379         newqad(0);\r
380         if (nenhere-iqhere-ithere) shldup=1.0;\r
381         if (neutz) attack(0);   // bad luck to start in a Romulan Neutral Zone\r
382 }\r
383 \r
384 void randomize(void) {\r
385         srand((int)time(NULL));\r
386 }\r
387 \r
388 int choose(int needprompt) {\r
389         tourn = 0;\r
390         thawed = 0;\r
391         skill = 0;\r
392         length = 0;\r
393         while (TRUE) {\r
394                 if (needprompt) /* Can start with command line options */\r
395                     proutn("Would you like a regular, tournament, or frozen game?");\r
396                 scan();\r
397                 if (strlen(citem)==0) continue; // Try again\r
398                 if (isit("tournament")) {\r
399                         while (scan() == IHEOL) {\r
400                                 proutn("Type in tournament number-");\r
401                         }\r
402                         if (aaitem == 0) {\r
403                                 chew();\r
404                                 continue; // We don't want a blank entry\r
405                         }\r
406                         tourn = (int)aaitem;\r
407                         thingx = -1;\r
408                         srand((unsigned int)(int)aaitem);\r
409                         break;\r
410                 }\r
411                 if (isit("frozen")) {\r
412                         thaw();\r
413                         chew();\r
414                         if (*game.passwd==0) continue;\r
415                         randomize();\r
416                         Rand(); Rand(); Rand(); Rand();\r
417                         if (!alldone) thawed = 1; // No plaque if not finished\r
418                         report(1);\r
419                         return TRUE;\r
420                 }\r
421                 if (isit("regular")) {\r
422                         skip(2);\r
423                         randomize();\r
424                         Rand(); Rand(); Rand(); Rand();\r
425                         break;\r
426                 }\r
427                 proutn("What is \"");\r
428                 proutn(citem);\r
429                 prout("\"?");\r
430                 chew();\r
431         }\r
432         while (length==0 || skill==0) {\r
433                 if (scan() == IHALPHA) {\r
434                         if (isit("short")) length = 1;\r
435                         else if (isit("medium")) length = 2;\r
436                         else if (isit("long")) length = 4;\r
437                         else if (isit("novice")) skill = 1;\r
438                         else if (isit("fair")) skill = 2;\r
439                         else if (isit("good")) skill = 3;\r
440                         else if (isit("expert")) skill = 4;\r
441                         else if (isit("emeritus")) skill = 5;\r
442                         else {\r
443                                 proutn("What is \"");\r
444                                 proutn(citem);\r
445                                 prout("\"?");\r
446                         }\r
447                 }\r
448                 else {\r
449                         chew();\r
450                         if (length==0) proutn("Would you like a Short, Medium, or Long game? ");\r
451                         else if (skill == 0) proutn("Are you a Novice, Fair, Good, Expert, or Emeritus player? ");\r
452                 }\r
453         }\r
454 #ifndef SERGEEV\r
455         while (TRUE) {\r
456                 scan();\r
457                 strcpy(game.passwd, citem);\r
458                 chew();\r
459                 if (*game.passwd != 0) break;\r
460                 proutn("Please type in a secret password-");\r
461         }\r
462 #else\r
463         for(i=0;i<3;i++) game.passwd[i]=(char)(97+(int)(Rand()*25));\r
464         game.passwd[3]=0;\r
465 #endif /* SERGEEV */\r
466 #ifdef DEBUG\r
467         if (strcmp(game.passwd, "debug")==0) idebug = 1;\r
468 #endif\r
469 \r
470         // Use parameters to generate initial values of things\r
471         damfac = 0.5 * skill;\r
472         game.state.rembase = 3.0*Rand()+2.0;\r
473         inbase = game.state.rembase;\r
474         inplan = (PLNETMAX/2) + (PLNETMAX/2+1)*Rand();\r
475         game.state.nromrem = (2.0+Rand())*skill;\r
476         game.state.nscrem = (skill > 2? 1 : 0);\r
477         game.state.remtime = 7.0 * length;\r
478         intime = game.state.remtime;\r
479         game.state.remkl = 2.0*intime*((skill+1 - 2*Rand())*skill*0.1+.15);\r
480         inkling = game.state.remkl;\r
481         incom = skill + 0.0625*inkling*Rand();\r
482         game.state.remcom= min(10, incom);\r
483         incom = game.state.remcom;\r
484         game.state.remres = (inkling+4*incom)*intime;\r
485         inresor = game.state.remres;\r
486         if (inkling > 50) {\r
487                 inbase = (game.state.rembase += 1);\r
488         }\r
489         return FALSE;\r
490 }\r
491 \r
492 void dropin(int iquad, int *ix, int *iy) {\r
493         do iran10(ix, iy);\r
494         while (game.quad[*ix][*iy] != IHDOT);\r
495         game.quad[*ix][*iy] = iquad;\r
496 }\r
497 \r
498 void newcnd(void) {\r
499         condit = IHGREEN;\r
500         if (energy < 1000.0) condit = IHYELLOW;\r
501         if (game.state.galaxy[quadx][quady] > 99 || game.state.newstuf[quadx][quady] > 9)\r
502                 condit = IHRED;\r
503         if (!alive) condit=IHDEAD;\r
504 }\r
505 \r
506 \r
507 void newqad(int shutup) {\r
508         int quadnum = game.state.galaxy[quadx][quady];\r
509         int newnum = game.state.newstuf[quadx][quady];\r
510         int i, j, ix, iy, nplan;\r
511 \r
512         iattak = 1;\r
513         justin = 1;\r
514         basex = basey = 0;\r
515         klhere = 0;\r
516         comhere = 0;\r
517         plnetx = plnety = 0;\r
518         ishere = 0;\r
519         irhere = 0;\r
520         iplnet = 0;\r
521         nenhere = 0;\r
522         neutz = 0;\r
523         inorbit = 0;\r
524         landed = -1;\r
525         ientesc = 0;\r
526         ithere = 0;\r
527         iqhere=0;\r
528         iqengry=0;\r
529         iseenit = 0;\r
530         if (iscate) {\r
531                 // Attempt to escape Super-commander, so tbeam back!\r
532                 iscate = 0;\r
533                 ientesc = 1;\r
534         }\r
535         // Clear quadrant\r
536         for (i=1; i <= 10; i++)\r
537                 for (j=1; j <= 10; j++) game.quad[i][j] = IHDOT;\r
538         // cope with supernova\r
539         if (quadnum > 999) {\r
540                 return;\r
541         }\r
542         klhere = quadnum/100;\r
543         irhere = newnum/10;\r
544         nplan = newnum%10;\r
545         nenhere = klhere + irhere;\r
546 \r
547         // Position Starship\r
548         game.quad[sectx][secty] = ship;\r
549 \r
550         if (quadnum >= 100) {\r
551                 // Position ordinary Klingons\r
552                 quadnum -= 100*klhere;\r
553                 for (i = 1; i <= klhere; i++) {\r
554                         dropin(IHK, &ix, &iy);\r
555                         game.kx[i] = ix;\r
556                         game.ky[i] = iy;\r
557                         game.kdist[i] = game.kavgd[i] = sqrt(square(sectx-ix) + square(secty-iy));\r
558                         game.kpower[i] = Rand()*150.0 +300.0 +25.0*skill;\r
559                 }\r
560                 // If we need a commander, promote a Klingon\r
561                 for (i = 1; i <= game.state.remcom ; i++) \r
562                         if (game.state.cx[i]==quadx && game.state.cy[i]==quady) break;\r
563                         \r
564                 if (i <= game.state.remcom) {\r
565                         game.quad[ix][iy] = IHC;\r
566                         game.kpower[klhere] = 950.0+400.0*Rand()+50.0*skill;\r
567                         comhere = 1;\r
568                 }\r
569 \r
570                 // If we need a super-commander, promote a Klingon\r
571                 if (quadx == game.state.isx && quady == game.state.isy) {\r
572                         game.quad[game.kx[1]][game.ky[1]] = IHS;\r
573                         game.kpower[1] = 1175.0 + 400.0*Rand() + 125.0*skill;\r
574                         iscate = game.state.remkl>1;\r
575                         ishere = 1;\r
576                 }\r
577         }\r
578         // Put in Romulans if needed\r
579         for (i = klhere+1; i <= nenhere; i++) {\r
580                 dropin(IHR, &ix, &iy);\r
581                 game.kx[i] = ix;\r
582                 game.ky[i] = iy;\r
583                 game.kdist[i] = game.kavgd[i] = sqrt(square(sectx-ix) + square(secty-iy));\r
584                 game.kpower[i] = Rand()*400.0 + 450.0 + 50.0*skill;\r
585         }\r
586         sortkl();\r
587         // If quadrant needs a starbase, put it in\r
588         if (quadnum >= 10) {\r
589                 quadnum -= 10;\r
590                 dropin(IHB, &basex, &basey);\r
591         }\r
592         \r
593         if (nplan) {\r
594                 // If quadrant needs a planet, put it in\r
595                 for (i=0; i < inplan; i++)\r
596                         if (game.state.plnets[i].x == quadx && game.state.plnets[i].y == quady) break;\r
597                 if (i < inplan) {\r
598                         iplnet = i;\r
599                         dropin(IHP, &plnetx, &plnety);\r
600                 }\r
601         }\r
602         // Check for condition\r
603         newcnd();\r
604         // And finally the stars\r
605         for (i = 1; i <= quadnum; i++) dropin(IHSTAR, &ix, &iy);\r
606 \r
607         // Check for RNZ\r
608         if (irhere > 0 && klhere == 0) {\r
609                 neutz = 1;\r
610                 if (game.damage[DRADIO] <= 0.0) {\r
611                         skip(1);\r
612                         prout("LT. Uhura- \"Captain, an urgent message.");\r
613                         prout("  I'll put it on audio.\"  CLICK");\r
614                         skip(1);\r
615                         prout("INTRUDER! YOU HAVE VIOLATED THE ROMULAN NEUTRAL ZONE.");\r
616                         prout("LEAVE AT ONCE, OR YOU WILL BE DESTROYED!");\r
617                 }\r
618         }\r
619 \r
620         if (shutup==0) {\r
621                 // Put in THING if needed\r
622                 if (thingx == quadx && thingy == quady) {\r
623                         dropin(IHQUEST, &ix, &iy);\r
624                         iran8(&thingx, &thingy);\r
625                         nenhere++;\r
626                         iqhere=1;\r
627                         game.kx[nenhere] = ix;\r
628                         game.ky[nenhere] = iy;\r
629                         game.kdist[nenhere] = game.kavgd[nenhere] =\r
630                             sqrt(square(sectx-ix) + square(secty-iy));\r
631                         game.kpower[nenhere] = Rand()*6000.0 +500.0 +250.0*skill;\r
632                         if (game.damage[DSRSENS] == 0.0) {\r
633                                 skip(1);\r
634                                 prout("MR. SPOCK- \"Captain, this is most unusual.");\r
635                                 prout("    Please examine your short-range scan.\"");\r
636                         }\r
637                 }\r
638         }\r
639 \r
640         // Decide if quadrant needs a Tholian\r
641         if ((skill < 3 && Rand() <= 0.02) ||   /* Lighten up if skill is low */\r
642                 (skill == 3 && Rand() <= 0.05) ||\r
643                 (skill > 3 && Rand() <= 0.08)\r
644 #ifdef DEBUG\r
645                 || strcmp(passwd, "tholianx")==0\r
646 #endif\r
647                 ) {\r
648                 do {\r
649                         ithx = Rand() > 0.5 ? 10 : 1;\r
650                         ithy = Rand() > 0.5 ? 10 : 1;\r
651                 } while (game.quad[ithx][ithy] != IHDOT);\r
652                 game.quad[ithx][ithy] = IHT;\r
653                 ithere = 1;\r
654                 nenhere++;\r
655                 game.kx[nenhere] = ithx;\r
656                 game.ky[nenhere] = ithy;\r
657                 game.kdist[nenhere] = game.kavgd[nenhere] =\r
658                     sqrt(square(sectx-ithx) + square(secty-ithy));\r
659                 game.kpower[nenhere] = Rand()*400.0 +100.0 +25.0*skill;\r
660                 /* Reserve unocupied corners */\r
661                 if (game.quad[1][1]==IHDOT) game.quad[1][1] = 'X';\r
662                 if (game.quad[1][10]==IHDOT) game.quad[1][10] = 'X';\r
663                 if (game.quad[10][1]==IHDOT) game.quad[10][1] = 'X';\r
664                 if (game.quad[10][10]==IHDOT) game.quad[10][10] = 'X';\r
665         }\r
666         sortkl();\r
667 \r
668         // Put in a few black holes\r
669         for (i = 1; i <= 3; i++)\r
670                 if (Rand() > 0.5) dropin(IHBLANK, &ix, &iy);\r
671 \r
672         // Take out X's in corners if Tholian present\r
673         if (ithere) {\r
674                 if (game.quad[1][1]=='X') game.quad[1][1] = IHDOT;\r
675                 if (game.quad[1][10]=='X') game.quad[1][10] = IHDOT;\r
676                 if (game.quad[10][1]=='X') game.quad[10][1] = IHDOT;\r
677                 if (game.quad[10][10]=='X') game.quad[10][10] = IHDOT;\r
678         }               \r
679 }\r
680 \r
681 void sortkl(void) {\r
682         double t;\r
683         int sw, j, k;\r
684 \r
685         // The author liked bubble sort. So we will use it. :-(\r
686 \r
687         if (nenhere-iqhere-ithere < 2) return;\r
688 \r
689         do {\r
690                 sw = FALSE;\r
691                 for (j = 1; j < nenhere; j++)\r
692                         if (game.kdist[j] > game.kdist[j+1]) {\r
693                                 sw = TRUE;\r
694                                 t = game.kdist[j];\r
695                                 game.kdist[j] = game.kdist[j+1];\r
696                                 game.kdist[j+1] = t;\r
697                                 t = game.kavgd[j];\r
698                                 game.kavgd[j] = game.kavgd[j+1];\r
699                                 game.kavgd[j+1] = t;\r
700                                 k = game.kx[j];\r
701                                 game.kx[j] = game.kx[j+1];\r
702                                 game.kx[j+1] = k;\r
703                                 k = game.ky[j];\r
704                                 game.ky[j] = game.ky[j+1];\r
705                                 game.ky[j+1] = k;\r
706                                 t = game.kpower[j];\r
707                                 game.kpower[j] = game.kpower[j+1];\r
708                                 game.kpower[j+1] = t;\r
709                         }\r
710         } while (sw);\r
711 }\r