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 --- data/scripts/default.lua | 39 ++++++++------- src/content_sao.cpp | 7 ++- src/scriptapi.cpp | 120 ++++++++++++++++++++++++++++++++++++----------- src/scriptapi.h | 7 ++- 4 files changed, 121 insertions(+), 52 deletions(-) diff --git a/data/scripts/default.lua b/data/scripts/default.lua index c525ecf1e..005e9ac15 100644 --- a/data/scripts/default.lua +++ b/data/scripts/default.lua @@ -61,7 +61,7 @@ end]] return s end]] -function basic_serialize(o) +function basic_dump2(o) if type(o) == "number" then return tostring(o) elseif type(o) == "string" then @@ -70,6 +70,8 @@ function basic_serialize(o) return tostring(o) elseif type(o) == "function" then return "" + elseif type(o) == "userdata" then + return "" elseif type(o) == "nil" then return "nil" else @@ -78,13 +80,14 @@ function basic_serialize(o) end end -function serialize(o, name, dumped) +function dump2(o, name, dumped) name = name or "_" dumped = dumped or {} io.write(name, " = ") if type(o) == "number" or type(o) == "string" or type(o) == "boolean" - or type(o) == "function" or type(o) == "nil" then - io.write(basic_serialize(o), "\n") + or type(o) == "function" or type(o) == "nil" + or type(o) == "userdata" then + io.write(basic_dump2(o), "\n") elseif type(o) == "table" then if dumped[o] then io.write(dumped[o], "\n") @@ -92,8 +95,8 @@ function serialize(o, name, dumped) dumped[o] = name io.write("{}\n") -- new table for k,v in pairs(o) do - local fieldname = string.format("%s[%s]", name, basic_serialize(k)) - serialize(v, fieldname, dumped) + local fieldname = string.format("%s[%s]", name, basic_dump2(k)) + dump2(v, fieldname, dumped) end end else @@ -131,8 +134,6 @@ end print("omg lol") print("minetest dump: "..dump(minetest)) -minetest.register_entity("a", "dummy string"); - --local TNT = minetest.new_entity { local TNT = { -- Maybe handle gravity and collision this way? dunno @@ -148,30 +149,34 @@ local TNT = { } -- Called after object is created -function TNT:on_create(env) +function TNT:on_create() + print("TNT:on_create()") end -- Called periodically -function TNT:on_step(env, dtime) - self.timer = self.timer + dtime +function TNT:on_step(dtime) + print("TNT:on_step()") + --[[self.timer = self.timer + dtime if self.timer > 4.0 then self.to_be_deleted = true -- Environment will delete this object at a suitable point of execution env:explode(self.pos, 3) -- Uh... well, something like that - end + end]] end -- Called when object is punched -function TNT:on_punch(env, hitter) - -- If tool is bomb defuser, revert back to being a block +function TNT:on_punch(hitter) + print("TNT:on_punch()") + --[[-- If tool is bomb defuser, revert back to being a block local item = hitter.inventory.get_current() if item.itemtype == "tool" and item.param == "bomb_defuser" then env:add_node(self.pos, 3072) self.to_be_deleted = true - end + end]] end -- Called when object is right-clicked -function TNT:on_rightclick(self, env, hitter) +function TNT:on_rightclick(clicker) + print("TNT:on_rightclick()") end print("TNT dump: "..dump(TNT)) @@ -181,5 +186,5 @@ minetest.register_entity("TNT", TNT) --print("minetest.registered_entities: "..dump(minetest.registered_entities)) print("minetest.registered_entities:") -serialize(minetest.registered_entities) +dump2(minetest.registered_entities) 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="<