diff options
author | Robert Vollmert <rvollmert@gmx.net> | 2009-10-14 14:31:46 +0200 |
---|---|---|
committer | Robert Vollmert <rvollmert@gmx.net> | 2009-10-14 15:06:35 +0200 |
commit | c4e7e2eae39db0f26e3106d09bcd90298fa739ab (patch) | |
tree | e6e220d9e231c89cd03b6ca51c3a88f1f25831a9 /crawl-ref/source | |
parent | 2f6262a7da259da4e7f09536580247f541cdd4a6 (diff) | |
download | crawl-ref-c4e7e2eae39db0f26e3106d09bcd90298fa739ab.tar.gz crawl-ref-c4e7e2eae39db0f26e3106d09bcd90298fa739ab.zip |
Replace quadrants by signs.
Diffstat (limited to 'crawl-ref/source')
-rw-r--r-- | crawl-ref/source/los.cc | 23 | ||||
-rw-r--r-- | crawl-ref/source/ray.cc | 126 | ||||
-rw-r--r-- | crawl-ref/source/ray.h | 16 |
3 files changed, 58 insertions, 107 deletions
diff --git a/crawl-ref/source/los.cc b/crawl-ref/source/los.cc index 394ee1d96a..d5f5e9a84a 100644 --- a/crawl-ref/source/los.cc +++ b/crawl-ref/source/los.cc @@ -144,7 +144,7 @@ struct los_ray : public ray_def unsigned int length; los_ray(double ax, double ay, double s) - : ray_def(ax, ay, s, QUAD_SE), length(0) + : ray_def(ax, ay, s), length(0) { } @@ -158,7 +158,7 @@ struct los_ray : public ray_def int cellnum; for (cellnum = 0; true; ++cellnum) { - copy.raw_advance_0(); + copy.raw_advance_pos(); c = copy.pos(); if (c.abs() > radius2) break; @@ -455,16 +455,8 @@ 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 = QUAD_SE; - else if ( tx < sx && ty >= sy ) - ray.quadrant = QUAD_SW; - else if ( tx < sx && ty < sy ) - ray.quadrant = QUAD_NW; - else if ( tx >= sx && ty < sy ) - ray.quadrant = QUAD_NE; - else - mpr("Bad ray quadrant!", MSGCH_DIAGNOSTICS); + ray.quadx = tx >= sx ? 1 : -1; + ray.quady = ty >= sy ? 1 : -1; } static int _cyclic_offset(int i, int cycle_dir, int startpoint, @@ -689,6 +681,9 @@ bool find_ray(const coord_def& source, const coord_def& target, bool allow_fallback, ray_def& ray, int cycle_dir, bool find_shortest, bool ignore_solid) { + if (target == source) + return false; + const int signx = ((target.x - source.x >= 0) ? 1 : -1); const int signy = ((target.y - source.y >= 0) ? 1 : -1); const int absx = signx * (target.x - source.x); @@ -707,7 +702,8 @@ bool find_ray(const coord_def& source, const coord_def& target, if (signy < 0) ray.accy = 1.0 - ray.accy; - ray.quadrant = QUAD_SE; + ray.quadx = 1; + ray.quady = 1; if (!_find_ray_se(abs, allow_fallback, ray, cycle_dir, find_shortest, ignore_solid, t)) @@ -874,7 +870,6 @@ void losight(env_show_grid& sh, const los_param& dat) const int quadrant_x[4] = { 1, -1, -1, 1 }; const int quadrant_y[4] = { 1, 1, -1, -1 }; - for (int q = 0; q < 4; ++q) _losight_quadrant(sh, dat, quadrant_x[q], quadrant_y[q]); diff --git a/crawl-ref/source/ray.cc b/crawl-ref/source/ray.cc index 2d6ebfcabe..50195ed2bc 100644 --- a/crawl-ref/source/ray.cc +++ b/crawl-ref/source/ray.cc @@ -18,8 +18,8 @@ static int ifloor(double d) return static_cast<int>(floor(d)); } -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) +ray_def::ray_def(double ax, double ay, double s, int qx, int qy, int idx) + : accx(ax), accy(ay), slope(s), quadx(qx), quady(qy), fullray_idx(idx) { } @@ -83,14 +83,6 @@ void ray_def::set_reflect_point(const double oldx, const double oldy, void ray_def::advance_and_bounce() { - quad_type bouncequad[4][3] = - { - // 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; adv_type rc = advance(false); @@ -101,18 +93,26 @@ 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][ADV_XY]; - else if (rc != ADV_XY) - quadrant = bouncequad[quadrant][rc]; - else + { + quadx = -quadx; + quady = -quady; + } + else if (rc == ADV_X) + quadx = -quadx; + else if (rc == ADV_Y) + quady = -quady; + else // rc == ADV_XY { ASSERT((oldx != newx) && (oldy != newy)); if (blocked_x && blocked_y) - quadrant = bouncequad[quadrant][rc]; + { + quadx = -quadx; + quady = -quady; + } else if (blocked_x) - quadrant = bouncequad[quadrant][ADV_Y]; + quady = -quady; else - quadrant = bouncequad[quadrant][ADV_X]; + quadx = -quadx; } set_reflect_point(oldaccx, oldaccy, blocked_x, blocked_y); @@ -121,34 +121,17 @@ void ray_def::advance_and_bounce() double ray_def::get_degrees() const { if (slope > 100.0) - { - if (quadrant == QUAD_NW || quadrant == QUAD_NE) - return (90.0); - else - return (270.0); - } + return (quadx < 0 ? 90.0 : 270.0); else if (double_is_zero(slope)) - { - if (quadrant == QUAD_SE || quadrant == QUAD_NE) - return (0.0); - else - return (180.0); - } - - double deg = atan(slope) * 180.0 / M_PI; // 0 < deg < 90 - - switch (quadrant) - { - case QUAD_SE: - return (360.0 - deg); - case QUAD_SW: - return (180.0 + deg); - case QUAD_NW: - return (180.0 - deg); - case QUAD_NE: - default: - return (deg); - } + return (quady > 0 ? 0.0 : 180.0); + + // 0 < deg < 90 + double deg = atan(slope) * 180.0 / M_PI; + if (quadx < 0) + deg = 180.0 - deg; + if (quady < 0) + deg = 360.0 - deg; + return (deg); } void ray_def::set_degrees(double deg) @@ -158,46 +141,29 @@ void ray_def::set_degrees(double deg) while (deg >= 360.0) deg -= 360.0; - double _slope = tan(deg / 180.0 * M_PI); - - if (double_is_zero(_slope)) - { - slope = 0.0; - - if (deg < 90.0 || deg > 270.0) - quadrant = QUAD_SE; - else - quadrant = QUAD_SW; - } - else if (_slope > 0) + if (deg > 180.0) { - slope = _slope; - - if (deg >= 180.0 && deg <= 270.0) - quadrant = QUAD_SW; - else - quadrant = QUAD_NE; + quady = -1; + deg = 360 - deg; } - else + if (deg > 90.0) { - slope = -_slope; - - if (deg >= 90 && deg <= 180) - quadrant = QUAD_NW; - else - quadrant = QUAD_SE; + quadx = -1; + deg = 180 - deg; } + slope = tan(deg / 180.0 * M_PI); + if (double_is_zero(slope)) + slope = 0.0; if (slope > 1000.0) slope = 1000.0; } void ray_def::regress() { - quad_type opp_quadrant[4] = { QUAD_NW, QUAD_NE, QUAD_SE, QUAD_SW }; - quadrant = opp_quadrant[quadrant]; + quadx = -quadx; quady= -quady; advance(false); - quadrant = opp_quadrant[quadrant]; + quadx = -quadx; quady= -quady; } adv_type ray_def::advance_through(const coord_def &target) @@ -235,11 +201,11 @@ adv_type ray_def::advance(bool shortest_possible, const coord_def *target) return (ret); } -// Advance a ray in quadrant 0. +// Advance a ray in the positive quadrant. // 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 -adv_type ray_def::raw_advance_0() +adv_type ray_def::raw_advance_pos() { // handle perpendiculars if (double_is_zero(slope)) @@ -302,22 +268,20 @@ adv_type ray_def::raw_advance_0() } /* - * Mirror ray into quadrant 0 or back. - * this.quadrant itself is not touched (used for the flip back). + * Mirror ray into positive quadrant or back. + * this.quad{x,y} are not touched (used for the flip back). */ void ray_def::flip() { - int signx[] = {1, -1, -1, 1}; - int signy[] = {1, 1, -1, -1}; - accx = 0.5 + signx[quadrant] * (accx - 0.5); - accy = 0.5 + signy[quadrant] * (accy - 0.5); + accx = 0.5 + quadx * (accx - 0.5); + accy = 0.5 + quady * (accy - 0.5); } adv_type ray_def::raw_advance() { adv_type rc; flip(); - rc = raw_advance_0(); + rc = raw_advance_pos(); flip(); return rc; } diff --git a/crawl-ref/source/ray.h b/crawl-ref/source/ray.h index 0bf5609b7f..1565595d7f 100644 --- a/crawl-ref/source/ray.h +++ b/crawl-ref/source/ray.h @@ -7,15 +7,6 @@ #ifndef RAY_H #define RAY_H -// quadrant -enum quad_type -{ - QUAD_SE = 0, - QUAD_SW = 1, - QUAD_NW = 2, - QUAD_NE = 3 -}; - // direction of advance: enum adv_type { @@ -30,12 +21,13 @@ public: double accx; double accy; double slope; - quad_type quadrant; + int quadx; + int quady; 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, - quad_type quadrant = QUAD_SE, int fullray_idx = -1); + int quadx = 1, int quady = 1, int fullray_idx = -1); int x() const; int y() const; coord_def pos() const; @@ -54,7 +46,7 @@ public: void set_degrees(double deg); protected: - adv_type raw_advance_0(); + adv_type raw_advance_pos(); void flip(); adv_type raw_advance(); double reflect(bool x, double oldc, double newc) const; |