Fix: Don't allow right-click to close world generation progress window. (#13084)
[openttd-github.git] / src / core / random_func.cpp
blob355eeff6163f8b43bc1eb82077b095537278116d
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 */
8 /** @file random_func.cpp Implementation of the pseudo random generator. */
10 #include "../stdafx.h"
11 #include "random_func.hpp"
12 #include "bitmath_func.hpp"
13 #include "../debug.h"
15 #ifdef RANDOM_DEBUG
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"
22 #endif /* RANDOM_DEBUG */
24 #if defined(_WIN32)
25 # include <windows.h>
26 # include <bcrypt.h>
27 #elif defined(__APPLE__) || defined(__NetBSD__) || defined(__FreeBSD__)
28 // No includes required.
29 #elif defined(__GLIBC__) && ((__GLIBC__ > 2) || ((__GLIBC__ == 2) && (__GLIBC_MINOR__ >= 25)))
30 # include <sys/random.h>
31 #elif defined(__EMSCRIPTEN__)
32 # include <emscripten.h>
33 #endif
35 #include "../safeguards.h"
37 Randomizer _random, _interactive_random;
39 /**
40 * Generate the next pseudo random number
41 * @return the random number
43 uint32_t Randomizer::Next()
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;
52 /**
53 * (Re)set the state of the random number generator.
54 * @param seed the new state
56 void Randomizer::SetSeed(uint32_t seed)
58 this->state[0] = seed;
59 this->state[1] = seed;
62 /**
63 * (Re)set the state of the random number generators.
64 * @param seed the new state
66 void SetRandomSeed(uint32_t seed)
68 _random.SetSeed(seed);
69 _interactive_random.SetSeed(seed * 0x1234567);
72 #ifdef RANDOM_DEBUG
73 uint32_t Random(const std::source_location location)
75 if (_networking && (!_network_server || (NetworkClientSocket::IsValidID(0) && NetworkClientSocket::Get(0)->status != NetworkClientSocket::STATUS_INACTIVE))) {
76 Debug(random, 0, "{:08x}; {:02x}; {:04x}; {:02x}; {}:{}", TimerGameEconomy::date, TimerGameEconomy::date_fract, _frame_counter, (uint8_t)_current_company, location.file_name(), location.line());
79 return _random.Next();
81 #endif /* RANDOM_DEBUG */
83 /**
84 * Fill the given buffer with random bytes.
86 * This function will attempt to use a cryptographically-strong random
87 * generator, but will fall back to a weaker random generator if none is
88 * available.
90 * In the end, the buffer will always be filled with some form of random
91 * bytes when this function returns.
93 * @param buf The buffer to fill with random bytes.
95 void RandomBytesWithFallback(std::span<uint8_t> buf)
97 #if defined(_WIN32)
98 auto res = BCryptGenRandom(nullptr, static_cast<PUCHAR>(buf.data()), static_cast<ULONG>(buf.size()), BCRYPT_USE_SYSTEM_PREFERRED_RNG);
99 if (res >= 0) return;
100 #elif defined(__APPLE__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
101 arc4random_buf(buf.data(), buf.size());
102 return;
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({
108 var buf = $0;
109 var bytes = $1;
111 var crypto = window.crypto;
112 if (crypto === undefined || crypto.getRandomValues === undefined) {
113 return -1;
116 crypto.getRandomValues(Module.HEAPU8.subarray(buf, buf + bytes));
117 return 1;
118 }, buf.data(), buf.size());
119 if (res > 0) return;
120 #else
121 # warning "No cryptographically-strong random generator available; using a fallback instead"
122 #endif
124 static bool warned_once = false;
125 Debug(misc, warned_once ? 1 : 0, "Cryptographically-strong random generator unavailable; using fallback");
126 warned_once = true;
128 for (uint i = 0; i < buf.size(); i++) {
129 buf[i] = static_cast<uint8_t>(InteractiveRandom());