From 6332f1c7917206e23f408ebb505c483416b84e5b Mon Sep 17 00:00:00 2001 From: "Eric S. Raymond" Date: Wed, 20 Sep 2006 03:57:04 +0000 Subject: [PATCH 1/1] More uses of distance() macro. Eliminate some magic numbers. --- src/battle.c | 45 ++++++++++++++++++++++----------------------- src/io.c | 24 ++++++++++++------------ src/moving.c | 41 ++++++++++++++++------------------------- src/planets.c | 8 ++++---- src/setup.c | 8 ++++---- src/sst.h | 6 +++--- 6 files changed, 61 insertions(+), 71 deletions(-) diff --git a/src/battle.c b/src/battle.c index a1bacfa..20b35ef 100644 --- a/src/battle.c +++ b/src/battle.c @@ -181,7 +181,7 @@ void ram(bool ibumpd, int ienm, coord w) void torpedo(double course, double r, int inx, int iny, double *hit, int i, int n) /* let a photon torpedo fly */ { - int l, iquad=0, jx=0, jy=0, ll; + int l, iquad=0, ll; bool shoved = false; double ac=course + 0.25*r; double angle = (15.0-ac)*0.5235988; @@ -189,9 +189,9 @@ void torpedo(double course, double r, int inx, int iny, double *hit, int i, int double deltax=-sin(angle), deltay=cos(angle), x=inx, y=iny, bigger; double ang, temp, xx, yy, kp, h1; struct quadrant *q = &game.state.galaxy[game.quadrant.x][game.quadrant.y]; - coord w; + coord w, jw; - w.x = w.y = 0; + w.x = w.y = jw.x = jw.y = 0; bigger = fabs(deltax); if (fabs(deltay) > bigger) bigger = fabs(deltay); deltax /= bigger; @@ -208,7 +208,7 @@ void torpedo(double course, double r, int inx, int iny, double *hit, int i, int w.y = y + 0.5; if (!VALID_SECTOR(w.x, w.y)) break; iquad=game.quad[w.x][w.y]; - tracktorpedo(w.x, w.y, l, i, n, iquad); + tracktorpedo(w, l, i, n, iquad); if (iquad==IHDOT) continue; /* hit something */ setwnd(message_window); @@ -232,19 +232,19 @@ void torpedo(double course, double r, int inx, int iny, double *hit, int i, int if (fabs(cos(ang)) > temp) temp = fabs(cos(ang)); xx = -sin(ang)/temp; yy = cos(ang)/temp; - jx=w.x+xx+0.5; - jy=w.y+yy+0.5; - if (!VALID_SECTOR(jx, jy)) return; - if (game.quad[jx][jy]==IHBLANK) { + jw.x=w.x+xx+0.5; + jw.y=w.y+yy+0.5; + if (!VALID_SECTOR(jw.x, jw.y)) return; + if (game.quad[jw.x][jw.y]==IHBLANK) { finish(FHOLE); return; } - if (game.quad[jx][jy]!=IHDOT) { + if (game.quad[jw.x][jw.y]!=IHDOT) { /* can't move into object */ return; } - game.sector.x = jx; - game.sector.y = jy; + game.sector.x = jw.x; + game.sector.y = jw.y; crmshp(); shoved = true; break; @@ -280,25 +280,25 @@ void torpedo(double course, double r, int inx, int iny, double *hit, int i, int if (fabs(cos(ang)) > temp) temp = fabs(cos(ang)); xx = -sin(ang)/temp; yy = cos(ang)/temp; - jx=w.x+xx+0.5; - jy=w.y+yy+0.5; - if (!VALID_SECTOR(jx, jy)) { + jw.x=w.x+xx+0.5; + jw.y=w.y+yy+0.5; + if (!VALID_SECTOR(jw.x, jw.y)) { prout(_(" damaged but not destroyed.")); return; } - if (game.quad[jx][jy]==IHBLANK) { + if (game.quad[jw.x][jw.y]==IHBLANK) { prout(_(" buffeted into black hole.")); - deadkl(w, iquad, jx, jy); + deadkl(w, iquad, jw.x, jw.y); return; } - if (game.quad[jx][jy]!=IHDOT) { + if (game.quad[jw.x][jw.y]!=IHDOT) { /* can't move into object */ prout(_(" damaged but not destroyed.")); return; } proutn(_(" damaged--")); - game.ks[ll].x = jx; - game.ks[ll].y = jy; + game.ks[ll].x = jw.x; + game.ks[ll].y = jw.y; shoved = true; break; case IHB: /* Hit a base */ @@ -426,9 +426,8 @@ void torpedo(double course, double r, int inx, int iny, double *hit, int i, int } if (shoved) { game.quad[w.x][w.y]=IHDOT; - game.quad[jx][jy]=iquad; - w.x = jx; w.y = jy; - prout(_(" displaced by blast to %s "), cramlc(sector, w)); + game.quad[jw.x][jw.y]=iquad; + prout(_(" displaced by blast to %s "), cramlc(sector, jw)); for_local_enemies(ll) game.kdist[ll] = game.kavgd[ll] = distance(game.sector,game.ks[ll]); sortkl(); @@ -1227,7 +1226,7 @@ void hittem(double *hits) w = game.ks[kk]; if (hit > 0.005) { if (!damaged(DSRSENS)) - boom(w.x, w.y); + boom(w); proutn(_("%d unit hit on "), (int)hit); } else diff --git a/src/io.c b/src/io.c index 3db1c45..b60fec5 100644 --- a/src/io.c +++ b/src/io.c @@ -340,26 +340,26 @@ void drawmaps(int mode) } } -static void put_srscan_sym(int x, int y, char sym) +static void put_srscan_sym(coord w, char sym) { - wmove(srscan_window, x+1, y*2+2); + wmove(srscan_window, w.x+1, w.y*2+2); waddch(srscan_window, sym); wrefresh(srscan_window); } -void boom(int ii, int jj) +void boom(coord w) /* enemy fall down, go boom */ { if (game.options & OPTION_CURSES) { drawmaps(2); setwnd(srscan_window); wattron(srscan_window, A_REVERSE); - put_srscan_sym(ii, jj, game.quad[ii][jj]); + put_srscan_sym(w, game.quad[w.x][w.y]); sound(500); delay(1000); nosound(); wattroff(srscan_window, A_REVERSE); - put_srscan_sym(ii, jj, game.quad[ii][jj]); + put_srscan_sym(w, game.quad[w.x][w.y]); delay(500); setwnd(message_window); } @@ -380,7 +380,7 @@ void warble(void) } } -void tracktorpedo(int ix, int iy, int l, int i, int n, int iquad) +void tracktorpedo(coord w, int l, int i, int n, int iquad) /* torpedo-track animation */ { if (!game.options & OPTION_CURSES) { @@ -395,7 +395,7 @@ void tracktorpedo(int ix, int iy, int l, int i, int n, int iquad) } } else if (l==4 || l==9) skip(1); - proutn("%d - %d ", ix, iy); + proutn("%d - %d ", w.x, w.y); } else { if (!damaged(DSRSENS) || game.condit==IHDOCKED) { if (i != 1 && l == 1) { @@ -403,23 +403,23 @@ void tracktorpedo(int ix, int iy, int l, int i, int n, int iquad) delay(400); } if ((iquad==IHDOT)||(iquad==IHBLANK)){ - put_srscan_sym(ix, iy, '+'); + put_srscan_sym(w, '+'); sound(l*10); delay(100); nosound(); - put_srscan_sym(ix, iy, iquad); + put_srscan_sym(w, iquad); } else { wattron(curwnd, A_REVERSE); - put_srscan_sym(ix, iy, iquad); + put_srscan_sym(w, iquad); sound(500); delay(1000); nosound(); wattroff(curwnd, A_REVERSE); - put_srscan_sym(ix, iy, iquad); + put_srscan_sym(w, iquad); } } else { - proutn("%d - %d ", ix, iy); + proutn("%d - %d ", w.x, w.y); } } } diff --git a/src/moving.c b/src/moving.c index 99c1891..348d165 100644 --- a/src/moving.c +++ b/src/moving.c @@ -8,10 +8,10 @@ void imove(void) /* movement execution for warp, impule, supernova, and tractor-beam events */ { double angle, deltax, deltay, bigger, x, y, - finald, finalx, finaly, stopegy, probf; + finald, stopegy, probf; int n, m, kink, kinks, iquad; - coord w; - bool trbeam = 0; + coord w, final; + bool trbeam = false; w.x = w.y = 0; if (game.inorbit) { @@ -53,9 +53,8 @@ void imove(void) if (game.nenhere != 0 && game.iattak != 2) { newcnd(); for_local_enemies(m) { - finald = sqrt((w.x-game.ks[m].x)*(double)(w.x-game.ks[m].x) + - (w.y-game.ks[m].y)*(double)(w.y-game.ks[m].y)); - game.kavgd[m] = 0.5 * (finald+game.kdist[m]); + finald = distance(w, game.ks[m]); + game.kavgd[m] = 0.5 * (finald + game.kdist[m]); } /* * Stas Sergeev added the game.condition @@ -123,8 +122,7 @@ void imove(void) if (iquad != IHDOT) { /* object encountered in flight path */ stopegy = 50.0*game.dist/game.optime; - game.dist=0.1*sqrt((game.sector.x-w.x)*(double)(game.sector.x-w.x) + - (game.sector.y-w.y)*(double)(game.sector.y-w.y)); + game.dist = distance(game.sector, w) / (QUADSIZE * 1.0); switch (iquad) { case IHT: /* Ram a Tholian */ case IHK: /* Ram enemy ship */ @@ -132,11 +130,9 @@ void imove(void) case IHS: case IHR: case IHQUEST: - game.sector.x = w.x; - game.sector.y = w.y; + game.sector = w; ram(0, iquad, game.sector); - finalx = game.sector.x; - finaly = game.sector.y; + final = game.sector; break; case IHBLANK: skip(1); @@ -174,10 +170,9 @@ void imove(void) proutn(_("Emergency stop required ")); prout(_("%2d units of energy."), (int)stopegy); game.energy -= stopegy; - finalx = x-deltax+0.5; - game.sector.x = finalx; - finaly = y-deltay+0.5; - game.sector.y = finaly; + final.x = x-deltax+0.5; + final.y = y-deltay+0.5; + game.sector = final; if (game.energy <= 0) { finish(FNRG); return; @@ -187,20 +182,16 @@ void imove(void) goto no_quad_change; /* sorry! */ } } - game.dist = 0.1*sqrt((game.sector.x-w.x)*(double)(game.sector.x-w.x) + - (game.sector.y-w.y)*(double)(game.sector.y-w.y)); - game.sector.x = w.x; - game.sector.y = w.y; + game.dist = distance(game.sector, w) / (QUADSIZE * 1.0); + game.sector = w; } - finalx = game.sector.x; - finaly = game.sector.y; + final = game.sector; no_quad_change: /* No quadrant change -- compute new avg enemy distances */ game.quad[game.sector.x][game.sector.y] = game.ship; if (game.nenhere) { for_local_enemies(m) { - finald = sqrt((w.x-game.ks[m].x)*(double)(w.x-game.ks[m].x) + - (w.y-game.ks[m].y)*(double)(w.y-game.ks[m].y)); + finald = distance(w, game.ks[m]); game.kavgd[m] = 0.5 * (finald+game.kdist[m]); game.kdist[m] = finald; } @@ -718,7 +709,7 @@ void atover(bool igrab) } else { prout(_("saved.")); - game.icrystl = 1; + game.icrystl = true; } } } diff --git a/src/planets.c b/src/planets.c index fd10f67..e0c75a6 100644 --- a/src/planets.c +++ b/src/planets.c @@ -181,7 +181,7 @@ void beam(void) prout(_("The shuttle craft Galileo is here!")); } if (game.landed!=1 && game.imine) { - game.icrystl = 1; + game.icrystl = true; game.cryprob = 0.05; } game.imine = false; @@ -209,7 +209,7 @@ void mine(void) prout(_("You've already mined enough crystals for this trip.")); return; } - if (game.icrystl == 1 && game.cryprob == 0.05) { + if (game.icrystl && game.cryprob == 0.05) { proutn(_("With all those fresh crystals aboard the ")); crmshp(); skip(1); @@ -229,7 +229,7 @@ void usecrystals(void) game.ididit = false; skip(1); chew(); - if (game.icrystl!=1) { + if (!game.icrystl) { prout(_("No dilithium crystals available.")); return; } @@ -364,7 +364,7 @@ void shuttle(void) game.iscraft = 1; game.icraft = false; if (game.imine) { - game.icrystl = 1; + game.icrystl = true; game.cryprob = 0.05; } game.imine = false; diff --git a/src/setup.c b/src/setup.c index 80ab2e4..f6a0ae1 100644 --- a/src/setup.c +++ b/src/setup.c @@ -177,7 +177,7 @@ void abandn(void) /* If at least one base left, give 'em the Faerie Queene */ skip(1); - game.icrystl = 0; /* crystals are lost */ + game.icrystl = false; /* crystals are lost */ game.nprobes = 0; /* No probes */ prout("You are captured by Klingons and released to"); prout("the Federation in a prisoner-of-war exchange."); @@ -209,7 +209,7 @@ void abandn(void) prout("Starfleet puts you in command of another ship,"); prout("the Faerie Queene, which is antiquated but,"); prout("still useable."); - if (game.icrystl!=0) prout("The dilithium crystals have been moved."); + if (game.icrystl) prout("The dilithium crystals have been moved."); game.imine = false; game.iscraft=0; /* Galileo disappears */ /* Resupply ship */ @@ -256,8 +256,8 @@ void setup(bool needprompt) game.battle.x = game.battle.y = 0; game.state.date = game.indate = 100.0*(int)(31.0*Rand()+20.0); game.nkinks = game.nhelp = game.casual = game.abandoned = 0; - game.resting = game.imine = game.icraft = false; - game.isatb = game.iscate = game.icrystl = game.state.nplankl = 0; + game.resting = game.imine = game.icrystl = game.icraft = false; + game.isatb = game.iscate = game.state.nplankl = 0; game.state.starkl = game.state.basekl = 0; game.iscraft = 1; game.landed = -1; diff --git a/src/sst.h b/src/sst.h index 07e7a2b..89cb16f 100644 --- a/src/sst.h +++ b/src/sst.h @@ -222,6 +222,7 @@ struct game { isarmed, // probe is armed inorbit, // orbiting a planet imine, // mining + icrystl, // dilithium crystals aboard thawed; // thawed game int inkling, // initial number of klingons inbase, // initial number of bases @@ -251,7 +252,6 @@ struct game { isatb, // =1 if super commander is attacking base iscate, // super commander is here iattak, // attack recursion elimination (was cracks[4]) - icrystl, // dilithium crystals aboard tourn, // tournament number iseenit, // seen base attack report proben, // number of moves for probe @@ -411,8 +411,8 @@ void probe(void); void iostart(void); void setwnd(WINDOW *); void warble(void); -void boom(int, int); -void tracktorpedo(int, int, int, int, int, int); +void boom(coord); +void tracktorpedo(coord, int, int, int, int); void cgetline(char *, int); void waitfor(void); void setpassword(void); -- 2.31.1