mirror of
https://github.com/love2d/love.git
synced 2026-08-12 08:30:56 +02:00
Cleaned up the code for love.math.random and related functions.
This commit is contained in:
@@ -55,56 +55,9 @@ public:
|
||||
|
||||
virtual ~Math();
|
||||
|
||||
/**
|
||||
* @copydoc RandomGenerator::random()
|
||||
**/
|
||||
inline double random()
|
||||
RandomGenerator *getRandomGenerator()
|
||||
{
|
||||
return rng.random();
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc RandomGenerator::random(double)
|
||||
**/
|
||||
inline double random(double max)
|
||||
{
|
||||
return rng.random(max);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc RandomGenerator::random(double,double)
|
||||
**/
|
||||
inline double random(double min, double max)
|
||||
{
|
||||
return rng.random(min, max);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc RandomGenerator::randomNormal()
|
||||
**/
|
||||
inline double randomNormal(double stddev)
|
||||
{
|
||||
return rng.randomNormal(stddev);
|
||||
}
|
||||
|
||||
inline void setRandomSeed(RandomGenerator::Seed seed)
|
||||
{
|
||||
rng.setSeed(seed);
|
||||
}
|
||||
|
||||
inline RandomGenerator::Seed getRandomSeed() const
|
||||
{
|
||||
return rng.getSeed();
|
||||
}
|
||||
|
||||
inline void setRandomState(const std::string &statestr)
|
||||
{
|
||||
rng.setState(statestr);
|
||||
}
|
||||
|
||||
inline std::string getRandomState() const
|
||||
{
|
||||
return rng.getState();
|
||||
return &rng;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -39,45 +39,10 @@ namespace love
|
||||
namespace math
|
||||
{
|
||||
|
||||
int w__random(lua_State *L)
|
||||
int w__getRandomGenerator(lua_State *L)
|
||||
{
|
||||
lua_pushnumber(L, Math::instance.random());
|
||||
return 1;
|
||||
}
|
||||
|
||||
int w_randomNormal(lua_State *L)
|
||||
{
|
||||
double stddev = luaL_optnumber(L, 1, 1.0);
|
||||
double mean = luaL_optnumber(L, 2, 0.0);
|
||||
double r = Math::instance.randomNormal(stddev);
|
||||
|
||||
lua_pushnumber(L, r + mean);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int w_setRandomSeed(lua_State *L)
|
||||
{
|
||||
luax_catchexcept(L, [&](){ Math::instance.setRandomSeed(luax_checkrandomseed(L, 1)); });
|
||||
return 0;
|
||||
}
|
||||
|
||||
int w_getRandomSeed(lua_State *L)
|
||||
{
|
||||
RandomGenerator::Seed s = Math::instance.getRandomSeed();
|
||||
lua_pushnumber(L, (lua_Number) s.b32.low);
|
||||
lua_pushnumber(L, (lua_Number) s.b32.high);
|
||||
return 2;
|
||||
}
|
||||
|
||||
int w_setRandomState(lua_State *L)
|
||||
{
|
||||
luax_catchexcept(L, [&](){ Math::instance.setRandomState(luax_checkstring(L, 1)); });
|
||||
return 0;
|
||||
}
|
||||
|
||||
int w_getRandomState(lua_State *L)
|
||||
{
|
||||
luax_pushstring(L, Math::instance.getRandomState());
|
||||
RandomGenerator *t = Math::instance.getRandomGenerator();
|
||||
luax_pushtype(L, MATH_RANDOM_GENERATOR_ID, t);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -423,8 +388,6 @@ int w_decompress(lua_State *L)
|
||||
// C functions in a struct, necessary for the FFI versions of math functions.
|
||||
struct FFI_Math
|
||||
{
|
||||
double (*random)(void);
|
||||
|
||||
float (*noise1)(float x);
|
||||
float (*noise2)(float x, float y);
|
||||
float (*noise3)(float x, float y, float z);
|
||||
@@ -436,11 +399,6 @@ struct FFI_Math
|
||||
|
||||
static FFI_Math ffifuncs =
|
||||
{
|
||||
[](void) -> double // random
|
||||
{
|
||||
return Math::instance.random();
|
||||
},
|
||||
|
||||
[](float x) -> float // noise1
|
||||
{
|
||||
return Math::instance.noise(x);
|
||||
@@ -471,12 +429,9 @@ static FFI_Math ffifuncs =
|
||||
// List of functions to wrap.
|
||||
static const luaL_Reg functions[] =
|
||||
{
|
||||
{ "_random", w__random }, // love.math.random is inside wrap_Math.lua.
|
||||
{ "randomNormal", w_randomNormal },
|
||||
{ "setRandomSeed", w_setRandomSeed },
|
||||
{ "getRandomSeed", w_getRandomSeed },
|
||||
{ "setRandomState", w_setRandomState },
|
||||
{ "getRandomState", w_getRandomState },
|
||||
// love.math.random etc. are defined in wrap_Math.lua.
|
||||
|
||||
{ "_getRandomGenerator", w__getRandomGenerator },
|
||||
{ "newRandomGenerator", w_newRandomGenerator },
|
||||
{ "newBezierCurve", w_newBezierCurve },
|
||||
{ "triangulate", w_triangulate },
|
||||
@@ -510,7 +465,7 @@ extern "C" int luaopen_love_math(lua_State *L)
|
||||
|
||||
int n = luax_register_module(L, w);
|
||||
|
||||
// Execute wrap_Event.lua, sending the math table and ffifuncs pointer as args.
|
||||
// Execute wrap_Math.lua, sending the math table and ffifuncs pointer as args.
|
||||
luaL_loadbuffer(L, math_lua, sizeof(math_lua), "wrap_Math.lua");
|
||||
lua_pushvalue(L, -2);
|
||||
lua_pushlightuserdata(L, &ffifuncs);
|
||||
|
||||
@@ -27,24 +27,30 @@ local love_math, ffifuncspointer = ...
|
||||
local type, tonumber, error = type, tonumber, error
|
||||
local floor = math.floor
|
||||
|
||||
local _random = love_math._random
|
||||
|
||||
local function getrandom(r, l, u)
|
||||
if u ~= nil then
|
||||
if type(r) ~= "number" then error("bad argument #1 to 'random' (number expected)", 2) end
|
||||
if type(l) ~= "number" then error("bad argument #2 to 'random' (number expected)", 2) end
|
||||
return floor(r * (u - l + 1)) + l
|
||||
elseif l ~= nil then
|
||||
if type(l) ~= "number" then error("bad argument #1 to 'random' (number expected)", 2) end
|
||||
return floor(r * l) + 1
|
||||
else
|
||||
return r
|
||||
end
|
||||
end
|
||||
local rng = love_math._getRandomGenerator()
|
||||
|
||||
function love_math.random(l, u)
|
||||
local r = _random()
|
||||
return getrandom(r, l, u)
|
||||
return rng:random(l, u)
|
||||
end
|
||||
|
||||
function love_math.randomNormal(stddev, mean)
|
||||
return rng:randomNormal(stddev, mean)
|
||||
end
|
||||
|
||||
function love_math.setRandomSeed(low, high)
|
||||
return rng:setSeed(low, high)
|
||||
end
|
||||
|
||||
function love_math.getRandomSeed()
|
||||
return rng:getSeed()
|
||||
end
|
||||
|
||||
function love_math.setRandomState(state)
|
||||
return rng:setState(state)
|
||||
end
|
||||
|
||||
function love_math.getRandomState()
|
||||
return rng:getState()
|
||||
end
|
||||
|
||||
if type(jit) ~= "table" or not jit.status() then
|
||||
@@ -60,8 +66,6 @@ if not status then return end
|
||||
pcall(ffi.cdef, [[
|
||||
typedef struct FFI_Math
|
||||
{
|
||||
double (*random)(void);
|
||||
|
||||
float (*noise1)(float x);
|
||||
float (*noise2)(float x, float y);
|
||||
float (*noise3)(float x, float y, float z);
|
||||
@@ -77,11 +81,6 @@ local ffifuncs = ffi.cast("FFI_Math *", ffifuncspointer)
|
||||
|
||||
-- Overwrite some regular love.math functions with FFI implementations.
|
||||
|
||||
function love_math.random(l, u)
|
||||
local r = tonumber(ffifuncs.random())
|
||||
return getrandom(r, l, u)
|
||||
end
|
||||
|
||||
function love_math.noise(x, y, z, w)
|
||||
if w ~= nil then
|
||||
return tonumber(ffifuncs.noise4(x, y, z, w))
|
||||
|
||||
Reference in New Issue
Block a user