… | |
… | |
6 | #else |
6 | #else |
7 | # define is_constant(c) 0 |
7 | # define is_constant(c) 0 |
8 | #endif |
8 | #endif |
9 | |
9 | |
10 | #include <cstddef> |
10 | #include <cstddef> |
|
|
11 | #include <cmath> |
|
|
12 | #include <new> |
|
|
13 | #include <vector> |
11 | |
14 | |
12 | #include <glib.h> |
15 | #include <glib.h> |
|
|
16 | |
|
|
17 | #include <shstr.h> |
|
|
18 | #include <traits.h> |
13 | |
19 | |
14 | // use a gcc extension for auto declarations until ISO C++ sanctifies them |
20 | // use a gcc extension for auto declarations until ISO C++ sanctifies them |
15 | #define AUTODECL(var,expr) typeof(expr) var = (expr) |
21 | #define AUTODECL(var,expr) typeof(expr) var = (expr) |
|
|
22 | |
|
|
23 | // very ugly macro that basicaly declares and initialises a variable |
|
|
24 | // that is in scope for the next statement only |
|
|
25 | // works only for stuff that can be assigned 0 and converts to false |
|
|
26 | // (note: works great for pointers) |
|
|
27 | // most ugly macro I ever wrote |
|
|
28 | #define declvar(type, name, value) if (type name = 0) { } else if (((name) = (value)), 1) |
|
|
29 | |
|
|
30 | // in range including end |
|
|
31 | #define IN_RANGE_INC(val,beg,end) \ |
|
|
32 | ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg)) |
|
|
33 | |
|
|
34 | // in range excluding end |
|
|
35 | #define IN_RANGE_EXC(val,beg,end) \ |
|
|
36 | ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg)) |
|
|
37 | |
|
|
38 | void fork_abort (const char *msg); |
|
|
39 | |
|
|
40 | template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } |
|
|
41 | template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } |
|
|
42 | 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; } |
|
|
43 | |
|
|
44 | template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } |
|
|
45 | |
|
|
46 | // this is much faster than crossfires original algorithm |
|
|
47 | // on modern cpus |
|
|
48 | inline int |
|
|
49 | isqrt (int n) |
|
|
50 | { |
|
|
51 | return (int)sqrtf ((float)n); |
|
|
52 | } |
|
|
53 | |
|
|
54 | // this is only twice as fast as naive sqrtf (dx*dy+dy*dy) |
|
|
55 | #if 0 |
|
|
56 | // and has a max. error of 6 in the range -100..+100. |
|
|
57 | #else |
|
|
58 | // and has a max. error of 9 in the range -100..+100. |
|
|
59 | #endif |
|
|
60 | inline int |
|
|
61 | idistance (int dx, int dy) |
|
|
62 | { |
|
|
63 | unsigned int dx_ = abs (dx); |
|
|
64 | unsigned int dy_ = abs (dy); |
|
|
65 | |
|
|
66 | #if 0 |
|
|
67 | return dx_ > dy_ |
|
|
68 | ? (dx_ * 61685 + dy_ * 26870) >> 16 |
|
|
69 | : (dy_ * 61685 + dx_ * 26870) >> 16; |
|
|
70 | #else |
|
|
71 | return dx_ + dy_ - min (dx_, dy_) * 5 / 8; |
|
|
72 | #endif |
|
|
73 | } |
|
|
74 | |
|
|
75 | /* |
|
|
76 | * absdir(int): Returns a number between 1 and 8, which represent |
|
|
77 | * the "absolute" direction of a number (it actually takes care of |
|
|
78 | * "overflow" in previous calculations of a direction). |
|
|
79 | */ |
|
|
80 | inline int |
|
|
81 | absdir (int d) |
|
|
82 | { |
|
|
83 | return ((d - 1) & 7) + 1; |
|
|
84 | } |
16 | |
85 | |
17 | // makes dynamically allocated objects zero-initialised |
86 | // makes dynamically allocated objects zero-initialised |
18 | struct zero_initialised |
87 | struct zero_initialised |
19 | { |
88 | { |
20 | void *operator new (size_t s, void *p) |
89 | void *operator new (size_t s, void *p) |
… | |
… | |
53 | |
122 | |
54 | // also copies src into the new area, like "memdup" |
123 | // also copies src into the new area, like "memdup" |
55 | // if src is 0, clears the memory |
124 | // if src is 0, clears the memory |
56 | template<typename T> |
125 | template<typename T> |
57 | inline T *salloc (int n, T *src) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), (void *)src); } |
126 | inline T *salloc (int n, T *src) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), (void *)src); } |
|
|
127 | |
|
|
128 | // clears the memory |
|
|
129 | template<typename T> |
|
|
130 | inline T *salloc0(int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), 0); } |
58 | |
131 | |
59 | // for symmetry |
132 | // for symmetry |
60 | template<typename T> |
133 | template<typename T> |
61 | inline void sfree (T *ptr, int n = 1) throw () |
134 | inline void sfree (T *ptr, int n = 1) throw () |
62 | { |
135 | { |
… | |
… | |
116 | { |
189 | { |
117 | p->~Tp (); |
190 | p->~Tp (); |
118 | } |
191 | } |
119 | }; |
192 | }; |
120 | |
193 | |
121 | struct refcounted |
194 | // P. L'Ecuyer, “Maximally Equidistributed Combined Tausworthe Generators”, Mathematics of Computation, 65, 213 (1996), 203–213. |
|
|
195 | // http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps |
|
|
196 | // http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps |
|
|
197 | struct tausworthe_random_generator |
122 | { |
198 | { |
123 | refcounted () : refcnt (0) { } |
199 | uint32_t state [4]; |
124 | // virtual ~refcounted (); |
200 | |
125 | void refcnt_inc () { ++refcnt; } |
201 | tausworthe_random_generator (uint32_t seed); |
126 | void refcnt_dec () { --refcnt; } |
202 | uint32_t next (); |
127 | bool dead () { return refcnt == 0; } |
203 | |
128 | mutable int refcnt; |
204 | uint32_t operator ()(uint32_t r_max) |
129 | #if 0 |
205 | { |
130 | private: |
206 | return next () % r_max; |
131 | static refcounted *rc_first; |
207 | } |
132 | refcounted *rc_next; |
208 | |
133 | #endif |
209 | // return a number within (min .. max) |
|
|
210 | int operator () (int r_min, int r_max) |
|
|
211 | { |
|
|
212 | return r_min + next () % max (r_max - r_min + 1, 1); |
|
|
213 | } |
|
|
214 | |
|
|
215 | double operator ()() |
|
|
216 | { |
|
|
217 | return next () / (double)0xFFFFFFFFU; |
|
|
218 | } |
134 | }; |
219 | }; |
|
|
220 | |
|
|
221 | typedef tausworthe_random_generator rand_gen; |
|
|
222 | |
|
|
223 | extern rand_gen rndm; |
135 | |
224 | |
136 | template<class T> |
225 | template<class T> |
137 | struct refptr |
226 | struct refptr |
138 | { |
227 | { |
139 | T *p; |
228 | T *p; |
… | |
… | |
161 | T &operator * () const { return *p; } |
250 | T &operator * () const { return *p; } |
162 | T *operator ->() const { return p; } |
251 | T *operator ->() const { return p; } |
163 | |
252 | |
164 | operator T *() const { return p; } |
253 | operator T *() const { return p; } |
165 | }; |
254 | }; |
|
|
255 | |
|
|
256 | typedef refptr<maptile> maptile_ptr; |
|
|
257 | typedef refptr<object> object_ptr; |
|
|
258 | typedef refptr<archetype> arch_ptr; |
|
|
259 | typedef refptr<client> client_ptr; |
|
|
260 | typedef refptr<player> player_ptr; |
166 | |
261 | |
167 | struct str_hash |
262 | struct str_hash |
168 | { |
263 | { |
169 | std::size_t operator ()(const char *s) const |
264 | std::size_t operator ()(const char *s) const |
170 | { |
265 | { |
… | |
… | |
196 | { |
291 | { |
197 | return !strcmp (a, b); |
292 | return !strcmp (a, b); |
198 | } |
293 | } |
199 | }; |
294 | }; |
200 | |
295 | |
201 | #include <vector> |
|
|
202 | |
|
|
203 | template<class obj> |
296 | template<class T> |
204 | struct unordered_vector : std::vector<obj, slice_allocator<obj> > |
297 | struct unordered_vector : std::vector<T, slice_allocator<T> > |
205 | { |
298 | { |
206 | typedef typename unordered_vector::iterator iterator; |
299 | typedef typename unordered_vector::iterator iterator; |
207 | |
300 | |
208 | void erase (unsigned int pos) |
301 | void erase (unsigned int pos) |
209 | { |
302 | { |
… | |
… | |
217 | { |
310 | { |
218 | erase ((unsigned int )(i - this->begin ())); |
311 | erase ((unsigned int )(i - this->begin ())); |
219 | } |
312 | } |
220 | }; |
313 | }; |
221 | |
314 | |
222 | template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } |
315 | template<class T, int T::* index> |
223 | template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } |
316 | struct object_vector : std::vector<T *, slice_allocator<T *> > |
224 | 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; } |
317 | { |
|
|
318 | void insert (T *obj) |
|
|
319 | { |
|
|
320 | assert (!(obj->*index)); |
|
|
321 | push_back (obj); |
|
|
322 | obj->*index = this->size (); |
|
|
323 | } |
225 | |
324 | |
226 | template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } |
325 | void insert (T &obj) |
|
|
326 | { |
|
|
327 | insert (&obj); |
|
|
328 | } |
|
|
329 | |
|
|
330 | void erase (T *obj) |
|
|
331 | { |
|
|
332 | assert (obj->*index); |
|
|
333 | int pos = obj->*index; |
|
|
334 | obj->*index = 0; |
|
|
335 | |
|
|
336 | if (pos < this->size ()) |
|
|
337 | { |
|
|
338 | (*this)[pos - 1] = (*this)[this->size () - 1]; |
|
|
339 | (*this)[pos - 1]->*index = pos; |
|
|
340 | } |
|
|
341 | |
|
|
342 | this->pop_back (); |
|
|
343 | } |
|
|
344 | |
|
|
345 | void erase (T &obj) |
|
|
346 | { |
|
|
347 | errase (&obj); |
|
|
348 | } |
|
|
349 | }; |
227 | |
350 | |
228 | // basically does what strncpy should do, but appends "..." to strings exceeding length |
351 | // basically does what strncpy should do, but appends "..." to strings exceeding length |
229 | void assign (char *dst, const char *src, int maxlen); |
352 | void assign (char *dst, const char *src, int maxlen); |
230 | |
353 | |
231 | // type-safe version of assign |
354 | // type-safe version of assign |
… | |
… | |
238 | typedef double tstamp; |
361 | typedef double tstamp; |
239 | |
362 | |
240 | // return current time as timestampe |
363 | // return current time as timestampe |
241 | tstamp now (); |
364 | tstamp now (); |
242 | |
365 | |
|
|
366 | int similar_direction (int a, int b); |
|
|
367 | |
243 | #endif |
368 | #endif |
244 | |
369 | |