From 5e2753c712e8f65fa50f4889fc1422393ba21413 Mon Sep 17 00:00:00 2001 From: kwolekr Date: Mon, 29 Dec 2014 12:58:55 -0500 Subject: Expose mapgen parameters on scripting init Add minetest.get_mapgen_params() Deprecate minetest.register_on_mapgen_init() --- src/emerge.cpp | 61 +++++++++++++++++++++++++++-------------- src/emerge.h | 5 ---- src/script/cpp_api/s_env.cpp | 31 --------------------- src/script/cpp_api/s_env.h | 2 -- src/script/lua_api/l_mapgen.cpp | 48 +++++++++++++++++++++++--------- src/script/lua_api/l_mapgen.h | 4 +++ src/server.cpp | 16 ++++------- 7 files changed, 84 insertions(+), 83 deletions(-) (limited to 'src') diff --git a/src/emerge.cpp b/src/emerge.cpp index 133fd4a59..2828d490b 100644 --- a/src/emerge.cpp +++ b/src/emerge.cpp @@ -82,7 +82,8 @@ public: /////////////////////////////// Emerge Manager //////////////////////////////// -EmergeManager::EmergeManager(IGameDef *gamedef) { +EmergeManager::EmergeManager(IGameDef *gamedef) +{ //register built-in mapgens registerMapgen("v5", new MapgenFactoryV5()); registerMapgen("v6", new MapgenFactoryV6()); @@ -132,7 +133,8 @@ EmergeManager::EmergeManager(IGameDef *gamedef) { } -EmergeManager::~EmergeManager() { +EmergeManager::~EmergeManager() +{ for (unsigned int i = 0; i != emergethread.size(); i++) { if (threads_active) { emergethread[i]->Stop(); @@ -162,7 +164,8 @@ EmergeManager::~EmergeManager() { } -void EmergeManager::loadMapgenParams() { +void EmergeManager::loadMapgenParams() +{ loadParamsFromSettings(g_settings); if (g_settings->get("fixed_map_seed").empty()) { @@ -174,7 +177,8 @@ void EmergeManager::loadMapgenParams() { } -void EmergeManager::initMapgens() { +void EmergeManager::initMapgens() +{ if (mapgen.size()) return; @@ -197,7 +201,8 @@ void EmergeManager::initMapgens() { } -Mapgen *EmergeManager::getCurrentMapgen() { +Mapgen *EmergeManager::getCurrentMapgen() +{ for (unsigned int i = 0; i != emergethread.size(); i++) { if (emergethread[i]->IsSameThread()) return emergethread[i]->mapgen; @@ -207,7 +212,8 @@ Mapgen *EmergeManager::getCurrentMapgen() { } -void EmergeManager::startThreads() { +void EmergeManager::startThreads() +{ if (threads_active) return; @@ -218,7 +224,8 @@ void EmergeManager::startThreads() { } -void EmergeManager::stopThreads() { +void EmergeManager::stopThreads() +{ if (!threads_active) return; @@ -236,7 +243,8 @@ void EmergeManager::stopThreads() { } -bool EmergeManager::enqueueBlockEmerge(u16 peer_id, v3s16 p, bool allow_generate) { +bool EmergeManager::enqueueBlockEmerge(u16 peer_id, v3s16 p, bool allow_generate) +{ std::map::const_iterator iter; BlockEmergeData *bedata; u16 count; @@ -290,7 +298,8 @@ bool EmergeManager::enqueueBlockEmerge(u16 peer_id, v3s16 p, bool allow_generate } -int EmergeManager::getGroundLevelAtPoint(v2s16 p) { +int EmergeManager::getGroundLevelAtPoint(v2s16 p) +{ if (mapgen.size() == 0 || !mapgen[0]) { errorstream << "EmergeManager: getGroundLevelAtPoint() called" " before mapgen initialized" << std::endl; @@ -301,7 +310,8 @@ int EmergeManager::getGroundLevelAtPoint(v2s16 p) { } -bool EmergeManager::isBlockUnderground(v3s16 blockpos) { +bool EmergeManager::isBlockUnderground(v3s16 blockpos) +{ /* v2s16 p = v2s16((blockpos.X * MAP_BLOCKSIZE) + MAP_BLOCKSIZE / 2, (blockpos.Y * MAP_BLOCKSIZE) + MAP_BLOCKSIZE / 2); @@ -315,7 +325,8 @@ bool EmergeManager::isBlockUnderground(v3s16 blockpos) { } -u32 EmergeManager::getBlockSeed(v3s16 p) { +u32 EmergeManager::getBlockSeed(v3s16 p) +{ return (u32)(params.seed & 0xFFFFFFFF) + p.Z * 38134234 + p.Y * 42123 + @@ -324,12 +335,13 @@ u32 EmergeManager::getBlockSeed(v3s16 p) { Mapgen *EmergeManager::createMapgen(std::string mgname, int mgid, - MapgenParams *mgparams) { + MapgenParams *mgparams) +{ std::map::const_iterator iter; iter = mglist.find(mgname); if (iter == mglist.end()) { errorstream << "EmergeManager; mapgen " << mgname << - " not registered" << std::endl; + " not registered" << std::endl; return NULL; } @@ -338,12 +350,13 @@ Mapgen *EmergeManager::createMapgen(std::string mgname, int mgid, } -MapgenSpecificParams *EmergeManager::createMapgenParams(std::string mgname) { +MapgenSpecificParams *EmergeManager::createMapgenParams(std::string mgname) +{ std::map::const_iterator iter; iter = mglist.find(mgname); if (iter == mglist.end()) { errorstream << "EmergeManager: mapgen " << mgname << - " not registered" << std::endl; + " not registered" << std::endl; return NULL; } @@ -352,7 +365,8 @@ MapgenSpecificParams *EmergeManager::createMapgenParams(std::string mgname) { } -void EmergeManager::loadParamsFromSettings(Settings *settings) { +void EmergeManager::loadParamsFromSettings(Settings *settings) +{ std::string seed_str; const char *setname = (settings == g_settings) ? "fixed_map_seed" : "seed"; @@ -373,7 +387,8 @@ void EmergeManager::loadParamsFromSettings(Settings *settings) { } -void EmergeManager::saveParamsToSettings(Settings *settings) { +void EmergeManager::saveParamsToSettings(Settings *settings) +{ settings->set("mg_name", params.mg_name); settings->setU64("seed", params.seed); settings->setS16("water_level", params.water_level); @@ -387,7 +402,8 @@ void EmergeManager::saveParamsToSettings(Settings *settings) { } -void EmergeManager::registerMapgen(std::string mgname, MapgenFactory *mgfactory) { +void EmergeManager::registerMapgen(std::string mgname, MapgenFactory *mgfactory) +{ mglist.insert(std::make_pair(mgname, mgfactory)); infostream << "EmergeManager: registered mapgen " << mgname << std::endl; } @@ -395,7 +411,8 @@ void EmergeManager::registerMapgen(std::string mgname, MapgenFactory *mgfactory) ////////////////////////////// Emerge Thread ////////////////////////////////// -bool EmergeThread::popBlockEmerge(v3s16 *pos, u8 *flags) { +bool EmergeThread::popBlockEmerge(v3s16 *pos, u8 *flags) +{ std::map::iterator iter; JMutexAutoLock queuelock(emerge->queuemutex); @@ -423,7 +440,8 @@ bool EmergeThread::popBlockEmerge(v3s16 *pos, u8 *flags) { bool EmergeThread::getBlockOrStartGen(v3s16 p, MapBlock **b, - BlockMakeData *data, bool allow_gen) { + BlockMakeData *data, bool allow_gen) +{ v2s16 p2d(p.X, p.Z); //envlock: usually takes <=1ms, sometimes 90ms or ~400ms to acquire JMutexAutoLock envlock(m_server->m_env_mutex); @@ -454,7 +472,8 @@ bool EmergeThread::getBlockOrStartGen(v3s16 p, MapBlock **b, } -void *EmergeThread::Thread() { +void *EmergeThread::Thread() +{ ThreadStarted(); log_register_thread("EmergeThread" + itos(id)); DSTACK(__FUNCTION_NAME); diff --git a/src/emerge.h b/src/emerge.h index 7de1c8391..5c84c70f9 100644 --- a/src/emerge.h +++ b/src/emerge.h @@ -26,11 +26,6 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "mapgen.h" // for MapgenParams #include "map.h" -#define MGPARAMS_SET_MGNAME 1 -#define MGPARAMS_SET_SEED 2 -#define MGPARAMS_SET_WATER_LEVEL 4 -#define MGPARAMS_SET_FLAGS 8 - #define BLOCK_EMERGE_ALLOWGEN (1<<0) #define EMERGE_DBG_OUT(x) \ diff --git a/src/script/cpp_api/s_env.cpp b/src/script/cpp_api/s_env.cpp index 916c562fb..c171bbf02 100644 --- a/src/script/cpp_api/s_env.cpp +++ b/src/script/cpp_api/s_env.cpp @@ -79,37 +79,6 @@ void ScriptApiEnv::player_event(ServerActiveObject* player, std::string type) } } -void ScriptApiEnv::environment_OnMapgenInit(MapgenParams *mgparams) -{ - SCRIPTAPI_PRECHECKHEADER - - // Get core.registered_on_mapgen_inits - lua_getglobal(L, "core"); - 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"); - - lua_pushinteger(L, mgparams->chunksize); - lua_setfield(L, -2, "chunksize"); - - std::string flagstr = writeFlagString(mgparams->flags, - flagdesc_mapgen, (u32)-1); - lua_pushstring(L, flagstr.c_str()); - lua_setfield(L, -2, "flags"); - - script_run_callbacks(L, 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 33bd81ab3..180cfabd0 100644 --- a/src/script/cpp_api/s_env.h +++ b/src/script/cpp_api/s_env.h @@ -34,8 +34,6 @@ 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); //called on player event void player_event(ServerActiveObject* player, std::string type); diff --git a/src/script/lua_api/l_mapgen.cpp b/src/script/lua_api/l_mapgen.cpp index cccbba5a8..b7d7d096e 100644 --- a/src/script/lua_api/l_mapgen.cpp +++ b/src/script/lua_api/l_mapgen.cpp @@ -325,6 +325,31 @@ int ModApiMapgen::l_get_mapgen_object(lua_State *L) return 0; } +int ModApiMapgen::l_get_mapgen_params(lua_State *L) +{ + MapgenParams *params = &getServer(L)->getEmergeManager()->params; + + lua_newtable(L); + + lua_pushstring(L, params->mg_name.c_str()); + lua_setfield(L, -2, "mgname"); + + lua_pushinteger(L, params->seed); + lua_setfield(L, -2, "seed"); + + lua_pushinteger(L, params->water_level); + lua_setfield(L, -2, "water_level"); + + lua_pushinteger(L, params->chunksize); + lua_setfield(L, -2, "chunksize"); + + std::string flagstr = writeFlagString(params->flags, flagdesc_mapgen, (u32)-1); + lua_pushstring(L, flagstr.c_str()); + lua_setfield(L, -2, "flags"); + + return 1; +} + // set_mapgen_params(params) // set mapgen parameters int ModApiMapgen::l_set_mapgen_params(lua_State *L) @@ -332,38 +357,34 @@ int ModApiMapgen::l_set_mapgen_params(lua_State *L) if (!lua_istable(L, 1)) return 0; - EmergeManager *emerge = getServer(L)->getEmergeManager(); - assert(emerge); - - std::string flagstr; + MapgenParams *params = &getServer(L)->getEmergeManager()->params; u32 flags = 0, flagmask = 0; lua_getfield(L, 1, "mgname"); if (lua_isstring(L, -1)) { - emerge->params.mg_name = std::string(lua_tostring(L, -1)); - delete emerge->params.sparams; - emerge->params.sparams = NULL; + params->mg_name = lua_tostring(L, -1); + delete params->sparams; + params->sparams = NULL; } lua_getfield(L, 1, "seed"); if (lua_isnumber(L, -1)) - emerge->params.seed = lua_tointeger(L, -1); + params->seed = lua_tointeger(L, -1); lua_getfield(L, 1, "water_level"); if (lua_isnumber(L, -1)) - emerge->params.water_level = lua_tointeger(L, -1); + params->water_level = lua_tointeger(L, -1); lua_getfield(L, 1, "flagmask"); if (lua_isstring(L, -1)) { - flagstr = lua_tostring(L, -1); - emerge->params.flags &= ~readFlagString(flagstr, flagdesc_mapgen, NULL); + params->flags &= ~readFlagString(lua_tostring(L, -1), flagdesc_mapgen, NULL); errorstream << "set_mapgen_params(): flagmask field is deprecated, " "see lua_api.txt" << std::endl; } if (getflagsfield(L, 1, "flags", flagdesc_mapgen, &flags, &flagmask)) { - emerge->params.flags &= ~flagmask; - emerge->params.flags |= flags; + params->flags &= ~flagmask; + params->flags |= flags; } return 0; @@ -818,6 +839,7 @@ void ModApiMapgen::Initialize(lua_State *L, int top) { API_FCT(get_mapgen_object); + API_FCT(get_mapgen_params); API_FCT(set_mapgen_params); API_FCT(set_noiseparams); API_FCT(set_gen_notify); diff --git a/src/script/lua_api/l_mapgen.h b/src/script/lua_api/l_mapgen.h index 247043a01..e98838470 100644 --- a/src/script/lua_api/l_mapgen.h +++ b/src/script/lua_api/l_mapgen.h @@ -33,6 +33,10 @@ private: // returns the requested object used during map generation static int l_get_mapgen_object(lua_State *L); + // get_mapgen_params() + // returns the currently active map generation parameter set + static int l_get_mapgen_params(lua_State *L); + // set_mapgen_params(params) // set mapgen parameters static int l_set_mapgen_params(lua_State *L); diff --git a/src/server.cpp b/src/server.cpp index a52cd639d..07f808202 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -295,6 +295,10 @@ Server::Server( // Lock environment JMutexAutoLock envlock(m_env_mutex); + // Create the Map and load parameters + ServerMap *servermap = new ServerMap(path_world, this, m_emerge); + m_emerge->loadMapgenParams(); + // Initialize scripting infostream<<"Server: Initializing Lua"<loadScript(scriptpath)) { + if (!m_script->loadScript(scriptpath)) throw ModError("Failed to load and run " + scriptpath); - } - // Print 'em infostream<<"Server: Loading mods: "; @@ -339,19 +341,11 @@ Server::Server( // Perform pending node name resolutions m_nodedef->runNodeResolverCallbacks(); - // Load the mapgen params from global settings now after any - // initial overrides have been set by the mods - m_emerge->loadMapgenParams(); - // Initialize Environment - ServerMap *servermap = new ServerMap(path_world, this, m_emerge); m_env = new ServerEnvironment(servermap, m_script, this, m_path_world); m_clients.setEnv(m_env); - // Run some callbacks after the MG params have been set up but before activation - m_script->environment_OnMapgenInit(&m_emerge->params); - // Initialize mapgens m_emerge->initMapgens(); -- cgit v1.2.3