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