From 039570342d9c77650d88835c82aef4e6c801a1a0 Mon Sep 17 00:00:00 2001 From: skidd13 Date: Wed, 21 Nov 2007 19:13:38 +0000 Subject: (svn r11488) -Codechange: Spilt the random functions out to seperate file -Codechange: Make the mersenne twister more readable -Codechange: Unify the seeding process of random --- projects/openttd.vcproj | 9 +- projects/openttd_vs80.vcproj | 12 +- projects/openttd_vs90.vcproj | 12 +- source.list | 3 +- src/core/random_func.cpp | 930 +++++++++++++++++++++++++++++++++++++++++++ src/core/random_func.hpp | 234 +++++++++++ src/functions.h | 44 +- src/mersenne.cpp | 76 ---- src/misc.cpp | 51 --- src/os2.cpp | 2 +- src/unix.cpp | 3 +- src/win32.cpp | 4 +- 12 files changed, 1192 insertions(+), 188 deletions(-) create mode 100644 src/core/random_func.cpp create mode 100644 src/core/random_func.hpp delete mode 100644 src/mersenne.cpp diff --git a/projects/openttd.vcproj b/projects/openttd.vcproj index 7ee50b846..cb4270d02 100644 --- a/projects/openttd.vcproj +++ b/projects/openttd.vcproj @@ -197,6 +197,9 @@ + + @@ -257,9 +260,6 @@ - - @@ -426,6 +426,9 @@ + + diff --git a/projects/openttd_vs80.vcproj b/projects/openttd_vs80.vcproj index 07ec91eea..62b4019a8 100644 --- a/projects/openttd_vs80.vcproj +++ b/projects/openttd_vs80.vcproj @@ -495,6 +495,10 @@ RelativePath=".\..\src\console_cmds.cpp" > + + @@ -575,10 +579,6 @@ RelativePath=".\..\src\md5.cpp" > - - @@ -799,6 +799,10 @@ RelativePath=".\..\src\console.h" > + + diff --git a/projects/openttd_vs90.vcproj b/projects/openttd_vs90.vcproj index 9e23cebd3..a997e1682 100644 --- a/projects/openttd_vs90.vcproj +++ b/projects/openttd_vs90.vcproj @@ -492,6 +492,10 @@ RelativePath=".\..\src\console_cmds.cpp" > + + @@ -572,10 +576,6 @@ RelativePath=".\..\src\md5.cpp" > - - @@ -796,6 +796,10 @@ RelativePath=".\..\src\console.h" > + + diff --git a/source.list b/source.list index a4cc2da0a..71b615a60 100644 --- a/source.list +++ b/source.list @@ -10,6 +10,7 @@ cargotype.cpp command.cpp console.cpp console_cmds.cpp +core/random_func.cpp currency.cpp date.cpp debug.cpp @@ -30,7 +31,6 @@ helpers.cpp landscape.cpp map.cpp md5.cpp -mersenne.cpp minilzo.cpp misc.cpp mixer.cpp @@ -108,6 +108,7 @@ cargopacket.h cargotype.h command.h console.h +core/random_func.hpp currency.h date.h debug.h diff --git a/src/core/random_func.cpp b/src/core/random_func.cpp new file mode 100644 index 000000000..be22e376d --- /dev/null +++ b/src/core/random_func.cpp @@ -0,0 +1,930 @@ +/* $Id$ */ + +/** @file random_func.cpp */ + +#include "../stdafx.h" +#include "../macros.h" +#include "../variables.h" +#include "random_func.hpp" + +uint32 InteractiveRandom() +{ + const uint32 s = _random_seeds[1][0]; + const uint32 t = _random_seeds[1][1]; + + _random_seeds[1][0] = s + ROR(t ^ 0x1234567F, 7) + 1; + return _random_seeds[1][1] = ROR(s, 3) - 1; +} + +uint InteractiveRandomRange(uint max) +{ + return GB(InteractiveRandom(), 0, 16) * max >> 16; +} + +#ifdef MERSENNE_TWISTER +// Source code for Mersenne Twister. +// A Random number generator with much higher quality random numbers. + +#define N (624) // length of _mt_state vector +#define M (397) // a period parameter +#define K (0x9908B0DFU) // a magic constant +#define hiBit(u) ((u) & 0x80000000U) // mask all but highest bit of u +#define loBit(u) ((u) & 0x00000001U) // mask all but lowest bit of u +#define loBits(u) ((u) & 0x7FFFFFFFU) // mask the highest bit of u +#define mixBits(u, v) (hiBit(u)|loBits(v)) // move hi bit of u to hi bit of v + +static uint32 _mt_state[N+1]; // _mt_state vector + 1 extra to not violate ANSI C +static uint32 *_mt_next; // _mt_next random value is computed from here +static int _mt_left = -1; // can *_mt_next++ this many times before reloading + +void SetRandomSeed(register uint32 seed) +{ + register uint32 *s = _mt_state; + _mt_left = 0; + + seed |= 1U; + seed &= 0xFFFFFFFFU; + + *s = seed; + + for (register uint i = N; i != 0; i--) { + seed *= 69069U; + *s++; + *s = seed & 0xFFFFFFFFU; + } +} + +static uint32 ReloadRandom() +{ + if (_mt_left < -1) SetRandomSeed(4357U); + + _mt_left = N - 1; + _mt_next = _mt_state + 1; + + register uint32 *p0 = _mt_state; + register uint32 *p2 = _mt_state + 2; + register uint32 *pM = _mt_state + M; + + register uint32 s0 = _mt_state[0]; + register uint32 s1 = _mt_state[1]; + + register uint i = 0; + + for (i = (N - M + 1); i != 0; i--) { + s0 = s1; + s1 = *p2; + *p0 = *pM ^ (mixBits(s0, s1) >> 1) ^ (loBit(s1) ? K : 0U); + *p0++; + *p2++; + *pM++; + } + + pM = _mt_state; + + for (i = M; i != 0; i--) { + s0 = s1; + s1 = *p2; + *p0 = *pM ^ (mixBits(s0, s1) >> 1) ^ (loBit(s1) ? K : 0U); + *p0++; + *p2++; + *pM++; + } + + s1 = _mt_state[0]; + *p0 = *pM ^ (mixBits(s0, s1) >> 1) ^ (loBit(s1) ? K : 0U); + + s1 ^= (s1 >> 11); + s1 ^= (s1 << 7) & 0x9D2C5680U; + s1 ^= (s1 << 15) & 0xEFC60000U; + s1 ^= (s1 >> 18); + return s1; +} + +uint32 Random() +{ + _mt_left--; + if (_mt_left < 0) return ReloadRandom(); + + uint32 y = *_mt_next; + *_mt_next++; + + y ^= (y >> 11); + y ^= (y << 7) & 0x9D2C5680U; + y ^= (y << 15) & 0xEFC60000U; + y ^= (y >> 18); + return y; +} + +#else /* MERSENNE_TWISTER */ +void SetRandomSeed(uint32 seed) +{ + _random_seeds[0][0] = seed; + _random_seeds[0][1] = seed; + _random_seeds[1][0] = seed * 0x1234567; + _random_seeds[1][1] = _random_seeds[1][0]; +} + +#ifdef RANDOM_DEBUG +#include "network/network_data.h" +uint32 DoRandom(int line, const char *file) +{ + if (_networking && (DEREF_CLIENT(0)->status != STATUS_INACTIVE || !_network_server)) + printf("Random [%d/%d] %s:%d\n",_frame_counter, (byte)_current_player, file, line); +#else /* RANDOM_DEBUG */ +uint32 Random() +{ +#endif /* RANDOM_DEBUG */ + const uint32 s = _random_seeds[0][0]; + const uint32 t = _random_seeds[0][1]; + + _random_seeds[0][0] = s + ROR(t ^ 0x1234567F, 7) + 1; + return _random_seeds[0][1] = ROR(s, 3) - 1; +} +#endif /* MERSENNE_TWISTER */ + +#if defined(RANDOM_DEBUG) && !defined(MERSENNE_TWISTER) +uint DoRandomRange(uint max, int line, const char *file) +{ + return GB(DoRandom(line, file), 0, 16) * max >> 16; +} +#else /* RANDOM_DEBUG & !MERSENNE_TWISTER */ +uint RandomRange(uint max) +{ + return GB(Random(), 0, 16) * max >> 16; +} +#endif /* RANDOM_DEBUG & !MERSENNE_TWISTER */ +/* $Id$ */ + +/** @file random_func.cpp */ + +#include "../stdafx.h" +#include "../macros.h" +#include "../variables.h" +#include "random_func.hpp" + +uint32 InteractiveRandom() +{ + const uint32 s = _random_seeds[1][0]; + const uint32 t = _random_seeds[1][1]; + + _random_seeds[1][0] = s + ROR(t ^ 0x1234567F, 7) + 1; + return _random_seeds[1][1] = ROR(s, 3) - 1; +} + +uint InteractiveRandomRange(uint max) +{ + return GB(InteractiveRandom(), 0, 16) * max >> 16; +} + +#ifdef MERSENNE_TWISTER +// Source code for Mersenne Twister. +// A Random number generator with much higher quality random numbers. + +#define N (624) // length of _mt_state vector +#define M (397) // a period parameter +#define K (0x9908B0DFU) // a magic constant +#define hiBit(u) ((u) & 0x80000000U) // mask all but highest bit of u +#define loBit(u) ((u) & 0x00000001U) // mask all but lowest bit of u +#define loBits(u) ((u) & 0x7FFFFFFFU) // mask the highest bit of u +#define mixBits(u, v) (hiBit(u)|loBits(v)) // move hi bit of u to hi bit of v + +static uint32 _mt_state[N+1]; // _mt_state vector + 1 extra to not violate ANSI C +static uint32 *_mt_next; // _mt_next random value is computed from here +static int _mt_left = -1; // can *_mt_next++ this many times before reloading + +void SetRandomSeed(register uint32 seed) +{ + register uint32 *s = _mt_state; + _mt_left = 0; + + seed |= 1U; + seed &= 0xFFFFFFFFU; + + *s = seed; + + for (register uint i = N; i != 0; i--) { + seed *= 69069U; + *s++; + *s = seed & 0xFFFFFFFFU; + } +} + +static uint32 ReloadRandom() +{ + if (_mt_left < -1) SetRandomSeed(4357U); + + _mt_left = N - 1; + _mt_next = _mt_state + 1; + + register uint32 *p0 = _mt_state; + register uint32 *p2 = _mt_state + 2; + register uint32 *pM = _mt_state + M; + + register uint32 s0 = _mt_state[0]; + register uint32 s1 = _mt_state[1]; + + register uint i = 0; + + for (i = (N - M + 1); i != 0; i--) { + s0 = s1; + s1 = *p2; + *p0 = *pM ^ (mixBits(s0, s1) >> 1) ^ (loBit(s1) ? K : 0U); + *p0++; + *p2++; + *pM++; + } + + pM = _mt_state; + + for (i = M; i != 0; i--) { + s0 = s1; + s1 = *p2; + *p0 = *pM ^ (mixBits(s0, s1) >> 1) ^ (loBit(s1) ? K : 0U); + *p0++; + *p2++; + *pM++; + } + + s1 = _mt_state[0]; + *p0 = *pM ^ (mixBits(s0, s1) >> 1) ^ (loBit(s1) ? K : 0U); + + s1 ^= (s1 >> 11); + s1 ^= (s1 << 7) & 0x9D2C5680U; + s1 ^= (s1 << 15) & 0xEFC60000U; + s1 ^= (s1 >> 18); + return s1; +} + +uint32 Random() +{ + _mt_left--; + if (_mt_left < 0) return ReloadRandom(); + + uint32 y = *_mt_next; + *_mt_next++; + + y ^= (y >> 11); + y ^= (y << 7) & 0x9D2C5680U; + y ^= (y << 15) & 0xEFC60000U; + y ^= (y >> 18); + return y; +} + +#else /* MERSENNE_TWISTER */ +void SetRandomSeed(uint32 seed) +{ + _random_seeds[0][0] = seed; + _random_seeds[0][1] = seed; + _random_seeds[1][0] = seed * 0x1234567; + _random_seeds[1][1] = _random_seeds[1][0]; +} + +#ifdef RANDOM_DEBUG +#include "network/network_data.h" +uint32 DoRandom(int line, const char *file) +{ + if (_networking && (DEREF_CLIENT(0)->status != STATUS_INACTIVE || !_network_server)) + printf("Random [%d/%d] %s:%d\n",_frame_counter, (byte)_current_player, file, line); +#else /* RANDOM_DEBUG */ +uint32 Random() +{ +#endif /* RANDOM_DEBUG */ + const uint32 s = _random_seeds[0][0]; + const uint32 t = _random_seeds[0][1]; + + _random_seeds[0][0] = s + ROR(t ^ 0x1234567F, 7) + 1; + return _random_seeds[0][1] = ROR(s, 3) - 1; +} +#endif /* MERSENNE_TWISTER */ + +#if defined(RANDOM_DEBUG) && !defined(MERSENNE_TWISTER) +uint DoRandomRange(uint max, int line, const char *file) +{ + return GB(DoRandom(line, file), 0, 16) * max >> 16; +} +#else /* RANDOM_DEBUG & !MERSENNE_TWISTER */ +uint RandomRange(uint max) +{ + return GB(Random(), 0, 16) * max >> 16; +} +#endif /* RANDOM_DEBUG & !MERSENNE_TWISTER */ +/* $Id$ */ + +/** @file random_func.cpp */ + +#include "../stdafx.h" +#include "../macros.h" +#include "../variables.h" +#include "random_func.hpp" + +uint32 InteractiveRandom() +{ + const uint32 s = _random_seeds[1][0]; + const uint32 t = _random_seeds[1][1]; + + _random_seeds[1][0] = s + ROR(t ^ 0x1234567F, 7) + 1; + return _random_seeds[1][1] = ROR(s, 3) - 1; +} + +uint InteractiveRandomRange(uint max) +{ + return GB(InteractiveRandom(), 0, 16) * max >> 16; +} + +#ifdef MERSENNE_TWISTER +// Source code for Mersenne Twister. +// A Random number generator with much higher quality random numbers. + +#define N (624) // length of _mt_state vector +#define M (397) // a period parameter +#define K (0x9908B0DFU) // a magic constant +#define hiBit(u) ((u) & 0x80000000U) // mask all but highest bit of u +#define loBit(u) ((u) & 0x00000001U) // mask all but lowest bit of u +#define loBits(u) ((u) & 0x7FFFFFFFU) // mask the highest bit of u +#define mixBits(u, v) (hiBit(u)|loBits(v)) // move hi bit of u to hi bit of v + +static uint32 _mt_state[N+1]; // _mt_state vector + 1 extra to not violate ANSI C +static uint32 *_mt_next; // _mt_next random value is computed from here +static int _mt_left = -1; // can *_mt_next++ this many times before reloading + +void SetRandomSeed(register uint32 seed) +{ + register uint32 *s = _mt_state; + _mt_left = 0; + + seed |= 1U; + seed &= 0xFFFFFFFFU; + + *s = seed; + + for (register uint i = N; i != 0; i--) { + seed *= 69069U; + *s++; + *s = seed & 0xFFFFFFFFU; + } +} + +static uint32 ReloadRandom() +{ + if (_mt_left < -1) SetRandomSeed(4357U); + + _mt_left = N - 1; + _mt_next = _mt_state + 1; + + register uint32 *p0 = _mt_state; + register uint32 *p2 = _mt_state + 2; + register uint32 *pM = _mt_state + M; + + register uint32 s0 = _mt_state[0]; + register uint32 s1 = _mt_state[1]; + + register uint i = 0; + + for (i = (N - M + 1); i != 0; i--) { + s0 = s1; + s1 = *p2; + *p0 = *pM ^ (mixBits(s0, s1) >> 1) ^ (loBit(s1) ? K : 0U); + *p0++; + *p2++; + *pM++; + } + + pM = _mt_state; + + for (i = M; i != 0; i--) { + s0 = s1; + s1 = *p2; + *p0 = *pM ^ (mixBits(s0, s1) >> 1) ^ (loBit(s1) ? K : 0U); + *p0++; + *p2++; + *pM++; + } + + s1 = _mt_state[0]; + *p0 = *pM ^ (mixBits(s0, s1) >> 1) ^ (loBit(s1) ? K : 0U); + + s1 ^= (s1 >> 11); + s1 ^= (s1 << 7) & 0x9D2C5680U; + s1 ^= (s1 << 15) & 0xEFC60000U; + s1 ^= (s1 >> 18); + return s1; +} + +uint32 Random() +{ + _mt_left--; + if (_mt_left < 0) return ReloadRandom(); + + uint32 y = *_mt_next; + *_mt_next++; + + y ^= (y >> 11); + y ^= (y << 7) & 0x9D2C5680U; + y ^= (y << 15) & 0xEFC60000U; + y ^= (y >> 18); + return y; +} + +#else /* MERSENNE_TWISTER */ +void SetRandomSeed(uint32 seed) +{ + _random_seeds[0][0] = seed; + _random_seeds[0][1] = seed; + _random_seeds[1][0] = seed * 0x1234567; + _random_seeds[1][1] = _random_seeds[1][0]; +} + +#ifdef RANDOM_DEBUG +#include "network/network_data.h" +uint32 DoRandom(int line, const char *file) +{ + if (_networking && (DEREF_CLIENT(0)->status != STATUS_INACTIVE || !_network_server)) + printf("Random [%d/%d] %s:%d\n",_frame_counter, (byte)_current_player, file, line); +#else /* RANDOM_DEBUG */ +uint32 Random() +{ +#endif /* RANDOM_DEBUG */ + const uint32 s = _random_seeds[0][0]; + const uint32 t = _random_seeds[0][1]; + + _random_seeds[0][0] = s + ROR(t ^ 0x1234567F, 7) + 1; + return _random_seeds[0][1] = ROR(s, 3) - 1; +} +#endif /* MERSENNE_TWISTER */ + +#if defined(RANDOM_DEBUG) && !defined(MERSENNE_TWISTER) +uint DoRandomRange(uint max, int line, const char *file) +{ + return GB(DoRandom(line, file), 0, 16) * max >> 16; +} +#else /* RANDOM_DEBUG & !MERSENNE_TWISTER */ +uint RandomRange(uint max) +{ + return GB(Random(), 0, 16) * max >> 16; +} +#endif /* RANDOM_DEBUG & !MERSENNE_TWISTER */ +/* $Id$ */ + +/** @file random_func.cpp */ + +#include "../stdafx.h" +#include "../macros.h" +#include "../variables.h" +#include "random_func.hpp" + +uint32 InteractiveRandom() +{ + const uint32 s = _random_seeds[1][0]; + const uint32 t = _random_seeds[1][1]; + + _random_seeds[1][0] = s + ROR(t ^ 0x1234567F, 7) + 1; + return _random_seeds[1][1] = ROR(s, 3) - 1; +} + +uint InteractiveRandomRange(uint max) +{ + return GB(InteractiveRandom(), 0, 16) * max >> 16; +} + +#ifdef MERSENNE_TWISTER +// Source code for Mersenne Twister. +// A Random number generator with much higher quality random numbers. + +#define N (624) // length of _mt_state vector +#define M (397) // a period parameter +#define K (0x9908B0DFU) // a magic constant +#define hiBit(u) ((u) & 0x80000000U) // mask all but highest bit of u +#define loBit(u) ((u) & 0x00000001U) // mask all but lowest bit of u +#define loBits(u) ((u) & 0x7FFFFFFFU) // mask the highest bit of u +#define mixBits(u, v) (hiBit(u)|loBits(v)) // move hi bit of u to hi bit of v + +static uint32 _mt_state[N+1]; // _mt_state vector + 1 extra to not violate ANSI C +static uint32 *_mt_next; // _mt_next random value is computed from here +static int _mt_left = -1; // can *_mt_next++ this many times before reloading + +void SetRandomSeed(register uint32 seed) +{ + register uint32 *s = _mt_state; + _mt_left = 0; + + seed |= 1U; + seed &= 0xFFFFFFFFU; + + *s = seed; + + for (register uint i = N; i != 0; i--) { + seed *= 69069U; + *s++; + *s = seed & 0xFFFFFFFFU; + } +} + +static uint32 ReloadRandom() +{ + if (_mt_left < -1) SetRandomSeed(4357U); + + _mt_left = N - 1; + _mt_next = _mt_state + 1; + + register uint32 *p0 = _mt_state; + register uint32 *p2 = _mt_state + 2; + register uint32 *pM = _mt_state + M; + + register uint32 s0 = _mt_state[0]; + register uint32 s1 = _mt_state[1]; + + register uint i = 0; + + for (i = (N - M + 1); i != 0; i--) { + s0 = s1; + s1 = *p2; + *p0 = *pM ^ (mixBits(s0, s1) >> 1) ^ (loBit(s1) ? K : 0U); + *p0++; + *p2++; + *pM++; + } + + pM = _mt_state; + + for (i = M; i != 0; i--) { + s0 = s1; + s1 = *p2; + *p0 = *pM ^ (mixBits(s0, s1) >> 1) ^ (loBit(s1) ? K : 0U); + *p0++; + *p2++; + *pM++; + } + + s1 = _mt_state[0]; + *p0 = *pM ^ (mixBits(s0, s1) >> 1) ^ (loBit(s1) ? K : 0U); + + s1 ^= (s1 >> 11); + s1 ^= (s1 << 7) & 0x9D2C5680U; + s1 ^= (s1 << 15) & 0xEFC60000U; + s1 ^= (s1 >> 18); + return s1; +} + +uint32 Random() +{ + _mt_left--; + if (_mt_left < 0) return ReloadRandom(); + + uint32 y = *_mt_next; + *_mt_next++; + + y ^= (y >> 11); + y ^= (y << 7) & 0x9D2C5680U; + y ^= (y << 15) & 0xEFC60000U; + y ^= (y >> 18); + return y; +} + +#else /* MERSENNE_TWISTER */ +void SetRandomSeed(uint32 seed) +{ + _random_seeds[0][0] = seed; + _random_seeds[0][1] = seed; + _random_seeds[1][0] = seed * 0x1234567; + _random_seeds[1][1] = _random_seeds[1][0]; +} + +#ifdef RANDOM_DEBUG +#include "network/network_data.h" +uint32 DoRandom(int line, const char *file) +{ + if (_networking && (DEREF_CLIENT(0)->status != STATUS_INACTIVE || !_network_server)) + printf("Random [%d/%d] %s:%d\n",_frame_counter, (byte)_current_player, file, line); +#else /* RANDOM_DEBUG */ +uint32 Random() +{ +#endif /* RANDOM_DEBUG */ + const uint32 s = _random_seeds[0][0]; + const uint32 t = _random_seeds[0][1]; + + _random_seeds[0][0] = s + ROR(t ^ 0x1234567F, 7) + 1; + return _random_seeds[0][1] = ROR(s, 3) - 1; +} +#endif /* MERSENNE_TWISTER */ + +#if defined(RANDOM_DEBUG) && !defined(MERSENNE_TWISTER) +uint DoRandomRange(uint max, int line, const char *file) +{ + return GB(DoRandom(line, file), 0, 16) * max >> 16; +} +#else /* RANDOM_DEBUG & !MERSENNE_TWISTER */ +uint RandomRange(uint max) +{ + return GB(Random(), 0, 16) * max >> 16; +} +#endif /* RANDOM_DEBUG & !MERSENNE_TWISTER */ +/* $Id$ */ + +/** @file random_func.cpp */ + +#include "../stdafx.h" +#include "../macros.h" +#include "../variables.h" +#include "random_func.hpp" + +uint32 InteractiveRandom() +{ + const uint32 s = _random_seeds[1][0]; + const uint32 t = _random_seeds[1][1]; + + _random_seeds[1][0] = s + ROR(t ^ 0x1234567F, 7) + 1; + return _random_seeds[1][1] = ROR(s, 3) - 1; +} + +uint InteractiveRandomRange(uint max) +{ + return GB(InteractiveRandom(), 0, 16) * max >> 16; +} + +#ifdef MERSENNE_TWISTER +// Source code for Mersenne Twister. +// A Random number generator with much higher quality random numbers. + +#define N (624) // length of _mt_state vector +#define M (397) // a period parameter +#define K (0x9908B0DFU) // a magic constant +#define hiBit(u) ((u) & 0x80000000U) // mask all but highest bit of u +#define loBit(u) ((u) & 0x00000001U) // mask all but lowest bit of u +#define loBits(u) ((u) & 0x7FFFFFFFU) // mask the highest bit of u +#define mixBits(u, v) (hiBit(u)|loBits(v)) // move hi bit of u to hi bit of v + +static uint32 _mt_state[N+1]; // _mt_state vector + 1 extra to not violate ANSI C +static uint32 *_mt_next; // _mt_next random value is computed from here +static int _mt_left = -1; // can *_mt_next++ this many times before reloading + +void SetRandomSeed(register uint32 seed) +{ + register uint32 *s = _mt_state; + _mt_left = 0; + + seed |= 1U; + seed &= 0xFFFFFFFFU; + + *s = seed; + + for (register uint i = N; i != 0; i--) { + seed *= 69069U; + *s++; + *s = seed & 0xFFFFFFFFU; + } +} + +static uint32 ReloadRandom() +{ + if (_mt_left < -1) SetRandomSeed(4357U); + + _mt_left = N - 1; + _mt_next = _mt_state + 1; + + register uint32 *p0 = _mt_state; + register uint32 *p2 = _mt_state + 2; + register uint32 *pM = _mt_state + M; + + register uint32 s0 = _mt_state[0]; + register uint32 s1 = _mt_state[1]; + + register uint i = 0; + + for (i = (N - M + 1); i != 0; i--) { + s0 = s1; + s1 = *p2; + *p0 = *pM ^ (mixBits(s0, s1) >> 1) ^ (loBit(s1) ? K : 0U); + *p0++; + *p2++; + *pM++; + } + + pM = _mt_state; + + for (i = M; i != 0; i--) { + s0 = s1; + s1 = *p2; + *p0 = *pM ^ (mixBits(s0, s1) >> 1) ^ (loBit(s1) ? K : 0U); + *p0++; + *p2++; + *pM++; + } + + s1 = _mt_state[0]; + *p0 = *pM ^ (mixBits(s0, s1) >> 1) ^ (loBit(s1) ? K : 0U); + + s1 ^= (s1 >> 11); + s1 ^= (s1 << 7) & 0x9D2C5680U; + s1 ^= (s1 << 15) & 0xEFC60000U; + s1 ^= (s1 >> 18); + return s1; +} + +uint32 Random() +{ + _mt_left--; + if (_mt_left < 0) return ReloadRandom(); + + uint32 y = *_mt_next; + *_mt_next++; + + y ^= (y >> 11); + y ^= (y << 7) & 0x9D2C5680U; + y ^= (y << 15) & 0xEFC60000U; + y ^= (y >> 18); + return y; +} + +#else /* MERSENNE_TWISTER */ +void SetRandomSeed(uint32 seed) +{ + _random_seeds[0][0] = seed; + _random_seeds[0][1] = seed; + _random_seeds[1][0] = seed * 0x1234567; + _random_seeds[1][1] = _random_seeds[1][0]; +} + +#ifdef RANDOM_DEBUG +#include "network/network_data.h" +uint32 DoRandom(int line, const char *file) +{ + if (_networking && (DEREF_CLIENT(0)->status != STATUS_INACTIVE || !_network_server)) + printf("Random [%d/%d] %s:%d\n",_frame_counter, (byte)_current_player, file, line); +#else /* RANDOM_DEBUG */ +uint32 Random() +{ +#endif /* RANDOM_DEBUG */ + const uint32 s = _random_seeds[0][0]; + const uint32 t = _random_seeds[0][1]; + + _random_seeds[0][0] = s + ROR(t ^ 0x1234567F, 7) + 1; + return _random_seeds[0][1] = ROR(s, 3) - 1; +} +#endif /* MERSENNE_TWISTER */ + +#if defined(RANDOM_DEBUG) && !defined(MERSENNE_TWISTER) +uint DoRandomRange(uint max, int line, const char *file) +{ + return GB(DoRandom(line, file), 0, 16) * max >> 16; +} +#else /* RANDOM_DEBUG & !MERSENNE_TWISTER */ +uint RandomRange(uint max) +{ + return GB(Random(), 0, 16) * max >> 16; +} +#endif /* RANDOM_DEBUG & !MERSENNE_TWISTER */ +/* $Id$ */ + +/** @file random_func.cpp */ + +#include "../stdafx.h" +#include "../macros.h" +#include "../variables.h" +#include "random_func.hpp" + +uint32 InteractiveRandom() +{ + const uint32 s = _random_seeds[1][0]; + const uint32 t = _random_seeds[1][1]; + + _random_seeds[1][0] = s + ROR(t ^ 0x1234567F, 7) + 1; + return _random_seeds[1][1] = ROR(s, 3) - 1; +} + +uint InteractiveRandomRange(uint max) +{ + return GB(InteractiveRandom(), 0, 16) * max >> 16; +} + +#ifdef MERSENNE_TWISTER +// Source code for Mersenne Twister. +// A Random number generator with much higher quality random numbers. + +#define N (624) // length of _mt_state vector +#define M (397) // a period parameter +#define K (0x9908B0DFU) // a magic constant +#define hiBit(u) ((u) & 0x80000000U) // mask all but highest bit of u +#define loBit(u) ((u) & 0x00000001U) // mask all but lowest bit of u +#define loBits(u) ((u) & 0x7FFFFFFFU) // mask the highest bit of u +#define mixBits(u, v) (hiBit(u)|loBits(v)) // move hi bit of u to hi bit of v + +static uint32 _mt_state[N+1]; // _mt_state vector + 1 extra to not violate ANSI C +static uint32 *_mt_next; // _mt_next random value is computed from here +static int _mt_left = -1; // can *_mt_next++ this many times before reloading + +void SetRandomSeed(register uint32 seed) +{ + register uint32 *s = _mt_state; + _mt_left = 0; + + seed |= 1U; + seed &= 0xFFFFFFFFU; + + *s = seed; + + for (register uint i = N; i != 0; i--) { + seed *= 69069U; + *s++; + *s = seed & 0xFFFFFFFFU; + } +} + +static uint32 ReloadRandom() +{ + if (_mt_left < -1) SetRandomSeed(4357U); + + _mt_left = N - 1; + _mt_next = _mt_state + 1; + + register uint32 *p0 = _mt_state; + register uint32 *p2 = _mt_state + 2; + register uint32 *pM = _mt_state + M; + + register uint32 s0 = _mt_state[0]; + register uint32 s1 = _mt_state[1]; + + register uint i = 0; + + for (i = (N - M + 1); i != 0; i--) { + s0 = s1; + s1 = *p2; + *p0 = *pM ^ (mixBits(s0, s1) >> 1) ^ (loBit(s1) ? K : 0U); + *p0++; + *p2++; + *pM++; + } + + pM = _mt_state; + + for (i = M; i != 0; i--) { + s0 = s1; + s1 = *p2; + *p0 = *pM ^ (mixBits(s0, s1) >> 1) ^ (loBit(s1) ? K : 0U); + *p0++; + *p2++; + *pM++; + } + + s1 = _mt_state[0]; + *p0 = *pM ^ (mixBits(s0, s1) >> 1) ^ (loBit(s1) ? K : 0U); + + s1 ^= (s1 >> 11); + s1 ^= (s1 << 7) & 0x9D2C5680U; + s1 ^= (s1 << 15) & 0xEFC60000U; + s1 ^= (s1 >> 18); + return s1; +} + +uint32 Random() +{ + _mt_left--; + if (_mt_left < 0) return ReloadRandom(); + + uint32 y = *_mt_next; + *_mt_next++; + + y ^= (y >> 11); + y ^= (y << 7) & 0x9D2C5680U; + y ^= (y << 15) & 0xEFC60000U; + y ^= (y >> 18); + return y; +} + +#else /* MERSENNE_TWISTER */ +void SetRandomSeed(uint32 seed) +{ + _random_seeds[0][0] = seed; + _random_seeds[0][1] = seed; + _random_seeds[1][0] = seed * 0x1234567; + _random_seeds[1][1] = _random_seeds[1][0]; +} + +#ifdef RANDOM_DEBUG +#include "network/network_data.h" +uint32 DoRandom(int line, const char *file) +{ + if (_networking && (DEREF_CLIENT(0)->status != STATUS_INACTIVE || !_network_server)) + printf("Random [%d/%d] %s:%d\n",_frame_counter, (byte)_current_player, file, line); +#else /* RANDOM_DEBUG */ +uint32 Random() +{ +#endif /* RANDOM_DEBUG */ + const uint32 s = _random_seeds[0][0]; + const uint32 t = _random_seeds[0][1]; + + _random_seeds[0][0] = s + ROR(t ^ 0x1234567F, 7) + 1; + return _random_seeds[0][1] = ROR(s, 3) - 1; +} +#endif /* MERSENNE_TWISTER */ + +#if defined(RANDOM_DEBUG) && !defined(MERSENNE_TWISTER) +uint DoRandomRange(uint max, int line, const char *file) +{ + return GB(DoRandom(line, file), 0, 16) * max >> 16; +} +#else /* RANDOM_DEBUG & !MERSENNE_TWISTER */ +uint RandomRange(uint max) +{ + return GB(Random(), 0, 16) * max >> 16; +} +#endif /* RANDOM_DEBUG & !MERSENNE_TWISTER */ diff --git a/src/core/random_func.hpp b/src/core/random_func.hpp new file mode 100644 index 000000000..2169c11fb --- /dev/null +++ b/src/core/random_func.hpp @@ -0,0 +1,234 @@ +/* $Id$ */ + +/** @file random_func.h */ + +#ifndef RANDOM_FUNC_HPP +#define RANDOM_FUNC_HPP + +/************** + * Warning: DO NOT enable this unless you understand what it does + * + * If enabled, in a network game all randoms will be dumped to the + * stdout if the first client joins (or if you are a client). This + * is to help finding desync problems. + * + * Warning: DO NOT enable this unless you understand what it does + **************/ + +//#define RANDOM_DEBUG + + +// Enable this to produce higher quality random numbers. +// Doesn't work with network yet. +// #define MERSENNE_TWISTER + +void SetRandomSeed(uint32 seed); +#ifdef RANDOM_DEBUG + #define Random() DoRandom(__LINE__, __FILE__) + uint32 DoRandom(int line, const char *file); + #define RandomRange(max) DoRandomRange(max, __LINE__, __FILE__) + uint DoRandomRange(uint max, int line, const char *file); +#else + uint32 Random(); + uint RandomRange(uint max); +#endif + +uint32 InteractiveRandom(); // Used for random sequences that are not the same on the other end of the multiplayer link +uint InteractiveRandomRange(uint max); + +#endif /* RANDOM_FUNC_HPP */ +/* $Id$ */ + +/** @file random_func.h */ + +#ifndef RANDOM_FUNC_HPP +#define RANDOM_FUNC_HPP + +/************** + * Warning: DO NOT enable this unless you understand what it does + * + * If enabled, in a network game all randoms will be dumped to the + * stdout if the first client joins (or if you are a client). This + * is to help finding desync problems. + * + * Warning: DO NOT enable this unless you understand what it does + **************/ + +//#define RANDOM_DEBUG + + +// Enable this to produce higher quality random numbers. +// Doesn't work with network yet. +// #define MERSENNE_TWISTER + +void SetRandomSeed(uint32 seed); +#ifdef RANDOM_DEBUG + #define Random() DoRandom(__LINE__, __FILE__) + uint32 DoRandom(int line, const char *file); + #define RandomRange(max) DoRandomRange(max, __LINE__, __FILE__) + uint DoRandomRange(uint max, int line, const char *file); +#else + uint32 Random(); + uint RandomRange(uint max); +#endif + +uint32 InteractiveRandom(); // Used for random sequences that are not the same on the other end of the multiplayer link +uint InteractiveRandomRange(uint max); + +#endif /* RANDOM_FUNC_HPP */ +/* $Id$ */ + +/** @file random_func.h */ + +#ifndef RANDOM_FUNC_HPP +#define RANDOM_FUNC_HPP + +/************** + * Warning: DO NOT enable this unless you understand what it does + * + * If enabled, in a network game all randoms will be dumped to the + * stdout if the first client joins (or if you are a client). This + * is to help finding desync problems. + * + * Warning: DO NOT enable this unless you understand what it does + **************/ + +//#define RANDOM_DEBUG + + +// Enable this to produce higher quality random numbers. +// Doesn't work with network yet. +// #define MERSENNE_TWISTER + +void SetRandomSeed(uint32 seed); +#ifdef RANDOM_DEBUG + #define Random() DoRandom(__LINE__, __FILE__) + uint32 DoRandom(int line, const char *file); + #define RandomRange(max) DoRandomRange(max, __LINE__, __FILE__) + uint DoRandomRange(uint max, int line, const char *file); +#else + uint32 Random(); + uint RandomRange(uint max); +#endif + +uint32 InteractiveRandom(); // Used for random sequences that are not the same on the other end of the multiplayer link +uint InteractiveRandomRange(uint max); + +#endif /* RANDOM_FUNC_HPP */ +/* $Id$ */ + +/** @file random_func.h */ + +#ifndef RANDOM_FUNC_HPP +#define RANDOM_FUNC_HPP + +/************** + * Warning: DO NOT enable this unless you understand what it does + * + * If enabled, in a network game all randoms will be dumped to the + * stdout if the first client joins (or if you are a client). This + * is to help finding desync problems. + * + * Warning: DO NOT enable this unless you understand what it does + **************/ + +//#define RANDOM_DEBUG + + +// Enable this to produce higher quality random numbers. +// Doesn't work with network yet. +// #define MERSENNE_TWISTER + +void SetRandomSeed(uint32 seed); +#ifdef RANDOM_DEBUG + #define Random() DoRandom(__LINE__, __FILE__) + uint32 DoRandom(int line, const char *file); + #define RandomRange(max) DoRandomRange(max, __LINE__, __FILE__) + uint DoRandomRange(uint max, int line, const char *file); +#else + uint32 Random(); + uint RandomRange(uint max); +#endif + +uint32 InteractiveRandom(); // Used for random sequences that are not the same on the other end of the multiplayer link +uint InteractiveRandomRange(uint max); + +#endif /* RANDOM_FUNC_HPP */ +/* $Id$ */ + +/** @file random_func.h */ + +#ifndef RANDOM_FUNC_HPP +#define RANDOM_FUNC_HPP + +/************** + * Warning: DO NOT enable this unless you understand what it does + * + * If enabled, in a network game all randoms will be dumped to the + * stdout if the first client joins (or if you are a client). This + * is to help finding desync problems. + * + * Warning: DO NOT enable this unless you understand what it does + **************/ + +//#define RANDOM_DEBUG + + +// Enable this to produce higher quality random numbers. +// Doesn't work with network yet. +// #define MERSENNE_TWISTER + +void SetRandomSeed(uint32 seed); +#ifdef RANDOM_DEBUG + #define Random() DoRandom(__LINE__, __FILE__) + uint32 DoRandom(int line, const char *file); + #define RandomRange(max) DoRandomRange(max, __LINE__, __FILE__) + uint DoRandomRange(uint max, int line, const char *file); +#else + uint32 Random(); + uint RandomRange(uint max); +#endif + +uint32 InteractiveRandom(); // Used for random sequences that are not the same on the other end of the multiplayer link +uint InteractiveRandomRange(uint max); + +#endif /* RANDOM_FUNC_HPP */ +/* $Id$ */ + +/** @file random_func.h */ + +#ifndef RANDOM_FUNC_HPP +#define RANDOM_FUNC_HPP + +/************** + * Warning: DO NOT enable this unless you understand what it does + * + * If enabled, in a network game all randoms will be dumped to the + * stdout if the first client joins (or if you are a client). This + * is to help finding desync problems. + * + * Warning: DO NOT enable this unless you understand what it does + **************/ + +//#define RANDOM_DEBUG + + +// Enable this to produce higher quality random numbers. +// Doesn't work with network yet. +// #define MERSENNE_TWISTER + +void SetRandomSeed(uint32 seed); +#ifdef RANDOM_DEBUG + #define Random() DoRandom(__LINE__, __FILE__) + uint32 DoRandom(int line, const char *file); + #define RandomRange(max) DoRandomRange(max, __LINE__, __FILE__) + uint DoRandomRange(uint max, int line, const char *file); +#else + uint32 Random(); + uint RandomRange(uint max); +#endif + +uint32 InteractiveRandom(); // Used for random sequences that are not the same on the other end of the multiplayer link +uint InteractiveRandomRange(uint max); + +#endif /* RANDOM_FUNC_HPP */ diff --git a/src/functions.h b/src/functions.h index ec680e7e8..191ad7708 100644 --- a/src/functions.h +++ b/src/functions.h @@ -7,6 +7,7 @@ #include "gfx.h" #include "viewport.h" +#include "core/random_func.hpp" void UpdateTownMaxPass(Town *t); @@ -32,52 +33,9 @@ void ShowInfo(const char *str); void CDECL ShowInfoF(const char *str, ...); /* openttd.cpp */ - -/************** - * Warning: DO NOT enable this unless you understand what it does - * - * If enabled, in a network game all randoms will be dumped to the - * stdout if the first client joins (or if you are a client). This - * is to help finding desync problems. - * - * Warning: DO NOT enable this unless you understand what it does - **************/ - -//#define RANDOM_DEBUG - - -// Enable this to produce higher quality random numbers. -// Doesn't work with network yet. -//#define MERSENNE_TWISTER - -// Mersenne twister functions -void SeedMT(uint32 seed); -uint32 RandomMT(); - - -#ifdef MERSENNE_TWISTER - static inline uint32 Random() { return RandomMT(); } - uint RandomRange(uint max); -#else - -#ifdef RANDOM_DEBUG - #define Random() DoRandom(__LINE__, __FILE__) - uint32 DoRandom(int line, const char *file); - #define RandomRange(max) DoRandomRange(max, __LINE__, __FILE__) - uint DoRandomRange(uint max, int line, const char *file); -#else - uint32 Random(); - uint RandomRange(uint max); -#endif -#endif // MERSENNE_TWISTER - static inline TileIndex RandomTileSeed(uint32 r) { return TILE_MASK(r); } static inline TileIndex RandomTile() { return TILE_MASK(Random()); } - -uint32 InteractiveRandom(); // Used for random sequences that are not the same on the other end of the multiplayer link -uint InteractiveRandomRange(uint max); - /* texteff.cpp */ void AddAnimatedTile(TileIndex tile); void DeleteAnimatedTile(TileIndex tile); diff --git a/src/mersenne.cpp b/src/mersenne.cpp deleted file mode 100644 index 70a443149..000000000 --- a/src/mersenne.cpp +++ /dev/null @@ -1,76 +0,0 @@ -/* $Id$ */ - -/** @file mersenne.cpp */ - -#include "stdafx.h" -#include "openttd.h" - -#ifdef MERSENNE_TWISTER - -// Source code for Mersenne Twister. -// A Random number generator with much higher quality random numbers. - -#define N (624) // length of _mt_state vector -#define M (397) // a period parameter -#define K (0x9908B0DFU) // a magic constant -#define hiBit(u) ((u) & 0x80000000U) // mask all but highest bit of u -#define loBit(u) ((u) & 0x00000001U) // mask all but lowest bit of u -#define loBits(u) ((u) & 0x7FFFFFFFU) // mask the highest bit of u -#define mixBits(u, v) (hiBit(u)|loBits(v)) // move hi bit of u to hi bit of v - -static uint32 _mt_state[N+1]; // _mt_state vector + 1 extra to not violate ANSI C -static uint32 *_mt_next; // _mt_next random value is computed from here -static int _mt_left = -1; // can *_mt_next++ this many times before reloading - -void SeedMT(uint32 seed) -{ - register uint32 x = (seed | 1U) & 0xFFFFFFFFU, *s = _mt_state; - register int j; - - for (_mt_left=0, *s++=x, j=N; --j; - *s++ = (x*=69069U) & 0xFFFFFFFFU); - } - - -static uint32 ReloadMT() - { - register uint32 *p0=_mt_state, *p2=_mt_state+2, *pM=_mt_state+M, s0, s1; - register int j; - - if (_mt_left < -1) - SeedMT(4357U); - - _mt_left=N-1, _mt_next=_mt_state+1; - - for (s0=_mt_state[0], s1=_mt_state[1], j=N-M+1; --j; s0=s1, s1=*p2++) - *p0++ = *pM++ ^ (mixBits(s0, s1) >> 1) ^ (loBit(s1) ? K : 0U); - - for (pM=_mt_state, j=M; --j; s0=s1, s1=*p2++) - *p0++ = *pM++ ^ (mixBits(s0, s1) >> 1) ^ (loBit(s1) ? K : 0U); - - s1=_mt_state[0], *p0 = *pM ^ (mixBits(s0, s1) >> 1) ^ (loBit(s1) ? K : 0U); - s1 ^= (s1 >> 11); - s1 ^= (s1 << 7) & 0x9D2C5680U; - s1 ^= (s1 << 15) & 0xEFC60000U; - return(s1 ^ (s1 >> 18)); - } - - -uint32 RandomMT() -{ - uint32 y; - - if (--_mt_left < 0) - return ReloadMT(); - - y = *_mt_next++; - y ^= (y >> 11); - y ^= (y << 7) & 0x9D2C5680U; - y ^= (y << 15) & 0xEFC60000U; - return y ^ (y >> 18); -} -#else - -void SeedMT(uint32 seed) {} - -#endif /* MERSENNE_TWISTER */ diff --git a/src/misc.cpp b/src/misc.cpp index 10fd76ecf..52cd764e8 100644 --- a/src/misc.cpp +++ b/src/misc.cpp @@ -27,57 +27,6 @@ char _name_array[512][32]; -#ifndef MERSENNE_TWISTER - -#ifdef RANDOM_DEBUG -#include "network/network_data.h" -uint32 DoRandom(int line, const char *file) -#else // RANDOM_DEBUG -uint32 Random() -#endif // RANDOM_DEBUG -{ - -uint32 s; -uint32 t; - -#ifdef RANDOM_DEBUG - if (_networking && (DEREF_CLIENT(0)->status != STATUS_INACTIVE || !_network_server)) - printf("Random [%d/%d] %s:%d\n",_frame_counter, (byte)_current_player, file, line); -#endif - - s = _random_seeds[0][0]; - t = _random_seeds[0][1]; - _random_seeds[0][0] = s + ROR(t ^ 0x1234567F, 7) + 1; - return _random_seeds[0][1] = ROR(s, 3) - 1; -} -#endif // MERSENNE_TWISTER - -#if defined(RANDOM_DEBUG) && !defined(MERSENNE_TWISTER) -uint DoRandomRange(uint max, int line, const char *file) -{ - return GB(DoRandom(line, file), 0, 16) * max >> 16; -} -#else -uint RandomRange(uint max) -{ - return GB(Random(), 0, 16) * max >> 16; -} -#endif - - -uint32 InteractiveRandom() -{ - uint32 t = _random_seeds[1][1]; - uint32 s = _random_seeds[1][0]; - _random_seeds[1][0] = s + ROR(t ^ 0x1234567F, 7) + 1; - return _random_seeds[1][1] = ROR(s, 3) - 1; -} - -uint InteractiveRandomRange(uint max) -{ - return GB(InteractiveRandom(), 0, 16) * max >> 16; -} - void InitializeVehicles(); void InitializeWaypoints(); void InitializeDepots(); diff --git a/src/os2.cpp b/src/os2.cpp index 7f2626587..7ffccf09a 100644 --- a/src/os2.cpp +++ b/src/os2.cpp @@ -163,7 +163,7 @@ void ShowOSErrorBox(const char *buf) int CDECL main(int argc, char* argv[]) { - _random_seeds[1][1] = _random_seeds[1][0] = _random_seeds[0][1] = _random_seeds[0][0] = time(NULL); + SetRandomSeed(time(NULL)); return ttd_main(argc, argv); } diff --git a/src/unix.cpp b/src/unix.cpp index 3933352e6..aa9a361eb 100644 --- a/src/unix.cpp +++ b/src/unix.cpp @@ -136,8 +136,7 @@ int CDECL main(int argc, char* argv[]) } #endif - _random_seeds[1][1] = _random_seeds[1][0] = _random_seeds[0][1] = _random_seeds[0][0] = time(NULL); - SeedMT(_random_seeds[0][1]); + SetRandomSeed(time(NULL)); signal(SIGPIPE, SIG_IGN); diff --git a/src/win32.cpp b/src/win32.cpp index c0479f64a..e0298a0f8 100644 --- a/src/win32.cpp +++ b/src/win32.cpp @@ -971,9 +971,7 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLi #endif /* setup random seed to something quite random */ - _random_seeds[1][0] = _random_seeds[0][0] = GetTickCount(); - _random_seeds[1][1] = _random_seeds[0][1] = _random_seeds[0][0] * 0x1234567; - SeedMT(_random_seeds[0][0]); + SetRandomSeed(GetTickCount()); argc = ParseCommandLine(cmdline, argv, lengthof(argv)); -- cgit v1.2.3-70-g09d2