2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file random_func.hpp Pseudo random number generator. */
|
2007-11-21 19:13:38 +00:00
|
|
|
|
|
|
|
#ifndef RANDOM_FUNC_HPP
|
|
|
|
#define RANDOM_FUNC_HPP
|
|
|
|
|
2019-08-05 18:16:51 +00:00
|
|
|
#include "bitmath_func.hpp"
|
|
|
|
|
2007-12-23 14:06:03 +00:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
/* Apple already has Random declared */
|
2020-07-02 21:36:10 +00:00
|
|
|
# define Random OTTD_Random
|
2007-12-23 14:06:03 +00:00
|
|
|
#endif /* __APPLE__ */
|
|
|
|
|
2008-01-29 00:27:25 +00:00
|
|
|
/**
|
|
|
|
* Structure to encapsulate the pseudo random number generators.
|
|
|
|
*/
|
|
|
|
struct Randomizer {
|
|
|
|
/** The state of the randomizer */
|
|
|
|
uint32 state[2];
|
|
|
|
|
|
|
|
uint32 Next();
|
2013-10-20 14:48:08 +00:00
|
|
|
uint32 Next(uint32 limit);
|
2008-01-29 00:27:25 +00:00
|
|
|
void SetSeed(uint32 seed);
|
|
|
|
};
|
|
|
|
extern Randomizer _random; ///< Random used in the game state calculations
|
2019-01-13 07:38:48 +00:00
|
|
|
extern Randomizer _interactive_random; ///< Random used everywhere else, where it does not (directly) influence the game state
|
2008-01-29 00:27:25 +00:00
|
|
|
|
2008-08-16 14:02:20 +00:00
|
|
|
/** Stores the state of all random number generators */
|
|
|
|
struct SavedRandomSeeds {
|
|
|
|
Randomizer random;
|
|
|
|
Randomizer interactive_random;
|
|
|
|
};
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Saves the current seeds
|
2008-08-16 14:02:20 +00:00
|
|
|
* @param storage Storage for saving
|
|
|
|
*/
|
|
|
|
static inline void SaveRandomSeeds(SavedRandomSeeds *storage)
|
|
|
|
{
|
|
|
|
storage->random = _random;
|
|
|
|
storage->interactive_random = _interactive_random;
|
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Restores previously saved seeds
|
2019-05-21 19:48:43 +00:00
|
|
|
* @param storage Storage where SaveRandomSeeds() stored the seeds
|
2008-08-16 14:02:20 +00:00
|
|
|
*/
|
|
|
|
static inline void RestoreRandomSeeds(const SavedRandomSeeds &storage)
|
|
|
|
{
|
|
|
|
_random = storage.random;
|
|
|
|
_interactive_random = storage.interactive_random;
|
|
|
|
}
|
|
|
|
|
2019-05-21 19:48:43 +00:00
|
|
|
struct GameRandomSeedChecker {
|
|
|
|
private:
|
|
|
|
Randomizer random;
|
|
|
|
|
|
|
|
public:
|
|
|
|
GameRandomSeedChecker()
|
|
|
|
{
|
|
|
|
this->random = _random;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Check() const
|
|
|
|
{
|
|
|
|
return (this->random.state[0] == _random.state[0] && this->random.state[1] == _random.state[1]);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2007-11-21 19:13:38 +00:00
|
|
|
void SetRandomSeed(uint32 seed);
|
|
|
|
#ifdef RANDOM_DEBUG
|
2020-07-02 21:36:10 +00:00
|
|
|
# ifdef __APPLE__
|
|
|
|
# define OTTD_Random() DoRandom(__LINE__, __FILE__)
|
|
|
|
# else
|
|
|
|
# define Random() DoRandom(__LINE__, __FILE__)
|
|
|
|
# endif
|
2007-11-21 19:13:38 +00:00
|
|
|
uint32 DoRandom(int line, const char *file);
|
2020-07-02 21:36:10 +00:00
|
|
|
# define RandomRange(limit) DoRandomRange(limit, __LINE__, __FILE__)
|
2013-10-20 14:48:08 +00:00
|
|
|
uint32 DoRandomRange(uint32 limit, int line, const char *file);
|
2007-11-21 19:13:38 +00:00
|
|
|
#else
|
2011-12-20 17:57:56 +00:00
|
|
|
static inline uint32 Random()
|
2008-06-22 15:41:38 +00:00
|
|
|
{
|
|
|
|
return _random.Next();
|
|
|
|
}
|
|
|
|
|
2013-10-20 14:48:08 +00:00
|
|
|
/**
|
|
|
|
* Pick a random number between 0 and \a limit - 1, inclusive. That means 0
|
|
|
|
* can be returned and \a limit - 1 can be returned, but \a limit can not be
|
|
|
|
* returned.
|
|
|
|
* @param limit Limit for the range to be picked from.
|
|
|
|
* @return A random number in [0,\a limit).
|
|
|
|
*/
|
|
|
|
static inline uint32 RandomRange(uint32 limit)
|
2008-06-22 15:41:38 +00:00
|
|
|
{
|
2013-10-20 14:48:08 +00:00
|
|
|
return _random.Next(limit);
|
2008-06-22 15:41:38 +00:00
|
|
|
}
|
2007-11-21 19:13:38 +00:00
|
|
|
#endif
|
|
|
|
|
2011-12-20 17:57:56 +00:00
|
|
|
static inline uint32 InteractiveRandom()
|
2008-06-22 15:41:38 +00:00
|
|
|
{
|
|
|
|
return _interactive_random.Next();
|
|
|
|
}
|
|
|
|
|
2013-10-20 14:48:08 +00:00
|
|
|
static inline uint32 InteractiveRandomRange(uint32 limit)
|
2008-06-22 15:41:38 +00:00
|
|
|
{
|
2013-10-20 14:48:08 +00:00
|
|
|
return _interactive_random.Next(limit);
|
2008-06-22 15:41:38 +00:00
|
|
|
}
|
2007-11-21 19:13:38 +00:00
|
|
|
|
2007-11-25 15:35:25 +00:00
|
|
|
/**
|
|
|
|
* Checks if a given randomize-number is below a given probability.
|
|
|
|
*
|
|
|
|
* This function is used to check if the given probability by the fraction of (a/b)
|
2009-04-09 22:37:59 +00:00
|
|
|
* is greater than low 16 bits of the given randomize-number r.
|
2007-11-25 15:35:25 +00:00
|
|
|
*
|
|
|
|
* Do not use this function twice on the same random 16 bits as it will yield
|
|
|
|
* the same result. One can use a random number for two calls to Chance16I,
|
|
|
|
* where one call sends the low 16 bits and the other the high 16 bits.
|
|
|
|
*
|
|
|
|
* @param a The numerator of the fraction
|
|
|
|
* @param b The denominator of the fraction, must of course not be null
|
|
|
|
* @param r The given randomize-number
|
2009-04-09 22:37:59 +00:00
|
|
|
* @return True if the probability given by r is less or equal to (a/b)
|
2007-11-25 15:35:25 +00:00
|
|
|
*/
|
2011-12-20 17:57:56 +00:00
|
|
|
static inline bool Chance16I(const uint a, const uint b, const uint32 r)
|
2007-11-25 15:35:25 +00:00
|
|
|
{
|
|
|
|
assert(b != 0);
|
2009-04-09 22:37:59 +00:00
|
|
|
return (((uint16)r * b + b / 2) >> 16) < a;
|
2007-11-25 15:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-04-25 22:22:27 +00:00
|
|
|
* Flips a coin with given probability.
|
2007-11-25 15:35:25 +00:00
|
|
|
*
|
2009-04-25 22:22:27 +00:00
|
|
|
* This function returns true with (a/b) probability.
|
2007-11-25 15:35:25 +00:00
|
|
|
*
|
|
|
|
* @see Chance16I()
|
2009-04-25 22:22:27 +00:00
|
|
|
* @param a The nominator of the fraction
|
2007-11-25 15:35:25 +00:00
|
|
|
* @param b The denominator of the fraction
|
2009-04-25 22:22:27 +00:00
|
|
|
* @return True with (a/b) probability
|
2007-11-25 15:35:25 +00:00
|
|
|
*/
|
2009-05-14 12:08:24 +00:00
|
|
|
#ifdef RANDOM_DEBUG
|
2020-07-02 21:36:10 +00:00
|
|
|
# define Chance16(a, b) Chance16I(a, b, DoRandom(__LINE__, __FILE__))
|
2009-05-14 12:08:24 +00:00
|
|
|
#else
|
2011-12-20 17:57:56 +00:00
|
|
|
static inline bool Chance16(const uint a, const uint b)
|
2007-11-25 15:35:25 +00:00
|
|
|
{
|
|
|
|
return Chance16I(a, b, Random());
|
|
|
|
}
|
2009-05-14 12:08:24 +00:00
|
|
|
#endif /* RANDOM_DEBUG */
|
2007-11-25 15:35:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Flips a coin with a given probability and saves the randomize-number in a variable.
|
|
|
|
*
|
|
|
|
* This function uses the same parameters as Chance16. The third parameter
|
|
|
|
* must be a variable the randomize-number from Random() is saved in.
|
|
|
|
*
|
2012-12-01 13:12:39 +00:00
|
|
|
* The low 16 bits of r will already be used and can therefore not be passed to
|
2007-11-25 15:35:25 +00:00
|
|
|
* Chance16I. One can only send the high 16 bits to Chance16I.
|
|
|
|
*
|
|
|
|
* @see Chance16I()
|
|
|
|
* @param a The numerator of the fraction
|
|
|
|
* @param b The denominator of the fraction
|
|
|
|
* @param r The variable to save the randomize-number from Random()
|
|
|
|
* @return True in (a/b) percent
|
|
|
|
*/
|
2009-05-14 12:08:24 +00:00
|
|
|
#ifdef RANDOM_DEBUG
|
2020-07-02 21:36:10 +00:00
|
|
|
# define Chance16R(a, b, r) (r = DoRandom(__LINE__, __FILE__), Chance16I(a, b, r))
|
2009-05-14 12:08:24 +00:00
|
|
|
#else
|
2011-12-20 17:57:56 +00:00
|
|
|
static inline bool Chance16R(const uint a, const uint b, uint32 &r)
|
2007-11-25 15:35:25 +00:00
|
|
|
{
|
|
|
|
r = Random();
|
|
|
|
return Chance16I(a, b, r);
|
|
|
|
}
|
2009-05-14 12:08:24 +00:00
|
|
|
#endif /* RANDOM_DEBUG */
|
2007-11-25 15:35:25 +00:00
|
|
|
|
2007-11-21 19:13:38 +00:00
|
|
|
#endif /* RANDOM_FUNC_HPP */
|