diff --git a/builtin/common/item_s.lua b/builtin/common/item_s.lua index 72a722ed1210..e16152429144 100644 --- a/builtin/common/item_s.lua +++ b/builtin/common/item_s.lua @@ -239,3 +239,18 @@ if core.set_read_node and core.set_push_node then core.set_push_node(push_node) core.set_push_node = nil end + +if INIT == "game" then + local get_node_raw = core.get_node_raw + core.get_node_raw = nil + + function core.get_node(pos) + local content, param1, param2 = get_node_raw(pos.x, pos.y, pos.z) + return {name = content2name[content], param1 = param1, param2 = param2} + end + + function core.get_node_or_nil(pos) + local content, param1, param2, pos_ok = get_node_raw(pos.x, pos.y, pos.z) + return pos_ok and {name = content2name[content], param1 = param1, param2 = param2} or nil + end +end diff --git a/src/script/lua_api/l_env.cpp b/src/script/lua_api/l_env.cpp index 39f36a31a104..0715928a72f6 100644 --- a/src/script/lua_api/l_env.cpp +++ b/src/script/lua_api/l_env.cpp @@ -324,39 +324,25 @@ int ModApiEnv::l_swap_node(lua_State *L) return 1; } -// get_node(pos) -// pos = {x=num, y=num, z=num} -int ModApiEnv::l_get_node(lua_State *L) -{ - GET_ENV_PTR; - - // pos - v3s16 pos = read_v3s16(L, 1); - // Do it - MapNode n = env->getMap().getNode(pos); - // Return node - pushnode(L, n); - return 1; -} - -// get_node_or_nil(pos) -// pos = {x=num, y=num, z=num} -int ModApiEnv::l_get_node_or_nil(lua_State *L) +// get_node_raw(x, y, z) -> content, param1, param2, pos_ok +int ModApiEnv::l_get_node_raw(lua_State *L) { GET_ENV_PTR; // pos - v3s16 pos = read_v3s16(L, 1); + double x = lua_tonumber(L, 1); + double y = lua_tonumber(L, 2); + double z = lua_tonumber(L, 3); + v3s16 pos = doubleToInt(v3d(x, y, z), 1.0); // Do it bool pos_ok; MapNode n = env->getMap().getNode(pos, &pos_ok); - if (pos_ok) { - // Return node - pushnode(L, n); - } else { - lua_pushnil(L); - } - return 1; + // Return node and pos_ok + lua_pushinteger(L, n.getContent()); + lua_pushinteger(L, n.getParam1()); + lua_pushinteger(L, n.getParam2()); + lua_pushboolean(L, pos_ok); + return 4; } // get_node_light(pos, timeofday) @@ -1479,8 +1465,7 @@ void ModApiEnv::Initialize(lua_State *L, int top) API_FCT(swap_node); API_FCT(add_item); API_FCT(remove_node); - API_FCT(get_node); - API_FCT(get_node_or_nil); + API_FCT(get_node_raw); API_FCT(get_node_light); API_FCT(get_natural_light); API_FCT(place_node); diff --git a/src/script/lua_api/l_env.h b/src/script/lua_api/l_env.h index 327cf9f75d3f..68be6e75138d 100644 --- a/src/script/lua_api/l_env.h +++ b/src/script/lua_api/l_env.h @@ -74,13 +74,9 @@ class ModApiEnv : public ModApiEnvBase { // pos = {x=num, y=num, z=num} static int l_swap_node(lua_State *L); - // get_node(pos) - // pos = {x=num, y=num, z=num} - static int l_get_node(lua_State *L); - - // get_node_or_nil(pos) - // pos = {x=num, y=num, z=num} - static int l_get_node_or_nil(lua_State *L); + // get_node_raw(x, y, z) -> content, param1, param2, pos_ok + // Used to implement get_node and get_node_or_nil in lua. + static int l_get_node_raw(lua_State *L); // get_node_light(pos, timeofday) // pos = {x=num, y=num, z=num} @@ -245,7 +241,7 @@ class ModApiEnv : public ModApiEnvBase { /* * Duplicates of certain env APIs that operate not on the global - * map but on a VoxelManipulator. This is for emerge scripting. + * map but on a VoxelManipulator. This is for emerge scripting. */ class ModApiEnvVM : public ModApiEnvBase { private: