diff options
author | Robert Vollmert <rvollmert@gmx.net> | 2009-10-13 18:05:01 +0200 |
---|---|---|
committer | Robert Vollmert <rvollmert@gmx.net> | 2009-10-13 18:05:01 +0200 |
commit | f022cb01ddd28932ce70f7738f8b9be90acae7ca (patch) | |
tree | 31db97dd22b36e22058bc78112d4a9e874b316b9 /crawl-ref/source/los.cc | |
parent | 7c413e4ddfec30e1f495c2c74368afcd31b9e3bd (diff) | |
download | crawl-ref-f022cb01ddd28932ce70f7738f8b9be90acae7ca.tar.gz crawl-ref-f022cb01ddd28932ce70f7738f8b9be90acae7ca.zip |
Move imbalance calculation into separate function.
Diffstat (limited to 'crawl-ref/source/los.cc')
-rw-r--r-- | crawl-ref/source/los.cc | 62 |
1 files changed, 34 insertions, 28 deletions
diff --git a/crawl-ref/source/los.cc b/crawl-ref/source/los.cc index 6a3d6204c1..849cee39e2 100644 --- a/crawl-ref/source/los.cc +++ b/crawl-ref/source/los.cc @@ -491,7 +491,7 @@ static double _calc_slope(double x, double y) return (VERTICAL_SLOPE); const double slope = y / x; - return (slope > VERTICAL_SLOPE? VERTICAL_SLOPE : slope); + return (slope > VERTICAL_SLOPE ? VERTICAL_SLOPE : slope); } static double _slope_factor(const ray_def &ray) @@ -518,6 +518,36 @@ static bool _superior_ray(int shortest, int imbalance, return (slope_diff > ray_slope_diff); } +// Compute the imbalance, defined as the +// number of consecutive diagonal or orthogonal moves +// in the ray. This is a reasonable measure of deviation from +// the Bresenham line between our selected source and +// destination. +int _imbalance(const std::vector<coord_def>& ray) +{ + int imb = 0; + int diags = 0, straights = 0; + for (int i = 1, size = ray.size(); i < size; ++i) + { + const int dist = + (ray[i] - ray[i - 1]).abs(); + + if (dist == 2) + { + straights = 0; + if (++diags > imb) + imb = diags; + } + else + { + diags = 0; + if (++straights > imb) + imb = straights; + } + } + return imb; +} + // Find a nonblocked ray from source to target. Return false if no // such ray could be found, otherwise return true and fill ray // appropriately. @@ -609,35 +639,11 @@ bool find_ray(const coord_def& source, const coord_def& target, } } - int cimbalance = 0; // If this ray is a candidate for shortest, calculate - // the imbalance. I'm defining 'imbalance' as the - // number of consecutive diagonal or orthogonal moves - // in the ray. This is a reasonable measure of deviation from - // the Bresenham line between our selected source and - // destination. + // the imbalance. + int cimbalance = 0; if (!blocked && find_shortest && shortest >= real_length) - { - int diags = 0, straights = 0; - for (int i = 1, size = unaliased_ray.size(); i < size; ++i) - { - const int dist = - (unaliased_ray[i] - unaliased_ray[i - 1]).abs(); - - if (dist == 2) - { - straights = 0; - if (++diags > cimbalance) - cimbalance = diags; - } - else - { - diags = 0; - if (++straights > cimbalance) - cimbalance = straights; - } - } - } + cimbalance = _imbalance(unaliased_ray); const double ray_slope_diff = find_shortest ? fabs(_slope_factor(lray) - want_slope) : 0.0; |