5 static long filelength(int fd) {
14 prout("-SUPER- STAR TREK");
17 prout("Latest update-21 Sept 78");
19 #endif /* __HISTORICAL__ */
22 void freeze(bool 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! */
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);
98 ** The ship is abandoned. If your current ship is the Faire
99 ** Queene, or if your shuttlecraft is dead, you're out of
100 ** luck. You need the shuttlecraft in order for the captain
101 ** (that's you!!) to escape.
103 ** Your crew can beam to an inhabited starsystem in the
104 ** quadrant, if there is one and if the transporter is working.
105 ** If there is no inhabited starsystem, or if the transporter
106 ** is out, they are left to die in outer space.
108 ** If there are no starbases left, you are captured by the
109 ** Klingons, who torture you mercilessly. However, if there
110 ** is at least one starbase, you are returned to the
111 ** Federation in a prisoner of war exchange. Of course, this
112 ** can't happen unless you have taken some prisoners.
122 if (game.condit==IHDOCKED) {
123 if (game.ship!=IHE) {
124 prout("You cannot abandon Ye Faerie Queene.");
129 /* Must take shuttle craft to exit */
130 if (game.damage[DSHUTTL]==-1) {
131 prout("Ye Faerie Queene has no shuttle craft.");
134 if (game.damage[DSHUTTL]<0) {
135 prout("Shuttle craft now serving Big Macs.");
138 if (game.damage[DSHUTTL]>0) {
139 prout("Shuttle craft damaged.");
142 if (game.landed==1) {
143 prout("You must be aboard the Enterprise.");
146 if (game.iscraft!=1) {
147 prout("Shuttle craft not currently available.");
150 /* Print abandon ship messages */
152 prouts("***ABANDON SHIP! ABANDON SHIP!");
154 prouts("***ALL HANDS ABANDON SHIP!");
156 prout("Captain and crew escape in shuttle craft.");
157 if (game.state.rembase==0) {
158 /* Oops! no place to go... */
162 q = &game.state.galaxy[game.quadrant.x][game.quadrant.y];
163 /* Dispose of crew */
164 if (!(game.options & OPTION_WORLDS) && !damaged(DTRANSP)) {
165 prout("Remainder of ship's complement beam down");
166 prout("to nearest habitable planet.");
167 } else if (q->planet != NOPLANET && !damaged(DTRANSP)) {
168 prout("Remainder of ship's complement beam down");
169 prout("to %s.", systemname(q->planet));
171 prout("Entire crew of %d left to die in outer space.");
172 game.casual += game.state.crew;
173 game.abandoned += game.state.crew;
176 /* If at least one base left, give 'em the Faerie Queene */
178 game.icrystl = 0; /* crystals are lost */
179 game.nprobes = 0; /* No probes */
180 prout("You are captured by Klingons and released to");
181 prout("the Federation in a prisoner-of-war exchange.");
182 nb = Rand()*game.state.rembase+1;
183 /* Set up quadrant and position FQ adjacient to base */
184 if (!same(game.quadrant, game.state.baseq[nb])) {
185 game.quadrant = game.state.baseq[nb];
186 game.sector.x = game.sector.y = 5;
190 /* position next to base by trial and error */
191 game.quad[game.sector.x][game.sector.y] = IHDOT;
193 game.sector.x = 3.0*Rand() - 1.0 + game.base.x;
194 game.sector.y = 3.0*Rand() - 1.0 + game.base.y;
195 if (VALID_SECTOR(game.sector.x, game.sector.y) &&
196 game.quad[game.sector.x][game.sector.y] == IHDOT) break;
198 if (l < QUADSIZE+1) break; /* found a spot */
199 game.sector.x=QUADSIZE/2;
200 game.sector.y=QUADSIZE/2;
204 /* Get new commission */
205 game.quad[game.sector.x][game.sector.y] = game.ship = IHF;
206 game.state.crew = FULLCREW;
207 prout("Starfleet puts you in command of another ship,");
208 prout("the Faerie Queene, which is antiquated but,");
209 prout("still useable.");
210 if (game.icrystl!=0) prout("The dilithium crystals have been moved.");
212 game.iscraft=0; /* Galileo disappears */
214 game.condit=IHDOCKED;
215 for (l = 0; l < NDEVICES; l++)
216 game.damage[l] = 0.0;
217 game.damage[DSHUTTL] = -1;
218 game.energy = game.inenrg = 3000.0;
219 game.shield = game.inshld = 1250.0;
220 game.torps = game.intorps = 6;
221 game.lsupres=game.inlsr=3.0;
228 void setup(bool needprompt)
230 int i,j, krem, klumper;
232 // Decide how many of everything
233 if (choose(needprompt)) return; // frozen game
234 // Prepare the Enterprise
235 game.alldone = game.gamewon = false;
237 game.state.crew = FULLCREW;
238 game.energy = game.inenrg = 5000.0;
239 game.shield = game.inshld = 2500.0;
244 iran(GALSIZE, &game.quadrant.x, &game.quadrant.y);
245 iran(QUADSIZE, &game.sector.x, &game.sector.y);
246 game.torps = game.intorps = 10;
247 game.nprobes = (int)(3.0*Rand() + 2.0); /* Give them 2-4 of these wonders */
249 game.wfacsq = game.warpfac * game.warpfac;
250 for (i=0; i < NDEVICES; i++)
251 game.damage[i] = 0.0;
252 // Set up assorted game parameters
253 game.battle.x = game.battle.y = 0;
254 game.state.date = game.indate = 100.0*(int)(31.0*Rand()+20.0);
255 game.nkinks = game.nhelp = game.casual = game.abandoned = 0;
256 game.resting = game.imine = false;
257 game.isatb = game.iscate = game.icrystl = game.icraft = game.state.nplankl = 0;
258 game.state.starkl = game.state.basekl = 0;
265 struct quadrant *quad = &game.state.galaxy[i][j];
267 quad->planet = NOPLANET;
272 quad->status = secure;
274 // Initialize times for extraneous events
275 schedule(FSNOVA, expran(0.5 * game.intime));
276 schedule(FTBEAM, expran(1.5 * (game.intime / game.state.remcom)));
277 schedule(FSNAP, 1.0 + Rand()); // Force an early snapshot
278 schedule(FBATTAK, expran(0.3*game.intime));
280 if (game.state.nscrem)
281 schedule(FSCMOVE, 0.2777);
286 if ((game.options & OPTION_WORLDS) && game.skill >= SKILL_GOOD)
287 schedule(FDISTR, expran(1.0 + game.intime));
292 // Starchart is functional but we've never seen it
293 game.lastchart = FOREVER;
294 // Put stars in the galaxy
298 int k = Rand()*9.0 + 1.0;
300 game.state.galaxy[i][j].stars = k;
302 // Locate star bases in galaxy
303 for (i = 1; i <= game.inbase; i++) {
306 do iran(GALSIZE, &ix, &iy);
307 while (game.state.galaxy[ix][iy].starbase);
309 for (j = i-1; j > 0; j--) {
310 /* Improved placement algorithm to spread out bases */
311 double distq = square(ix-game.state.baseq[j].x) + square(iy-game.state.baseq[j].y);
312 if (distq < 6.0*(BASEMAX+1-game.inbase) && Rand() < 0.75) {
315 prout("=== Abandoning base #%d at %d-%d", i, ix, iy);
318 else if (distq < 6.0 * (BASEMAX+1-game.inbase)) {
320 prout("=== Saving base #%d, close to #%d", i, j);
325 game.state.baseq[i].x = ix;
326 game.state.baseq[i].y = iy;
327 game.state.galaxy[ix][iy].starbase = 1;
328 game.state.chart[ix][iy].starbase = 1;
330 // Position ordinary Klingon Battle Cruisers
332 klumper = 0.25*game.skill*(9.0-game.length)+1.0;
333 if (klumper > MAXKLQUAD)
337 int klump = (1.0 - r*r)*klumper;
338 if (klump > krem) klump = krem;
340 do iran(GALSIZE,&ix,&iy);
341 while (game.state.galaxy[ix][iy].supernova ||
342 game.state.galaxy[ix][iy].klingons + klump > 9);
343 game.state.galaxy[ix][iy].klingons += klump;
345 // Position Klingon Commander Ships
349 for (i = 1; i <= game.incom; i++) {
351 do { /* IF debugging, put commanders by bases, always! */
353 if (game.idebug && klumper <= game.inbase) {
354 ix = game.state.baseq[klumper].x;
355 iy = game.state.baseq[klumper].y;
360 iran(GALSIZE, &ix, &iy);
362 while ((!game.state.galaxy[ix][iy].klingons && Rand() < 0.75)||
363 game.state.galaxy[ix][iy].supernova||
364 game.state.galaxy[ix][iy].klingons > 8);
365 // check for duplicate
366 for (j = 1; j < i; j++)
367 if (game.state.kcmdr[j].x==ix && game.state.kcmdr[j].y==iy) break;
369 game.state.galaxy[ix][iy].klingons++;
370 game.state.kcmdr[i].x = ix;
371 game.state.kcmdr[i].y = iy;
373 // Locate planets in galaxy
374 for (i = 0; i < game.inplan; i++) {
375 do iran(GALSIZE, &ix, &iy); while (game.state.galaxy[ix][iy].planet != NOPLANET);
376 game.state.plnets[i].w.x = ix;
377 game.state.plnets[i].w.y = iy;
379 game.state.plnets[i].pclass = M; // All inhabited planets are class M
380 game.state.plnets[i].crystals = 0;
381 game.state.plnets[i].known = known;
382 game.state.plnets[i].inhabited = i;
384 game.state.plnets[i].pclass = Rand()*3.0; // Planet class M N or O
385 game.state.plnets[i].crystals = 1.5*Rand(); // 1 in 3 chance of crystals
386 game.state.plnets[i].known = unknown;
387 game.state.plnets[i].inhabited = UNINHABITED;
389 if ((game.options & OPTION_WORLDS) || i >= NINHAB)
390 game.state.galaxy[ix][iy].planet = i;
393 for (i = 1; i <= game.state.nromrem; i++) {
394 iran(GALSIZE, &ix, &iy);
395 game.state.galaxy[ix][iy].romulans = 1;
397 // Locate the Super Commander
398 if (game.state.nscrem > 0) {
399 do iran(GALSIZE, &ix, &iy);
400 while (game.state.galaxy[ix][iy].supernova || game.state.galaxy[ix][iy].klingons > 8);
401 game.state.kscmdr.x = ix;
402 game.state.kscmdr.y = iy;
403 game.state.galaxy[ix][iy].klingons++;
405 // Place thing (in tournament game, thingx == -1, don't want one!)
407 iran(GALSIZE, &thing.x, &thing.y);
410 thing.x = thing.y = 0;
417 if (game.skill == SKILL_NOVICE) {
418 prout("It is stardate %d. The Federation is being attacked by",
419 (int)game.state.date);
420 prout("a deadly Klingon invasion force. As captain of the United");
421 prout("Starship U.S.S. Enterprise, it is your mission to seek out");
422 prout("and destroy this invasion force of %d battle cruisers.",
424 prout("You have an initial allotment of %d stardates to complete", (int)game.intime);
425 prout("your mission. As you proceed you may be given more time.");
427 prout("You will have %d supporting starbases.", game.inbase);
428 proutn("Starbase locations- ");
431 prout("Stardate %d.", (int)game.state.date);
433 prout("%d Klingons.", INKLINGTOT);
434 prout("An unknown number of Romulans.");
435 if (game.state.nscrem) prout("and one (GULP) Super-Commander.");
436 prout("%d stardates.",(int)game.intime);
437 proutn("%d starbases in ", game.inbase);
439 for (i = 1; i <= game.inbase; i++) {
440 proutn(cramlc(0, game.state.baseq[i]));
444 proutn("The Enterprise is currently in ");
445 proutn(cramlc(quadrant, game.quadrant));
447 proutn(cramlc(sector, game.sector));
450 if (game.state.nscrem) prout(" YOU'LL NEED IT.");
453 if (game.nenhere-iqhere-game.ithere) game.shldup = true;
454 if (game.neutz) attack(0); // bad luck to start in a Romulan Neutral Zone
457 bool choose(bool needprompt)
462 game.skill = SKILL_NONE;
464 if (needprompt) /* Can start with command line options */
465 proutn("Would you like a regular, tournament, or saved game? ");
467 if (strlen(citem)==0) continue; // Try again
468 if (isit("tournament")) {
469 while (scan() == IHEOL) {
470 proutn("Type in tournament number-");
474 continue; // We don't want a blank entry
476 game.tourn = (int)aaitem;
478 srand((unsigned int)(int)aaitem);
481 if (isit("saved") || isit("frozen")) {
482 if (thaw()) continue;
484 if (*game.passwd==0) continue;
485 if (!game.alldone) game.thawed = 1; // No plaque if not finished
490 if (isit("regular")) break;
491 proutn("What is \"");
496 while (game.length==0 || game.skill==SKILL_NONE) {
497 if (scan() == IHALPHA) {
498 if (isit("short")) game.length = 1;
499 else if (isit("medium")) game.length = 2;
500 else if (isit("long")) game.length = 4;
501 else if (isit("novice")) game.skill = SKILL_NOVICE;
502 else if (isit("fair")) game.skill = SKILL_FAIR;
503 else if (isit("good")) game.skill = SKILL_GOOD;
504 else if (isit("expert")) game.skill = SKILL_EXPERT;
505 else if (isit("emeritus")) game.skill = SKILL_EMERITUS;
507 proutn("What is \"");
514 if (game.length==0) proutn("Would you like a Short, Medium, or Long game? ");
515 else if (game.skill == SKILL_NONE) proutn("Are you a Novice, Fair, Good, Expert, or Emeritus player? ");
518 // Choose game options -- added by ESR for SST2K
519 if (scan() != IHALPHA) {
521 proutn("Choose your game style (or just press enter): ");
525 // Approximates the UT FORTRAN version.
526 game.options &=~ (OPTION_THOLIAN | OPTION_PLANETS | OPTION_THINGY | OPTION_PROBE | OPTION_RAMMING | OPTION_MVBADDY | OPTION_BLKHOLE | OPTION_BASE | OPTION_WORLDS);
527 game.options |= OPTION_PLAIN;
529 else if (isit("almy")) {
530 // Approximates Tom Almy's version.
531 game.options &=~ (OPTION_THINGY | OPTION_BLKHOLE | OPTION_BASE | OPTION_WORLDS);
532 game.options |= OPTION_ALMY;
534 else if (isit("fancy"))
536 else if (strlen(citem)) {
537 proutn("What is \"");
542 if (strcmp(game.passwd, "debug")==0) {
544 fputs("=== Debug mode enabled\n", stdout);
547 // Use parameters to generate initial values of things
548 game.damfac = 0.5 * game.skill;
549 game.state.rembase = 2.0 + Rand()*(BASEMAX-2.0);
550 game.inbase = game.state.rembase;
551 if (game.options & OPTION_PLANETS)
552 game.inplan = NINHAB + (MAXUNINHAB/2) + (MAXUNINHAB/2+1)*Rand();
553 game.state.nromrem = game.inrom = (2.0+Rand())*game.skill;
554 game.state.nscrem = game.inscom = (game.skill > SKILL_FAIR ? 1 : 0);
555 game.state.remtime = 7.0 * game.length;
556 game.intime = game.state.remtime;
557 game.state.remkl = game.inkling = 2.0*game.intime*((game.skill+1 - 2*Rand())*game.skill*0.1+.15);
558 game.incom = game.skill + 0.0625*game.inkling*Rand();
559 game.state.remcom = min(10, game.incom);
560 game.incom = game.state.remcom;
561 game.state.remres = (game.inkling+4*game.incom)*game.intime;
562 game.inresor = game.state.remres;
563 if (game.inkling > 50) {
564 game.inbase = (game.state.rembase += 1);
569 void dropin(int iquad, coord *w)
571 do iran(QUADSIZE, &w->x, &w->y);
572 while (game.quad[w->x][w->y] != IHDOT);
573 game.quad[w->x][w->y] = iquad;
578 game.condit = IHGREEN;
579 if (game.energy < 1000.0) game.condit = IHYELLOW;
580 if (game.state.galaxy[game.quadrant.x][game.quadrant.y].klingons || game.state.galaxy[game.quadrant.x][game.quadrant.y].romulans)
582 if (!game.alive) game.condit=IHDEAD;
585 void newkling(int i, coord *pi)
586 /* drop new Klingon into current quadrant */
590 game.kdist[i] = game.kavgd[i] = sqrt(square(game.sector.x-pi->x) + square(game.sector.y-pi->y));
591 game.kpower[i] = Rand()*150.0 +300.0 +25.0*game.skill;
594 void newqad(bool shutup)
598 struct quadrant *here;
602 game.base.x = game.base.y = 0;
605 game.plnet.x = game.plnet.y = 0;
611 game.inorbit = false;
619 // Attempt to escape Super-commander, so tbeam back!
626 game.quad[i][j] = IHDOT;
627 here = &game.state.galaxy[game.quadrant.x][game.quadrant.y];
628 // cope with supernova
631 game.klhere = here->klingons;
632 game.irhere = here->romulans;
633 game.nenhere = game.klhere + game.irhere;
636 game.quad[game.sector.x][game.sector.y] = game.ship;
638 if (here->klingons) {
639 // Position ordinary Klingons
640 for (i = 1; i <= game.klhere; i++)
642 // If we need a commander, promote a Klingon
644 if (game.state.kcmdr[i].x==game.quadrant.x && game.state.kcmdr[i].y==game.quadrant.y) break;
646 if (i <= game.state.remcom) {
647 game.quad[w.x][w.y] = IHC;
648 game.kpower[game.klhere] = 950.0+400.0*Rand()+50.0*game.skill;
652 // If we need a super-commander, promote a Klingon
653 if (game.quadrant.x == game.state.kscmdr.x && game.quadrant.y == game.state.kscmdr.y) {
654 game.quad[game.ks[1].x][game.ks[1].y] = IHS;
655 game.kpower[1] = 1175.0 + 400.0*Rand() + 125.0*game.skill;
656 game.iscate = game.state.remkl>1;
660 // Put in Romulans if needed
661 for (i = game.klhere+1; i <= game.nenhere; i++) {
664 game.kdist[i] = game.kavgd[i] = sqrt(square(game.sector.x-w.x) + square(game.sector.y-w.y));
665 game.kpower[i] = Rand()*400.0 + 450.0 + 50.0*game.skill;
667 // If quadrant needs a starbase, put it in
669 dropin(IHB, &game.base);
671 // If quadrant needs a planet, put it in
672 if (here->planet != NOPLANET) {
673 game.iplnet = here->planet;
674 if (game.state.plnets[here->planet].inhabited == UNINHABITED)
675 dropin(IHP, &game.plnet);
677 dropin(IHW, &game.plnet);
679 // Check for game.condition
681 // And finally the stars
682 for (i = 1; i <= here->stars; i++)
686 if (game.irhere > 0 && game.klhere == 0 && (here->planet == NOPLANET || game.state.plnets[here->planet].inhabited == UNINHABITED)) {
688 if (!damaged(DRADIO)) {
690 prout("LT. Uhura- \"Captain, an urgent message.");
691 prout(" I'll put it on audio.\" CLICK");
693 prout("INTRUDER! YOU HAVE VIOLATED THE ROMULAN NEUTRAL ZONE.");
694 prout("LEAVE AT ONCE, OR YOU WILL BE DESTROYED!");
699 // Put in THING if needed
700 if (same(thing, game.quadrant)) {
702 iran(GALSIZE, &thing.x, &thing.y);
705 game.ks[game.nenhere] = w;
706 game.kdist[game.nenhere] = game.kavgd[game.nenhere] =
707 sqrt(square(game.sector.x-w.x) + square(game.sector.y-w.y));
708 game.kpower[game.nenhere] = Rand()*6000.0 +500.0 +250.0*game.skill;
709 if (!damaged(DSRSENS)) {
711 prout("MR. SPOCK- \"Captain, this is most unusual.");
712 prout(" Please examine your short-range scan.\"");
717 // Decide if quadrant needs a Tholian
718 if (game.options & OPTION_THOLIAN) {
719 if ((game.skill < SKILL_GOOD && Rand() <= 0.02) || /* Lighten up if skill is low */
720 (game.skill == SKILL_GOOD && Rand() <= 0.05) ||
721 (game.skill > SKILL_GOOD && Rand() <= 0.08)
724 game.tholian.x = Rand() > 0.5 ? QUADSIZE : 1;
725 game.tholian.y = Rand() > 0.5 ? QUADSIZE : 1;
726 } while (game.quad[game.tholian.x][game.tholian.y] != IHDOT);
727 game.quad[game.tholian.x][game.tholian.y] = IHT;
730 game.ks[game.nenhere].x = game.tholian.x;
731 game.ks[game.nenhere].y = game.tholian.y;
732 game.kdist[game.nenhere] = game.kavgd[game.nenhere] =
733 sqrt(square(game.sector.x-game.tholian.x) + square(game.sector.y-game.tholian.y));
734 game.kpower[game.nenhere] = Rand()*400.0 +100.0 +25.0*game.skill;
735 /* Reserve unocupied corners */
736 if (game.quad[1][1]==IHDOT) game.quad[1][1] = 'X';
737 if (game.quad[1][QUADSIZE]==IHDOT) game.quad[1][QUADSIZE] = 'X';
738 if (game.quad[QUADSIZE][1]==IHDOT) game.quad[QUADSIZE][1] = 'X';
739 if (game.quad[QUADSIZE][QUADSIZE]==IHDOT) game.quad[QUADSIZE][QUADSIZE] = 'X';
745 // Put in a few black holes
746 for (i = 1; i <= 3; i++)
750 // Take out X's in corners if Tholian present
752 if (game.quad[1][1]=='X') game.quad[1][1] = IHDOT;
753 if (game.quad[1][QUADSIZE]=='X') game.quad[1][QUADSIZE] = IHDOT;
754 if (game.quad[QUADSIZE][1]=='X') game.quad[QUADSIZE][1] = IHDOT;
755 if (game.quad[QUADSIZE][QUADSIZE]=='X') game.quad[QUADSIZE][QUADSIZE] = IHDOT;
765 // The author liked bubble sort. So we will use it. :-(
767 if (game.nenhere-iqhere-game.ithere < 2) return;
771 for (j = 1; j < game.nenhere; j++)
772 if (game.kdist[j] > game.kdist[j+1]) {
775 game.kdist[j] = game.kdist[j+1];
778 game.kavgd[j] = game.kavgd[j+1];
781 game.ks[j].x = game.ks[j+1].x;
784 game.ks[j].y = game.ks[j+1].y;
787 game.kpower[j] = game.kpower[j+1];
788 game.kpower[j+1] = t;
793 void setpassword(void)
795 if (!(game.options & OPTION_CURSES)) {
798 strcpy(game.passwd, citem);
800 if (*game.passwd != 0) break;
801 proutn(_("Please type in a secret password-"));
805 for(i=0;i<3;i++) game.passwd[i]=(char)(97+(int)(Rand()*25));