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 (condit==IHDOCKED) {
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 Mac's.");
116 if (game.damage[DSHUTTL]>0) {
117 prout("Shuttle craft damaged.");
121 prout("You must be aboard the Enterprise.");
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 /* Ops! no place to go... */
142 /* If at least one base left, give 'em the Faerie Queene */
144 icrystl = 0; /* crystals are lost */
145 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 (quadx!=game.state.baseqx[nb] || quady!=game.state.baseqy[nb]) {
151 quadx = game.state.baseqx[nb];
152 quady = game.state.baseqy[nb];
157 /* position next to base by trial and error */
158 game.quad[sectx][secty] = IHDOT;
160 sectx = 3.0*Rand() - 1.0 + basex;
161 secty = 3.0*Rand() - 1.0 + basey;
162 if (VALID_SECTOR(sectx, secty) &&
163 game.quad[sectx][secty] == IHDOT) break;
165 if (l < QUADSIZE+1) break; /* found a spot */
171 /* Get new commission */
172 game.quad[sectx][secty] = 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 (icrystl!=0) prout("The dilithium crystals have been moved.");
178 iscraft=0; /* Gallileo disappears */
181 for (l = 0; l < NDEVICES; l++)
182 game.damage[l] = 0.0;
183 game.damage[DSHUTTL] = -1;
184 energy = inenrg = 3000.0;
185 shield = inshld = 1250.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 alldone = gamewon = 0;
206 energy = inenrg = 5000.0;
207 shield = inshld = 2500.0;
208 shldchg = shldup = 0;
211 iran(GALSIZE, &quadx, &quady);
212 iran(QUADSIZE, §x, §y);
213 torps = intorps = 10;
214 nprobes = (int)(3.0*Rand() + 2.0); /* Give them 2-4 of these wonders */
216 wfacsq = warpfac * warpfac;
217 for (i=0; i < NDEVICES; i++)
218 game.damage[i] = 0.0;
219 // Set up assorted game parameters
221 game.state.date = indate = 100.0*(int)(31.0*Rand()+20.0);
222 game.state.killk = game.state.killc = nkinks = nhelp = resting = casual = game.state.nromkl = 0;
223 isatb = iscate = imine = icrystl = icraft = game.state.nsckill = 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].planets = 0;
233 game.state.galaxy[i][j].romulans = 0;
235 // Initialize times for extraneous events
236 game.future[FSNOVA] = game.state.date + expran(0.5 * intime);
237 game.future[FTBEAM] = game.state.date + expran(1.5 * (intime / game.state.remcom));
238 game.future[FSNAP] = game.state.date + 1.0 + Rand(); // Force an early snapshot
239 game.future[FBATTAK] = game.state.date + expran(0.3*intime);
240 game.future[FCDBAS] = FOREVER;
241 game.future[FSCMOVE] = game.state.nscrem ? game.state.date+0.2777 : FOREVER;
242 game.future[FSCDBAS] = FOREVER;
243 game.future[FDSPROB] = FOREVER;
244 // Starchart is functional but we've never seen it
246 // Put stars in the galaxy
250 int k = Rand()*9.0 + 1.0;
252 game.state.galaxy[i][j].stars = k;
254 // Locate star bases in galaxy
255 for (i = 1; i <= inbase; i++) {
258 do iran(GALSIZE, &ix, &iy);
259 while (game.state.galaxy[ix][iy].starbase);
261 for (j = i-1; j > 0; j--) {
262 /* Improved placement algorithm to spread out bases */
263 double distq = square(ix-game.state.baseqx[j]) + square(iy-game.state.baseqy[j]);
264 if (distq < 6.0*(BASEMAX+1-inbase) && Rand() < 0.75) {
267 prout("DEBUG: Abandoning base #%d at %d-%d", i, ix, iy);
272 else if (distq < 6.0 * (BASEMAX+1-inbase)) {
273 prout("DEBUG: saving base #%d, close to #%d", i, j);
279 game.state.baseqx[i] = ix;
280 game.state.baseqy[i] = iy;
281 game.state.galaxy[ix][iy].starbase = 1;
282 game.state.chart[ix][iy].starbase = 1;
284 // Position ordinary Klingon Battle Cruisers
285 krem = inkling - incom - game.state.nscrem;
286 klumper = 0.25*skill*(9.0-length)+1.0;
287 if (klumper > 9) klumper = 9; // Can't have more than 9 in quadrant
290 int klump = (1.0 - r*r)*klumper;
291 if (klump > krem) klump = krem;
293 do iran(GALSIZE,&ix,&iy);
294 while (game.state.galaxy[ix][iy].supernova ||
295 game.state.galaxy[ix][iy].klingons + klump > 9);
296 game.state.galaxy[ix][iy].klingons += klump;
298 // Position Klingon Commander Ships
302 for (i = 1; i <= incom; i++) {
304 do { /* IF debugging, put commanders by bases, always! */
306 if (idebug && klumper <= inbase) {
307 ix = game.state.baseqx[klumper];
308 iy = game.state.baseqy[klumper];
313 iran(GALSIZE, &ix, &iy);
315 while ((!game.state.galaxy[ix][iy].klingons && Rand() < 0.75)||
316 game.state.galaxy[ix][iy].supernova||
317 game.state.galaxy[ix][iy].klingons > 8);
318 // check for duplicate
319 for (j = 1; j < i; j++)
320 if (game.state.cx[j]==ix && game.state.cy[j]==iy) break;
322 game.state.galaxy[ix][iy].klingons++;
323 game.state.cx[i] = ix;
324 game.state.cy[i] = iy;
326 // Locate planets in galaxy
327 for (i = 0; i < inplan; i++) {
328 do iran(GALSIZE, &ix, &iy); while (game.state.galaxy[ix][iy].planets);
329 game.state.galaxy[ix][iy].planets = 1;
330 game.state.plnets[i].x = ix;
331 game.state.plnets[i].y = iy;
332 game.state.plnets[i].pclass = Rand()*3.0; // Planet class M N or O
333 game.state.plnets[i].crystals = 1.5*Rand(); // 1 in 3 chance of crystals
334 game.state.plnets[i].known = unknown;
337 for (i = 1; i <= game.state.nromrem; i++) {
338 iran(GALSIZE, &ix, &iy);
339 game.state.galaxy[ix][iy].romulans = 1;
341 // Locate the Super Commander
342 if (game.state.nscrem > 0) {
343 do iran(GALSIZE, &ix, &iy);
344 while (game.state.galaxy[ix][iy].supernova || game.state.galaxy[ix][iy].klingons > 8);
347 game.state.galaxy[ix][iy].klingons++;
349 // Place thing (in tournament game, thingx == -1, don't want one!)
351 iran(GALSIZE, &thingx, &thingy);
361 if (skill == SKILL_NOVICE) {
362 prout("It is stardate %d. The Federation is being attacked by",
363 (int)game.state.date);
364 prout("a deadly Klingon invasion force. As captain of the United");
365 prout("Starship U.S.S. Enterprise, it is your mission to seek out");
366 prout("and destroy this invasion force of %d battle cruisers.",
368 prout("You have an initial allotment of %d stardates to complete", (int)intime);
369 prout("your mission. As you proceed you may be given more time.");
371 prout("You will have %d supporting starbases.", inbase);
372 proutn("Starbase locations- ");
375 prout("Stardate %d.", (int)game.state.date);
377 prout("%d Klingons.", inkling);
378 prout("An unknown number of Romulans.");
379 if (game.state.nscrem) prout("and one (GULP) Super-Commander.");
380 prout("%d stardates.",(int)intime);
381 proutn("%d starbases in ", inbase);
383 for (i = 1; i <= inbase; i++) {
384 proutn(cramlc(0, game.state.baseqx[i], game.state.baseqy[i]));
388 proutn("The Enterprise is currently in ");
389 proutn(cramlc(quadrant, quadx, quady));
391 proutn(cramlc(sector, sectx, secty));
394 if (game.state.nscrem) prout(" YOU'LL NEED IT.");
397 if (nenhere-iqhere-ithere) shldup=1.0;
398 if (neutz) attack(0); // bad luck to start in a Romulan Neutral Zone
403 srand((int)time(NULL));
406 int choose(int needprompt)
413 if (needprompt) /* Can start with command line options */
414 proutn("Would you like a regular, tournament, or saved game? ");
416 if (strlen(citem)==0) continue; // Try again
417 if (isit("tournament")) {
418 while (scan() == IHEOL) {
419 proutn("Type in tournament number-");
423 continue; // We don't want a blank entry
427 srand((unsigned int)(int)aaitem);
430 if (isit("saved") || isit("frozen")) {
431 if (thaw()) continue;
433 if (*game.passwd==0) continue;
434 if (!alldone) thawed = 1; // No plaque if not finished
439 if (isit("regular")) break;
440 proutn("What is \"");
445 while (length==0 || skill==SKILL_NONE) {
446 if (scan() == IHALPHA) {
447 if (isit("short")) length = 1;
448 else if (isit("medium")) length = 2;
449 else if (isit("long")) length = 4;
450 else if (isit("novice")) skill = SKILL_NOVICE;
451 else if (isit("fair")) skill = SKILL_FAIR;
452 else if (isit("good")) skill = SKILL_GOOD;
453 else if (isit("expert")) skill = SKILL_EXPERT;
454 else if (isit("emeritus")) skill = SKILL_EMERITUS;
456 proutn("What is \"");
463 if (length==0) proutn("Would you like a Short, Medium, or Long game? ");
464 else if (skill == SKILL_NONE) proutn("Are you a Novice, Fair, Good, Expert, or Emeritus player? ");
469 if (strcmp(game.passwd, "debug")==0) idebug = 1;
472 // Use parameters to generate initial values of things
473 damfac = 0.5 * skill;
474 game.state.rembase = 2.0 + Rand()*(BASEMAX-2.0);
475 inbase = game.state.rembase;
476 if (game.options & OPTION_PLANETS)
477 inplan = (PLNETMAX/2) + (PLNETMAX/2+1)*Rand();
478 game.state.nromrem = (2.0+Rand())*skill;
479 game.state.nscrem = (skill > SKILL_FAIR ? 1 : 0);
480 game.state.remtime = 7.0 * length;
481 intime = game.state.remtime;
482 game.state.remkl = 2.0*intime*((skill+1 - 2*Rand())*skill*0.1+.15);
483 inkling = game.state.remkl;
484 incom = skill + 0.0625*inkling*Rand();
485 game.state.remcom= min(10, incom);
486 incom = game.state.remcom;
487 game.state.remres = (inkling+4*incom)*intime;
488 inresor = game.state.remres;
490 inbase = (game.state.rembase += 1);
495 void dropin(int iquad, int *ix, int *iy)
497 do iran(QUADSIZE, ix, iy);
498 while (game.quad[*ix][*iy] != IHDOT);
499 game.quad[*ix][*iy] = iquad;
505 if (energy < 1000.0) condit = IHYELLOW;
506 if (game.state.galaxy[quadx][quady].klingons || game.state.galaxy[quadx][quady].romulans)
508 if (!alive) condit=IHDEAD;
512 void newqad(int shutup)
514 int i, j, ix, iy, nplan;
535 // Attempt to escape Super-commander, so tbeam back!
542 game.quad[i][j] = IHDOT;
543 // cope with supernova
544 if (game.state.galaxy[quadx][quady].supernova)
546 klhere = game.state.galaxy[quadx][quady].klingons;
547 irhere = game.state.galaxy[quadx][quady].romulans;
548 nplan = game.state.galaxy[quadx][quady].planets;
549 nenhere = klhere + irhere;
552 game.quad[sectx][secty] = ship;
554 if (game.state.galaxy[quadx][quady].klingons) {
555 // Position ordinary Klingons
556 for (i = 1; i <= klhere; i++) {
557 dropin(IHK, &ix, &iy);
560 game.kdist[i] = game.kavgd[i] = sqrt(square(sectx-ix) + square(secty-iy));
561 game.kpower[i] = Rand()*150.0 +300.0 +25.0*skill;
563 // If we need a commander, promote a Klingon
565 if (game.state.cx[i]==quadx && game.state.cy[i]==quady) break;
567 if (i <= game.state.remcom) {
568 game.quad[ix][iy] = IHC;
569 game.kpower[klhere] = 950.0+400.0*Rand()+50.0*skill;
573 // If we need a super-commander, promote a Klingon
574 if (quadx == game.state.isx && quady == game.state.isy) {
575 game.quad[game.kx[1]][game.ky[1]] = IHS;
576 game.kpower[1] = 1175.0 + 400.0*Rand() + 125.0*skill;
577 iscate = game.state.remkl>1;
581 // Put in Romulans if needed
582 for (i = klhere+1; i <= nenhere; i++) {
583 dropin(IHR, &ix, &iy);
586 game.kdist[i] = game.kavgd[i] = sqrt(square(sectx-ix) + square(secty-iy));
587 game.kpower[i] = Rand()*400.0 + 450.0 + 50.0*skill;
589 // If quadrant needs a starbase, put it in
590 if (game.state.galaxy[quadx][quady].starbase)
591 dropin(IHB, &basex, &basey);
594 // If quadrant needs a planet, put it in
595 for (i=0; i < inplan; i++)
596 if (game.state.plnets[i].x == quadx && game.state.plnets[i].y == quady) break;
599 dropin(IHP, &plnetx, &plnety);
602 // Check for condition
604 // And finally the stars
605 for (i = 1; i <= game.state.galaxy[quadx][quady].stars; i++)
606 dropin(IHSTAR, &ix, &iy);
609 if (irhere > 0 && klhere == 0) {
611 if (game.damage[DRADIO] <= 0.0) {
613 prout("LT. Uhura- \"Captain, an urgent message.");
614 prout(" I'll put it on audio.\" CLICK");
616 prout("INTRUDER! YOU HAVE VIOLATED THE ROMULAN NEUTRAL ZONE.");
617 prout("LEAVE AT ONCE, OR YOU WILL BE DESTROYED!");
622 // Put in THING if needed
623 if (thingx == quadx && thingy == quady) {
624 dropin(IHQUEST, &ix, &iy);
625 iran(GALSIZE, &thingx, &thingy);
628 game.kx[nenhere] = ix;
629 game.ky[nenhere] = iy;
630 game.kdist[nenhere] = game.kavgd[nenhere] =
631 sqrt(square(sectx-ix) + square(secty-iy));
632 game.kpower[nenhere] = Rand()*6000.0 +500.0 +250.0*skill;
633 if (game.damage[DSRSENS] == 0.0) {
635 prout("MR. SPOCK- \"Captain, this is most unusual.");
636 prout(" Please examine your short-range scan.\"");
641 // Decide if quadrant needs a Tholian
642 if (game.options & OPTION_THOLIAN) {
643 if ((skill < SKILL_GOOD && Rand() <= 0.02) || /* Lighten up if skill is low */
644 (skill == SKILL_GOOD && Rand() <= 0.05) ||
645 (skill > SKILL_GOOD && Rand() <= 0.08)
647 || strcmp(game.passwd, "tholianx")==0
651 ithx = Rand() > 0.5 ? QUADSIZE : 1;
652 ithy = Rand() > 0.5 ? QUADSIZE : 1;
653 } while (game.quad[ithx][ithy] != IHDOT);
654 game.quad[ithx][ithy] = IHT;
657 game.kx[nenhere] = ithx;
658 game.ky[nenhere] = ithy;
659 game.kdist[nenhere] = game.kavgd[nenhere] =
660 sqrt(square(sectx-ithx) + square(secty-ithy));
661 game.kpower[nenhere] = Rand()*400.0 +100.0 +25.0*skill;
662 /* Reserve unocupied corners */
663 if (game.quad[1][1]==IHDOT) game.quad[1][1] = 'X';
664 if (game.quad[1][QUADSIZE]==IHDOT) game.quad[1][QUADSIZE] = 'X';
665 if (game.quad[QUADSIZE][1]==IHDOT) game.quad[QUADSIZE][1] = 'X';
666 if (game.quad[QUADSIZE][QUADSIZE]==IHDOT) game.quad[QUADSIZE][QUADSIZE] = 'X';
672 // Put in a few black holes
673 for (i = 1; i <= 3; i++)
675 dropin(IHBLANK, &ix, &iy);
677 // Take out X's in corners if Tholian present
679 if (game.quad[1][1]=='X') game.quad[1][1] = IHDOT;
680 if (game.quad[1][QUADSIZE]=='X') game.quad[1][QUADSIZE] = IHDOT;
681 if (game.quad[QUADSIZE][1]=='X') game.quad[QUADSIZE][1] = IHDOT;
682 if (game.quad[QUADSIZE][QUADSIZE]=='X') game.quad[QUADSIZE][QUADSIZE] = IHDOT;
691 // The author liked bubble sort. So we will use it. :-(
693 if (nenhere-iqhere-ithere < 2) return;
697 for (j = 1; j < nenhere; j++)
698 if (game.kdist[j] > game.kdist[j+1]) {
701 game.kdist[j] = game.kdist[j+1];
704 game.kavgd[j] = game.kavgd[j+1];
707 game.kx[j] = game.kx[j+1];
710 game.ky[j] = game.ky[j+1];
713 game.kpower[j] = game.kpower[j+1];
714 game.kpower[j+1] = t;