2007-11-21 20:13:38 +01:00
|
|
|
/* $Id$ */
|
|
|
|
|
2008-05-06 17:11:33 +02:00
|
|
|
/** @file random_func.cpp Implementation of the pseudo random generator. */
|
2007-11-21 20:13:38 +01:00
|
|
|
|
|
|
|
#include "../stdafx.h"
|
|
|
|
#include "random_func.hpp"
|
2007-12-21 20:21:21 +01:00
|
|
|
#include "bitmath_func.hpp"
|
2007-11-21 20:13:38 +01:00
|
|
|
|
2008-01-29 01:27:25 +01:00
|
|
|
Randomizer _random, _interactive_random;
|
2007-12-27 00:04:26 +01:00
|
|
|
|
2008-01-29 01:27:25 +01:00
|
|
|
uint32 Randomizer::Next()
|
2007-11-21 20:13:38 +01:00
|
|
|
{
|
2008-01-29 01:27:25 +01:00
|
|
|
const uint32 s = this->state[0];
|
|
|
|
const uint32 t = this->state[1];
|
2007-11-21 20:13:38 +01:00
|
|
|
|
2008-01-29 01:27:25 +01:00
|
|
|
this->state[0] = s + ROR(t ^ 0x1234567F, 7) + 1;
|
|
|
|
return this->state[1] = ROR(s, 3) - 1;
|
2007-11-21 20:13:38 +01:00
|
|
|
}
|
|
|
|
|
2008-01-29 01:27:25 +01:00
|
|
|
uint32 Randomizer::Next(uint16 max)
|
2007-11-21 20:13:38 +01:00
|
|
|
{
|
2008-01-29 01:27:25 +01:00
|
|
|
return GB(this->Next(), 0, 16) * max >> 16;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Randomizer::SetSeed(uint32 seed)
|
|
|
|
{
|
|
|
|
this->state[0] = seed;
|
|
|
|
this->state[1] = seed;
|
2007-11-21 20:13:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetRandomSeed(uint32 seed)
|
|
|
|
{
|
2008-01-29 01:27:25 +01:00
|
|
|
_random.SetSeed(seed);
|
|
|
|
_interactive_random.SetSeed(seed * 0x1234567);
|
2007-11-21 20:13:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef RANDOM_DEBUG
|
2008-10-18 16:16:29 +02:00
|
|
|
#include "../network/network_internal.h"
|
2008-09-30 22:51:04 +02:00
|
|
|
#include "../company_func.h"
|
2008-02-03 17:21:19 +01:00
|
|
|
|
2007-11-21 20:13:38 +01:00
|
|
|
uint32 DoRandom(int line, const char *file)
|
|
|
|
{
|
2009-05-22 17:13:50 +02:00
|
|
|
if (_networking && (!_network_server || (NetworkClientSocket::IsValidID(0) && NetworkClientSocket::Get(0)->status != STATUS_INACTIVE))) {
|
2008-12-28 23:23:43 +01:00
|
|
|
printf("Random [%d/%d] %s:%d\n", _frame_counter, (byte)_current_company, file, line);
|
2008-01-29 01:27:25 +01:00
|
|
|
}
|
2007-11-21 20:13:38 +01:00
|
|
|
|
2008-02-03 17:21:19 +01:00
|
|
|
return _random.Next();
|
2007-11-21 20:13:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
uint DoRandomRange(uint max, int line, const char *file)
|
|
|
|
{
|
|
|
|
return GB(DoRandom(line, file), 0, 16) * max >> 16;
|
|
|
|
}
|
2008-03-31 18:07:50 +02:00
|
|
|
#endif /* RANDOM_DEBUG */
|