Abstract away the operations involving a sentinel value for coordinates.
authorEric S. Raymond <esr@thyrsus.com>
Fri, 22 Sep 2006 11:38:54 +0000 (11:38 +0000)
committerEric S. Raymond <esr@thyrsus.com>
Fri, 22 Sep 2006 11:38:54 +0000 (11:38 +0000)
Besides making the code more readable, the sentinel may change someday
if we go zero-origin everywhere.

src/battle.c
src/events.c
src/moving.c
src/planets.c
src/setup.c
src/sst.h

index 9fc6082a3a7ff11213bd77dddf86697b974622d3..f4c6e9070bb6069f67debf3d418621ece7ee7c15 100644 (file)
@@ -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 */
index b4d465888957c1a71c68c0db4394e7d92b19ad84..7acb042fa8ba76803a2e57773b68f6fcb51e9a1b 100644 (file)
@@ -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;
            }
index 948cdb40c73402a78f68940cda09899fae906876..3373b37b0738d29c4f65c4800fa99afcf41a736f 100644 (file)
@@ -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;
index a34b1f694b06b0c68a4f2faf5de49fc2875524d7..ca8996fc360ceb843031638b84ffe855ef56b196 100644 (file)
@@ -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);
index 741f35f00016b627ab1fef2bed17e75f6d0364fd..44e378b28dcaae0a25da6a1dade0c4b13734baa9 100644 (file)
@@ -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;
index a397ed25633ad63db6a05fad108280b8c47ae0b6..a9bf781e546a588bcd558fdde1a99ff2441fb253 100644 (file)
--- 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;