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