diff options
author | Perttu Ahola <celeron55@gmail.com> | 2011-11-26 03:20:19 +0200 |
---|---|---|
committer | Perttu Ahola <celeron55@gmail.com> | 2011-11-29 19:13:53 +0200 |
commit | 945d01fd9efe93e892a6a2be2467373e3c684300 (patch) | |
tree | ed5c1daac607992f4784b6085eeed3b28bfa73a4 /src | |
parent | d99d27dcbedee30e82f66ee76bf50b986ebfdc00 (diff) | |
download | minetest-945d01fd9efe93e892a6a2be2467373e3c684300.tar.gz minetest-945d01fd9efe93e892a6a2be2467373e3c684300.tar.bz2 minetest-945d01fd9efe93e892a6a2be2467373e3c684300.zip |
on_newplayer and on_respawnplayer callbacks
Diffstat (limited to 'src')
-rw-r--r-- | src/player.h | 6 | ||||
-rw-r--r-- | src/scriptapi.cpp | 93 | ||||
-rw-r--r-- | src/scriptapi.h | 4 | ||||
-rw-r--r-- | src/server.cpp | 26 |
4 files changed, 104 insertions, 25 deletions
diff --git a/src/player.h b/src/player.h index 56bb5083c..e133c7afb 100644 --- a/src/player.h +++ b/src/player.h @@ -208,6 +208,12 @@ public: u8 getType() const {return ACTIVEOBJECT_TYPE_PLAYER;} + + virtual void setPos(v3f pos) + { setPosition(pos); } + virtual void moveTo(v3f pos, bool continuous) + { setPosition(pos); } + virtual std::string getDescription(){return getName();} // Returns a reference virtual InventoryItem* getWieldedItem(); diff --git a/src/scriptapi.cpp b/src/scriptapi.cpp index 4a746c8f4..fd9d10195 100644 --- a/src/scriptapi.cpp +++ b/src/scriptapi.cpp @@ -41,9 +41,8 @@ extern "C" { /* TODO: -- Node definition - Random node triggers (like grass growth) -- Deterministic node triggers (like falling sand) +- All kinds of callbacks - Object visual client-side stuff - Blink effect - Spritesheets and animation @@ -848,6 +847,20 @@ static int l_register_on_punchnode(lua_State *L) return register_lua_callback(L, "registered_on_punchnodes"); } +// register_on_newplayer(function) +static int l_register_on_newplayer(lua_State *L) +{ + infostream<<"register_on_newplayer"<<std::endl; + return register_lua_callback(L, "registered_on_newplayers"); +} + +// register_on_respawnplayer(function) +static int l_register_on_respawnplayer(lua_State *L) +{ + infostream<<"register_on_respawnplayer"<<std::endl; + return register_lua_callback(L, "registered_on_respawnplayers"); +} + static const struct luaL_Reg minetest_f [] = { {"register_nodedef_defaults", l_register_nodedef_defaults}, {"register_entity", l_register_entity}, @@ -858,6 +871,8 @@ static const struct luaL_Reg minetest_f [] = { {"register_on_placenode", l_register_on_placenode}, {"register_on_dignode", l_register_on_dignode}, {"register_on_punchnode", l_register_on_punchnode}, + {"register_on_newplayer", l_register_on_newplayer}, + {"register_on_respawnplayer", l_register_on_respawnplayer}, {"get_nodedef", l_get_nodedef}, {NULL, NULL} }; @@ -1327,30 +1342,25 @@ void scriptapi_export(lua_State *L, Server *server) // Add tables to minetest - /*lua_newtable(L); - lua_setfield(L, -2, "registered_blocks");*/ - lua_newtable(L); lua_setfield(L, -2, "registered_nodes"); - lua_newtable(L); lua_setfield(L, -2, "registered_entities"); - lua_newtable(L); lua_setfield(L, -2, "registered_globalsteps"); - lua_newtable(L); lua_setfield(L, -2, "registered_on_placenodes"); - 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, "registered_on_newplayers"); + lua_newtable(L); + lua_setfield(L, -2, "registered_on_respawnplayers"); + lua_newtable(L); lua_setfield(L, -2, "object_refs"); - lua_newtable(L); lua_setfield(L, -2, "luaentities"); @@ -1455,6 +1465,65 @@ void scriptapi_rm_object_reference(lua_State *L, ServerActiveObject *cobj) lua_settable(L, objectstable); } + +/* + misc +*/ + +void scriptapi_on_newplayer(lua_State *L, ServerActiveObject *player) +{ + realitycheck(L); + assert(lua_checkstack(L, 20)); + StackUnroller stack_unroller(L); + + // Get minetest.registered_on_newplayers + lua_getglobal(L, "minetest"); + lua_getfield(L, -1, "registered_on_newplayers"); + 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\n", lua_tostring(L, -1)); + // value removed, keep key for next iteration + } +} +bool scriptapi_on_respawnplayer(lua_State *L, ServerActiveObject *player) +{ + realitycheck(L); + assert(lua_checkstack(L, 20)); + StackUnroller stack_unroller(L); + + bool positioning_handled_by_some = false; + + // Get minetest.registered_on_respawnplayers + lua_getglobal(L, "minetest"); + lua_getfield(L, -1, "registered_on_respawnplayers"); + 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, 1, 0)) + script_error(L, "error: %s\n", lua_tostring(L, -1)); + bool positioning_handled = lua_toboolean(L, -1); + lua_pop(L, 1); + if(positioning_handled) + positioning_handled_by_some = true; + // value removed, keep key for next iteration + } + return positioning_handled_by_some; +} + /* environment */ diff --git a/src/scriptapi.h b/src/scriptapi.h index 33f090f74..08c835172 100644 --- a/src/scriptapi.h +++ b/src/scriptapi.h @@ -50,6 +50,10 @@ void scriptapi_environment_on_dignode(lua_State *L, v3s16 p, MapNode oldnode, void scriptapi_environment_on_punchnode(lua_State *L, v3s16 p, MapNode node, ServerActiveObject *puncher); +/* misc */ +void scriptapi_on_newplayer(lua_State *L, ServerActiveObject *player); +bool scriptapi_on_respawnplayer(lua_State *L, ServerActiveObject *player); + /* luaentity */ // Returns true if succesfully added into Lua; false otherwise. bool scriptapi_luaentity_add(lua_State *L, u16 id, const char *name, diff --git a/src/server.cpp b/src/server.cpp index 6cb125aa8..78b7e79bd 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -4348,9 +4348,13 @@ void Server::HandlePlayerHP(Player *player, s16 damage) void Server::RespawnPlayer(Player *player) { - v3f pos = findSpawnPos(m_env->getServerMap()); - player->setPosition(pos); player->hp = 20; + ServerRemotePlayer *srp = (ServerRemotePlayer*)player; + bool repositioned = scriptapi_on_respawnplayer(m_lua, srp); + if(!repositioned){ + v3f pos = findSpawnPos(m_env->getServerMap()); + player->setPosition(pos); + } SendMovePlayer(player); SendPlayerHP(player); } @@ -4617,9 +4621,7 @@ Player *Server::emergePlayer(const char *name, const char *password, u16 peer_id m_authmanager.setPrivs(name, stringToPrivs(g_settings->get("default_privs"))); - /* - Set player position - */ + /* Set player position */ infostream<<"Server: Finding spawn place for player \"" <<name<<"\""<<std::endl; @@ -4628,16 +4630,14 @@ Player *Server::emergePlayer(const char *name, const char *password, u16 peer_id player = new ServerRemotePlayer(m_env, pos, peer_id, name); - /* - Add player to environment - */ - + /* Add player to environment */ m_env->addPlayer(player); - /* - Add stuff to inventory - */ - + /* Run scripts */ + ServerRemotePlayer *srp = (ServerRemotePlayer*)player; + scriptapi_on_newplayer(m_lua, srp); + + /* Add stuff to inventory */ if(g_settings->getBool("creative_mode")) { // Warning: double code above |