mirror of
https://github.com/luanti-org/luanti.git
synced 2025-06-27 16:36:03 +00:00
Add API for restoring PseudoRandom and PcgRandom state (#14123)
This commit is contained in:
parent
8093044f07
commit
ceaa7e2fb0
9 changed files with 155 additions and 29 deletions
|
@ -425,6 +425,17 @@ int LuaPseudoRandom::l_next(lua_State *L)
|
|||
return 1;
|
||||
}
|
||||
|
||||
int LuaPseudoRandom::l_get_state(lua_State *L)
|
||||
{
|
||||
NO_MAP_LOCK_REQUIRED;
|
||||
|
||||
LuaPseudoRandom *o = checkObject<LuaPseudoRandom>(L, 1);
|
||||
PseudoRandom &pseudo = o->m_pseudo;
|
||||
int val = pseudo.getState();
|
||||
lua_pushinteger(L, val);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int LuaPseudoRandom::create_object(lua_State *L)
|
||||
{
|
||||
|
@ -462,6 +473,7 @@ void LuaPseudoRandom::Register(lua_State *L)
|
|||
const char LuaPseudoRandom::className[] = "PseudoRandom";
|
||||
const luaL_Reg LuaPseudoRandom::methods[] = {
|
||||
luamethod(LuaPseudoRandom, next),
|
||||
luamethod(LuaPseudoRandom, get_state),
|
||||
{0,0}
|
||||
};
|
||||
|
||||
|
@ -496,6 +508,45 @@ int LuaPcgRandom::l_rand_normal_dist(lua_State *L)
|
|||
return 1;
|
||||
}
|
||||
|
||||
int LuaPcgRandom::l_get_state(lua_State *L)
|
||||
{
|
||||
NO_MAP_LOCK_REQUIRED;
|
||||
|
||||
LuaPcgRandom *o = checkObject<LuaPcgRandom>(L, 1);
|
||||
|
||||
u64 state[2];
|
||||
o->m_rnd.getState(state);
|
||||
|
||||
std::ostringstream oss;
|
||||
oss << std::hex << std::setw(16) << std::setfill('0')
|
||||
<< state[0] << state[1];
|
||||
|
||||
lua_pushstring(L, oss.str().c_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
int LuaPcgRandom::l_set_state(lua_State *L)
|
||||
{
|
||||
NO_MAP_LOCK_REQUIRED;
|
||||
|
||||
LuaPcgRandom *o = checkObject<LuaPcgRandom>(L, 1);
|
||||
|
||||
std::string l_string = readParam<std::string>(L, 2);
|
||||
if (l_string.size() != 32) {
|
||||
throw LuaError("PcgRandom:set_state: Expected hex string of 32 characters");
|
||||
}
|
||||
|
||||
std::istringstream s_state_0(l_string.substr(0, 16));
|
||||
std::istringstream s_state_1(l_string.substr(16, 16));
|
||||
|
||||
u64 state[2];
|
||||
s_state_0 >> std::hex >> state[0];
|
||||
s_state_1 >> std::hex >> state[1];
|
||||
|
||||
o->m_rnd.setState(state);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int LuaPcgRandom::create_object(lua_State *L)
|
||||
{
|
||||
|
@ -536,6 +587,8 @@ const char LuaPcgRandom::className[] = "PcgRandom";
|
|||
const luaL_Reg LuaPcgRandom::methods[] = {
|
||||
luamethod(LuaPcgRandom, next),
|
||||
luamethod(LuaPcgRandom, rand_normal_dist),
|
||||
luamethod(LuaPcgRandom, get_state),
|
||||
luamethod(LuaPcgRandom, set_state),
|
||||
{0,0}
|
||||
};
|
||||
|
||||
|
|
|
@ -116,6 +116,8 @@ private:
|
|||
// next(self, min=0, max=32767) -> get next value
|
||||
static int l_next(lua_State *L);
|
||||
|
||||
// save state
|
||||
static int l_get_state(lua_State *L);
|
||||
public:
|
||||
LuaPseudoRandom(s32 seed) : m_pseudo(seed) {}
|
||||
|
||||
|
@ -150,6 +152,9 @@ private:
|
|||
// get next normally distributed random value
|
||||
static int l_rand_normal_dist(lua_State *L);
|
||||
|
||||
// save and restore state
|
||||
static int l_get_state(lua_State *L);
|
||||
static int l_set_state(lua_State *L);
|
||||
public:
|
||||
LuaPcgRandom(u64 seed) : m_rnd(seed) {}
|
||||
LuaPcgRandom(u64 seed, u64 seq) : m_rnd(seed, seq) {}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue