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