Expose mapgen parameters on scripting init

Add minetest.get_mapgen_params()
Deprecate minetest.register_on_mapgen_init()
This commit is contained in:
kwolekr 2014-12-29 12:58:55 -05:00
parent ff3cfb746e
commit 5e2753c712
9 changed files with 97 additions and 86 deletions

@ -390,7 +390,6 @@ end
core.registered_on_chat_messages, core.register_on_chat_message = make_registration() core.registered_on_chat_messages, core.register_on_chat_message = make_registration()
core.registered_globalsteps, core.register_globalstep = make_registration() core.registered_globalsteps, core.register_globalstep = make_registration()
core.registered_playerevents, core.register_playerevent = make_registration() core.registered_playerevents, core.register_playerevent = make_registration()
core.registered_on_mapgen_inits, core.register_on_mapgen_init = make_registration()
core.registered_on_shutdown, core.register_on_shutdown = make_registration() core.registered_on_shutdown, core.register_on_shutdown = make_registration()
core.registered_on_punchnodes, core.register_on_punchnode = make_registration() core.registered_on_punchnodes, core.register_on_punchnode = make_registration()
core.registered_on_placenodes, core.register_on_placenode = make_registration() core.registered_on_placenodes, core.register_on_placenode = make_registration()
@ -409,3 +408,9 @@ core.registered_craft_predicts, core.register_craft_predict = make_registration(
core.registered_on_protection_violation, core.register_on_protection_violation = make_registration() core.registered_on_protection_violation, core.register_on_protection_violation = make_registration()
core.registered_on_item_eats, core.register_on_item_eat = make_registration() core.registered_on_item_eats, core.register_on_item_eat = make_registration()
--
-- Compatibility for on_mapgen_init()
--
core.register_on_mapgen_init = function(func) func(core.get_mapgen_params()) end

@ -1504,8 +1504,9 @@ minetest.register_on_player_receive_fields(func(player, formname, fields))
^ Newest functions are called first ^ Newest functions are called first
^ If function returns true, remaining functions are not called ^ If function returns true, remaining functions are not called
minetest.register_on_mapgen_init(func(MapgenParams)) minetest.register_on_mapgen_init(func(MapgenParams))
^ Called just before the map generator is initialized but before the environment is initialized ^ Deprecated; maintained for compatibility
^ MapgenParams consists of a table with the fields mgname, seed, water_level, and flags ^ This immediately calls func(minetest.get_mapgen_params())
^ MapgenParams consists of a table with the fields mgname, chunksize, seed, water_level, and flags
minetest.register_on_craft(func(itemstack, player, old_craft_grid, craft_inv)) minetest.register_on_craft(func(itemstack, player, old_craft_grid, craft_inv))
^ Called when player crafts something ^ Called when player crafts something
^ itemstack is the output ^ itemstack is the output
@ -1621,6 +1622,10 @@ minetest.set_gen_notify(flags, {deco_ids})
^ The second parameter is a list of IDS of decorations which notification is requested for ^ The second parameter is a list of IDS of decorations which notification is requested for
minetest.get_mapgen_object(objectname) minetest.get_mapgen_object(objectname)
^ Return requested mapgen object if available (see Mapgen objects) ^ Return requested mapgen object if available (see Mapgen objects)
minetest.get_mapgen_params()
^ Returns mapgen params
^ A table consisting of mgname, seed, chunksize, water_level, and flags
^ Can be called at any time
minetest.set_mapgen_params(MapgenParams) minetest.set_mapgen_params(MapgenParams)
^ Set map generation parameters ^ Set map generation parameters
^ Function cannot be called after the registration period; only initialization and on_mapgen_init ^ Function cannot be called after the registration period; only initialization and on_mapgen_init

@ -82,7 +82,8 @@ public:
/////////////////////////////// Emerge Manager //////////////////////////////// /////////////////////////////// Emerge Manager ////////////////////////////////
EmergeManager::EmergeManager(IGameDef *gamedef) { EmergeManager::EmergeManager(IGameDef *gamedef)
{
//register built-in mapgens //register built-in mapgens
registerMapgen("v5", new MapgenFactoryV5()); registerMapgen("v5", new MapgenFactoryV5());
registerMapgen("v6", new MapgenFactoryV6()); 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++) { for (unsigned int i = 0; i != emergethread.size(); i++) {
if (threads_active) { if (threads_active) {
emergethread[i]->Stop(); emergethread[i]->Stop();
@ -162,7 +164,8 @@ EmergeManager::~EmergeManager() {
} }
void EmergeManager::loadMapgenParams() { void EmergeManager::loadMapgenParams()
{
loadParamsFromSettings(g_settings); loadParamsFromSettings(g_settings);
if (g_settings->get("fixed_map_seed").empty()) { if (g_settings->get("fixed_map_seed").empty()) {
@ -174,7 +177,8 @@ void EmergeManager::loadMapgenParams() {
} }
void EmergeManager::initMapgens() { void EmergeManager::initMapgens()
{
if (mapgen.size()) if (mapgen.size())
return; return;
@ -197,7 +201,8 @@ void EmergeManager::initMapgens() {
} }
Mapgen *EmergeManager::getCurrentMapgen() { Mapgen *EmergeManager::getCurrentMapgen()
{
for (unsigned int i = 0; i != emergethread.size(); i++) { for (unsigned int i = 0; i != emergethread.size(); i++) {
if (emergethread[i]->IsSameThread()) if (emergethread[i]->IsSameThread())
return emergethread[i]->mapgen; return emergethread[i]->mapgen;
@ -207,7 +212,8 @@ Mapgen *EmergeManager::getCurrentMapgen() {
} }
void EmergeManager::startThreads() { void EmergeManager::startThreads()
{
if (threads_active) if (threads_active)
return; return;
@ -218,7 +224,8 @@ void EmergeManager::startThreads() {
} }
void EmergeManager::stopThreads() { void EmergeManager::stopThreads()
{
if (!threads_active) if (!threads_active)
return; 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<v3s16, BlockEmergeData *>::const_iterator iter; std::map<v3s16, BlockEmergeData *>::const_iterator iter;
BlockEmergeData *bedata; BlockEmergeData *bedata;
u16 count; 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]) { if (mapgen.size() == 0 || !mapgen[0]) {
errorstream << "EmergeManager: getGroundLevelAtPoint() called" errorstream << "EmergeManager: getGroundLevelAtPoint() called"
" before mapgen initialized" << std::endl; " 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, v2s16 p = v2s16((blockpos.X * MAP_BLOCKSIZE) + MAP_BLOCKSIZE / 2,
(blockpos.Y * 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) + return (u32)(params.seed & 0xFFFFFFFF) +
p.Z * 38134234 + p.Z * 38134234 +
p.Y * 42123 + p.Y * 42123 +
@ -324,12 +335,13 @@ u32 EmergeManager::getBlockSeed(v3s16 p) {
Mapgen *EmergeManager::createMapgen(std::string mgname, int mgid, Mapgen *EmergeManager::createMapgen(std::string mgname, int mgid,
MapgenParams *mgparams) { MapgenParams *mgparams)
{
std::map<std::string, MapgenFactory *>::const_iterator iter; std::map<std::string, MapgenFactory *>::const_iterator iter;
iter = mglist.find(mgname); iter = mglist.find(mgname);
if (iter == mglist.end()) { if (iter == mglist.end()) {
errorstream << "EmergeManager; mapgen " << mgname << errorstream << "EmergeManager; mapgen " << mgname <<
" not registered" << std::endl; " not registered" << std::endl;
return NULL; 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<std::string, MapgenFactory *>::const_iterator iter; std::map<std::string, MapgenFactory *>::const_iterator iter;
iter = mglist.find(mgname); iter = mglist.find(mgname);
if (iter == mglist.end()) { if (iter == mglist.end()) {
errorstream << "EmergeManager: mapgen " << mgname << errorstream << "EmergeManager: mapgen " << mgname <<
" not registered" << std::endl; " not registered" << std::endl;
return NULL; 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; std::string seed_str;
const char *setname = (settings == g_settings) ? "fixed_map_seed" : "seed"; 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->set("mg_name", params.mg_name);
settings->setU64("seed", params.seed); settings->setU64("seed", params.seed);
settings->setS16("water_level", params.water_level); 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)); mglist.insert(std::make_pair(mgname, mgfactory));
infostream << "EmergeManager: registered mapgen " << mgname << std::endl; infostream << "EmergeManager: registered mapgen " << mgname << std::endl;
} }
@ -395,7 +411,8 @@ void EmergeManager::registerMapgen(std::string mgname, MapgenFactory *mgfactory)
////////////////////////////// Emerge Thread ////////////////////////////////// ////////////////////////////// Emerge Thread //////////////////////////////////
bool EmergeThread::popBlockEmerge(v3s16 *pos, u8 *flags) { bool EmergeThread::popBlockEmerge(v3s16 *pos, u8 *flags)
{
std::map<v3s16, BlockEmergeData *>::iterator iter; std::map<v3s16, BlockEmergeData *>::iterator iter;
JMutexAutoLock queuelock(emerge->queuemutex); JMutexAutoLock queuelock(emerge->queuemutex);
@ -423,7 +440,8 @@ bool EmergeThread::popBlockEmerge(v3s16 *pos, u8 *flags) {
bool EmergeThread::getBlockOrStartGen(v3s16 p, MapBlock **b, bool EmergeThread::getBlockOrStartGen(v3s16 p, MapBlock **b,
BlockMakeData *data, bool allow_gen) { BlockMakeData *data, bool allow_gen)
{
v2s16 p2d(p.X, p.Z); v2s16 p2d(p.X, p.Z);
//envlock: usually takes <=1ms, sometimes 90ms or ~400ms to acquire //envlock: usually takes <=1ms, sometimes 90ms or ~400ms to acquire
JMutexAutoLock envlock(m_server->m_env_mutex); 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(); ThreadStarted();
log_register_thread("EmergeThread" + itos(id)); log_register_thread("EmergeThread" + itos(id));
DSTACK(__FUNCTION_NAME); DSTACK(__FUNCTION_NAME);

@ -26,11 +26,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "mapgen.h" // for MapgenParams #include "mapgen.h" // for MapgenParams
#include "map.h" #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 BLOCK_EMERGE_ALLOWGEN (1<<0)
#define EMERGE_DBG_OUT(x) \ #define EMERGE_DBG_OUT(x) \

@ -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) void ScriptApiEnv::initializeEnvironment(ServerEnvironment *env)
{ {
SCRIPTAPI_PRECHECKHEADER SCRIPTAPI_PRECHECKHEADER

@ -34,8 +34,6 @@ public:
void environment_Step(float dtime); void environment_Step(float dtime);
// After generating a piece of map // After generating a piece of map
void environment_OnGenerated(v3s16 minp, v3s16 maxp,u32 blockseed); void environment_OnGenerated(v3s16 minp, v3s16 maxp,u32 blockseed);
// After initializing mapgens
void environment_OnMapgenInit(MapgenParams *mgparams);
//called on player event //called on player event
void player_event(ServerActiveObject* player, std::string type); void player_event(ServerActiveObject* player, std::string type);

@ -325,6 +325,31 @@ int ModApiMapgen::l_get_mapgen_object(lua_State *L)
return 0; 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_params(params)
// set mapgen parameters // set mapgen parameters
int ModApiMapgen::l_set_mapgen_params(lua_State *L) 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)) if (!lua_istable(L, 1))
return 0; return 0;
EmergeManager *emerge = getServer(L)->getEmergeManager(); MapgenParams *params = &getServer(L)->getEmergeManager()->params;
assert(emerge);
std::string flagstr;
u32 flags = 0, flagmask = 0; u32 flags = 0, flagmask = 0;
lua_getfield(L, 1, "mgname"); lua_getfield(L, 1, "mgname");
if (lua_isstring(L, -1)) { if (lua_isstring(L, -1)) {
emerge->params.mg_name = std::string(lua_tostring(L, -1)); params->mg_name = lua_tostring(L, -1);
delete emerge->params.sparams; delete params->sparams;
emerge->params.sparams = NULL; params->sparams = NULL;
} }
lua_getfield(L, 1, "seed"); lua_getfield(L, 1, "seed");
if (lua_isnumber(L, -1)) if (lua_isnumber(L, -1))
emerge->params.seed = lua_tointeger(L, -1); params->seed = lua_tointeger(L, -1);
lua_getfield(L, 1, "water_level"); lua_getfield(L, 1, "water_level");
if (lua_isnumber(L, -1)) 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"); lua_getfield(L, 1, "flagmask");
if (lua_isstring(L, -1)) { if (lua_isstring(L, -1)) {
flagstr = lua_tostring(L, -1); params->flags &= ~readFlagString(lua_tostring(L, -1), flagdesc_mapgen, NULL);
emerge->params.flags &= ~readFlagString(flagstr, flagdesc_mapgen, NULL);
errorstream << "set_mapgen_params(): flagmask field is deprecated, " errorstream << "set_mapgen_params(): flagmask field is deprecated, "
"see lua_api.txt" << std::endl; "see lua_api.txt" << std::endl;
} }
if (getflagsfield(L, 1, "flags", flagdesc_mapgen, &flags, &flagmask)) { if (getflagsfield(L, 1, "flags", flagdesc_mapgen, &flags, &flagmask)) {
emerge->params.flags &= ~flagmask; params->flags &= ~flagmask;
emerge->params.flags |= flags; params->flags |= flags;
} }
return 0; return 0;
@ -818,6 +839,7 @@ void ModApiMapgen::Initialize(lua_State *L, int top)
{ {
API_FCT(get_mapgen_object); API_FCT(get_mapgen_object);
API_FCT(get_mapgen_params);
API_FCT(set_mapgen_params); API_FCT(set_mapgen_params);
API_FCT(set_noiseparams); API_FCT(set_noiseparams);
API_FCT(set_gen_notify); API_FCT(set_gen_notify);

@ -33,6 +33,10 @@ private:
// returns the requested object used during map generation // returns the requested object used during map generation
static int l_get_mapgen_object(lua_State *L); 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_params(params)
// set mapgen parameters // set mapgen parameters
static int l_set_mapgen_params(lua_State *L); static int l_set_mapgen_params(lua_State *L);

@ -295,6 +295,10 @@ Server::Server(
// Lock environment // Lock environment
JMutexAutoLock envlock(m_env_mutex); 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 // Initialize scripting
infostream<<"Server: Initializing Lua"<<std::endl; infostream<<"Server: Initializing Lua"<<std::endl;
@ -302,10 +306,8 @@ Server::Server(
std::string scriptpath = getBuiltinLuaPath() + DIR_DELIM "init.lua"; std::string scriptpath = getBuiltinLuaPath() + DIR_DELIM "init.lua";
if (!m_script->loadScript(scriptpath)) { if (!m_script->loadScript(scriptpath))
throw ModError("Failed to load and run " + scriptpath); throw ModError("Failed to load and run " + scriptpath);
}
// Print 'em // Print 'em
infostream<<"Server: Loading mods: "; infostream<<"Server: Loading mods: ";
@ -339,19 +341,11 @@ Server::Server(
// Perform pending node name resolutions // Perform pending node name resolutions
m_nodedef->runNodeResolverCallbacks(); 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 // Initialize Environment
ServerMap *servermap = new ServerMap(path_world, this, m_emerge);
m_env = new ServerEnvironment(servermap, m_script, this, m_path_world); m_env = new ServerEnvironment(servermap, m_script, this, m_path_world);
m_clients.setEnv(m_env); 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 // Initialize mapgens
m_emerge->initMapgens(); m_emerge->initMapgens();