random_func.hpp

Go to the documentation of this file.
00001 /* $Id: random_func.hpp 20283 2010-08-01 19:22:34Z frosch $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * 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.
00006  * 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.
00007  * 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/>.
00008  */
00009 
00012 #ifndef RANDOM_FUNC_HPP
00013 #define RANDOM_FUNC_HPP
00014 
00015 #if defined(__APPLE__)
00016   /* Apple already has Random declared */
00017   #define Random OTTD_Random
00018 #endif /* __APPLE__ */
00019 
00020 /**************
00021  * Warning: DO NOT enable this unless you understand what it does
00022  *
00023  * If enabled, in a network game all randoms will be dumped to the
00024  *  stdout if the first client joins (or if you are a client). This
00025  *  is to help finding desync problems.
00026  *
00027  * Warning: DO NOT enable this unless you understand what it does
00028  **************/
00029 
00030 //#define RANDOM_DEBUG
00031 
00032 
00036 struct Randomizer {
00038   uint32 state[2];
00039 
00044   uint32 Next();
00045 
00051   uint32 Next(uint32 max);
00052 
00057   void SetSeed(uint32 seed);
00058 };
00059 extern Randomizer _random; 
00060 extern Randomizer _interactive_random; 
00061 
00063 struct SavedRandomSeeds {
00064   Randomizer random;
00065   Randomizer interactive_random;
00066 };
00067 
00072 static inline void SaveRandomSeeds(SavedRandomSeeds *storage)
00073 {
00074   storage->random = _random;
00075   storage->interactive_random = _interactive_random;
00076 }
00077 
00082 static inline void RestoreRandomSeeds(const SavedRandomSeeds &storage)
00083 {
00084   _random = storage.random;
00085   _interactive_random = storage.interactive_random;
00086 }
00087 
00088 void SetRandomSeed(uint32 seed);
00089 #ifdef RANDOM_DEBUG
00090   #ifdef __APPLE__
00091     #define OTTD_Random() DoRandom(__LINE__, __FILE__)
00092   #else
00093     #define Random() DoRandom(__LINE__, __FILE__)
00094   #endif
00095   uint32 DoRandom(int line, const char *file);
00096   #define RandomRange(max) DoRandomRange(max, __LINE__, __FILE__)
00097   uint32 DoRandomRange(uint32 max, int line, const char *file);
00098 #else
00099   static FORCEINLINE uint32 Random()
00100   {
00101     return _random.Next();
00102   }
00103 
00104   static FORCEINLINE uint32 RandomRange(uint32 max)
00105   {
00106     return _random.Next(max);
00107   }
00108 #endif
00109 
00110 static FORCEINLINE uint32 InteractiveRandom()
00111 {
00112   return _interactive_random.Next();
00113 }
00114 
00115 static FORCEINLINE uint32 InteractiveRandomRange(uint32 max)
00116 {
00117   return _interactive_random.Next(max);
00118 }
00119 
00135 static FORCEINLINE bool Chance16I(const uint a, const uint b, const uint32 r)
00136 {
00137   assert(b != 0);
00138   return (((uint16)r * b + b / 2) >> 16) < a;
00139 }
00140 
00151 #ifdef RANDOM_DEBUG
00152   #define Chance16(a, b) Chance16I(a, b, DoRandom(__LINE__, __FILE__))
00153 #else
00154 static FORCEINLINE bool Chance16(const uint a, const uint b)
00155 {
00156   return Chance16I(a, b, Random());
00157 }
00158 #endif /* RANDOM_DEBUG */
00159 
00175 #ifdef RANDOM_DEBUG
00176   #define Chance16R(a, b, r) (r = DoRandom(__LINE__, __FILE__), Chance16I(a, b, r))
00177 #else
00178 static FORCEINLINE bool Chance16R(const uint a, const uint b, uint32 &r)
00179 {
00180   r = Random();
00181   return Chance16I(a, b, r);
00182 }
00183 #endif /* RANDOM_DEBUG */
00184 
00185 #endif /* RANDOM_FUNC_HPP */

Generated on Sun Jan 9 16:01:53 2011 for OpenTTD by  doxygen 1.6.1