… | |
… | |
2225 | * (the opposite of crossfire's find_dir_2!) |
2225 | * (the opposite of crossfire's find_dir_2!) |
2226 | */ |
2226 | */ |
2227 | int |
2227 | int |
2228 | find_dir_2 (int x, int y) |
2228 | find_dir_2 (int x, int y) |
2229 | { |
2229 | { |
|
|
2230 | #if 1 // new algorithm |
|
|
2231 | // this works by putting x, y into 16 sectors, which |
|
|
2232 | // are not equal sized, but are a better approximation |
|
|
2233 | // then the old algorithm, and then using a mapping |
|
|
2234 | // table to map it into a direction value. |
|
|
2235 | |
|
|
2236 | static const uint8 dir[16] = { |
|
|
2237 | 4, 5, 4, 3, |
|
|
2238 | 2, 1, 2, 3, |
|
|
2239 | 6, 5, 6, 7, |
|
|
2240 | 8, 1, 8, 7, |
|
|
2241 | }; |
|
|
2242 | int sector = 0; |
|
|
2243 | |
|
|
2244 | // this is a bit ugly, but more likely to result in branchless code |
|
|
2245 | sector |= x < 0 ? 8 : 0; |
|
|
2246 | x = x < 0 ? -x : x; // abs |
|
|
2247 | |
|
|
2248 | sector |= y < 0 ? 4 : 0; |
|
|
2249 | y = y < 0 ? -y : y; // abs |
|
|
2250 | |
|
|
2251 | if (x > y) |
|
|
2252 | { |
|
|
2253 | sector |= 2; |
|
|
2254 | |
|
|
2255 | if (x > y * 2) |
|
|
2256 | sector |= 1; |
|
|
2257 | } |
|
|
2258 | else |
|
|
2259 | { |
|
|
2260 | if (y > x * 2) |
|
|
2261 | sector |= 1; |
|
|
2262 | else if (!y) |
|
|
2263 | return 0; // x == 0 here |
|
|
2264 | } |
|
|
2265 | |
|
|
2266 | return dir [sector]; |
|
|
2267 | #else // old algorithm |
2230 | int q; |
2268 | int q; |
2231 | |
2269 | |
2232 | if (y) |
2270 | if (y) |
2233 | q = 128 * x / y; |
2271 | q = 128 * x / y; |
2234 | else if (x) |
2272 | else if (x) |
… | |
… | |
2252 | if (q < 52) return 1; |
2290 | if (q < 52) return 1; |
2253 | if (q < 309) return 8; |
2291 | if (q < 309) return 8; |
2254 | |
2292 | |
2255 | return 7; |
2293 | return 7; |
2256 | } |
2294 | } |
|
|
2295 | #endif |
2257 | } |
2296 | } |
2258 | |
2297 | |
2259 | /* |
2298 | /* |
2260 | * dirdiff(dir1, dir2) returns how many 45-degrees differences there is |
2299 | * dirdiff(dir1, dir2) returns how many 45-degrees differences there is |
2261 | * between two directions (which are expected to be absolute (see absdir()) |
2300 | * between two directions (which are expected to be absolute (see absdir()) |