From: Eric S. Raymond Date: Fri, 22 Sep 2006 11:38:54 +0000 (+0000) Subject: Abstract away the operations involving a sentinel value for coordinates. X-Git-Tag: 2.0~203 X-Git-Url: https://jxself.org/git/?p=super-star-trek.git;a=commitdiff_plain;h=4207ce95e80fe7e2d5114079fa4255e0ad506618 Abstract away the operations involving a sentinel value for coordinates. Besides making the code more readable, the sentinel may change someday if we go zero-origin everywhere. --- diff --git a/src/battle.c b/src/battle.c index 9fc6082..f4c6e90 100644 --- a/src/battle.c +++ b/src/battle.c @@ -387,7 +387,7 @@ void torpedo(double course, double r, coord in, double *hit, int i, int n) q->planet = NOPLANET; DESTROY(&game.state.plnets[game.iplnet]); game.iplnet = 0; - game.plnet.x = game.plnet.y = 0; + invalidate(game.plnet); game.quad[w.x][w.y] = IHDOT; if (game.landed) { /* captain perishes on planet */ @@ -401,7 +401,7 @@ void torpedo(double course, double r, coord in, double *hit, int i, int n) q->planet = NOPLANET; DESTROY(&game.state.plnets[game.iplnet]); game.iplnet = 0; - game.plnet.x = game.plnet.y = 0; + invalidate(game.plnet); game.quad[w.x][w.y] = IHDOT; if (game.landed) { /* captain perishes on planet */ @@ -719,7 +719,7 @@ void deadkl(coord w, feature type, coord mv) else if (type == IHQUEST) { /* Killed a Thingy */ iqhere = iqengry = false; - thing.x =thing.y = 0; + invalidate(thing); } else { /* Some type of a Klingon */ diff --git a/src/events.c b/src/events.c index b4d4658..7acb042 100644 --- a/src/events.c +++ b/src/events.c @@ -317,7 +317,7 @@ void events(void) if (i > game.state.remcom || game.state.rembase == 0 || !game.state.galaxy[game.battle.x][game.battle.y].starbase) { /* No action to take after all */ - game.battle.x = game.battle.y = 0; + invalidate(game.battle); break; } } @@ -358,9 +358,8 @@ void events(void) game.battle = hold; game.isatb = 0; } - else { - game.battle.x = game.battle.y = 0; - } + else + invalidate(game.battle); break; case FSCMOVE: /* Supercommander moves */ schedule(FSCMOVE, 0.2777); @@ -676,7 +675,8 @@ void nova(coord nov) crmena(true, IHP, sector, scratch); prout(_(" destroyed.")); DESTROY(&game.state.plnets[game.iplnet]); - game.iplnet = game.plnet.x = game.plnet.y = 0; + game.iplnet = 0; + invalidate(game.plnet); if (game.landed) { finish(FPNOVA); return; @@ -690,7 +690,7 @@ void nova(coord nov) break; game.state.baseq[i] = game.state.baseq[game.state.rembase]; game.state.rembase--; - game.base.x = game.base.y = 0; + invalidate(game.base); game.state.basekl++; newcnd(); crmena(true, IHB, sector, scratch); @@ -875,7 +875,7 @@ void snova(bool induced, coord *w) for (l = 1; l <= maxloop; l++) { if (same(game.state.kcmdr[l], nq)) { game.state.kcmdr[l] = game.state.kcmdr[game.state.remcom]; - game.state.kcmdr[game.state.remcom].x = game.state.kcmdr[game.state.remcom].y = 0; + invalidate(game.state.kcmdr[game.state.remcom]); game.state.remcom--; kldead--; if (game.state.remcom==0) unschedule(FTBEAM); @@ -902,7 +902,7 @@ void snova(bool induced, coord *w) for (loop = 1; loop <= maxloop; loop++) if (same(game.state.baseq[loop], nq)) { game.state.baseq[loop] = game.state.baseq[game.state.rembase]; - game.state.baseq[game.state.rembase].x = game.state.baseq[game.state.rembase].y = 0; + invalidate(game.state.baseq[game.state.rembase]); game.state.rembase--; break; } diff --git a/src/moving.c b/src/moving.c index 948cdb4..3373b37 100644 --- a/src/moving.c +++ b/src/moving.c @@ -219,7 +219,7 @@ void dock(bool verbose) prout(_("You must first leave standard orbit.")); return; } - if (game.base.x==0 || abs(game.sector.x-game.base.x) > 1 || abs(game.sector.y-game.base.y) > 1) { + if (!is_valid(game.base) || abs(game.sector.x-game.base.x) > 1 || abs(game.sector.y-game.base.y) > 1) { crmshp(); prout(_(" not adjacent to base.")); return; @@ -793,7 +793,7 @@ void timwrp() game.isatb = 0; unschedule(FCDBAS); unschedule(FSCDBAS); - game.battle.x = game.battle.y = 0; + invalidate(game.battle); /* Make sure Galileo is consistant -- Snapshot may have been taken when on planet, which would give us two Galileos! */ @@ -987,7 +987,7 @@ void mayday(void) break; } } - if (game.sector.x==0){ + if (!is_valid(game.sector)){ prout(_("You have been lost in space...")); finish(FMATERIALIZE); return; diff --git a/src/planets.c b/src/planets.c index a34b1f6..ca8996f 100644 --- a/src/planets.c +++ b/src/planets.c @@ -55,7 +55,7 @@ void orbit(void) prout(_("Both warp and impulse engines damaged.")); return; } - if (game.plnet.x == 0 || abs(game.sector.x-game.plnet.x) > 1 || abs(game.sector.y-game.plnet.y) > 1) { + if (!is_valid(game.plnet) || abs(game.sector.x-game.plnet.x) > 1 || abs(game.sector.y-game.plnet.y) > 1) { crmshp(); prout(_(" not adjacent to planet.")); skip(1); diff --git a/src/setup.c b/src/setup.c index 741f35f..44e378b 100644 --- a/src/setup.c +++ b/src/setup.c @@ -212,7 +212,7 @@ void setup(bool needprompt) for (i=0; i < NDEVICES; i++) game.damage[i] = 0.0; // Set up assorted game parameters - game.battle.x = game.battle.y = 0; + invalidate(game.battle); game.state.date = game.indate = 100.0*(int)(31.0*Rand()+20.0); game.nkinks = game.nhelp = game.casual = game.abandoned = 0; game.iscate = game.resting = game.imine = game.icrystl = game.icraft = false; @@ -366,7 +366,7 @@ void setup(bool needprompt) thing = randplace(GALSIZE); } else - thing.x = thing.y = 0; + invalidate(thing); skip(2); game.state.snap = false; @@ -562,10 +562,10 @@ void newqad(bool shutup) struct quadrant *q; game.justin = true; - game.base.x = game.base.y = 0; + invalidate(game.base); game.klhere = 0; game.comhere = false; - game.plnet.x = game.plnet.y = 0; + invalidate(game.plnet); game.ishere = false; game.irhere = 0; game.iplnet = 0; diff --git a/src/sst.h b/src/sst.h index a397ed2..a9bf781 100644 --- a/src/sst.h +++ b/src/sst.h @@ -45,6 +45,8 @@ typedef struct {int x; int y;} coord; #define square(i) ((i)*(i)) #define same(c1, c2) ((c1.x == c2.x) && (c1.y == c2.y)) #define distance(c1, c2) sqrt(square(c1.x - c2.x) + square(c1.y - c2.y)) +#define invalidate(w) w.x = w.y = 0 +#define is_valid(w) (w.x == 0 || w.y == 0) typedef struct { coord w;