2007-11-21 19:13:38 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file random_func.cpp Implementation of the pseudo random generator. */
|
2007-11-21 19:13:38 +00:00
|
|
|
|
|
|
|
#include "../stdafx.h"
|
|
|
|
#include "random_func.hpp"
|
2007-12-21 19:21:21 +00:00
|
|
|
#include "bitmath_func.hpp"
|
2007-11-21 19:13:38 +00:00
|
|
|
|
2008-01-29 00:27:25 +00:00
|
|
|
Randomizer _random, _interactive_random;
|
2007-12-26 23:04:26 +00:00
|
|
|
|
2011-01-22 09:53:15 +00:00
|
|
|
/**
|
|
|
|
* Generate the next pseudo random number
|
|
|
|
* @return the random number
|
|
|
|
*/
|
2008-01-29 00:27:25 +00:00
|
|
|
uint32 Randomizer::Next()
|
2007-11-21 19:13:38 +00:00
|
|
|
{
|
2008-01-29 00:27:25 +00:00
|
|
|
const uint32 s = this->state[0];
|
|
|
|
const uint32 t = this->state[1];
|
2007-11-21 19:13:38 +00:00
|
|
|
|
2008-01-29 00:27:25 +00:00
|
|
|
this->state[0] = s + ROR(t ^ 0x1234567F, 7) + 1;
|
|
|
|
return this->state[1] = ROR(s, 3) - 1;
|
2007-11-21 19:13:38 +00:00
|
|
|
}
|
|
|
|
|
2011-01-22 09:53:15 +00:00
|
|
|
/**
|
|
|
|
* Generate the next pseudo random number scaled to max
|
|
|
|
* @param max the maximum value of the returned random number
|
|
|
|
* @return the random number
|
|
|
|
*/
|
2010-04-17 11:49:25 +00:00
|
|
|
uint32 Randomizer::Next(uint32 max)
|
2007-11-21 19:13:38 +00:00
|
|
|
{
|
2010-04-17 11:49:25 +00:00
|
|
|
return ((uint64)this->Next() * (uint64)max) >> 32;
|
2008-01-29 00:27:25 +00:00
|
|
|
}
|
|
|
|
|
2011-01-22 09:53:15 +00:00
|
|
|
/**
|
|
|
|
* (Re)set the state of the random number generator.
|
|
|
|
* @param seed the new state
|
|
|
|
*/
|
2008-01-29 00:27:25 +00:00
|
|
|
void Randomizer::SetSeed(uint32 seed)
|
|
|
|
{
|
|
|
|
this->state[0] = seed;
|
|
|
|
this->state[1] = seed;
|
2007-11-21 19:13:38 +00:00
|
|
|
}
|
|
|
|
|
2011-01-22 09:53:15 +00:00
|
|
|
/**
|
|
|
|
* (Re)set the state of the random number generators.
|
|
|
|
* @param seed the new state
|
|
|
|
*/
|
2007-11-21 19:13:38 +00:00
|
|
|
void SetRandomSeed(uint32 seed)
|
|
|
|
{
|
2008-01-29 00:27:25 +00:00
|
|
|
_random.SetSeed(seed);
|
|
|
|
_interactive_random.SetSeed(seed * 0x1234567);
|
2007-11-21 19:13:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef RANDOM_DEBUG
|
2010-02-13 20:13:34 +00:00
|
|
|
#include "../network/network.h"
|
2010-11-27 12:55:43 +00:00
|
|
|
#include "../network/network_server.h"
|
2008-10-18 14:16:29 +00:00
|
|
|
#include "../network/network_internal.h"
|
2008-09-30 20:51:04 +00:00
|
|
|
#include "../company_func.h"
|
2008-02-03 16:21:19 +00:00
|
|
|
|
2007-11-21 19:13:38 +00:00
|
|
|
uint32 DoRandom(int line, const char *file)
|
|
|
|
{
|
2010-11-27 12:55:43 +00:00
|
|
|
if (_networking && (!_network_server || (NetworkClientSocket::IsValidID(0) && NetworkClientSocket::Get(0)->status != NetworkClientSocket::STATUS_INACTIVE))) {
|
2008-12-28 22:23:43 +00:00
|
|
|
printf("Random [%d/%d] %s:%d\n", _frame_counter, (byte)_current_company, file, line);
|
2008-01-29 00:27:25 +00:00
|
|
|
}
|
2007-11-21 19:13:38 +00:00
|
|
|
|
2008-02-03 16:21:19 +00:00
|
|
|
return _random.Next();
|
2007-11-21 19:13:38 +00:00
|
|
|
}
|
|
|
|
|
2010-04-17 11:49:25 +00:00
|
|
|
uint32 DoRandomRange(uint32 max, int line, const char *file)
|
2007-11-21 19:13:38 +00:00
|
|
|
{
|
2010-04-17 11:49:25 +00:00
|
|
|
return ((uint64)DoRandom(line, file) * (uint64)max) >> 32;
|
2007-11-21 19:13:38 +00:00
|
|
|
}
|
2008-03-31 16:07:50 +00:00
|
|
|
#endif /* RANDOM_DEBUG */
|