5 static long filelength(int fd) {
12 /* issue a historically correct banner */
15 prout("-SUPER- STAR TREK");
18 prout("Latest update-21 Sept 78");
20 #endif /* __HISTORICAL__ */
23 void freeze(bool boss)
29 strcpy(citem, "emsave.trk");
32 if ((key = scan()) == IHEOL) {
33 proutn(_("File name: "));
41 if (strchr(citem, '.') == NULL) {
42 strcat(citem, ".trk");
45 if ((fp = fopen(citem, "wb")) == NULL) {
46 proutn(_("Can't freeze game as file "));
51 strcpy(game.magic, SSTMAGIC);
52 fwrite(&game, sizeof(game), 1, fp);
59 /* retrieve saved game */
64 game.passwd[0] = '\0';
65 if ((key = scan()) == IHEOL) {
66 proutn("File name: ");
74 if (strchr(citem, '.') == NULL) {
75 strcat(citem, ".trk");
77 if ((fp = fopen(citem, "rb")) == NULL) {
78 proutn("Can't find game file ");
83 fread(&game, sizeof(game), 1, fp);
84 if (feof(fp) || ftell(fp) != filelength(fileno(fp)) || strcmp(game.magic, SSTMAGIC)) {
85 prout("Game file format is bad, should begin with " SSTMAGIC);
99 ** The ship is abandoned. If your current ship is the Faire
100 ** Queene, or if your shuttlecraft is dead, you're out of
101 ** luck. You need the shuttlecraft in order for the captain
102 ** (that's you!!) to escape.
104 ** Your crew can beam to an inhabited starsystem in the
105 ** quadrant, if there is one and if the transporter is working.
106 ** If there is no inhabited starsystem, or if the transporter
107 ** is out, they are left to die in outer space.
109 ** If there are no starbases left, you are captured by the
110 ** Klingons, who torture you mercilessly. However, if there
111 ** is at least one starbase, you are returned to the
112 ** Federation in a prisoner of war exchange. Of course, this
113 ** can't happen unless you have taken some prisoners.
124 if (game.condit==IHDOCKED) {
125 if (game.ship!=IHE) {
126 prout("You cannot abandon Ye Faerie Queene.");
131 /* Must take shuttle craft to exit */
132 if (game.damage[DSHUTTL]==-1) {
133 prout("Ye Faerie Queene has no shuttle craft.");
136 if (game.damage[DSHUTTL]<0) {
137 prout("Shuttle craft now serving Big Macs.");
140 if (game.damage[DSHUTTL]>0) {
141 prout("Shuttle craft damaged.");
144 if (game.landed==1) {
145 prout("You must be aboard the Enterprise.");
148 if (game.iscraft!=1) {
149 prout("Shuttle craft not currently available.");
152 /* Print abandon ship messages */
154 prouts("***ABANDON SHIP! ABANDON SHIP!");
156 prouts("***ALL HANDS ABANDON SHIP!");
158 prout("Captain and crew escape in shuttle craft.");
159 if (game.state.rembase==0) {
160 /* Oops! no place to go... */
164 q = &game.state.galaxy[game.quadrant.x][game.quadrant.y];
165 /* Dispose of crew */
166 if (!(game.options & OPTION_WORLDS) && !damaged(DTRANSP)) {
167 prout("Remainder of ship's complement beam down");
168 prout("to nearest habitable planet.");
169 } else if (q->planet != NOPLANET && !damaged(DTRANSP)) {
170 prout("Remainder of ship's complement beam down");
171 prout("to %s.", systemname(q->planet));
173 prout("Entire crew of %d left to die in outer space.");
174 game.casual += game.state.crew;
175 game.abandoned += game.state.crew;
178 /* If at least one base left, give 'em the Faerie Queene */
180 game.icrystl = false; /* crystals are lost */
181 game.nprobes = 0; /* No probes */
182 prout("You are captured by Klingons and released to");
183 prout("the Federation in a prisoner-of-war exchange.");
184 nb = Rand()*game.state.rembase+1;
185 /* Set up quadrant and position FQ adjacient to base */
186 if (!same(game.quadrant, game.state.baseq[nb])) {
187 game.quadrant = game.state.baseq[nb];
188 game.sector.x = game.sector.y = 5;
192 /* position next to base by trial and error */
193 game.quad[game.sector.x][game.sector.y] = IHDOT;
195 game.sector.x = 3.0*Rand() - 1.0 + game.base.x;
196 game.sector.y = 3.0*Rand() - 1.0 + game.base.y;
197 if (VALID_SECTOR(game.sector.x, game.sector.y) &&
198 game.quad[game.sector.x][game.sector.y] == IHDOT) break;
200 if (l < QUADSIZE+1) break; /* found a spot */
201 game.sector.x=QUADSIZE/2;
202 game.sector.y=QUADSIZE/2;
206 /* Get new commission */
207 game.quad[game.sector.x][game.sector.y] = game.ship = IHF;
208 game.state.crew = FULLCREW;
209 prout("Starfleet puts you in command of another ship,");
210 prout("the Faerie Queene, which is antiquated but,");
211 prout("still useable.");
212 if (game.icrystl) prout("The dilithium crystals have been moved.");
214 game.iscraft=0; /* Galileo disappears */
216 game.condit=IHDOCKED;
217 for (l = 0; l < NDEVICES; l++)
218 game.damage[l] = 0.0;
219 game.damage[DSHUTTL] = -1;
220 game.energy = game.inenrg = 3000.0;
221 game.shield = game.inshld = 1250.0;
222 game.torps = game.intorps = 6;
223 game.lsupres=game.inlsr=3.0;
230 void setup(bool needprompt)
231 /* prepare to play, set up cosmos */
233 int i,j, krem, klumper;
235 // Decide how many of everything
236 if (choose(needprompt)) return; // frozen game
237 // Prepare the Enterprise
238 game.alldone = game.gamewon = false;
240 game.state.crew = FULLCREW;
241 game.energy = game.inenrg = 5000.0;
242 game.shield = game.inshld = 2500.0;
247 game.quadrant = iran(GALSIZE);
248 game.sector = iran(QUADSIZE);
249 game.torps = game.intorps = 10;
250 game.nprobes = (int)(3.0*Rand() + 2.0); /* Give them 2-4 of these wonders */
252 game.wfacsq = game.warpfac * game.warpfac;
253 for (i=0; i < NDEVICES; i++)
254 game.damage[i] = 0.0;
255 // Set up assorted game parameters
256 game.battle.x = game.battle.y = 0;
257 game.state.date = game.indate = 100.0*(int)(31.0*Rand()+20.0);
258 game.nkinks = game.nhelp = game.casual = game.abandoned = 0;
259 game.resting = game.imine = game.icrystl = game.icraft = false;
260 game.isatb = game.iscate = game.state.nplankl = 0;
261 game.state.starkl = game.state.basekl = 0;
268 struct quadrant *quad = &game.state.galaxy[i][j];
270 quad->planet = NOPLANET;
275 quad->status = secure;
277 // Initialize times for extraneous events
278 schedule(FSNOVA, expran(0.5 * game.intime));
279 schedule(FTBEAM, expran(1.5 * (game.intime / game.state.remcom)));
280 schedule(FSNAP, 1.0 + Rand()); // Force an early snapshot
281 schedule(FBATTAK, expran(0.3*game.intime));
283 if (game.state.nscrem)
284 schedule(FSCMOVE, 0.2777);
289 if ((game.options & OPTION_WORLDS) && game.skill >= SKILL_GOOD)
290 schedule(FDISTR, expran(1.0 + game.intime));
295 // Starchart is functional but we've never seen it
296 game.lastchart = FOREVER;
297 // Put stars in the galaxy
301 int k = Rand()*9.0 + 1.0;
303 game.state.galaxy[i][j].stars = k;
305 // Locate star bases in galaxy
306 for (i = 1; i <= game.inbase; i++) {
309 do w = iran(GALSIZE);
310 while (game.state.galaxy[w.x][w.y].starbase);
312 for (j = i-1; j > 0; j--) {
313 /* Improved placement algorithm to spread out bases */
314 double distq = square(w.x-game.state.baseq[j].x) + square(w.y-game.state.baseq[j].y);
315 if (distq < 6.0*(BASEMAX+1-game.inbase) && Rand() < 0.75) {
318 prout("=== Abandoning base #%d at %d-%d", i, w.x, w.y);
321 else if (distq < 6.0 * (BASEMAX+1-game.inbase)) {
323 prout("=== Saving base #%d, close to #%d", i, j);
328 game.state.baseq[i] = w;
329 game.state.galaxy[w.x][w.y].starbase = 1;
330 game.state.chart[w.x][w.y].starbase = 1;
332 // Position ordinary Klingon Battle Cruisers
334 klumper = 0.25*game.skill*(9.0-game.length)+1.0;
335 if (klumper > MAXKLQUAD)
339 int klump = (1.0 - r*r)*klumper;
340 if (klump > krem) klump = krem;
342 do w = iran(GALSIZE);
343 while (game.state.galaxy[w.x][w.y].supernova ||
344 game.state.galaxy[w.x][w.y].klingons + klump > 9);
345 game.state.galaxy[w.x][w.y].klingons += klump;
347 // Position Klingon Commander Ships
351 for (i = 1; i <= game.incom; i++) {
353 do { /* IF debugging, put commanders by bases, always! */
355 if (game.idebug && klumper <= game.inbase) {
356 w = game.state.baseq[klumper];
363 while ((!game.state.galaxy[w.x][w.y].klingons && Rand() < 0.75)||
364 game.state.galaxy[w.x][w.y].supernova||
365 game.state.galaxy[w.x][w.y].klingons > 8);
366 // check for duplicate
367 for (j = 1; j < i; j++)
368 if (game.state.kcmdr[j].x==w.x && game.state.kcmdr[j].y==w.y) break;
370 game.state.galaxy[w.x][w.y].klingons++;
371 game.state.kcmdr[i] = w;
373 // Locate planets in galaxy
374 for (i = 0; i < game.inplan; i++) {
375 do w = iran(GALSIZE);
376 while (game.state.galaxy[w.x][w.y].planet != NOPLANET);
377 game.state.plnets[i].w = w;
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[w.x][w.y].planet = i;
393 for (i = 1; i <= game.state.nromrem; i++) {
395 game.state.galaxy[w.x][w.y].romulans = 1;
397 // Locate the Super Commander
398 if (game.state.nscrem > 0) {
399 do w = iran(GALSIZE);
400 while (game.state.galaxy[w.x][w.y].supernova || game.state.galaxy[w.x][w.y].klingons > 8);
401 game.state.kscmdr = w;
402 game.state.galaxy[w.x][w.y].klingons++;
404 // Place thing (in tournament game, thingx == -1, don't want one!)
406 thing = iran(GALSIZE);
409 thing.x = thing.y = 0;
416 if (game.skill == SKILL_NOVICE) {
417 prout("It is stardate %d. The Federation is being attacked by",
418 (int)game.state.date);
419 prout("a deadly Klingon invasion force. As captain of the United");
420 prout("Starship U.S.S. Enterprise, it is your mission to seek out");
421 prout("and destroy this invasion force of %d battle cruisers.",
423 prout("You have an initial allotment of %d stardates to complete", (int)game.intime);
424 prout("your mission. As you proceed you may be given more time.");
426 prout("You will have %d supporting starbases.", game.inbase);
427 proutn("Starbase locations- ");
430 prout("Stardate %d.", (int)game.state.date);
432 prout("%d Klingons.", INKLINGTOT);
433 prout("An unknown number of Romulans.");
434 if (game.state.nscrem) prout("and one (GULP) Super-Commander.");
435 prout("%d stardates.",(int)game.intime);
436 proutn("%d starbases in ", game.inbase);
438 for (i = 1; i <= game.inbase; i++) {
439 proutn(cramlc(0, game.state.baseq[i]));
443 proutn("The Enterprise is currently in ");
444 proutn(cramlc(quadrant, game.quadrant));
446 proutn(cramlc(sector, game.sector));
449 if (game.state.nscrem) prout(" YOU'LL NEED IT.");
452 if (game.nenhere-iqhere-game.ithere) game.shldup = true;
453 if (game.neutz) attack(0); // bad luck to start in a Romulan Neutral Zone
456 bool choose(bool needprompt)
457 /* choose your game type */
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 coord dropin(int iquad)
570 /* drop a feature on a random dot in the current quadrant */
573 do w = iran(QUADSIZE);
574 while (game.quad[w.x][w.y] != IHDOT);
575 game.quad[w.x][w.y] = iquad;
580 /* update our alert status */
582 game.condit = IHGREEN;
583 if (game.energy < 1000.0) game.condit = IHYELLOW;
584 if (game.state.galaxy[game.quadrant.x][game.quadrant.y].klingons || game.state.galaxy[game.quadrant.x][game.quadrant.y].romulans)
586 if (!game.alive) game.condit=IHDEAD;
589 coord newkling(int i)
590 /* drop new Klingon into current quadrant */
592 coord pi = dropin(IHK);
594 game.kdist[i] = game.kavgd[i] = distance(game.sector, pi);
595 game.kpower[i] = Rand()*150.0 +300.0 +25.0*game.skill;
599 void newqad(bool shutup)
600 /* set up a new state of quadrant, for when we enter or re-enter it */
608 game.base.x = game.base.y = 0;
610 game.comhere = false;
611 game.plnet.x = game.plnet.y = 0;
617 game.inorbit = false;
619 game.ientesc = false;
623 game.iseenit = false;
625 // Attempt to escape Super-commander, so tbeam back!
632 game.quad[i][j] = IHDOT;
633 q = &game.state.galaxy[game.quadrant.x][game.quadrant.y];
634 // cope with supernova
637 game.klhere = q->klingons;
638 game.irhere = q->romulans;
639 game.nenhere = game.klhere + game.irhere;
642 game.quad[game.sector.x][game.sector.y] = game.ship;
645 w.x = w.y = 0; /* quiet a gcc warning */
646 // Position ordinary Klingons
647 for (i = 1; i <= game.klhere; i++)
649 // If we need a commander, promote a Klingon
651 if (same(game.state.kcmdr[i], game.quadrant))
654 if (i <= game.state.remcom) {
655 game.quad[w.x][w.y] = IHC;
656 game.kpower[game.klhere] = 950.0+400.0*Rand()+50.0*game.skill;
660 // If we need a super-commander, promote a Klingon
661 if (same(game.quadrant, game.state.kscmdr)) {
662 game.quad[game.ks[1].x][game.ks[1].y] = IHS;
663 game.kpower[1] = 1175.0 + 400.0*Rand() + 125.0*game.skill;
664 game.iscate = game.state.remkl>1;
668 // Put in Romulans if needed
669 for (i = game.klhere+1; i <= game.nenhere; i++) {
672 game.kdist[i] = game.kavgd[i] = distance(game.sector, w);
673 game.kpower[i] = Rand()*400.0 + 450.0 + 50.0*game.skill;
675 // If quadrant needs a starbase, put it in
677 game.base = dropin(IHB);
679 // If quadrant needs a planet, put it in
680 if (q->planet != NOPLANET) {
681 game.iplnet = q->planet;
682 if (game.state.plnets[q->planet].inhabited == UNINHABITED)
683 game.plnet = dropin(IHP);
685 game.plnet = dropin(IHW);
687 // Check for game.condition
689 // And finally the stars
690 for (i = 1; i <= q->stars; i++)
694 if (game.irhere > 0 && game.klhere == 0 && (q->planet == NOPLANET || game.state.plnets[q->planet].inhabited == UNINHABITED)) {
696 if (!damaged(DRADIO)) {
698 prout("LT. Uhura- \"Captain, an urgent message.");
699 prout(" I'll put it on audio.\" CLICK");
701 prout("INTRUDER! YOU HAVE VIOLATED THE ROMULAN NEUTRAL ZONE.");
702 prout("LEAVE AT ONCE, OR YOU WILL BE DESTROYED!");
707 // Put in THING if needed
708 if (same(thing, game.quadrant)) {
710 thing = iran(GALSIZE);
713 game.ks[game.nenhere] = w;
714 game.kdist[game.nenhere] = game.kavgd[game.nenhere] =
715 distance(game.sector, w);
716 game.kpower[game.nenhere] = Rand()*6000.0 +500.0 +250.0*game.skill;
717 if (!damaged(DSRSENS)) {
719 prout("MR. SPOCK- \"Captain, this is most unusual.");
720 prout(" Please examine your short-range scan.\"");
725 // Decide if quadrant needs a Tholian
726 if (game.options & OPTION_THOLIAN) {
727 if ((game.skill < SKILL_GOOD && Rand() <= 0.02) || /* Lighten up if skill is low */
728 (game.skill == SKILL_GOOD && Rand() <= 0.05) ||
729 (game.skill > SKILL_GOOD && Rand() <= 0.08)
732 game.tholian.x = Rand() > 0.5 ? QUADSIZE : 1;
733 game.tholian.y = Rand() > 0.5 ? QUADSIZE : 1;
734 } while (game.quad[game.tholian.x][game.tholian.y] != IHDOT);
735 game.quad[game.tholian.x][game.tholian.y] = IHT;
738 game.ks[game.nenhere] = game.tholian;
739 game.kdist[game.nenhere] = game.kavgd[game.nenhere] =
740 distance(game.sector, game.tholian);
741 game.kpower[game.nenhere] = Rand()*400.0 +100.0 +25.0*game.skill;
742 /* Reserve unocupied corners */
743 if (game.quad[1][1]==IHDOT) game.quad[1][1] = 'X';
744 if (game.quad[1][QUADSIZE]==IHDOT) game.quad[1][QUADSIZE] = 'X';
745 if (game.quad[QUADSIZE][1]==IHDOT) game.quad[QUADSIZE][1] = 'X';
746 if (game.quad[QUADSIZE][QUADSIZE]==IHDOT) game.quad[QUADSIZE][QUADSIZE] = 'X';
752 // Put in a few black holes
753 for (i = 1; i <= 3; i++)
757 // Take out X's in corners if Tholian present
759 if (game.quad[1][1]=='X') game.quad[1][1] = IHDOT;
760 if (game.quad[1][QUADSIZE]=='X') game.quad[1][QUADSIZE] = IHDOT;
761 if (game.quad[QUADSIZE][1]=='X') game.quad[QUADSIZE][1] = IHDOT;
762 if (game.quad[QUADSIZE][QUADSIZE]=='X') game.quad[QUADSIZE][QUADSIZE] = IHDOT;
767 /* sort Klingons by distance from us */
773 // The author liked bubble sort. So we will use it. :-(
775 if (game.nenhere-iqhere-game.ithere < 2) return;
779 for (j = 1; j < game.nenhere; j++)
780 if (game.kdist[j] > game.kdist[j+1]) {
783 game.kdist[j] = game.kdist[j+1];
786 game.kavgd[j] = game.kavgd[j+1];
789 game.ks[j].x = game.ks[j+1].x;
792 game.ks[j].y = game.ks[j+1].y;
795 game.kpower[j] = game.kpower[j+1];
796 game.kpower[j+1] = t;
801 void setpassword(void)
802 /* set the self-destruct password */
804 if (!(game.options & OPTION_CURSES)) {
807 strcpy(game.passwd, citem);
809 if (*game.passwd != 0) break;
810 proutn(_("Please type in a secret password-"));
814 for(i=0;i<3;i++) game.passwd[i]=(char)(97+(int)(Rand()*25));