]> git.lizzy.rs Git - dragonfireclient.git/commitdiff
Add 'persistence' alias for Lua noiseparams and validate more vector parameters
authorkwolekr <kwolekr@minetest.net>
Mon, 20 Apr 2015 01:42:40 +0000 (21:42 -0400)
committerkwolekr <kwolekr@minetest.net>
Mon, 20 Apr 2015 01:42:40 +0000 (21:42 -0400)
src/script/common/c_content.cpp
src/script/common/c_converter.cpp
src/script/common/c_converter.h
src/script/lua_api/l_noise.cpp

index 8bb22186b9bf2ea86b2347a26edc068d0b600950..8f82b692a689fd81561b3d9188d210488258fa94 100644 (file)
@@ -983,14 +983,16 @@ bool read_noiseparams(lua_State *L, int index, NoiseParams *np)
        if (!lua_istable(L, index))
                return false;
 
-       np->offset     = getfloatfield_default(L, index, "offset",     0.0);
-       np->scale      = getfloatfield_default(L, index, "scale",      0.0);
-       np->persist    = getfloatfield_default(L, index, "persist",    0.0);
-       np->lacunarity = getfloatfield_default(L, index, "lacunarity", 2.0);
-       np->seed       = getintfield_default(L,   index, "seed",       0);
-       np->octaves    = getintfield_default(L,   index, "octaves",    0);
-
-       u32 flags = 0, flagmask = 0;
+       getfloatfield(L, index, "offset",      np->offset);
+       getfloatfield(L, index, "scale",       np->scale);
+       getfloatfield(L, index, "persist",     np->persist);
+       getfloatfield(L, index, "persistence", np->persist);
+       getfloatfield(L, index, "lacunarity",  np->lacunarity);
+       getintfield(L,   index, "seed",        np->seed);
+       getintfield(L,   index, "octaves",     np->octaves);
+
+       u32 flags    = 0;
+       u32 flagmask = 0;
        np->flags = getflagsfield(L, index, "flags", flagdesc_noiseparams,
                &flags, &flagmask) ? flags : NOISE_FLAG_DEFAULTS;
 
index 5070dca2deb4fc42e8e066030e79e6fcd2d02a26..755485ee4985f3a041752d392a3812df36795361 100644 (file)
@@ -59,6 +59,19 @@ v2s16 read_v2s16(lua_State *L, int index)
        return p;
 }
 
+v2s16 check_v2s16(lua_State *L, int index)
+{
+       v2s16 p;
+       luaL_checktype(L, index, LUA_TTABLE);
+       lua_getfield(L, index, "x");
+       p.X = luaL_checknumber(L, -1);
+       lua_pop(L, 1);
+       lua_getfield(L, index, "y");
+       p.Y = luaL_checknumber(L, -1);
+       lua_pop(L, 1);
+       return p;
+}
+
 v2s32 read_v2s32(lua_State *L, int index)
 {
        v2s32 p;
@@ -85,6 +98,19 @@ v2f read_v2f(lua_State *L, int index)
        return p;
 }
 
+v2f check_v2f(lua_State *L, int index)
+{
+       v2f p;
+       luaL_checktype(L, index, LUA_TTABLE);
+       lua_getfield(L, index, "x");
+       p.X = luaL_checknumber(L, -1);
+       lua_pop(L, 1);
+       lua_getfield(L, index, "y");
+       p.Y = luaL_checknumber(L, -1);
+       lua_pop(L, 1);
+       return p;
+}
+
 v3f read_v3f(lua_State *L, int index)
 {
        v3f pos;
@@ -285,6 +311,32 @@ bool getintfield(lua_State *L, int table,
        return got;
 }
 
+bool getintfield(lua_State *L, int table,
+               const char *fieldname, u16 &result)
+{
+       lua_getfield(L, table, fieldname);
+       bool got = false;
+       if(lua_isnumber(L, -1)){
+               result = lua_tonumber(L, -1);
+               got = true;
+       }
+       lua_pop(L, 1);
+       return got;
+}
+
+bool getintfield(lua_State *L, int table,
+               const char *fieldname, u32 &result)
+{
+       lua_getfield(L, table, fieldname);
+       bool got = false;
+       if(lua_isnumber(L, -1)){
+               result = lua_tonumber(L, -1);
+               got = true;
+       }
+       lua_pop(L, 1);
+       return got;
+}
+
 bool getfloatfield(lua_State *L, int table,
                const char *fieldname, float &result)
 {
index fdcd0c8fcad0fbc55fab66fc15eca51f75060204..1d5be697121527d9ffe36aa58ba97a0b839e00a8 100644 (file)
@@ -53,6 +53,10 @@ size_t             getstringlistfield(lua_State *L, int table,
                              std::vector<std::string> *result);
 bool               getintfield(lua_State *L, int table,
                              const char *fieldname, int &result);
+bool               getintfield(lua_State *L, int table,
+                             const char *fieldname, u16 &result);
+bool               getintfield(lua_State *L, int table,
+                             const char *fieldname, u32 &result);
 void               read_groups(lua_State *L, int index,
                              std::map<std::string, int> &result);
 bool               getboolfield(lua_State *L, int table,
@@ -72,6 +76,8 @@ void               setboolfield(lua_State *L, int table,
 
 
 v3f                 checkFloatPos       (lua_State *L, int index);
+v2f                 check_v2f           (lua_State *L, int index);
+v2s16               check_v2s16         (lua_State *L, int index);
 v3f                 check_v3f           (lua_State *L, int index);
 v3s16               check_v3s16         (lua_State *L, int index);
 
index bf3dca58968f4f3accdc42ae9a738fa3de4cd3da..84f8875b803f3ff618f072139464d5e3bf330879 100644 (file)
@@ -43,7 +43,7 @@ int LuaPerlinNoise::l_get2d(lua_State *L)
 {
        NO_MAP_LOCK_REQUIRED;
        LuaPerlinNoise *o = checkobject(L, 1);
-       v2f p = read_v2f(L, 2);
+       v2f p = check_v2f(L, 2);
        lua_Number val = NoisePerlin2D(&o->np, p.X, p.Y, 0);
        lua_pushnumber(L, val);
        return 1;
@@ -54,7 +54,7 @@ int LuaPerlinNoise::l_get3d(lua_State *L)
 {
        NO_MAP_LOCK_REQUIRED;
        LuaPerlinNoise *o = checkobject(L, 1);
-       v3f p = read_v3f(L, 2);
+       v3f p = check_v3f(L, 2);
        lua_Number val = NoisePerlin3D(&o->np, p.X, p.Y, p.Z, 0);
        lua_pushnumber(L, val);
        return 1;
@@ -168,7 +168,7 @@ int LuaPerlinNoiseMap::l_get2dMap(lua_State *L)
        size_t i = 0;
 
        LuaPerlinNoiseMap *o = checkobject(L, 1);
-       v2f p = read_v2f(L, 2);
+       v2f p = check_v2f(L, 2);
 
        Noise *n = o->noise;
        n->perlinMap2D(p.X, p.Y);
@@ -191,7 +191,7 @@ int LuaPerlinNoiseMap::l_get2dMap_flat(lua_State *L)
        NO_MAP_LOCK_REQUIRED;
 
        LuaPerlinNoiseMap *o = checkobject(L, 1);
-       v2f p = read_v2f(L, 2);
+       v2f p = check_v2f(L, 2);
 
        Noise *n = o->noise;
        n->perlinMap2D(p.X, p.Y);
@@ -213,7 +213,7 @@ int LuaPerlinNoiseMap::l_get3dMap(lua_State *L)
        size_t i = 0;
 
        LuaPerlinNoiseMap *o = checkobject(L, 1);
-       v3f p = read_v3f(L, 2);
+       v3f p = check_v3f(L, 2);
 
        if (!o->m_is3d)
                return 0;
@@ -243,7 +243,7 @@ int LuaPerlinNoiseMap::l_get3dMap_flat(lua_State *L)
        NO_MAP_LOCK_REQUIRED;
 
        LuaPerlinNoiseMap *o = checkobject(L, 1);
-       v3f p = read_v3f(L, 2);
+       v3f p = check_v3f(L, 2);
 
        if (!o->m_is3d)
                return 0;