diff options
author | Robert Vollmert <rvollmert@gmx.net> | 2009-10-11 19:03:45 +0200 |
---|---|---|
committer | Robert Vollmert <rvollmert@gmx.net> | 2009-10-11 21:30:30 +0200 |
commit | 38d50b4e0df9c9baab31313c1d1db2b678a72a3a (patch) | |
tree | 7a48f2066fe4b1ba46fdfa39bb7895cf72acf80a /crawl-ref/source/ray.cc | |
parent | 4783b4ed0e228b3de642d71c6554ffcf60ab7858 (diff) | |
download | crawl-ref-38d50b4e0df9c9baab31313c1d1db2b678a72a3a.tar.gz crawl-ref-38d50b4e0df9c9baab31313c1d1db2b678a72a3a.zip |
Make advance direction and quadrants explicit as enums.
Diffstat (limited to 'crawl-ref/source/ray.cc')
-rw-r--r-- | crawl-ref/source/ray.cc | 89 |
1 files changed, 44 insertions, 45 deletions
diff --git a/crawl-ref/source/ray.cc b/crawl-ref/source/ray.cc index 804090b4dc..6fea3ec663 100644 --- a/crawl-ref/source/ray.cc +++ b/crawl-ref/source/ray.cc @@ -18,7 +18,7 @@ int ifloor(double d) return static_cast<int>(floor(d)); } -ray_def::ray_def(double ax, double ay, double s, int q, int idx) +ray_def::ray_def(double ax, double ay, double s, quad_type q, int idx) : accx(ax), accy(ay), slope(s), quadrant(q), fullray_idx(idx) { } @@ -83,14 +83,17 @@ void ray_def::set_reflect_point(const double oldx, const double oldy, void ray_def::advance_and_bounce() { - // 0 = down-right, 1 = down-left, 2 = up-left, 3 = up-right - int bouncequad[4][3] = + quad_type bouncequad[4][3] = { - { 1, 3, 2 }, { 0, 2, 3 }, { 3, 1, 0 }, { 2, 0, 1 } + // ADV_X ADV_Y ADV_XY + { QUAD_SW, QUAD_NE, QUAD_NW }, // QUAD_SE + { QUAD_SE, QUAD_NW, QUAD_NE }, // QUAD_SW + { QUAD_NE, QUAD_SW, QUAD_SE }, // QUAD_NW + { QUAD_NW, QUAD_SE, QUAD_SW } // QUAD_NE }; int oldx = x(), oldy = y(); const double oldaccx = accx, oldaccy = accy; - int rc = advance(false); + adv_type rc = advance(false); int newx = x(), newy = y(); ASSERT(grid_is_solid(grd[newx][newy])); @@ -98,18 +101,18 @@ void ray_def::advance_and_bounce() const bool blocked_y = grid_is_solid(grd[newx][oldy]); if (double_is_zero(slope) || slope > 100.0) - quadrant = bouncequad[quadrant][2]; - else if ( rc != 2 ) + quadrant = bouncequad[quadrant][ADV_XY]; + else if (rc != ADV_XY) quadrant = bouncequad[quadrant][rc]; else { ASSERT((oldx != newx) && (oldy != newy)); - if ( blocked_x && blocked_y ) + if (blocked_x && blocked_y) quadrant = bouncequad[quadrant][rc]; - else if ( blocked_x ) - quadrant = bouncequad[quadrant][1]; + else if (blocked_x) + quadrant = bouncequad[quadrant][ADV_Y]; else - quadrant = bouncequad[quadrant][0]; + quadrant = bouncequad[quadrant][ADV_X]; } set_reflect_point(oldaccx, oldaccy, blocked_x, blocked_y); @@ -119,37 +122,33 @@ double ray_def::get_degrees() const { if (slope > 100.0) { - if (quadrant == 3 || quadrant == 2) + if (quadrant == QUAD_NW || quadrant == QUAD_NE) return (90.0); else return (270.0); } else if (double_is_zero(slope)) { - if (quadrant == 0 || quadrant == 3) + if (quadrant == QUAD_SE || quadrant == QUAD_NE) return (0.0); else return (180.0); } - double deg = atan(slope) * 180.0 / M_PI; + double deg = atan(slope) * 180.0 / M_PI; // 0 < deg < 90 switch (quadrant) { - case 0: + case QUAD_SE: return (360.0 - deg); - - case 1: + case QUAD_SW: return (180.0 + deg); - - case 2: + case QUAD_NW: return (180.0 - deg); - - case 3: + case QUAD_NE: + default: return (deg); } - ASSERT(!"ray has illegal quadrant"); - return (0.0); } void ray_def::set_degrees(double deg) @@ -166,27 +165,27 @@ void ray_def::set_degrees(double deg) slope = 0.0; if (deg < 90.0 || deg > 270.0) - quadrant = 0; // right/east + quadrant = QUAD_SE; else - quadrant = 1; // left/west + quadrant = QUAD_SW; } else if (_slope > 0) { slope = _slope; if (deg >= 180.0 && deg <= 270.0) - quadrant = 1; + quadrant = QUAD_SW; else - quadrant = 3; + quadrant = QUAD_NE; } else { slope = -_slope; if (deg >= 90 && deg <= 180) - quadrant = 2; + quadrant = QUAD_NW; else - quadrant = 0; + quadrant = QUAD_SE; } if (slope > 1000.0) @@ -195,18 +194,18 @@ void ray_def::set_degrees(double deg) void ray_def::regress() { - int opp_quadrant[4] = { 2, 3, 0, 1 }; + quad_type opp_quadrant[4] = { QUAD_NW, QUAD_NE, QUAD_SE, QUAD_SW }; quadrant = opp_quadrant[quadrant]; advance(false); quadrant = opp_quadrant[quadrant]; } -int ray_def::advance_through(const coord_def &target) +adv_type ray_def::advance_through(const coord_def &target) { return (advance(true, &target)); } -int ray_def::advance(bool shortest_possible, const coord_def *target) +adv_type ray_def::advance(bool shortest_possible, const coord_def *target) { if (!shortest_possible) return (raw_advance()); @@ -215,18 +214,18 @@ int ray_def::advance(bool shortest_possible, const coord_def *target) // step ahead and see if we can get a diagonal. const coord_def old = pos(); - const int ret = raw_advance(); + const adv_type ret = raw_advance(); - if (ret == 2 || (target && pos() == *target)) + if (ret == ADV_XY || (target && pos() == *target)) return (ret); const double maccx = accx, maccy = accy; - if (raw_advance() != 2) + if (raw_advance() != ADV_XY) { const coord_def second = pos(); // If we can convert to a diagonal, do so. if ((second - old).abs() == 2) - return (2); + return (ADV_XY); } // No diagonal, so roll back. @@ -240,18 +239,18 @@ int ray_def::advance(bool shortest_possible, const coord_def *target) // note that slope must be nonnegative! // returns 0 if the advance was in x, 1 if it was in y, 2 if it was // the diagonal -int ray_def::raw_advance_0() +adv_type ray_def::raw_advance_0() { // handle perpendiculars if (double_is_zero(slope)) { accx += 1.0; - return 0; + return ADV_X; } if (slope > 100.0) { accy += 1.0; - return 1; + return ADV_Y; } const double xtarget = ifloor(accx) + 1; @@ -274,21 +273,21 @@ int ray_def::raw_advance_0() accx = xtarget + EPSILON_VALUE * 2 / slope; accy = ytarget + EPSILON_VALUE * 2; } - return 2; + return ADV_XY; } // move to the boundary double traveldist; - int rc = -1; + adv_type rc; if (distdiff > 0.0) { traveldist = ydistance / slope; - rc = 1; + rc = ADV_Y; } else { traveldist = xdistance; - rc = 0; + rc = ADV_X; } // and a little into the next cell, taking care @@ -314,9 +313,9 @@ void ray_def::flip() accy *= signy[quadrant]; } -int ray_def::raw_advance() +adv_type ray_def::raw_advance() { - int rc; + adv_type rc; flip(); rc = raw_advance_0(); flip(); |