… | |
… | |
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> |
|
|
19 | |
|
|
20 | // use a gcc extension for auto declarations until ISO C++ sanctifies them |
|
|
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 | // 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 | |
13 | |
67 | |
14 | // makes dynamically allocated objects zero-initialised |
68 | // makes dynamically allocated objects zero-initialised |
15 | struct zero_initialised |
69 | struct zero_initialised |
16 | { |
70 | { |
17 | void *operator new (size_t s, void *p) |
71 | void *operator new (size_t s, void *p) |
… | |
… | |
39 | { |
93 | { |
40 | g_slice_free1 (s, p); |
94 | g_slice_free1 (s, p); |
41 | } |
95 | } |
42 | }; |
96 | }; |
43 | |
97 | |
|
|
98 | void *salloc_ (int n) throw (std::bad_alloc); |
|
|
99 | void *salloc_ (int n, void *src) throw (std::bad_alloc); |
|
|
100 | |
44 | // strictly the same as g_slice_alloc, but never returns 0 |
101 | // strictly the same as g_slice_alloc, but never returns 0 |
45 | void *alloc (int s) throw (std::bad_alloc); |
102 | template<typename T> |
|
|
103 | inline T *salloc (int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T)); } |
|
|
104 | |
|
|
105 | // also copies src into the new area, like "memdup" |
|
|
106 | // if src is 0, clears the memory |
|
|
107 | template<typename T> |
|
|
108 | inline T *salloc (int n, T *src) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), (void *)src); } |
|
|
109 | |
|
|
110 | // clears the memory |
|
|
111 | template<typename T> |
|
|
112 | inline T *salloc0(int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), 0); } |
|
|
113 | |
46 | // for symmetry |
114 | // for symmetry |
47 | inline void dealloc (void *p, int s) throw () |
115 | template<typename T> |
|
|
116 | inline void sfree (T *ptr, int n = 1) throw () |
48 | { |
117 | { |
49 | g_slice_free1 (s, p); |
118 | g_slice_free1 (n * sizeof (T), (void *)ptr); |
50 | } |
119 | } |
51 | |
120 | |
52 | // a STL-compatible allocator that uses g_slice |
121 | // a STL-compatible allocator that uses g_slice |
53 | // boy, this is verbose |
122 | // boy, this is verbose |
54 | template<typename Tp> |
123 | template<typename Tp> |
… | |
… | |
78 | pointer address (reference x) const { return &x; } |
147 | pointer address (reference x) const { return &x; } |
79 | const_pointer address (const_reference x) const { return &x; } |
148 | const_pointer address (const_reference x) const { return &x; } |
80 | |
149 | |
81 | pointer allocate (size_type n, const_pointer = 0) |
150 | pointer allocate (size_type n, const_pointer = 0) |
82 | { |
151 | { |
83 | return static_cast<pointer>(alloc (n * sizeof (Tp))); |
152 | return salloc<Tp> (n); |
84 | } |
153 | } |
85 | |
154 | |
86 | void deallocate (pointer p, size_type n) |
155 | void deallocate (pointer p, size_type n) |
87 | { |
156 | { |
88 | dealloc (static_cast<void *>(p), n * sizeof (Tp)); |
157 | sfree<Tp> (p, n); |
89 | } |
158 | } |
90 | |
159 | |
91 | size_type max_size ()const throw () |
160 | size_type max_size ()const throw () |
92 | { |
161 | { |
93 | return size_t (-1) / sizeof (Tp); |
162 | return size_t (-1) / sizeof (Tp); |
… | |
… | |
100 | |
169 | |
101 | void destroy (pointer p) |
170 | void destroy (pointer p) |
102 | { |
171 | { |
103 | p->~Tp (); |
172 | p->~Tp (); |
104 | } |
173 | } |
105 | }; |
|
|
106 | |
|
|
107 | struct refcounted |
|
|
108 | { |
|
|
109 | mutable int refcnt; |
|
|
110 | refcounted () : refcnt (0) { } |
|
|
111 | void refcnt_inc () { ++refcnt; } |
|
|
112 | void refcnt_dec () { --refcnt; |
|
|
113 | if (refcnt < 0)abort();}//D |
|
|
114 | }; |
174 | }; |
115 | |
175 | |
116 | template<class T> |
176 | template<class T> |
117 | struct refptr |
177 | struct refptr |
118 | { |
178 | { |
… | |
… | |
141 | T &operator * () const { return *p; } |
201 | T &operator * () const { return *p; } |
142 | T *operator ->() const { return p; } |
202 | T *operator ->() const { return p; } |
143 | |
203 | |
144 | operator T *() const { return p; } |
204 | operator T *() const { return p; } |
145 | }; |
205 | }; |
|
|
206 | |
|
|
207 | typedef refptr<maptile> maptile_ptr; |
|
|
208 | typedef refptr<object> object_ptr; |
|
|
209 | typedef refptr<archetype> arch_ptr; |
|
|
210 | typedef refptr<client> client_ptr; |
|
|
211 | typedef refptr<player> player_ptr; |
146 | |
212 | |
147 | struct str_hash |
213 | struct str_hash |
148 | { |
214 | { |
149 | std::size_t operator ()(const char *s) const |
215 | std::size_t operator ()(const char *s) const |
150 | { |
216 | { |
… | |
… | |
176 | { |
242 | { |
177 | return !strcmp (a, b); |
243 | return !strcmp (a, b); |
178 | } |
244 | } |
179 | }; |
245 | }; |
180 | |
246 | |
181 | #include <vector> |
|
|
182 | |
|
|
183 | template<class obj> |
247 | template<class T> |
184 | struct unordered_vector : std::vector<obj, slice_allocator<obj> > |
248 | struct unordered_vector : std::vector<T, slice_allocator<T> > |
185 | { |
249 | { |
186 | typedef typename unordered_vector::iterator iterator; |
250 | typedef typename unordered_vector::iterator iterator; |
187 | |
251 | |
188 | void erase (unsigned int pos) |
252 | void erase (unsigned int pos) |
189 | { |
253 | { |
… | |
… | |
194 | } |
258 | } |
195 | |
259 | |
196 | void erase (iterator i) |
260 | void erase (iterator i) |
197 | { |
261 | { |
198 | erase ((unsigned int )(i - this->begin ())); |
262 | erase ((unsigned int )(i - this->begin ())); |
|
|
263 | } |
|
|
264 | }; |
|
|
265 | |
|
|
266 | template<class T, int T::* index> |
|
|
267 | struct object_vector : std::vector<T *, slice_allocator<T *> > |
|
|
268 | { |
|
|
269 | void insert (T *obj) |
|
|
270 | { |
|
|
271 | assert (!(obj->*index)); |
|
|
272 | push_back (obj); |
|
|
273 | obj->*index = this->size (); |
|
|
274 | } |
|
|
275 | |
|
|
276 | void insert (T &obj) |
|
|
277 | { |
|
|
278 | insert (&obj); |
|
|
279 | } |
|
|
280 | |
|
|
281 | void erase (T *obj) |
|
|
282 | { |
|
|
283 | assert (obj->*index); |
|
|
284 | int pos = obj->*index; |
|
|
285 | obj->*index = 0; |
|
|
286 | |
|
|
287 | if (pos < this->size ()) |
|
|
288 | { |
|
|
289 | (*this)[pos - 1] = (*this)[this->size () - 1]; |
|
|
290 | (*this)[pos - 1]->*index = pos; |
|
|
291 | } |
|
|
292 | |
|
|
293 | this->pop_back (); |
|
|
294 | } |
|
|
295 | |
|
|
296 | void erase (T &obj) |
|
|
297 | { |
|
|
298 | errase (&obj); |
199 | } |
299 | } |
200 | }; |
300 | }; |
201 | |
301 | |
202 | template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } |
302 | template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } |
203 | template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } |
303 | template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } |
… | |
… | |
213 | inline void assign (char (&dst)[N], const char *src) |
313 | inline void assign (char (&dst)[N], const char *src) |
214 | { |
314 | { |
215 | assign ((char *)&dst, src, N); |
315 | assign ((char *)&dst, src, N); |
216 | } |
316 | } |
217 | |
317 | |
|
|
318 | typedef double tstamp; |
|
|
319 | |
|
|
320 | // return current time as timestampe |
|
|
321 | tstamp now (); |
|
|
322 | |
|
|
323 | int similar_direction (int a, int b); |
|
|
324 | |
218 | #endif |
325 | #endif |
219 | |
326 | |