Rework the way the ReinitSeparation command is called. The old way was way too danger...
[openttd-joker.git] / src / core / random_func.cpp
blob73be5d83bb493b86a3a34d78f63c0fb468d6459e
1 /* $Id: random_func.cpp 25893 2013-10-20 14:48:08Z fonsinchen $ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
10 /** @file random_func.cpp Implementation of the pseudo random generator. */
12 #include "../stdafx.h"
13 #include "random_func.hpp"
14 #include "bitmath_func.hpp"
16 #ifdef RANDOM_DEBUG
17 #include "../network/network.h"
18 #include "../network/network_server.h"
19 #include "../network/network_internal.h"
20 #include "../company_func.h"
21 #include "../fileio_func.h"
22 #include "../date_func.h"
23 #endif /* RANDOM_DEBUG */
25 #include "../safeguards.h"
27 Randomizer _random, _interactive_random;
29 /**
30 * Generate the next pseudo random number
31 * @return the random number
33 uint32 Randomizer::Next()
35 const uint32 s = this->state[0];
36 const uint32 t = this->state[1];
38 this->state[0] = s + ROR(t ^ 0x1234567F, 7) + 1;
39 return this->state[1] = ROR(s, 3) - 1;
42 /**
43 * Generate the next pseudo random number scaled to \a limit, excluding \a limit
44 * itself.
45 * @param limit Limit of the range to be generated from.
46 * @return Random number in [0,\a limit)
48 uint32 Randomizer::Next(uint32 limit)
50 return ((uint64)this->Next() * (uint64)limit) >> 32;
53 /**
54 * (Re)set the state of the random number generator.
55 * @param seed the new state
57 void Randomizer::SetSeed(uint32 seed)
59 this->state[0] = seed;
60 this->state[1] = seed;
63 /**
64 * (Re)set the state of the random number generators.
65 * @param seed the new state
67 void SetRandomSeed(uint32 seed)
69 _random.SetSeed(seed);
70 _interactive_random.SetSeed(seed * 0x1234567);
73 #ifdef RANDOM_DEBUG
74 uint32 DoRandom(int line, const char *file)
76 if (_networking && (!_network_server || (NetworkClientSocket::IsValidID(0) && NetworkClientSocket::Get(0)->status != NetworkClientSocket::STATUS_INACTIVE))) {
77 DEBUG(random, 0, "%08x; %02x; %04x; %02x; %s:%d", _date, _date_fract, _frame_counter, (byte)_current_company, file, line);
80 return _random.Next();
83 uint32 DoRandomRange(uint32 limit, int line, const char *file)
85 return ((uint64)DoRandom(line, file) * (uint64)limit) >> 32;
87 #endif /* RANDOM_DEBUG */