random_func.cpp

Go to the documentation of this file.
00001 /* $Id: random_func.cpp 14910 2009-01-08 13:57:50Z rubidium $ */
00002 
00005 #include "../stdafx.h"
00006 #include "random_func.hpp"
00007 #include "bitmath_func.hpp"
00008 
00009 Randomizer _random, _interactive_random;
00010 
00011 uint32 Randomizer::Next()
00012 {
00013   const uint32 s = this->state[0];
00014   const uint32 t = this->state[1];
00015 
00016   this->state[0] = s + ROR(t ^ 0x1234567F, 7) + 1;
00017   return this->state[1] = ROR(s, 3) - 1;
00018 }
00019 
00020 uint32 Randomizer::Next(uint16 max)
00021 {
00022   return GB(this->Next(), 0, 16) * max >> 16;
00023 }
00024 
00025 void Randomizer::SetSeed(uint32 seed)
00026 {
00027   this->state[0] = seed;
00028   this->state[1] = seed;
00029 }
00030 
00031 void SetRandomSeed(uint32 seed)
00032 {
00033   _random.SetSeed(seed);
00034   _interactive_random.SetSeed(seed * 0x1234567);
00035 }
00036 
00037 #ifdef RANDOM_DEBUG
00038 #include "../network/network_internal.h"
00039 #include "../company_func.h"
00040 
00041 uint32 DoRandom(int line, const char *file)
00042 {
00043   if (_networking && (GetNetworkClientSocket(0)->status != STATUS_INACTIVE || !_network_server)) {
00044     printf("Random [%d/%d] %s:%d\n", _frame_counter, (byte)_current_company, file, line);
00045   }
00046 
00047   return _random.Next();
00048 }
00049 
00050 uint DoRandomRange(uint max, int line, const char *file)
00051 {
00052   return GB(DoRandom(line, file), 0, 16) * max >> 16;
00053 }
00054 #endif /* RANDOM_DEBUG */

Generated on Sun Mar 15 22:49:45 2009 for openttd by  doxygen 1.5.6