From d7ddf0df59133fd0ff3f49d0ed83079e3a695aeb Mon Sep 17 00:00:00 2001 From: "Eric S. Raymond" Date: Sun, 6 Feb 2005 16:24:55 +0000 Subject: [PATCH] Magic number elimination. Try to replace instances of 8 meaning the galaxy size with GALSIZE, and 10 meaning the quadrant size with QUADSIZE. It's not certain I got all of them, however. --- ai.c | 8 ++++---- battle.c | 10 +++++----- events.c | 28 ++++++++++++++-------------- moving.c | 36 ++++++++++++++++++------------------ planets.c | 4 ++-- reports.c | 26 +++++++++++++------------- setup.c | 30 +++++++++++++++--------------- sst.h | 22 ++++++++++++---------- 8 files changed, 83 insertions(+), 81 deletions(-) diff --git a/ai.c b/ai.c index ad550a1..5865c29 100644 --- a/ai.c +++ b/ai.c @@ -3,9 +3,9 @@ static int tryexit(int lookx, int looky, int ienm, int loccom, int irun) { int iqx, iqy, l; - iqx = quadx+(lookx+9)/10 - 1; - iqy = quady+(looky+9)/10 - 1; - if (iqx < 1 || iqx > 8 || iqy < 1 || iqy > 8 || + iqx = quadx+(lookx+(QUADSIZE-1))/QUADSIZE - 1; + iqy = quady+(looky+(QUADSIZE-1))/QUADSIZE - 1; + if (iqx < 1 || iqx > GALSIZE || iqy < 1 || iqy > GALSIZE || game.state.galaxy[iqx][iqy] > 899) return 0; /* no can do -- neg energy, supernovae, or >8 Klingons */ if (ienm == IHR) return 0; /* Romulans cannot escape! */ @@ -302,7 +302,7 @@ static int movescom(int iqx, int iqy, int flag, int *ipage) { int i; if ((iqx==quadx && iqy==quady) || - iqx < 1 || iqx > 8 || iqy < 1 || iqy > 8 || + iqx < 1 || iqx > GALSIZE || iqy < 1 || iqy > GALSIZE || game.state.galaxy[iqx][iqy] > 899) return 1; if (flag) { /* Avoid quadrants with bases if we want to avoid Enterprise */ diff --git a/battle.c b/battle.c index e405471..15ff3b3 100644 --- a/battle.c +++ b/battle.c @@ -193,10 +193,10 @@ void torpedo(double course, double r, int inx, int iny, double *hit, int wait, i for (l=1; l <= 15; l++) { x += deltax; ix = x + 0.5; - if (ix < 1 || ix > 10) break; + if (ix < 1 || ix > QUADSIZE) break; y += deltay; iy = y + 0.5; - if (iy < 1 || iy > 10) break; + if (iy < 1 || iy > QUADSIZE) break; iquad=game.quad[ix][iy]; tracktorpedo(x, y, ix, iy, wait, l, i, n, iquad); wait = 1; @@ -225,7 +225,7 @@ void torpedo(double course, double r, int inx, int iny, double *hit, int wait, i yy = cos(ang)/temp; jx=ix+xx+0.5; jy=iy+yy+0.5; - if (jx<1 || jx>10 || jy<1 ||jy > 10) return; + if (jx<1 || jx>QUADSIZE || jy<1 ||jy > QUADSIZE) return; if (game.quad[jx][jy]==IHBLANK) { finish(FHOLE); return; @@ -273,7 +273,7 @@ void torpedo(double course, double r, int inx, int iny, double *hit, int wait, i yy = cos(ang)/temp; jx=ix+xx+0.5; jy=iy+yy+0.5; - if (jx<1 || jx>10 || jy<1 ||jy > 10) { + if (jx<1 || jx>QUADSIZE || jy<1 ||jy > QUADSIZE) { prout(" damaged but not destroyed."); return; } @@ -678,7 +678,7 @@ void deadkl(int ix, int iy, int type, int ixx, int iyy) { static int targetcheck(double x, double y, double *course) { double deltx, delty; /* Return TRUE if target is invalid */ - if (x < 1.0 || x > 10.0 || y < 1.0 || y > 10.0) { + if (x < 1.0 || x > QUADSIZE || y < 1.0 || y > QUADSIZE) { huh(); return 1; } diff --git a/events.c b/events.c index fd9d53c..4923660 100644 --- a/events.c +++ b/events.c @@ -12,8 +12,8 @@ void events(void) { if (stdamtim == 1e30 && game.damage[DRADIO] != 0.0) { /* chart will no longer be updated because radio is dead */ stdamtim = game.state.date; - for (i=1; i <= 8 ; i++) - for (j=1; j <= 8; j++) + for (i=1; i <= GALSIZE ; i++) + for (j=1; j <= GALSIZE; j++) if (game.starch[i][j] == 1) game.starch[i][j] = game.state.galaxy[i][j]+1000; } @@ -58,8 +58,8 @@ void events(void) { prout("Lt. Uhura- \"Captain, the sub-space radio is working and"); prout(" surveillance reports are coming in."); skip(1); - for (i=1; i <= 8 ; i++) - for (j=1; j <= 8; j++) + for (i=1; i <= GALSIZE ; i++) + for (j=1; j <= GALSIZE; j++) if (game.starch[i][j] > 999) game.starch[i][j] = 1; if (iseenit==0) { attakreport(0); @@ -309,7 +309,7 @@ void events(void) { if (probecx != i || probecy != j) { probecx = i; probecy = j; - if (i < 1 || i > 8 || j < 1 || j > 8 || + if (i < 1 || i > GALSIZE || j < 1 || j > GALSIZE || game.state.galaxy[probecx][probecy] == 1000) { // Left galaxy or ran into supernova if (game.damage[DRADIO]==0.0 || condit == IHDOCKED) { @@ -317,7 +317,7 @@ void events(void) { ipage = 1; skip(1); proutn("Lt. Uhura- \"The deep space probe "); - if (i < 1 ||i > 8 || j < 1 || j > 8) + if (i < 1 ||i > GALSIZE || j < 1 || j > GALSIZE) proutn("has left the galaxy"); else proutn("is no longer transmitting"); @@ -438,7 +438,7 @@ void nova(int ix, int iy) { if (j==2 && nn== 2) continue; ii = hits[mm][1]+nn-2; jj = hits[mm][2]+j-2; - if (ii < 1 || ii > 10 || jj < 1 || jj > 10) continue; + if (ii < 1 || ii > QUADSIZE || jj < 1 || jj > QUADSIZE) continue; iquad = game.quad[ii][jj]; switch (iquad) { // case IHDOT: /* Empty space ends reaction @@ -531,7 +531,7 @@ void nova(int ix, int iy) { newcy = jj + jj - hits[mm][2]; crmena(1, iquad, 2, ii, jj); proutn(" damaged"); - if (newcx<1 || newcx>10 || newcy<1 || newcy>10) { + if (newcx<1 || newcx>QUADSIZE || newcy<1 || newcy>QUADSIZE) { /* can't leave quadrant */ skip(1); break; @@ -603,15 +603,15 @@ void snova(int insx, int insy) { /* Scheduled supernova -- select star */ /* logic changed here so that we won't favor quadrants in top left of universe */ - for (nqx = 1; nqx<=8; nqx++) { - for (nqy = 1; nqy<=8; nqy++) { + for (nqx = 1; nqx<=GALSIZE; nqx++) { + for (nqy = 1; nqy<=GALSIZE; nqy++) { stars += game.state.galaxy[nqx][nqy] % 10; } } if (stars == 0) return; /* nothing to supernova exists */ num = Rand()*stars + 1; - for (nqx = 1; nqx<=8; nqx++) { - for (nqy = 1; nqy<=8; nqy++) { + for (nqx = 1; nqx<=GALSIZE; nqx++) { + for (nqy = 1; nqy<=GALSIZE; nqy++) { num -= game.state.galaxy[nqx][nqy] % 10; if (num <= 0) break; } @@ -641,8 +641,8 @@ void snova(int insx, int insy) { /* we are in the quadrant! */ insipient = 1; num = Rand()* (game.state.galaxy[nqx][nqy]%10) + 1; - for (nsx=1; nsx < 10; nsx++) { - for (nsy=1; nsy < 10; nsy++) { + for (nsx=1; nsx < QUADSIZE; nsx++) { + for (nsy=1; nsy < QUADSIZE; nsy++) { if (game.quad[nsx][nsy]==IHSTAR) { num--; if (num==0) break; diff --git a/moving.c b/moving.c index 887977a..ac368e1 100644 --- a/moving.c +++ b/moving.c @@ -42,7 +42,7 @@ void imove(void) { for (l = 1; l <= n; l++) { ix = (x += deltax) + 0.5; iy = (y += deltay) + 0.5; - if (ix < 1 || ix > 10 || iy < 1 || iy > 10) { + if (ix < 1 || ix > QUADSIZE || iy < 1 || iy > QUADSIZE) { /* Leaving quadrant -- allow final enemy attack */ /* Don't do it if being pushed by Nova */ if (nenhere != 0 && iattak != 2) { @@ -62,8 +62,8 @@ void imove(void) { if (alldone) return; } /* compute final position -- new quadrant and sector */ - x = 10*(quadx-1)+sectx; - y = 10*(quady-1)+secty; + x = QUADSIZE*(quadx-1)+sectx; + y = QUADSIZE*(quady-1)+secty; ix = x+10.0*dist*bigger*deltax+0.5; iy = y+10.0*dist*bigger*deltay+0.5; /* check for edge of galaxy */ @@ -78,11 +78,11 @@ void imove(void) { iy = -iy + 1; kink = 1; } - if (ix > 80) { + if (ix > GALSIZE*QUADSIZE) { ix = 161 - ix; kink = 1; } - if (iy > 80) { + if (iy > GALSIZE*QUADSIZE) { iy = 161 - iy; kink = 1; } @@ -102,10 +102,10 @@ void imove(void) { } /* Compute final position in new quadrant */ if (trbeam) return; /* Don't bother if we are to be beamed */ - quadx = (ix+9)/10; - quady = (iy+9)/10; - sectx = ix - 10*(quadx-1); - secty = iy - 10*(quady-1); + quadx = (ix+9)/QUADSIZE; + quady = (iy+9)/QUADSIZE; + sectx = ix - QUADSIZE*(quadx-1); + secty = iy - QUADSIZE*(quady-1); prout("\n\rEntering %s.", cramlc(quadrant, quadx, quady)); game.quad[sectx][secty] = ship; @@ -373,8 +373,8 @@ static void getcd(int isprobe, int akey) { } itemp = 1; } - if (irowq<1 || irowq > 8 || icolq<1 || icolq > 8 || - irows<1 || irows > 10 || icols<1 || icols > 10) { + if (irowq<1 || irowq > GALSIZE || icolq<1 || icolq > GALSIZE || + irows<1 || irows > QUADSIZE || icols<1 || icols > QUADSIZE) { huh(); return; } @@ -588,10 +588,10 @@ void warp(int i) { for (l = 1; l <= n; l++) { x += deltax; ix = x + 0.5; - if (ix < 1 || ix > 10) break; + if (ix < 1 || ix > QUADSIZE) break; y += deltay; iy = y +0.5; - if (iy < 1 || iy > 10) break; + if (iy < 1 || iy > QUADSIZE) break; if (game.quad[ix][iy] != IHDOT) { blooey = 0; twarp = 0; @@ -803,8 +803,8 @@ void timwrp() { /* Revert star chart to earlier era, if it was known then*/ if (game.damage[DRADIO]==0.0 || stdamtim > game.state.date) { - for (l = 1; l <= 8; l++) - for (ll = 1; ll <= 8; ll++) + for (l = 1; l <= GALSIZE; l++) + for (ll = 1; ll <= GALSIZE; ll++) if (game.starch[l][ll] > 1) game.starch[l][ll]=game.damage[DRADIO]>0.0 ? game.state.galaxy[l][ll]+1000 :1; prout("Spock has reconstructed a correct star chart from memory"); @@ -885,8 +885,8 @@ void probe(void) { probeiny /= bigger; probeinx /= bigger; proben = 10.0*dist*bigger +0.5; - probex = quadx*10 + sectx - 1; // We will use better packing than original - probey = quady*10 + secty - 1; + probex = quadx*QUADSIZE + sectx - 1; // We will use better packing than original + probey = quady*QUADSIZE + secty - 1; probecx = quadx; probecy = quady; game.future[FDSPROB] = game.state.date + 0.01; // Time to move one sector @@ -950,7 +950,7 @@ void help(void) { for (l = 1; l <= 5; l++) { ix = basex+3.0*Rand()-1; iy = basey+3.0*Rand()-1; - if (ix>=1 && ix<=10 && iy>=1 && iy<=10 && game.quad[ix][iy]==IHDOT) { + if (ix>=1 && ix<=QUADSIZE && iy>=1 && iy<=QUADSIZE && game.quad[ix][iy]==IHDOT) { /* found one -- finish up */ sectx=ix; secty=iy; diff --git a/planets.c b/planets.c index 35ccec7..322fb1e 100644 --- a/planets.c +++ b/planets.c @@ -476,8 +476,8 @@ void deathray(void) { proutn("Spock- \"I believe the word is"); prouts(" *ASTONISHING*"); prout(" Mr. Sulu."); - for (i=1; i<=10; i++) - for (j=1; j<=10; j++) + for (i=1; i<=QUADSIZE; i++) + for (j=1; j<=QUADSIZE; j++) if (game.quad[i][j] == IHDOT) game.quad[i][j] = IHQUEST; prout(" Captain, our quadrant is now infested with"); prouts(" - - - - - - *THINGS*."); diff --git a/reports.c b/reports.c index 6424bae..4c3fd33 100644 --- a/reports.c +++ b/reports.c @@ -129,7 +129,7 @@ void lrscan(void) { for (x = quadx-1; x <= quadx+1; x++) { proutn(" "); for (y = quady-1; y <= quady+1; y++) { - if (x == 0 || x > 8 || y == 0 || y > 8) + if (x == 0 || x > GALSIZE || y == 0 || y > GALSIZE) proutn(" -1"); else { if (game.state.galaxy[x][y]<1000) proutn(" %3d", game.state.galaxy[x][y]); @@ -173,8 +173,8 @@ void chart(int nn) { if (condit == IHDOCKED) { /* We are docked, so restore chart from base information */ stdamtim = game.state.date; - for (i=1; i <= 8 ; i++) - for (j=1; j <= 8; j++) + for (i=1; i <= GALSIZE ; i++) + for (j=1; j <= GALSIZE; j++) if (game.starch[i][j] == 1) game.starch[i][j] = game.state.galaxy[i][j]+1000; } else { @@ -184,9 +184,9 @@ void chart(int nn) { } prout(" 1 2 3 4 5 6 7 8"); - for (i = 1; i <= 8; i++) { + for (i = 1; i <= GALSIZE; i++) { proutn("%d |", i); - for (j = 1; j <= 8; j++) { + for (j = 1; j <= GALSIZE; j++) { char buf[4]; proutn(" "); if (game.starch[i][j] < 0) @@ -203,7 +203,7 @@ void chart(int nn) { proutn(buf); } proutn(" |"); - if (i<8) proutn("\n\r"); + if (i 10) { + if (k > sizeof(requests)/sizeof(requests[0])) { prout("UNRECOGNIZED REQUEST. Legal requests are:\n" " date, condition, position, lsupport, warpfactor,\n" " energy, torpedoes, shields, klingons, time."); @@ -346,7 +346,7 @@ int srscan(int l) { break; } } - if (i<10) proutn("\n\r"); + if (iix1) ix2 = 1; - else ix2=10; + else ix2=QUADSIZE; if (quadx>iy1) iy2 = 1; - else iy2=10; + else iy2=QUADSIZE; } - if (ix1 > 8 || ix1 < 1 || iy1 > 8 || iy1 < 1 || - ix2 > 10 || ix2 < 1 || iy2 > 10 || iy2 < 1) { + if (ix1 > GALSIZE || ix1 < 1 || iy1 > GALSIZE || iy1 < 1 || + ix2 > QUADSIZE || ix2 < 1 || iy2 > QUADSIZE || iy2 < 1) { huh(); return; } diff --git a/setup.c b/setup.c index dff55e8..0ecac03 100644 --- a/setup.c +++ b/setup.c @@ -154,11 +154,11 @@ 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 */ @@ -222,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)); @@ -237,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; @@ -525,8 +525,8 @@ 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; @@ -650,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(); @@ -663,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; } } diff --git a/sst.h b/sst.h index 95ef551..beeee5c 100644 --- a/sst.h +++ b/sst.h @@ -18,6 +18,8 @@ #define PHASEFAC (2.0) #define PLNETMAX (10) #define NEVENTS (8) +#define GALSIZE (8) +#define QUADSIZE (10) typedef struct { int x; /* Quadrant location of planet */ @@ -38,11 +40,11 @@ typedef struct { basekl, // destroyed bases killk, // Klingons killed killc, // commanders killed - galaxy[9][9], // The Galaxy (subscript 0 not used) - cx[11],cy[11], // Commander quadrant coordinates + galaxy[GALSIZE+1][GALSIZE+1], // The Galaxy (subscript 0 not used) + cx[QUADSIZE+1],cy[QUADSIZE+1], // Commander quadrant coordinates baseqx[6], // Base quadrant X baseqy[6], // Base quadrant Y - newstuf[9][9], // Extended galaxy goodies + newstuf[GALSIZE+1][GALSIZE+1], // Extended galaxy goodies isx, isy, // Coordinate of Super Commander nscrem, // remaining super commanders nromkl, // Romulans killed @@ -68,16 +70,16 @@ EXTERN struct { char magic[sizeof(SSTMAGIC)]; snapshot state; snapshot snapsht; - char quad[11][11]; // contents of our quadrant - double kpower[21]; // enemy energy levels - double kdist[21]; // enemy distances - double kavgd[21]; // average distances + char quad[QUADSIZE+1][QUADSIZE+1]; // contents of our quadrant + double kpower[(QUADSIZE+1)*(QUADSIZE+1)]; // enemy energy levels + double kdist[(QUADSIZE+1)*(QUADSIZE+1)]; // enemy distances + double kavgd[(QUADSIZE+1)*(QUADSIZE+1)]; // average distances double damage[NDEVICES+1]; // damage encountered double future[NEVENTS+1]; // future events char passwd[10]; // Self Destruct password - int kx[21]; // enemy sector locations - int ky[21]; - int starch[9][9]; // star chart + int kx[(QUADSIZE+1)*(QUADSIZE+1)]; // enemy sector locations + int ky[(QUADSIZE+1)*(QUADSIZE+1)]; + int starch[GALSIZE+1][GALSIZE+1]; // star chart /* members with macro definitions start here */ int inkling, inbase, -- 2.31.1