1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-06-27 16:36:03 +00:00

Add support for NoiseParams in minetest.get_perlin() and add docs on NoiseParams to lua_api.txt

This commit is contained in:
kwolekr 2014-12-12 02:02:26 -05:00
parent c151099b79
commit 2b8180a417
4 changed files with 100 additions and 33 deletions

View file

@ -590,12 +590,20 @@ int ModApiEnvMod::l_get_perlin(lua_State *L)
{
GET_ENV_PTR;
int seeddiff = luaL_checkint(L, 1);
int octaves = luaL_checkint(L, 2);
float persistence = luaL_checknumber(L, 3);
float scale = luaL_checknumber(L, 4);
NoiseParams params;
LuaPerlinNoise *n = new LuaPerlinNoise(seeddiff + int(env->getServerMap().getSeed()), octaves, persistence, scale);
if (lua_istable(L, 1)) {
read_noiseparams(L, 1, &params);
} else {
params.seed = luaL_checkint(L, 1);
params.octaves = luaL_checkint(L, 2);
params.persist = luaL_checknumber(L, 3);
params.spread = v3f(1, 1, 1) * luaL_checknumber(L, 4);
}
params.seed += (int)env->getServerMap().getSeed();
LuaPerlinNoise *n = new LuaPerlinNoise(&params);
*(void **)(lua_newuserdata(L, sizeof(void *))) = n;
luaL_getmetatable(L, "PerlinNoise");
lua_setmetatable(L, -2);

View file

@ -36,8 +36,8 @@ int LuaPerlinNoise::l_get2d(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
LuaPerlinNoise *o = checkobject(L, 1);
v2f pos2d = read_v2f(L,2);
lua_Number val = noise2d_perlin(pos2d.X/o->scale, pos2d.Y/o->scale, o->seed, o->octaves, o->persistence);
v2f p = read_v2f(L, 2);
lua_Number val = NoisePerlin2D(&o->np, p.X, p.Y, 0);
lua_pushnumber(L, val);
return 1;
}
@ -47,23 +47,30 @@ int LuaPerlinNoise::l_get3d(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
LuaPerlinNoise *o = checkobject(L, 1);
v3f pos3d = read_v3f(L,2);
lua_Number val = noise3d_perlin(pos3d.X/o->scale, pos3d.Y/o->scale, pos3d.Z/o->scale, o->seed, o->octaves, o->persistence);
v3f p = read_v3f(L, 2);
lua_Number val = NoisePerlin3D(&o->np, p.X, p.Y, p.Z, 0);
lua_pushnumber(L, val);
return 1;
}
LuaPerlinNoise::LuaPerlinNoise(int a_seed, int a_octaves, float a_persistence,
float a_scale):
seed(a_seed),
octaves(a_octaves),
persistence(a_persistence),
scale(a_scale)
LuaPerlinNoise::LuaPerlinNoise(NoiseParams *params) :
np(*params)
{
}
/*
LuaPerlinNoise::LuaPerlinNoise(int a_seed, int a_octaves,
float a_persistence, float a_scale)
{
np.seed = a_seed;
np.octaves = a_octaves;
np.persist = a_persistence;
np.spread = v3f(a_scale, a_scale, a_scale);
}
*/
LuaPerlinNoise::~LuaPerlinNoise()
{
}
@ -74,11 +81,20 @@ LuaPerlinNoise::~LuaPerlinNoise()
int LuaPerlinNoise::create_object(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
int seed = luaL_checkint(L, 1);
int octaves = luaL_checkint(L, 2);
float persistence = luaL_checknumber(L, 3);
float scale = luaL_checknumber(L, 4);
LuaPerlinNoise *o = new LuaPerlinNoise(seed, octaves, persistence, scale);
NoiseParams params;
if (lua_istable(L, 1)) {
read_noiseparams(L, 1, &params);
} else {
params.seed = luaL_checkint(L, 1);
params.octaves = luaL_checkint(L, 2);
params.persist = luaL_checknumber(L, 3);
params.spread = v3f(1, 1, 1) * luaL_checknumber(L, 4);
}
LuaPerlinNoise *o = new LuaPerlinNoise(&params);
*(void **)(lua_newuserdata(L, sizeof(void *))) = o;
luaL_getmetatable(L, className);
lua_setmetatable(L, -2);

View file

@ -29,10 +29,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
*/
class LuaPerlinNoise : public ModApiBase {
private:
int seed;
int octaves;
float persistence;
float scale;
NoiseParams np;
static const char className[];
static const luaL_reg methods[];
@ -45,9 +43,7 @@ private:
static int l_get3d(lua_State *L);
public:
LuaPerlinNoise(int a_seed, int a_octaves, float a_persistence,
float a_scale);
LuaPerlinNoise(NoiseParams *params);
~LuaPerlinNoise();
// LuaPerlinNoise(seed, octaves, persistence, scale)