From 797f0f77ccdfd001fff39396ceae760478c2b920 Mon Sep 17 00:00:00 2001 From: "Eric S. Raymond" Date: Mon, 7 Feb 2005 16:08:11 +0000 Subject: [PATCH] More origin-hiding. --- ai.c | 42 ++++++++++++++++++++++-------------------- battle.c | 14 +++++++------- events.c | 12 ++++++------ moving.c | 8 ++++---- setup.c | 2 +- sst.h | 7 +++++-- 6 files changed, 45 insertions(+), 40 deletions(-) diff --git a/ai.c b/ai.c index c141bbe..33e4873 100644 --- a/ai.c +++ b/ai.c @@ -273,26 +273,28 @@ void movcom(void) /* Figure out which Klingon is the commander (or Supercommander) and do move */ - if (comhere) for (i = 1; i <= nenhere; i++) { - ix = game.kx[i]; - iy = game.ky[i]; - if (game.quad[ix][iy] == IHC) { - movebaddy(ix, iy, i, IHC); - break; + if (comhere) + for_local_enemies(i) { + ix = game.kx[i]; + iy = game.ky[i]; + if (game.quad[ix][iy] == IHC) { + movebaddy(ix, iy, i, IHC); + break; + } } - } - if (ishere) for (i = 1; i <= nenhere; i++) { - ix = game.kx[i]; - iy = game.ky[i]; - if (game.quad[ix][iy] == IHS) { - movebaddy(ix, iy, i, IHS); - break; + if (ishere) + for_local_enemies(i) { + ix = game.kx[i]; + iy = game.ky[i]; + if (game.quad[ix][iy] == IHS) { + movebaddy(ix, iy, i, IHS); + break; + } } - } /* if skill level is high, move other Klingons and Romulans too! Move these last so they can base their actions on what the commander(s) do. */ - if (skill >= SKILL_EXPERT) for (i = 1; i <= nenhere; i++) { + if (skill >= SKILL_EXPERT) for_local_enemies(i) { ix = game.kx[i]; iy = game.ky[i]; if (game.quad[ix][iy] == IHK || game.quad[ix][iy] == IHR) @@ -313,7 +315,7 @@ static int movescom(int iqx, int iqy, int flag, int *ipage) return 1; if (flag) { /* Avoid quadrants with bases if we want to avoid Enterprise */ - for (i = 1; i <= game.state.rembase; i++) + for_starbases(i) if (game.state.baseqx[i]==iqx && game.state.baseqy[i]==iqy) return 1; } if (justin && !iscate) return 1; @@ -329,7 +331,7 @@ static int movescom(int iqx, int iqy, int flag, int *ipage) ishere=0; ientesc=0; game.future[FSCDBAS]=1e30; - for (i = 1; i <= nenhere; i++) + for_local_enemies(i) if (game.quad[game.kx[i]][game.ky[i]] == IHS) break; game.quad[game.kx[i]][game.ky[i]] = IHDOT; game.kx[i] = game.kx[nenhere]; @@ -397,7 +399,7 @@ void scom(int *ipage) } sx = game.state.isx; sy = game.state.isy; - for (i = 1; i <= game.state.rembase; i++) { + for_starbases(i) { basetbl[i] = i; ibqx = game.state.baseqx[i]; ibqy = game.state.baseqy[i]; @@ -425,7 +427,7 @@ void scom(int *ipage) without too many Klingons, and not already under attack. */ ifindit = iwhichb = 0; - for (i2 = 1; i2 <= game.state.rembase; i2++) { + for_starbases(i2) { i = basetbl[i2]; /* bug in original had it not finding nearest*/ ibqx = game.state.baseqx[i]; ibqy = game.state.baseqy[i]; @@ -498,7 +500,7 @@ void scom(int *ipage) if (game.state.rembase == 0) { game.future[FSCMOVE] = 1e30; } - else for (i=1; i<=game.state.rembase; i++) { + else for_starbases(i) { ibqx = game.state.baseqx[i]; ibqy = game.state.baseqy[i]; if (ibqx==game.state.isx && ibqy == game.state.isy && game.state.isx != batx && game.state.isy != baty) { diff --git a/battle.c b/battle.c index 5490399..5e1c404 100644 --- a/battle.c +++ b/battle.c @@ -255,7 +255,7 @@ void torpedo(double course, double r, int inx, int iny, double *hit, int wait, i case IHR: /* Hit a regular enemy */ case IHK: /* find the enemy */ - for (ll=1; ll <= nenhere; ll++) + for_local_enemies(ll) if (ix==game.kx[ll] && iy==game.ky[ll]) break; kp = fabs(game.kpower[ll]); h1 = 700.0 + 100.0*Rand() - @@ -410,7 +410,7 @@ void torpedo(double course, double r, int inx, int iny, double *hit, int wait, i game.quad[jx][jy]=iquad; game.quad[ix][iy]=IHDOT; prout(" displaced by blast to %s ", cramlc(sector, jx, jy)); - for (ll=1; ll<=nenhere; ll++) + for_local_enemies(ll) game.kdist[ll] = game.kavgd[ll] = sqrt(square(sectx-game.kx[ll])+square(secty-game.ky[ll])); sortkl(); return; @@ -482,7 +482,7 @@ void attack(int torps_ok) if (shldchg == 1) chgfac = 0.25+0.5*Rand(); skip(1); if (skill <= SKILL_FAIR) i = 2; - for (l=1; l <= nenhere; l++) { + for_local_enemies(l) { if (game.kpower[l] < 0) continue; /* too weak to attack */ /* compute hit strength and diminsh shield power */ r = Rand(); @@ -595,7 +595,7 @@ void attack(int torps_ok) } } /* After attack, reset average distance to enemies */ - for (l = 1; l <= nenhere; l++) + for_local_enemies(l) game.kavgd[l] = game.kdist[l]; sortkl(); return; @@ -666,7 +666,7 @@ void deadkl(int ix, int iy, int type, int ixx, int iyy) /* Remove enemy ship from arrays describing local conditions */ if (game.future[FCDBAS] < 1e30 && batx==quadx && baty==quady && type==IHC) game.future[FCDBAS] = 1e30; - for (i=1; i<=nenhere; i++) + for_local_enemies(i) if (game.kx[i]==ix && game.ky[i]==iy) break; nenhere--; if (i <= nenhere) { @@ -997,7 +997,7 @@ void phasers(void) irec=0; do { chew(); - if (!kz) for (i = 1; i <= nenhere; i++) + if (!kz) for_local_enemies(i) irec+=fabs(game.kpower[i])/(PHASEFAC*pow(0.90,game.kdist[i]))* (1.01+0.05*Rand()) + 1.0; kz=1; @@ -1032,7 +1032,7 @@ void phasers(void) if (nenhere) { extra = 0.0; powrem = rpow; - for (i = 1; i <= nenhere; i++) { + for_local_enemies(i) { hits[i] = 0.0; if (powrem <= 0) continue; hits[i] = fabs(game.kpower[i])/(PHASEFAC*pow(0.90,game.kdist[i])); diff --git a/events.c b/events.c index 8c3d378..fc03bb0 100644 --- a/events.c +++ b/events.c @@ -177,8 +177,8 @@ void events(void) break; } i = 0; - for (j=1; j<=game.state.rembase; j++) { - for (k=1; k<=game.state.remcom; k++) + for_commanders (j) { + for_commanders(k) if (game.state.baseqx[j]==game.state.cx[k] && game.state.baseqy[j]==game.state.cy[k] && (game.state.baseqx[j]!=quadx || game.state.baseqy[j]!=quady) && (game.state.baseqx[j]!=game.state.isx || game.state.baseqy[j]!=game.state.isy)) { @@ -236,7 +236,7 @@ void events(void) if (line==FCDBAS) { game.future[FCDBAS] = 1e30; /* find the lucky pair */ - for (i = 1; i <= game.state.remcom; i++) + for_commanders(i) if (game.state.cx[i]==batx && game.state.cy[i]==baty) break; if (i > game.state.remcom || game.state.rembase == 0 || @@ -273,7 +273,7 @@ void events(void) } /* Remove Starbase from galaxy */ game.state.galaxy[batx][baty].starbase = FALSE; - for (i=1; i <= game.state.rembase; i++) + for_starbases(i) if (game.state.baseqx[i]==batx && game.state.baseqy[i]==baty) { game.state.baseqx[i]=game.state.baseqx[game.state.rembase]; game.state.baseqy[i]=game.state.baseqy[game.state.rembase]; @@ -481,7 +481,7 @@ void nova(int ix, int iy) break; case IHB: /* Destroy base */ game.state.galaxy[quadx][quady].starbase = FALSE; - for (i = 1; i <= game.state.rembase; i++) + for_starbases(i) if (game.state.baseqx[i]==quadx && game.state.baseqy[i]==quady) break; game.state.baseqx[i] = game.state.baseqx[game.state.rembase]; @@ -524,7 +524,7 @@ void nova(int ix, int iy) case IHC: /* Damage/destroy big enemies */ case IHS: case IHR: - for (ll = 1; ll <= nenhere; ll++) + for_local_enemies(ll) if (game.kx[ll]==ii && game.ky[ll]==jj) break; game.kpower[ll] -= 800.0; /* If firepower is lost, die */ if (game.kpower[ll] <= 0.0) { diff --git a/moving.c b/moving.c index fbbe76f..f44aff4 100644 --- a/moving.c +++ b/moving.c @@ -48,7 +48,7 @@ void imove(void) /* Don't do it if being pushed by Nova */ if (nenhere != 0 && iattak != 2) { newcnd(); - for (l = 1; l <= nenhere; l++) { + for_local_enemies(l) { finald = sqrt((ix-game.kx[l])*(double)(ix-game.kx[l]) + (iy-game.ky[l])*(double)(iy-game.ky[l])); game.kavgd[l] = 0.5 * (finald+game.kdist[l]); @@ -193,7 +193,7 @@ no_quad_change: /* No quadrant change -- compute new avg enemy distances */ game.quad[sectx][secty] = ship; if (nenhere) { - for (l = 1; l <= nenhere; l++) { + for_local_enemies(l) { finald = sqrt((ix-game.kx[l])*(double)(ix-game.kx[l]) + (iy-game.ky[l])*(double)(iy-game.ky[l])); game.kavgd[l] = 0.5 * (finald+game.kdist[l]); @@ -202,7 +202,7 @@ no_quad_change: sortkl(); if (!game.state.galaxy[quadx][quady].supernova && iattak == 0) attack(0); - for (l = 1 ; l <= nenhere; l++) game.kavgd[l] = game.kdist[l]; + for_local_enemies(l) game.kavgd[l] = game.kdist[l]; } newcnd(); iattak = 0; @@ -929,7 +929,7 @@ void help(void) } else { ddist = 1e30; - for (l = 1; l <= game.state.rembase; l++) { + for_starbases(l) { xdist=10.0*sqrt(square(game.state.baseqx[l]-quadx)+square(game.state.baseqy[l]-quady)); if (xdist < ddist) { ddist = xdist; diff --git a/setup.c b/setup.c index 2b36068..17c14cf 100644 --- a/setup.c +++ b/setup.c @@ -559,7 +559,7 @@ void newqad(int shutup) game.kpower[i] = Rand()*150.0 +300.0 +25.0*skill; } // If we need a commander, promote a Klingon - for (i = 1; i <= game.state.remcom ; i++) + for_commanders(i) if (game.state.cx[i]==quadx && game.state.cy[i]==quady) break; if (i <= game.state.remcom) { diff --git a/sst.h b/sst.h index 203a169..d9ed121 100644 --- a/sst.h +++ b/sst.h @@ -27,8 +27,11 @@ */ #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++) +#define for_quadrants(i) for (i = 1; i <= GALSIZE; i++) +#define for_sectors(i) for (i = 1; i <= QUADSIZE; i++) +#define for_commanders(i) for (i = 1; i <= game.state.remcom; i++) +#define for_local_enemies(i) for (i = 1; i <= nenhere; i++) +#define for_starbases(i) for (i = 1; i <= game.state.rembase; i++) typedef struct { int x; /* Quadrant location of planet */ -- 2.31.1