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.condition==docked) {
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.");
145 prout("You must be aboard the Enterprise.");
148 if (game.iscraft != onship) {
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 = offship; /* Galileo disappears */
216 game.condition=docked;
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;
243 game.shldchg = false;
247 game.quadrant = randplace(GALSIZE);
248 game.sector = randplace(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.iscate = game.resting = game.imine = game.icrystl = game.icraft = false;
260 game.isatb = game.state.nplankl = 0;
261 game.state.starkl = game.state.basekl = 0;
262 game.iscraft = onship;
268 struct quadrant *quad = &game.state.galaxy[i][j];
270 quad->planet = NOPLANET;
273 quad->starbase = false;
274 quad->supernova = false;
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 = randplace(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 = true;
330 game.state.chart[w.x][w.y].starbase = true;
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 = randplace(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];
361 w = randplace(GALSIZE);
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 = randplace(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++) {
394 w = randplace(GALSIZE);
395 game.state.galaxy[w.x][w.y].romulans = 1;
397 // Locate the Super Commander
398 if (game.state.nscrem > 0) {
399 do w = randplace(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 = randplace(GALSIZE);
409 thing.x = thing.y = 0;
412 game.state.snap = false;
414 if (game.skill == SKILL_NOVICE) {
415 prout("It is stardate %d. The Federation is being attacked by",
416 (int)game.state.date);
417 prout("a deadly Klingon invasion force. As captain of the United");
418 prout("Starship U.S.S. Enterprise, it is your mission to seek out");
419 prout("and destroy this invasion force of %d battle cruisers.",
421 prout("You have an initial allotment of %d stardates to complete", (int)game.intime);
422 prout("your mission. As you proceed you may be given more time.");
424 prout("You will have %d supporting starbases.", game.inbase);
425 proutn("Starbase locations- ");
428 prout("Stardate %d.", (int)game.state.date);
430 prout("%d Klingons.", INKLINGTOT);
431 prout("An unknown number of Romulans.");
432 if (game.state.nscrem) prout("and one (GULP) Super-Commander.");
433 prout("%d stardates.",(int)game.intime);
434 proutn("%d starbases in ", game.inbase);
436 for (i = 1; i <= game.inbase; i++) {
437 proutn(cramlc(0, game.state.baseq[i]));
441 proutn("The Enterprise is currently in ");
442 proutn(cramlc(quadrant, game.quadrant));
444 proutn(cramlc(sector, game.sector));
447 if (game.state.nscrem) prout(" YOU'LL NEED IT.");
450 if (game.nenhere-iqhere-game.ithere) game.shldup = true;
451 if (game.neutz) attack(0); // bad luck to start in a Romulan Neutral Zone
454 bool choose(bool needprompt)
455 /* choose your game type */
460 game.skill = SKILL_NONE;
462 if (needprompt) /* Can start with command line options */
463 proutn("Would you like a regular, tournament, or saved game? ");
465 if (strlen(citem)==0) continue; // Try again
466 if (isit("tournament")) {
467 while (scan() == IHEOL) {
468 proutn("Type in tournament number-");
472 continue; // We don't want a blank entry
474 game.tourn = (int)aaitem;
476 srand((unsigned int)(int)aaitem);
479 if (isit("saved") || isit("frozen")) {
480 if (thaw()) continue;
482 if (*game.passwd==0) continue;
483 if (!game.alldone) game.thawed = 1; // No plaque if not finished
488 if (isit("regular")) break;
489 proutn("What is \"");
494 while (game.length==0 || game.skill==SKILL_NONE) {
495 if (scan() == IHALPHA) {
496 if (isit("short")) game.length = 1;
497 else if (isit("medium")) game.length = 2;
498 else if (isit("long")) game.length = 4;
499 else if (isit("novice")) game.skill = SKILL_NOVICE;
500 else if (isit("fair")) game.skill = SKILL_FAIR;
501 else if (isit("good")) game.skill = SKILL_GOOD;
502 else if (isit("expert")) game.skill = SKILL_EXPERT;
503 else if (isit("emeritus")) game.skill = SKILL_EMERITUS;
505 proutn("What is \"");
512 if (game.length==0) proutn("Would you like a Short, Medium, or Long game? ");
513 else if (game.skill == SKILL_NONE) proutn("Are you a Novice, Fair, Good, Expert, or Emeritus player? ");
516 // Choose game options -- added by ESR for SST2K
517 if (scan() != IHALPHA) {
519 proutn("Choose your game style (or just press enter): ");
523 // Approximates the UT FORTRAN version.
524 game.options &=~ (OPTION_THOLIAN | OPTION_PLANETS | OPTION_THINGY | OPTION_PROBE | OPTION_RAMMING | OPTION_MVBADDY | OPTION_BLKHOLE | OPTION_BASE | OPTION_WORLDS);
525 game.options |= OPTION_PLAIN;
527 else if (isit("almy")) {
528 // Approximates Tom Almy's version.
529 game.options &=~ (OPTION_THINGY | OPTION_BLKHOLE | OPTION_BASE | OPTION_WORLDS);
530 game.options |= OPTION_ALMY;
532 else if (isit("fancy"))
534 else if (strlen(citem)) {
535 proutn("What is \"");
540 if (strcmp(game.passwd, "debug")==0) {
542 fputs("=== Debug mode enabled\n", stdout);
545 // Use parameters to generate initial values of things
546 game.damfac = 0.5 * game.skill;
547 game.state.rembase = 2.0 + Rand()*(BASEMAX-2.0);
548 game.inbase = game.state.rembase;
549 if (game.options & OPTION_PLANETS)
550 game.inplan = NINHAB + (MAXUNINHAB/2) + (MAXUNINHAB/2+1)*Rand();
551 game.state.nromrem = game.inrom = (2.0+Rand())*game.skill;
552 game.state.nscrem = game.inscom = (game.skill > SKILL_FAIR ? 1 : 0);
553 game.state.remtime = 7.0 * game.length;
554 game.intime = game.state.remtime;
555 game.state.remkl = game.inkling = 2.0*game.intime*((game.skill+1 - 2*Rand())*game.skill*0.1+.15);
556 game.incom = game.skill + 0.0625*game.inkling*Rand();
557 game.state.remcom = min(10, game.incom);
558 game.incom = game.state.remcom;
559 game.state.remres = (game.inkling+4*game.incom)*game.intime;
560 game.inresor = game.state.remres;
561 if (game.inkling > 50) {
562 game.inbase = (game.state.rembase += 1);
567 coord dropin(feature iquad)
568 /* drop a feature on a random dot in the current quadrant */
571 do w = randplace(QUADSIZE);
572 while (game.quad[w.x][w.y] != IHDOT);
573 game.quad[w.x][w.y] = iquad;
578 /* update our alert status */
580 game.condition = green;
581 if (game.energy < 1000.0) game.condition = yellow;
582 if (game.state.galaxy[game.quadrant.x][game.quadrant.y].klingons || game.state.galaxy[game.quadrant.x][game.quadrant.y].romulans)
583 game.condition = red;
584 if (!game.alive) game.condition=dead;
587 coord newkling(int i)
588 /* drop new Klingon into current quadrant */
590 coord pi = dropin(IHK);
592 game.kdist[i] = game.kavgd[i] = distance(game.sector, pi);
593 game.kpower[i] = Rand()*150.0 +300.0 +25.0*game.skill;
597 void newqad(bool shutup)
598 /* set up a new state of quadrant, for when we enter or re-enter it */
606 game.base.x = game.base.y = 0;
608 game.comhere = false;
609 game.plnet.x = game.plnet.y = 0;
615 game.inorbit = false;
617 game.ientesc = false;
621 game.iseenit = false;
623 // Attempt to escape Super-commander, so tbeam back!
630 game.quad[i][j] = IHDOT;
631 q = &game.state.galaxy[game.quadrant.x][game.quadrant.y];
632 // cope with supernova
635 game.klhere = q->klingons;
636 game.irhere = q->romulans;
637 game.nenhere = game.klhere + game.irhere;
640 game.quad[game.sector.x][game.sector.y] = game.ship;
643 w.x = w.y = 0; /* quiet a gcc warning */
644 // Position ordinary Klingons
645 for (i = 1; i <= game.klhere; i++)
647 // If we need a commander, promote a Klingon
649 if (same(game.state.kcmdr[i], game.quadrant))
652 if (i <= game.state.remcom) {
653 game.quad[w.x][w.y] = IHC;
654 game.kpower[game.klhere] = 950.0+400.0*Rand()+50.0*game.skill;
658 // If we need a super-commander, promote a Klingon
659 if (same(game.quadrant, game.state.kscmdr)) {
660 game.quad[game.ks[1].x][game.ks[1].y] = IHS;
661 game.kpower[1] = 1175.0 + 400.0*Rand() + 125.0*game.skill;
662 game.iscate = (game.state.remkl > 1);
666 // Put in Romulans if needed
667 for (i = game.klhere+1; i <= game.nenhere; i++) {
670 game.kdist[i] = game.kavgd[i] = distance(game.sector, w);
671 game.kpower[i] = Rand()*400.0 + 450.0 + 50.0*game.skill;
673 // If quadrant needs a starbase, put it in
675 game.base = dropin(IHB);
677 // If quadrant needs a planet, put it in
678 if (q->planet != NOPLANET) {
679 game.iplnet = q->planet;
680 if (game.state.plnets[q->planet].inhabited == UNINHABITED)
681 game.plnet = dropin(IHP);
683 game.plnet = dropin(IHW);
685 // Check for condition
687 // And finally the stars
688 for (i = 1; i <= q->stars; i++)
692 if (game.irhere > 0 && game.klhere == 0 && (q->planet == NOPLANET || game.state.plnets[q->planet].inhabited == UNINHABITED)) {
694 if (!damaged(DRADIO)) {
696 prout("LT. Uhura- \"Captain, an urgent message.");
697 prout(" I'll put it on audio.\" CLICK");
699 prout("INTRUDER! YOU HAVE VIOLATED THE ROMULAN NEUTRAL ZONE.");
700 prout("LEAVE AT ONCE, OR YOU WILL BE DESTROYED!");
705 // Put in THING if needed
706 if (same(thing, game.quadrant)) {
708 thing = randplace(GALSIZE);
711 game.ks[game.nenhere] = w;
712 game.kdist[game.nenhere] = game.kavgd[game.nenhere] =
713 distance(game.sector, w);
714 game.kpower[game.nenhere] = Rand()*6000.0 +500.0 +250.0*game.skill;
715 if (!damaged(DSRSENS)) {
717 prout("MR. SPOCK- \"Captain, this is most unusual.");
718 prout(" Please examine your short-range scan.\"");
723 // Decide if quadrant needs a Tholian
724 if (game.options & OPTION_THOLIAN) {
725 if ((game.skill < SKILL_GOOD && Rand() <= 0.02) || /* Lighten up if skill is low */
726 (game.skill == SKILL_GOOD && Rand() <= 0.05) ||
727 (game.skill > SKILL_GOOD && Rand() <= 0.08)
730 game.tholian.x = Rand() > 0.5 ? QUADSIZE : 1;
731 game.tholian.y = Rand() > 0.5 ? QUADSIZE : 1;
732 } while (game.quad[game.tholian.x][game.tholian.y] != IHDOT);
733 game.quad[game.tholian.x][game.tholian.y] = IHT;
736 game.ks[game.nenhere] = game.tholian;
737 game.kdist[game.nenhere] = game.kavgd[game.nenhere] =
738 distance(game.sector, game.tholian);
739 game.kpower[game.nenhere] = Rand()*400.0 +100.0 +25.0*game.skill;
740 /* Reserve unocupied corners */
741 if (game.quad[1][1]==IHDOT) game.quad[1][1] = 'X';
742 if (game.quad[1][QUADSIZE]==IHDOT) game.quad[1][QUADSIZE] = 'X';
743 if (game.quad[QUADSIZE][1]==IHDOT) game.quad[QUADSIZE][1] = 'X';
744 if (game.quad[QUADSIZE][QUADSIZE]==IHDOT) game.quad[QUADSIZE][QUADSIZE] = 'X';
750 // Put in a few black holes
751 for (i = 1; i <= 3; i++)
755 // Take out X's in corners if Tholian present
757 if (game.quad[1][1]=='X') game.quad[1][1] = IHDOT;
758 if (game.quad[1][QUADSIZE]=='X') game.quad[1][QUADSIZE] = IHDOT;
759 if (game.quad[QUADSIZE][1]=='X') game.quad[QUADSIZE][1] = IHDOT;
760 if (game.quad[QUADSIZE][QUADSIZE]=='X') game.quad[QUADSIZE][QUADSIZE] = IHDOT;
765 /* sort Klingons by distance from us */
771 // The author liked bubble sort. So we will use it. :-(
773 if (game.nenhere-iqhere-game.ithere < 2) return;
777 for (j = 1; j < game.nenhere; j++)
778 if (game.kdist[j] > game.kdist[j+1]) {
781 game.kdist[j] = game.kdist[j+1];
784 game.kavgd[j] = game.kavgd[j+1];
787 game.ks[j].x = game.ks[j+1].x;
790 game.ks[j].y = game.ks[j+1].y;
793 game.kpower[j] = game.kpower[j+1];
794 game.kpower[j+1] = t;
799 void setpassword(void)
800 /* set the self-destruct password */
802 if (!(game.options & OPTION_CURSES)) {
805 strcpy(game.passwd, citem);
807 if (*game.passwd != 0) break;
808 proutn(_("Please type in a secret password-"));
812 for(i=0;i<3;i++) game.passwd[i]=(char)(97+(int)(Rand()*25));