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
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void Randomizer::SetSeed(uint32 seed)
|
|
|
|
{
|
|
|
|
this->state[0] = seed;
|
|
|
|
this->state[1] = seed;
|
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"
|
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)
|
|
|
|
{
|
2009-05-22 15:13:50 +00:00
|
|
|
if (_networking && (!_network_server || (NetworkClientSocket::IsValidID(0) && NetworkClientSocket::Get(0)->status != 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 */
|