From cc03718d3c492a401bbc4b071d0ae1f0c808de95 Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Thu, 17 Nov 2011 11:22:24 +0200 Subject: Node place/dig Lua callbacks --- src/scriptapi.cpp | 218 ++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 189 insertions(+), 29 deletions(-) (limited to 'src/scriptapi.cpp') diff --git a/src/scriptapi.cpp b/src/scriptapi.cpp index cd501060f..c9356fe4f 100644 --- a/src/scriptapi.cpp +++ b/src/scriptapi.cpp @@ -123,7 +123,7 @@ public: } }; -v3f readFloatPos(lua_State *L, int index) +static v3f readFloatPos(lua_State *L, int index) { v3f pos; lua_pushvalue(L, index); // Push pos @@ -142,6 +142,42 @@ v3f readFloatPos(lua_State *L, int index) return pos; } +static void pushpos(lua_State *L, v3s16 p) +{ + lua_newtable(L); + lua_pushnumber(L, p.X); + lua_setfield(L, -2, "x"); + lua_pushnumber(L, p.Y); + lua_setfield(L, -2, "y"); + lua_pushnumber(L, p.Z); + lua_setfield(L, -2, "z"); +} + +static void pushnode(lua_State *L, const MapNode &n, INodeDefManager *ndef) +{ + lua_newtable(L); + lua_pushstring(L, ndef->get(n).name.c_str()); + lua_setfield(L, -2, "name"); + lua_pushnumber(L, n.getParam1()); + lua_setfield(L, -2, "param1"); + lua_pushnumber(L, n.getParam2()); + lua_setfield(L, -2, "param2"); +} + +static MapNode readnode(lua_State *L, int index, INodeDefManager *ndef) +{ + lua_getfield(L, index, "name"); + const char *name = lua_tostring(L, -1); + lua_pop(L, 1); + lua_getfield(L, index, "param1"); + u8 param1 = lua_tonumber(L, -1); + lua_pop(L, 1); + lua_getfield(L, index, "param2"); + u8 param2 = lua_tonumber(L, -1); + lua_pop(L, 1); + return MapNode(ndef, name, param1, param2); +} + /* Global functions */ @@ -180,32 +216,6 @@ static int l_register_entity(lua_State *L) return 0; /* number of results */ } -// Register a global step function -// register_globalstep(function) -static int l_register_globalstep(lua_State *L) -{ - luaL_checktype(L, 1, LUA_TFUNCTION); - infostream<<"register_globalstep"<getWritableNodeDefManager(); + + // Get minetest.registered_on_placenodes + lua_getglobal(L, "minetest"); + lua_getfield(L, -1, "registered_on_placenodes"); + luaL_checktype(L, -1, LUA_TTABLE); + int table = lua_gettop(L); + // Foreach + lua_pushnil(L); + while(lua_next(L, table) != 0){ + // key at index -2 and value at index -1 + luaL_checktype(L, -1, LUA_TFUNCTION); + // Call function + pushpos(L, p); + pushnode(L, newnode, ndef); + if(lua_pcall(L, 2, 0, 0)) + script_error(L, "error: %s\n", lua_tostring(L, -1)); + // value removed, keep key for next iteration + } +} + +void scriptapi_environment_on_dignode(lua_State *L, v3s16 p, MapNode oldnode) +{ + realitycheck(L); + assert(lua_checkstack(L, 20)); + //infostream<<"scriptapi_environment_on_dignode"<getWritableNodeDefManager(); + + // Get minetest.registered_on_dignodes + lua_getglobal(L, "minetest"); + lua_getfield(L, -1, "registered_on_dignodes"); + luaL_checktype(L, -1, LUA_TTABLE); + int table = lua_gettop(L); + // Foreach + lua_pushnil(L); + while(lua_next(L, table) != 0){ + // key at index -2 and value at index -1 + luaL_checktype(L, -1, LUA_TFUNCTION); + // Call function + pushpos(L, p); + pushnode(L, oldnode, ndef); + if(lua_pcall(L, 2, 0, 0)) + script_error(L, "error: %s\n", lua_tostring(L, -1)); + // value removed, keep key for next iteration + } +} + /* luaentity */ -- cgit v1.2.3