From 38d50b4e0df9c9baab31313c1d1db2b678a72a3a Mon Sep 17 00:00:00 2001 From: Robert Vollmert Date: Sun, 11 Oct 2009 19:03:45 +0200 Subject: Make advance direction and quadrants explicit as enums. --- crawl-ref/source/los.cc | 10 +++--- crawl-ref/source/ray.cc | 89 ++++++++++++++++++++++++------------------------- crawl-ref/source/ray.h | 32 +++++++++++++----- 3 files changed, 72 insertions(+), 59 deletions(-) (limited to 'crawl-ref') diff --git a/crawl-ref/source/los.cc b/crawl-ref/source/los.cc index e89a75f239..48c7596b23 100644 --- a/crawl-ref/source/los.cc +++ b/crawl-ref/source/los.cc @@ -199,7 +199,7 @@ static std::vector _find_nonduped_cellrays() static void _register_ray(double accx, double accy, double slope) { coord_def pos[LOS_MAX_RANGE * 2 + 1]; - ray_def ray = ray_def(accx, accy, slope, 0); + ray_def ray = ray_def(accx, accy, slope, QUAD_SE); // find out which cells the ray passes through int raylen = ray.footprint(LOS_RADIUS2, pos); @@ -359,13 +359,13 @@ void raycast() static void _set_ray_quadrant(ray_def& ray, int sx, int sy, int tx, int ty) { if ( tx >= sx && ty >= sy ) - ray.quadrant = 0; + ray.quadrant = QUAD_SE; else if ( tx < sx && ty >= sy ) - ray.quadrant = 1; + ray.quadrant = QUAD_SW; else if ( tx < sx && ty < sy ) - ray.quadrant = 2; + ray.quadrant = QUAD_NW; else if ( tx >= sx && ty < sy ) - ray.quadrant = 3; + ray.quadrant = QUAD_NE; else mpr("Bad ray quadrant!", MSGCH_DIAGNOSTICS); } 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(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(); diff --git a/crawl-ref/source/ray.h b/crawl-ref/source/ray.h index 201a92edf1..c55ff1390e 100644 --- a/crawl-ref/source/ray.h +++ b/crawl-ref/source/ray.h @@ -7,8 +7,22 @@ #ifndef RAY_H #define RAY_H -int shoot_ray(double accx, double accy, const double slope, - int maxrange, int xpos[], int ypos[]); +// quadrant +enum quad_type +{ + QUAD_SE = 0, + QUAD_SW = 1, + QUAD_NW = 2, + QUAD_NE = 3 +}; + +// direction of advance: +enum adv_type +{ + ADV_X = 0, // changed x + ADV_Y = 1, // changed y + ADV_XY = 2 // changed x and y (diagonal) +}; struct ray_def { @@ -16,19 +30,19 @@ public: double accx; double accy; double slope; - int quadrant; // 0 down-right, 1 down-left, 2 up-left, 3 up-right + quad_type quadrant; int fullray_idx; // for cycling: where did we come from? public: ray_def(double accx = 0.0, double accy = 0.0, double slope = 0.0, - int quadrant = 0, int fullray_idx = -1); + quad_type quadrant = QUAD_SE, int fullray_idx = -1); int x() const; int y() const; coord_def pos() const; - // returns the direction taken (0,1,2) - int advance(bool shorten = false, const coord_def *p = NULL); - int advance_through(const coord_def &point); + // returns the direction taken + adv_type advance(bool shorten = false, const coord_def *p = NULL); + adv_type advance_through(const coord_def &point); void advance_and_bounce(); void regress(); @@ -40,9 +54,9 @@ public: void set_degrees(double deg); private: - int raw_advance_0(); + adv_type raw_advance_0(); void flip(); - int raw_advance(); + adv_type raw_advance(); double reflect(bool x, double oldc, double newc) const; void set_reflect_point(const double oldx, const double oldy, bool blocked_x, bool blocked_y); -- cgit v1.2.3-54-g00ecf