From 41c91391fce65147aa7f3b5ceb7db5758709199a Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Sat, 12 Nov 2011 00:46:05 +0200 Subject: Scripting WIP; Lua entity step callback works --- src/content_sao.cpp | 7 ++- src/scriptapi.cpp | 120 ++++++++++++++++++++++++++++++++++++++++------------ src/scriptapi.h | 7 ++- 3 files changed, 99 insertions(+), 35 deletions(-) (limited to 'src') diff --git a/src/content_sao.cpp b/src/content_sao.cpp index d1303b471..3507ec154 100644 --- a/src/content_sao.cpp +++ b/src/content_sao.cpp @@ -1516,7 +1516,7 @@ LuaEntitySAO::~LuaEntitySAO() { if(m_registered){ lua_State *L = m_env->getLua(); - scriptapi_luaentity_deregister(L, m_id); + scriptapi_luaentity_rm(L, m_id); } } @@ -1527,7 +1527,7 @@ void LuaEntitySAO::addedToEnvironment(u16 id) // Create entity by name and state m_registered = true; lua_State *L = m_env->getLua(); - scriptapi_luaentity_register(L, id, m_init_name.c_str(), m_init_state.c_str()); + scriptapi_luaentity_add(L, id, m_init_name.c_str(), m_init_state.c_str()); } ServerActiveObject* LuaEntitySAO::create(ServerEnvironment *env, v3f pos, @@ -1553,7 +1553,7 @@ void LuaEntitySAO::step(float dtime, bool send_recommended) { if(m_registered){ lua_State *L = m_env->getLua(); - scriptapi_luaentity_step(L, m_id, dtime, send_recommended); + scriptapi_luaentity_step(L, m_id, dtime); } } @@ -1578,7 +1578,6 @@ std::string LuaEntitySAO::getStaticData() // state if(m_registered){ lua_State *L = m_env->getLua(); - scriptapi_luaentity_deregister(L, m_id); std::string state = scriptapi_luaentity_get_state(L, m_id); os< stack top + // registered_entities[name] = object + lua_setfield(L, registered_entities, name); + + // Get registered object to top of stack + lua_pushvalue(L, 2); + + // Set __index to point to itself + lua_pushvalue(L, -1); + lua_setfield(L, -2, "__index"); + + // Set metatable.__index = metatable + luaL_getmetatable(L, "minetest.entity"); + lua_pushvalue(L, -1); // duplicate metatable + lua_setfield(L, -2, "__index"); + // Set object metatable + lua_setmetatable(L, -2); return 0; /* number of results */ } @@ -286,36 +301,76 @@ void scriptapi_export(lua_State *L, Server *server) ObjectRef::Register(L); } -void scriptapi_luaentity_register(lua_State *L, u16 id, const char *name, +// Dump stack top with the dump2 function +static void dump2(lua_State *L, const char *name) +{ + // Dump object (debug) + lua_getglobal(L, "dump2"); + luaL_checktype(L, -1, LUA_TFUNCTION); + lua_pushvalue(L, -2); // Get previous stack top as first parameter + lua_pushstring(L, name); + if(lua_pcall(L, 2, 0, 0)) + script_error(L, "error: %s\n", lua_tostring(L, -1)); +} + +void scriptapi_luaentity_add(lua_State *L, u16 id, const char *name, const char *init_state) { realitycheck(L); assert(lua_checkstack(L, 20)); - infostream<<"scriptapi_luaentity_register: id="<