From 760ecf730c119682b414153a629ec2adc224434f Mon Sep 17 00:00:00 2001 From: "Eric S. Raymond" Date: Sun, 6 Feb 2005 17:16:42 +0000 Subject: [PATCH 1/1] More magic-number chasing. --- ai.c | 10 +++++----- battle.c | 6 +++--- events.c | 32 ++++++++++++++++---------------- moving.c | 10 +++++----- planets.c | 2 +- reports.c | 10 +++++----- setup.c | 36 ++++++++++++++++++------------------ sst.c | 4 ++-- sst.h | 12 ++++++++++++ 9 files changed, 67 insertions(+), 55 deletions(-) diff --git a/ai.c b/ai.c index 1367f43..0fe92c8 100644 --- a/ai.c +++ b/ai.c @@ -39,8 +39,8 @@ static int tryexit(int lookx, int looky, int ienm, int loccom, int irun) { nenhere--; if (condit != IHDOCKED) newcnd(); /* Handle global matters related to escape */ - game.state.galaxy[quadx][quady] -= 100; - game.state.galaxy[iqx][iqy] += 100; + game.state.galaxy[quadx][quady] -= ENEMY_PLACE; + game.state.galaxy[iqx][iqy] += ENEMY_PLACE; if (ienm==IHS) { ishere=0; iscate=0; @@ -311,10 +311,10 @@ static int movescom(int iqx, int iqy, int flag, int *ipage) { } if (justin && !iscate) return 1; /* do the move */ - game.state.galaxy[game.state.isx][game.state.isy] -= 100; + game.state.galaxy[game.state.isx][game.state.isy] -= ENEMY_PLACE; game.state.isx = iqx; game.state.isy = iqy; - game.state.galaxy[game.state.isx][game.state.isy] += 100; + game.state.galaxy[game.state.isx][game.state.isy] += ENEMY_PLACE; if (ishere) { /* SC has scooted, Remove him from current quadrant */ iscate=0; @@ -588,7 +588,7 @@ void movetho(void) { game.ky[nenhere]=ithy; /* check to see if all holes plugged */ - for (i = 1; i < 11; i++) { + for (i = 1; i < QUADSIZE+1; i++) { if (game.quad[1][i]!=IHWEB && game.quad[1][i]!=IHT) return; if (game.quad[QUADSIZE][i]!=IHWEB && game.quad[QUADSIZE][i]!=IHT) return; if (game.quad[i][1]!=IHWEB && game.quad[i][1]!=IHT) return; diff --git a/battle.c b/battle.c index 15ff3b3..36a6cfa 100644 --- a/battle.c +++ b/battle.c @@ -306,7 +306,7 @@ void torpedo(double course, double r, int inx, int iny, double *hit, int wait, i game.quad[ix][iy]=IHDOT; game.state.rembase--; basex=basey=0; - game.state.galaxy[quadx][quady] -= 10; + game.state.galaxy[quadx][quady] -= BASE_PLACE; game.state.basekl++; newcnd(); return; @@ -513,7 +513,7 @@ void attack(int k) { r += 0.002*game.kpower[l]*r; torpedo(course, r, jx, jy, &hit, 0, 1, 1); if (game.state.remkl==0) finish(FWON); /* Klingons did themselves in! */ - if (game.state.galaxy[quadx][quady] == 1000 || + if (game.state.galaxy[quadx][quady] == SUPERNOVA_PLACE || alldone) return; /* Supernova or finished */ if (hit == 0) continue; } @@ -602,7 +602,7 @@ void deadkl(int ix, int iy, int type, int ixx, int iyy) { /* Decide what kind of enemy it is and update approriately */ if (type == IHR) { /* chalk up a Romulan */ - game.state.newstuf[quadx][quady] -= 10; + game.state.newstuf[quadx][quady] -= ROMULAN_PLACE; irhere--; game.state.nromkl++; game.state.nromrem--; diff --git a/events.c b/events.c index 51470c1..c1a5632 100644 --- a/events.c +++ b/events.c @@ -14,7 +14,7 @@ void events(void) { stdamtim = game.state.date; 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; + if (game.starch[i][j] == 1) game.starch[i][j] = game.state.galaxy[i][j]+SUPERNOVA_PLACE; } for (;;) { @@ -77,7 +77,7 @@ void events(void) { ipage=1; snova(0,0); game.future[FSNOVA] = game.state.date + expran(0.5*intime); - if (game.state.galaxy[quadx][quady] == 1000) return; + if (game.state.galaxy[quadx][quady] == SUPERNOVA_PLACE) return; break; case FSPY: /* Check with spy to see if S.C. should tractor beam */ if (game.state.nscrem == 0 || @@ -233,7 +233,7 @@ void events(void) { case FSCDBAS: /* Supercommander destroys base */ game.future[FSCDBAS] = 1e30; isatb = 2; - if (game.state.galaxy[game.state.isx][game.state.isy]%100 < 10) break; /* WAS RETURN! */ + if (game.state.galaxy[game.state.isx][game.state.isy]%ENEMY_PLACE < BASE_PLACE) break; /* WAS RETURN! */ ixhold = batx; iyhold = baty; batx = game.state.isx; @@ -245,7 +245,7 @@ void events(void) { for (i = 1; i <= game.state.remcom; i++) if (game.state.cx[i]==batx && game.state.cy[i]==baty) break; if (i > game.state.remcom || game.state.rembase == 0 || - game.state.galaxy[batx][baty] % 100 < 10) { + game.state.galaxy[batx][baty] % ENEMY_PLACE < BASE_PLACE) { /* No action to take after all */ batx = baty = 0; break; @@ -256,7 +256,7 @@ void events(void) { if (game.starch[batx][baty] == -1) game.starch[batx][baty] = 0; /* Handle case where base is in same quadrant as starship */ if (batx==quadx && baty==quady) { - if (game.starch[batx][baty] > 999) game.starch[batx][baty] -= 10; + if (game.starch[batx][baty] >= SUPERNOVA_PLACE) game.starch[batx][baty] -= BASE_PLACE; game.quad[basex][basey]= IHDOT; basex=basey=0; newcnd(); @@ -277,7 +277,7 @@ void events(void) { else prout("a Klingon Commander"); } /* Remove Starbase from galaxy */ - game.state.galaxy[batx][baty] -= 10; + game.state.galaxy[batx][baty] -= BASE_PLACE; for (i=1; i <= game.state.rembase; i++) if (game.state.baseqx[i]==batx && game.state.baseqy[i]==baty) { game.state.baseqx[i]=game.state.baseqx[game.state.rembase]; @@ -310,7 +310,7 @@ void events(void) { probecx = i; probecy = j; if (i < 1 || i > GALSIZE || j < 1 || j > GALSIZE || - game.state.galaxy[probecx][probecy] == 1000) { + game.state.galaxy[probecx][probecy] == SUPERNOVA_PLACE) { // Left galaxy or ran into supernova if (game.damage[DRADIO]==0.0 || condit == IHDOCKED) { if (ipage==0) pause_game(1); @@ -339,14 +339,14 @@ void events(void) { radio. */ if (game.damage[DRADIO] == 0.0 || condit == IHDOCKED) game.starch[probecx][probecy] = game.damage[DRADIO] > 0.0 ? - game.state.galaxy[probecx][probecy]+1000 : 1; + game.state.galaxy[probecx][probecy]+SUPERNOVA_PLACE : 1; proben--; // One less to travel if (proben == 0 && isarmed && - game.state.galaxy[probecx][probecy] % 10 > 0) { + game.state.galaxy[probecx][probecy] % BASE_PLACE > 0) { /* lets blow the sucker! */ snova(1,0); game.future[FDSPROB] = 1e30; - if (game.state.galaxy[quadx][quady] == 1000) return; + if (game.state.galaxy[quadx][quady] == SUPERNOVA_PLACE) return; } break; } @@ -401,7 +401,7 @@ void wait(void) { /* Repair Deathray if long rest at starbase */ if (origTime-delay >= 9.99 && condit == IHDOCKED) game.damage[DDRAY] = 0.0; - } while (game.state.galaxy[quadx][quady] != 1000); // leave if quadrant supernovas + } while (game.state.galaxy[quadx][quady] != SUPERNOVA_PLACE); // leave if quadrant supernovas resting = 0; Time = 0; @@ -477,7 +477,7 @@ void nova(int ix, int iy) { game.quad[ii][jj] = IHDOT; break; case IHB: /* Destroy base */ - game.state.galaxy[quadx][quady] -= 10; + game.state.galaxy[quadx][quady] -= BASE_PLACE; for (i = 1; i <= game.state.rembase; i++) if (game.state.baseqx[i]==quadx && game.state.baseqy[i]==quady) break; game.state.baseqx[i] = game.state.baseqx[game.state.rembase]; @@ -605,14 +605,14 @@ void snova(int insx, int insy) { left of universe */ for (nqx = 1; nqx<=GALSIZE; nqx++) { for (nqy = 1; nqy<=GALSIZE; nqy++) { - stars += game.state.galaxy[nqx][nqy] % 10; + stars += game.state.galaxy[nqx][nqy] % BASE_PLACE; } } if (stars == 0) return; /* nothing to supernova exists */ num = Rand()*stars + 1; for (nqx = 1; nqx<=GALSIZE; nqx++) { for (nqy = 1; nqy<=GALSIZE; nqy++) { - num -= game.state.galaxy[nqx][nqy] % 10; + num -= game.state.galaxy[nqx][nqy] % BASE_PLACE; if (num <= 0) break; } if (num <=0) break; @@ -640,7 +640,7 @@ void snova(int insx, int insy) { else { /* we are in the quadrant! */ insipient = 1; - num = Rand()* (game.state.galaxy[nqx][nqy]%10) + 1; + num = Rand()* (game.state.galaxy[nqx][nqy]%BASE_PLACE) + 1; for (nsx=1; nsx < QUADSIZE; nsx++) { for (nsy=1; nsy < QUADSIZE; nsy++) { if (game.quad[nsx][nsy]==IHSTAR) { @@ -738,7 +738,7 @@ void snova(int insx, int insy) { game.damage[DRADIO] == 0 || condit == IHDOCKED) game.starch[nqx][nqy] = 1; - game.state.galaxy[nqx][nqy] = 1000; + game.state.galaxy[nqx][nqy] = SUPERNOVA_PLACE; /* If supernova destroys last klingons give special message */ if (game.state.remkl==0 && (nqx != quadx || nqy != quady)) { skip(2); diff --git a/moving.c b/moving.c index ac368e1..d12c4ab 100644 --- a/moving.c +++ b/moving.c @@ -57,7 +57,7 @@ void imove(void) { * that attacks only happen if Klingons * are present and your skill is > 3. */ - if (skill > 3 && klhere > 0 && game.state.galaxy[quadx][quady] != 1000) + if (skill > 3 && klhere > 0 && game.state.galaxy[quadx][quady] != SUPERNOVA_PLACE) attack(0); if (alldone) return; } @@ -202,7 +202,7 @@ label100: game.kdist[l] = finald; } sortkl(); - if (game.state.galaxy[quadx][quady] != 1000 && iattak == 0) + if (game.state.galaxy[quadx][quady] != SUPERNOVA_PLACE && iattak == 0) attack(0); for (l = 1 ; l <= nenhere; l++) game.kavgd[l] = game.kdist[l]; } @@ -727,7 +727,7 @@ void atover(int igrab) { crmshp(); skip(1); prout("safely out of quadrant."); - game.starch[quadx][quady] = game.damage[DRADIO] > 0.0 ? game.state.galaxy[quadx][quady]+1000:1; + game.starch[quadx][quady] = game.damage[DRADIO] > 0.0 ? game.state.galaxy[quadx][quady]+SUPERNOVA_PLACE:1; /* Try to use warp engines */ if (game.damage[DWARPEN]) { @@ -757,7 +757,7 @@ void atover(int igrab) { return; } /* Repeat if another snova */ - } while (game.state.galaxy[quadx][quady] == 1000); + } while (game.state.galaxy[quadx][quady] == SUPERNOVA_PLACE); if (game.state.remkl==0) finish(FWON); /* Snova killed remaining enemy. */ } @@ -806,7 +806,7 @@ void timwrp() { 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; + game.starch[l][ll]=game.damage[DRADIO]>0.0 ? game.state.galaxy[l][ll]+SUPERNOVA_PLACE :1; prout("Spock has reconstructed a correct star chart from memory"); if (game.damage[DRADIO] > 0.0) stdamtim = game.state.date; } diff --git a/planets.c b/planets.c index 322fb1e..a46e0e2 100644 --- a/planets.c +++ b/planets.c @@ -22,7 +22,7 @@ static int consumeTime(void) { events(); /* Used to avoid if future[FSCMOVE] within time */ // future[FSNOVA] = asave; /*fails if game over, quadrant super-novas or we've moved to new quadrant*/ - if (alldone || game.state.galaxy[quadx][quady] == 1000 || justin != 0) return 1; + if (alldone || game.state.galaxy[quadx][quady] == SUPERNOVA_PLACE || justin != 0) return 1; return 0; } diff --git a/reports.c b/reports.c index f71b8c5..8b01a6d 100644 --- a/reports.c +++ b/reports.c @@ -132,9 +132,9 @@ void lrscan(void) { 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]); + if (game.state.galaxy[x][y] 0 ? game.state.galaxy[x][y]+1000 : 1; + game.starch[x][y] = game.damage[DRADIO] > 0 ? game.state.galaxy[x][y]+SUPERNOVA_PLACE : 1; } } prout(" "); @@ -175,7 +175,7 @@ void chart(int nn) { stdamtim = game.state.date; 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; + if (game.starch[i][j] == 1) game.starch[i][j] = game.state.galaxy[i][j]+SUPERNOVA_PLACE; } else { proutn("(Last surveillance update %d stardates ago.", @@ -193,7 +193,7 @@ void chart(int nn) { strcpy(buf, ".1."); else if (game.starch[i][j] == 0) strcpy(buf, "..."); - else if (game.state.galaxy[i][j]>=1000) + else if (game.state.galaxy[i][j]>=SUPERNOVA_PLACE) strcpy(buf, "***"); else sprintf(buf, "%03d", game.state.galaxy[i][j]); @@ -227,7 +227,7 @@ int srscan(int l) { prout(" [Using Base's sensors]"); } else proutn(" Short-range scan\n\r"); - if (goodScan) game.starch[quadx][quady] = game.damage[DRADIO]>0.0 ? game.state.galaxy[quadx][quady]+1000:1; + if (goodScan) game.starch[quadx][quady] = game.damage[DRADIO]>0.0 ? game.state.galaxy[quadx][quady]+SUPERNOVA_PLACE:1; scan(); if (isit("chart")) nn = TRUE; if (isit("no")) rightside = FALSE; diff --git a/setup.c b/setup.c index 572d922..fb12ed8 100644 --- a/setup.c +++ b/setup.c @@ -161,9 +161,9 @@ void abandn(void) { 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); } } @@ -248,7 +248,7 @@ 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 */ @@ -271,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; @@ -284,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 @@ -310,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; } @@ -328,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) { @@ -336,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) { @@ -490,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; } @@ -531,17 +531,17 @@ void newqad(int shutup) { 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; @@ -576,8 +576,8 @@ void newqad(int shutup) { game.kpower[i] = Rand()*400.0 + 450.0 + 50.0*skill; } // If quadrant needs a starbase, put it in - if (quadnum >= 10) { - quadnum -= 10; + if (quadnum >= BASE_PLACE) { + quadnum -= BASE_PLACE; dropin(IHB, &basex, &basey); } @@ -637,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; diff --git a/sst.c b/sst.c index f6b21c7..a9884e7 100644 --- a/sst.c +++ b/sst.c @@ -454,14 +454,14 @@ static void makemoves(void) { events(); if (alldone) break; // Events did us in } - if (game.state.galaxy[quadx][quady] == 1000) { // Galaxy went Nova! + if (game.state.galaxy[quadx][quady] == SUPERNOVA_PLACE) { // Galaxy went Nova! atover(0); continue; } if (hitme && justin==0) { attack(2); if (alldone) break; - if (game.state.galaxy[quadx][quady] == 1000) { // went NOVA! + if (game.state.galaxy[quadx][quady] == SUPERNOVA_PLACE) { // went NOVA! atover(0); hitme = TRUE; continue; diff --git a/sst.h b/sst.h index 0cb66ce..4ebc05d 100644 --- a/sst.h +++ b/sst.h @@ -58,6 +58,18 @@ typedef struct { remtime; // remaining time } snapshot; // Data that is snapshot +/* + * This is how the integers in galaxy are encoded. + * Someday these should turn into structure fields. + */ +#define SUPERNOVA_PLACE 1000 +#define ENEMY_PLACE 100 +#define BASE_PLACE 10 +#define STAR_PLACE 1 + +/* for newstuff */ +#define ROMULAN_PLACE 10 + // Scalar variables that are needed for freezing the game // are placed in a structure. #defines are used to access by their // original names. Gee, I could have done this with the d structure, -- 2.31.1