--- deliantra/server/include/util.h 2008/04/20 05:24:55 1.70 +++ deliantra/server/include/util.h 2008/12/04 03:48:19 1.78 @@ -1,7 +1,7 @@ /* * This file is part of Deliantra, the Roguelike Realtime MMORPG. * - * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Deliantra team + * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team * * Deliantra is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -22,7 +22,7 @@ #ifndef UTIL_H__ #define UTIL_H__ -#define DEBUG_POISON 0xaa // poison memory before freeing it if != 0 +#define DEBUG_POISON 0x00 // poison memory before freeing it if != 0 #define DEBUG_SALLOC 0 // add a debug wrapper around all sallocs #define PREFER_MALLOC 0 // use malloc and not the slice allocator @@ -98,16 +98,43 @@ template static inline T max (T a, U b) { return (U)a > b ? (U)a : b; } template static inline T clamp (T v, U a, V b) { return v < (T)a ? (T)a : v >(T)b ? (T)b : v; } +template static inline void min_it (T &v, T m) { v = min (v, m); } +template static inline void max_it (T &v, T m) { v = max (v, m); } +template static inline void clamp_it (T &v, T a, T b) { v = clamp (v, a, b); } + template static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } template static inline T min (T a, U b, V c) { return min (a, min (b, c)); } template static inline T max (T a, U b, V c) { return max (a, max (b, c)); } +// div, with correct rounding (< 0.5 downwards, >=0.5 upwards) +template static inline T div (T val, T div) { return (val + div / 2) / div; } +// div, round-up +template static inline T div_ru (T val, T div) { return (val + div - 1) / div; } +// div, round-down +template static inline T div_rd (T val, T div) { return (val ) / div; } + template static inline T lerp (T val, T min_in, T max_in, T min_out, T max_out) { - return (val - min_in) * (max_out - min_out) / (max_in - min_in) + min_out; + return min_out + div ((val - min_in) * (max_out - min_out), max_in - min_in); +} + +// lerp, round-down +template +static inline T +lerp_rd (T val, T min_in, T max_in, T min_out, T max_out) +{ + return min_out + div_rd ((val - min_in) * (max_out - min_out), max_in - min_in); +} + +// lerp, round-up +template +static inline T +lerp_ru (T val, T min_in, T max_in, T min_out, T max_out) +{ + return min_out + div_ru ((val - min_in) * (max_out - min_out), max_in - min_in); } // lots of stuff taken from FXT @@ -226,6 +253,14 @@ } } +// nulls the pointer +template +inline void sfree0 (T *&ptr, int n = 1) throw () +{ + sfree (ptr, n); + ptr = 0; +} + // makes dynamically allocated objects zero-initialised struct zero_initialised { @@ -256,6 +291,35 @@ } }; +// makes dynamically allocated objects zero-initialised +struct slice_allocated +{ + void *operator new (size_t s, void *p) + { + return p; + } + + void *operator new (size_t s) + { + return salloc (s); + } + + void *operator new[] (size_t s) + { + return salloc (s); + } + + void operator delete (void *p, size_t s) + { + sfree ((char *)p, s); + } + + void operator delete[] (void *p, size_t s) + { + sfree ((char *)p, s); + } +}; + // a STL-compatible allocator that uses g_slice // boy, this is verbose template @@ -330,7 +394,7 @@ void seed (uint32_t seed); uint32_t next (); - // uniform distribution + // uniform distribution, 0 .. max (0, num - 1) uint32_t operator ()(uint32_t num) { return is_constant (num) @@ -358,7 +422,7 @@ typedef tausworthe_random_generator rand_gen; -extern rand_gen rndm; +extern rand_gen rndm, rmg_rndm; INTERFACE_CLASS (attachable) struct refcnt_base