More magic-number chasing.
[super-star-trek.git] / setup.c
diff --git a/setup.c b/setup.c
index 3a5a794b105b228ccc2f06ea0d3f5bec2ff2d517..fb12ed86bc1e445f30e6971767a593aeb3a66360 100644 (file)
--- a/setup.c
+++ b/setup.c
@@ -1,11 +1,7 @@
 #include <time.h>
-#ifdef SERGEEV
 #include <sys/stat.h>
-#include <conio.h>
-#endif /* SERGEEV */
 #include "sst.h"
 
-#ifdef SERGEEV
 #ifdef __linux__
 static long filelength(int fd) {
 struct stat buf;
@@ -13,16 +9,15 @@ struct stat buf;
     return buf.st_size;
 }
 #endif
-#endif /* SERGEEV */
 
 void prelim(void) {
        skip(2);
        prout("-SUPER- STAR TREK");
        skip(1);
-#ifndef SERGEEV
+#ifdef __HISTORICAL__
        prout("Latest update-21 Sept 78");
        skip(1);
-#endif /* SERGEEV */
+#endif /* __HISTORICAL__ */
 }
 
 void freeze(int boss) {
@@ -84,7 +79,7 @@ int thaw(void) {
                return 1;
        }
        fread(&game, sizeof(game), 1, fp);
-       if (strcmp(game.magic, SSTMAGIC)) {
+       if (feof(fp) || ftell(fp) != filelength(fileno(fp)) || strcmp(game.magic, SSTMAGIC)) {
                prout("Game file format is bad, should begin with " SSTMAGIC);
                skip(1);
                fclose(fp);
@@ -159,16 +154,16 @@ void abandn(void) {
                for (;;) {
                        /* position next to base by trial and error */
                        game.quad[sectx][secty] = IHDOT;
-                       for (l = 1; l <= 10; l++) {
+                       for (l = 1; l <= QUADSIZE; l++) {
                                sectx = 3.0*Rand() - 1.0 + basex;
                                secty = 3.0*Rand() - 1.0 + basey;
-                               if (sectx >= 1 && sectx <= 10 &&
-                                       secty >= 1 && secty <= 10 &&
+                               if (sectx >= 1 && sectx <= QUADSIZE &&
+                                       secty >= 1 && secty <= QUADSIZE &&
                                        game.quad[sectx][secty] == IHDOT) break;
                        }
-                       if (l < 11) break; /* found a spot */
-                       sectx=5;
-                       secty=5;
+                       if (l < QUADSIZE+1) break; /* found a spot */
+                       sectx=QUADSIZE/2;
+                       secty=QUADSIZE/2;
                        newqad(1);
                }
        }
@@ -227,8 +222,8 @@ void setup(int needprompt) {
        landed = -1;
        alive = 1;
        docfac = 0.25;
-       for (i = 1; i <= 8; i++)
-               for (j = 1; j <= 8; j++) game.state.newstuf[i][j] = game.starch[i][j] = 0;
+       for (i = 1; i <= GALSIZE; i++)
+               for (j = 1; j <= GALSIZE; j++) game.state.newstuf[i][j] = game.starch[i][j] = 0;
        // Initialize times for extraneous events
        game.future[FSNOVA] = game.state.date + expran(0.5 * intime);
        game.future[FTBEAM] = game.state.date + expran(1.5 * (intime / game.state.remcom));
@@ -242,8 +237,8 @@ void setup(int needprompt) {
        stdamtim = 1e30;
        // Put stars in the galaxy
        instar = 0;
-       for (i=1; i<=8; i++)
-               for (j=1; j<=8; j++) {
+       for (i=1; i<=GALSIZE; i++)
+               for (j=1; j<=GALSIZE; j++) {
                        int k = Rand()*9.0 + 1.0;
                        instar += k;
                        game.state.galaxy[i][j] = k;
@@ -253,12 +248,12 @@ void setup(int needprompt) {
                int contflag;
                do {
                        do iran8(&ix, &iy);
-                       while (game.state.galaxy[ix][iy] >= 10);
+                       while (game.state.galaxy[ix][iy] >= BASE_PLACE);
                        contflag = FALSE;
                        for (j = i-1; j > 0; j--) {
                                /* Improved placement algorithm to spread out bases */
                                double distq = square(ix-game.state.baseqx[j]) + square(iy-game.state.baseqy[j]);
-                               if (distq < 6.0*(6-inbase) && Rand() < 0.75) {
+                               if (distq < 6.0*(BASEMAX-inbase) && Rand() < 0.75) {
                                        contflag = TRUE;
 #ifdef DEBUG
                                        proutn("DEBUG: Abandoning base #%d at %d-%d\n", i, ix, iy);
@@ -266,7 +261,7 @@ void setup(int needprompt) {
                                        break;
                                }
 #ifdef DEBUG
-                               else if (distq < 6.0 * (6-inbase)) {
+                               else if (distq < 6.0 * (BASEMAX-inbase)) {
                                        proutn("DEBUG: saving base #%d, close to #%d\n", i, j);
                                }
 #endif
@@ -276,7 +271,7 @@ void setup(int needprompt) {
                game.state.baseqx[i] = ix;
                game.state.baseqy[i] = iy;
                game.starch[ix][iy] = -1;
-               game.state.galaxy[ix][iy] += 10;
+               game.state.galaxy[ix][iy] += BASE_PLACE;
        }
        // Position ordinary Klingon Battle Cruisers
        krem = inkling - incom - game.state.nscrem;
@@ -289,7 +284,7 @@ void setup(int needprompt) {
                krem -= klump;
                klump *= 100;
                do iran8(&ix, &iy);
-               while (game.state.galaxy[ix][iy] + klump >= 1000);
+               while (game.state.galaxy[ix][iy] + klump >= SUPERNOVA_PLACE);
                game.state.galaxy[ix][iy] += klump;
        } while (krem > 0);
        // Position Klingon Commander Ships
@@ -315,7 +310,7 @@ void setup(int needprompt) {
                        for (j = 1; j < i; j++)
                                if (game.state.cx[j]==ix && game.state.cy[j]==iy) break;
                } while (j < i);
-               game.state.galaxy[ix][iy] += 100;
+               game.state.galaxy[ix][iy] += ENEMY_PLACE;
                game.state.cx[i] = ix;
                game.state.cy[i] = iy;
        }
@@ -333,7 +328,7 @@ void setup(int needprompt) {
        // Locate Romulans
        for (i = 1; i <= game.state.nromrem; i++) {
                iran8(&ix, &iy);
-               game.state.newstuf[ix][iy] += 10;
+               game.state.newstuf[ix][iy] += ROMULAN_PLACE;
        }
        // Locate the Super Commander
        if (game.state.nscrem > 0) {
@@ -341,7 +336,7 @@ void setup(int needprompt) {
                while (game.state.galaxy[ix][iy] >= 900);
                game.state.isx = ix;
                game.state.isy = iy;
-               game.state.galaxy[ix][iy] += 100;
+               game.state.galaxy[ix][iy] += ENEMY_PLACE;
        }
        // Place thing (in tournament game, thingx == -1, don't want one!)
        if (thingx != -1) {
@@ -389,9 +384,7 @@ void setup(int needprompt) {
        skip(2);
        prout("Good Luck!");
        if (game.state.nscrem) prout("  YOU'LL NEED IT.");
-#ifdef SERGEEV
-        getche();
-#endif /* SERGEEV */
+        waitfor();
        newqad(0);
         if (nenhere-iqhere-ithere) shldup=1.0;
        if (neutz) attack(0);   // bad luck to start in a Romulan Neutral Zone
@@ -402,13 +395,13 @@ void randomize(void) {
 }
 
 int choose(int needprompt) {
+        while (TRUE) {
        tourn = 0;
        thawed = 0;
        skill = 0;
        length = 0;
-       while (TRUE) {
                if (needprompt) /* Can start with command line options */
-                   proutn("Would you like a regular, tournament, or frozen game?");
+                        proutn("Would you like a regular, tournament, or saved game? ");
                scan();
                if (strlen(citem)==0) continue; // Try again
                if (isit("tournament")) {
@@ -424,22 +417,16 @@ int choose(int needprompt) {
                        srand((unsigned int)(int)aaitem);
                        break;
                }
-               if (isit("frozen")) {
-                       thaw();
+               if (isit("saved") || isit("frozen")) {
+                        if (thaw()) continue;
                        chew();
                        if (*game.passwd==0) continue;
-                       randomize();
-                       Rand(); Rand(); Rand(); Rand();
                        if (!alldone) thawed = 1; // No plaque if not finished
-                       report(1);
+                        report();
+                        waitfor();
                        return TRUE;
                }
-               if (isit("regular")) {
-                       skip(2);
-                       randomize();
-                       Rand(); Rand(); Rand(); Rand();
-                       break;
-               }
+                if (isit("regular")) break;
                proutn("What is \"");
                proutn(citem);
                prout("\"?");
@@ -467,18 +454,7 @@ int choose(int needprompt) {
                        else if (skill == 0) proutn("Are you a Novice, Fair, Good, Expert, or Emeritus player? ");
                }
        }
-#ifndef SERGEEV
-       while (TRUE) {
-               scan();
-               strcpy(game.passwd, citem);
-               chew();
-               if (*game.passwd != 0) break;
-               proutn("Please type in a secret password-");
-       }
-#else
-        for(i=0;i<3;i++) game.passwd[i]=(char)(97+(int)(Rand()*25));
-        game.passwd[3]=0;
-#endif /* SERGEEV */
+       setpassword();
 #ifdef DEBUG
        if (strcmp(game.passwd, "debug")==0) idebug = 1;
 #endif
@@ -514,7 +490,7 @@ void dropin(int iquad, int *ix, int *iy) {
 void newcnd(void) {
        condit = IHGREEN;
        if (energy < 1000.0) condit = IHYELLOW;
-       if (game.state.galaxy[quadx][quady] > 99 || game.state.newstuf[quadx][quady] > 9)
+       if (game.state.galaxy[quadx][quady] >= ENEMY_PLACE || game.state.newstuf[quadx][quady] > 9)
                condit = IHRED;
         if (!alive) condit=IHDEAD;
 }
@@ -549,23 +525,23 @@ void newqad(int shutup) {
                ientesc = 1;
        }
        // Clear quadrant
-       for (i=1; i <= 10; i++)
-               for (j=1; j <= 10; j++) game.quad[i][j] = IHDOT;
+       for (i=1; i <= QUADSIZE; i++)
+               for (j=1; j <= QUADSIZE; j++) game.quad[i][j] = IHDOT;
        // cope with supernova
        if (quadnum > 999) {
                return;
        }
-       klhere = quadnum/100;
-       irhere = newnum/10;
+       klhere = quadnum/ENEMY_PLACE;
+       irhere = newnum/ROMULAN_PLACE;
        nplan = newnum%10;
        nenhere = klhere + irhere;
 
        // Position Starship
        game.quad[sectx][secty] = ship;
 
-       if (quadnum >= 100) {
+       if (quadnum >= ENEMY_PLACE) {
                // Position ordinary Klingons
-               quadnum -= 100*klhere;
+               quadnum -= ENEMY_PLACE*klhere;
                for (i = 1; i <= klhere; i++) {
                        dropin(IHK, &ix, &iy);
                        game.kx[i] = ix;
@@ -599,10 +575,9 @@ void newqad(int shutup) {
                game.kdist[i] = game.kavgd[i] = sqrt(square(sectx-ix) + square(secty-iy));
                game.kpower[i] = Rand()*400.0 + 450.0 + 50.0*skill;
        }
-       sortkl();
        // If quadrant needs a starbase, put it in
-       if (quadnum >= 10) {
-               quadnum -= 10;
+       if (quadnum >= BASE_PLACE) {
+               quadnum -= BASE_PLACE;
                dropin(IHB, &basex, &basey);
        }
        
@@ -662,8 +637,8 @@ void newqad(int shutup) {
 #endif
                 ) {
                 do {
-                        ithx = Rand() > 0.5 ? 10 : 1;
-                        ithy = Rand() > 0.5 ? 10 : 1;
+                        ithx = Rand() > 0.5 ? QUADSIZE : 1;
+                        ithy = Rand() > 0.5 ? QUADSIZE : 1;
                 } while (game.quad[ithx][ithy] != IHDOT);
                 game.quad[ithx][ithy] = IHT;
                 ithere = 1;
@@ -675,9 +650,9 @@ void newqad(int shutup) {
                 game.kpower[nenhere] = Rand()*400.0 +100.0 +25.0*skill;
                 /* Reserve unocupied corners */
                 if (game.quad[1][1]==IHDOT) game.quad[1][1] = 'X';
-                if (game.quad[1][10]==IHDOT) game.quad[1][10] = 'X';
-                if (game.quad[10][1]==IHDOT) game.quad[10][1] = 'X';
-                if (game.quad[10][10]==IHDOT) game.quad[10][10] = 'X';
+                if (game.quad[1][QUADSIZE]==IHDOT) game.quad[1][QUADSIZE] = 'X';
+                if (game.quad[QUADSIZE][1]==IHDOT) game.quad[QUADSIZE][1] = 'X';
+                if (game.quad[QUADSIZE][QUADSIZE]==IHDOT) game.quad[QUADSIZE][QUADSIZE] = 'X';
         }
         sortkl();
 
@@ -688,9 +663,9 @@ void newqad(int shutup) {
        // Take out X's in corners if Tholian present
        if (ithere) {
                if (game.quad[1][1]=='X') game.quad[1][1] = IHDOT;
-               if (game.quad[1][10]=='X') game.quad[1][10] = IHDOT;
-               if (game.quad[10][1]=='X') game.quad[10][1] = IHDOT;
-               if (game.quad[10][10]=='X') game.quad[10][10] = IHDOT;
+               if (game.quad[1][QUADSIZE]=='X') game.quad[1][QUADSIZE] = IHDOT;
+               if (game.quad[QUADSIZE][1]=='X') game.quad[QUADSIZE][1] = IHDOT;
+               if (game.quad[QUADSIZE][QUADSIZE]=='X') game.quad[QUADSIZE][QUADSIZE] = IHDOT;
        }               
 }