ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/util.h
(Generate patch)

Comparing deliantra/server/include/util.h (file contents):
Revision 1.26 by root, Sun Jan 7 02:39:14 2007 UTC vs.
Revision 1.30 by root, Mon Jan 15 01:50:33 2007 UTC

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>
11#include <new> 12#include <new>
12#include <vector> 13#include <vector>
13 14
14#include <glib.h> 15#include <glib.h>
15 16
23// that is in scope for the next statement only 24// that is in scope for the next statement only
24// works only for stuff that can be assigned 0 and converts to false 25// works only for stuff that can be assigned 0 and converts to false
25// (note: works great for pointers) 26// (note: works great for pointers)
26// most ugly macro I ever wrote 27// most ugly macro I ever wrote
27#define declvar(type, name, value) if (type name = 0) { } else if (((name) = (value)), 1) 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
40inline int
41isqrt (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
52inline int
53idistance (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 */
72inline int
73absdir (int d)
74{
75 return ((d - 1) & 7) + 1;
76}
28 77
29// makes dynamically allocated objects zero-initialised 78// makes dynamically allocated objects zero-initialised
30struct zero_initialised 79struct zero_initialised
31{ 80{
32 void *operator new (size_t s, void *p) 81 void *operator new (size_t s, void *p)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines