From bdcf85461c171a9263fb0ba0e0ec07661bd082f5 Mon Sep 17 00:00:00 2001 From: "Eric S. Raymond" Date: Mon, 7 Feb 2005 15:40:59 +0000 Subject: [PATCH] Hide some differences between 0-origin and 1-origin addressing. --- ai.c | 6 +++--- battle.c | 9 ++++----- events.c | 20 ++++++++++---------- moving.c | 16 +++++++--------- planets.c | 4 ++-- reports.c | 15 +++++++-------- setup.c | 17 ++++++++--------- sst.h | 9 +++++++++ 8 files changed, 50 insertions(+), 46 deletions(-) diff --git a/ai.c b/ai.c index c6a862e..f1eca73 100644 --- a/ai.c +++ b/ai.c @@ -6,7 +6,7 @@ static int tryexit(int lookx, int looky, int ienm, int loccom, int irun) iqx = quadx+(lookx+(QUADSIZE-1))/QUADSIZE - 1; iqy = quady+(looky+(QUADSIZE-1))/QUADSIZE - 1; - if (iqx < 1 || iqx > GALSIZE || iqy < 1 || iqy > GALSIZE || + if (!VALID_QUADRANT(iqx,iqy) || game.state.galaxy[iqx][iqy].supernova || game.state.galaxy[iqx][iqy].klingons > 8) return 0; /* no can do -- neg energy, supernovae, or >8 Klingons */ @@ -307,7 +307,7 @@ static int movescom(int iqx, int iqy, int flag, int *ipage) int i; if ((iqx==quadx && iqy==quady) || - iqx < 1 || iqx > GALSIZE || iqy < 1 || iqy > GALSIZE || + !VALID_QUADRANT(iqx, iqy) || game.state.galaxy[iqx][iqy].supernova || game.state.galaxy[iqx][iqy].klingons > 8) return 1; @@ -595,7 +595,7 @@ void movetho(void) game.ky[nenhere]=ithy; /* check to see if all holes plugged */ - for (i = 1; i < QUADSIZE+1; i++) { + for_sectors(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 5356e3d..ef45ca2 100644 --- a/battle.c +++ b/battle.c @@ -198,10 +198,9 @@ 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 > QUADSIZE) break; y += deltay; iy = y + 0.5; - if (iy < 1 || iy > QUADSIZE) break; + if (!VALID_SECTOR(ix, iy)) break; iquad=game.quad[ix][iy]; tracktorpedo(x, y, ix, iy, wait, l, i, n, iquad); wait = 1; @@ -230,7 +229,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>QUADSIZE || jy<1 ||jy > QUADSIZE) return; + if (!VALID_SECTOR(jx, jy)) return; if (game.quad[jx][jy]==IHBLANK) { finish(FHOLE); return; @@ -278,7 +277,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>QUADSIZE || jy<1 ||jy > QUADSIZE) { + if (!VALID_SECTOR(jx, jy)) { prout(" damaged but not destroyed."); return; } @@ -690,7 +689,7 @@ static int targetcheck(double x, double y, double *course) { double deltx, delty; /* Return TRUE if target is invalid */ - if (x < 1.0 || x > QUADSIZE || y < 1.0 || y > QUADSIZE) { + if (!VALID_SECTOR(x, y)) { huh(); return 1; } diff --git a/events.c b/events.c index 55cc816..8c3d378 100644 --- a/events.c +++ b/events.c @@ -304,7 +304,7 @@ void events(void) if (probecx != i || probecy != j) { probecx = i; probecy = j; - if (i < 1 || i > GALSIZE || j < 1 || j > GALSIZE || + if (!VALID_QUADRANT(i, j) || game.state.galaxy[probecx][probecy].supernova) { // Left galaxy or ran into supernova if (game.damage[DRADIO]==0.0 || condit == IHDOCKED) { @@ -312,7 +312,7 @@ void events(void) ipage = 1; skip(1); proutn("Lt. Uhura- \"The deep space probe "); - if (i < 1 ||i > GALSIZE || j < 1 || j > GALSIZE) + if (!VALID_QUADRANT(j, i)) proutn("has left the galaxy"); else proutn("is no longer transmitting"); @@ -441,7 +441,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 > QUADSIZE || jj < 1 || jj > QUADSIZE) continue; + if (!VALID_SECTOR(jj, ii)) continue; iquad = game.quad[ii][jj]; switch (iquad) { // case IHDOT: /* Empty space ends reaction @@ -535,7 +535,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>QUADSIZE || newcy<1 || newcy>QUADSIZE) { + if (!VALID_SECTOR(newcx, newcy)) { /* can't leave quadrant */ skip(1); break; @@ -610,15 +610,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<=GALSIZE; nqx++) { - for (nqy = 1; nqy<=GALSIZE; nqy++) { + for_quadrants(nqx) { + for_quadrants(nqy) { stars += game.state.galaxy[nqx][nqy].stars; } } if (stars == 0) return; /* nothing to supernova exists */ num = Rand()*stars + 1; - for (nqx = 1; nqx<=GALSIZE; nqx++) { - for (nqy = 1; nqy<=GALSIZE; nqy++) { + for_quadrants(nqx) { + for_quadrants(nqy) { num -= game.state.galaxy[nqx][nqy].stars; if (num <= 0) break; } @@ -648,8 +648,8 @@ void snova(int insx, int insy) /* we are in the quadrant! */ incipient = 1; num = Rand()* game.state.galaxy[nqx][nqy].stars + 1; - for (nsx=1; nsx < QUADSIZE; nsx++) { - for (nsy=1; nsy < QUADSIZE; nsy++) { + for_sectors(nsx) { + for_sectors(nsy) { if (game.quad[nsx][nsy]==IHSTAR) { num--; if (num==0) break; diff --git a/moving.c b/moving.c index 2ba016a..fbbe76f 100644 --- a/moving.c +++ b/moving.c @@ -43,7 +43,7 @@ void imove(void) for (l = 1; l <= n; l++) { ix = (x += deltax) + 0.5; iy = (y += deltay) + 0.5; - if (ix < 1 || ix > QUADSIZE || iy < 1 || iy > QUADSIZE) { + if (!VALID_SECTOR(ix, iy)) { /* Leaving quadrant -- allow final enemy attack */ /* Don't do it if being pushed by Nova */ if (nenhere != 0 && iattak != 2) { @@ -372,11 +372,10 @@ static void getcd(int isprobe, int akey) { } itemp = 1; } - if (irowq<1 || irowq > GALSIZE || icolq<1 || icolq > GALSIZE || - irows<1 || irows > QUADSIZE || icols<1 || icols > QUADSIZE) { - huh(); - return; - } + if (!VALID_QUADRANT(icolq,irowq)||!VALID_SECTOR(icols,irows)) { + huh(); + return; + } skip(1); if (!isprobe) { if (itemp) { @@ -589,10 +588,9 @@ void warp(int i) for (l = 1; l <= n; l++) { x += deltax; ix = x + 0.5; - if (ix < 1 || ix > QUADSIZE) break; y += deltay; iy = y +0.5; - if (iy < 1 || iy > QUADSIZE) break; + if (!VALID_SECTOR(ix, iy)) break; if (game.quad[ix][iy] != IHDOT) { blooey = 0; twarp = 0; @@ -953,7 +951,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<=QUADSIZE && iy>=1 && iy<=QUADSIZE && game.quad[ix][iy]==IHDOT) { + if (VALID_SECTOR(ix,iy) && game.quad[ix][iy]==IHDOT) { /* found one -- finish up */ sectx=ix; secty=iy; diff --git a/planets.c b/planets.c index 5cf8965..87ca4ad 100644 --- a/planets.c +++ b/planets.c @@ -482,8 +482,8 @@ void deathray(void) proutn("Spock- \"I believe the word is"); prouts(" *ASTONISHING*"); prout(" Mr. Sulu."); - for (i=1; i<=QUADSIZE; i++) - for (j=1; j<=QUADSIZE; j++) + for_sectors(i) + for_sectors(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 3f654cf..842ca87 100644 --- a/reports.c +++ b/reports.c @@ -132,7 +132,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 > GALSIZE || y == 0 || y > GALSIZE) + if (!VALID_QUADRANT(x, y)) proutn(" -1"); else { if (!game.damage[DRADIO]) @@ -176,8 +176,8 @@ void rechart(void) { int i, j; stdamtim = game.state.date; - for (i=1; i <= GALSIZE ; i++) - for (j=1; j <= GALSIZE; j++) + for_quadrants(i) + for_quadrants(j) if (game.state.galaxy[i][j].charted) { game.state.chart[i][j].klingons = game.state.galaxy[i][j].klingons; game.state.chart[i][j].starbase = game.state.galaxy[i][j].starbase; @@ -208,9 +208,9 @@ void chart(int nn) rechart(); prout(" 1 2 3 4 5 6 7 8"); - for (i = 1; i <= GALSIZE; i++) { + for_quadrants(i) { proutn("%d |", i); - for (j = 1; j <= GALSIZE; j++) { + for_quadrants(j) { char buf[4]; proutn(" "); if (game.state.galaxy[i][j].supernova) @@ -381,7 +381,7 @@ int srscan(int l) jj = (req!=0 ? req : i); if (leftside && i <= QUADSIZE) { proutn("%2d ", i); - for (j = 1; j <= QUADSIZE; j++) { + for_sectors(j) { sectscan(goodScan, i, j); } } @@ -436,8 +436,7 @@ void eta(void) else iy2=QUADSIZE; } - if (ix1 > GALSIZE || ix1 < 1 || iy1 > GALSIZE || iy1 < 1 || - ix2 > QUADSIZE || ix2 < 1 || iy2 > QUADSIZE || iy2 < 1) { + if (!VALID_QUADRANT(ix1, iy1) || !VALID_SECTOR(ix2, iy2)) { huh(); return; } diff --git a/setup.c b/setup.c index f4a0373..2b36068 100644 --- a/setup.c +++ b/setup.c @@ -156,11 +156,10 @@ void abandn(void) for (;;) { /* position next to base by trial and error */ game.quad[sectx][secty] = IHDOT; - for (l = 1; l <= QUADSIZE; l++) { + for_sectors(l) { sectx = 3.0*Rand() - 1.0 + basex; secty = 3.0*Rand() - 1.0 + basey; - if (sectx >= 1 && sectx <= QUADSIZE && - secty >= 1 && secty <= QUADSIZE && + if (VALID_SECTOR(sectx, secty) && game.quad[sectx][secty] == IHDOT) break; } if (l < QUADSIZE+1) break; /* found a spot */ @@ -227,8 +226,8 @@ void setup(int needprompt) landed = -1; alive = 1; docfac = 0.25; - for (i = 1; i <= GALSIZE; i++) - for (j = 1; j <= GALSIZE; j++) { + for_quadrants(i) + for_quadrants(j) { game.state.galaxy[i][j].charted = 0; game.state.galaxy[i][j].planets = 0; game.state.galaxy[i][j].romulans = 0; @@ -246,8 +245,8 @@ void setup(int needprompt) stdamtim = 1e30; // Put stars in the galaxy instar = 0; - for (i=1; i<=GALSIZE; i++) - for (j=1; j<=GALSIZE; j++) { + for_quadrants(i) + for_quadrants(j) { int k = Rand()*9.0 + 1.0; instar += k; game.state.galaxy[i][j].stars = k; @@ -536,8 +535,8 @@ void newqad(int shutup) ientesc = 1; } // Clear quadrant - for (i=1; i <= QUADSIZE; i++) - for (j=1; j <= QUADSIZE; j++) + for_sectors(i) + for_sectors(j) game.quad[i][j] = IHDOT; // cope with supernova if (game.state.galaxy[quadx][quady].supernova) diff --git a/sst.h b/sst.h index 7859610..203a169 100644 --- a/sst.h +++ b/sst.h @@ -21,6 +21,15 @@ #define QUADSIZE (10) #define BASEMAX (6) +/* + * These macros hide the difference between 0-origin and 1-origin addressing. + * They're a step towards de-FORTRANizing the code. + */ +#define VALID_QUADRANT(x, y) ((x)>=1 && (x)<=GALSIZE && (y)>=1 && (y)<=GALSIZE) +#define VALID_SECTOR(x, y) ((x)>=1 && (x)<=QUADSIZE && (y)>=1 && (y)<=QUADSIZE) +#define for_quadrants(i) for (i = 1; i < GALSIZE+1; i++) +#define for_sectors(i) for (i = 1; i < QUADSIZE+1; i++) + typedef struct { int x; /* Quadrant location of planet */ int y; -- 2.31.1