author | darkvater |
Mon, 13 Sep 2004 20:38:36 +0000 | |
changeset 236 | da0ae9d977e0 |
parent 222 | b88456001397 |
child 239 | 23958632a582 |
permissions | -rw-r--r-- |
0 | 1 |
#ifndef MACROS_H |
2 |
#define MACROS_H |
|
3 |
||
4 |
#define MAX_INT 0x7FFFFFFF |
|
5 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
22
diff
changeset
|
6 |
#ifdef min |
0 | 7 |
#undef min |
8 |
#endif |
|
9 |
||
10 |
#ifdef max |
|
11 |
#undef max |
|
12 |
#endif |
|
13 |
||
14 |
static INLINE int min(int a, int b) { if (a <= b) return a; return b; } |
|
15 |
static INLINE int max(int a, int b) { if (a >= b) return a; return b; } |
|
236
da0ae9d977e0
(svn r237) -Fix: [1025836] Company value problem (again). Now company value rightly shows the value, including ALL your money.
darkvater
parents:
222
diff
changeset
|
16 |
static INLINE int64 max64(int64 a, int64 b) { if (a >= b) return a; return b; } |
0 | 17 |
|
18 |
static INLINE uint minu(uint a, uint b) { if (a <= b) return a; return b; } |
|
19 |
static INLINE uint maxu(uint a, uint b) { if (a >= b) return a; return b; } |
|
20 |
||
21 |
||
22 |
static INLINE int clamp(int a, int min, int max) { if (a <= min) return min; if (a >= max) return max; return a; } |
|
23 |
static INLINE int clamp2(int a, int min, int max) { if (a <= min) a=min; if (a >= max) a=max; return a; } |
|
24 |
static INLINE bool int32_add_overflow(int32 a, int32 b) { return (int32)(a^b)>=0 && (int32)(a^(a+b))<0; } |
|
25 |
static INLINE bool int32_sub_overflow(int32 a, int32 b) { return (int32)(a^b)<0 && (int32)(a^(a-b))<0; } |
|
26 |
||
27 |
static INLINE bool str_eq(const byte *a, const byte *b) |
|
28 |
{ |
|
29 |
int i=0; |
|
30 |
while (a[i] == b[i]) { |
|
31 |
if (a[i] == 0) |
|
32 |
return true; |
|
33 |
i++; |
|
34 |
} |
|
35 |
return false; |
|
36 |
} |
|
37 |
||
38 |
// Will crash if strings are equal |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
22
diff
changeset
|
39 |
static INLINE bool str_is_below(byte *a, byte *b) { |
0 | 40 |
while (*a <= *b) { |
41 |
if (*a < *b) return true; |
|
42 |
a++; |
|
43 |
b++; |
|
44 |
} |
|
45 |
return false; |
|
46 |
} |
|
47 |
||
48 |
||
49 |
static INLINE int32 BIGMULSS(int32 a, int32 b, int shift) { |
|
50 |
return (int32)(((int64)(a) * (int64)(b)) >> (shift)); |
|
51 |
} |
|
52 |
||
236
da0ae9d977e0
(svn r237) -Fix: [1025836] Company value problem (again). Now company value rightly shows the value, including ALL your money.
darkvater
parents:
222
diff
changeset
|
53 |
static INLINE int64 BIGMULSS64(int64 a, int64 b, int shift) { |
da0ae9d977e0
(svn r237) -Fix: [1025836] Company value problem (again). Now company value rightly shows the value, including ALL your money.
darkvater
parents:
222
diff
changeset
|
54 |
return ((a) * (b)) >> (shift); |
da0ae9d977e0
(svn r237) -Fix: [1025836] Company value problem (again). Now company value rightly shows the value, including ALL your money.
darkvater
parents:
222
diff
changeset
|
55 |
} |
da0ae9d977e0
(svn r237) -Fix: [1025836] Company value problem (again). Now company value rightly shows the value, including ALL your money.
darkvater
parents:
222
diff
changeset
|
56 |
|
0 | 57 |
static INLINE uint32 BIGMULUS(uint32 a, uint32 b, int shift) { |
58 |
return (uint32)(((uint64)(a) * (uint64)(b)) >> (shift)); |
|
59 |
} |
|
60 |
||
61 |
static INLINE int64 BIGMULS(int32 a, int32 b) { |
|
62 |
return (int32)(((int64)(a) * (int64)(b))); |
|
63 |
} |
|
64 |
||
65 |
/* OPT: optimized into an unsigned comparison */ |
|
66 |
//#define IS_INSIDE_1D(x, base, size) ((x) >= (base) && (x) < (base) + (size)) |
|
67 |
#define IS_INSIDE_1D(x, base, size) ( (uint)((x) - (base)) < ((uint)(size)) ) |
|
68 |
||
69 |
#define TILE_X_BITS 8 |
|
70 |
#define TILE_Y_BITS 8 |
|
71 |
#define LANDSCAPE_SIZE_FACTOR 1 |
|
72 |
||
73 |
#define TILE_FROM_XY(x,y) (int)((((y) >> 4) << TILE_X_BITS) + ((x) >> 4)) |
|
74 |
#define TILE_XY(x,y) (int)(((y) << TILE_X_BITS) + (x)) |
|
75 |
||
76 |
#define IS_TILETYPE(_t_, _s_) (_map_type_and_height[(_t_)] >> 4 == (_s_)) |
|
77 |
#define GET_TILETYPE(_t_) (_map_type_and_height[(_t_)] >> 4) |
|
78 |
#define GET_TILEHEIGHT(_t_) ((_map_type_and_height[_t_] & 0xF) * 8) |
|
79 |
||
80 |
enum { |
|
81 |
CORRECT_Z_BITS = 1 << 1 | 1 << 2 | 1 << 3 | 1 << 4 | 1 << 5 | 1 << 6 | 1 << 7 |
|
82 |
}; |
|
83 |
#define CORRECT_Z(tileh) (CORRECT_Z_BITS & (1 << tileh)) |
|
84 |
||
85 |
#define TILES_X (1 << TILE_X_BITS) |
|
86 |
#define TILES_Y (1 << TILE_Y_BITS) |
|
87 |
||
88 |
#define TILE_X_MAX (TILES_X-1) |
|
89 |
#define TILE_Y_MAX (TILES_Y-1) |
|
90 |
||
91 |
#define TILE_ASSERT(x) assert( TILE_MASK(x) == (x) ); |
|
92 |
||
93 |
extern uint SafeTileAdd(uint x, int add, const char *exp, const char *file, int line); |
|
94 |
||
95 |
#if !defined(_DEBUG) || TILE_X_BITS != 8 |
|
96 |
# define TILE_ADD(x,y) ((x)+(y)) |
|
97 |
#else |
|
98 |
# if defined(__GNUC__) |
|
99 |
# define TILE_ADD(x,y) (SafeTileAdd((x),(y), "??", __FILE__, __LINE__)) |
|
100 |
# else |
|
101 |
# define TILE_ADD(x,y) (SafeTileAdd((x),(y), #x ## ", " ## #y, __FILE__, __LINE__)) |
|
102 |
# endif |
|
103 |
#endif |
|
104 |
||
105 |
#define TILE_ADDXY(tile, x, y) TILE_ADD(tile, TILE_XY(x,y)) |
|
106 |
||
107 |
#if TILE_X_BITS == 8 |
|
108 |
#define GET_TILE_X(t) (uint)((byte)(t)) |
|
109 |
#define GET_TILE_Y(t) (uint)((t) >> 8) |
|
110 |
#define TILE_MASK(x) ((uint16)(x)) |
|
111 |
#else |
|
112 |
#define GET_TILE_X(t) (uint)((t) & ((1 << TILE_X_BITS)-1)) |
|
113 |
#define GET_TILE_Y(t) (uint)((t) >> TILE_X_BITS) |
|
114 |
#define TILE_MASK(x) (int)((x) & ((1 << (TILE_X_BITS + TILE_Y_BITS))-1)) |
|
115 |
#endif |
|
116 |
||
117 |
//#define REMADP_COORDS(x,y,z) { int t = x; x = (y-t)*2; y+=t-z; } |
|
118 |
||
119 |
#define PACK_POINT(x,y) ((x) | ((y) << 16)) |
|
120 |
#define UNPACK_POINT_X(p) ((uint16)(p)) |
|
121 |
#define UNPACK_POINT_Y(p) ((uint16)(p>>16)) |
|
122 |
||
123 |
#define PACK_PPOINT(p) PACK_POINT((p).x, (p).y) |
|
124 |
||
125 |
#define HASBIT(x,y) ((x) & (1 << (y))) |
|
126 |
#define SETBIT(x,y) ((x) |= (1 << (y))) |
|
127 |
#define CLRBIT(x,y) ((x) &= ~(1 << (y))) |
|
128 |
||
129 |
// checking more bits. Maybe unneccessary, but easy to use |
|
130 |
#define HASBITS(x,y) ((x) & (y)) |
|
131 |
#define SETBITS(x,y) ((x) |= (y)) |
|
132 |
#define CLRBITS(x,y) ((x) &= ~(y)) |
|
133 |
||
134 |
#define PLAYER_SPRITE_COLOR(owner) ((_player_colors[owner] << 16) + 0x3070000) |
|
135 |
#define SPRITE_PALETTE(x) ((x) + 0x8000) |
|
136 |
||
137 |
extern const byte _ffb_64[128]; |
|
22 | 138 |
/* Returns the position of the first bit that is not zero, counted from the |
139 |
* left. Ie, 10110100 returns 2, 00000001 returns 0, etc. When x == 0 returns |
|
140 |
* 0. |
|
141 |
*/ |
|
0 | 142 |
#define FIND_FIRST_BIT(x) _ffb_64[(x)] |
22 | 143 |
/* Returns x with the first bit that is not zero, counted from the left, set |
144 |
* to zero. So, 10110100 returns 10110000, 00000001 returns 00000000, etc. |
|
145 |
*/ |
|
0 | 146 |
#define KILL_FIRST_BIT(x) _ffb_64[(x)+64] |
147 |
||
148 |
static INLINE int FindFirstBit2x64(int value) |
|
149 |
{ |
|
150 |
int i = 0; |
|
151 |
if ( (byte) value == 0) { |
|
152 |
i += 8; |
|
153 |
value >>= 8; |
|
154 |
} |
|
155 |
return i + FIND_FIRST_BIT(value & 0x3F); |
|
156 |
} |
|
157 |
||
158 |
||
159 |
#if TILE_X_BITS + TILE_Y_BITS <= 16 |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
22
diff
changeset
|
160 |
typedef uint16 TileIndex; |
0 | 161 |
typedef int16 TileIndexDiff; |
162 |
#else |
|
163 |
typedef uint32 TileIndex; |
|
164 |
typedef int32 TileIndexDiff; |
|
165 |
#endif |
|
166 |
||
167 |
/* [min,max), strictly less than */ |
|
168 |
#define IS_BYTE_INSIDE(a,min,max) ((byte)((a)-(min)) < (byte)((max)-(min))) |
|
169 |
#define IS_INT_INSIDE(a,min,max) ((uint)((a)-(min)) < (uint)((max)-(min))) |
|
170 |
||
171 |
||
172 |
#define CHANCE16(a,b) ((uint16)Random() <= (uint16)((65536 * a) / b)) |
|
173 |
#define CHANCE16R(a,b,r) ((uint16)(r=Random()) <= (uint16)((65536 * a) / b)) |
|
174 |
#define CHANCE16I(a,b,v) ((uint16)(v) <= (uint16)((65536 * a) / b)) |
|
175 |
||
176 |
#define BEGIN_TILE_LOOP(var,w,h,tile) \ |
|
177 |
{int h_cur = h; \ |
|
178 |
uint var = tile; \ |
|
179 |
do { \ |
|
180 |
int w_cur = w; \ |
|
181 |
do { |
|
182 |
||
183 |
#define END_TILE_LOOP(var,w,h,tile) \ |
|
184 |
} while (++var, --w_cur != 0); \ |
|
185 |
} while (var += TILE_XY(0,1) - (w), --h_cur != 0);} |
|
186 |
||
187 |
||
188 |
#define for_each_bit(_i,_b) \ |
|
189 |
for(_i=0; _b!=0; _i++,_b>>=1) \ |
|
190 |
if (_b&1) |
|
191 |
||
192 |
#define assert_array(i,j) assert(i < lengthof(j)) |
|
193 |
||
194 |
#define abs myabs |
|
195 |
||
196 |
||
197 |
static INLINE int intxchg_(int *a, int b) { int t = *a; *a = b; return t; } |
|
198 |
#define intxchg(a,b) intxchg_(&(a), (b)) |
|
199 |
#define intswap(a,b) ((b) = intxchg_(&(a), (b))) |
|
200 |
||
201 |
static INLINE int myabs(int a) { if (a<0) a = -a; return a; } |
|
236
da0ae9d977e0
(svn r237) -Fix: [1025836] Company value problem (again). Now company value rightly shows the value, including ALL your money.
darkvater
parents:
222
diff
changeset
|
202 |
static INLINE int64 myabs64(int64 a) { if (a<0) a = -a; return a; } |
0 | 203 |
|
204 |
static INLINE void swap_byte(byte *a, byte *b) { byte t = *a; *a = *b; *b = t; } |
|
205 |
static INLINE void swap_uint16(uint16 *a, uint16 *b) { uint16 t = *a; *a = *b; *b = t; } |
|
206 |
static INLINE void swap_int16(int16 *a, int16 *b) { int16 t = *a; *a = *b; *b = t; } |
|
207 |
static INLINE void swap_tile(TileIndex *a, TileIndex *b) { TileIndex t = *a; *a = *b; *b = t; } |
|
208 |
||
209 |
||
210 |
||
211 |
#if defined(TTD_LITTLE_ENDIAN) |
|
222
b88456001397
(svn r223) -Fix: Const correctness and miscellaneous fixes. Thank you Tron for your diligent fixing of warnings (and some possibly bugs) (Tron)
darkvater
parents:
193
diff
changeset
|
212 |
# define READ_LE_UINT16(b) (*(const uint16*)(b)) |
0 | 213 |
# define ADD_WORD(x) (x)&0xFF, ((x) >> 8)&0xFF |
214 |
# define ADD_DWORD(x) (x)&0xFF, ((x) >> 8)&0xFF, ((x) >> 16)&0xFF, ((x) >> 24)&0xFF |
|
215 |
#elif defined(TTD_BIG_ENDIAN) |
|
216 |
static INLINE uint16 READ_LE_UINT16(const void *b) { |
|
222
b88456001397
(svn r223) -Fix: Const correctness and miscellaneous fixes. Thank you Tron for your diligent fixing of warnings (and some possibly bugs) (Tron)
darkvater
parents:
193
diff
changeset
|
217 |
return ((const byte*)b)[0] + (((const byte*)b)[1] << 8); |
0 | 218 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
22
diff
changeset
|
219 |
# define ADD_WORD(x) ((x) >> 8)&0xFF, (x)&0xFF |
0 | 220 |
# define ADD_DWORD(x) ((x) >> 24)&0xFF, ((x) >> 16)&0xFF, ((x) >> 8)&0xFF, (x)&0xFF |
221 |
#endif |
|
222 |
||
222
b88456001397
(svn r223) -Fix: Const correctness and miscellaneous fixes. Thank you Tron for your diligent fixing of warnings (and some possibly bugs) (Tron)
darkvater
parents:
193
diff
changeset
|
223 |
static INLINE void WRITE_LE_UINT16(void *b, uint16 x) { |
0 | 224 |
((byte*)b)[0] = (byte)x; |
225 |
((byte*)b)[1] = (byte)(x >> 8); |
|
226 |
} |
|
227 |
||
22 | 228 |
#define MAX_DETOUR 6 |
229 |
||
0 | 230 |
#endif /* MACROS_H */ |