OpenTTD
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 
28 
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 */
bool _networking
are we in networking mode?
Definition: network.cpp:56
void SetRandomSeed(uint32 seed)
(Re)set the state of the random number generators.
Definition: random_func.cpp:67
Functions related to bit mathematics.
Pseudo random number generator.
DateFract _date_fract
Fractional part of the day.
Definition: date.cpp:29
Randomizer _interactive_random
Random used every else where is does not (directly) influence the game state.
Definition: random_func.cpp:27
void SetSeed(uint32 seed)
(Re)set the state of the random number generator.
Definition: random_func.cpp:57
static T ROR(const T x, const uint8 n)
ROtate x Right by n.
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:39
uint32 _frame_counter
The current frame.
Definition: network.cpp:72
CompanyByte _current_company
Company currently doing an action.
Definition: company_cmd.cpp:47
Randomizer _random
Random used in the game state calculations.
Definition: random_func.cpp:27
uint32 Next()
Generate the next pseudo random number.
Definition: random_func.cpp:33
bool _network_server
network-server is active
Definition: network.cpp:57
uint32 state[2]
The state of the randomizer.
Definition: random_func.hpp:25
Structure to encapsulate the pseudo random number generators.
Definition: random_func.hpp:23
Date _date
Current date in days (day counter)
Definition: date.cpp:28