6 static long filelength(int fd) {
15 prout("-SUPER- STAR TREK");
18 prout("Latest update-21 Sept 78");
20 #endif /* __HISTORICAL__ */
23 void freeze(int boss) {
27 strcpy(citem, "emsave.trk");
30 if ((key = scan()) == IHEOL) {
31 proutn("File name: ");
39 if (strchr(citem, '.') == NULL) {
40 strcat(citem, ".trk");
43 if ((fp = fopen(citem, "wb")) == NULL) {
44 proutn("Can't freeze game as file ");
49 strcpy(game.magic, SSTMAGIC);
50 fwrite(&game, sizeof(game), 1, fp);
54 /* I hope that's enough! */
62 game.passwd[0] = '\0';
63 if ((key = scan()) == IHEOL) {
64 proutn("File name: ");
72 if (strchr(citem, '.') == NULL) {
73 strcat(citem, ".trk");
75 if ((fp = fopen(citem, "rb")) == NULL) {
76 proutn("Can't find game file ");
81 fread(&game, sizeof(game), 1, fp);
82 if (feof(fp) || ftell(fp) != filelength(fileno(fp)) || strcmp(game.magic, SSTMAGIC)) {
83 prout("Game file format is bad, should begin with " SSTMAGIC);
98 if (condit==IHDOCKED) {
100 prout("You cannot abandon Ye Faerie Queene.");
105 /* Must take shuttle craft to exit */
106 if (game.damage[DSHUTTL]==-1) {
107 prout("Ye Faerie Queene has no shuttle craft.");
110 if (game.damage[DSHUTTL]<0) {
111 prout("Shuttle craft now serving Big Mac's.");
114 if (game.damage[DSHUTTL]>0) {
115 prout("Shuttle craft damaged.");
119 prout("You must be aboard the Enterprise.");
123 prout("Shuttle craft not currently available.");
126 /* Print abandon ship messages */
128 prouts("***ABANDON SHIP! ABANDON SHIP!");
130 prouts("***ALL HANDS ABANDON SHIP!");
132 prout("Captain and crew escape in shuttle craft.");
133 prout("Remainder of ship's complement beam down");
134 prout("to nearest habitable planet.");
135 if (game.state.rembase==0) {
136 /* Ops! no place to go... */
140 /* If at least one base left, give 'em the Faerie Queene */
142 icrystl = 0; /* crystals are lost */
143 nprobes = 0; /* No probes */
144 prout("You are captured by Klingons and released to");
145 prout("the Federation in a prisoner-of-war exchange.");
146 nb = Rand()*game.state.rembase+1;
147 /* Set up quadrant and position FQ adjacient to base */
148 if (quadx!=game.state.baseqx[nb] || quady!=game.state.baseqy[nb]) {
149 quadx = game.state.baseqx[nb];
150 quady = game.state.baseqy[nb];
155 /* position next to base by trial and error */
156 game.quad[sectx][secty] = IHDOT;
157 for (l = 1; l <= 10; l++) {
158 sectx = 3.0*Rand() - 1.0 + basex;
159 secty = 3.0*Rand() - 1.0 + basey;
160 if (sectx >= 1 && sectx <= 10 &&
161 secty >= 1 && secty <= 10 &&
162 game.quad[sectx][secty] == IHDOT) break;
164 if (l < 11) break; /* found a spot */
170 /* Get new commission */
171 game.quad[sectx][secty] = ship = IHF;
172 prout("Starfleet puts you in command of another ship,");
173 prout("the Faerie Queene, which is antiquated but,");
174 prout("still useable.");
175 if (icrystl!=0) prout("The dilithium crystals have been moved.");
177 iscraft=0; /* Gallileo disappears */
180 for (l = 1; l <= NDEVICES; l++) game.damage[l] = 0.0;
181 game.damage[DSHUTTL] = -1;
182 energy = inenrg = 3000.0;
183 shield = inshld = 1250.0;
192 void setup(int needprompt) {
193 int i,j, krem, klumper;
198 // Decide how many of everything
199 if (choose(needprompt)) return; // frozen game
200 // Prepare the Enterprise
201 alldone = gamewon = 0;
203 energy = inenrg = 5000.0;
204 shield = inshld = 2500.0;
205 shldchg = shldup = 0;
208 iran8(&quadx, &quady);
209 iran10(§x, §y);
210 torps = intorps = 10;
211 nprobes = (int)(3.0*Rand() + 2.0); /* Give them 2-4 of these wonders */
213 wfacsq = warpfac * warpfac;
214 for (i=0; i <= NDEVICES; i++) game.damage[i] = 0.0;
215 // Set up assorted game parameters
217 game.state.date = indate = 100.0*(int)(31.0*Rand()+20.0);
218 game.state.killk = game.state.killc = nkinks = nhelp = resting = casual = game.state.nromkl = 0;
219 isatb = iscate = imine = icrystl = icraft = game.state.nsckill = game.state.nplankl = 0;
220 game.state.starkl = game.state.basekl = 0;
225 for (i = 1; i <= 8; i++)
226 for (j = 1; j <= 8; j++) game.state.newstuf[i][j] = game.starch[i][j] = 0;
227 // Initialize times for extraneous events
228 game.future[FSNOVA] = game.state.date + expran(0.5 * intime);
229 game.future[FTBEAM] = game.state.date + expran(1.5 * (intime / game.state.remcom));
230 game.future[FSNAP] = game.state.date + 1.0 + Rand(); // Force an early snapshot
231 game.future[FBATTAK] = game.state.date + expran(0.3*intime);
232 game.future[FCDBAS] = 1e30;
233 game.future[FSCMOVE] = game.state.nscrem ? game.state.date+0.2777 : 1e30;
234 game.future[FSCDBAS] = 1e30;
235 game.future[FDSPROB] = 1e30;
236 // Starchart is functional
238 // Put stars in the galaxy
241 for (j=1; j<=8; j++) {
242 int k = Rand()*9.0 + 1.0;
244 game.state.galaxy[i][j] = k;
246 // Locate star bases in galaxy
247 for (i = 1; i <= inbase; i++) {
251 while (game.state.galaxy[ix][iy] >= 10);
253 for (j = i-1; j > 0; j--) {
254 /* Improved placement algorithm to spread out bases */
255 double distq = square(ix-game.state.baseqx[j]) + square(iy-game.state.baseqy[j]);
256 if (distq < 6.0*(6-inbase) && Rand() < 0.75) {
259 proutn("DEBUG: Abandoning base #%d at %d-%d\n", i, ix, iy);
264 else if (distq < 6.0 * (6-inbase)) {
265 proutn("DEBUG: saving base #%d, close to #%d\n", i, j);
271 game.state.baseqx[i] = ix;
272 game.state.baseqy[i] = iy;
273 game.starch[ix][iy] = -1;
274 game.state.galaxy[ix][iy] += 10;
276 // Position ordinary Klingon Battle Cruisers
277 krem = inkling - incom - game.state.nscrem;
278 klumper = 0.25*skill*(9.0-length)+1.0;
279 if (klumper > 9) klumper = 9; // Can't have more than 9 in quadrant
282 int klump = (1.0 - r*r)*klumper;
283 if (klump > krem) klump = krem;
287 while (game.state.galaxy[ix][iy] + klump >= 1000);
288 game.state.galaxy[ix][iy] += klump;
290 // Position Klingon Commander Ships
294 for (i = 1; i <= incom; i++) {
296 do { /* IF debugging, put commanders by bases, always! */
298 if (idebug && klumper <= inbase) {
299 ix = game.state.baseqx[klumper];
300 iy = game.state.baseqy[klumper];
307 while ((game.state.galaxy[ix][iy] < 99 && Rand() < 0.75)||
308 game.state.galaxy[ix][iy]>899);
309 // check for duplicate
310 for (j = 1; j < i; j++)
311 if (game.state.cx[j]==ix && game.state.cy[j]==iy) break;
313 game.state.galaxy[ix][iy] += 100;
314 game.state.cx[i] = ix;
315 game.state.cy[i] = iy;
317 // Locate planets in galaxy
318 for (i = 0; i < inplan; i++) {
320 while (game.state.newstuf[ix][iy] > 0);
321 game.state.newstuf[ix][iy] = 1;
322 game.state.plnets[i].x = ix;
323 game.state.plnets[i].y = iy;
324 game.state.plnets[i].pclass = Rand()*3.0; // Planet class M N or O
325 game.state.plnets[i].crystals = 1.5*Rand(); // 1 in 3 chance of crystals
326 game.state.plnets[i].known = unknown;
329 for (i = 1; i <= game.state.nromrem; i++) {
331 game.state.newstuf[ix][iy] += 10;
333 // Locate the Super Commander
334 if (game.state.nscrem > 0) {
336 while (game.state.galaxy[ix][iy] >= 900);
339 game.state.galaxy[ix][iy] += 100;
341 // Place thing (in tournament game, thingx == -1, don't want one!)
343 iran8(&thingx, &thingy);
354 prout("It is stardate %d. The Federation is being attacked by",
355 (int)game.state.date);
356 prout("a deadly Klingon invasion force. As captain of the United");
357 prout("Starship U.S.S. Enterprise, it is your mission to seek out");
358 prout("and destroy this invasion force of %d battle cruisers.",
360 prout("You have an initial allotment of %d stardates to complete", (int)intime);
361 prout("your mission. As you proceed you may be given more time.");
363 prout("You will have %d supporting starbases.", inbase);
364 proutn("Starbase locations- ");
367 prout("Stardate %d.", (int)game.state.date);
369 prout("%d Klingons.", inkling);
370 prout("An unknown number of Romulans.");
371 if (game.state.nscrem) prout("and one (GULP) Super-Commander.");
372 prout("%d stardates.",(int)intime);
373 proutn("%d starbases in ", inbase);
375 for (i = 1; i <= inbase; i++) {
376 proutn(cramlc(0, game.state.baseqx[i], game.state.baseqy[i]));
380 proutn("The Enterprise is currently in ");
381 proutn(cramlc(quadrant, quadx, quady));
383 proutn(cramlc(sector, sectx, secty));
386 if (game.state.nscrem) prout(" YOU'LL NEED IT.");
389 if (nenhere-iqhere-ithere) shldup=1.0;
390 if (neutz) attack(0); // bad luck to start in a Romulan Neutral Zone
393 void randomize(void) {
394 srand((int)time(NULL));
397 int choose(int needprompt) {
403 if (needprompt) /* Can start with command line options */
404 proutn("Would you like a regular, tournament, or saved game? ");
406 if (strlen(citem)==0) continue; // Try again
407 if (isit("tournament")) {
408 while (scan() == IHEOL) {
409 proutn("Type in tournament number-");
413 continue; // We don't want a blank entry
417 srand((unsigned int)(int)aaitem);
420 if (isit("saved") || isit("frozen")) {
421 if (thaw()) continue;
423 if (*game.passwd==0) continue;
424 if (!alldone) thawed = 1; // No plaque if not finished
429 if (isit("regular")) break;
430 proutn("What is \"");
435 while (length==0 || skill==0) {
436 if (scan() == IHALPHA) {
437 if (isit("short")) length = 1;
438 else if (isit("medium")) length = 2;
439 else if (isit("long")) length = 4;
440 else if (isit("novice")) skill = 1;
441 else if (isit("fair")) skill = 2;
442 else if (isit("good")) skill = 3;
443 else if (isit("expert")) skill = 4;
444 else if (isit("emeritus")) skill = 5;
446 proutn("What is \"");
453 if (length==0) proutn("Would you like a Short, Medium, or Long game? ");
454 else if (skill == 0) proutn("Are you a Novice, Fair, Good, Expert, or Emeritus player? ");
459 if (strcmp(game.passwd, "debug")==0) idebug = 1;
462 // Use parameters to generate initial values of things
463 damfac = 0.5 * skill;
464 game.state.rembase = 3.0*Rand()+2.0;
465 inbase = game.state.rembase;
466 inplan = (PLNETMAX/2) + (PLNETMAX/2+1)*Rand();
467 game.state.nromrem = (2.0+Rand())*skill;
468 game.state.nscrem = (skill > 2? 1 : 0);
469 game.state.remtime = 7.0 * length;
470 intime = game.state.remtime;
471 game.state.remkl = 2.0*intime*((skill+1 - 2*Rand())*skill*0.1+.15);
472 inkling = game.state.remkl;
473 incom = skill + 0.0625*inkling*Rand();
474 game.state.remcom= min(10, incom);
475 incom = game.state.remcom;
476 game.state.remres = (inkling+4*incom)*intime;
477 inresor = game.state.remres;
479 inbase = (game.state.rembase += 1);
484 void dropin(int iquad, int *ix, int *iy) {
486 while (game.quad[*ix][*iy] != IHDOT);
487 game.quad[*ix][*iy] = iquad;
492 if (energy < 1000.0) condit = IHYELLOW;
493 if (game.state.galaxy[quadx][quady] > 99 || game.state.newstuf[quadx][quady] > 9)
495 if (!alive) condit=IHDEAD;
499 void newqad(int shutup) {
500 int quadnum = game.state.galaxy[quadx][quady];
501 int newnum = game.state.newstuf[quadx][quady];
502 int i, j, ix, iy, nplan;
523 // Attempt to escape Super-commander, so tbeam back!
528 for (i=1; i <= 10; i++)
529 for (j=1; j <= 10; j++) game.quad[i][j] = IHDOT;
530 // cope with supernova
534 klhere = quadnum/100;
537 nenhere = klhere + irhere;
540 game.quad[sectx][secty] = ship;
542 if (quadnum >= 100) {
543 // Position ordinary Klingons
544 quadnum -= 100*klhere;
545 for (i = 1; i <= klhere; i++) {
546 dropin(IHK, &ix, &iy);
549 game.kdist[i] = game.kavgd[i] = sqrt(square(sectx-ix) + square(secty-iy));
550 game.kpower[i] = Rand()*150.0 +300.0 +25.0*skill;
552 // If we need a commander, promote a Klingon
553 for (i = 1; i <= game.state.remcom ; i++)
554 if (game.state.cx[i]==quadx && game.state.cy[i]==quady) break;
556 if (i <= game.state.remcom) {
557 game.quad[ix][iy] = IHC;
558 game.kpower[klhere] = 950.0+400.0*Rand()+50.0*skill;
562 // If we need a super-commander, promote a Klingon
563 if (quadx == game.state.isx && quady == game.state.isy) {
564 game.quad[game.kx[1]][game.ky[1]] = IHS;
565 game.kpower[1] = 1175.0 + 400.0*Rand() + 125.0*skill;
566 iscate = game.state.remkl>1;
570 // Put in Romulans if needed
571 for (i = klhere+1; i <= nenhere; i++) {
572 dropin(IHR, &ix, &iy);
575 game.kdist[i] = game.kavgd[i] = sqrt(square(sectx-ix) + square(secty-iy));
576 game.kpower[i] = Rand()*400.0 + 450.0 + 50.0*skill;
578 // If quadrant needs a starbase, put it in
581 dropin(IHB, &basex, &basey);
585 // If quadrant needs a planet, put it in
586 for (i=0; i < inplan; i++)
587 if (game.state.plnets[i].x == quadx && game.state.plnets[i].y == quady) break;
590 dropin(IHP, &plnetx, &plnety);
593 // Check for condition
595 // And finally the stars
596 for (i = 1; i <= quadnum; i++) dropin(IHSTAR, &ix, &iy);
599 if (irhere > 0 && klhere == 0) {
601 if (game.damage[DRADIO] <= 0.0) {
603 prout("LT. Uhura- \"Captain, an urgent message.");
604 prout(" I'll put it on audio.\" CLICK");
606 prout("INTRUDER! YOU HAVE VIOLATED THE ROMULAN NEUTRAL ZONE.");
607 prout("LEAVE AT ONCE, OR YOU WILL BE DESTROYED!");
612 // Put in THING if needed
613 if (thingx == quadx && thingy == quady) {
614 dropin(IHQUEST, &ix, &iy);
615 iran8(&thingx, &thingy);
618 game.kx[nenhere] = ix;
619 game.ky[nenhere] = iy;
620 game.kdist[nenhere] = game.kavgd[nenhere] =
621 sqrt(square(sectx-ix) + square(secty-iy));
622 game.kpower[nenhere] = Rand()*6000.0 +500.0 +250.0*skill;
623 if (game.damage[DSRSENS] == 0.0) {
625 prout("MR. SPOCK- \"Captain, this is most unusual.");
626 prout(" Please examine your short-range scan.\"");
631 // Decide if quadrant needs a Tholian
632 if ((skill < 3 && Rand() <= 0.02) || /* Lighten up if skill is low */
633 (skill == 3 && Rand() <= 0.05) ||
634 (skill > 3 && Rand() <= 0.08)
636 || strcmp(passwd, "tholianx")==0
640 ithx = Rand() > 0.5 ? 10 : 1;
641 ithy = Rand() > 0.5 ? 10 : 1;
642 } while (game.quad[ithx][ithy] != IHDOT);
643 game.quad[ithx][ithy] = IHT;
646 game.kx[nenhere] = ithx;
647 game.ky[nenhere] = ithy;
648 game.kdist[nenhere] = game.kavgd[nenhere] =
649 sqrt(square(sectx-ithx) + square(secty-ithy));
650 game.kpower[nenhere] = Rand()*400.0 +100.0 +25.0*skill;
651 /* Reserve unocupied corners */
652 if (game.quad[1][1]==IHDOT) game.quad[1][1] = 'X';
653 if (game.quad[1][10]==IHDOT) game.quad[1][10] = 'X';
654 if (game.quad[10][1]==IHDOT) game.quad[10][1] = 'X';
655 if (game.quad[10][10]==IHDOT) game.quad[10][10] = 'X';
659 // Put in a few black holes
660 for (i = 1; i <= 3; i++)
661 if (Rand() > 0.5) dropin(IHBLANK, &ix, &iy);
663 // Take out X's in corners if Tholian present
665 if (game.quad[1][1]=='X') game.quad[1][1] = IHDOT;
666 if (game.quad[1][10]=='X') game.quad[1][10] = IHDOT;
667 if (game.quad[10][1]=='X') game.quad[10][1] = IHDOT;
668 if (game.quad[10][10]=='X') game.quad[10][10] = IHDOT;
676 // The author liked bubble sort. So we will use it. :-(
678 if (nenhere-iqhere-ithere < 2) return;
682 for (j = 1; j < nenhere; j++)
683 if (game.kdist[j] > game.kdist[j+1]) {
686 game.kdist[j] = game.kdist[j+1];
689 game.kavgd[j] = game.kavgd[j+1];
692 game.kx[j] = game.kx[j+1];
695 game.ky[j] = game.ky[j+1];
698 game.kpower[j] = game.kpower[j+1];
699 game.kpower[j+1] = t;