5 static long filelength(int fd) {
14 prout("-SUPER- STAR TREK");
17 prout("Latest update-21 Sept 78");
19 #endif /* __HISTORICAL__ */
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! */
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);
100 if (game.condit==IHDOCKED) {
101 if (game.ship!=IHE) {
102 prout("You cannot abandon Ye Faerie Queene.");
107 /* Must take shuttle craft to exit */
108 if (game.damage[DSHUTTL]==-1) {
109 prout("Ye Faerie Queene has no shuttle craft.");
112 if (game.damage[DSHUTTL]<0) {
113 prout("Shuttle craft now serving Big Macs.");
116 if (game.damage[DSHUTTL]>0) {
117 prout("Shuttle craft damaged.");
120 if (game.landed==1) {
121 prout("You must be aboard the Enterprise.");
124 if (game.iscraft!=1) {
125 prout("Shuttle craft not currently available.");
128 /* Print abandon ship messages */
130 prouts("***ABANDON SHIP! ABANDON SHIP!");
132 prouts("***ALL HANDS ABANDON SHIP!");
134 prout("Captain and crew escape in shuttle craft.");
135 prout("Remainder of ship's complement beam down");
136 prout("to nearest habitable planet.");
137 if (game.state.rembase==0) {
138 /* Oops! no place to go... */
142 /* If at least one base left, give 'em the Faerie Queene */
144 game.icrystl = 0; /* crystals are lost */
145 game.nprobes = 0; /* No probes */
146 prout("You are captured by Klingons and released to");
147 prout("the Federation in a prisoner-of-war exchange.");
148 nb = Rand()*game.state.rembase+1;
149 /* Set up quadrant and position FQ adjacient to base */
150 if (game.quadx!=game.state.baseqx[nb] || game.quady!=game.state.baseqy[nb]) {
151 game.quadx = game.state.baseqx[nb];
152 game.quady = game.state.baseqy[nb];
153 game.sectx = game.secty = 5;
157 /* position next to base by trial and error */
158 game.quad[game.sectx][game.secty] = IHDOT;
160 game.sectx = 3.0*Rand() - 1.0 + game.basex;
161 game.secty = 3.0*Rand() - 1.0 + game.basey;
162 if (VALID_SECTOR(game.sectx, game.secty) &&
163 game.quad[game.sectx][game.secty] == IHDOT) break;
165 if (l < QUADSIZE+1) break; /* found a spot */
166 game.sectx=QUADSIZE/2;
167 game.secty=QUADSIZE/2;
171 /* Get new commission */
172 game.quad[game.sectx][game.secty] = game.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 (game.icrystl!=0) prout("The dilithium crystals have been moved.");
178 game.iscraft=0; /* Gallileo disappears */
180 game.condit=IHDOCKED;
181 for (l = 0; l < NDEVICES; l++)
182 game.damage[l] = 0.0;
183 game.damage[DSHUTTL] = -1;
184 game.energy = game.inenrg = 3000.0;
185 game.shield = game.inshld = 1250.0;
186 game.torps = game.intorps = 6;
187 game.lsupres=game.inlsr=3.0;
194 void setup(int needprompt)
196 int i,j, krem, klumper;
201 // Decide how many of everything
202 if (choose(needprompt)) return; // frozen game
203 // Prepare the Enterprise
204 game.alldone = game.gamewon = 0;
206 game.energy = game.inenrg = 5000.0;
207 game.shield = game.inshld = 2500.0;
208 game.shldchg = game.shldup = 0;
211 iran(GALSIZE, &game.quadx, &game.quady);
212 iran(QUADSIZE, &game.sectx, &game.secty);
213 game.torps = game.intorps = 10;
214 game.nprobes = (int)(3.0*Rand() + 2.0); /* Give them 2-4 of these wonders */
216 game.wfacsq = game.warpfac * game.warpfac;
217 for (i=0; i < NDEVICES; i++)
218 game.damage[i] = 0.0;
219 // Set up assorted game parameters
220 game.batx = game.baty = 0;
221 game.state.date = game.indate = 100.0*(int)(31.0*Rand()+20.0);
222 game.nkinks = game.nhelp = game.resting = game.casual = 0;
223 game.isatb = game.iscate = game.imine = game.icrystl = game.icraft = game.state.nplankl = 0;
224 game.state.starkl = game.state.basekl = 0;
231 game.state.galaxy[i][j].charted = 0;
232 game.state.galaxy[i][j].planet = NULL;
233 game.state.galaxy[i][j].romulans = 0;
234 game.state.galaxy[i][j].klingons = 0;
235 game.state.galaxy[i][j].starbase = 0;
236 game.state.galaxy[i][j].supernova = 0;
238 // Initialize times for extraneous events
239 game.future[FSNOVA] = game.state.date + expran(0.5 * game.intime);
240 game.future[FTBEAM] = game.state.date + expran(1.5 * (game.intime / game.state.remcom));
241 game.future[FSNAP] = game.state.date + 1.0 + Rand(); // Force an early snapshot
242 game.future[FBATTAK] = game.state.date + expran(0.3*game.intime);
243 game.future[FCDBAS] = FOREVER;
244 game.future[FSCMOVE] = game.state.nscrem ? game.state.date+0.2777 : FOREVER;
245 game.future[FSCDBAS] = FOREVER;
246 game.future[FDSPROB] = FOREVER;
247 // Starchart is functional but we've never seen it
248 game.lastchart = FOREVER;
249 // Put stars in the galaxy
253 int k = Rand()*9.0 + 1.0;
255 game.state.galaxy[i][j].stars = k;
257 // Locate star bases in galaxy
258 for (i = 1; i <= game.inbase; i++) {
261 do iran(GALSIZE, &ix, &iy);
262 while (game.state.galaxy[ix][iy].starbase);
264 for (j = i-1; j > 0; j--) {
265 /* Improved placement algorithm to spread out bases */
266 double distq = square(ix-game.state.baseqx[j]) + square(iy-game.state.baseqy[j]);
267 if (distq < 6.0*(BASEMAX+1-game.inbase) && Rand() < 0.75) {
270 prout("DEBUG: Abandoning base #%d at %d-%d", i, ix, iy);
275 else if (distq < 6.0 * (BASEMAX+1-game.inbase)) {
276 prout("DEBUG: saving base #%d, close to #%d", i, j);
282 game.state.baseqx[i] = ix;
283 game.state.baseqy[i] = iy;
284 game.state.galaxy[ix][iy].starbase = 1;
285 game.state.chart[ix][iy].starbase = 1;
287 // Position ordinary Klingon Battle Cruisers
289 klumper = 0.25*game.skill*(9.0-game.length)+1.0;
290 if (klumper > 9) klumper = 9; // Can't have more than 9 in quadrant
293 int klump = (1.0 - r*r)*klumper;
294 if (klump > krem) klump = krem;
296 do iran(GALSIZE,&ix,&iy);
297 while (game.state.galaxy[ix][iy].supernova ||
298 game.state.galaxy[ix][iy].klingons + klump > 9);
299 game.state.galaxy[ix][iy].klingons += klump;
301 // Position Klingon Commander Ships
305 for (i = 1; i <= game.incom; i++) {
307 do { /* IF debugging, put commanders by bases, always! */
309 if (game.idebug && klumper <= game.inbase) {
310 ix = game.state.baseqx[klumper];
311 iy = game.state.baseqy[klumper];
316 iran(GALSIZE, &ix, &iy);
318 while ((!game.state.galaxy[ix][iy].klingons && Rand() < 0.75)||
319 game.state.galaxy[ix][iy].supernova||
320 game.state.galaxy[ix][iy].klingons > 8);
321 // check for duplicate
322 for (j = 1; j < i; j++)
323 if (game.state.cx[j]==ix && game.state.cy[j]==iy) break;
325 game.state.galaxy[ix][iy].klingons++;
326 game.state.cx[i] = ix;
327 game.state.cy[i] = iy;
329 // Locate planets in galaxy
330 for (i = 0; i < game.inplan; i++) {
331 do iran(GALSIZE, &ix, &iy); while (game.state.galaxy[ix][iy].planet);
332 game.state.plnets[i].x = ix;
333 game.state.plnets[i].y = iy;
335 game.state.plnets[i].pclass = M; // All inhabited planets are class M
336 game.state.plnets[i].crystals = 0;
337 game.state.plnets[i].known = known;
338 game.state.plnets[i].inhabited = i;
340 game.state.plnets[i].pclass = Rand()*3.0; // Planet class M N or O
341 game.state.plnets[i].crystals = 1.5*Rand(); // 1 in 3 chance of crystals
342 game.state.plnets[i].known = unknown;
343 game.state.plnets[i].inhabited = UNINHABITED;
345 if ((game.options & OPTION_WORLDS) || i >= NINHAB)
346 game.state.galaxy[ix][iy].planet = game.state.plnets + i;
349 for (i = 1; i <= game.state.nromrem; i++) {
350 iran(GALSIZE, &ix, &iy);
351 game.state.galaxy[ix][iy].romulans = 1;
353 // Locate the Super Commander
354 if (game.state.nscrem > 0) {
355 do iran(GALSIZE, &ix, &iy);
356 while (game.state.galaxy[ix][iy].supernova || game.state.galaxy[ix][iy].klingons > 8);
359 game.state.galaxy[ix][iy].klingons++;
361 // Place thing (in tournament game, thingx == -1, don't want one!)
363 iran(GALSIZE, &thingx, &thingy);
373 if (game.skill == SKILL_NOVICE) {
374 prout("It is stardate %d. The Federation is being attacked by",
375 (int)game.state.date);
376 prout("a deadly Klingon invasion force. As captain of the United");
377 prout("Starship U.S.S. Enterprise, it is your mission to seek out");
378 prout("and destroy this invasion force of %d battle cruisers.",
380 prout("You have an initial allotment of %d stardates to complete", (int)game.intime);
381 prout("your mission. As you proceed you may be given more time.");
383 prout("You will have %d supporting starbases.", game.inbase);
384 proutn("Starbase locations- ");
387 prout("Stardate %d.", (int)game.state.date);
389 prout("%d Klingons.", INKLINGTOT);
390 prout("An unknown number of Romulans.");
391 if (game.state.nscrem) prout("and one (GULP) Super-Commander.");
392 prout("%d stardates.",(int)game.intime);
393 proutn("%d starbases in ", game.inbase);
395 for (i = 1; i <= game.inbase; i++) {
396 proutn(cramlc(0, game.state.baseqx[i], game.state.baseqy[i]));
400 proutn("The Enterprise is currently in ");
401 proutn(cramlc(quadrant, game.quadx, game.quady));
403 proutn(cramlc(sector, game.sectx, game.secty));
406 if (game.state.nscrem) prout(" YOU'LL NEED IT.");
409 if (game.nenhere-iqhere-game.ithere) game.shldup=1.0;
410 if (game.neutz) attack(0); // bad luck to start in a Romulan Neutral Zone
413 int choose(int needprompt)
418 game.skill = SKILL_NONE;
420 if (needprompt) /* Can start with command line options */
421 proutn("Would you like a regular, tournament, or saved game? ");
423 if (strlen(citem)==0) continue; // Try again
424 if (isit("tournament")) {
425 while (scan() == IHEOL) {
426 proutn("Type in tournament number-");
430 continue; // We don't want a blank entry
432 game.tourn = (int)aaitem;
434 srand((unsigned int)(int)aaitem);
437 if (isit("saved") || isit("frozen")) {
438 if (thaw()) continue;
440 if (*game.passwd==0) continue;
441 if (!game.alldone) game.thawed = 1; // No plaque if not finished
446 if (isit("regular")) break;
447 proutn("What is \"");
452 while (game.length==0 || game.skill==SKILL_NONE) {
453 if (scan() == IHALPHA) {
454 if (isit("short")) game.length = 1;
455 else if (isit("medium")) game.length = 2;
456 else if (isit("long")) game.length = 4;
457 else if (isit("novice")) game.skill = SKILL_NOVICE;
458 else if (isit("fair")) game.skill = SKILL_FAIR;
459 else if (isit("good")) game.skill = SKILL_GOOD;
460 else if (isit("expert")) game.skill = SKILL_EXPERT;
461 else if (isit("emeritus")) game.skill = SKILL_EMERITUS;
463 proutn("What is \"");
470 if (game.length==0) proutn("Would you like a Short, Medium, or Long game? ");
471 else if (game.skill == SKILL_NONE) proutn("Are you a Novice, Fair, Good, Expert, or Emeritus player? ");
474 // Choose game options -- added by ESR for SST2K
475 if (scan() != IHALPHA) {
477 proutn("Choose your game options: ");
481 // Approximates the UT FORTRAN version.
482 game.options &=~ (OPTION_THOLIAN | OPTION_PLANETS | OPTION_THINGY | OPTION_PROBE | OPTION_RAMMING | OPTION_MVBADDY | OPTION_BLKHOLE | OPTION_BASE);
483 game.options |= OPTION_PLAIN;
485 else if (isit("almy")) {
486 // Approximates Tom Almy's version.
487 game.options &=~ (OPTION_THINGY | OPTION_BLKHOLE | OPTION_BASE);
488 game.options |= OPTION_ALMY;
490 else if (isit("fancy"))
492 else if (strlen(citem)) {
493 proutn("What is \"");
499 if (strcmp(game.passwd, "debug")==0) game.idebug = 1;
502 // Use parameters to generate initial values of things
503 game.damfac = 0.5 * game.skill;
504 game.state.rembase = 2.0 + Rand()*(BASEMAX-2.0);
505 game.inbase = game.state.rembase;
506 if (game.options & OPTION_PLANETS)
507 game.inplan = NINHAB + (MAXUNINHAB/2) + (MAXUNINHAB/2+1)*Rand();
508 game.state.nromrem = game.inrom = (2.0+Rand())*game.skill;
509 game.state.nscrem = game.inscom = (game.skill > SKILL_FAIR ? 1 : 0);
510 game.state.remtime = 7.0 * game.length;
511 game.intime = game.state.remtime;
512 game.state.remkl = game.inkling = 2.0*game.intime*((game.skill+1 - 2*Rand())*game.skill*0.1+.15);
513 game.incom = game.skill + 0.0625*game.inkling*Rand();
514 game.state.remcom = min(10, game.incom);
515 game.incom = game.state.remcom;
516 game.state.remres = (game.inkling+4*game.incom)*game.intime;
517 game.inresor = game.state.remres;
518 if (game.inkling > 50) {
519 game.inbase = (game.state.rembase += 1);
524 void dropin(int iquad, int *ix, int *iy)
526 do iran(QUADSIZE, ix, iy);
527 while (game.quad[*ix][*iy] != IHDOT);
528 game.quad[*ix][*iy] = iquad;
533 game.condit = IHGREEN;
534 if (game.energy < 1000.0) game.condit = IHYELLOW;
535 if (game.state.galaxy[game.quadx][game.quady].klingons || game.state.galaxy[game.quadx][game.quady].romulans)
537 if (!game.alive) game.condit=IHDEAD;
541 void newqad(int shutup)
548 game.basex = game.basey = 0;
551 game.plnetx = game.plnety = 0;
565 // Attempt to escape Super-commander, so tbeam back!
572 game.quad[i][j] = IHDOT;
573 // cope with supernova
574 if (game.state.galaxy[game.quadx][game.quady].supernova)
576 game.klhere = game.state.galaxy[game.quadx][game.quady].klingons;
577 game.irhere = game.state.galaxy[game.quadx][game.quady].romulans;
578 game.nenhere = game.klhere + game.irhere;
581 game.quad[game.sectx][game.secty] = game.ship;
583 if (game.state.galaxy[game.quadx][game.quady].klingons) {
584 // Position ordinary Klingons
585 for (i = 1; i <= game.klhere; i++) {
586 dropin(IHK, &ix, &iy);
589 game.kdist[i] = game.kavgd[i] = sqrt(square(game.sectx-ix) + square(game.secty-iy));
590 game.kpower[i] = Rand()*150.0 +300.0 +25.0*game.skill;
592 // If we need a commander, promote a Klingon
594 if (game.state.cx[i]==game.quadx && game.state.cy[i]==game.quady) break;
596 if (i <= game.state.remcom) {
597 game.quad[ix][iy] = IHC;
598 game.kpower[game.klhere] = 950.0+400.0*Rand()+50.0*game.skill;
602 // If we need a super-commander, promote a Klingon
603 if (game.quadx == game.state.isx && game.quady == game.state.isy) {
604 game.quad[game.kx[1]][game.ky[1]] = IHS;
605 game.kpower[1] = 1175.0 + 400.0*Rand() + 125.0*game.skill;
606 game.iscate = game.state.remkl>1;
610 // Put in Romulans if needed
611 for (i = game.klhere+1; i <= game.nenhere; i++) {
612 dropin(IHR, &ix, &iy);
615 game.kdist[i] = game.kavgd[i] = sqrt(square(game.sectx-ix) + square(game.secty-iy));
616 game.kpower[i] = Rand()*400.0 + 450.0 + 50.0*game.skill;
618 // If quadrant needs a starbase, put it in
619 if (game.state.galaxy[game.quadx][game.quady].starbase)
620 dropin(IHB, &game.basex, &game.basey);
622 // If quadrant needs a planet, put it in
623 planhere = game.state.galaxy[game.quadx][game.quady].planet;
625 game.iplnet = planhere - game.state.plnets;
626 dropin(IHP, &game.plnetx, &game.plnety);
628 // Check for game.condition
630 // And finally the stars
631 for (i = 1; i <= game.state.galaxy[game.quadx][game.quady].stars; i++)
632 dropin(IHSTAR, &ix, &iy);
635 if (game.irhere > 0 && game.klhere == 0 && (!planhere || planhere->inhabited == UNINHABITED)) {
637 if (game.damage[DRADIO] <= 0.0) {
639 prout("LT. Uhura- \"Captain, an urgent message.");
640 prout(" I'll put it on audio.\" CLICK");
642 prout("INTRUDER! YOU HAVE VIOLATED THE ROMULAN NEUTRAL ZONE.");
643 prout("LEAVE AT ONCE, OR YOU WILL BE DESTROYED!");
648 // Put in THING if needed
649 if (thingx == game.quadx && thingy == game.quady) {
650 dropin(IHQUEST, &ix, &iy);
651 iran(GALSIZE, &thingx, &thingy);
654 game.kx[game.nenhere] = ix;
655 game.ky[game.nenhere] = iy;
656 game.kdist[game.nenhere] = game.kavgd[game.nenhere] =
657 sqrt(square(game.sectx-ix) + square(game.secty-iy));
658 game.kpower[game.nenhere] = Rand()*6000.0 +500.0 +250.0*game.skill;
659 if (game.damage[DSRSENS] == 0.0) {
661 prout("MR. SPOCK- \"Captain, this is most unusual.");
662 prout(" Please examine your short-range scan.\"");
667 // Decide if quadrant needs a Tholian
668 if (game.options & OPTION_THOLIAN) {
669 if ((game.skill < SKILL_GOOD && Rand() <= 0.02) || /* Lighten up if skill is low */
670 (game.skill == SKILL_GOOD && Rand() <= 0.05) ||
671 (game.skill > SKILL_GOOD && Rand() <= 0.08)
673 || strcmp(game.passwd, "tholianx")==0
677 game.ithx = Rand() > 0.5 ? QUADSIZE : 1;
678 game.ithy = Rand() > 0.5 ? QUADSIZE : 1;
679 } while (game.quad[game.ithx][game.ithy] != IHDOT);
680 game.quad[game.ithx][game.ithy] = IHT;
683 game.kx[game.nenhere] = game.ithx;
684 game.ky[game.nenhere] = game.ithy;
685 game.kdist[game.nenhere] = game.kavgd[game.nenhere] =
686 sqrt(square(game.sectx-game.ithx) + square(game.secty-game.ithy));
687 game.kpower[game.nenhere] = Rand()*400.0 +100.0 +25.0*game.skill;
688 /* Reserve unocupied corners */
689 if (game.quad[1][1]==IHDOT) game.quad[1][1] = 'X';
690 if (game.quad[1][QUADSIZE]==IHDOT) game.quad[1][QUADSIZE] = 'X';
691 if (game.quad[QUADSIZE][1]==IHDOT) game.quad[QUADSIZE][1] = 'X';
692 if (game.quad[QUADSIZE][QUADSIZE]==IHDOT) game.quad[QUADSIZE][QUADSIZE] = 'X';
698 // Put in a few black holes
699 for (i = 1; i <= 3; i++)
701 dropin(IHBLANK, &ix, &iy);
703 // Take out X's in corners if Tholian present
705 if (game.quad[1][1]=='X') game.quad[1][1] = IHDOT;
706 if (game.quad[1][QUADSIZE]=='X') game.quad[1][QUADSIZE] = IHDOT;
707 if (game.quad[QUADSIZE][1]=='X') game.quad[QUADSIZE][1] = IHDOT;
708 if (game.quad[QUADSIZE][QUADSIZE]=='X') game.quad[QUADSIZE][QUADSIZE] = IHDOT;
717 // The author liked bubble sort. So we will use it. :-(
719 if (game.nenhere-iqhere-game.ithere < 2) return;
723 for (j = 1; j < game.nenhere; j++)
724 if (game.kdist[j] > game.kdist[j+1]) {
727 game.kdist[j] = game.kdist[j+1];
730 game.kavgd[j] = game.kavgd[j+1];
733 game.kx[j] = game.kx[j+1];
736 game.ky[j] = game.ky[j+1];
739 game.kpower[j] = game.kpower[j+1];
740 game.kpower[j+1] = t;