2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
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"
|
|
|
|
|
2005-11-16 13:11:28 +00:00
|
|
|
/// Fetch n bits starting at bit s from x
|
|
|
|
#define GB(x, s, n) (((x) >> (s)) & ((1U << (n)) - 1))
|
|
|
|
/// Set n bits starting at bit s in x to d
|
|
|
|
#define SB(x, s, n, d) ((x) = ((x) & ~(((1U << (n)) - 1) << (s))) | ((d) << (s)))
|
|
|
|
/// Add i to the n bits starting at bit s in x
|
|
|
|
#define AB(x, s, n, i) ((x) = ((x) & ~(((1U << (n)) - 1) << (s))) | (((x) + ((i) << (s))) & (((1U << (n)) - 1) << (s))))
|
|
|
|
|
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
|
|
|
|
|
2007-01-11 12:38:04 +00:00
|
|
|
template <typename T>
|
|
|
|
static inline T max(T a, T b)
|
|
|
|
{
|
|
|
|
return a >= b ? a : b;
|
|
|
|
}
|
2007-01-11 11:05:01 +00:00
|
|
|
|
2007-01-10 18:56:51 +00:00
|
|
|
static inline int min(int a, int 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; }
|
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
|
|
|
|
2006-03-28 21:51:14 +00:00
|
|
|
static inline uint clampu(uint a, uint min, uint 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) {
|
2006-08-15 00:19:01 +00:00
|
|
|
return (int64)(a) * (int64)(b);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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)) )
|
|
|
|
|
|
|
|
|
2006-03-03 19:42:09 +00:00
|
|
|
#define HASBIT(x,y) (((x) & (1 << (y))) != 0)
|
2005-01-09 16:02:06 +00:00
|
|
|
#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))
|
|
|
|
|
2006-06-03 15:14:41 +00:00
|
|
|
#define GENERAL_SPRITE_COLOR(color) ( ((color) + PALETTE_RECOLOR_START) << PALETTE_SPRITE_START)
|
2006-03-24 18:16:39 +00:00
|
|
|
#define PLAYER_SPRITE_COLOR(owner) ( GENERAL_SPRITE_COLOR(_player_colors[owner]))
|
2005-07-24 15:56:31 +00:00
|
|
|
#define SPRITE_PALETTE(x) ((x) | PALETTE_MODIFIER_COLOR)
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
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?
|
|
|
|
*/
|
2005-11-16 13:11:28 +00:00
|
|
|
if (GB(value, 0, 8) == 0) {
|
|
|
|
return FIND_FIRST_BIT(GB(value, 8, 6)) + 8;
|
2005-01-31 11:23:10 +00:00
|
|
|
} else {
|
2005-11-16 13:11:28 +00:00
|
|
|
return FIND_FIRST_BIT(GB(value, 0, 6));
|
2005-01-31 11:23:10 +00:00
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-01-31 11:23:10 +00:00
|
|
|
static inline int KillFirstBit2x64(int value)
|
|
|
|
{
|
2005-11-16 13:11:28 +00:00
|
|
|
if (GB(value, 0, 8) == 0) {
|
|
|
|
return KILL_FIRST_BIT(GB(value, 8, 6)) << 8;
|
2005-01-31 11:23:10 +00:00
|
|
|
} else {
|
2005-11-16 13:11:28 +00:00
|
|
|
return value & (KILL_FIRST_BIT(GB(value, 0, 6)) | 0x3F00);
|
2005-01-31 11:23:10 +00:00
|
|
|
}
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-05-11 10:33:58 +00:00
|
|
|
/** returns true if value a has only one bit set to 1 */
|
|
|
|
#define HAS_SINGLE_BIT(a) ( ((a) & ((a) - 1)) == 0)
|
|
|
|
|
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)))
|
|
|
|
|
|
|
|
|
2006-06-03 15:14:41 +00:00
|
|
|
#define CHANCE16(a,b) ((uint16)Random() <= (uint16)((65536 * (a)) / (b)))
|
2006-06-03 19:23:14 +00:00
|
|
|
#define CHANCE16R(a,b,r) ((uint16)(r=Random()) <= (uint16)((65536 * (a)) / (b)))
|
2006-06-03 15:14:41 +00:00
|
|
|
#define CHANCE16I(a,b,v) ((uint16)(v) <= (uint16)((65536 * (a)) / (b)))
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
|
2006-02-01 07:36:15 +00:00
|
|
|
#define for_each_bit(_i, _b) \
|
|
|
|
for (_i = 0; _b != 0; _i++, _b >>= 1) \
|
|
|
|
if (_b & 1)
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
#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
|
|
|
|
|
|
|
|
2006-02-03 21:51:42 +00:00
|
|
|
static inline uint16 ReadLE16Aligned(const void* x)
|
|
|
|
{
|
|
|
|
return FROM_LE16(*(const uint16*)x);
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-02-03 21:51:42 +00:00
|
|
|
static inline uint16 ReadLE16Unaligned(const void* x)
|
|
|
|
{
|
|
|
|
#ifdef OTTD_ALIGNMENT
|
|
|
|
return ((const byte*)x)[0] | ((const byte*)x)[1] << 8;
|
|
|
|
#else
|
|
|
|
return FROM_LE16(*(const uint16*)x);
|
2004-08-09 17:04:08 +00:00
|
|
|
#endif
|
2006-02-03 21:51:42 +00:00
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-07-17 09:41:28 +00:00
|
|
|
/**
|
|
|
|
* ROtate x Left/Right by n (must be >= 0)
|
|
|
|
* @note Assumes a byte has 8 bits
|
|
|
|
*/
|
|
|
|
#define ROL(x, n) ((x) << (n) | (x) >> (sizeof(x) * 8 - (n)))
|
|
|
|
#define ROR(x, n) ((x) >> (n) | (x) << (sizeof(x) * 8 - (n)))
|
2005-05-22 07:12:09 +00:00
|
|
|
|
2005-09-08 12:48:26 +00:00
|
|
|
/**
|
|
|
|
* Return the smallest multiple of n equal or greater than x
|
|
|
|
* @note n must be a power of 2
|
|
|
|
*/
|
|
|
|
#define ALIGN(x, n) (((x) + (n) - 1) & ~((n) - 1))
|
|
|
|
|
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
- New optional landscape generator (TerraGenesis Perlin)
- Load heightmaps (either BMP or PNG)
- Progress dialog while generating worlds (no longer a 'hanging' screen)
- New dialogs for NewGame, Create Scenario and Play Heightmap
- Easier to configure your landscape
- More things to configure (tree-placer, ..)
- Speedup of world generation
- New console command 'restart': restart the map EXACTLY as it was when you
first started it (needs a game made after or with this commit)
- New console command 'getseed': get the seed of your map and share it with
others (of course only works with generated maps)
- Many new, world generation related, things
- Many internal cleanups and rewrites
Many tnx to those people who helped making this:
Belugas, DaleStan, glx, KUDr, RichK67, Rubidium, and TrueLight (alfabetic)
Many tnx to those who helped testing:
Arnau, Bjarni, and tokai (alfabetic)
And to all other people who helped testing and sending comments / bugs
Stats: 673 lines changed, 3534 new lines, 79 new strings
2006-08-19 10:00:30 +00:00
|
|
|
/** return the largest value that can be entered in a variable.
|
|
|
|
* known to work for uint32.
|
|
|
|
* used by TGP to set the max value of the _patches.generation_seed in its definition
|
|
|
|
*/
|
|
|
|
#define MAX_UVALUE(type) ((type)~(type)0)
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#endif /* MACROS_H */
|