|
|
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 | |
|
|
26 | //#define PREFER_MALLOC |
|
|
27 | |
4 | #if __GNUC__ >= 3 |
28 | #if __GNUC__ >= 3 |
5 | # 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) |
6 | #else |
32 | #else |
7 | # define is_constant(c) 0 |
33 | # define is_constant(c) 0 |
|
|
34 | # define expect(expr,value) (expr) |
|
|
35 | # define prefetch(addr,rw,locality) |
8 | #endif |
36 | #endif |
|
|
37 | |
|
|
38 | #if __GNUC__ < 4 || (__GNUC__ == 4 || __GNUC_MINOR__ < 4) |
|
|
39 | # define decltype(x) typeof(x) |
|
|
40 | #endif |
|
|
41 | |
|
|
42 | // put into ifs if you are very sure that the expression |
|
|
43 | // is mostly true or mosty false. note that these return |
|
|
44 | // booleans, not the expression. |
|
|
45 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
46 | #define expect_true(expr) expect ((expr) != 0, 1) |
9 | |
47 | |
10 | #include <cstddef> |
48 | #include <cstddef> |
|
|
49 | #include <cmath> |
|
|
50 | #include <new> |
|
|
51 | #include <vector> |
11 | |
52 | |
12 | #include <glib.h> |
53 | #include <glib.h> |
13 | |
54 | |
|
|
55 | #include <shstr.h> |
|
|
56 | #include <traits.h> |
|
|
57 | |
14 | // use a gcc extension for auto declarations until ISO C++ sanctifies them |
58 | // use a gcc extension for auto declarations until ISO C++ sanctifies them |
15 | #define AUTODECL(var,expr) typeof(expr) var = (expr) |
59 | #define auto(var,expr) decltype(expr) var = (expr) |
|
|
60 | |
|
|
61 | // very ugly macro that basicaly declares and initialises a variable |
|
|
62 | // that is in scope for the next statement only |
|
|
63 | // works only for stuff that can be assigned 0 and converts to false |
|
|
64 | // (note: works great for pointers) |
|
|
65 | // most ugly macro I ever wrote |
|
|
66 | #define declvar(type, name, value) if (type name = 0) { } else if (((name) = (value)), 1) |
|
|
67 | |
|
|
68 | // in range including end |
|
|
69 | #define IN_RANGE_INC(val,beg,end) \ |
|
|
70 | ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg)) |
|
|
71 | |
|
|
72 | // in range excluding end |
|
|
73 | #define IN_RANGE_EXC(val,beg,end) \ |
|
|
74 | ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg)) |
|
|
75 | |
|
|
76 | void fork_abort (const char *msg); |
|
|
77 | |
|
|
78 | // rationale for using (U) not (T) is to reduce signed/unsigned issues, |
|
|
79 | // as a is often a constant while b is the variable. it is still a bug, though. |
|
|
80 | template<typename T, typename U> static inline T min (T a, U b) { return (U)a < b ? (U)a : b; } |
|
|
81 | template<typename T, typename U> static inline T max (T a, U b) { return (U)a > b ? (U)a : b; } |
|
|
82 | 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; } |
|
|
83 | |
|
|
84 | template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } |
|
|
85 | |
|
|
86 | template<typename T> |
|
|
87 | static inline T |
|
|
88 | lerp (T val, T min_in, T max_in, T min_out, T max_out) |
|
|
89 | { |
|
|
90 | return (val - min_in) * (max_out - min_out) / (max_in - min_in) + min_out; |
|
|
91 | } |
|
|
92 | |
|
|
93 | // lots of stuff taken from FXT |
|
|
94 | |
|
|
95 | /* Rotate right. This is used in various places for checksumming */ |
|
|
96 | //TODO: that sucks, use a better checksum algo |
|
|
97 | static inline uint32_t |
|
|
98 | rotate_right (uint32_t c, uint32_t count = 1) |
|
|
99 | { |
|
|
100 | return (c << (32 - count)) | (c >> count); |
|
|
101 | } |
|
|
102 | |
|
|
103 | static inline uint32_t |
|
|
104 | rotate_left (uint32_t c, uint32_t count = 1) |
|
|
105 | { |
|
|
106 | return (c >> (32 - count)) | (c << count); |
|
|
107 | } |
|
|
108 | |
|
|
109 | // Return abs(a-b) |
|
|
110 | // Both a and b must not have the most significant bit set |
|
|
111 | static inline uint32_t |
|
|
112 | upos_abs_diff (uint32_t a, uint32_t b) |
|
|
113 | { |
|
|
114 | long d1 = b - a; |
|
|
115 | long d2 = (d1 & (d1 >> 31)) << 1; |
|
|
116 | |
|
|
117 | return d1 - d2; // == (b - d) - (a + d); |
|
|
118 | } |
|
|
119 | |
|
|
120 | // Both a and b must not have the most significant bit set |
|
|
121 | static inline uint32_t |
|
|
122 | upos_min (uint32_t a, uint32_t b) |
|
|
123 | { |
|
|
124 | int32_t d = b - a; |
|
|
125 | d &= d >> 31; |
|
|
126 | return a + d; |
|
|
127 | } |
|
|
128 | |
|
|
129 | // Both a and b must not have the most significant bit set |
|
|
130 | static inline uint32_t |
|
|
131 | upos_max (uint32_t a, uint32_t b) |
|
|
132 | { |
|
|
133 | int32_t d = b - a; |
|
|
134 | d &= d >> 31; |
|
|
135 | return b - d; |
|
|
136 | } |
|
|
137 | |
|
|
138 | // this is much faster than crossfires original algorithm |
|
|
139 | // on modern cpus |
|
|
140 | inline int |
|
|
141 | isqrt (int n) |
|
|
142 | { |
|
|
143 | return (int)sqrtf ((float)n); |
|
|
144 | } |
|
|
145 | |
|
|
146 | // this is only twice as fast as naive sqrtf (dx*dy+dy*dy) |
|
|
147 | #if 0 |
|
|
148 | // and has a max. error of 6 in the range -100..+100. |
|
|
149 | #else |
|
|
150 | // and has a max. error of 9 in the range -100..+100. |
|
|
151 | #endif |
|
|
152 | inline int |
|
|
153 | idistance (int dx, int dy) |
|
|
154 | { |
|
|
155 | unsigned int dx_ = abs (dx); |
|
|
156 | unsigned int dy_ = abs (dy); |
|
|
157 | |
|
|
158 | #if 0 |
|
|
159 | return dx_ > dy_ |
|
|
160 | ? (dx_ * 61685 + dy_ * 26870) >> 16 |
|
|
161 | : (dy_ * 61685 + dx_ * 26870) >> 16; |
|
|
162 | #else |
|
|
163 | return dx_ + dy_ - min (dx_, dy_) * 5 / 8; |
|
|
164 | #endif |
|
|
165 | } |
|
|
166 | |
|
|
167 | /* |
|
|
168 | * absdir(int): Returns a number between 1 and 8, which represent |
|
|
169 | * the "absolute" direction of a number (it actually takes care of |
|
|
170 | * "overflow" in previous calculations of a direction). |
|
|
171 | */ |
|
|
172 | inline int |
|
|
173 | absdir (int d) |
|
|
174 | { |
|
|
175 | return ((d - 1) & 7) + 1; |
|
|
176 | } |
16 | |
177 | |
17 | // makes dynamically allocated objects zero-initialised |
178 | // makes dynamically allocated objects zero-initialised |
18 | struct zero_initialised |
179 | struct zero_initialised |
19 | { |
180 | { |
20 | void *operator new (size_t s, void *p) |
181 | void *operator new (size_t s, void *p) |
… | |
… | |
62 | |
223 | |
63 | // for symmetry |
224 | // for symmetry |
64 | template<typename T> |
225 | template<typename T> |
65 | inline void sfree (T *ptr, int n = 1) throw () |
226 | inline void sfree (T *ptr, int n = 1) throw () |
66 | { |
227 | { |
|
|
228 | #ifdef PREFER_MALLOC |
|
|
229 | free (ptr); |
|
|
230 | #else |
67 | g_slice_free1 (n * sizeof (T), (void *)ptr); |
231 | g_slice_free1 (n * sizeof (T), (void *)ptr); |
|
|
232 | #endif |
68 | } |
233 | } |
69 | |
234 | |
70 | // a STL-compatible allocator that uses g_slice |
235 | // a STL-compatible allocator that uses g_slice |
71 | // boy, this is verbose |
236 | // boy, this is verbose |
72 | template<typename Tp> |
237 | template<typename Tp> |
… | |
… | |
120 | { |
285 | { |
121 | p->~Tp (); |
286 | p->~Tp (); |
122 | } |
287 | } |
123 | }; |
288 | }; |
124 | |
289 | |
125 | struct refcounted |
290 | // P. L'Ecuyer, “Maximally Equidistributed Combined Tausworthe Generators”, Mathematics of Computation, 65, 213 (1996), 203–213. |
|
|
291 | // http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps |
|
|
292 | // http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps |
|
|
293 | struct tausworthe_random_generator |
126 | { |
294 | { |
127 | refcounted () : refcnt (0) { } |
295 | // generator |
128 | virtual ~refcounted (); |
296 | uint32_t state [4]; |
129 | void refcnt_inc () { ++refcnt; } |
297 | |
130 | void refcnt_dec () { --refcnt; } |
298 | void operator =(const tausworthe_random_generator &src) |
131 | bool dead () { return refcnt == 0; } |
299 | { |
132 | mutable int refcnt; |
300 | state [0] = src.state [0]; |
133 | #if 0 |
301 | state [1] = src.state [1]; |
134 | private: |
302 | state [2] = src.state [2]; |
135 | static refcounted *rc_first; |
303 | state [3] = src.state [3]; |
136 | refcounted *rc_next; |
304 | } |
137 | #endif |
305 | |
|
|
306 | void seed (uint32_t seed); |
|
|
307 | uint32_t next (); |
|
|
308 | |
|
|
309 | // uniform distribution |
|
|
310 | uint32_t operator ()(uint32_t num) |
|
|
311 | { |
|
|
312 | return is_constant (num) |
|
|
313 | ? (next () * (uint64_t)num) >> 32U |
|
|
314 | : get_range (num); |
|
|
315 | } |
|
|
316 | |
|
|
317 | // return a number within (min .. max) |
|
|
318 | int operator () (int r_min, int r_max) |
|
|
319 | { |
|
|
320 | return is_constant (r_min) && is_constant (r_max) && r_min <= r_max |
|
|
321 | ? r_min + operator ()(r_max - r_min + 1) |
|
|
322 | : get_range (r_min, r_max); |
|
|
323 | } |
|
|
324 | |
|
|
325 | double operator ()() |
|
|
326 | { |
|
|
327 | return this->next () / (double)0xFFFFFFFFU; |
|
|
328 | } |
|
|
329 | |
|
|
330 | protected: |
|
|
331 | uint32_t get_range (uint32_t r_max); |
|
|
332 | int get_range (int r_min, int r_max); |
138 | }; |
333 | }; |
|
|
334 | |
|
|
335 | typedef tausworthe_random_generator rand_gen; |
|
|
336 | |
|
|
337 | extern rand_gen rndm; |
139 | |
338 | |
140 | template<class T> |
339 | template<class T> |
141 | struct refptr |
340 | struct refptr |
142 | { |
341 | { |
143 | T *p; |
342 | T *p; |
… | |
… | |
166 | T *operator ->() const { return p; } |
365 | T *operator ->() const { return p; } |
167 | |
366 | |
168 | operator T *() const { return p; } |
367 | operator T *() const { return p; } |
169 | }; |
368 | }; |
170 | |
369 | |
171 | typedef refptr<player> player_ptr; |
370 | typedef refptr<maptile> maptile_ptr; |
172 | typedef refptr<object> object_ptr; |
371 | typedef refptr<object> object_ptr; |
173 | typedef refptr<archetype> arch_ptr; |
372 | typedef refptr<archetype> arch_ptr; |
|
|
373 | typedef refptr<client> client_ptr; |
|
|
374 | typedef refptr<player> player_ptr; |
174 | |
375 | |
175 | struct str_hash |
376 | struct str_hash |
176 | { |
377 | { |
177 | std::size_t operator ()(const char *s) const |
378 | std::size_t operator ()(const char *s) const |
178 | { |
379 | { |
… | |
… | |
204 | { |
405 | { |
205 | return !strcmp (a, b); |
406 | return !strcmp (a, b); |
206 | } |
407 | } |
207 | }; |
408 | }; |
208 | |
409 | |
209 | #include <vector> |
|
|
210 | |
|
|
211 | template<class obj> |
410 | template<class T> |
212 | struct unordered_vector : std::vector<obj, slice_allocator<obj> > |
411 | struct unordered_vector : std::vector<T, slice_allocator<T> > |
213 | { |
412 | { |
214 | typedef typename unordered_vector::iterator iterator; |
413 | typedef typename unordered_vector::iterator iterator; |
215 | |
414 | |
216 | void erase (unsigned int pos) |
415 | void erase (unsigned int pos) |
217 | { |
416 | { |
… | |
… | |
225 | { |
424 | { |
226 | erase ((unsigned int )(i - this->begin ())); |
425 | erase ((unsigned int )(i - this->begin ())); |
227 | } |
426 | } |
228 | }; |
427 | }; |
229 | |
428 | |
230 | template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } |
429 | template<class T, int T::* index> |
231 | template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } |
430 | struct object_vector : std::vector<T *, slice_allocator<T *> > |
232 | template<typename T, typename U, typename V> static inline T clamp (T v, U a, V b) { return v < (T)a ? a : v >(T)b ? b : v; } |
431 | { |
|
|
432 | void insert (T *obj) |
|
|
433 | { |
|
|
434 | assert (!(obj->*index)); |
|
|
435 | push_back (obj); |
|
|
436 | obj->*index = this->size (); |
|
|
437 | } |
233 | |
438 | |
234 | template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } |
439 | void insert (T &obj) |
|
|
440 | { |
|
|
441 | insert (&obj); |
|
|
442 | } |
|
|
443 | |
|
|
444 | void erase (T *obj) |
|
|
445 | { |
|
|
446 | assert (obj->*index); |
|
|
447 | unsigned int pos = obj->*index; |
|
|
448 | obj->*index = 0; |
|
|
449 | |
|
|
450 | if (pos < this->size ()) |
|
|
451 | { |
|
|
452 | (*this)[pos - 1] = (*this)[this->size () - 1]; |
|
|
453 | (*this)[pos - 1]->*index = pos; |
|
|
454 | } |
|
|
455 | |
|
|
456 | this->pop_back (); |
|
|
457 | } |
|
|
458 | |
|
|
459 | void erase (T &obj) |
|
|
460 | { |
|
|
461 | errase (&obj); |
|
|
462 | } |
|
|
463 | }; |
235 | |
464 | |
236 | // basically does what strncpy should do, but appends "..." to strings exceeding length |
465 | // basically does what strncpy should do, but appends "..." to strings exceeding length |
237 | void assign (char *dst, const char *src, int maxlen); |
466 | void assign (char *dst, const char *src, int maxlen); |
238 | |
467 | |
239 | // type-safe version of assign |
468 | // type-safe version of assign |
… | |
… | |
246 | typedef double tstamp; |
475 | typedef double tstamp; |
247 | |
476 | |
248 | // return current time as timestampe |
477 | // return current time as timestampe |
249 | tstamp now (); |
478 | tstamp now (); |
250 | |
479 | |
251 | #endif |
480 | int similar_direction (int a, int b); |
252 | |
481 | |
|
|
482 | // like printf, but returns a std::string |
|
|
483 | const std::string format (const char *format, ...); |
|
|
484 | |
|
|
485 | #endif |
|
|
486 | |