truelight@0: #ifndef MACROS_H truelight@0: #define MACROS_H truelight@0: truelight@0: #define MAX_INT 0x7FFFFFFF truelight@0: truelight@193: #ifdef min truelight@0: #undef min truelight@0: #endif truelight@0: truelight@0: #ifdef max truelight@0: #undef max truelight@0: #endif truelight@0: truelight@0: static INLINE int min(int a, int b) { if (a <= b) return a; return b; } truelight@0: static INLINE int max(int a, int b) { if (a >= b) return a; return b; } truelight@0: truelight@0: static INLINE uint minu(uint a, uint b) { if (a <= b) return a; return b; } truelight@0: static INLINE uint maxu(uint a, uint b) { if (a >= b) return a; return b; } truelight@0: truelight@0: truelight@0: static INLINE int clamp(int a, int min, int max) { if (a <= min) return min; if (a >= max) return max; return a; } truelight@0: static INLINE int clamp2(int a, int min, int max) { if (a <= min) a=min; if (a >= max) a=max; return a; } truelight@0: static INLINE bool int32_add_overflow(int32 a, int32 b) { return (int32)(a^b)>=0 && (int32)(a^(a+b))<0; } truelight@0: static INLINE bool int32_sub_overflow(int32 a, int32 b) { return (int32)(a^b)<0 && (int32)(a^(a-b))<0; } truelight@0: truelight@0: static INLINE bool str_eq(const byte *a, const byte *b) truelight@0: { truelight@0: int i=0; truelight@0: while (a[i] == b[i]) { truelight@0: if (a[i] == 0) truelight@0: return true; truelight@0: i++; truelight@0: } truelight@0: return false; truelight@0: } truelight@0: truelight@0: // Will crash if strings are equal truelight@193: static INLINE bool str_is_below(byte *a, byte *b) { truelight@0: while (*a <= *b) { truelight@0: if (*a < *b) return true; truelight@0: a++; truelight@0: b++; truelight@0: } truelight@0: return false; truelight@0: } truelight@0: truelight@0: truelight@0: static INLINE int32 BIGMULSS(int32 a, int32 b, int shift) { truelight@0: return (int32)(((int64)(a) * (int64)(b)) >> (shift)); truelight@0: } truelight@0: truelight@0: static INLINE uint32 BIGMULUS(uint32 a, uint32 b, int shift) { truelight@0: return (uint32)(((uint64)(a) * (uint64)(b)) >> (shift)); truelight@0: } truelight@0: truelight@0: static INLINE int64 BIGMULS(int32 a, int32 b) { truelight@0: return (int32)(((int64)(a) * (int64)(b))); truelight@0: } truelight@0: truelight@0: /* OPT: optimized into an unsigned comparison */ truelight@0: //#define IS_INSIDE_1D(x, base, size) ((x) >= (base) && (x) < (base) + (size)) truelight@0: #define IS_INSIDE_1D(x, base, size) ( (uint)((x) - (base)) < ((uint)(size)) ) truelight@0: truelight@0: #define TILE_X_BITS 8 truelight@0: #define TILE_Y_BITS 8 truelight@0: #define LANDSCAPE_SIZE_FACTOR 1 truelight@0: truelight@0: #define TILE_FROM_XY(x,y) (int)((((y) >> 4) << TILE_X_BITS) + ((x) >> 4)) truelight@0: #define TILE_XY(x,y) (int)(((y) << TILE_X_BITS) + (x)) truelight@0: truelight@0: #define IS_TILETYPE(_t_, _s_) (_map_type_and_height[(_t_)] >> 4 == (_s_)) truelight@0: #define GET_TILETYPE(_t_) (_map_type_and_height[(_t_)] >> 4) truelight@0: #define GET_TILEHEIGHT(_t_) ((_map_type_and_height[_t_] & 0xF) * 8) truelight@0: truelight@0: enum { truelight@0: CORRECT_Z_BITS = 1 << 1 | 1 << 2 | 1 << 3 | 1 << 4 | 1 << 5 | 1 << 6 | 1 << 7 truelight@0: }; truelight@0: #define CORRECT_Z(tileh) (CORRECT_Z_BITS & (1 << tileh)) truelight@0: truelight@0: #define TILES_X (1 << TILE_X_BITS) truelight@0: #define TILES_Y (1 << TILE_Y_BITS) truelight@0: truelight@0: #define TILE_X_MAX (TILES_X-1) truelight@0: #define TILE_Y_MAX (TILES_Y-1) truelight@0: truelight@0: #define TILE_ASSERT(x) assert( TILE_MASK(x) == (x) ); truelight@0: truelight@0: extern uint SafeTileAdd(uint x, int add, const char *exp, const char *file, int line); truelight@0: truelight@0: #if !defined(_DEBUG) || TILE_X_BITS != 8 truelight@0: # define TILE_ADD(x,y) ((x)+(y)) truelight@0: #else truelight@0: # if defined(__GNUC__) truelight@0: # define TILE_ADD(x,y) (SafeTileAdd((x),(y), "??", __FILE__, __LINE__)) truelight@0: # else truelight@0: # define TILE_ADD(x,y) (SafeTileAdd((x),(y), #x ## ", " ## #y, __FILE__, __LINE__)) truelight@0: # endif truelight@0: #endif truelight@0: truelight@0: #define TILE_ADDXY(tile, x, y) TILE_ADD(tile, TILE_XY(x,y)) truelight@0: truelight@0: #if TILE_X_BITS == 8 truelight@0: #define GET_TILE_X(t) (uint)((byte)(t)) truelight@0: #define GET_TILE_Y(t) (uint)((t) >> 8) truelight@0: #define TILE_MASK(x) ((uint16)(x)) truelight@0: #else truelight@0: #define GET_TILE_X(t) (uint)((t) & ((1 << TILE_X_BITS)-1)) truelight@0: #define GET_TILE_Y(t) (uint)((t) >> TILE_X_BITS) truelight@0: #define TILE_MASK(x) (int)((x) & ((1 << (TILE_X_BITS + TILE_Y_BITS))-1)) truelight@0: #endif truelight@0: truelight@0: //#define REMADP_COORDS(x,y,z) { int t = x; x = (y-t)*2; y+=t-z; } truelight@0: truelight@0: #define PACK_POINT(x,y) ((x) | ((y) << 16)) truelight@0: #define UNPACK_POINT_X(p) ((uint16)(p)) truelight@0: #define UNPACK_POINT_Y(p) ((uint16)(p>>16)) truelight@0: truelight@0: #define PACK_PPOINT(p) PACK_POINT((p).x, (p).y) truelight@0: truelight@0: #define HASBIT(x,y) ((x) & (1 << (y))) truelight@0: #define SETBIT(x,y) ((x) |= (1 << (y))) truelight@0: #define CLRBIT(x,y) ((x) &= ~(1 << (y))) truelight@0: truelight@0: // checking more bits. Maybe unneccessary, but easy to use truelight@0: #define HASBITS(x,y) ((x) & (y)) truelight@0: #define SETBITS(x,y) ((x) |= (y)) truelight@0: #define CLRBITS(x,y) ((x) &= ~(y)) truelight@0: truelight@0: #define PLAYER_SPRITE_COLOR(owner) ((_player_colors[owner] << 16) + 0x3070000) truelight@0: #define SPRITE_PALETTE(x) ((x) + 0x8000) truelight@0: truelight@0: extern const byte _ffb_64[128]; darkvater@22: /* Returns the position of the first bit that is not zero, counted from the darkvater@22: * left. Ie, 10110100 returns 2, 00000001 returns 0, etc. When x == 0 returns darkvater@22: * 0. darkvater@22: */ truelight@0: #define FIND_FIRST_BIT(x) _ffb_64[(x)] darkvater@22: /* Returns x with the first bit that is not zero, counted from the left, set darkvater@22: * to zero. So, 10110100 returns 10110000, 00000001 returns 00000000, etc. darkvater@22: */ truelight@0: #define KILL_FIRST_BIT(x) _ffb_64[(x)+64] truelight@0: truelight@0: static INLINE int FindFirstBit2x64(int value) truelight@0: { truelight@0: int i = 0; truelight@0: if ( (byte) value == 0) { truelight@0: i += 8; truelight@0: value >>= 8; truelight@0: } truelight@0: return i + FIND_FIRST_BIT(value & 0x3F); truelight@0: } truelight@0: truelight@0: truelight@0: #if TILE_X_BITS + TILE_Y_BITS <= 16 truelight@193: typedef uint16 TileIndex; truelight@0: typedef int16 TileIndexDiff; truelight@0: #else truelight@0: typedef uint32 TileIndex; truelight@0: typedef int32 TileIndexDiff; truelight@0: #endif truelight@0: truelight@0: /* [min,max), strictly less than */ truelight@0: #define IS_BYTE_INSIDE(a,min,max) ((byte)((a)-(min)) < (byte)((max)-(min))) truelight@0: #define IS_INT_INSIDE(a,min,max) ((uint)((a)-(min)) < (uint)((max)-(min))) truelight@0: truelight@0: truelight@0: #define CHANCE16(a,b) ((uint16)Random() <= (uint16)((65536 * a) / b)) truelight@0: #define CHANCE16R(a,b,r) ((uint16)(r=Random()) <= (uint16)((65536 * a) / b)) truelight@0: #define CHANCE16I(a,b,v) ((uint16)(v) <= (uint16)((65536 * a) / b)) truelight@0: truelight@0: #define BEGIN_TILE_LOOP(var,w,h,tile) \ truelight@0: {int h_cur = h; \ truelight@0: uint var = tile; \ truelight@0: do { \ truelight@0: int w_cur = w; \ truelight@0: do { truelight@0: truelight@0: #define END_TILE_LOOP(var,w,h,tile) \ truelight@0: } while (++var, --w_cur != 0); \ truelight@0: } while (var += TILE_XY(0,1) - (w), --h_cur != 0);} truelight@0: truelight@0: truelight@0: #define for_each_bit(_i,_b) \ truelight@0: for(_i=0; _b!=0; _i++,_b>>=1) \ truelight@0: if (_b&1) truelight@0: truelight@0: #define assert_array(i,j) assert(i < lengthof(j)) truelight@0: truelight@0: #define abs myabs truelight@0: truelight@0: truelight@0: static INLINE int intxchg_(int *a, int b) { int t = *a; *a = b; return t; } truelight@0: #define intxchg(a,b) intxchg_(&(a), (b)) truelight@0: #define intswap(a,b) ((b) = intxchg_(&(a), (b))) truelight@0: truelight@0: static INLINE int myabs(int a) { if (a<0) a = -a; return a; } truelight@0: truelight@0: static INLINE void swap_byte(byte *a, byte *b) { byte t = *a; *a = *b; *b = t; } truelight@0: static INLINE void swap_uint16(uint16 *a, uint16 *b) { uint16 t = *a; *a = *b; *b = t; } truelight@0: static INLINE void swap_int16(int16 *a, int16 *b) { int16 t = *a; *a = *b; *b = t; } truelight@0: static INLINE void swap_tile(TileIndex *a, TileIndex *b) { TileIndex t = *a; *a = *b; *b = t; } truelight@0: truelight@0: truelight@0: truelight@0: #if defined(TTD_LITTLE_ENDIAN) truelight@0: # define READ_LE_UINT16(b) (*(uint16*)(b)) truelight@0: # define ADD_WORD(x) (x)&0xFF, ((x) >> 8)&0xFF truelight@0: # define ADD_DWORD(x) (x)&0xFF, ((x) >> 8)&0xFF, ((x) >> 16)&0xFF, ((x) >> 24)&0xFF truelight@0: #elif defined(TTD_BIG_ENDIAN) truelight@0: static INLINE uint16 READ_LE_UINT16(const void *b) { truelight@0: return ((byte*)b)[0] + (((byte*)b)[1] << 8); truelight@0: } truelight@193: # define ADD_WORD(x) ((x) >> 8)&0xFF, (x)&0xFF truelight@0: # define ADD_DWORD(x) ((x) >> 24)&0xFF, ((x) >> 16)&0xFF, ((x) >> 8)&0xFF, (x)&0xFF truelight@0: #endif truelight@0: truelight@0: static INLINE void WRITE_LE_UINT16(const void *b, uint16 x) { truelight@0: ((byte*)b)[0] = (byte)x; truelight@0: ((byte*)b)[1] = (byte)(x >> 8); truelight@0: } truelight@0: darkvater@22: #define MAX_DETOUR 6 darkvater@22: truelight@0: #endif /* MACROS_H */