aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/script/lua_api/l_env.cpp18
-rw-r--r--src/script/lua_api/l_noise.cpp46
-rw-r--r--src/script/lua_api/l_noise.h10
3 files changed, 47 insertions, 27 deletions
diff --git a/src/script/lua_api/l_env.cpp b/src/script/lua_api/l_env.cpp
index 8f1f851d7..3d2e20424 100644
--- a/src/script/lua_api/l_env.cpp
+++ b/src/script/lua_api/l_env.cpp
@@ -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);
diff --git a/src/script/lua_api/l_noise.cpp b/src/script/lua_api/l_noise.cpp
index 6c6b35358..f4ae3fb08 100644
--- a/src/script/lua_api/l_noise.cpp
+++ b/src/script/lua_api/l_noise.cpp
@@ -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);
diff --git a/src/script/lua_api/l_noise.h b/src/script/lua_api/l_noise.h
index 6f6173fc2..6e3029aef 100644
--- a/src/script/lua_api/l_noise.h
+++ b/src/script/lua_api/l_noise.h
@@ -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)