From 945d01fd9efe93e892a6a2be2467373e3c684300 Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Sat, 26 Nov 2011 03:20:19 +0200 Subject: [PATCH] on_newplayer and on_respawnplayer callbacks --- data/mods/default/init.lua | 72 ++++------------------------- src/player.h | 6 +++ src/scriptapi.cpp | 93 +++++++++++++++++++++++++++++++++----- src/scriptapi.h | 4 ++ src/server.cpp | 26 +++++------ 5 files changed, 114 insertions(+), 87 deletions(-) diff --git a/data/mods/default/init.lua b/data/mods/default/init.lua index 29ec6abb8..6512f55eb 100644 --- a/data/mods/default/init.lua +++ b/data/mods/default/init.lua @@ -15,6 +15,9 @@ -- minetest.register_on_placenode(func(pos, newnode, placer)) -- minetest.register_on_dignode(func(pos, oldnode, digger)) -- minetest.register_on_punchnode(func(pos, node, puncher)) +-- minetest.register_on_newplayer(func(ObjectRef)) +-- minetest.register_on_respawnplayer(func(ObjectRef)) +-- ^ return true in func to disable regular player placement -- -- Global objects: -- minetest.env - environment reference @@ -1304,6 +1307,12 @@ function on_punchnode(p, node) end minetest.register_on_punchnode(on_punchnode) +minetest.register_on_respawnplayer(function(player) + print("on_respawnplayer") + -- player:setpos({x=0, y=30, z=0}) + -- return true +end) + -- -- Done, print some random stuff -- @@ -1311,65 +1320,4 @@ minetest.register_on_punchnode(on_punchnode) print("minetest.registered_entities:") dump2(minetest.registered_entities) --- --- Some random pre-implementation planning and drafting --- - ---[[ -function TNT:on_rightclick(clicker) - print("TNT:on_rightclick()") - print("self: "..dump(self)) - print("getmetatable(self): "..dump(getmetatable(self))) - print("getmetatable(getmetatable(self)): "..dump(getmetatable(getmetatable(self)))) - pos = self.object:getpos() - print("TNT:on_rightclick(): object position: "..dump(pos)) - pos = {x=pos.x+0.5+1, y=pos.y+0.5, z=pos.z+0.5} - --minetest.env:add_node(pos, {name="stone") -end ---]] - ---[=[ - -register_block(0, { - textures = "stone.png", - makefacetype = 0, - get_dig_duration = function(env, pos, digger) - -- Check stuff like digger.current_tool - return 1.5 - end, - on_dig = function(env, pos, digger) - env:remove_node(pos) - digger.inventory.put("MaterialItem2 0"); - end, -}) - -register_block(1, { - textures = {"grass.png","mud.png","mud_grass_side.png","mud_grass_side.png","mud_grass_side.png","mud_grass_side.png"}, - makefacetype = 0, - get_dig_duration = function(env, pos, digger) - -- Check stuff like digger.current_tool - return 0.5 - end, - on_dig = function(env, pos, digger) - env:remove_node(pos) - digger.inventory.put("MaterialItem2 1"); - end, -}) - --- Consider the "miscellaneous block namespace" to be 0xc00...0xfff = 3072...4095 -register_block(3072, { - textures = {"tnt_top.png","tnt_bottom.png","tnt_side.png","tnt_side.png","tnt_side.png","tnt_side.png"}, - makefacetype = 0, - get_dig_duration = function(env, pos, digger) - -- Cannot be dug - return nil - end, - -- on_dig = function(env, pos, digger) end, -- Not implemented - on_hit = function(env, pos, hitter) - -- Replace with TNT object, which will explode after timer, follow gravity, blink and stuff - env:add_object("tnt", pos) - env:remove_node(pos) - end, -}) ---]=] - +-- END 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"<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 \"" <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