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;
159 for (l = 1; l <= QUADSIZE; l++) {
160 sectx = 3.0*Rand() - 1.0 + basex;
161 secty = 3.0*Rand() - 1.0 + basey;
162 if (sectx >= 1 && sectx <= QUADSIZE &&
163 secty >= 1 && secty <= QUADSIZE &&
164 game.quad[sectx][secty] == IHDOT) break;
166 if (l < QUADSIZE+1) break; /* found a spot */
172 /* Get new commission */
173 game.quad[sectx][secty] = ship = IHF;
174 prout("Starfleet puts you in command of another ship,");
175 prout("the Faerie Queene, which is antiquated but,");
176 prout("still useable.");
177 if (icrystl!=0) prout("The dilithium crystals have been moved.");
179 iscraft=0; /* Gallileo disappears */
182 for (l = 0; l < NDEVICES; l++)
183 game.damage[l] = 0.0;
184 game.damage[DSHUTTL] = -1;
185 energy = inenrg = 3000.0;
186 shield = inshld = 1250.0;
195 void setup(int needprompt)
197 int i,j, krem, klumper;
202 // Decide how many of everything
203 if (choose(needprompt)) return; // frozen game
204 // Prepare the Enterprise
205 alldone = gamewon = 0;
207 energy = inenrg = 5000.0;
208 shield = inshld = 2500.0;
209 shldchg = shldup = 0;
212 iran(GALSIZE, &quadx, &quady);
213 iran(QUADSIZE, §x, §y);
214 torps = intorps = 10;
215 nprobes = (int)(3.0*Rand() + 2.0); /* Give them 2-4 of these wonders */
217 wfacsq = warpfac * warpfac;
218 for (i=0; i < NDEVICES; i++)
219 game.damage[i] = 0.0;
220 // Set up assorted game parameters
222 game.state.date = indate = 100.0*(int)(31.0*Rand()+20.0);
223 game.state.killk = game.state.killc = nkinks = nhelp = resting = casual = game.state.nromkl = 0;
224 isatb = iscate = imine = icrystl = icraft = game.state.nsckill = game.state.nplankl = 0;
225 game.state.starkl = game.state.basekl = 0;
230 for (i = 1; i <= GALSIZE; i++)
231 for (j = 1; j <= GALSIZE; j++) {
232 game.state.galaxy[i][j].charted = 0;
233 game.state.galaxy[i][j].planets = 0;
234 game.state.galaxy[i][j].romulans = 0;
236 // Initialize times for extraneous events
237 game.future[FSNOVA] = game.state.date + expran(0.5 * intime);
238 game.future[FTBEAM] = game.state.date + expran(1.5 * (intime / game.state.remcom));
239 game.future[FSNAP] = game.state.date + 1.0 + Rand(); // Force an early snapshot
240 game.future[FBATTAK] = game.state.date + expran(0.3*intime);
241 game.future[FCDBAS] = 1e30;
242 game.future[FSCMOVE] = game.state.nscrem ? game.state.date+0.2777 : 1e30;
243 game.future[FSCDBAS] = 1e30;
244 game.future[FDSPROB] = 1e30;
245 // Starchart is functional
247 // Put stars in the galaxy
249 for (i=1; i<=GALSIZE; i++)
250 for (j=1; j<=GALSIZE; j++) {
251 int k = Rand()*9.0 + 1.0;
253 game.state.galaxy[i][j].stars = k;
255 // Locate star bases in galaxy
256 for (i = 1; i <= inbase; i++) {
259 do iran(GALSIZE, &ix, &iy);
260 while (game.state.galaxy[ix][iy].starbase);
262 for (j = i-1; j > 0; j--) {
263 /* Improved placement algorithm to spread out bases */
264 double distq = square(ix-game.state.baseqx[j]) + square(iy-game.state.baseqy[j]);
265 if (distq < 6.0*(BASEMAX-inbase) && Rand() < 0.75) {
268 proutn("DEBUG: Abandoning base #%d at %d-%d\n", i, ix, iy);
273 else if (distq < 6.0 * (BASEMAX-inbase)) {
274 proutn("DEBUG: saving base #%d, close to #%d\n", i, j);
280 game.state.baseqx[i] = ix;
281 game.state.baseqy[i] = iy;
282 game.state.galaxy[ix][iy].starbase = 1;
283 game.state.chart[ix][iy].starbase = 1;
285 // Position ordinary Klingon Battle Cruisers
286 krem = inkling - incom - game.state.nscrem;
287 klumper = 0.25*skill*(9.0-length)+1.0;
288 if (klumper > 9) klumper = 9; // Can't have more than 9 in quadrant
291 int klump = (1.0 - r*r)*klumper;
292 if (klump > krem) klump = krem;
294 do iran(GALSIZE,&ix,&iy); while (game.state.galaxy[ix][iy].supernova);
295 game.state.galaxy[ix][iy].klingons += klump;
297 // Position Klingon Commander Ships
301 for (i = 1; i <= incom; i++) {
303 do { /* IF debugging, put commanders by bases, always! */
305 if (idebug && klumper <= inbase) {
306 ix = game.state.baseqx[klumper];
307 iy = game.state.baseqy[klumper];
312 iran(GALSIZE, &ix, &iy);
314 while ((!game.state.galaxy[ix][iy].klingons && Rand() < 0.75)||
315 game.state.galaxy[ix][iy].supernova||
316 game.state.galaxy[ix][iy].klingons > 8);
317 // check for duplicate
318 for (j = 1; j < i; j++)
319 if (game.state.cx[j]==ix && game.state.cy[j]==iy) break;
321 game.state.galaxy[ix][iy].klingons++;
322 game.state.cx[i] = ix;
323 game.state.cy[i] = iy;
325 // Locate planets in galaxy
326 for (i = 0; i < inplan; i++) {
327 do iran(GALSIZE, &ix, &iy); while (game.state.galaxy[ix][iy].planets);
328 game.state.galaxy[ix][iy].planets = 1;
329 game.state.plnets[i].x = ix;
330 game.state.plnets[i].y = iy;
331 game.state.plnets[i].pclass = Rand()*3.0; // Planet class M N or O
332 game.state.plnets[i].crystals = 1.5*Rand(); // 1 in 3 chance of crystals
333 game.state.plnets[i].known = unknown;
336 for (i = 1; i <= game.state.nromrem; i++) {
337 iran(GALSIZE, &ix, &iy);
338 game.state.galaxy[ix][iy].romulans = 1;
340 // Locate the Super Commander
341 if (game.state.nscrem > 0) {
342 do iran(GALSIZE, &ix, &iy);
343 while (game.state.galaxy[ix][iy].supernova || game.state.galaxy[ix][iy].klingons > 8);
346 game.state.galaxy[ix][iy].klingons++;
348 // Place thing (in tournament game, thingx == -1, don't want one!)
350 iran(GALSIZE, &thingx, &thingy);
360 if (skill == SKILL_NOVICE) {
361 prout("It is stardate %d. The Federation is being attacked by",
362 (int)game.state.date);
363 prout("a deadly Klingon invasion force. As captain of the United");
364 prout("Starship U.S.S. Enterprise, it is your mission to seek out");
365 prout("and destroy this invasion force of %d battle cruisers.",
367 prout("You have an initial allotment of %d stardates to complete", (int)intime);
368 prout("your mission. As you proceed you may be given more time.");
370 prout("You will have %d supporting starbases.", inbase);
371 proutn("Starbase locations- ");
374 prout("Stardate %d.", (int)game.state.date);
376 prout("%d Klingons.", inkling);
377 prout("An unknown number of Romulans.");
378 if (game.state.nscrem) prout("and one (GULP) Super-Commander.");
379 prout("%d stardates.",(int)intime);
380 proutn("%d starbases in ", inbase);
382 for (i = 1; i <= inbase; i++) {
383 proutn(cramlc(0, game.state.baseqx[i], game.state.baseqy[i]));
387 proutn("The Enterprise is currently in ");
388 proutn(cramlc(quadrant, quadx, quady));
390 proutn(cramlc(sector, sectx, secty));
393 if (game.state.nscrem) prout(" YOU'LL NEED IT.");
396 if (nenhere-iqhere-ithere) shldup=1.0;
397 if (neutz) attack(0); // bad luck to start in a Romulan Neutral Zone
402 srand((int)time(NULL));
405 int choose(int needprompt)
412 if (needprompt) /* Can start with command line options */
413 proutn("Would you like a regular, tournament, or saved game? ");
415 if (strlen(citem)==0) continue; // Try again
416 if (isit("tournament")) {
417 while (scan() == IHEOL) {
418 proutn("Type in tournament number-");
422 continue; // We don't want a blank entry
426 srand((unsigned int)(int)aaitem);
429 if (isit("saved") || isit("frozen")) {
430 if (thaw()) continue;
432 if (*game.passwd==0) continue;
433 if (!alldone) thawed = 1; // No plaque if not finished
438 if (isit("regular")) break;
439 proutn("What is \"");
444 while (length==0 || skill==SKILL_NONE) {
445 if (scan() == IHALPHA) {
446 if (isit("short")) length = 1;
447 else if (isit("medium")) length = 2;
448 else if (isit("long")) length = 4;
449 else if (isit("novice")) skill = SKILL_NOVICE;
450 else if (isit("fair")) skill = SKILL_FAIR;
451 else if (isit("good")) skill = SKILL_GOOD;
452 else if (isit("expert")) skill = SKILL_EXPERT;
453 else if (isit("emeritus")) skill = SKILL_EMERITUS;
455 proutn("What is \"");
462 if (length==0) proutn("Would you like a Short, Medium, or Long game? ");
463 else if (skill == SKILL_NONE) proutn("Are you a Novice, Fair, Good, Expert, or Emeritus player? ");
468 if (strcmp(game.passwd, "debug")==0) idebug = 1;
471 // Use parameters to generate initial values of things
472 damfac = 0.5 * skill;
473 game.state.rembase = 3.0*Rand()+2.0;
474 inbase = game.state.rembase;
475 if (game.options & OPTION_PLANETS)
476 inplan = (PLNETMAX/2) + (PLNETMAX/2+1)*Rand();
477 game.state.nromrem = (2.0+Rand())*skill;
478 game.state.nscrem = (skill > SKILL_FAIR ? 1 : 0);
479 game.state.remtime = 7.0 * length;
480 intime = game.state.remtime;
481 game.state.remkl = 2.0*intime*((skill+1 - 2*Rand())*skill*0.1+.15);
482 inkling = game.state.remkl;
483 incom = skill + 0.0625*inkling*Rand();
484 game.state.remcom= min(10, incom);
485 incom = game.state.remcom;
486 game.state.remres = (inkling+4*incom)*intime;
487 inresor = game.state.remres;
489 inbase = (game.state.rembase += 1);
494 void dropin(int iquad, int *ix, int *iy)
496 do iran(QUADSIZE, ix, iy);
497 while (game.quad[*ix][*iy] != IHDOT);
498 game.quad[*ix][*iy] = iquad;
504 if (energy < 1000.0) condit = IHYELLOW;
505 if (game.state.galaxy[quadx][quady].klingons || game.state.galaxy[quadx][quady].romulans)
507 if (!alive) condit=IHDEAD;
511 void newqad(int shutup)
513 int i, j, ix, iy, nplan;
534 // Attempt to escape Super-commander, so tbeam back!
539 for (i=1; i <= QUADSIZE; i++)
540 for (j=1; j <= QUADSIZE; j++)
541 game.quad[i][j] = IHDOT;
542 // cope with supernova
543 if (game.state.galaxy[quadx][quady].supernova)
545 klhere = game.state.galaxy[quadx][quady].klingons;
546 irhere = game.state.galaxy[quadx][quady].romulans;
547 nplan = game.state.galaxy[quadx][quady].planets;
548 nenhere = klhere + irhere;
551 game.quad[sectx][secty] = ship;
553 if (game.state.galaxy[quadx][quady].klingons) {
554 // Position ordinary Klingons
555 for (i = 1; i <= klhere; i++) {
556 dropin(IHK, &ix, &iy);
559 game.kdist[i] = game.kavgd[i] = sqrt(square(sectx-ix) + square(secty-iy));
560 game.kpower[i] = Rand()*150.0 +300.0 +25.0*skill;
562 // If we need a commander, promote a Klingon
563 for (i = 1; i <= game.state.remcom ; i++)
564 if (game.state.cx[i]==quadx && game.state.cy[i]==quady) break;
566 if (i <= game.state.remcom) {
567 game.quad[ix][iy] = IHC;
568 game.kpower[klhere] = 950.0+400.0*Rand()+50.0*skill;
572 // If we need a super-commander, promote a Klingon
573 if (quadx == game.state.isx && quady == game.state.isy) {
574 game.quad[game.kx[1]][game.ky[1]] = IHS;
575 game.kpower[1] = 1175.0 + 400.0*Rand() + 125.0*skill;
576 iscate = game.state.remkl>1;
580 // Put in Romulans if needed
581 for (i = klhere+1; i <= nenhere; i++) {
582 dropin(IHR, &ix, &iy);
585 game.kdist[i] = game.kavgd[i] = sqrt(square(sectx-ix) + square(secty-iy));
586 game.kpower[i] = Rand()*400.0 + 450.0 + 50.0*skill;
588 // If quadrant needs a starbase, put it in
589 if (game.state.galaxy[quadx][quady].starbase)
590 dropin(IHB, &basex, &basey);
593 // If quadrant needs a planet, put it in
594 for (i=0; i < inplan; i++)
595 if (game.state.plnets[i].x == quadx && game.state.plnets[i].y == quady) break;
598 dropin(IHP, &plnetx, &plnety);
601 // Check for condition
603 // And finally the stars
604 for (i = 1; i <= game.state.galaxy[quadx][quady].stars; i++)
605 dropin(IHSTAR, &ix, &iy);
608 if (irhere > 0 && klhere == 0) {
610 if (game.damage[DRADIO] <= 0.0) {
612 prout("LT. Uhura- \"Captain, an urgent message.");
613 prout(" I'll put it on audio.\" CLICK");
615 prout("INTRUDER! YOU HAVE VIOLATED THE ROMULAN NEUTRAL ZONE.");
616 prout("LEAVE AT ONCE, OR YOU WILL BE DESTROYED!");
621 // Put in THING if needed
622 if (thingx == quadx && thingy == quady) {
623 dropin(IHQUEST, &ix, &iy);
624 iran(GALSIZE, &thingx, &thingy);
627 game.kx[nenhere] = ix;
628 game.ky[nenhere] = iy;
629 game.kdist[nenhere] = game.kavgd[nenhere] =
630 sqrt(square(sectx-ix) + square(secty-iy));
631 game.kpower[nenhere] = Rand()*6000.0 +500.0 +250.0*skill;
632 if (game.damage[DSRSENS] == 0.0) {
634 prout("MR. SPOCK- \"Captain, this is most unusual.");
635 prout(" Please examine your short-range scan.\"");
640 // Decide if quadrant needs a Tholian
641 if (game.options & OPTION_THOLIAN) {
642 if ((skill < SKILL_GOOD && Rand() <= 0.02) || /* Lighten up if skill is low */
643 (skill == SKILL_GOOD && Rand() <= 0.05) ||
644 (skill > SKILL_GOOD && Rand() <= 0.08)
646 || strcmp(game.passwd, "tholianx")==0
650 ithx = Rand() > 0.5 ? QUADSIZE : 1;
651 ithy = Rand() > 0.5 ? QUADSIZE : 1;
652 } while (game.quad[ithx][ithy] != IHDOT);
653 game.quad[ithx][ithy] = IHT;
656 game.kx[nenhere] = ithx;
657 game.ky[nenhere] = ithy;
658 game.kdist[nenhere] = game.kavgd[nenhere] =
659 sqrt(square(sectx-ithx) + square(secty-ithy));
660 game.kpower[nenhere] = Rand()*400.0 +100.0 +25.0*skill;
661 /* Reserve unocupied corners */
662 if (game.quad[1][1]==IHDOT) game.quad[1][1] = 'X';
663 if (game.quad[1][QUADSIZE]==IHDOT) game.quad[1][QUADSIZE] = 'X';
664 if (game.quad[QUADSIZE][1]==IHDOT) game.quad[QUADSIZE][1] = 'X';
665 if (game.quad[QUADSIZE][QUADSIZE]==IHDOT) game.quad[QUADSIZE][QUADSIZE] = 'X';
671 // Put in a few black holes
672 for (i = 1; i <= 3; i++)
674 dropin(IHBLANK, &ix, &iy);
676 // Take out X's in corners if Tholian present
678 if (game.quad[1][1]=='X') game.quad[1][1] = IHDOT;
679 if (game.quad[1][QUADSIZE]=='X') game.quad[1][QUADSIZE] = IHDOT;
680 if (game.quad[QUADSIZE][1]=='X') game.quad[QUADSIZE][1] = IHDOT;
681 if (game.quad[QUADSIZE][QUADSIZE]=='X') game.quad[QUADSIZE][QUADSIZE] = IHDOT;
690 // The author liked bubble sort. So we will use it. :-(
692 if (nenhere-iqhere-ithere < 2) return;
696 for (j = 1; j < nenhere; j++)
697 if (game.kdist[j] > game.kdist[j+1]) {
700 game.kdist[j] = game.kdist[j+1];
703 game.kavgd[j] = game.kavgd[j+1];
706 game.kx[j] = game.kx[j+1];
709 game.ky[j] = game.ky[j+1];
712 game.kpower[j] = game.kpower[j+1];
713 game.kpower[j+1] = t;