From 4f2c1e36a10ee00c6779a02dd4441fa680fb3497 Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Fri, 6 Jan 2012 00:12:33 +0200 Subject: Implement minetest.register_on_dieplayer() --- src/scriptapi.cpp | 26 ++++++++++++++++++++++++++ src/scriptapi.h | 1 + src/server.cpp | 3 ++- 3 files changed, 29 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/scriptapi.cpp b/src/scriptapi.cpp index 35b597337..8321a4529 100644 --- a/src/scriptapi.cpp +++ b/src/scriptapi.cpp @@ -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); diff --git a/src/scriptapi.h b/src/scriptapi.h index d107b15ce..af8afa3d9 100644 --- a/src/scriptapi.h +++ b/src/scriptapi.h @@ -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); diff --git a/src/server.cpp b/src/server.cpp index d1a71bdbc..d704bf861 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -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){ -- cgit v1.2.3