… | |
… | |
445 | { |
445 | { |
446 | while (op) |
446 | while (op) |
447 | { |
447 | { |
448 | // adjust by actual difference to account for rounding errors |
448 | // adjust by actual difference to account for rounding errors |
449 | // i.e. (w2 - w1) / f != w2 / f - w1 / f and the latter is correct |
449 | // i.e. (w2 - w1) / f != w2 / f - w1 / f and the latter is correct |
450 | weight = weight_adjust_for (op, op->carrying) |
450 | weight = weight_adjust_for (op, op->carrying + weight) |
451 | - weight_adjust_for (op, op->carrying - weight); |
451 | - weight_adjust_for (op, op->carrying); |
452 | |
452 | |
453 | if (!weight) |
453 | if (!weight) |
454 | return; |
454 | return; |
455 | |
455 | |
456 | op->carrying += weight; |
456 | op->carrying += weight; |
… | |
… | |
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 | // basically, it maps these comparisons to each bit |
|
|
2236 | // bit #3: x < 0 |
|
|
2237 | // bit #2: y < 0 |
|
|
2238 | // bit #1: x > y |
|
|
2239 | // bit #0: x > 2y |
|
|
2240 | |
|
|
2241 | static const uint8 dir[16] = { |
|
|
2242 | 4, 5, 4, 3, |
|
|
2243 | 2, 1, 2, 3, |
|
|
2244 | 6, 5, 6, 7, |
|
|
2245 | 8, 1, 8, 7, |
|
|
2246 | }; |
|
|
2247 | int sector = 0; |
|
|
2248 | |
|
|
2249 | // this is a bit ugly, but more likely to result in branchless code |
|
|
2250 | sector |= x < 0 ? 8 : 0; |
|
|
2251 | x = x < 0 ? -x : x; // abs |
|
|
2252 | |
|
|
2253 | sector |= y < 0 ? 4 : 0; |
|
|
2254 | y = y < 0 ? -y : y; // abs |
|
|
2255 | |
|
|
2256 | if (x > y) |
|
|
2257 | { |
|
|
2258 | sector |= 2; |
|
|
2259 | |
|
|
2260 | if (x > y * 2) |
|
|
2261 | sector |= 1; |
|
|
2262 | } |
|
|
2263 | else |
|
|
2264 | { |
|
|
2265 | if (y > x * 2) |
|
|
2266 | sector |= 1; |
|
|
2267 | else if (!y) |
|
|
2268 | return 0; // x == 0 here |
|
|
2269 | } |
|
|
2270 | |
|
|
2271 | return dir [sector]; |
|
|
2272 | #else // old algorithm |
2230 | int q; |
2273 | int q; |
2231 | |
2274 | |
2232 | if (y) |
2275 | if (y) |
2233 | q = 128 * x / y; |
2276 | q = 128 * x / y; |
2234 | else if (x) |
2277 | else if (x) |
… | |
… | |
2252 | if (q < 52) return 1; |
2295 | if (q < 52) return 1; |
2253 | if (q < 309) return 8; |
2296 | if (q < 309) return 8; |
2254 | |
2297 | |
2255 | return 7; |
2298 | return 7; |
2256 | } |
2299 | } |
|
|
2300 | #endif |
2257 | } |
2301 | } |
2258 | |
2302 | |
2259 | /* |
2303 | /* |
2260 | * dirdiff(dir1, dir2) returns how many 45-degrees differences there is |
2304 | * dirdiff(dir1, dir2) returns how many 45-degrees differences there is |
2261 | * between two directions (which are expected to be absolute (see absdir()) |
2305 | * between two directions (which are expected to be absolute (see absdir()) |