diff options
author | ultramage <ultramage@54d463be-8e91-2dee-dedb-b68131a5f0ec> | 2007-12-26 17:18:57 +0000 |
---|---|---|
committer | ultramage <ultramage@54d463be-8e91-2dee-dedb-b68131a5f0ec> | 2007-12-26 17:18:57 +0000 |
commit | 1e452a27de8de22d933f5b37c407aa3980948a66 (patch) | |
tree | f9403acedd93c5d4b5ea6b0d7eba5e30cb084cad /src/map/path.c | |
parent | 8cd1c4cafcf7aa6b49d26a4c81cb6ed6962b3c12 (diff) | |
download | hercules-1e452a27de8de22d933f5b37c407aa3980948a66.tar.gz hercules-1e452a27de8de22d933f5b37c407aa3980948a66.tar.bz2 hercules-1e452a27de8de22d933f5b37c407aa3980948a66.tar.xz hercules-1e452a27de8de22d933f5b37c407aa3980948a66.zip |
Moved distance-related functions to path.c/h
git-svn-id: https://rathena.svn.sourceforge.net/svnroot/rathena/trunk@11981 54d463be-8e91-2dee-dedb-b68131a5f0ec
Diffstat (limited to 'src/map/path.c')
-rw-r--r-- | src/map/path.c | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/map/path.c b/src/map/path.c index 52dcd0ac8..549144d7b 100644 --- a/src/map/path.c +++ b/src/map/path.c @@ -453,3 +453,46 @@ bool path_search(struct walkpath_data *wpd,int m,int x0,int y0,int x1,int y1,int return true; } + + +//Distance functions, taken from http://www.flipcode.com/articles/article_fastdistance.shtml +int check_distance(int dx, int dy, int distance) +{ +#ifdef CIRCULAR_AREA + //In this case, we just do a square comparison. Add 1 tile grace for diagonal range checks. + return (dx*dx + dy*dy <= distance*distance + (dx&&dy?1:0)); +#else + if (dx < 0) dx = -dx; + if (dy < 0) dy = -dy; + return ((dx<dy?dy:dx) <= distance); +#endif +} + +unsigned int distance(int dx, int dy) +{ +#ifdef CIRCULAR_AREA + unsigned int min, max; + + if ( dx < 0 ) dx = -dx; + if ( dy < 0 ) dy = -dy; + //There appears to be something wrong with the aproximation below when either dx/dy is 0! [Skotlex] + if ( dx == 0 ) return dy; + if ( dy == 0 ) return dx; + + if ( dx < dy ) + { + min = dx; + max = dy; + } else { + min = dy; + max = dx; + } + // coefficients equivalent to ( 123/128 * max ) and ( 51/128 * min ) + return ((( max << 8 ) + ( max << 3 ) - ( max << 4 ) - ( max << 1 ) + + ( min << 7 ) - ( min << 5 ) + ( min << 3 ) - ( min << 1 )) >> 8 ); +#else + if (dx < 0) dx = -dx; + if (dy < 0) dy = -dy; + return (dx<dy?dy:dx); +#endif +} |