Add on_generated hook
authorPerttu Ahola <celeron55@gmail.com>
Sat, 26 Nov 2011 13:19:03 +0000 (15:19 +0200)
committerPerttu Ahola <celeron55@gmail.com>
Tue, 29 Nov 2011 17:13:54 +0000 (19:13 +0200)
data/mods/default/init.lua
src/mapgen.h
src/scriptapi.cpp
src/scriptapi.h
src/server.cpp

index 88f457e290a8c16b58665c15038110b77c09894a..6ea15b4ee984ea65a6335e3e6dd160caba871ecf 100644 (file)
@@ -15,6 +15,7 @@
 -- 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_generated(func(minp, maxp))
 -- minetest.register_on_newplayer(func(ObjectRef))
 -- minetest.register_on_respawnplayer(func(ObjectRef))
 -- ^ return true in func to disable regular player placement
@@ -1329,6 +1330,12 @@ minetest.register_on_respawnplayer(function(player)
        -- return true
 end)
 
+minetest.register_on_generated(function(minp, maxp)
+       --print("on_generated: minp="..dump(minp).." maxp="..dump(maxp))
+       --cp = {x=(minp.x+maxp.x)/2, y=(minp.y+maxp.y)/2, z=(minp.z+maxp.z)/2}
+       --minetest.env:add_node(cp, {name="sand"})
+end)
+
 -- Example setting get
 print("setting max_users = " .. dump(minetest.setting_get("max_users")))
 print("setting asdf = " .. dump(minetest.setting_get("asdf")))
index 55aede09969d3b514ed3eb2274cd7c777ee5a7cb..207dfdaf78cfdb336d7ef2970475038a86695816 100644 (file)
@@ -60,7 +60,7 @@ namespace mapgen
                u64 seed;
                v3s16 blockpos;
                UniqueQueue<v3s16> transforming_liquid;
-               INodeDefManager *nodedef; // Destructor deletes
+               INodeDefManager *nodedef;
 
                BlockMakeData();
                ~BlockMakeData();
index badf38181912b64fc85bbd676c8223ac5a4b159b..976b482c227317bd1b1fc3e2fd03442136f0583d 100644 (file)
@@ -843,6 +843,13 @@ static int l_register_on_punchnode(lua_State *L)
        return register_lua_callback(L, "registered_on_punchnodes");
 }
 
+// register_on_generated(function)
+static int l_register_on_generated(lua_State *L)
+{
+       infostream<<"register_on_generated"<<std::endl;
+       return register_lua_callback(L, "registered_on_generateds");
+}
+
 // register_on_newplayer(function)
 static int l_register_on_newplayer(lua_State *L)
 {
@@ -893,6 +900,7 @@ 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_generated", l_register_on_generated},
        {"register_on_newplayer", l_register_on_newplayer},
        {"register_on_respawnplayer", l_register_on_respawnplayer},
        {"setting_get", l_setting_get},
@@ -1391,6 +1399,8 @@ void scriptapi_export(lua_State *L, Server *server)
        lua_newtable(L);
        lua_setfield(L, -2, "registered_on_punchnodes");
        lua_newtable(L);
+       lua_setfield(L, -2, "registered_on_generateds");
+       lua_newtable(L);
        lua_setfield(L, -2, "registered_on_newplayers");
        lua_newtable(L);
        lua_setfield(L, -2, "registered_on_respawnplayers");
@@ -1694,6 +1704,32 @@ void scriptapi_environment_on_punchnode(lua_State *L, v3s16 p, MapNode node,
        }
 }
 
+void scriptapi_environment_on_generated(lua_State *L, v3s16 minp, v3s16 maxp)
+{
+       realitycheck(L);
+       assert(lua_checkstack(L, 20));
+       infostream<<"scriptapi_environment_on_generated"<<std::endl;
+       StackUnroller stack_unroller(L);
+
+       // Get minetest.registered_on_generateds
+       lua_getglobal(L, "minetest");
+       lua_getfield(L, -1, "registered_on_generateds");
+       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
+               pushpos(L, minp);
+               pushpos(L, maxp);
+               if(lua_pcall(L, 2, 0, 0))
+                       script_error(L, "error: %s\n", lua_tostring(L, -1));
+               // value removed, keep key for next iteration
+       }
+}
+
 /*
        luaentity
 */
index 08c83517238f914965640663ad892e49c4c33108..9bdf99c62879400a3358573f67a35ad297cd1a85 100644 (file)
@@ -49,6 +49,8 @@ void scriptapi_environment_on_dignode(lua_State *L, v3s16 p, MapNode oldnode,
 // When punching node
 void scriptapi_environment_on_punchnode(lua_State *L, v3s16 p, MapNode node,
                ServerActiveObject *puncher);
+// After generating a piece of map
+void scriptapi_environment_on_generated(lua_State *L, v3s16 minp, v3s16 maxp);
 
 /* misc */
 void scriptapi_on_newplayer(lua_State *L, ServerActiveObject *player);
index c9881567dfd54f77fb00307f014c8d6a3ee25259..49fdabd67da9ddbf752cbcd2f74d1bbdd1b6c132 100644 (file)
@@ -234,6 +234,11 @@ void * EmergeThread::Thread()
                                                if(enable_mapgen_debug_info)
                                                        infostream<<"EmergeThread: generating"<<std::endl;
                                                block = map.generateBlock(p, modified_blocks);
+                                               
+                                               v3s16 minp = block->getPos()*MAP_BLOCKSIZE;
+                                               v3s16 maxp = minp + v3s16(1,1,1)*(MAP_BLOCKSIZE-1);
+                                               scriptapi_environment_on_generated(m_server->m_lua,
+                                                               minp, maxp);
                                        }
                                }