OpenTTD Source: src/core/random_func.cpp Source File

OpenTTD Source 13.2.1

random_func.cpp
Go to the documentation of this file.
1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
7
10 #include "../stdafx.h"
11 #include "random_func.hpp"
12 #include "bitmath_func.hpp"
13
14 #ifdef RANDOM_DEBUG
15 #include "../network/network.h"
16 #include "../network/network_server.h"
17 #include "../network/network_internal.h"
18 #include "../company_func.h"
19 #include "../fileio_func.h"
20 #include "../date_func.h"
21 #endif /* RANDOM_DEBUG */
22
23 #include "../safeguards.h"
24
25 Randomizer _random, _interactive_random;
26
31 uint32 Randomizer::Next()
32 {
33 const uint32 s = this->state[0];
34 const uint32 t = this->state[1];
35
36 this->state[0] = s + ROR(t ^ 0x1234567F, 7) + 1;
37 return this->state[1] = ROR(s, 3) - 1;
38 }
39
46 uint32 Randomizer::Next(uint32 limit)
47 {
48 return ((uint64)this->Next() * (uint64)limit) >> 32;
49 }
50
55 void Randomizer::SetSeed(uint32 seed)
56 {
57 this->state[0] = seed;
58 this->state[1] = seed;
59 }
60
65 void SetRandomSeed(uint32 seed)
66 {
67 _random.SetSeed(seed);
68 _interactive_random.SetSeed(seed * 0x1234567);
69 }
70
71 #ifdef RANDOM_DEBUG
72 uint32 DoRandom(int line, const char *file)
73 {
74 if (_networking && (!_network_server || (NetworkClientSocket::IsValidID(0) && NetworkClientSocket::Get(0)->status != NetworkClientSocket::STATUS_INACTIVE))) {
75 Debug(random, 0, "{:08x}; {:02x}; {:04x}; {:02x}; {}:{}", _date, _date_fract, _frame_counter, (byte)_current_company, file, line);
76 }
77
78 return _random.Next();
79 }
80
81 uint32 DoRandomRange(uint32 limit, int line, const char *file)
82 {
83 return ((uint64)DoRandom(line, file) * (uint64)limit) >> 32;
84 }
85 #endif /* RANDOM_DEBUG */
_frame_counter
uint32 _frame_counter
The current frame.
Definition: network.cpp:72
Randomizer::Next
uint32 Next()
Generate the next pseudo random number.
Definition: random_func.cpp:31
_date_fract
DateFract _date_fract
Fractional part of the day.
Definition: date.cpp:29
_network_server
bool _network_server
network-server is active
Definition: network.cpp:59
_random
Randomizer _random
Random used in the game state calculations.
Definition: random_func.cpp:25
_interactive_random
Randomizer _interactive_random
Random used everywhere else, where it does not (directly) influence the game state.
Definition: random_func.cpp:25
bitmath_func.hpp
_date
Date _date
Current date in days (day counter)
Definition: date.cpp:28
_networking
bool _networking
are we in networking mode?
Definition: network.cpp:58
Randomizer::state
uint32 state[2]
The state of the randomizer.
Definition: random_func.hpp:23
_current_company
CompanyID _current_company
Company currently doing an action.
Definition: company_cmd.cpp:47
SetRandomSeed
void SetRandomSeed(uint32 seed)
(Re)set the state of the random number generators.
Definition: random_func.cpp:65
Randomizer
Structure to encapsulate the pseudo random number generators.
Definition: random_func.hpp:21
Debug
#define Debug(name, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
random_func.hpp
Randomizer::SetSeed
void SetSeed(uint32 seed)
(Re)set the state of the random number generator.
Definition: random_func.cpp:55
ROR
static T ROR(const T x, const uint8 n)
ROtate x Right by n.
Definition: bitmath_func.hpp:317