… | |
… | |
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) |
16 | |
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 | // this is much faster than crossfires original algorithm |
|
|
41 | // on modern cpus |
|
|
42 | inline int |
|
|
43 | isqrt (int n) |
|
|
44 | { |
|
|
45 | return (int)sqrtf ((float)n); |
|
|
46 | } |
|
|
47 | |
|
|
48 | // this is only twice as fast as naive sqrtf (dx*dy+dy*dy) |
|
|
49 | #if 0 |
|
|
50 | // and has a max. error of 6 in the range -100..+100. |
|
|
51 | #else |
|
|
52 | // and has a max. error of 9 in the range -100..+100. |
|
|
53 | #endif |
|
|
54 | inline int |
|
|
55 | idistance (int dx, int dy) |
|
|
56 | { |
|
|
57 | unsigned int dx_ = abs (dx); |
|
|
58 | unsigned int dy_ = abs (dy); |
|
|
59 | |
|
|
60 | #if 0 |
|
|
61 | return dx_ > dy_ |
|
|
62 | ? (dx_ * 61685 + dy_ * 26870) >> 16 |
|
|
63 | : (dy_ * 61685 + dx_ * 26870) >> 16; |
|
|
64 | #else |
|
|
65 | return dx_ + dy_ - min (dx_, dy_) * 5 / 8; |
|
|
66 | #endif |
|
|
67 | } |
|
|
68 | |
|
|
69 | /* |
|
|
70 | * absdir(int): Returns a number between 1 and 8, which represent |
|
|
71 | * the "absolute" direction of a number (it actually takes care of |
|
|
72 | * "overflow" in previous calculations of a direction). |
|
|
73 | */ |
|
|
74 | inline int |
|
|
75 | absdir (int d) |
|
|
76 | { |
|
|
77 | return ((d - 1) & 7) + 1; |
|
|
78 | } |
|
|
79 | |
17 | // makes dynamically allocated objects zero-initialised |
80 | // makes dynamically allocated objects zero-initialised |
18 | struct zero_initialised |
81 | struct zero_initialised |
19 | { |
82 | { |
20 | void *operator new (size_t s, void *p) |
83 | void *operator new (size_t s, void *p) |
21 | { |
84 | { |
… | |
… | |
42 | { |
105 | { |
43 | g_slice_free1 (s, p); |
106 | g_slice_free1 (s, p); |
44 | } |
107 | } |
45 | }; |
108 | }; |
46 | |
109 | |
|
|
110 | void *salloc_ (int n) throw (std::bad_alloc); |
|
|
111 | void *salloc_ (int n, void *src) throw (std::bad_alloc); |
|
|
112 | |
47 | // strictly the same as g_slice_alloc, but never returns 0 |
113 | // strictly the same as g_slice_alloc, but never returns 0 |
48 | void *salloc (int size) throw (std::bad_alloc); |
114 | template<typename T> |
|
|
115 | inline T *salloc (int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T)); } |
|
|
116 | |
49 | // also copies src into the new area, like "memdup" |
117 | // also copies src into the new area, like "memdup" |
50 | // if src is 0, clears the memory |
118 | // if src is 0, clears the memory |
51 | void *salloc (int size, void *src) throw (std::bad_alloc); |
|
|
52 | |
|
|
53 | // and as a template |
|
|
54 | template<typename T> |
119 | template<typename T> |
55 | inline T *salloc (int size) throw (std::bad_alloc) { return (T *)salloc (size * sizeof (T)); } |
120 | inline T *salloc (int n, T *src) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), (void *)src); } |
|
|
121 | |
|
|
122 | // clears the memory |
56 | template<typename T> |
123 | template<typename T> |
57 | inline T *salloc (int size, T *src) throw (std::bad_alloc) { return (T *)salloc (size * sizeof (T), (void *)src); } |
124 | inline T *salloc0(int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), 0); } |
58 | |
125 | |
59 | // for symmetry |
126 | // for symmetry |
60 | template<typename T> |
127 | template<typename T> |
61 | inline void sfree (T *ptr, int size) throw () |
128 | inline void sfree (T *ptr, int n = 1) throw () |
62 | { |
129 | { |
63 | g_slice_free1 (size * sizeof (T), (void *)ptr); |
130 | g_slice_free1 (n * sizeof (T), (void *)ptr); |
64 | } |
131 | } |
65 | |
132 | |
66 | // a STL-compatible allocator that uses g_slice |
133 | // a STL-compatible allocator that uses g_slice |
67 | // boy, this is verbose |
134 | // boy, this is verbose |
68 | template<typename Tp> |
135 | template<typename Tp> |
… | |
… | |
116 | { |
183 | { |
117 | p->~Tp (); |
184 | p->~Tp (); |
118 | } |
185 | } |
119 | }; |
186 | }; |
120 | |
187 | |
121 | struct refcounted |
|
|
122 | { |
|
|
123 | refcounted () : refcnt (0) { } |
|
|
124 | // virtual ~refcounted (); |
|
|
125 | void refcnt_inc () { ++refcnt; } |
|
|
126 | void refcnt_dec () { --refcnt; } |
|
|
127 | bool dead () { return refcnt == 0; } |
|
|
128 | mutable int refcnt; |
|
|
129 | #if 0 |
|
|
130 | private: |
|
|
131 | static refcounted *rc_first; |
|
|
132 | refcounted *rc_next; |
|
|
133 | #endif |
|
|
134 | }; |
|
|
135 | |
|
|
136 | template<class T> |
188 | template<class T> |
137 | struct refptr |
189 | struct refptr |
138 | { |
190 | { |
139 | T *p; |
191 | T *p; |
140 | |
192 | |
… | |
… | |
161 | T &operator * () const { return *p; } |
213 | T &operator * () const { return *p; } |
162 | T *operator ->() const { return p; } |
214 | T *operator ->() const { return p; } |
163 | |
215 | |
164 | operator T *() const { return p; } |
216 | operator T *() const { return p; } |
165 | }; |
217 | }; |
|
|
218 | |
|
|
219 | typedef refptr<maptile> maptile_ptr; |
|
|
220 | typedef refptr<object> object_ptr; |
|
|
221 | typedef refptr<archetype> arch_ptr; |
|
|
222 | typedef refptr<client> client_ptr; |
|
|
223 | typedef refptr<player> player_ptr; |
166 | |
224 | |
167 | struct str_hash |
225 | struct str_hash |
168 | { |
226 | { |
169 | std::size_t operator ()(const char *s) const |
227 | std::size_t operator ()(const char *s) const |
170 | { |
228 | { |
… | |
… | |
196 | { |
254 | { |
197 | return !strcmp (a, b); |
255 | return !strcmp (a, b); |
198 | } |
256 | } |
199 | }; |
257 | }; |
200 | |
258 | |
201 | #include <vector> |
|
|
202 | |
|
|
203 | template<class obj> |
259 | template<class T> |
204 | struct unordered_vector : std::vector<obj, slice_allocator<obj> > |
260 | struct unordered_vector : std::vector<T, slice_allocator<T> > |
205 | { |
261 | { |
206 | typedef typename unordered_vector::iterator iterator; |
262 | typedef typename unordered_vector::iterator iterator; |
207 | |
263 | |
208 | void erase (unsigned int pos) |
264 | void erase (unsigned int pos) |
209 | { |
265 | { |
… | |
… | |
214 | } |
270 | } |
215 | |
271 | |
216 | void erase (iterator i) |
272 | void erase (iterator i) |
217 | { |
273 | { |
218 | erase ((unsigned int )(i - this->begin ())); |
274 | erase ((unsigned int )(i - this->begin ())); |
|
|
275 | } |
|
|
276 | }; |
|
|
277 | |
|
|
278 | template<class T, int T::* index> |
|
|
279 | struct object_vector : std::vector<T *, slice_allocator<T *> > |
|
|
280 | { |
|
|
281 | void insert (T *obj) |
|
|
282 | { |
|
|
283 | assert (!(obj->*index)); |
|
|
284 | push_back (obj); |
|
|
285 | obj->*index = this->size (); |
|
|
286 | } |
|
|
287 | |
|
|
288 | void insert (T &obj) |
|
|
289 | { |
|
|
290 | insert (&obj); |
|
|
291 | } |
|
|
292 | |
|
|
293 | void erase (T *obj) |
|
|
294 | { |
|
|
295 | assert (obj->*index); |
|
|
296 | int pos = obj->*index; |
|
|
297 | obj->*index = 0; |
|
|
298 | |
|
|
299 | if (pos < this->size ()) |
|
|
300 | { |
|
|
301 | (*this)[pos - 1] = (*this)[this->size () - 1]; |
|
|
302 | (*this)[pos - 1]->*index = pos; |
|
|
303 | } |
|
|
304 | |
|
|
305 | this->pop_back (); |
|
|
306 | } |
|
|
307 | |
|
|
308 | void erase (T &obj) |
|
|
309 | { |
|
|
310 | errase (&obj); |
219 | } |
311 | } |
220 | }; |
312 | }; |
221 | |
313 | |
222 | template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } |
314 | template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } |
223 | template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } |
315 | template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } |
… | |
… | |
238 | typedef double tstamp; |
330 | typedef double tstamp; |
239 | |
331 | |
240 | // return current time as timestampe |
332 | // return current time as timestampe |
241 | tstamp now (); |
333 | tstamp now (); |
242 | |
334 | |
|
|
335 | int similar_direction (int a, int b); |
|
|
336 | |
243 | #endif |
337 | #endif |
244 | |
338 | |