From 0ce0c8fcfba655c8db5f53ce8e3ab7adfa59768a Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Mon, 21 Nov 2011 11:15:15 +0200 Subject: Improve LuaEntity velocity/acceleration handling (by kahrl); implement staticdata interface to Lua --- src/scriptapi.cpp | 192 ++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 128 insertions(+), 64 deletions(-) (limited to 'src/scriptapi.cpp') diff --git a/src/scriptapi.cpp b/src/scriptapi.cpp index a18c144d3..02db2ce02 100644 --- a/src/scriptapi.cpp +++ b/src/scriptapi.cpp @@ -61,6 +61,7 @@ TODO: meta.set("owner", playername) meta.get("owner") - Item definition (actually, only CraftItem) +- (not scripting) Putting items in node metadata (virtual) */ static void stackDump(lua_State *L, std::ostream &o) @@ -424,24 +425,21 @@ static int l_register_craft(lua_State *L) return 0; /* number of results */ } -// Register a global step function -// register_globalstep(function) -static int l_register_globalstep(lua_State *L) +static int register_lua_callback(lua_State *L, const char *tablename) { luaL_checktype(L, 1, LUA_TFUNCTION); - infostream<<"register_globalstep"<getGameDef()->ndef()); // Do it - env->getMap().addNodeWithEvent(pos, n); - return 0; + bool succeeded = env->getMap().addNodeWithEvent(pos, n); + lua_pushboolean(L, succeeded); + return 1; } // EnvRef:remove_node(pos) @@ -598,8 +577,9 @@ private: // pos v3s16 pos = readpos(L, 2); // Do it - env->getMap().removeNodeWithEvent(pos); - return 0; + bool succeeded = env->getMap().removeNodeWithEvent(pos); + lua_pushboolean(L, succeeded); + return 1; } // EnvRef:get_node(pos) @@ -810,6 +790,32 @@ private: return 0; } + // setvelocity(self, velocity) + static int l_setvelocity(lua_State *L) + { + ObjectRef *ref = checkobject(L, 1); + LuaEntitySAO *co = getluaobject(ref); + if(co == NULL) return 0; + // pos + v3f pos = readFloatPos(L, 2); + // Do it + co->setVelocity(pos); + return 0; + } + + // setacceleration(self, acceleration) + static int l_setacceleration(lua_State *L) + { + ObjectRef *ref = checkobject(L, 1); + LuaEntitySAO *co = getluaobject(ref); + if(co == NULL) return 0; + // pos + v3f pos = readFloatPos(L, 2); + // Do it + co->setAcceleration(pos); + return 0; + } + // add_to_inventory(self, itemstring) // returns: true if item was added, false otherwise static int l_add_to_inventory(lua_State *L) @@ -902,6 +908,8 @@ const luaL_reg ObjectRef::methods[] = { method(ObjectRef, getpos), method(ObjectRef, setpos), method(ObjectRef, moveto), + method(ObjectRef, setvelocity), + method(ObjectRef, setacceleration), method(ObjectRef, add_to_inventory), {0,0} }; @@ -957,6 +965,9 @@ void scriptapi_export(lua_State *L, Server *server) lua_newtable(L); lua_setfield(L, -2, "registered_on_dignodes"); + lua_newtable(L); + lua_setfield(L, -2, "registered_on_punchnodes"); + lua_newtable(L); lua_setfield(L, -2, "object_refs"); @@ -1159,12 +1170,45 @@ void scriptapi_environment_on_dignode(lua_State *L, v3s16 p, MapNode oldnode) } } +void scriptapi_environment_on_punchnode(lua_State *L, v3s16 p, MapNode oldnode) +{ + realitycheck(L); + assert(lua_checkstack(L, 20)); + //infostream<<"scriptapi_environment_on_punchnode"<getWritableNodeDefManager(); + + // Get minetest.registered_on_punchnodes + lua_getglobal(L, "minetest"); + lua_getfield(L, -1, "registered_on_punchnodes"); + 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 */ bool scriptapi_luaentity_add(lua_State *L, u16 id, const char *name, - const char *init_state) + const std::string &staticdata) { realitycheck(L); assert(lua_checkstack(L, 20)); @@ -1172,8 +1216,6 @@ bool scriptapi_luaentity_add(lua_State *L, u16 id, const char *name, <