Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #ifndef RANDOM_FUNC_HPP
00013 #define RANDOM_FUNC_HPP
00014
00015 #if defined(__APPLE__)
00016
00017 #define Random OTTD_Random
00018 #endif
00019
00023 struct Randomizer {
00025 uint32 state[2];
00026
00027 uint32 Next();
00028 uint32 Next(uint32 limit);
00029 void SetSeed(uint32 seed);
00030 };
00031 extern Randomizer _random;
00032 extern Randomizer _interactive_random;
00033
00035 struct SavedRandomSeeds {
00036 Randomizer random;
00037 Randomizer interactive_random;
00038 };
00039
00044 static inline void SaveRandomSeeds(SavedRandomSeeds *storage)
00045 {
00046 storage->random = _random;
00047 storage->interactive_random = _interactive_random;
00048 }
00049
00054 static inline void RestoreRandomSeeds(const SavedRandomSeeds &storage)
00055 {
00056 _random = storage.random;
00057 _interactive_random = storage.interactive_random;
00058 }
00059
00060 void SetRandomSeed(uint32 seed);
00061 #ifdef RANDOM_DEBUG
00062 #ifdef __APPLE__
00063 #define OTTD_Random() DoRandom(__LINE__, __FILE__)
00064 #else
00065 #define Random() DoRandom(__LINE__, __FILE__)
00066 #endif
00067 uint32 DoRandom(int line, const char *file);
00068 #define RandomRange(limit) DoRandomRange(limit, __LINE__, __FILE__)
00069 uint32 DoRandomRange(uint32 limit, int line, const char *file);
00070 #else
00071 static inline uint32 Random()
00072 {
00073 return _random.Next();
00074 }
00075
00083 static inline uint32 RandomRange(uint32 limit)
00084 {
00085 return _random.Next(limit);
00086 }
00087 #endif
00088
00089 static inline uint32 InteractiveRandom()
00090 {
00091 return _interactive_random.Next();
00092 }
00093
00094 static inline uint32 InteractiveRandomRange(uint32 limit)
00095 {
00096 return _interactive_random.Next(limit);
00097 }
00098
00114 static inline bool Chance16I(const uint a, const uint b, const uint32 r)
00115 {
00116 assert(b != 0);
00117 return (((uint16)r * b + b / 2) >> 16) < a;
00118 }
00119
00130 #ifdef RANDOM_DEBUG
00131 #define Chance16(a, b) Chance16I(a, b, DoRandom(__LINE__, __FILE__))
00132 #else
00133 static inline bool Chance16(const uint a, const uint b)
00134 {
00135 return Chance16I(a, b, Random());
00136 }
00137 #endif
00138
00154 #ifdef RANDOM_DEBUG
00155 #define Chance16R(a, b, r) (r = DoRandom(__LINE__, __FILE__), Chance16I(a, b, r))
00156 #else
00157 static inline bool Chance16R(const uint a, const uint b, uint32 &r)
00158 {
00159 r = Random();
00160 return Chance16I(a, b, r);
00161 }
00162 #endif
00163
00164 #endif