(svn r12004) -Codechange: refactor the random functions to reduce code duplication.
authorrubidium
Tue, 29 Jan 2008 00:27:25 +0000
changeset 8434 558c39956ba2
parent 8433 33899f3d6b5c
child 8435 6425e2ecd2a6
(svn r12004) -Codechange: refactor the random functions to reduce code duplication.
src/core/random_func.cpp
src/core/random_func.hpp
src/genworld.cpp
src/misc.cpp
src/network/network.cpp
src/oldloader.cpp
--- a/src/core/random_func.cpp	Tue Jan 29 00:26:31 2008 +0000
+++ b/src/core/random_func.cpp	Tue Jan 29 00:27:25 2008 +0000
@@ -6,20 +6,26 @@
 #include "random_func.hpp"
 #include "bitmath_func.hpp"
 
-uint32 _random_seeds[2][2];
+Randomizer _random, _interactive_random;
 
-uint32 InteractiveRandom()
+uint32 Randomizer::Next()
 {
-	const uint32 s = _random_seeds[1][0];
-	const uint32 t = _random_seeds[1][1];
+	const uint32 s = this->state[0];
+	const uint32 t = this->state[1];
 
-	_random_seeds[1][0] = s + ROR(t ^ 0x1234567F, 7) + 1;
-	return _random_seeds[1][1] = ROR(s, 3) - 1;
+	this->state[0] = s + ROR(t ^ 0x1234567F, 7) + 1;
+	return this->state[1] = ROR(s, 3) - 1;
 }
 
-uint InteractiveRandomRange(uint max)
+uint32 Randomizer::Next(uint16 max)
 {
-	return GB(InteractiveRandom(), 0, 16) * max >> 16;
+	return GB(this->Next(), 0, 16) * max >> 16;
+}
+
+void Randomizer::SetSeed(uint32 seed)
+{
+	this->state[0] = seed;
+	this->state[1] = seed;
 }
 
 #ifdef MERSENNE_TWISTER
@@ -119,28 +125,21 @@
 #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];
+	_random.SetSeed(seed);
+	_interactive_random.SetSeed(seed * 0x1234567);
 }
 
 #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))
+	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()
-{
+	}
+
+	return _random->Next()
+}
 #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)
@@ -148,9 +147,4 @@
 {
 	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 */
--- a/src/core/random_func.hpp	Tue Jan 29 00:26:31 2008 +0000
+++ b/src/core/random_func.hpp	Tue Jan 29 00:27:25 2008 +0000
@@ -27,6 +27,35 @@
 // Doesn't work with network yet.
 // #define MERSENNE_TWISTER
 
+/**
+ * Structure to encapsulate the pseudo random number generators.
+ */
+struct Randomizer {
+	/** The state of the randomizer */
+	uint32 state[2];
+
+	/**
+	 * Generate the next pseudo random number
+	 * @return the random number
+	 */
+	uint32 Next();
+
+	/**
+	 * Generate the next pseudo random number scaled to max
+	 * @param max the maximum value of the returned random number
+	 * @return the random number
+	 */
+	uint32 Next(uint16 max);
+
+	/**
+	 * (Re)set the state of the random number generator.
+	 * @param seed the new state
+	 */
+	void SetSeed(uint32 seed);
+};
+extern Randomizer _random; ///< Random used in the game state calculations
+extern Randomizer _interactive_random; ///< Random used every else where is does not (directly) influence the game state
+
 void SetRandomSeed(uint32 seed);
 #ifdef RANDOM_DEBUG
 	#define Random() DoRandom(__LINE__, __FILE__)
@@ -34,12 +63,12 @@
 	#define RandomRange(max) DoRandomRange(max, __LINE__, __FILE__)
 	uint DoRandomRange(uint max, int line, const char *file);
 #else
-	uint32 Random();
-	uint RandomRange(uint max);
+	static inline uint32 Random() { return _random.Next(); }
+	static inline uint32 RandomRange(uint16 max) { return _random.Next(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);
+static inline uint32 InteractiveRandom() { return _interactive_random.Next(); }
+static inline uint32 InteractiveRandomRange(uint16 max) { return _interactive_random.Next(max); }
 
 /**
  * Checks if a given randomize-number is below a given probability.
@@ -100,6 +129,4 @@
 	return Chance16I(a, b, r);
 }
 
-extern uint32 _random_seeds[2][2];
-
 #endif /* RANDOM_FUNC_HPP */
--- a/src/genworld.cpp	Tue Jan 29 00:26:31 2008 +0000
+++ b/src/genworld.cpp	Tue Jan 29 00:27:25 2008 +0000
@@ -90,7 +90,7 @@
 	if (_network_dedicated) DEBUG(net, 0, "Generating map, please wait...");
 	/* Set the Random() seed to generation_seed so we produce the same map with the same seed */
 	if (_patches.generation_seed == GENERATE_NEW_SEED) _patches.generation_seed = _patches_newgame.generation_seed = InteractiveRandom();
-	_random_seeds[0][0] = _random_seeds[0][1] = _patches.generation_seed;
+	_random.SetSeed(_patches.generation_seed);
 	SetGeneratingWorldProgress(GWP_MAP_INIT, 2);
 	SetObjectToPlace(SPR_CURSOR_ZZZ, PAL_NONE, VHM_NONE, WC_MAIN_WINDOW, 0);
 
--- a/src/misc.cpp	Tue Jan 29 00:26:31 2008 +0000
+++ b/src/misc.cpp	Tue Jan 29 00:27:25 2008 +0000
@@ -205,8 +205,8 @@
 	SLEG_CONDVAR(_cur_tileloop_tile,      SLE_UINT32,                  6, SL_MAX_VERSION),
 	    SLEG_VAR(_disaster_delay,         SLE_UINT16),
 	    SLEG_VAR(_station_tick_ctr,       SLE_UINT16),
-	    SLEG_VAR(_random_seeds[0][0],     SLE_UINT32),
-	    SLEG_VAR(_random_seeds[0][1],     SLE_UINT32),
+	    SLEG_VAR(_random.state[0],        SLE_UINT32),
+	    SLEG_VAR(_random.state[1],        SLE_UINT32),
 	SLEG_CONDVAR(_cur_town_ctr,           SLE_FILE_U8  | SLE_VAR_U32,  0, 9),
 	SLEG_CONDVAR(_cur_town_ctr,           SLE_UINT32,                 10, SL_MAX_VERSION),
 	    SLEG_VAR(_cur_player_tick_index,  SLE_FILE_U8  | SLE_VAR_U32),
--- a/src/network/network.cpp	Tue Jan 29 00:26:31 2008 +0000
+++ b/src/network/network.cpp	Tue Jan 29 00:27:25 2008 +0000
@@ -1235,9 +1235,9 @@
 	if (_sync_frame != 0) {
 		if (_sync_frame == _frame_counter) {
 #ifdef NETWORK_SEND_DOUBLE_SEED
-			if (_sync_seed_1 != _random_seeds[0][0] || _sync_seed_2 != _random_seeds[0][1]) {
+			if (_sync_seed_1 != _random.state[0] || _sync_seed_2 != _random.state[1]) {
 #else
-			if (_sync_seed_1 != _random_seeds[0][0]) {
+			if (_sync_seed_1 != _random.state[0]) {
 #endif
 				NetworkError(STR_NETWORK_ERR_DESYNC);
 				DebugDumpCommands("ddc:serr:%d;%d\n", _date, _date_fract);
@@ -1332,9 +1332,9 @@
 		// Then we make the frame
 		StateGameLoop();
 
-		_sync_seed_1 = _random_seeds[0][0];
+		_sync_seed_1 = _random.state[0];
 #ifdef NETWORK_SEND_DOUBLE_SEED
-		_sync_seed_2 = _random_seeds[0][1];
+		_sync_seed_2 = _random.state[1];
 #endif
 
 		NetworkServer_Tick(send_frame);
--- a/src/oldloader.cpp	Tue Jan 29 00:26:31 2008 +0000
+++ b/src/oldloader.cpp	Tue Jan 29 00:27:25 2008 +0000
@@ -1456,7 +1456,7 @@
 	OCL_VAR ( OC_FILE_U16 | OC_VAR_U32, 1, &_date ),
 	OCL_VAR ( OC_UINT16,   1, &_date_fract ),
 	OCL_NULL( 600 ),            ///< TextEffects
-	OCL_VAR ( OC_UINT32,   2, &_random_seeds[0] ),
+	OCL_VAR ( OC_UINT32,   2, &_random.state ),
 
 	OCL_ASSERT( 0x264 ),
 	OCL_CHUNK(  70, LoadOldTown ),