More uses of distance() macro. Eliminate some magic numbers.
authorEric S. Raymond <esr@thyrsus.com>
Wed, 20 Sep 2006 03:57:04 +0000 (03:57 +0000)
committerEric S. Raymond <esr@thyrsus.com>
Wed, 20 Sep 2006 03:57:04 +0000 (03:57 +0000)
src/battle.c
src/io.c
src/moving.c
src/planets.c
src/setup.c
src/sst.h

index a1bacfab1a672ad652e3230247f4b6109c463104..20b35ef3b296894d12a919be851b7c8e1e0192bf 100644 (file)
@@ -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 */
 {
 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;
     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];
     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;
     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];
        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);
        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;
            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;
            }
                finish(FHOLE);
                return;
            }
-           if (game.quad[jx][jy]!=IHDOT) {
+           if (game.quad[jw.x][jw.y]!=IHDOT) {
                /* can't move into object */
                return;
            }
                /* 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;
            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;
            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;
            }
                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."));
                prout(_(" buffeted into black hole."));
-               deadkl(w, iquad, jx, jy);
+               deadkl(w, iquad, jw.x, jw.y);
                return;
            }
                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--"));
                /* 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 */
            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;
     }
     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();
        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))
        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
            proutn(_("%d unit hit on "), (int)hit);
        }
        else
index 3db1c459b8d62f4a5fa3f11493ca632eb663465e..b60fec51645f9d8ba8c597c14e63e28677f1814c 100644 (file)
--- 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);
 }
 
     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);
 /* 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);
        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);
     }
        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) {
 /* 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);
            }
        } 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) {
     } 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)){
                delay(400);
            }
            if ((iquad==IHDOT)||(iquad==IHBLANK)){
-               put_srscan_sym(ix, iy, '+');
+               put_srscan_sym(w, '+');
                sound(l*10);
                delay(100);
                nosound();
                sound(l*10);
                delay(100);
                nosound();
-               put_srscan_sym(ix, iy, iquad);
+               put_srscan_sym(w, iquad);
            }
            else {
                wattron(curwnd, A_REVERSE);
            }
            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);
                sound(500);
                delay(1000);
                nosound();
                wattroff(curwnd, A_REVERSE);
-               put_srscan_sym(ix, iy, iquad);
+               put_srscan_sym(w, iquad);
            }
        } else {
            }
        } else {
-           proutn("%d - %d   ", ix, iy);
+           proutn("%d - %d   ", w.x, w.y);
        }
     }
 }
        }
     }
 }
index 99c18916da63bfaea1a42a31084b3d08ea53e4b0..348d1655a193c1db69ac5fd7bc73c0fe8fdb0c95 100644 (file)
@@ -8,10 +8,10 @@ void imove(void)
 /* movement execution for warp, impule, supernova, and tractor-beam events */
 {
     double angle, deltax, deltay, bigger, x, y,
 /* 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;
     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) {
 
     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) {
                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
                    }
                    /*
                     * 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;
            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 */
                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:
                case IHS:
                case IHR:
                case IHQUEST:
-                   game.sector.x = w.x;
-                   game.sector.y = w.y;
+                   game.sector = w;
                    ram(0, iquad, game.sector);
                    ram(0, iquad, game.sector);
-                   finalx = game.sector.x;
-                   finaly = game.sector.y;
+                   final = game.sector;
                    break;
                case IHBLANK:
                    skip(1);
                    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;
                    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;
                    if (game.energy <= 0) {
                        finish(FNRG);
                        return;
@@ -187,20 +182,16 @@ void imove(void)
                goto no_quad_change;    /* sorry! */
            }
        }
                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) {
 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;
        }
            game.kavgd[m] = 0.5 * (finald+game.kdist[m]);
            game.kdist[m] = finald;
        }
@@ -718,7 +709,7 @@ void atover(bool igrab)
            }
            else {
                prout(_("saved."));
            }
            else {
                prout(_("saved."));
-               game.icrystl = 1;
+               game.icrystl = true;
            }
        }
     }
            }
        }
     }
index fd10f67a1a07e75ec254465f7174b99f7db3ba4d..e0c75a681dc2e3e75a974542ace0f1df7937a8e8 100644 (file)
@@ -181,7 +181,7 @@ void beam(void)
        prout(_("The shuttle craft Galileo is here!"));
     }
     if (game.landed!=1 && game.imine) {
        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;
        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;
     }
        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);
        proutn(_("With all those fresh crystals aboard the "));
        crmshp();
        skip(1);
@@ -229,7 +229,7 @@ void usecrystals(void)
     game.ididit = false;
     skip(1);
     chew();
     game.ididit = false;
     skip(1);
     chew();
-    if (game.icrystl!=1) {
+    if (!game.icrystl) {
        prout(_("No dilithium crystals available."));
        return;
     }
        prout(_("No dilithium crystals available."));
        return;
     }
@@ -364,7 +364,7 @@ void shuttle(void)
            game.iscraft = 1;
            game.icraft = false;
            if (game.imine) {
            game.iscraft = 1;
            game.icraft = false;
            if (game.imine) {
-               game.icrystl = 1;
+               game.icrystl = true;
                game.cryprob = 0.05;
            }
            game.imine = false;
                game.cryprob = 0.05;
            }
            game.imine = false;
index 80ab2e4bca4db1d3ec9e0b3248ec16a15c4f2da1..f6a0ae10ce5aa15d20762068d28f0daceca1a97d 100644 (file)
@@ -177,7 +177,7 @@ void abandn(void)
 
        /* If at least one base left, give 'em the Faerie Queene */
        skip(1);
 
        /* 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.");
        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.");
     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 */
     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.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;
     game.state.starkl = game.state.basekl = 0;
     game.iscraft = 1;
     game.landed = -1;
index 07e7a2bc37d3516b143435e31bc7bdd7cd9a16b7..89cb16f69c58261d0f9bdede84062b9f1af2dce4 100644 (file)
--- a/src/sst.h
+++ b/src/sst.h
@@ -222,6 +222,7 @@ struct game {
        isarmed,        // probe is armed
        inorbit,        // orbiting a planet
        imine,          // mining
        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
        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])
        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
        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 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);
 void cgetline(char *, int);
 void waitfor(void);
 void setpassword(void);