--- deliantra/server/include/util.h 2010/04/28 19:49:50 1.101 +++ deliantra/server/include/util.h 2010/05/02 11:23:51 1.106 @@ -57,14 +57,16 @@ // use C0X decltype for auto declarations until ISO C++ sanctifies them (if ever) #define auto(var,expr) decltype(expr) var = (expr) -// could use the sizeof (arr) /( sizeof (arr [0]) here, but C++ is -// much more obfuscated... :) - +#if cplusplus_does_not_suck +// does not work for local types (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm) template -inline int array_length (const T (&arr)[N]) +static inline int array_length (const T (&arr)[N]) { return N; } +#else +#define array_length(name) (sizeof (name) / sizeof (name [0])) +#endif // very ugly macro that basically declares and initialises a variable // that is in scope for the next statement only @@ -104,7 +106,11 @@ static inline T sign (T v) { return v < 0 ? -1 : +1; } // relies on 2c representation template<> -inline sint8 sign (sint8 v) { return 1 - (sint8 (uint8 (v) >> 7) * 2); } +inline sint8 sign (sint8 v) { return 1 - (sint8 (uint8 (v) >> 7) * 2); } +template<> +inline sint16 sign (sint16 v) { return 1 - (sint16 (uint16 (v) >> 15) * 2); } +template<> +inline sint32 sign (sint32 v) { return 1 - (sint32 (uint32 (v) >> 31) * 2); } // sign0 returns -1, 0 or +1 template @@ -119,6 +125,10 @@ { return expect_false (val < 0) ? - ((-val + (div - 1) / 2) / div) : (val + div / 2) / div; } + +template<> inline float div (float val, float div) { return val / div; } +template<> inline double div (double val, double div) { return val / div; } + // div, round-up template static inline T div_ru (T val, T div) { @@ -473,7 +483,7 @@ template struct random_number_generator : generator { - // uniform distribution, 0 .. max (0, num - 1) + // uniform distribution, [0 .. num - 1] uint32_t operator ()(uint32_t num) { return !is_constant (num) ? get_range (num) // non-constant @@ -484,15 +494,15 @@ // return a number within the closed interval [min .. max] int operator () (int r_min, int r_max) { - return is_constant (r_min) && is_constant (r_max) && r_min <= r_max + return is_constant (r_min <= r_max) && r_min <= r_max ? r_min + operator ()(r_max - r_min + 1) : get_range (r_min, r_max); } - // return a number within the closed interval [0..1] + // return a number within the half-open interval [0..1[ double operator ()() { - return this->next () / (double)0xFFFFFFFFU; + return this->next () / 0x100000000; } protected: @@ -574,6 +584,7 @@ typedef refptr arch_ptr; typedef refptr client_ptr; typedef refptr player_ptr; +typedef refptr region_ptr; #define STRHSH_NULL 2166136261