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/scriptapi.cpp | 120 ++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 93 insertions(+), 27 deletions(-) (limited to 'src/scriptapi.cpp') diff --git a/src/scriptapi.cpp b/src/scriptapi.cpp index d055c1976..fc5364c87 100644 --- a/src/scriptapi.cpp +++ b/src/scriptapi.cpp @@ -76,21 +76,36 @@ static void realitycheck(lua_State *L) } } -// Register new object prototype (must be based on entity) +// Register new object prototype +// register_entity(name, prototype) static int l_register_entity(lua_State *L) { const char *name = luaL_checkstring(L, 1); - luaL_checkany(L, 2); + luaL_checktype(L, 2, LUA_TTABLE); infostream<<"register_entity: "< 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="<