10 #include "../stdafx.h"
16 #include "../network/network.h"
17 #include "../network/network_server.h"
18 #include "../network/network_internal.h"
19 #include "../company_func.h"
20 #include "../fileio_func.h"
21 #include "../timer/timer_game_calendar.h"
27 #elif defined(__APPLE__) || defined(__NetBSD__) || defined(__FreeBSD__)
29 #elif defined(__GLIBC__) && ((__GLIBC__ > 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 25)))
30 # include <sys/random.h>
31 #elif defined(__EMSCRIPTEN__)
32 # include <emscripten.h>
35 #include "../safeguards.h"
45 const uint32_t s = this->
state[0];
46 const uint32_t t = this->
state[1];
48 this->
state[0] = s + std::rotr(t ^ 0x1234567F, 7) + 1;
49 return this->
state[1] = std::rotr(s, 3) - 1;
58 this->
state[0] = seed;
59 this->
state[1] = seed;
73 uint32_t Random(
const std::source_location location)
75 if (
_networking && (!
_network_server || (NetworkClientSocket::IsValidID(0) && NetworkClientSocket::Get(0)->status != NetworkClientSocket::STATUS_INACTIVE))) {
98 auto res = BCryptGenRandom(
nullptr,
static_cast<PUCHAR
>(buf.data()),
static_cast<ULONG
>(buf.size()), BCRYPT_USE_SYSTEM_PREFERRED_RNG);
100 #elif defined(__APPLE__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
101 arc4random_buf(buf.data(), buf.size());
103 #elif defined(__GLIBC__) && ((__GLIBC__ > 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 25)))
104 auto res = getrandom(buf.data(), buf.size(), 0);
105 if (res > 0 &&
static_cast<size_t>(res) == buf.size())
return;
106 #elif defined(__EMSCRIPTEN__)
107 auto res = EM_ASM_INT({
111 var crypto = window.crypto;
112 if (crypto === undefined || crypto.getRandomValues === undefined) {
116 crypto.getRandomValues(Module.HEAPU8.subarray(buf, buf + bytes));
118 }, buf.data(), buf.size());
121 # warning "No cryptographically-strong random generator available; using a fallback instead"
124 static bool warned_once =
false;
125 Debug(misc, warned_once ? 1 : 0,
"Cryptographically-strong random generator unavailable; using fallback");
128 for (uint i = 0; i < buf.size(); i++) {
129 buf[i] =
static_cast<uint8_t
>(InteractiveRandom());
Functions related to bit mathematics.
static Date date
Current date in days (day counter).
static DateFract date_fract
Fractional part of the day.
CompanyID _current_company
Company currently doing an action.
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
uint32_t _frame_counter
The current frame.
bool _networking
are we in networking mode?
bool _network_server
network-server is active
Randomizer _random
Random used in the game state calculations.
void RandomBytesWithFallback(std::span< uint8_t > buf)
Fill the given buffer with random bytes.
Randomizer _interactive_random
Random used everywhere else, where it does not (directly) influence the game state.
void SetRandomSeed(uint32_t seed)
(Re)set the state of the random number generators.
Pseudo random number generator.
Structure to encapsulate the pseudo random number generators.
void SetSeed(uint32_t seed)
(Re)set the state of the random number generator.
uint32_t state[2]
The state of the randomizer.
uint32_t Next()
Generate the next pseudo random number.