2014-03-15 16:59:03 +00:00
|
|
|
// SONIC ROBO BLAST 2
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Copyright (C) 1993-1996 by id Software, Inc.
|
|
|
|
// Copyright (C) 1998-2000 by DooM Legacy Team.
|
2020-01-06 22:16:27 +00:00
|
|
|
// Copyright (C) 2012-2016 by Matthew "Kaito Sinclaire" Walsh.
|
2019-12-06 18:49:42 +00:00
|
|
|
// Copyright (C) 1999-2019 by Sonic Team Junior.
|
2014-03-15 16:59:03 +00:00
|
|
|
//
|
|
|
|
// This program is free software distributed under the
|
|
|
|
// terms of the GNU General Public License, version 2.
|
|
|
|
// See the 'LICENSE' file for more details.
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
/// \file m_random.c
|
2016-03-26 22:48:12 +00:00
|
|
|
/// \brief RNG for client effects and PRNG for game actions
|
2014-03-15 16:59:03 +00:00
|
|
|
|
|
|
|
#include "doomdef.h"
|
|
|
|
#include "doomtype.h"
|
|
|
|
#include "doomstat.h" // totalplaytime
|
|
|
|
|
|
|
|
#include "m_random.h"
|
|
|
|
#include "m_fixed.h"
|
|
|
|
|
2016-03-27 14:33:15 +00:00
|
|
|
|
|
|
|
|
2014-03-15 16:59:03 +00:00
|
|
|
// ---------------------------
|
|
|
|
// RNG functions (not synched)
|
|
|
|
// ---------------------------
|
|
|
|
|
2016-03-27 14:33:15 +00:00
|
|
|
/** Provides a random fixed point number. Distribution is uniform.
|
|
|
|
* As with all M_Random functions, not synched in netgames.
|
2014-03-15 16:59:03 +00:00
|
|
|
*
|
2016-03-27 14:33:15 +00:00
|
|
|
* \return A random fixed point number from [0,1).
|
2014-03-15 16:59:03 +00:00
|
|
|
*/
|
2016-03-27 14:33:15 +00:00
|
|
|
fixed_t M_RandomFixed(void)
|
2014-03-15 16:59:03 +00:00
|
|
|
{
|
2016-03-27 22:57:50 +00:00
|
|
|
#if RAND_MAX < 65535
|
|
|
|
// Compensate for insufficient randomness.
|
|
|
|
fixed_t rndv = (rand()&1)<<15;
|
|
|
|
return rand()^rndv;
|
|
|
|
#else
|
2016-03-27 14:33:15 +00:00
|
|
|
return (rand() & 0xFFFF);
|
2016-03-27 22:57:50 +00:00
|
|
|
#endif
|
2014-03-15 16:59:03 +00:00
|
|
|
}
|
|
|
|
|
2016-03-27 14:33:15 +00:00
|
|
|
/** Provides a random byte. Distribution is uniform.
|
|
|
|
* As with all M_Random functions, not synched in netgames.
|
2014-03-15 16:59:03 +00:00
|
|
|
*
|
2016-03-27 14:33:15 +00:00
|
|
|
* \return A random integer from [0, 255].
|
2014-03-15 16:59:03 +00:00
|
|
|
*/
|
2016-03-27 14:33:15 +00:00
|
|
|
UINT8 M_RandomByte(void)
|
2014-03-15 16:59:03 +00:00
|
|
|
{
|
2016-03-27 14:33:15 +00:00
|
|
|
return (rand() & 0xFF);
|
2014-03-15 16:59:03 +00:00
|
|
|
}
|
|
|
|
|
2016-03-27 14:33:15 +00:00
|
|
|
/** Provides a random integer for picking random elements from an array.
|
|
|
|
* Distribution is uniform.
|
|
|
|
* As with all M_Random functions, not synched in netgames.
|
2014-03-15 16:59:03 +00:00
|
|
|
*
|
2016-03-27 14:33:15 +00:00
|
|
|
* \param a Number of items in array.
|
|
|
|
* \return A random integer from [0,a).
|
2014-03-15 16:59:03 +00:00
|
|
|
*/
|
|
|
|
INT32 M_RandomKey(INT32 a)
|
|
|
|
{
|
|
|
|
return (INT32)((rand()/((unsigned)RAND_MAX+1.0f))*a);
|
|
|
|
}
|
|
|
|
|
2016-03-27 14:33:15 +00:00
|
|
|
/** Provides a random integer in a given range.
|
2014-03-15 16:59:03 +00:00
|
|
|
* Distribution is uniform.
|
2016-03-27 14:33:15 +00:00
|
|
|
* As with all M_Random functions, not synched in netgames.
|
2014-03-15 16:59:03 +00:00
|
|
|
*
|
2016-03-27 14:33:15 +00:00
|
|
|
* \param a Lower bound.
|
|
|
|
* \param b Upper bound.
|
|
|
|
* \return A random integer from [a,b].
|
2014-03-15 16:59:03 +00:00
|
|
|
*/
|
|
|
|
INT32 M_RandomRange(INT32 a, INT32 b)
|
|
|
|
{
|
|
|
|
return (INT32)((rand()/((unsigned)RAND_MAX+1.0f))*(b-a+1))+a;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ------------------------
|
|
|
|
// PRNG functions (synched)
|
|
|
|
// ------------------------
|
|
|
|
|
|
|
|
// Holds the current seed.
|
2016-03-29 13:20:26 +00:00
|
|
|
static UINT32 randomseed = 0xBADE4404;
|
2014-03-15 16:59:03 +00:00
|
|
|
|
|
|
|
// Holds the INITIAL seed value. Used for demos, possibly other debugging.
|
2016-03-29 13:20:26 +00:00
|
|
|
static UINT32 initialseed = 0xBADE4404;
|
2014-03-15 16:59:03 +00:00
|
|
|
|
2016-03-27 14:33:15 +00:00
|
|
|
/** Provides a random fixed point number.
|
2016-03-26 22:48:12 +00:00
|
|
|
* This is a variant of an xorshift PRNG; state fits in a 32 bit integer structure.
|
2014-03-15 16:59:03 +00:00
|
|
|
*
|
2016-03-26 22:48:12 +00:00
|
|
|
* \return A random fixed point number from [0,1).
|
|
|
|
*/
|
|
|
|
ATTRINLINE static fixed_t FUNCINLINE __internal_prng__(void)
|
|
|
|
{
|
2016-03-29 13:14:31 +00:00
|
|
|
randomseed ^= randomseed >> 13;
|
|
|
|
randomseed ^= randomseed >> 11;
|
2016-03-26 22:48:12 +00:00
|
|
|
randomseed ^= randomseed << 21;
|
2016-03-29 13:14:31 +00:00
|
|
|
return ( (randomseed*36548569) >> 4) & (FRACUNIT-1);
|
2016-03-26 22:48:12 +00:00
|
|
|
}
|
|
|
|
|
2016-03-27 14:33:15 +00:00
|
|
|
/** Provides a random fixed point number. Distribution is uniform.
|
|
|
|
* Literally a wrapper for the internal PRNG function.
|
2016-03-26 22:48:12 +00:00
|
|
|
*
|
2016-03-27 14:33:15 +00:00
|
|
|
* \return A random fixed point number from [0,1).
|
2014-03-15 16:59:03 +00:00
|
|
|
*/
|
|
|
|
#ifndef DEBUGRANDOM
|
2016-03-27 14:33:15 +00:00
|
|
|
fixed_t P_RandomFixed(void)
|
2014-03-15 16:59:03 +00:00
|
|
|
{
|
|
|
|
#else
|
2016-04-28 00:17:35 +00:00
|
|
|
fixed_t P_RandomFixedD(const char *rfile, INT32 rline)
|
2014-03-15 16:59:03 +00:00
|
|
|
{
|
2016-03-27 14:33:15 +00:00
|
|
|
CONS_Printf("P_RandomFixed() at: %sp %d\n", rfile, rline);
|
2014-03-15 16:59:03 +00:00
|
|
|
#endif
|
2016-03-27 14:33:15 +00:00
|
|
|
return __internal_prng__();
|
2014-03-15 16:59:03 +00:00
|
|
|
}
|
|
|
|
|
2016-03-27 14:33:15 +00:00
|
|
|
/** Provides a random byte. Distribution is uniform.
|
|
|
|
* If you're curious, (&0xFF00) >> 8 gives the same result
|
|
|
|
* as a fixed point multiplication by 256.
|
2014-03-15 16:59:03 +00:00
|
|
|
*
|
2016-03-27 14:33:15 +00:00
|
|
|
* \return Random integer from [0, 255].
|
2016-03-26 22:48:12 +00:00
|
|
|
* \sa __internal_prng__
|
2014-03-15 16:59:03 +00:00
|
|
|
*/
|
|
|
|
#ifndef DEBUGRANDOM
|
2016-03-27 14:33:15 +00:00
|
|
|
UINT8 P_RandomByte(void)
|
2016-03-26 22:48:12 +00:00
|
|
|
{
|
|
|
|
#else
|
2016-03-27 14:33:15 +00:00
|
|
|
UINT8 P_RandomByteD(const char *rfile, INT32 rline)
|
2016-03-26 22:48:12 +00:00
|
|
|
{
|
2016-03-27 14:33:15 +00:00
|
|
|
CONS_Printf("P_RandomByte() at: %sp %d\n", rfile, rline);
|
2016-03-26 22:48:12 +00:00
|
|
|
#endif
|
2016-03-27 14:33:15 +00:00
|
|
|
return (UINT8)((__internal_prng__()&0xFF00)>>8);
|
2016-03-26 22:48:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Provides a random integer for picking random elements from an array.
|
|
|
|
* Distribution is uniform.
|
2016-03-27 14:33:15 +00:00
|
|
|
* NOTE: Maximum range is 65536.
|
2016-03-26 22:48:12 +00:00
|
|
|
*
|
2016-03-27 14:33:15 +00:00
|
|
|
* \param a Number of items in array.
|
2016-03-26 22:48:12 +00:00
|
|
|
* \return A random integer from [0,a).
|
|
|
|
* \sa __internal_prng__
|
2014-03-15 16:59:03 +00:00
|
|
|
*/
|
|
|
|
#ifndef DEBUGRANDOM
|
|
|
|
INT32 P_RandomKey(INT32 a)
|
|
|
|
{
|
|
|
|
#else
|
|
|
|
INT32 P_RandomKeyD(const char *rfile, INT32 rline, INT32 a)
|
|
|
|
{
|
2014-03-17 12:13:16 +00:00
|
|
|
CONS_Printf("P_RandomKey() at: %sp %d\n", rfile, rline);
|
2014-03-15 16:59:03 +00:00
|
|
|
#endif
|
2016-05-11 20:54:40 +00:00
|
|
|
return (INT32)(((INT64)__internal_prng__() * a) >> FRACBITS);
|
2014-03-15 16:59:03 +00:00
|
|
|
}
|
|
|
|
|
2016-03-26 22:48:12 +00:00
|
|
|
/** Provides a random integer in a given range.
|
|
|
|
* Distribution is uniform.
|
2016-03-27 14:33:15 +00:00
|
|
|
* NOTE: Maximum range is 65536.
|
2014-03-15 16:59:03 +00:00
|
|
|
*
|
2016-03-27 14:33:15 +00:00
|
|
|
* \param a Lower bound.
|
|
|
|
* \param b Upper bound.
|
|
|
|
* \return A random integer from [a,b].
|
2016-03-26 22:48:12 +00:00
|
|
|
* \sa __internal_prng__
|
2014-03-15 16:59:03 +00:00
|
|
|
*/
|
|
|
|
#ifndef DEBUGRANDOM
|
|
|
|
INT32 P_RandomRange(INT32 a, INT32 b)
|
|
|
|
{
|
|
|
|
#else
|
|
|
|
INT32 P_RandomRangeD(const char *rfile, INT32 rline, INT32 a, INT32 b)
|
|
|
|
{
|
2014-03-17 12:13:16 +00:00
|
|
|
CONS_Printf("P_RandomRange() at: %sp %d\n", rfile, rline);
|
2014-03-15 16:59:03 +00:00
|
|
|
#endif
|
2016-05-11 20:54:40 +00:00
|
|
|
return (INT32)(((INT64)__internal_prng__() * (b-a+1)) >> FRACBITS) + a;
|
2014-03-15 16:59:03 +00:00
|
|
|
}
|
|
|
|
|
2016-03-26 22:48:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
// ----------------------
|
|
|
|
// PRNG seeds & debugging
|
|
|
|
// ----------------------
|
|
|
|
|
|
|
|
/** Peeks to see what the next result from the PRNG will be.
|
|
|
|
* Used for debugging.
|
2014-03-15 16:59:03 +00:00
|
|
|
*
|
2016-03-26 22:48:12 +00:00
|
|
|
* \return A 'random' fixed point number from [0,1).
|
|
|
|
* \sa __internal_prng__
|
2014-03-15 16:59:03 +00:00
|
|
|
*/
|
2016-03-26 22:48:12 +00:00
|
|
|
fixed_t P_RandomPeek(void)
|
2014-03-15 16:59:03 +00:00
|
|
|
{
|
2016-03-26 22:48:12 +00:00
|
|
|
UINT32 r = randomseed;
|
|
|
|
fixed_t ret = __internal_prng__();
|
|
|
|
randomseed = r;
|
|
|
|
return ret;
|
2014-03-15 16:59:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Gets the current random seed. Used by netgame savegames.
|
|
|
|
*
|
|
|
|
* \return Current random seed.
|
|
|
|
* \sa P_SetRandSeed
|
|
|
|
*/
|
2014-03-17 12:13:16 +00:00
|
|
|
#ifndef DEBUGRANDOM
|
2014-03-15 16:59:03 +00:00
|
|
|
UINT32 P_GetRandSeed(void)
|
|
|
|
{
|
2014-03-17 12:13:16 +00:00
|
|
|
#else
|
|
|
|
UINT32 P_GetRandSeedD(const char *rfile, INT32 rline)
|
|
|
|
{
|
|
|
|
CONS_Printf("P_GetRandSeed() at: %sp %d\n", rfile, rline);
|
|
|
|
#endif
|
2014-03-15 16:59:03 +00:00
|
|
|
return randomseed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Gets the initial random seed. Used by demos.
|
|
|
|
*
|
|
|
|
* \return Initial random seed.
|
|
|
|
* \sa P_SetRandSeed
|
|
|
|
*/
|
2014-03-17 12:13:16 +00:00
|
|
|
#ifndef DEBUGRANDOM
|
2014-03-15 16:59:03 +00:00
|
|
|
UINT32 P_GetInitSeed(void)
|
|
|
|
{
|
2014-03-17 12:13:16 +00:00
|
|
|
#else
|
|
|
|
UINT32 P_GetInitSeedD(const char *rfile, INT32 rline)
|
|
|
|
{
|
|
|
|
CONS_Printf("P_GetInitSeed() at: %sp %d\n", rfile, rline);
|
|
|
|
#endif
|
2014-03-15 16:59:03 +00:00
|
|
|
return initialseed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Sets the random seed.
|
|
|
|
* Used at the beginning of the game, and also for netgames.
|
|
|
|
*
|
|
|
|
* \param rindex New random index.
|
|
|
|
* \sa P_GetRandSeed
|
|
|
|
*/
|
2014-03-17 12:13:16 +00:00
|
|
|
#ifndef DEBUGRANDOM
|
2014-03-15 16:59:03 +00:00
|
|
|
void P_SetRandSeed(UINT32 seed)
|
|
|
|
{
|
2014-03-17 12:13:16 +00:00
|
|
|
#else
|
|
|
|
void P_SetRandSeedD(const char *rfile, INT32 rline, UINT32 seed)
|
|
|
|
{
|
|
|
|
CONS_Printf("P_SetRandSeed() at: %sp %d\n", rfile, rline);
|
|
|
|
#endif
|
2016-03-29 13:20:26 +00:00
|
|
|
// xorshift requires a nonzero seed
|
|
|
|
// this should never happen, but just in case it DOES, we check
|
|
|
|
if (!seed) seed = 0xBADE4404;
|
2014-03-15 16:59:03 +00:00
|
|
|
randomseed = initialseed = seed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Gets a randomized seed for setting the random seed.
|
|
|
|
*
|
|
|
|
* \sa P_GetRandSeed
|
|
|
|
*/
|
|
|
|
UINT32 M_RandomizedSeed(void)
|
|
|
|
{
|
2016-03-27 22:57:50 +00:00
|
|
|
return ((totalplaytime & 0xFFFF) << 16)|M_RandomFixed();
|
2014-03-15 16:59:03 +00:00
|
|
|
}
|