7 static long filelength(int fd) {
16 prout("-SUPER- STAR TREK");
18 #ifndef __HISTORICAL__
19 prout("Latest update-21 Sept 78");
21 #endif /* __HISTORICAL__ */
24 void freeze(int boss) {
28 strcpy(citem, "emsave.trk");
31 if ((key = scan()) == IHEOL) {
32 proutn("File name: ");
40 if (strchr(citem, '.') == NULL) {
41 strcat(citem, ".trk");
44 if ((fp = fopen(citem, "wb")) == NULL) {
45 proutn("Can't freeze game as file ");
50 strcpy(game.magic, SSTMAGIC);
51 fwrite(&game, sizeof(game), 1, fp);
55 /* I hope that's enough! */
63 game.passwd[0] = '\0';
64 if ((key = scan()) == IHEOL) {
65 proutn("File name: ");
73 if (strchr(citem, '.') == NULL) {
74 strcat(citem, ".trk");
76 if ((fp = fopen(citem, "rb")) == NULL) {
77 proutn("Can't find game file ");
82 fread(&game, sizeof(game), 1, fp);
83 if (feof(fp) || ftell(fp) != filelength(fileno(fp)) || strcmp(game.magic, SSTMAGIC)) {
84 prout("Game file format is bad, should begin with " SSTMAGIC);
99 if (condit==IHDOCKED) {
101 prout("You cannot abandon Ye Faerie Queene.");
106 /* Must take shuttle craft to exit */
107 if (game.damage[DSHUTTL]==-1) {
108 prout("Ye Faerie Queene has no shuttle craft.");
111 if (game.damage[DSHUTTL]<0) {
112 prout("Shuttle craft now serving Big Mac's.");
115 if (game.damage[DSHUTTL]>0) {
116 prout("Shuttle craft damaged.");
120 prout("You must be aboard the Enterprise.");
124 prout("Shuttle craft not currently available.");
127 /* Print abandon ship messages */
129 prouts("***ABANDON SHIP! ABANDON SHIP!");
131 prouts("***ALL HANDS ABANDON SHIP!");
133 prout("Captain and crew escape in shuttle craft.");
134 prout("Remainder of ship's complement beam down");
135 prout("to nearest habitable planet.");
136 if (game.state.rembase==0) {
137 /* Ops! no place to go... */
141 /* If at least one base left, give 'em the Faerie Queene */
143 icrystl = 0; /* crystals are lost */
144 nprobes = 0; /* No probes */
145 prout("You are captured by Klingons and released to");
146 prout("the Federation in a prisoner-of-war exchange.");
147 nb = Rand()*game.state.rembase+1;
148 /* Set up quadrant and position FQ adjacient to base */
149 if (quadx!=game.state.baseqx[nb] || quady!=game.state.baseqy[nb]) {
150 quadx = game.state.baseqx[nb];
151 quady = game.state.baseqy[nb];
156 /* position next to base by trial and error */
157 game.quad[sectx][secty] = IHDOT;
158 for (l = 1; l <= 10; l++) {
159 sectx = 3.0*Rand() - 1.0 + basex;
160 secty = 3.0*Rand() - 1.0 + basey;
161 if (sectx >= 1 && sectx <= 10 &&
162 secty >= 1 && secty <= 10 &&
163 game.quad[sectx][secty] == IHDOT) break;
165 if (l < 11) break; /* found a spot */
171 /* Get new commission */
172 game.quad[sectx][secty] = ship = IHF;
173 prout("Starfleet puts you in command of another ship,");
174 prout("the Faerie Queene, which is antiquated but,");
175 prout("still useable.");
176 if (icrystl!=0) prout("The dilithium crystals have been moved.");
178 iscraft=0; /* Gallileo disappears */
181 for (l = 1; l <= NDEVICES; l++) game.damage[l] = 0.0;
182 game.damage[DSHUTTL] = -1;
183 energy = inenrg = 3000.0;
184 shield = inshld = 1250.0;
193 void setup(int needprompt) {
194 int i,j, krem, klumper;
199 // Decide how many of everything
200 if (choose(needprompt)) return; // frozen game
201 // Prepare the Enterprise
202 alldone = gamewon = 0;
204 energy = inenrg = 5000.0;
205 shield = inshld = 2500.0;
206 shldchg = shldup = 0;
209 iran8(&quadx, &quady);
210 iran10(§x, §y);
211 torps = intorps = 10;
212 nprobes = (int)(3.0*Rand() + 2.0); /* Give them 2-4 of these wonders */
214 wfacsq = warpfac * warpfac;
215 for (i=0; i <= NDEVICES; i++) game.damage[i] = 0.0;
216 // Set up assorted game parameters
218 game.state.date = indate = 100.0*(int)(31.0*Rand()+20.0);
219 game.state.killk = game.state.killc = nkinks = nhelp = resting = casual = game.state.nromkl = 0;
220 isatb = iscate = imine = icrystl = icraft = game.state.nsckill = game.state.nplankl = 0;
221 game.state.starkl = game.state.basekl = 0;
226 for (i = 1; i <= 8; i++)
227 for (j = 1; j <= 8; j++) game.state.newstuf[i][j] = game.starch[i][j] = 0;
228 // Initialize times for extraneous events
229 game.future[FSNOVA] = game.state.date + expran(0.5 * intime);
230 game.future[FTBEAM] = game.state.date + expran(1.5 * (intime / game.state.remcom));
231 game.future[FSNAP] = game.state.date + 1.0 + Rand(); // Force an early snapshot
232 game.future[FBATTAK] = game.state.date + expran(0.3*intime);
233 game.future[FCDBAS] = 1e30;
234 game.future[FSCMOVE] = game.state.nscrem ? game.state.date+0.2777 : 1e30;
235 game.future[FSCDBAS] = 1e30;
236 game.future[FDSPROB] = 1e30;
237 // Starchart is functional
239 // Put stars in the galaxy
242 for (j=1; j<=8; j++) {
243 int k = Rand()*9.0 + 1.0;
245 game.state.galaxy[i][j] = k;
247 // Locate star bases in galaxy
248 for (i = 1; i <= inbase; i++) {
252 while (game.state.galaxy[ix][iy] >= 10);
254 for (j = i-1; j > 0; j--) {
255 /* Improved placement algorithm to spread out bases */
256 double distq = square(ix-game.state.baseqx[j]) + square(iy-game.state.baseqy[j]);
257 if (distq < 6.0*(6-inbase) && Rand() < 0.75) {
260 proutn("DEBUG: Abandoning base #%d at %d-%d\n", i, ix, iy);
265 else if (distq < 6.0 * (6-inbase)) {
266 proutn("DEBUG: saving base #%d, close to #%d\n", i, j);
272 game.state.baseqx[i] = ix;
273 game.state.baseqy[i] = iy;
274 game.starch[ix][iy] = -1;
275 game.state.galaxy[ix][iy] += 10;
277 // Position ordinary Klingon Battle Cruisers
278 krem = inkling - incom - game.state.nscrem;
279 klumper = 0.25*skill*(9.0-length)+1.0;
280 if (klumper > 9) klumper = 9; // Can't have more than 9 in quadrant
283 int klump = (1.0 - r*r)*klumper;
284 if (klump > krem) klump = krem;
288 while (game.state.galaxy[ix][iy] + klump >= 1000);
289 game.state.galaxy[ix][iy] += klump;
291 // Position Klingon Commander Ships
295 for (i = 1; i <= incom; i++) {
297 do { /* IF debugging, put commanders by bases, always! */
299 if (idebug && klumper <= inbase) {
300 ix = game.state.baseqx[klumper];
301 iy = game.state.baseqy[klumper];
308 while ((game.state.galaxy[ix][iy] < 99 && Rand() < 0.75)||
309 game.state.galaxy[ix][iy]>899);
310 // check for duplicate
311 for (j = 1; j < i; j++)
312 if (game.state.cx[j]==ix && game.state.cy[j]==iy) break;
314 game.state.galaxy[ix][iy] += 100;
315 game.state.cx[i] = ix;
316 game.state.cy[i] = iy;
318 // Locate planets in galaxy
319 for (i = 0; i < inplan; i++) {
321 while (game.state.newstuf[ix][iy] > 0);
322 game.state.newstuf[ix][iy] = 1;
323 game.state.plnets[i].x = ix;
324 game.state.plnets[i].y = iy;
325 game.state.plnets[i].pclass = Rand()*3.0; // Planet class M N or O
326 game.state.plnets[i].crystals = 1.5*Rand(); // 1 in 3 chance of crystals
327 game.state.plnets[i].known = unknown;
330 for (i = 1; i <= game.state.nromrem; i++) {
332 game.state.newstuf[ix][iy] += 10;
334 // Locate the Super Commander
335 if (game.state.nscrem > 0) {
337 while (game.state.galaxy[ix][iy] >= 900);
340 game.state.galaxy[ix][iy] += 100;
342 // Place thing (in tournament game, thingx == -1, don't want one!)
344 iran8(&thingx, &thingy);
355 prout("It is stardate %d. The Federation is being attacked by",
356 (int)game.state.date);
357 prout("a deadly Klingon invasion force. As captain of the United");
358 prout("Starship U.S.S. Enterprise, it is your mission to seek out");
359 prout("and destroy this invasion force of %d battle cruisers.",
361 prout("You have an initial allotment of %d stardates to complete", (int)intime);
362 prout("your mission. As you proceed you may be given more time.");
364 prout("You will have %d supporting starbases.", inbase);
365 proutn("Starbase locations- ");
368 prout("Stardate %d.", (int)game.state.date);
370 prout("%d Klingons.", inkling);
371 prout("An unknown number of Romulans.");
372 if (game.state.nscrem) prout("and one (GULP) Super-Commander.");
373 prout("%d stardates.",(int)intime);
374 proutn("%d starbases in ", inbase);
376 for (i = 1; i <= inbase; i++) {
377 proutn(cramlc(0, game.state.baseqx[i], game.state.baseqy[i]));
381 proutn("The Enterprise is currently in ");
382 proutn(cramlc(quadrant, quadx, quady));
384 proutn(cramlc(sector, sectx, secty));
387 if (game.state.nscrem) prout(" YOU'LL NEED IT.");
390 if (nenhere-iqhere-ithere) shldup=1.0;
391 if (neutz) attack(0); // bad luck to start in a Romulan Neutral Zone
394 void randomize(void) {
395 srand((int)time(NULL));
398 int choose(int needprompt) {
404 if (needprompt) /* Can start with command line options */
405 proutn("Would you like a regular, tournament, or saved game? ");
407 if (strlen(citem)==0) continue; // Try again
408 if (isit("tournament")) {
409 while (scan() == IHEOL) {
410 proutn("Type in tournament number-");
414 continue; // We don't want a blank entry
418 srand((unsigned int)(int)aaitem);
421 if (isit("saved") || isit("frozen")) {
422 if (thaw()) continue;
424 if (*game.passwd==0) continue;
425 if (!alldone) thawed = 1; // No plaque if not finished
430 if (isit("regular")) break;
431 proutn("What is \"");
436 while (length==0 || skill==0) {
437 if (scan() == IHALPHA) {
438 if (isit("short")) length = 1;
439 else if (isit("medium")) length = 2;
440 else if (isit("long")) length = 4;
441 else if (isit("novice")) skill = 1;
442 else if (isit("fair")) skill = 2;
443 else if (isit("good")) skill = 3;
444 else if (isit("expert")) skill = 4;
445 else if (isit("emeritus")) skill = 5;
447 proutn("What is \"");
454 if (length==0) proutn("Would you like a Short, Medium, or Long game? ");
455 else if (skill == 0) proutn("Are you a Novice, Fair, Good, Expert, or Emeritus player? ");
460 if (strcmp(game.passwd, "debug")==0) idebug = 1;
463 // Use parameters to generate initial values of things
464 damfac = 0.5 * skill;
465 game.state.rembase = 3.0*Rand()+2.0;
466 inbase = game.state.rembase;
467 inplan = (PLNETMAX/2) + (PLNETMAX/2+1)*Rand();
468 game.state.nromrem = (2.0+Rand())*skill;
469 game.state.nscrem = (skill > 2? 1 : 0);
470 game.state.remtime = 7.0 * length;
471 intime = game.state.remtime;
472 game.state.remkl = 2.0*intime*((skill+1 - 2*Rand())*skill*0.1+.15);
473 inkling = game.state.remkl;
474 incom = skill + 0.0625*inkling*Rand();
475 game.state.remcom= min(10, incom);
476 incom = game.state.remcom;
477 game.state.remres = (inkling+4*incom)*intime;
478 inresor = game.state.remres;
480 inbase = (game.state.rembase += 1);
485 void dropin(int iquad, int *ix, int *iy) {
487 while (game.quad[*ix][*iy] != IHDOT);
488 game.quad[*ix][*iy] = iquad;
493 if (energy < 1000.0) condit = IHYELLOW;
494 if (game.state.galaxy[quadx][quady] > 99 || game.state.newstuf[quadx][quady] > 9)
496 if (!alive) condit=IHDEAD;
500 void newqad(int shutup) {
501 int quadnum = game.state.galaxy[quadx][quady];
502 int newnum = game.state.newstuf[quadx][quady];
503 int i, j, ix, iy, nplan;
524 // Attempt to escape Super-commander, so tbeam back!
529 for (i=1; i <= 10; i++)
530 for (j=1; j <= 10; j++) game.quad[i][j] = IHDOT;
531 // cope with supernova
535 klhere = quadnum/100;
538 nenhere = klhere + irhere;
541 game.quad[sectx][secty] = ship;
543 if (quadnum >= 100) {
544 // Position ordinary Klingons
545 quadnum -= 100*klhere;
546 for (i = 1; i <= klhere; i++) {
547 dropin(IHK, &ix, &iy);
550 game.kdist[i] = game.kavgd[i] = sqrt(square(sectx-ix) + square(secty-iy));
551 game.kpower[i] = Rand()*150.0 +300.0 +25.0*skill;
553 // If we need a commander, promote a Klingon
554 for (i = 1; i <= game.state.remcom ; i++)
555 if (game.state.cx[i]==quadx && game.state.cy[i]==quady) break;
557 if (i <= game.state.remcom) {
558 game.quad[ix][iy] = IHC;
559 game.kpower[klhere] = 950.0+400.0*Rand()+50.0*skill;
563 // If we need a super-commander, promote a Klingon
564 if (quadx == game.state.isx && quady == game.state.isy) {
565 game.quad[game.kx[1]][game.ky[1]] = IHS;
566 game.kpower[1] = 1175.0 + 400.0*Rand() + 125.0*skill;
567 iscate = game.state.remkl>1;
571 // Put in Romulans if needed
572 for (i = klhere+1; i <= nenhere; i++) {
573 dropin(IHR, &ix, &iy);
576 game.kdist[i] = game.kavgd[i] = sqrt(square(sectx-ix) + square(secty-iy));
577 game.kpower[i] = Rand()*400.0 + 450.0 + 50.0*skill;
579 // If quadrant needs a starbase, put it in
582 dropin(IHB, &basex, &basey);
586 // If quadrant needs a planet, put it in
587 for (i=0; i < inplan; i++)
588 if (game.state.plnets[i].x == quadx && game.state.plnets[i].y == quady) break;
591 dropin(IHP, &plnetx, &plnety);
594 // Check for condition
596 // And finally the stars
597 for (i = 1; i <= quadnum; i++) dropin(IHSTAR, &ix, &iy);
600 if (irhere > 0 && klhere == 0) {
602 if (game.damage[DRADIO] <= 0.0) {
604 prout("LT. Uhura- \"Captain, an urgent message.");
605 prout(" I'll put it on audio.\" CLICK");
607 prout("INTRUDER! YOU HAVE VIOLATED THE ROMULAN NEUTRAL ZONE.");
608 prout("LEAVE AT ONCE, OR YOU WILL BE DESTROYED!");
613 // Put in THING if needed
614 if (thingx == quadx && thingy == quady) {
615 dropin(IHQUEST, &ix, &iy);
616 iran8(&thingx, &thingy);
619 game.kx[nenhere] = ix;
620 game.ky[nenhere] = iy;
621 game.kdist[nenhere] = game.kavgd[nenhere] =
622 sqrt(square(sectx-ix) + square(secty-iy));
623 game.kpower[nenhere] = Rand()*6000.0 +500.0 +250.0*skill;
624 if (game.damage[DSRSENS] == 0.0) {
626 prout("MR. SPOCK- \"Captain, this is most unusual.");
627 prout(" Please examine your short-range scan.\"");
632 // Decide if quadrant needs a Tholian
633 if ((skill < 3 && Rand() <= 0.02) || /* Lighten up if skill is low */
634 (skill == 3 && Rand() <= 0.05) ||
635 (skill > 3 && Rand() <= 0.08)
637 || strcmp(passwd, "tholianx")==0
641 ithx = Rand() > 0.5 ? 10 : 1;
642 ithy = Rand() > 0.5 ? 10 : 1;
643 } while (game.quad[ithx][ithy] != IHDOT);
644 game.quad[ithx][ithy] = IHT;
647 game.kx[nenhere] = ithx;
648 game.ky[nenhere] = ithy;
649 game.kdist[nenhere] = game.kavgd[nenhere] =
650 sqrt(square(sectx-ithx) + square(secty-ithy));
651 game.kpower[nenhere] = Rand()*400.0 +100.0 +25.0*skill;
652 /* Reserve unocupied corners */
653 if (game.quad[1][1]==IHDOT) game.quad[1][1] = 'X';
654 if (game.quad[1][10]==IHDOT) game.quad[1][10] = 'X';
655 if (game.quad[10][1]==IHDOT) game.quad[10][1] = 'X';
656 if (game.quad[10][10]==IHDOT) game.quad[10][10] = 'X';
660 // Put in a few black holes
661 for (i = 1; i <= 3; i++)
662 if (Rand() > 0.5) dropin(IHBLANK, &ix, &iy);
664 // Take out X's in corners if Tholian present
666 if (game.quad[1][1]=='X') game.quad[1][1] = IHDOT;
667 if (game.quad[1][10]=='X') game.quad[1][10] = IHDOT;
668 if (game.quad[10][1]=='X') game.quad[10][1] = IHDOT;
669 if (game.quad[10][10]=='X') game.quad[10][10] = IHDOT;
677 // The author liked bubble sort. So we will use it. :-(
679 if (nenhere-iqhere-ithere < 2) return;
683 for (j = 1; j < nenhere; j++)
684 if (game.kdist[j] > game.kdist[j+1]) {
687 game.kdist[j] = game.kdist[j+1];
690 game.kavgd[j] = game.kavgd[j+1];
693 game.kx[j] = game.kx[j+1];
696 game.ky[j] = game.ky[j+1];
699 game.kpower[j] = game.kpower[j+1];
700 game.kpower[j+1] = t;