|
|
1 | /* |
|
|
2 | * This file is part of Crossfire TRT, the Multiplayer Online Role Playing Game. |
|
|
3 | * |
|
|
4 | * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team |
|
|
5 | * |
|
|
6 | * Crossfire TRT is free software; you can redistribute it and/or modify it |
|
|
7 | * under the terms of the GNU General Public License as published by the Free |
|
|
8 | * Software Foundation; either version 2 of the License, or (at your option) |
|
|
9 | * any later version. |
|
|
10 | * |
|
|
11 | * This program is distributed in the hope that it will be useful, but |
|
|
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
|
|
13 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
|
14 | * for more details. |
|
|
15 | * |
|
|
16 | * You should have received a copy of the GNU General Public License along |
|
|
17 | * with Crossfire TRT; if not, write to the Free Software Foundation, Inc. 51 |
|
|
18 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
|
19 | * |
|
|
20 | * The authors can be reached via e-mail to <crossfire@schmorp.de> |
|
|
21 | */ |
|
|
22 | |
1 | #ifndef UTIL_H__ |
23 | #ifndef UTIL_H__ |
2 | #define UTIL_H__ |
24 | #define UTIL_H__ |
3 | |
25 | |
4 | //#define PREFER_MALLOC |
26 | //#define PREFER_MALLOC |
5 | |
27 | |
6 | #if __GNUC__ >= 3 |
28 | #if __GNUC__ >= 3 |
7 | # define is_constant(c) __builtin_constant_p (c) |
29 | # define is_constant(c) __builtin_constant_p (c) |
|
|
30 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
31 | # define prefetch(addr,rw,locality) __builtin_prefetch (addr, rw, locality) |
8 | #else |
32 | #else |
9 | # define is_constant(c) 0 |
33 | # define is_constant(c) 0 |
|
|
34 | # define expect(expr,value) (expr) |
|
|
35 | # define prefetch(addr,rw,locality) |
10 | #endif |
36 | #endif |
|
|
37 | |
|
|
38 | // put into ifs if you are very sure that the expression |
|
|
39 | // is mostly true or mosty false. note that these return |
|
|
40 | // booleans, not the expression. |
|
|
41 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
42 | #define expect_true(expr) expect ((expr) != 0, 1) |
11 | |
43 | |
12 | #include <cstddef> |
44 | #include <cstddef> |
13 | #include <cmath> |
45 | #include <cmath> |
14 | #include <new> |
46 | #include <new> |
15 | #include <vector> |
47 | #include <vector> |
… | |
… | |
18 | |
50 | |
19 | #include <shstr.h> |
51 | #include <shstr.h> |
20 | #include <traits.h> |
52 | #include <traits.h> |
21 | |
53 | |
22 | // use a gcc extension for auto declarations until ISO C++ sanctifies them |
54 | // use a gcc extension for auto declarations until ISO C++ sanctifies them |
23 | #define AUTODECL(var,expr) typeof(expr) var = (expr) |
55 | #define auto(var,expr) typeof(expr) var = (expr) |
24 | |
56 | |
25 | // very ugly macro that basicaly declares and initialises a variable |
57 | // very ugly macro that basicaly declares and initialises a variable |
26 | // that is in scope for the next statement only |
58 | // that is in scope for the next statement only |
27 | // works only for stuff that can be assigned 0 and converts to false |
59 | // works only for stuff that can be assigned 0 and converts to false |
28 | // (note: works great for pointers) |
60 | // (note: works great for pointers) |
… | |
… | |
45 | template<typename T, typename U> static inline T max (T a, U b) { return (U)a > b ? (U)a : b; } |
77 | template<typename T, typename U> static inline T max (T a, U b) { return (U)a > b ? (U)a : b; } |
46 | template<typename T, typename U, typename V> static inline T clamp (T v, U a, V b) { return v < (T)a ? (T)a : v >(T)b ? (T)b : v; } |
78 | template<typename T, typename U, typename V> static inline T clamp (T v, U a, V b) { return v < (T)a ? (T)a : v >(T)b ? (T)b : v; } |
47 | |
79 | |
48 | template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } |
80 | template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } |
49 | |
81 | |
|
|
82 | template<typename T> |
|
|
83 | static inline T |
|
|
84 | lerp (T val, T min_in, T max_in, T min_out, T max_out) |
|
|
85 | { |
|
|
86 | return (val - min_in) * (max_out - min_out) / (max_in - min_in) + min_out; |
|
|
87 | } |
|
|
88 | |
50 | // lots of stuff taken from FXT |
89 | // lots of stuff taken from FXT |
51 | |
90 | |
52 | /* Rotate right. This is used in various places for checksumming */ |
91 | /* Rotate right. This is used in various places for checksumming */ |
53 | //TODO: this sucks, use a better checksum algo |
92 | //TODO: that sucks, use a better checksum algo |
54 | static inline uint32_t |
93 | static inline uint32_t |
55 | rotate_right (uint32_t c) |
94 | rotate_right (uint32_t c, uint32_t count = 1) |
56 | { |
95 | { |
57 | return (c << 31) | (c >> 1); |
96 | return (c << (32 - count)) | (c >> count); |
|
|
97 | } |
|
|
98 | |
|
|
99 | static inline uint32_t |
|
|
100 | rotate_left (uint32_t c, uint32_t count = 1) |
|
|
101 | { |
|
|
102 | return (c >> (32 - count)) | (c << count); |
58 | } |
103 | } |
59 | |
104 | |
60 | // Return abs(a-b) |
105 | // Return abs(a-b) |
61 | // Both a and b must not have the most significant bit set |
106 | // Both a and b must not have the most significant bit set |
62 | static inline uint32_t |
107 | static inline uint32_t |
… | |
… | |
256 | |
301 | |
257 | void seed (uint32_t seed); |
302 | void seed (uint32_t seed); |
258 | uint32_t next (); |
303 | uint32_t next (); |
259 | |
304 | |
260 | // uniform distribution |
305 | // uniform distribution |
261 | uint32_t operator ()(uint32_t r_max) |
306 | uint32_t operator ()(uint32_t num) |
262 | { |
307 | { |
263 | return is_constant (r_max) |
308 | return is_constant (num) |
264 | ? this->next () % r_max |
309 | ? (next () * (uint64_t)num) >> 32U |
265 | : get_range (r_max); |
310 | : get_range (num); |
266 | } |
311 | } |
267 | |
312 | |
268 | // return a number within (min .. max) |
313 | // return a number within (min .. max) |
269 | int operator () (int r_min, int r_max) |
314 | int operator () (int r_min, int r_max) |
270 | { |
315 | { |
271 | return is_constant (r_min) && is_constant (r_max) |
316 | return is_constant (r_min) && is_constant (r_max) && r_min <= r_max |
272 | ? r_min + (*this) (max (r_max - r_min + 1, 1)) |
317 | ? r_min + operator ()(r_max - r_min + 1) |
273 | : get_range (r_min, r_max); |
318 | : get_range (r_min, r_max); |
274 | } |
319 | } |
275 | |
320 | |
276 | double operator ()() |
321 | double operator ()() |
277 | { |
322 | { |
… | |
… | |
393 | } |
438 | } |
394 | |
439 | |
395 | void erase (T *obj) |
440 | void erase (T *obj) |
396 | { |
441 | { |
397 | assert (obj->*index); |
442 | assert (obj->*index); |
398 | int pos = obj->*index; |
443 | unsigned int pos = obj->*index; |
399 | obj->*index = 0; |
444 | obj->*index = 0; |
400 | |
445 | |
401 | if (pos < this->size ()) |
446 | if (pos < this->size ()) |
402 | { |
447 | { |
403 | (*this)[pos - 1] = (*this)[this->size () - 1]; |
448 | (*this)[pos - 1] = (*this)[this->size () - 1]; |
… | |
… | |
428 | // return current time as timestampe |
473 | // return current time as timestampe |
429 | tstamp now (); |
474 | tstamp now (); |
430 | |
475 | |
431 | int similar_direction (int a, int b); |
476 | int similar_direction (int a, int b); |
432 | |
477 | |
|
|
478 | // like printf, but returns a std::string |
|
|
479 | const std::string format (const char *format, ...); |
|
|
480 | |
433 | #endif |
481 | #endif |
434 | |
482 | |