| 1 |
root |
1.1 |
#ifndef UTIL_H__ |
| 2 |
|
|
#define UTIL_H__ |
| 3 |
|
|
|
| 4 |
root |
1.36 |
//#define PREFER_MALLOC |
| 5 |
|
|
|
| 6 |
root |
1.2 |
#if __GNUC__ >= 3 |
| 7 |
|
|
# define is_constant(c) __builtin_constant_p (c) |
| 8 |
|
|
#else |
| 9 |
|
|
# define is_constant(c) 0 |
| 10 |
|
|
#endif |
| 11 |
|
|
|
| 12 |
root |
1.11 |
#include <cstddef> |
| 13 |
root |
1.28 |
#include <cmath> |
| 14 |
root |
1.25 |
#include <new> |
| 15 |
|
|
#include <vector> |
| 16 |
root |
1.11 |
|
| 17 |
|
|
#include <glib.h> |
| 18 |
|
|
|
| 19 |
root |
1.25 |
#include <shstr.h> |
| 20 |
|
|
#include <traits.h> |
| 21 |
|
|
|
| 22 |
root |
1.14 |
// use a gcc extension for auto declarations until ISO C++ sanctifies them |
| 23 |
root |
1.40 |
#define auto(var,expr) typeof(expr) var = (expr) |
| 24 |
root |
1.14 |
|
| 25 |
root |
1.26 |
// very ugly macro that basicaly declares and initialises a variable |
| 26 |
|
|
// that is in scope for the next statement only |
| 27 |
|
|
// works only for stuff that can be assigned 0 and converts to false |
| 28 |
|
|
// (note: works great for pointers) |
| 29 |
|
|
// most ugly macro I ever wrote |
| 30 |
|
|
#define declvar(type, name, value) if (type name = 0) { } else if (((name) = (value)), 1) |
| 31 |
|
|
|
| 32 |
root |
1.27 |
// in range including end |
| 33 |
|
|
#define IN_RANGE_INC(val,beg,end) \ |
| 34 |
|
|
((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg)) |
| 35 |
|
|
|
| 36 |
|
|
// in range excluding end |
| 37 |
|
|
#define IN_RANGE_EXC(val,beg,end) \ |
| 38 |
|
|
((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg)) |
| 39 |
|
|
|
| 40 |
root |
1.31 |
void fork_abort (const char *msg); |
| 41 |
|
|
|
| 42 |
root |
1.35 |
// rationale for using (U) not (T) is to reduce signed/unsigned issues, |
| 43 |
|
|
// as a is often a constant while b is the variable. it is still a bug, though. |
| 44 |
|
|
template<typename T, typename U> static inline T min (T a, U b) { return (U)a < b ? (U)a : b; } |
| 45 |
|
|
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; } |
| 47 |
root |
1.32 |
|
| 48 |
|
|
template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } |
| 49 |
|
|
|
| 50 |
root |
1.37 |
// lots of stuff taken from FXT |
| 51 |
|
|
|
| 52 |
|
|
/* Rotate right. This is used in various places for checksumming */ |
| 53 |
root |
1.38 |
//TODO: that sucks, use a better checksum algo |
| 54 |
root |
1.37 |
static inline uint32_t |
| 55 |
root |
1.38 |
rotate_right (uint32_t c, uint32_t count = 1) |
| 56 |
root |
1.37 |
{ |
| 57 |
root |
1.38 |
return (c << (32 - count)) | (c >> count); |
| 58 |
|
|
} |
| 59 |
|
|
|
| 60 |
|
|
static inline uint32_t |
| 61 |
|
|
rotate_left (uint32_t c, uint32_t count = 1) |
| 62 |
|
|
{ |
| 63 |
|
|
return (c >> (32 - count)) | (c << count); |
| 64 |
root |
1.37 |
} |
| 65 |
|
|
|
| 66 |
|
|
// Return abs(a-b) |
| 67 |
|
|
// Both a and b must not have the most significant bit set |
| 68 |
|
|
static inline uint32_t |
| 69 |
|
|
upos_abs_diff (uint32_t a, uint32_t b) |
| 70 |
|
|
{ |
| 71 |
|
|
long d1 = b - a; |
| 72 |
|
|
long d2 = (d1 & (d1 >> 31)) << 1; |
| 73 |
|
|
|
| 74 |
|
|
return d1 - d2; // == (b - d) - (a + d); |
| 75 |
|
|
} |
| 76 |
|
|
|
| 77 |
|
|
// Both a and b must not have the most significant bit set |
| 78 |
|
|
static inline uint32_t |
| 79 |
|
|
upos_min (uint32_t a, uint32_t b) |
| 80 |
|
|
{ |
| 81 |
|
|
int32_t d = b - a; |
| 82 |
|
|
d &= d >> 31; |
| 83 |
|
|
return a + d; |
| 84 |
|
|
} |
| 85 |
|
|
|
| 86 |
|
|
// Both a and b must not have the most significant bit set |
| 87 |
|
|
static inline uint32_t |
| 88 |
|
|
upos_max (uint32_t a, uint32_t b) |
| 89 |
|
|
{ |
| 90 |
|
|
int32_t d = b - a; |
| 91 |
|
|
d &= d >> 31; |
| 92 |
|
|
return b - d; |
| 93 |
|
|
} |
| 94 |
|
|
|
| 95 |
root |
1.28 |
// this is much faster than crossfires original algorithm |
| 96 |
|
|
// on modern cpus |
| 97 |
|
|
inline int |
| 98 |
|
|
isqrt (int n) |
| 99 |
|
|
{ |
| 100 |
|
|
return (int)sqrtf ((float)n); |
| 101 |
|
|
} |
| 102 |
|
|
|
| 103 |
|
|
// this is only twice as fast as naive sqrtf (dx*dy+dy*dy) |
| 104 |
|
|
#if 0 |
| 105 |
|
|
// and has a max. error of 6 in the range -100..+100. |
| 106 |
|
|
#else |
| 107 |
|
|
// and has a max. error of 9 in the range -100..+100. |
| 108 |
|
|
#endif |
| 109 |
|
|
inline int |
| 110 |
|
|
idistance (int dx, int dy) |
| 111 |
|
|
{ |
| 112 |
|
|
unsigned int dx_ = abs (dx); |
| 113 |
|
|
unsigned int dy_ = abs (dy); |
| 114 |
|
|
|
| 115 |
|
|
#if 0 |
| 116 |
|
|
return dx_ > dy_ |
| 117 |
|
|
? (dx_ * 61685 + dy_ * 26870) >> 16 |
| 118 |
|
|
: (dy_ * 61685 + dx_ * 26870) >> 16; |
| 119 |
|
|
#else |
| 120 |
root |
1.30 |
return dx_ + dy_ - min (dx_, dy_) * 5 / 8; |
| 121 |
root |
1.28 |
#endif |
| 122 |
|
|
} |
| 123 |
|
|
|
| 124 |
root |
1.29 |
/* |
| 125 |
|
|
* absdir(int): Returns a number between 1 and 8, which represent |
| 126 |
|
|
* the "absolute" direction of a number (it actually takes care of |
| 127 |
|
|
* "overflow" in previous calculations of a direction). |
| 128 |
|
|
*/ |
| 129 |
|
|
inline int |
| 130 |
|
|
absdir (int d) |
| 131 |
|
|
{ |
| 132 |
|
|
return ((d - 1) & 7) + 1; |
| 133 |
|
|
} |
| 134 |
root |
1.28 |
|
| 135 |
root |
1.1 |
// makes dynamically allocated objects zero-initialised |
| 136 |
|
|
struct zero_initialised |
| 137 |
|
|
{ |
| 138 |
root |
1.11 |
void *operator new (size_t s, void *p) |
| 139 |
|
|
{ |
| 140 |
|
|
memset (p, 0, s); |
| 141 |
|
|
return p; |
| 142 |
|
|
} |
| 143 |
|
|
|
| 144 |
|
|
void *operator new (size_t s) |
| 145 |
|
|
{ |
| 146 |
|
|
return g_slice_alloc0 (s); |
| 147 |
|
|
} |
| 148 |
|
|
|
| 149 |
|
|
void *operator new[] (size_t s) |
| 150 |
|
|
{ |
| 151 |
|
|
return g_slice_alloc0 (s); |
| 152 |
|
|
} |
| 153 |
|
|
|
| 154 |
|
|
void operator delete (void *p, size_t s) |
| 155 |
|
|
{ |
| 156 |
|
|
g_slice_free1 (s, p); |
| 157 |
|
|
} |
| 158 |
|
|
|
| 159 |
|
|
void operator delete[] (void *p, size_t s) |
| 160 |
|
|
{ |
| 161 |
|
|
g_slice_free1 (s, p); |
| 162 |
|
|
} |
| 163 |
|
|
}; |
| 164 |
|
|
|
| 165 |
root |
1.20 |
void *salloc_ (int n) throw (std::bad_alloc); |
| 166 |
|
|
void *salloc_ (int n, void *src) throw (std::bad_alloc); |
| 167 |
|
|
|
| 168 |
root |
1.12 |
// strictly the same as g_slice_alloc, but never returns 0 |
| 169 |
root |
1.20 |
template<typename T> |
| 170 |
|
|
inline T *salloc (int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T)); } |
| 171 |
|
|
|
| 172 |
root |
1.17 |
// also copies src into the new area, like "memdup" |
| 173 |
root |
1.18 |
// if src is 0, clears the memory |
| 174 |
|
|
template<typename T> |
| 175 |
root |
1.20 |
inline T *salloc (int n, T *src) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), (void *)src); } |
| 176 |
root |
1.18 |
|
| 177 |
root |
1.21 |
// clears the memory |
| 178 |
|
|
template<typename T> |
| 179 |
|
|
inline T *salloc0(int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), 0); } |
| 180 |
|
|
|
| 181 |
root |
1.12 |
// for symmetry |
| 182 |
root |
1.18 |
template<typename T> |
| 183 |
root |
1.20 |
inline void sfree (T *ptr, int n = 1) throw () |
| 184 |
root |
1.12 |
{ |
| 185 |
root |
1.36 |
#ifdef PREFER_MALLOC |
| 186 |
|
|
free (ptr); |
| 187 |
|
|
#else |
| 188 |
root |
1.20 |
g_slice_free1 (n * sizeof (T), (void *)ptr); |
| 189 |
root |
1.36 |
#endif |
| 190 |
root |
1.12 |
} |
| 191 |
root |
1.11 |
|
| 192 |
|
|
// a STL-compatible allocator that uses g_slice |
| 193 |
|
|
// boy, this is verbose |
| 194 |
|
|
template<typename Tp> |
| 195 |
|
|
struct slice_allocator |
| 196 |
|
|
{ |
| 197 |
|
|
typedef size_t size_type; |
| 198 |
|
|
typedef ptrdiff_t difference_type; |
| 199 |
|
|
typedef Tp *pointer; |
| 200 |
|
|
typedef const Tp *const_pointer; |
| 201 |
|
|
typedef Tp &reference; |
| 202 |
|
|
typedef const Tp &const_reference; |
| 203 |
|
|
typedef Tp value_type; |
| 204 |
|
|
|
| 205 |
|
|
template <class U> |
| 206 |
|
|
struct rebind |
| 207 |
|
|
{ |
| 208 |
|
|
typedef slice_allocator<U> other; |
| 209 |
|
|
}; |
| 210 |
|
|
|
| 211 |
|
|
slice_allocator () throw () { } |
| 212 |
|
|
slice_allocator (const slice_allocator &o) throw () { } |
| 213 |
|
|
template<typename Tp2> |
| 214 |
|
|
slice_allocator (const slice_allocator<Tp2> &) throw () { } |
| 215 |
|
|
|
| 216 |
|
|
~slice_allocator () { } |
| 217 |
|
|
|
| 218 |
|
|
pointer address (reference x) const { return &x; } |
| 219 |
|
|
const_pointer address (const_reference x) const { return &x; } |
| 220 |
|
|
|
| 221 |
|
|
pointer allocate (size_type n, const_pointer = 0) |
| 222 |
|
|
{ |
| 223 |
root |
1.18 |
return salloc<Tp> (n); |
| 224 |
root |
1.11 |
} |
| 225 |
|
|
|
| 226 |
|
|
void deallocate (pointer p, size_type n) |
| 227 |
|
|
{ |
| 228 |
root |
1.19 |
sfree<Tp> (p, n); |
| 229 |
root |
1.11 |
} |
| 230 |
|
|
|
| 231 |
|
|
size_type max_size ()const throw () |
| 232 |
|
|
{ |
| 233 |
|
|
return size_t (-1) / sizeof (Tp); |
| 234 |
|
|
} |
| 235 |
|
|
|
| 236 |
|
|
void construct (pointer p, const Tp &val) |
| 237 |
|
|
{ |
| 238 |
|
|
::new (p) Tp (val); |
| 239 |
|
|
} |
| 240 |
|
|
|
| 241 |
|
|
void destroy (pointer p) |
| 242 |
|
|
{ |
| 243 |
|
|
p->~Tp (); |
| 244 |
|
|
} |
| 245 |
root |
1.1 |
}; |
| 246 |
|
|
|
| 247 |
root |
1.32 |
// P. L'Ecuyer, “Maximally Equidistributed Combined Tausworthe Generators”, Mathematics of Computation, 65, 213 (1996), 203–213. |
| 248 |
|
|
// http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps |
| 249 |
|
|
// http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps |
| 250 |
|
|
struct tausworthe_random_generator |
| 251 |
|
|
{ |
| 252 |
root |
1.34 |
// generator |
| 253 |
root |
1.32 |
uint32_t state [4]; |
| 254 |
|
|
|
| 255 |
root |
1.34 |
void operator =(const tausworthe_random_generator &src) |
| 256 |
|
|
{ |
| 257 |
|
|
state [0] = src.state [0]; |
| 258 |
|
|
state [1] = src.state [1]; |
| 259 |
|
|
state [2] = src.state [2]; |
| 260 |
|
|
state [3] = src.state [3]; |
| 261 |
|
|
} |
| 262 |
|
|
|
| 263 |
|
|
void seed (uint32_t seed); |
| 264 |
root |
1.32 |
uint32_t next (); |
| 265 |
|
|
|
| 266 |
root |
1.34 |
// uniform distribution |
| 267 |
root |
1.32 |
uint32_t operator ()(uint32_t r_max) |
| 268 |
|
|
{ |
| 269 |
root |
1.34 |
return is_constant (r_max) |
| 270 |
root |
1.41 |
? (next () * (uint64_t)r_max) >> 32U |
| 271 |
root |
1.34 |
: get_range (r_max); |
| 272 |
root |
1.32 |
} |
| 273 |
|
|
|
| 274 |
|
|
// return a number within (min .. max) |
| 275 |
|
|
int operator () (int r_min, int r_max) |
| 276 |
|
|
{ |
| 277 |
root |
1.34 |
return is_constant (r_min) && is_constant (r_max) |
| 278 |
root |
1.41 |
? r_min + operator ()(max (r_max - r_min + 1, 1)) |
| 279 |
root |
1.34 |
: get_range (r_min, r_max); |
| 280 |
root |
1.32 |
} |
| 281 |
|
|
|
| 282 |
|
|
double operator ()() |
| 283 |
|
|
{ |
| 284 |
root |
1.34 |
return this->next () / (double)0xFFFFFFFFU; |
| 285 |
root |
1.32 |
} |
| 286 |
root |
1.34 |
|
| 287 |
|
|
protected: |
| 288 |
|
|
uint32_t get_range (uint32_t r_max); |
| 289 |
|
|
int get_range (int r_min, int r_max); |
| 290 |
root |
1.32 |
}; |
| 291 |
|
|
|
| 292 |
|
|
typedef tausworthe_random_generator rand_gen; |
| 293 |
|
|
|
| 294 |
|
|
extern rand_gen rndm; |
| 295 |
|
|
|
| 296 |
root |
1.7 |
template<class T> |
| 297 |
|
|
struct refptr |
| 298 |
|
|
{ |
| 299 |
|
|
T *p; |
| 300 |
|
|
|
| 301 |
|
|
refptr () : p(0) { } |
| 302 |
|
|
refptr (const refptr<T> &p) : p(p.p) { if (p) p->refcnt_inc (); } |
| 303 |
|
|
refptr (T *p) : p(p) { if (p) p->refcnt_inc (); } |
| 304 |
|
|
~refptr () { if (p) p->refcnt_dec (); } |
| 305 |
|
|
|
| 306 |
|
|
const refptr<T> &operator =(T *o) |
| 307 |
|
|
{ |
| 308 |
|
|
if (p) p->refcnt_dec (); |
| 309 |
|
|
p = o; |
| 310 |
|
|
if (p) p->refcnt_inc (); |
| 311 |
|
|
|
| 312 |
|
|
return *this; |
| 313 |
|
|
} |
| 314 |
|
|
|
| 315 |
|
|
const refptr<T> &operator =(const refptr<T> o) |
| 316 |
|
|
{ |
| 317 |
|
|
*this = o.p; |
| 318 |
|
|
return *this; |
| 319 |
|
|
} |
| 320 |
|
|
|
| 321 |
|
|
T &operator * () const { return *p; } |
| 322 |
|
|
T *operator ->() const { return p; } |
| 323 |
|
|
|
| 324 |
|
|
operator T *() const { return p; } |
| 325 |
|
|
}; |
| 326 |
|
|
|
| 327 |
root |
1.24 |
typedef refptr<maptile> maptile_ptr; |
| 328 |
root |
1.22 |
typedef refptr<object> object_ptr; |
| 329 |
|
|
typedef refptr<archetype> arch_ptr; |
| 330 |
root |
1.24 |
typedef refptr<client> client_ptr; |
| 331 |
|
|
typedef refptr<player> player_ptr; |
| 332 |
root |
1.22 |
|
| 333 |
root |
1.4 |
struct str_hash |
| 334 |
|
|
{ |
| 335 |
|
|
std::size_t operator ()(const char *s) const |
| 336 |
|
|
{ |
| 337 |
|
|
unsigned long hash = 0; |
| 338 |
|
|
|
| 339 |
|
|
/* use the one-at-a-time hash function, which supposedly is |
| 340 |
|
|
* better than the djb2-like one used by perl5.005, but |
| 341 |
|
|
* certainly is better then the bug used here before. |
| 342 |
|
|
* see http://burtleburtle.net/bob/hash/doobs.html |
| 343 |
|
|
*/ |
| 344 |
|
|
while (*s) |
| 345 |
|
|
{ |
| 346 |
|
|
hash += *s++; |
| 347 |
|
|
hash += hash << 10; |
| 348 |
|
|
hash ^= hash >> 6; |
| 349 |
|
|
} |
| 350 |
|
|
|
| 351 |
|
|
hash += hash << 3; |
| 352 |
|
|
hash ^= hash >> 11; |
| 353 |
|
|
hash += hash << 15; |
| 354 |
|
|
|
| 355 |
|
|
return hash; |
| 356 |
|
|
} |
| 357 |
|
|
}; |
| 358 |
|
|
|
| 359 |
|
|
struct str_equal |
| 360 |
|
|
{ |
| 361 |
|
|
bool operator ()(const char *a, const char *b) const |
| 362 |
|
|
{ |
| 363 |
|
|
return !strcmp (a, b); |
| 364 |
|
|
} |
| 365 |
|
|
}; |
| 366 |
|
|
|
| 367 |
root |
1.26 |
template<class T> |
| 368 |
|
|
struct unordered_vector : std::vector<T, slice_allocator<T> > |
| 369 |
root |
1.6 |
{ |
| 370 |
root |
1.11 |
typedef typename unordered_vector::iterator iterator; |
| 371 |
root |
1.6 |
|
| 372 |
|
|
void erase (unsigned int pos) |
| 373 |
|
|
{ |
| 374 |
|
|
if (pos < this->size () - 1) |
| 375 |
|
|
(*this)[pos] = (*this)[this->size () - 1]; |
| 376 |
|
|
|
| 377 |
|
|
this->pop_back (); |
| 378 |
|
|
} |
| 379 |
|
|
|
| 380 |
|
|
void erase (iterator i) |
| 381 |
|
|
{ |
| 382 |
|
|
erase ((unsigned int )(i - this->begin ())); |
| 383 |
|
|
} |
| 384 |
|
|
}; |
| 385 |
|
|
|
| 386 |
root |
1.26 |
template<class T, int T::* index> |
| 387 |
|
|
struct object_vector : std::vector<T *, slice_allocator<T *> > |
| 388 |
|
|
{ |
| 389 |
|
|
void insert (T *obj) |
| 390 |
|
|
{ |
| 391 |
|
|
assert (!(obj->*index)); |
| 392 |
|
|
push_back (obj); |
| 393 |
|
|
obj->*index = this->size (); |
| 394 |
|
|
} |
| 395 |
|
|
|
| 396 |
|
|
void insert (T &obj) |
| 397 |
|
|
{ |
| 398 |
|
|
insert (&obj); |
| 399 |
|
|
} |
| 400 |
|
|
|
| 401 |
|
|
void erase (T *obj) |
| 402 |
|
|
{ |
| 403 |
|
|
assert (obj->*index); |
| 404 |
pippijn |
1.39 |
unsigned int pos = obj->*index; |
| 405 |
root |
1.26 |
obj->*index = 0; |
| 406 |
|
|
|
| 407 |
|
|
if (pos < this->size ()) |
| 408 |
|
|
{ |
| 409 |
|
|
(*this)[pos - 1] = (*this)[this->size () - 1]; |
| 410 |
|
|
(*this)[pos - 1]->*index = pos; |
| 411 |
|
|
} |
| 412 |
|
|
|
| 413 |
|
|
this->pop_back (); |
| 414 |
|
|
} |
| 415 |
|
|
|
| 416 |
|
|
void erase (T &obj) |
| 417 |
|
|
{ |
| 418 |
|
|
errase (&obj); |
| 419 |
|
|
} |
| 420 |
|
|
}; |
| 421 |
|
|
|
| 422 |
root |
1.10 |
// basically does what strncpy should do, but appends "..." to strings exceeding length |
| 423 |
|
|
void assign (char *dst, const char *src, int maxlen); |
| 424 |
|
|
|
| 425 |
|
|
// type-safe version of assign |
| 426 |
root |
1.9 |
template<int N> |
| 427 |
|
|
inline void assign (char (&dst)[N], const char *src) |
| 428 |
|
|
{ |
| 429 |
root |
1.10 |
assign ((char *)&dst, src, N); |
| 430 |
root |
1.9 |
} |
| 431 |
|
|
|
| 432 |
root |
1.17 |
typedef double tstamp; |
| 433 |
|
|
|
| 434 |
|
|
// return current time as timestampe |
| 435 |
|
|
tstamp now (); |
| 436 |
|
|
|
| 437 |
root |
1.25 |
int similar_direction (int a, int b); |
| 438 |
|
|
|
| 439 |
root |
1.1 |
#endif |
| 440 |
|
|
|