diff options
author | kwolekr <kwolekr@minetest.net> | 2013-06-27 17:06:52 -0400 |
---|---|---|
committer | kwolekr <kwolekr@minetest.net> | 2013-06-27 22:35:35 -0400 |
commit | 2e292b67a0a02b045969034c06aaf92b42a83a81 (patch) | |
tree | 499310d109cbd737633eb99a245e0054a0c458d7 /src/script | |
parent | 18882a4d2603488bdfb5a519a8bedf300b154940 (diff) | |
download | minetest-2e292b67a0a02b045969034c06aaf92b42a83a81.tar.gz minetest-2e292b67a0a02b045969034c06aaf92b42a83a81.tar.bz2 minetest-2e292b67a0a02b045969034c06aaf92b42a83a81.zip |
Add Lua on_mapgen_init callback, and minetest.set_mapgen_params API
Diffstat (limited to 'src/script')
-rw-r--r-- | src/script/cpp_api/s_env.cpp | 28 | ||||
-rw-r--r-- | src/script/cpp_api/s_env.h | 3 | ||||
-rw-r--r-- | src/script/lua_api/l_env.cpp | 54 | ||||
-rw-r--r-- | src/script/lua_api/l_env.h | 4 |
4 files changed, 89 insertions, 0 deletions
diff --git a/src/script/cpp_api/s_env.cpp b/src/script/cpp_api/s_env.cpp index 334d196bc..632b28f45 100644 --- a/src/script/cpp_api/s_env.cpp +++ b/src/script/cpp_api/s_env.cpp @@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "common/c_converter.h" #include "log.h" #include "environment.h" +#include "mapgen.h" #include "lua_api/l_env.h" extern "C" { @@ -55,6 +56,33 @@ void ScriptApiEnv::environment_Step(float dtime) runCallbacks(1, RUN_CALLBACKS_MODE_FIRST); } +void ScriptApiEnv::environment_OnMapgenInit(MapgenParams *mgparams) +{ + SCRIPTAPI_PRECHECKHEADER + + // Get minetest.registered_on_mapgen_inits + lua_getglobal(L, "minetest"); + lua_getfield(L, -1, "registered_on_mapgen_inits"); + + // Call callbacks + lua_newtable(L); + + lua_pushstring(L, mgparams->mg_name.c_str()); + lua_setfield(L, -2, "mgname"); + + lua_pushinteger(L, mgparams->seed); + lua_setfield(L, -2, "seed"); + + lua_pushinteger(L, mgparams->water_level); + lua_setfield(L, -2, "water_level"); + + std::string flagstr = writeFlagString(mgparams->flags, flagdesc_mapgen); + lua_pushstring(L, flagstr.c_str()); + lua_setfield(L, -2, "flags"); + + runCallbacks(1, RUN_CALLBACKS_MODE_FIRST); +} + void ScriptApiEnv::initializeEnvironment(ServerEnvironment *env) { SCRIPTAPI_PRECHECKHEADER diff --git a/src/script/cpp_api/s_env.h b/src/script/cpp_api/s_env.h index c5b739eb4..51cf15036 100644 --- a/src/script/cpp_api/s_env.h +++ b/src/script/cpp_api/s_env.h @@ -24,6 +24,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "irr_v3d.h" class ServerEnvironment; +struct MapgenParams; class ScriptApiEnv : virtual public ScriptApiBase @@ -33,6 +34,8 @@ public: void environment_Step(float dtime); // After generating a piece of map void environment_OnGenerated(v3s16 minp, v3s16 maxp,u32 blockseed); + // After initializing mapgens + void environment_OnMapgenInit(MapgenParams *mgparams); void initializeEnvironment(ServerEnvironment *env); }; diff --git a/src/script/lua_api/l_env.cpp b/src/script/lua_api/l_env.cpp index b5c40977e..b6bafbcd9 100644 --- a/src/script/lua_api/l_env.cpp +++ b/src/script/lua_api/l_env.cpp @@ -652,6 +652,59 @@ int ModApiEnvMod::l_get_mapgen_object(lua_State *L) return nargs; } +// minetest.set_mapgen_params(params) +// set mapgen parameters +int ModApiEnvMod::l_set_mapgen_params(lua_State *L) +{ + if (!lua_istable(L, 1)) + return 0; + + EmergeManager *emerge = getServer(L)->getEmergeManager(); + if (emerge->mapgen.size()) + return 0; + + MapgenParams *oparams = new MapgenParams; + u32 paramsmodified = 0; + u32 flagmask = 0; + + lua_getfield(L, 1, "mgname"); + if (lua_isstring(L, -1)) { + oparams->mg_name = std::string(lua_tostring(L, -1)); + paramsmodified |= MGPARAMS_SET_MGNAME; + } + + lua_getfield(L, 1, "seed"); + if (lua_isnumber(L, -1)) { + oparams->seed = lua_tointeger(L, -1); + paramsmodified |= MGPARAMS_SET_SEED; + } + + lua_getfield(L, 1, "water_level"); + if (lua_isnumber(L, -1)) { + oparams->water_level = lua_tointeger(L, -1); + paramsmodified |= MGPARAMS_SET_WATER_LEVEL; + } + + lua_getfield(L, 1, "flags"); + if (lua_isstring(L, -1)) { + std::string flagstr = std::string(lua_tostring(L, -1)); + oparams->flags = readFlagString(flagstr, flagdesc_mapgen); + paramsmodified |= MGPARAMS_SET_FLAGS; + + lua_getfield(L, 1, "flagmask"); + if (lua_isstring(L, -1)) { + flagstr = std::string(lua_tostring(L, -1)); + flagmask = readFlagString(flagstr, flagdesc_mapgen); + } + } + + emerge->luaoverride_params = oparams; + emerge->luaoverride_params_modified = paramsmodified; + emerge->luaoverride_flagmask = flagmask; + + return 0; +} + // minetest.clear_objects() // clear all objects in the environment int ModApiEnvMod::l_clear_objects(lua_State *L) @@ -799,6 +852,7 @@ bool ModApiEnvMod::Initialize(lua_State *L,int top) retval &= API_FCT(get_perlin_map); retval &= API_FCT(get_voxel_manip); retval &= API_FCT(get_mapgen_object); + retval &= API_FCT(set_mapgen_params); retval &= API_FCT(clear_objects); retval &= API_FCT(spawn_tree); retval &= API_FCT(find_path); diff --git a/src/script/lua_api/l_env.h b/src/script/lua_api/l_env.h index 8f769e350..713cfa69f 100644 --- a/src/script/lua_api/l_env.h +++ b/src/script/lua_api/l_env.h @@ -117,6 +117,10 @@ private: // minetest.get_mapgen_object(objectname) // returns the requested object used during map generation static int l_get_mapgen_object(lua_State *L); + + // minetest.set_mapgen_params(params) + // set mapgen parameters + static int l_set_mapgen_params(lua_State *L); // minetest.clear_objects() // clear all objects in the environment |