OpenTTD: src/core/random_func.cpp Source File

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

random_func.cpp
Go to the documentation of this file.
1 /* $Id: random_func.cpp 27049 2014-10-28 11:32:19Z peter1138 $ */
2
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 */
9
12 #include "../stdafx.h"
13 #include "random_func.hpp"
14 #include "bitmath_func.hpp"
15
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 */
24
25 #include "../safeguards.h"
26
27 Randomizer _random, _interactive_random;
28
33 uint32 Randomizer::Next()
34 {
35 const uint32 s = this->state[0];
36 const uint32 t = this->state[1];
37
38 this->state[0] = s + ROR(t ^ 0x1234567F, 7) + 1;
39 return this->state[1] = ROR(s, 3) - 1;
40 }
41
48 uint32 Randomizer::Next(uint32 limit)
49 {
50 return ((uint64)this->Next() * (uint64)limit) >> 32;
51 }
52
57 void Randomizer::SetSeed(uint32 seed)
58 {
59 this->state[0] = seed;
60 this->state[1] = seed;
61 }
62
67 void SetRandomSeed(uint32 seed)
68 {
69 _random.SetSeed(seed);
70 _interactive_random.SetSeed(seed * 0x1234567);
71 }
72
73 #ifdef RANDOM_DEBUG
74 uint32 DoRandom(int line, const char *file)
75 {
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);
78 }
79
80 return _random.Next();
81 }
82
83 uint32 DoRandomRange(uint32 limit, int line, const char *file)
84 {
85 return ((uint64)DoRandom(line, file) * (uint64)limit) >> 32;
86 }
87 #endif /* RANDOM_DEBUG */