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 */
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;
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;
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;
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;
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;
}
{
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;
}
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) {
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");
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
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;
/* 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;
}
/* 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;
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) {
}
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) {
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;
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;
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*.");
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])
{
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;
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)
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);
}
}
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;
}
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 */
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;
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;
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)
#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;