Implement minetest.register_on_dieplayer()
authorPerttu Ahola <celeron55@gmail.com>
Thu, 5 Jan 2012 22:12:33 +0000 (00:12 +0200)
committerPerttu Ahola <celeron55@gmail.com>
Thu, 5 Jan 2012 22:12:33 +0000 (00:12 +0200)
data/builtin.lua
data/mods/default/init.lua
src/scriptapi.cpp
src/scriptapi.h
src/server.cpp

index b2ce96881f676265fd46eefc2b097140dc98cc97..1046e934e5f566d32c2153fbe9be1a27c8187967 100644 (file)
@@ -435,6 +435,7 @@ minetest.registered_on_dignodes, minetest.register_on_dignode = make_registratio
 minetest.registered_on_punchnodes, minetest.register_on_punchnode = make_registration()
 minetest.registered_on_generateds, minetest.register_on_generated = make_registration()
 minetest.registered_on_newplayers, minetest.register_on_newplayer = make_registration()
+minetest.registered_on_dieplayers, minetest.register_on_dieplayer = make_registration()
 minetest.registered_on_respawnplayers, minetest.register_on_respawnplayer = make_registration()
 
 -- END
index 8de651efa9343cc6522f2084deaba18427f36815..aa03eabe8d342daf5cbbef52041ac1f3c10fb029 100644 (file)
 -- minetest.register_on_punchnode(func(pos, node, puncher))
 -- minetest.register_on_generated(func(minp, maxp))
 -- minetest.register_on_newplayer(func(ObjectRef))
+-- minetest.register_on_dieplayer(func(ObjectRef))
 -- minetest.register_on_respawnplayer(func(ObjectRef))
 -- ^ return true in func to disable regular player placement
+-- ^ currently called _before_ repositioning of player occurs
 -- minetest.register_on_chat_message(func(name, message))
 -- minetest.add_to_creative_inventory(itemstring)
 -- minetest.setting_get(name) -> string or nil
index 35b5973379e0414957ff9ef10b5a22bfda206270..8321a452971a410f4c80e2f5815e79b35fe6d7b3 100644 (file)
@@ -3638,6 +3638,32 @@ void scriptapi_on_newplayer(lua_State *L, ServerActiveObject *player)
                // value removed, keep key for next iteration
        }
 }
+
+void scriptapi_on_dieplayer(lua_State *L, ServerActiveObject *player)
+{
+    realitycheck(L);
+    assert(lua_checkstack(L, 20));
+    StackUnroller stack_unroller(L);
+    
+    // Get minetest.registered_on_dieplayers
+    lua_getglobal(L, "minetest");
+    lua_getfield(L, -1, "registered_on_dieplayers");
+    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
+       objectref_get_or_create(L, player);
+        if(lua_pcall(L, 1, 0, 0))
+            script_error(L, "error: %s", lua_tostring(L, -1));
+        // value removed, keep key for next iteration
+    }
+}
+
+
 bool scriptapi_on_respawnplayer(lua_State *L, ServerActiveObject *player)
 {
        realitycheck(L);
index d107b15ce82904696d53b173ca8e93c5f8822495..af8afa3d9d30b2db480d244611d91098045fb694 100644 (file)
@@ -62,6 +62,7 @@ void scriptapi_environment_on_generated(lua_State *L, v3s16 minp, v3s16 maxp);
 
 /* misc */
 void scriptapi_on_newplayer(lua_State *L, ServerActiveObject *player);
+void scriptapi_on_dieplayer(lua_State *L, ServerActiveObject *player);
 bool scriptapi_on_respawnplayer(lua_State *L, ServerActiveObject *player);
 void scriptapi_get_creative_inventory(lua_State *L, ServerRemotePlayer *player);
 
index d1a71bdbc75f605a114911d63e98831f41ebe565..d704bf861bdaad04c1c903a9413911b9ddac5c5a 100644 (file)
@@ -4537,7 +4537,8 @@ void Server::HandlePlayerHP(Player *player, s16 damage)
        
        player->hp = 0;
        
-       //TODO: Throw items around
+       // Trigger scripted stuff
+       scriptapi_on_dieplayer(m_lua, srp);
        
        // Handle players that are not connected
        if(player->peer_id == PEER_ID_INEXISTENT){