2004-08-09 17:04:08 +00:00
|
|
|
#ifndef MACROS_H
|
|
|
|
#define MACROS_H
|
|
|
|
|
2005-01-07 17:02:43 +00:00
|
|
|
#include "map.h"
|
|
|
|
|
2004-09-10 19:02:27 +00:00
|
|
|
#ifdef min
|
2004-08-09 17:04:08 +00:00
|
|
|
#undef min
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef max
|
|
|
|
#undef max
|
|
|
|
#endif
|
|
|
|
|
2004-11-24 13:19:48 +00:00
|
|
|
static inline int min(int a, int b) { if (a <= b) return a; return b; }
|
|
|
|
static inline int max(int a, int b) { if (a >= b) return a; return b; }
|
|
|
|
static inline int64 max64(int64 a, int64 b) { if (a >= b) return a; return b; }
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-11-24 13:19:48 +00:00
|
|
|
static inline uint minu(uint a, uint b) { if (a <= b) return a; return b; }
|
|
|
|
static inline uint maxu(uint a, uint b) { if (a >= b) return a; return b; }
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
|
2005-02-22 18:28:20 +00:00
|
|
|
static inline int clamp(int a, int min, int max)
|
|
|
|
{
|
|
|
|
if (a <= min) return min;
|
|
|
|
if (a >= max) return max;
|
|
|
|
return a;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
|
2004-11-24 13:19:48 +00:00
|
|
|
static inline int32 BIGMULSS(int32 a, int32 b, int shift) {
|
2004-08-09 17:04:08 +00:00
|
|
|
return (int32)(((int64)(a) * (int64)(b)) >> (shift));
|
|
|
|
}
|
|
|
|
|
2004-11-24 13:19:48 +00:00
|
|
|
static inline int64 BIGMULSS64(int64 a, int64 b, int shift) {
|
2004-09-13 20:38:36 +00:00
|
|
|
return ((a) * (b)) >> (shift);
|
|
|
|
}
|
|
|
|
|
2004-11-24 13:19:48 +00:00
|
|
|
static inline uint32 BIGMULUS(uint32 a, uint32 b, int shift) {
|
2004-08-09 17:04:08 +00:00
|
|
|
return (uint32)(((uint64)(a) * (uint64)(b)) >> (shift));
|
|
|
|
}
|
|
|
|
|
2004-11-24 13:19:48 +00:00
|
|
|
static inline int64 BIGMULS(int32 a, int32 b) {
|
2004-08-09 17:04:08 +00:00
|
|
|
return (int32)(((int64)(a) * (int64)(b)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* OPT: optimized into an unsigned comparison */
|
|
|
|
//#define IS_INSIDE_1D(x, base, size) ((x) >= (base) && (x) < (base) + (size))
|
|
|
|
#define IS_INSIDE_1D(x, base, size) ( (uint)((x) - (base)) < ((uint)(size)) )
|
|
|
|
|
|
|
|
|
2005-01-09 16:02:06 +00:00
|
|
|
#define HASBIT(x,y) ((x) & (1 << (y)))
|
|
|
|
#define SETBIT(x,y) ((x) |= (1 << (y)))
|
|
|
|
#define CLRBIT(x,y) ((x) &= ~(1 << (y)))
|
|
|
|
#define TOGGLEBIT(x,y) ((x) ^= (1 << (y)))
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// checking more bits. Maybe unneccessary, but easy to use
|
|
|
|
#define HASBITS(x,y) ((x) & (y))
|
|
|
|
#define SETBITS(x,y) ((x) |= (y))
|
|
|
|
#define CLRBITS(x,y) ((x) &= ~(y))
|
|
|
|
|
|
|
|
#define PLAYER_SPRITE_COLOR(owner) ((_player_colors[owner] << 16) + 0x3070000)
|
|
|
|
#define SPRITE_PALETTE(x) ((x) + 0x8000)
|
|
|
|
|
|
|
|
extern const byte _ffb_64[128];
|
2004-08-11 22:07:08 +00:00
|
|
|
/* Returns the position of the first bit that is not zero, counted from the
|
|
|
|
* left. Ie, 10110100 returns 2, 00000001 returns 0, etc. When x == 0 returns
|
|
|
|
* 0.
|
|
|
|
*/
|
2004-08-09 17:04:08 +00:00
|
|
|
#define FIND_FIRST_BIT(x) _ffb_64[(x)]
|
2004-08-11 22:07:08 +00:00
|
|
|
/* Returns x with the first bit that is not zero, counted from the left, set
|
|
|
|
* to zero. So, 10110100 returns 10110000, 00000001 returns 00000000, etc.
|
|
|
|
*/
|
2004-08-09 17:04:08 +00:00
|
|
|
#define KILL_FIRST_BIT(x) _ffb_64[(x)+64]
|
|
|
|
|
2004-11-24 13:19:48 +00:00
|
|
|
static inline int FindFirstBit2x64(int value)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-01-31 11:23:10 +00:00
|
|
|
/*
|
2004-08-09 17:04:08 +00:00
|
|
|
int i = 0;
|
|
|
|
if ( (byte) value == 0) {
|
|
|
|
i += 8;
|
|
|
|
value >>= 8;
|
|
|
|
}
|
|
|
|
return i + FIND_FIRST_BIT(value & 0x3F);
|
2005-01-31 11:23:10 +00:00
|
|
|
|
|
|
|
Faster ( or at least cleaner ) implementation below?
|
|
|
|
*/
|
|
|
|
if ( (byte) value == 0) {
|
|
|
|
return FIND_FIRST_BIT((value >> 8) & 0x3F) + 8;
|
|
|
|
} else {
|
|
|
|
return FIND_FIRST_BIT(value & 0x3F);
|
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-01-31 11:23:10 +00:00
|
|
|
static inline int KillFirstBit2x64(int value)
|
|
|
|
{
|
|
|
|
if ( (byte) value == 0) {
|
|
|
|
return KILL_FIRST_BIT((value >> 8) & 0x3F) << 8;
|
|
|
|
} else {
|
|
|
|
return value & (KILL_FIRST_BIT(value & 0x3F)|0x3F00);
|
|
|
|
}
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
/* [min,max), strictly less than */
|
|
|
|
#define IS_BYTE_INSIDE(a,min,max) ((byte)((a)-(min)) < (byte)((max)-(min)))
|
|
|
|
#define IS_INT_INSIDE(a,min,max) ((uint)((a)-(min)) < (uint)((max)-(min)))
|
|
|
|
|
|
|
|
|
|
|
|
#define CHANCE16(a,b) ((uint16)Random() <= (uint16)((65536 * a) / b))
|
|
|
|
#define CHANCE16R(a,b,r) ((uint16)(r=Random()) <= (uint16)((65536 * a) / b))
|
|
|
|
#define CHANCE16I(a,b,v) ((uint16)(v) <= (uint16)((65536 * a) / b))
|
|
|
|
|
|
|
|
#define BEGIN_TILE_LOOP(var,w,h,tile) \
|
|
|
|
{int h_cur = h; \
|
|
|
|
uint var = tile; \
|
|
|
|
do { \
|
|
|
|
int w_cur = w; \
|
|
|
|
do {
|
|
|
|
|
|
|
|
#define END_TILE_LOOP(var,w,h,tile) \
|
|
|
|
} while (++var, --w_cur != 0); \
|
|
|
|
} while (var += TILE_XY(0,1) - (w), --h_cur != 0);}
|
|
|
|
|
|
|
|
|
|
|
|
#define for_each_bit(_i,_b) \
|
|
|
|
for(_i=0; _b!=0; _i++,_b>>=1) \
|
|
|
|
if (_b&1)
|
|
|
|
|
|
|
|
#define abs myabs
|
|
|
|
|
|
|
|
|
2004-11-24 13:19:48 +00:00
|
|
|
static inline int intxchg_(int *a, int b) { int t = *a; *a = b; return t; }
|
2004-08-09 17:04:08 +00:00
|
|
|
#define intswap(a,b) ((b) = intxchg_(&(a), (b)))
|
2005-06-01 11:52:44 +00:00
|
|
|
static inline int uintxchg_(uint *a, uint b) { uint t = *a; *a = b; return t; }
|
|
|
|
#define uintswap(a,b) ((b) = uintxchg_(&(a), (b)))
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-11-24 13:19:48 +00:00
|
|
|
static inline int myabs(int a) { if (a<0) a = -a; return a; }
|
|
|
|
static inline int64 myabs64(int64 a) { if (a<0) a = -a; return a; }
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-11-24 13:19:48 +00:00
|
|
|
static inline void swap_byte(byte *a, byte *b) { byte t = *a; *a = *b; *b = t; }
|
|
|
|
static inline void swap_uint16(uint16 *a, uint16 *b) { uint16 t = *a; *a = *b; *b = t; }
|
|
|
|
static inline void swap_int16(int16 *a, int16 *b) { int16 t = *a; *a = *b; *b = t; }
|
2005-01-25 21:43:57 +00:00
|
|
|
static inline void swap_int32(int32 *a, int32 *b) { int32 t = *a; *a = *b; *b = t; }
|
2004-11-24 13:19:48 +00:00
|
|
|
static inline void swap_tile(TileIndex *a, TileIndex *b) { TileIndex t = *a; *a = *b; *b = t; }
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(TTD_LITTLE_ENDIAN)
|
2004-09-12 21:49:38 +00:00
|
|
|
# define READ_LE_UINT16(b) (*(const uint16*)(b))
|
2004-08-09 17:04:08 +00:00
|
|
|
#elif defined(TTD_BIG_ENDIAN)
|
2004-11-24 13:19:48 +00:00
|
|
|
static inline uint16 READ_LE_UINT16(const void *b) {
|
2004-09-12 21:49:38 +00:00
|
|
|
return ((const byte*)b)[0] + (((const byte*)b)[1] << 8);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-05-22 07:12:09 +00:00
|
|
|
// Fetch count bits starting at bit start from value
|
|
|
|
#define GB(value, start, count) (((value) >> (start)) & ((1 << (count)) - 1))
|
|
|
|
// Set count bits in value starting at bit start to data
|
|
|
|
#define SB(value, start, count, data) ((value) = ((value) & ~(((1 << (count)) - 1) << (start))) | ((data) << (start)))
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#endif /* MACROS_H */
|