From: SmallJoker Date: Sat, 3 Mar 2018 09:59:43 +0000 (+0100) Subject: c_converter: Function template for numeric fields, add v3s16 default (#7090) X-Git-Tag: 5.0.0~557 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=ebbd158774853a2fae21cb3ee3787803b94d7b93;p=oweals%2Fminetest.git c_converter: Function template for numeric fields, add v3s16 default (#7090) --- diff --git a/src/script/common/c_converter.cpp b/src/script/common/c_converter.cpp index ce2548e83..0ee5afa3e 100644 --- a/src/script/common/c_converter.cpp +++ b/src/script/common/c_converter.cpp @@ -467,71 +467,6 @@ bool getstringfield(lua_State *L, int table, return got; } -bool getintfield(lua_State *L, int table, - const char *fieldname, int &result) -{ - lua_getfield(L, table, fieldname); - bool got = false; - if(lua_isnumber(L, -1)){ - result = lua_tointeger(L, -1); - got = true; - } - lua_pop(L, 1); - return got; -} - -bool getintfield(lua_State *L, int table, - const char *fieldname, u8 &result) -{ - lua_getfield(L, table, fieldname); - bool got = false; - if(lua_isnumber(L, -1)){ - result = lua_tointeger(L, -1); - got = true; - } - lua_pop(L, 1); - return got; -} - -bool getintfield(lua_State *L, int table, - const char *fieldname, s8 &result) -{ - lua_getfield(L, table, fieldname); - bool got = false; - if (lua_isnumber(L, -1)) { - result = lua_tointeger(L, -1); - got = true; - } - lua_pop(L, 1); - 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_tointeger(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_tointeger(L, -1); - got = true; - } - lua_pop(L, 1); - return got; -} - bool getfloatfield(lua_State *L, int table, const char *fieldname, float &result) { @@ -612,6 +547,13 @@ bool getboolfield_default(lua_State *L, int table, return result; } +v3s16 getv3s16field_default(lua_State *L, int table, + const char *fieldname, v3s16 default_) +{ + getv3intfield(L, table, fieldname, default_); + return default_; +} + void setstringfield(lua_State *L, int table, const char *fieldname, const char *value) { diff --git a/src/script/common/c_converter.h b/src/script/common/c_converter.h index 7827d8146..67b23f131 100644 --- a/src/script/common/c_converter.h +++ b/src/script/common/c_converter.h @@ -42,31 +42,51 @@ bool getboolfield_default(lua_State *L, int table, const char *fieldname, bool default_); float getfloatfield_default(lua_State *L, int table, const char *fieldname, float default_); -int getintfield_default (lua_State *L, int table, +int getintfield_default(lua_State *L, int table, const char *fieldname, int default_); +template +bool getintfield(lua_State *L, int table, + const char *fieldname, T &result) +{ + lua_getfield(L, table, fieldname); + bool got = false; + if (lua_isnumber(L, -1)){ + result = lua_tointeger(L, -1); + got = true; + } + lua_pop(L, 1); + return got; +} + +template +bool getv3intfield(lua_State *L, int index, + const char *fieldname, T &result) +{ + lua_getfield(L, index, fieldname); + bool got = false; + if (lua_istable(L, -1)) { + got = getintfield(L, index, "x", result.X) || + getintfield(L, index, "y", result.Y) || + getintfield(L, index, "z", result.Z); + } + lua_pop(L, 1); + return got; +} + +v3s16 getv3s16field_default(lua_State *L, int table, + const char *fieldname, v3s16 default_); bool getstringfield(lua_State *L, int table, const char *fieldname, std::string &result); size_t getstringlistfield(lua_State *L, int table, const char *fieldname, std::vector *result); -bool getintfield(lua_State *L, int table, - const char *fieldname, int &result); -bool getintfield(lua_State *L, int table, - const char *fieldname, u8 &result); -bool getintfield(lua_State *L, int table, - const char *fieldname, s8 &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::unordered_map &result); bool getboolfield(lua_State *L, int table, const char *fieldname, bool &result); bool getfloatfield(lua_State *L, int table, const char *fieldname, float &result); - std::string checkstringfield(lua_State *L, int table, const char *fieldname);