OpenTTD: src/core/random_func.hpp Source File

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

random_func.hpp
Go to the documentation of this file.
1 /* $Id: random_func.hpp 25893 2013-10-20 14:48:08Z fonsinchen $ */
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 #ifndef RANDOM_FUNC_HPP
13 #define RANDOM_FUNC_HPP
14
15 #if defined(__APPLE__)
16 /* Apple already has Random declared */
17 #define Random OTTD_Random
18 #endif /* __APPLE__ */
19
23 struct Randomizer {
25 uint32 state[2];
26
27 uint32 Next();
28 uint32 Next(uint32 limit);
29 void SetSeed(uint32 seed);
30 };
31 extern Randomizer _random;
32 extern Randomizer _interactive_random;
33
35 struct SavedRandomSeeds {
36 Randomizer random;
37 Randomizer interactive_random;
38 };
39
44 static inline void SaveRandomSeeds(SavedRandomSeeds *storage)
45 {
46 storage->random = _random;
47 storage->interactive_random = _interactive_random;
48 }
49
54 static inline void RestoreRandomSeeds(const SavedRandomSeeds &storage)
55 {
56 _random = storage.random;
57 _interactive_random = storage.interactive_random;
58 }
59
60 void SetRandomSeed(uint32 seed);
61 #ifdef RANDOM_DEBUG
62 #ifdef __APPLE__
63 #define OTTD_Random() DoRandom(__LINE__, __FILE__)
64 #else
65 #define Random() DoRandom(__LINE__, __FILE__)
66 #endif
67 uint32 DoRandom(int line, const char *file);
68 #define RandomRange(limit) DoRandomRange(limit, __LINE__, __FILE__)
69 uint32 DoRandomRange(uint32 limit, int line, const char *file);
70 #else
71 static inline uint32 Random()
72 {
73 return _random.Next();
74 }
75
83 static inline uint32 RandomRange(uint32 limit)
84 {
85 return _random.Next(limit);
86 }
87 #endif
88
89 static inline uint32 InteractiveRandom()
90 {
91 return _interactive_random.Next();
92 }
93
94 static inline uint32 InteractiveRandomRange(uint32 limit)
95 {
96 return _interactive_random.Next(limit);
97 }
98
114 static inline bool Chance16I(const uint a, const uint b, const uint32 r)
115 {
116 assert(b != 0);
117 return (((uint16)r * b + b / 2) >> 16) < a;
118 }
119
130 #ifdef RANDOM_DEBUG
131 #define Chance16(a, b) Chance16I(a, b, DoRandom(__LINE__, __FILE__))
132 #else
133 static inline bool Chance16(const uint a, const uint b)
134 {
135 return Chance16I(a, b, Random());
136 }
137 #endif /* RANDOM_DEBUG */
138
154 #ifdef RANDOM_DEBUG
155 #define Chance16R(a, b, r) (r = DoRandom(__LINE__, __FILE__), Chance16I(a, b, r))
156 #else
157 static inline bool Chance16R(const uint a, const uint b, uint32 &r)
158 {
159 r = Random();
160 return Chance16I(a, b, r);
161 }
162 #endif /* RANDOM_DEBUG */
163
164 #endif /* RANDOM_FUNC_HPP */