3 #include <sys/stat.h>
\r
10 static long filelength(int fd) {
\r
16 #endif /* SERGEEV */
\r
20 prout("-SUPER- STAR TREK");
\r
23 prout("Latest update-21 Sept 78");
\r
25 #endif /* SERGEEV */
\r
28 void freeze(int boss) {
\r
32 strcpy(citem, "emsave.trk");
\r
35 if ((key = scan()) == IHEOL) {
\r
36 proutn("File name: ");
\r
39 if (key != IHALPHA) {
\r
44 if (strchr(citem, '.') == NULL) {
\r
45 strcat(citem, ".trk");
\r
48 if ((fp = fopen(citem, "wb")) == NULL) {
\r
49 proutn("Can't freeze game as file ");
\r
54 strcpy(game.magic, SSTMAGIC);
\r
55 fwrite(&game, sizeof(game), 1, fp);
\r
59 /* I hope that's enough! */
\r
67 game.passwd[0] = '\0';
\r
68 if ((key = scan()) == IHEOL) {
\r
69 proutn("File name: ");
\r
72 if (key != IHALPHA) {
\r
77 if (strchr(citem, '.') == NULL) {
\r
78 strcat(citem, ".trk");
\r
80 if ((fp = fopen(citem, "rb")) == NULL) {
\r
81 proutn("Can't find game file ");
\r
86 fread(&game, sizeof(game), 1, fp);
\r
87 if (strcmp(game.magic, SSTMAGIC)) {
\r
88 prout("Game file format is bad, should begin with " SSTMAGIC);
\r
103 if (condit==IHDOCKED) {
\r
105 prout("You cannot abandon Ye Faerie Queene.");
\r
110 /* Must take shuttle craft to exit */
\r
111 if (game.damage[DSHUTTL]==-1) {
\r
112 prout("Ye Faerie Queene has no shuttle craft.");
\r
115 if (game.damage[DSHUTTL]<0) {
\r
116 prout("Shuttle craft now serving Big Mac's.");
\r
119 if (game.damage[DSHUTTL]>0) {
\r
120 prout("Shuttle craft damaged.");
\r
124 prout("You must be aboard the Enterprise.");
\r
128 prout("Shuttle craft not currently available.");
\r
131 /* Print abandon ship messages */
\r
133 prouts("***ABANDON SHIP! ABANDON SHIP!");
\r
135 prouts("***ALL HANDS ABANDON SHIP!");
\r
137 prout("Captain and crew escape in shuttle craft.");
\r
138 prout("Remainder of ship's complement beam down");
\r
139 prout("to nearest habitable planet.");
\r
140 if (game.state.rembase==0) {
\r
141 /* Ops! no place to go... */
\r
145 /* If at least one base left, give 'em the Faerie Queene */
\r
147 icrystl = 0; /* crystals are lost */
\r
148 nprobes = 0; /* No probes */
\r
149 prout("You are captured by Klingons and released to");
\r
150 prout("the Federation in a prisoner-of-war exchange.");
\r
151 nb = Rand()*game.state.rembase+1;
\r
152 /* Set up quadrant and position FQ adjacient to base */
\r
153 if (quadx!=game.state.baseqx[nb] || quady!=game.state.baseqy[nb]) {
\r
154 quadx = game.state.baseqx[nb];
\r
155 quady = game.state.baseqy[nb];
\r
160 /* position next to base by trial and error */
\r
161 game.quad[sectx][secty] = IHDOT;
\r
162 for (l = 1; l <= 10; l++) {
\r
163 sectx = 3.0*Rand() - 1.0 + basex;
\r
164 secty = 3.0*Rand() - 1.0 + basey;
\r
165 if (sectx >= 1 && sectx <= 10 &&
\r
166 secty >= 1 && secty <= 10 &&
\r
167 game.quad[sectx][secty] == IHDOT) break;
\r
169 if (l < 11) break; /* found a spot */
\r
175 /* Get new commission */
\r
176 game.quad[sectx][secty] = ship = IHF;
\r
177 prout("Starfleet puts you in command of another ship,");
\r
178 prout("the Faerie Queene, which is antiquated but,");
\r
179 prout("still useable.");
\r
180 if (icrystl!=0) prout("The dilithium crystals have been moved.");
\r
182 iscraft=0; /* Gallileo disappears */
\r
183 /* Resupply ship */
\r
185 for (l = 1; l <= NDEVICES; l++) game.damage[l] = 0.0;
\r
186 game.damage[DSHUTTL] = -1;
\r
187 energy = inenrg = 3000.0;
\r
188 shield = inshld = 1250.0;
\r
189 torps = intorps = 6;
\r
197 void setup(int needprompt) {
\r
198 int i,j, krem, klumper;
\r
203 // Decide how many of everything
\r
204 if (choose(needprompt)) return; // frozen game
\r
205 // Prepare the Enterprise
\r
206 alldone = gamewon = 0;
\r
208 energy = inenrg = 5000.0;
\r
209 shield = inshld = 2500.0;
\r
210 shldchg = shldup = 0;
\r
213 iran8(&quadx, &quady);
\r
214 iran10(§x, §y);
\r
215 torps = intorps = 10;
\r
216 nprobes = (int)(3.0*Rand() + 2.0); /* Give them 2-4 of these wonders */
\r
218 wfacsq = warpfac * warpfac;
\r
219 for (i=0; i <= NDEVICES; i++) game.damage[i] = 0.0;
\r
220 // Set up assorted game parameters
\r
222 game.state.date = indate = 100.0*(int)(31.0*Rand()+20.0);
\r
223 game.state.killk = game.state.killc = nkinks = nhelp = resting = casual = game.state.nromkl = 0;
\r
224 isatb = iscate = imine = icrystl = icraft = game.state.nsckill = game.state.nplankl = 0;
\r
225 game.state.starkl = game.state.basekl = 0;
\r
230 for (i = 1; i <= 8; i++)
\r
231 for (j = 1; j <= 8; j++) game.state.newstuf[i][j] = game.starch[i][j] = 0;
\r
232 // Initialize times for extraneous events
\r
233 game.future[FSNOVA] = game.state.date + expran(0.5 * intime);
\r
234 game.future[FTBEAM] = game.state.date + expran(1.5 * (intime / game.state.remcom));
\r
235 game.future[FSNAP] = game.state.date + 1.0 + Rand(); // Force an early snapshot
\r
236 game.future[FBATTAK] = game.state.date + expran(0.3*intime);
\r
237 game.future[FCDBAS] = 1e30;
\r
238 game.future[FSCMOVE] = game.state.nscrem ? game.state.date+0.2777 : 1e30;
\r
239 game.future[FSCDBAS] = 1e30;
\r
240 game.future[FDSPROB] = 1e30;
\r
241 // Starchart is functional
\r
243 // Put stars in the galaxy
\r
245 for (i=1; i<=8; i++)
\r
246 for (j=1; j<=8; j++) {
\r
247 int k = Rand()*9.0 + 1.0;
\r
249 game.state.galaxy[i][j] = k;
\r
251 // Locate star bases in galaxy
\r
252 for (i = 1; i <= inbase; i++) {
\r
255 do iran8(&ix, &iy);
\r
256 while (game.state.galaxy[ix][iy] >= 10);
\r
258 for (j = i-1; j > 0; j--) {
\r
259 /* Improved placement algorithm to spread out bases */
\r
260 double distq = square(ix-game.state.baseqx[j]) + square(iy-game.state.baseqy[j]);
\r
261 if (distq < 6.0*(6-inbase) && Rand() < 0.75) {
\r
264 proutn("DEBUG: Abandoning base #%d at %d-%d\n", i, ix, iy);
\r
269 else if (distq < 6.0 * (6-inbase)) {
\r
270 proutn("DEBUG: saving base #%d, close to #%d\n", i, j);
\r
274 } while (contflag);
\r
276 game.state.baseqx[i] = ix;
\r
277 game.state.baseqy[i] = iy;
\r
278 game.starch[ix][iy] = -1;
\r
279 game.state.galaxy[ix][iy] += 10;
\r
281 // Position ordinary Klingon Battle Cruisers
\r
282 krem = inkling - incom - game.state.nscrem;
\r
283 klumper = 0.25*skill*(9.0-length)+1.0;
\r
284 if (klumper > 9) klumper = 9; // Can't have more than 9 in quadrant
\r
287 int klump = (1.0 - r*r)*klumper;
\r
288 if (klump > krem) klump = krem;
\r
291 do iran8(&ix, &iy);
\r
292 while (game.state.galaxy[ix][iy] + klump >= 1000);
\r
293 game.state.galaxy[ix][iy] += klump;
\r
294 } while (krem > 0);
\r
295 // Position Klingon Commander Ships
\r
299 for (i = 1; i <= incom; i++) {
\r
301 do { /* IF debugging, put commanders by bases, always! */
\r
303 if (idebug && klumper <= inbase) {
\r
304 ix = game.state.baseqx[klumper];
\r
305 iy = game.state.baseqy[klumper];
\r
312 while ((game.state.galaxy[ix][iy] < 99 && Rand() < 0.75)||
\r
313 game.state.galaxy[ix][iy]>899);
\r
314 // check for duplicate
\r
315 for (j = 1; j < i; j++)
\r
316 if (game.state.cx[j]==ix && game.state.cy[j]==iy) break;
\r
318 game.state.galaxy[ix][iy] += 100;
\r
319 game.state.cx[i] = ix;
\r
320 game.state.cy[i] = iy;
\r
322 // Locate planets in galaxy
\r
323 for (i = 0; i < inplan; i++) {
\r
324 do iran8(&ix, &iy);
\r
325 while (game.state.newstuf[ix][iy] > 0);
\r
326 game.state.newstuf[ix][iy] = 1;
\r
327 game.state.plnets[i].x = ix;
\r
328 game.state.plnets[i].y = iy;
\r
329 game.state.plnets[i].pclass = Rand()*3.0; // Planet class M N or O
\r
330 game.state.plnets[i].crystals = 1.5*Rand(); // 1 in 3 chance of crystals
\r
331 game.state.plnets[i].known = unknown;
\r
334 for (i = 1; i <= game.state.nromrem; i++) {
\r
336 game.state.newstuf[ix][iy] += 10;
\r
338 // Locate the Super Commander
\r
339 if (game.state.nscrem > 0) {
\r
340 do iran8(&ix, &iy);
\r
341 while (game.state.galaxy[ix][iy] >= 900);
\r
342 game.state.isx = ix;
\r
343 game.state.isy = iy;
\r
344 game.state.galaxy[ix][iy] += 100;
\r
346 // Place thing (in tournament game, thingx == -1, don't want one!)
\r
347 if (thingx != -1) {
\r
348 iran8(&thingx, &thingy);
\r
351 thingx = thingy = 0;
\r
356 game.state.snap = 0;
\r
359 prout("It is stardate %d. The Federation is being attacked by",
\r
360 (int)game.state.date);
\r
361 prout("a deadly Klingon invasion force. As captain of the United");
\r
362 prout("Starship U.S.S. Enterprise, it is your mission to seek out");
\r
363 prout("and destroy this invasion force of %d battle cruisers.",
\r
365 prout("You have an initial allotment of %d stardates to complete", (int)intime);
\r
366 prout("your mission. As you proceed you may be given more time.");
\r
368 prout("You will have %d supporting starbases.", inbase);
\r
369 proutn("Starbase locations- ");
\r
372 prout("Stardate %d.", (int)game.state.date);
\r
374 prout("%d Klingons.", inkling);
\r
375 prout("An unknown number of Romulans.");
\r
376 if (game.state.nscrem) prout("and one (GULP) Super-Commander.");
\r
377 prout("%d stardates.",(int)intime);
\r
378 proutn("%d starbases in ", inbase);
\r
380 for (i = 1; i <= inbase; i++) {
\r
381 proutn(cramlc(0, game.state.baseqx[i], game.state.baseqy[i]));
\r
385 proutn("The Enterprise is currently in ");
\r
386 proutn(cramlc(quadrant, quadx, quady));
\r
388 proutn(cramlc(sector, sectx, secty));
\r
390 prout("Good Luck!");
\r
391 if (game.state.nscrem) prout(" YOU'LL NEED IT.");
\r
394 #endif /* SERGEEV */
\r
396 if (nenhere-iqhere-ithere) shldup=1.0;
\r
397 if (neutz) attack(0); // bad luck to start in a Romulan Neutral Zone
\r
400 void randomize(void) {
\r
401 srand((int)time(NULL));
\r
404 int choose(int needprompt) {
\r
410 if (needprompt) /* Can start with command line options */
\r
411 proutn("Would you like a regular, tournament, or frozen game?");
\r
413 if (strlen(citem)==0) continue; // Try again
\r
414 if (isit("tournament")) {
\r
415 while (scan() == IHEOL) {
\r
416 proutn("Type in tournament number-");
\r
420 continue; // We don't want a blank entry
\r
422 tourn = (int)aaitem;
\r
424 srand((unsigned int)(int)aaitem);
\r
427 if (isit("frozen")) {
\r
430 if (*game.passwd==0) continue;
\r
432 Rand(); Rand(); Rand(); Rand();
\r
433 if (!alldone) thawed = 1; // No plaque if not finished
\r
437 if (isit("regular")) {
\r
440 Rand(); Rand(); Rand(); Rand();
\r
443 proutn("What is \"");
\r
448 while (length==0 || skill==0) {
\r
449 if (scan() == IHALPHA) {
\r
450 if (isit("short")) length = 1;
\r
451 else if (isit("medium")) length = 2;
\r
452 else if (isit("long")) length = 4;
\r
453 else if (isit("novice")) skill = 1;
\r
454 else if (isit("fair")) skill = 2;
\r
455 else if (isit("good")) skill = 3;
\r
456 else if (isit("expert")) skill = 4;
\r
457 else if (isit("emeritus")) skill = 5;
\r
459 proutn("What is \"");
\r
466 if (length==0) proutn("Would you like a Short, Medium, or Long game? ");
\r
467 else if (skill == 0) proutn("Are you a Novice, Fair, Good, Expert, or Emeritus player? ");
\r
473 strcpy(game.passwd, citem);
\r
475 if (*game.passwd != 0) break;
\r
476 proutn("Please type in a secret password-");
\r
479 for(i=0;i<3;i++) game.passwd[i]=(char)(97+(int)(Rand()*25));
\r
481 #endif /* SERGEEV */
\r
483 if (strcmp(game.passwd, "debug")==0) idebug = 1;
\r
486 // Use parameters to generate initial values of things
\r
487 damfac = 0.5 * skill;
\r
488 game.state.rembase = 3.0*Rand()+2.0;
\r
489 inbase = game.state.rembase;
\r
490 inplan = (PLNETMAX/2) + (PLNETMAX/2+1)*Rand();
\r
491 game.state.nromrem = (2.0+Rand())*skill;
\r
492 game.state.nscrem = (skill > 2? 1 : 0);
\r
493 game.state.remtime = 7.0 * length;
\r
494 intime = game.state.remtime;
\r
495 game.state.remkl = 2.0*intime*((skill+1 - 2*Rand())*skill*0.1+.15);
\r
496 inkling = game.state.remkl;
\r
497 incom = skill + 0.0625*inkling*Rand();
\r
498 game.state.remcom= min(10, incom);
\r
499 incom = game.state.remcom;
\r
500 game.state.remres = (inkling+4*incom)*intime;
\r
501 inresor = game.state.remres;
\r
502 if (inkling > 50) {
\r
503 inbase = (game.state.rembase += 1);
\r
508 void dropin(int iquad, int *ix, int *iy) {
\r
510 while (game.quad[*ix][*iy] != IHDOT);
\r
511 game.quad[*ix][*iy] = iquad;
\r
514 void newcnd(void) {
\r
516 if (energy < 1000.0) condit = IHYELLOW;
\r
517 if (game.state.galaxy[quadx][quady] > 99 || game.state.newstuf[quadx][quady] > 9)
\r
519 if (!alive) condit=IHDEAD;
\r
523 void newqad(int shutup) {
\r
524 int quadnum = game.state.galaxy[quadx][quady];
\r
525 int newnum = game.state.newstuf[quadx][quady];
\r
526 int i, j, ix, iy, nplan;
\r
533 plnetx = plnety = 0;
\r
547 // Attempt to escape Super-commander, so tbeam back!
\r
552 for (i=1; i <= 10; i++)
\r
553 for (j=1; j <= 10; j++) game.quad[i][j] = IHDOT;
\r
554 // cope with supernova
\r
555 if (quadnum > 999) {
\r
558 klhere = quadnum/100;
\r
559 irhere = newnum/10;
\r
561 nenhere = klhere + irhere;
\r
563 // Position Starship
\r
564 game.quad[sectx][secty] = ship;
\r
566 if (quadnum >= 100) {
\r
567 // Position ordinary Klingons
\r
568 quadnum -= 100*klhere;
\r
569 for (i = 1; i <= klhere; i++) {
\r
570 dropin(IHK, &ix, &iy);
\r
573 game.kdist[i] = game.kavgd[i] = sqrt(square(sectx-ix) + square(secty-iy));
\r
574 game.kpower[i] = Rand()*150.0 +300.0 +25.0*skill;
\r
576 // If we need a commander, promote a Klingon
\r
577 for (i = 1; i <= game.state.remcom ; i++)
\r
578 if (game.state.cx[i]==quadx && game.state.cy[i]==quady) break;
\r
580 if (i <= game.state.remcom) {
\r
581 game.quad[ix][iy] = IHC;
\r
582 game.kpower[klhere] = 950.0+400.0*Rand()+50.0*skill;
\r
586 // If we need a super-commander, promote a Klingon
\r
587 if (quadx == game.state.isx && quady == game.state.isy) {
\r
588 game.quad[game.kx[1]][game.ky[1]] = IHS;
\r
589 game.kpower[1] = 1175.0 + 400.0*Rand() + 125.0*skill;
\r
590 iscate = game.state.remkl>1;
\r
594 // Put in Romulans if needed
\r
595 for (i = klhere+1; i <= nenhere; i++) {
\r
596 dropin(IHR, &ix, &iy);
\r
599 game.kdist[i] = game.kavgd[i] = sqrt(square(sectx-ix) + square(secty-iy));
\r
600 game.kpower[i] = Rand()*400.0 + 450.0 + 50.0*skill;
\r
603 // If quadrant needs a starbase, put it in
\r
604 if (quadnum >= 10) {
\r
606 dropin(IHB, &basex, &basey);
\r
610 // If quadrant needs a planet, put it in
\r
611 for (i=0; i < inplan; i++)
\r
612 if (game.state.plnets[i].x == quadx && game.state.plnets[i].y == quady) break;
\r
615 dropin(IHP, &plnetx, &plnety);
\r
618 // Check for condition
\r
620 // And finally the stars
\r
621 for (i = 1; i <= quadnum; i++) dropin(IHSTAR, &ix, &iy);
\r
624 if (irhere > 0 && klhere == 0) {
\r
626 if (game.damage[DRADIO] <= 0.0) {
\r
628 prout("LT. Uhura- \"Captain, an urgent message.");
\r
629 prout(" I'll put it on audio.\" CLICK");
\r
631 prout("INTRUDER! YOU HAVE VIOLATED THE ROMULAN NEUTRAL ZONE.");
\r
632 prout("LEAVE AT ONCE, OR YOU WILL BE DESTROYED!");
\r
637 // Put in THING if needed
\r
638 if (thingx == quadx && thingy == quady) {
\r
639 dropin(IHQUEST, &ix, &iy);
\r
640 iran8(&thingx, &thingy);
\r
643 game.kx[nenhere] = ix;
\r
644 game.ky[nenhere] = iy;
\r
645 game.kdist[nenhere] = game.kavgd[nenhere] =
\r
646 sqrt(square(sectx-ix) + square(secty-iy));
\r
647 game.kpower[nenhere] = Rand()*6000.0 +500.0 +250.0*skill;
\r
648 if (game.damage[DSRSENS] == 0.0) {
\r
650 prout("MR. SPOCK- \"Captain, this is most unusual.");
\r
651 prout(" Please examine your short-range scan.\"");
\r
656 // Decide if quadrant needs a Tholian
\r
657 if ((skill < 3 && Rand() <= 0.02) || /* Lighten up if skill is low */
\r
658 (skill == 3 && Rand() <= 0.05) ||
\r
659 (skill > 3 && Rand() <= 0.08)
\r
661 || strcmp(passwd, "tholianx")==0
\r
665 ithx = Rand() > 0.5 ? 10 : 1;
\r
666 ithy = Rand() > 0.5 ? 10 : 1;
\r
667 } while (game.quad[ithx][ithy] != IHDOT);
\r
668 game.quad[ithx][ithy] = IHT;
\r
671 game.kx[nenhere] = ithx;
\r
672 game.ky[nenhere] = ithy;
\r
673 game.kdist[nenhere] = game.kavgd[nenhere] =
\r
674 sqrt(square(sectx-ithx) + square(secty-ithy));
\r
675 game.kpower[nenhere] = Rand()*400.0 +100.0 +25.0*skill;
\r
676 /* Reserve unocupied corners */
\r
677 if (game.quad[1][1]==IHDOT) game.quad[1][1] = 'X';
\r
678 if (game.quad[1][10]==IHDOT) game.quad[1][10] = 'X';
\r
679 if (game.quad[10][1]==IHDOT) game.quad[10][1] = 'X';
\r
680 if (game.quad[10][10]==IHDOT) game.quad[10][10] = 'X';
\r
684 // Put in a few black holes
\r
685 for (i = 1; i <= 3; i++)
\r
686 if (Rand() > 0.5) dropin(IHBLANK, &ix, &iy);
\r
688 // Take out X's in corners if Tholian present
\r
690 if (game.quad[1][1]=='X') game.quad[1][1] = IHDOT;
\r
691 if (game.quad[1][10]=='X') game.quad[1][10] = IHDOT;
\r
692 if (game.quad[10][1]=='X') game.quad[10][1] = IHDOT;
\r
693 if (game.quad[10][10]=='X') game.quad[10][10] = IHDOT;
\r
697 void sortkl(void) {
\r
701 // The author liked bubble sort. So we will use it. :-(
\r
703 if (nenhere-iqhere-ithere < 2) return;
\r
707 for (j = 1; j < nenhere; j++)
\r
708 if (game.kdist[j] > game.kdist[j+1]) {
\r
711 game.kdist[j] = game.kdist[j+1];
\r
712 game.kdist[j+1] = t;
\r
714 game.kavgd[j] = game.kavgd[j+1];
\r
715 game.kavgd[j+1] = t;
\r
717 game.kx[j] = game.kx[j+1];
\r
720 game.ky[j] = game.ky[j+1];
\r
722 t = game.kpower[j];
\r
723 game.kpower[j] = game.kpower[j+1];
\r
724 game.kpower[j+1] = t;
\r