From: Perttu Ahola Date: Mon, 21 Nov 2011 09:15:15 +0000 (+0200) Subject: Improve LuaEntity velocity/acceleration handling (by kahrl); implement staticdata... X-Git-Url: http://81.2.79.47:8989/gitweb/?a=commitdiff_plain;h=0ce0c8fcfba655c8db5f53ce8e3ab7adfa59768a;p=zefram%2Fminetest%2Fminetest_engine.git Improve LuaEntity velocity/acceleration handling (by kahrl); implement staticdata interface to Lua --- diff --git a/data/mods/default/init.lua b/data/mods/default/init.lua index 949816c9..a8a1d08f 100644 --- a/data/mods/default/init.lua +++ b/data/mods/default/init.lua @@ -101,6 +101,7 @@ end -- - add_node(pos, node) -- - remove_node(pos) -- - get_node(pos) +-- - add_luaentity(pos, name) -- -- ObjectRef is basically ServerActiveObject. -- ObjectRef methods: @@ -114,6 +115,12 @@ end -- - Functions receive a "luaentity" as self: -- - It has the member .object, which is an ObjectRef pointing to the object -- - The original prototype stuff is visible directly via a metatable +-- - Callbacks: +-- - on_activate(self, staticdata) +-- - on_step(self, dtime) +-- - on_punch(self, hitter) +-- - on_rightclick(self, clicker) +-- - get_staticdata(self): return string -- -- MapNode representation: -- {name="name", param1=num, param2=num} @@ -654,11 +661,20 @@ local TNT = { --textures = {"mese.png^[forcesingle"}, -- Initial value for our timer timer = 0, + -- Number of punches required to defuse + health = 3, -- List names of state variables, for serializing object state -- (NOTE: not implemented and implementation will not be like this) -- state_variables = {"timer"}, } +-- Called when a TNT object is created +function TNT:on_activate(staticdata) + print("TNT:on_activate()") + self.object:setvelocity({x=0, y=1, z=0}) + self.object:setacceleration({x=0, y=-5, z=0}) +end + -- Called periodically function TNT:on_step(dtime) --print("TNT:on_step()") @@ -667,15 +683,18 @@ end -- Called when object is punched function TNT:on_punch(hitter) print("TNT:on_punch()") - self.object:remove() - hitter:add_to_inventory("CraftItem testobject1 1") + self.health = self.health - 1 + if self.health <= 0 then + self.object:remove() + hitter:add_to_inventory("NodeItem TNT 1") + end end -- Called when object is right-clicked function TNT:on_rightclick(clicker) - pos = self.object:getpos() - pos = {x=pos.x, y=pos.y+0.1, z=pos.z} - self.object:moveto(pos, false) + --pos = self.object:getpos() + --pos = {x=pos.x, y=pos.y+0.1, z=pos.z} + --self.object:moveto(pos, false) end print("TNT dump: "..dump(TNT)) @@ -694,16 +713,13 @@ function register_falling_node(nodename, texture) visual = "cube", textures = {texture,texture,texture,texture,texture,texture}, -- State - fallspeed = 0, -- Methods on_step = function(self, dtime) - -- Apply gravity manually - self.fallspeed = self.fallspeed + dtime * 5 - fp = self.object:getpos() - fp.y = fp.y - self.fallspeed * dtime - self.object:moveto(fp, true) + -- Set gravity + self.object:setacceleration({x=0, y=-10, z=0}) -- Turn to actual sand when collides to ground or just move - bcp = {x=fp.x, y=fp.y-0.5, z=fp.z} -- Position of bottom center point + pos = self.object:getpos() + bcp = {x=pos.x, y=pos.y-0.5, z=pos.z} -- Position of bottom center point bcn = minetest.env:get_node(bcp) if bcn.name ~= "air" then -- Turn to a sand node @@ -772,6 +788,16 @@ function on_dignode(p, node) end minetest.register_on_dignode(on_dignode) +function on_punchnode(p, node) + print("on_punchnode") + if node.name == "TNT" then + minetest.env:remove_node(p) + minetest.env:add_luaentity(p, "TNT") + nodeupdate(p) + end +end +minetest.register_on_punchnode(on_punchnode) + -- -- Done, print some random stuff -- diff --git a/src/content_cao.cpp b/src/content_cao.cpp index ca9e2281..f5ef3fb0 100644 --- a/src/content_cao.cpp +++ b/src/content_cao.cpp @@ -1278,6 +1278,8 @@ LuaEntityCAO::LuaEntityCAO(IGameDef *gamedef): m_meshnode(NULL), m_spritenode(NULL), m_position(v3f(0,10*BS,0)), + m_velocity(v3f(0,0,0)), + m_acceleration(v3f(0,0,0)), m_yaw(0), m_prop(new LuaEntityProperties) { @@ -1455,6 +1457,9 @@ void LuaEntityCAO::updateNodePos() void LuaEntityCAO::step(float dtime, ClientEnvironment *env) { + m_position += dtime * m_velocity + 0.5 * dtime * dtime * m_acceleration; + m_velocity += dtime * m_acceleration; + pos_translator.update(m_position, pos_translator.aim_is_end, pos_translator.anim_time); pos_translator.translate(dtime); updateNodePos(); } @@ -1471,6 +1476,10 @@ void LuaEntityCAO::processMessage(const std::string &data) bool do_interpolate = readU8(is); // pos m_position = readV3F1000(is); + // velocity + m_velocity = readV3F1000(is); + // acceleration + m_acceleration = readV3F1000(is); // yaw m_yaw = readF1000(is); // is_end_position (for interpolation) diff --git a/src/content_cao.h b/src/content_cao.h index 3a3fbbcd..3f6b9d87 100644 --- a/src/content_cao.h +++ b/src/content_cao.h @@ -428,6 +428,8 @@ private: scene::IMeshSceneNode *m_meshnode; scene::MyBillboardSceneNode *m_spritenode; v3f m_position; + v3f m_velocity; + v3f m_acceleration; float m_yaw; struct LuaEntityProperties *m_prop; SmoothTranslator pos_translator; diff --git a/src/content_sao.cpp b/src/content_sao.cpp index dc112275..986e3f15 100644 --- a/src/content_sao.cpp +++ b/src/content_sao.cpp @@ -1550,6 +1550,8 @@ LuaEntitySAO::LuaEntitySAO(ServerEnvironment *env, v3f pos, m_init_state(state), m_registered(false), m_prop(new LuaEntityProperties), + m_velocity(0,0,0), + m_acceleration(0,0,0), m_yaw(0), m_last_sent_yaw(0), m_last_sent_position(0,0,0), @@ -1610,6 +1612,9 @@ void LuaEntitySAO::step(float dtime, bool send_recommended) { m_last_sent_position_timer += dtime; + m_base_position += dtime * m_velocity + 0.5 * dtime * dtime * m_acceleration; + m_velocity += dtime * m_acceleration; + if(m_registered){ lua_State *L = m_env->getLua(); scriptapi_luaentity_step(L, m_id, dtime); @@ -1618,6 +1623,7 @@ void LuaEntitySAO::step(float dtime, bool send_recommended) if(send_recommended == false) return; + // TODO: force send when velocity/acceleration changes enough float minchange = 0.2*BS; if(m_last_sent_position_timer > 1.0){ minchange = 0.01*BS; @@ -1659,7 +1665,7 @@ std::string LuaEntitySAO::getStaticData() // state if(m_registered){ lua_State *L = m_env->getLua(); - std::string state = scriptapi_luaentity_get_state(L, m_id); + std::string state = scriptapi_luaentity_get_staticdata(L, m_id); os<getSendRecommendedInterval(); @@ -1727,6 +1745,10 @@ void LuaEntitySAO::sendPosition(bool do_interpolate, bool is_movement_end) writeU8(os, do_interpolate); // pos writeV3F1000(os, m_base_position); + // velocity + writeV3F1000(os, m_velocity); + // acceleration + writeV3F1000(os, m_acceleration); // yaw writeF1000(os, m_yaw); // is_end_position (for interpolation) diff --git a/src/content_sao.h b/src/content_sao.h index cd747496..04d33647 100644 --- a/src/content_sao.h +++ b/src/content_sao.h @@ -216,6 +216,8 @@ public: void setPos(v3f pos); void moveTo(v3f pos, bool continuous); float getMinimumSavedMovement(); + void setVelocity(v3f velocity); + void setAcceleration(v3f acceleration); private: void sendPosition(bool do_interpolate, bool is_movement_end); @@ -224,6 +226,8 @@ private: bool m_registered; struct LuaEntityProperties *m_prop; + v3f m_velocity; + v3f m_acceleration; float m_yaw; float m_last_sent_yaw; v3f m_last_sent_position; diff --git a/src/scriptapi.cpp b/src/scriptapi.cpp index a18c144d..02db2ce0 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, <getMap().getNode(p_under); + } + catch(InvalidPositionException &e) + { + infostream<<"Server: Not punching: Node not found." + <<" Adding block to emerge queue." + <