mirror of
https://github.com/love2d/love.git
synced 2026-08-12 08:30:56 +02:00
Renamed love.math.g/setRandomState and rng:g/setState to love.math.g/setRandomSeed and rng:g/setSeed.
getRandomSeed now always returns the previously set random seed instead of the current (implementation-dependent) random state.
This commit is contained in:
@@ -53,24 +53,24 @@ public:
|
||||
virtual ~Math()
|
||||
{}
|
||||
|
||||
inline void setRandomState(RandomGenerator::State state)
|
||||
inline void setRandomSeed(RandomGenerator::Seed seed)
|
||||
{
|
||||
rng.setState(state);
|
||||
rng.setSeed(seed);
|
||||
}
|
||||
|
||||
inline void setRandomState(uint32 low, uint32 high)
|
||||
inline void setRandomSeed(uint32 low, uint32 high)
|
||||
{
|
||||
rng.setState(low, high);
|
||||
rng.setSeed(low, high);
|
||||
}
|
||||
|
||||
inline RandomGenerator::State getRandomState() const
|
||||
inline RandomGenerator::Seed getRandomSeed() const
|
||||
{
|
||||
return rng.getState();
|
||||
return rng.getSeed();
|
||||
}
|
||||
|
||||
inline void getRandomState(uint32 &low, uint32 &high) const
|
||||
inline void getRandomSeed(uint32 &low, uint32 &high) const
|
||||
{
|
||||
rng.getState(low, high);
|
||||
rng.getSeed(low, high);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -37,21 +37,24 @@ RandomGenerator::RandomGenerator()
|
||||
// because it is too big for some compilers to handle ... if you know what
|
||||
// i mean
|
||||
#ifdef LOVE_BIG_ENDIAN
|
||||
rng_state.b32.a = 0x0139408D;
|
||||
rng_state.b32.b = 0xCBBF7A44;
|
||||
seed.b32.a = 0x0139408D;
|
||||
seed.b32.b = 0xCBBF7A44;
|
||||
#else
|
||||
rng_state.b32.b = 0x0139408D;
|
||||
rng_state.b32.a = 0xCBBF7A44;
|
||||
seed.b32.b = 0x0139408D;
|
||||
seed.b32.a = 0xCBBF7A44;
|
||||
#endif
|
||||
|
||||
rng_state = seed;
|
||||
}
|
||||
|
||||
void RandomGenerator::setState(RandomGenerator::State state)
|
||||
void RandomGenerator::setSeed(RandomGenerator::Seed newseed)
|
||||
{
|
||||
// 0 xor 0 is still 0, so Xorshift can't generate new numbers.
|
||||
if (state.b64 == 0)
|
||||
throw love::Exception("Invalid random state.");
|
||||
if (newseed.b64 == 0)
|
||||
throw love::Exception("Invalid random seed.");
|
||||
|
||||
rng_state = state;
|
||||
seed = newseed;
|
||||
rng_state = seed;
|
||||
}
|
||||
|
||||
uint64 RandomGenerator::rand()
|
||||
|
||||
@@ -40,7 +40,7 @@ class RandomGenerator : public Object
|
||||
{
|
||||
public:
|
||||
|
||||
union State
|
||||
union Seed
|
||||
{
|
||||
uint64 b64;
|
||||
struct
|
||||
@@ -54,42 +54,42 @@ public:
|
||||
virtual ~RandomGenerator() {}
|
||||
|
||||
/**
|
||||
* Set pseudo-random state.
|
||||
* Set pseudo-random seed.
|
||||
* It's up to the implementation how to use this.
|
||||
**/
|
||||
void setState(State state);
|
||||
void setSeed(Seed seed);
|
||||
|
||||
/**
|
||||
* Separately set the low and high parts of the pseudo-random state.
|
||||
* Separately set the low and high bits of the pseudo-random seed.
|
||||
**/
|
||||
inline void setState(uint32 low, uint32 high)
|
||||
inline void setSeed(uint32 low, uint32 high)
|
||||
{
|
||||
State newstate;
|
||||
Seed newseed;
|
||||
|
||||
#ifdef LOVE_BIG_ENDIAN
|
||||
newstate.b32.a = high;
|
||||
newstate.b32.b = low;
|
||||
newseed.b32.a = high;
|
||||
newseed.b32.b = low;
|
||||
#else
|
||||
newstate.b32.b = high;
|
||||
newstate.b32.a = low;
|
||||
newseed.b32.b = high;
|
||||
newseed.b32.a = low;
|
||||
#endif
|
||||
|
||||
setState(newstate);
|
||||
setSeed(newseed);
|
||||
}
|
||||
|
||||
inline State getState() const
|
||||
inline Seed getSeed() const
|
||||
{
|
||||
return rng_state;
|
||||
return seed;
|
||||
}
|
||||
|
||||
inline void getState(uint32 &low, uint32 &high) const
|
||||
inline void getSeed(uint32 &low, uint32 &high) const
|
||||
{
|
||||
#ifdef LOVE_BIG_ENDIAN
|
||||
high = rng_state.b32.a;
|
||||
low = rng_state.b32.b;
|
||||
high = seed.b32.a;
|
||||
low = seed.b32.b;
|
||||
#else
|
||||
high = rng_state.b32.b;
|
||||
low = rng_state.b32.a;
|
||||
high = seed.b32.b;
|
||||
low = seed.b32.a;
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -140,7 +140,8 @@ public:
|
||||
|
||||
private:
|
||||
|
||||
State rng_state;
|
||||
Seed seed;
|
||||
Seed rng_state;
|
||||
double last_randomnormal;
|
||||
|
||||
}; // RandomGenerator
|
||||
|
||||
@@ -32,16 +32,16 @@ namespace love
|
||||
namespace math
|
||||
{
|
||||
|
||||
int w_setRandomState(lua_State *L)
|
||||
int w_setRandomSeed(lua_State *L)
|
||||
{
|
||||
EXCEPT_GUARD(Math::instance.setRandomState(luax_checkrandomstate(L, 1));)
|
||||
EXCEPT_GUARD(Math::instance.setRandomSeed(luax_checkrandomseed(L, 1));)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int w_getRandomState(lua_State *L)
|
||||
int w_getRandomSeed(lua_State *L)
|
||||
{
|
||||
uint32 low = 0, high = 0;
|
||||
Math::instance.getRandomState(low, high);
|
||||
Math::instance.getRandomSeed(low, high);
|
||||
lua_pushnumber(L, (lua_Number) low);
|
||||
lua_pushnumber(L, (lua_Number) high);
|
||||
return 2;
|
||||
@@ -64,9 +64,9 @@ int w_randomNormal(lua_State *L)
|
||||
|
||||
int w_newRandomGenerator(lua_State *L)
|
||||
{
|
||||
RandomGenerator::State s;
|
||||
RandomGenerator::Seed s;
|
||||
if (lua_gettop(L) > 0)
|
||||
s = luax_checkrandomstate(L, 1);
|
||||
s = luax_checkrandomseed(L, 1);
|
||||
|
||||
RandomGenerator *t = Math::instance.newRandomGenerator();
|
||||
|
||||
@@ -76,7 +76,7 @@ int w_newRandomGenerator(lua_State *L)
|
||||
|
||||
try
|
||||
{
|
||||
t->setState(s);
|
||||
t->setSeed(s);
|
||||
}
|
||||
catch (love::Exception &e)
|
||||
{
|
||||
@@ -277,8 +277,8 @@ int w_noise(lua_State *L)
|
||||
// List of functions to wrap.
|
||||
static const luaL_Reg functions[] =
|
||||
{
|
||||
{ "setRandomState", w_setRandomState },
|
||||
{ "getRandomState", w_getRandomState },
|
||||
{ "setRandomSeed", w_setRandomSeed },
|
||||
{ "getRandomSeed", w_getRandomSeed },
|
||||
{ "random", w_random },
|
||||
{ "randomNormal", w_randomNormal },
|
||||
{ "newRandomGenerator", w_newRandomGenerator },
|
||||
|
||||
@@ -30,8 +30,8 @@ namespace love
|
||||
namespace math
|
||||
{
|
||||
|
||||
int w_setRandomState(lua_State *L);
|
||||
int w_getRandomState(lua_State *L);
|
||||
int w_setRandomSeed(lua_State *L);
|
||||
int w_getRandomSeed(lua_State *L);
|
||||
int w_random(lua_State *L);
|
||||
int w_randomNormal(lua_State *L);
|
||||
int w_newRandomGenerator(lua_State *L);
|
||||
|
||||
@@ -29,26 +29,26 @@ namespace math
|
||||
{
|
||||
|
||||
template <typename T>
|
||||
static T checkrandomstate_part(lua_State *L, int idx)
|
||||
static T checkrandomseed_part(lua_State *L, int idx)
|
||||
{
|
||||
double num = luaL_checknumber(L, idx);
|
||||
double inf = std::numeric_limits<double>::infinity();
|
||||
|
||||
// Disallow conversions from infinity and NaN.
|
||||
if (num == inf || num == -inf || num != num)
|
||||
luaL_argerror(L, idx, "invalid random state");
|
||||
luaL_argerror(L, idx, "invalid random seed");
|
||||
|
||||
return (T) num;
|
||||
}
|
||||
|
||||
RandomGenerator::State luax_checkrandomstate(lua_State *L, int idx)
|
||||
RandomGenerator::Seed luax_checkrandomseed(lua_State *L, int idx)
|
||||
{
|
||||
RandomGenerator::State s;
|
||||
RandomGenerator::Seed s;
|
||||
|
||||
if (!lua_isnoneornil(L, idx + 1))
|
||||
{
|
||||
uint32 low = checkrandomstate_part<uint32>(L, idx);
|
||||
uint32 high = checkrandomstate_part<uint32>(L, idx + 1);
|
||||
uint32 low = checkrandomseed_part<uint32>(L, idx);
|
||||
uint32 high = checkrandomseed_part<uint32>(L, idx + 1);
|
||||
|
||||
#ifdef LOVE_BIG_ENDIAN
|
||||
s.b32.a = high;
|
||||
@@ -59,7 +59,7 @@ RandomGenerator::State luax_checkrandomstate(lua_State *L, int idx)
|
||||
#endif
|
||||
}
|
||||
else
|
||||
s.b64 = checkrandomstate_part<uint64>(L, idx);
|
||||
s.b64 = checkrandomseed_part<uint64>(L, idx);
|
||||
|
||||
return s;
|
||||
}
|
||||
@@ -95,19 +95,19 @@ RandomGenerator *luax_checkrandomgenerator(lua_State *L, int idx)
|
||||
return luax_checktype<RandomGenerator>(L, idx, "RandomGenerator", MATH_RANDOM_GENERATOR_T);
|
||||
}
|
||||
|
||||
int w_RandomGenerator_setState(lua_State *L)
|
||||
int w_RandomGenerator_setSeed(lua_State *L)
|
||||
{
|
||||
RandomGenerator *rng = luax_checkrandomgenerator(L, 1);
|
||||
EXCEPT_GUARD(rng->setState(luax_checkrandomstate(L, 2));)
|
||||
EXCEPT_GUARD(rng->setSeed(luax_checkrandomseed(L, 2));)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int w_RandomGenerator_getState(lua_State *L)
|
||||
int w_RandomGenerator_getSeed(lua_State *L)
|
||||
{
|
||||
RandomGenerator *rng = luax_checkrandomgenerator(L, 1);
|
||||
|
||||
uint32 low = 0, high = 0;
|
||||
rng->getState(low, high);
|
||||
rng->getSeed(low, high);
|
||||
|
||||
lua_pushnumber(L, (lua_Number) low);
|
||||
lua_pushnumber(L, (lua_Number) high);
|
||||
@@ -134,8 +134,8 @@ int w_RandomGenerator_randomNormal(lua_State *L)
|
||||
|
||||
static const luaL_Reg functions[] =
|
||||
{
|
||||
{ "setState", w_RandomGenerator_setState },
|
||||
{ "getState", w_RandomGenerator_getState },
|
||||
{ "setSeed", w_RandomGenerator_setSeed },
|
||||
{ "getSeed", w_RandomGenerator_getSeed },
|
||||
{ "random", w_RandomGenerator_random },
|
||||
{ "randomNormal", w_RandomGenerator_randomNormal },
|
||||
{ 0, 0 }
|
||||
|
||||
@@ -32,12 +32,12 @@ namespace math
|
||||
{
|
||||
|
||||
// Helper functions.
|
||||
RandomGenerator::State luax_checkrandomstate(lua_State *L, int idx);
|
||||
RandomGenerator::Seed luax_checkrandomseed(lua_State *L, int idx);
|
||||
int luax_getrandom(lua_State *L, int startidx, double r);
|
||||
|
||||
RandomGenerator *luax_checkrandomgenerator(lua_State *L, int idx);
|
||||
int w_RandomGenerator_setState(lua_State *L);
|
||||
int w_RandomGenerator_getState(lua_State *L);
|
||||
int w_RandomGenerator_setSeed(lua_State *L);
|
||||
int w_RandomGenerator_getSeed(lua_State *L);
|
||||
int w_RandomGenerator_random(lua_State *L);
|
||||
int w_RandomGenerator_randomNormal(lua_State *L);
|
||||
extern "C" int luaopen_randomgenerator(lua_State *L);
|
||||
|
||||
@@ -453,7 +453,7 @@ end
|
||||
function love.run()
|
||||
|
||||
if love.math then
|
||||
love.math.setRandomState(os.time())
|
||||
love.math.setRandomSeed(os.time())
|
||||
end
|
||||
|
||||
if love.event then
|
||||
|
||||
@@ -805,8 +805,7 @@ const unsigned char boot_lua[] =
|
||||
0x29, 0x0a,
|
||||
0x09, 0x69, 0x66, 0x20, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x6d, 0x61, 0x74, 0x68, 0x20, 0x74, 0x68, 0x65, 0x6e, 0x0a,
|
||||
0x09, 0x09, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x6d, 0x61, 0x74, 0x68, 0x2e, 0x73, 0x65, 0x74, 0x52, 0x61, 0x6e,
|
||||
0x64, 0x6f, 0x6d, 0x53, 0x74, 0x61, 0x74, 0x65, 0x28, 0x6f, 0x73, 0x2e, 0x74, 0x69, 0x6d, 0x65, 0x28, 0x29,
|
||||
0x29, 0x0a,
|
||||
0x64, 0x6f, 0x6d, 0x53, 0x65, 0x65, 0x64, 0x28, 0x6f, 0x73, 0x2e, 0x74, 0x69, 0x6d, 0x65, 0x28, 0x29, 0x29, 0x0a,
|
||||
0x09, 0x65, 0x6e, 0x64, 0x0a,
|
||||
0x09, 0x69, 0x66, 0x20, 0x6c, 0x6f, 0x76, 0x65, 0x2e, 0x65, 0x76, 0x65, 0x6e, 0x74, 0x20, 0x74, 0x68, 0x65,
|
||||
0x6e, 0x0a,
|
||||
|
||||
Reference in New Issue
Block a user