-- 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
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
--
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
/*
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
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},
{"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}
};
// 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");
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
*/
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);
}
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;
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