25#elif defined(__APPLE__) || defined(__NetBSD__) || defined(__FreeBSD__)
27#elif defined(__GLIBC__) && ((__GLIBC__ > 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 25)))
28# include <sys/random.h>
29#elif defined(__EMSCRIPTEN__)
30# include <emscripten.h>
43 const uint32_t s = this->
state[0];
44 const uint32_t t = this->
state[1];
46 this->
state[0] = s + std::rotr(t ^ 0x1234567F, 7) + 1;
47 return this->
state[1] = std::rotr(s, 3) - 1;
56 this->
state[0] = seed;
57 this->
state[1] = seed;
71uint32_t Random(
const std::source_location location)
73 if (
_networking && (!
_network_server || (NetworkClientSocket::IsValidID(0) && NetworkClientSocket::Get(0)->status != NetworkClientSocket::ClientStatus::Inactive))) {
96 auto res = BCryptGenRandom(
nullptr,
static_cast<PUCHAR
>(buf.data()),
static_cast<ULONG
>(buf.size()), BCRYPT_USE_SYSTEM_PREFERRED_RNG);
98#elif defined(__APPLE__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
99 arc4random_buf(buf.data(), buf.size());
101#elif defined(__GLIBC__) && ((__GLIBC__ > 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 25)))
102 auto res = getrandom(buf.data(), buf.size(), 0);
103 if (res > 0 &&
static_cast<size_t>(res) == buf.size())
return;
104#elif defined(__EMSCRIPTEN__)
105 auto res = EM_ASM_INT({
109 var crypto = window.crypto;
110 if (crypto === undefined || crypto.getRandomValues === undefined) {
114 crypto.getRandomValues(Module.HEAPU8.subarray(buf, buf + bytes));
116 }, buf.data(), buf.size());
119# warning "No cryptographically-strong random generator available; using a fallback instead"
123 Debug(
Facility::Misc, severity,
"Cryptographically-strong random generator unavailable; using fallback");
126 for (uint i = 0; i < buf.size(); i++) {
127 buf[i] =
static_cast<uint8_t
>(InteractiveRandom());
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.
Functions related to companies.
Functions related to debugging.
#define Debug(facility, severity, format_string,...)
Output a line of debugging information.
@ Random
Random message facility.
@ Misc
Misc message facility.
Severity
Debug message severity levels.
@ Critical
Critical, user should know about this.
@ Error
Error, but we are recovering.
uint32_t _frame_counter
The current frame.
bool _networking
are we in networking mode?
bool _network_server
network-server is active
Basic functions/variables used all over the place.
Variables and function used internally.
Server part of the network protocol.
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.
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.
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.
Definition of the game-economy-timer.