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