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