Add core.get_mapgen_names() to Main Menu API (and use it)

Also rewrite mapgen registration for static initialization
This commit is contained in:
kwolekr 2014-12-29 21:23:51 -05:00
parent 5e2753c712
commit ca89e63b27
6 changed files with 60 additions and 36 deletions

@ -16,7 +16,7 @@
--51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. --51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
local function create_world_formspec(dialogdata) local function create_world_formspec(dialogdata)
local mapgens = {"v5", "v6", "v7", "singlenode"} local mapgens = core.get_mapgen_names()
local current_seed = core.setting_get("fixed_map_seed") or "" local current_seed = core.setting_get("fixed_map_seed") or ""
local current_mg = core.setting_get("mg_name") local current_mg = core.setting_get("mg_name")

@ -143,6 +143,8 @@ core.get_game(index)
addon_mods_paths = {[1] = <path>,}, addon_mods_paths = {[1] = <path>,},
} }
core.get_games() -> table of all games in upper format (possible in async calls) core.get_games() -> table of all games in upper format (possible in async calls)
core.get_mapgen_names() -> table of all map generator algorithms registered in
the core (possible in async calls)
Favorites: Favorites:
core.get_favorites(location) -> list of favorites (possible in async calls) core.get_favorites(location) -> list of favorites (possible in async calls)

@ -48,6 +48,17 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "mapgen_v7.h" #include "mapgen_v7.h"
#include "mapgen_singlenode.h" #include "mapgen_singlenode.h"
struct MapgenDesc {
const char *name;
MapgenFactory *factory;
};
MapgenDesc reg_mapgens[] = {
{"v5", new MapgenFactoryV5},
{"v6", new MapgenFactoryV6},
{"v7", new MapgenFactoryV7},
{"singlenode", new MapgenFactorySinglenode},
};
class EmergeThread : public JThread class EmergeThread : public JThread
{ {
@ -84,12 +95,6 @@ public:
EmergeManager::EmergeManager(IGameDef *gamedef) EmergeManager::EmergeManager(IGameDef *gamedef)
{ {
//register built-in mapgens
registerMapgen("v5", new MapgenFactoryV5());
registerMapgen("v6", new MapgenFactoryV6());
registerMapgen("v7", new MapgenFactoryV7());
registerMapgen("singlenode", new MapgenFactorySinglenode());
this->ndef = gamedef->getNodeDefManager(); this->ndef = gamedef->getNodeDefManager();
this->biomemgr = new BiomeManager(gamedef); this->biomemgr = new BiomeManager(gamedef);
this->oremgr = new OreManager(gamedef); this->oremgr = new OreManager(gamedef);
@ -147,11 +152,6 @@ EmergeManager::~EmergeManager()
emergethread.clear(); emergethread.clear();
mapgen.clear(); mapgen.clear();
std::map<std::string, MapgenFactory *>::iterator it;
for (it = mglist.begin(); it != mglist.end(); ++it)
delete it->second;
mglist.clear();
delete biomemgr; delete biomemgr;
delete oremgr; delete oremgr;
delete decomgr; delete decomgr;
@ -334,33 +334,40 @@ u32 EmergeManager::getBlockSeed(v3s16 p)
} }
Mapgen *EmergeManager::createMapgen(std::string mgname, int mgid, void EmergeManager::getMapgenNames(std::list<const char *> &mgnames)
{
for (u32 i = 0; i != ARRLEN(reg_mapgens); i++)
mgnames.push_back(reg_mapgens[i].name);
}
Mapgen *EmergeManager::createMapgen(const std::string &mgname, int mgid,
MapgenParams *mgparams) MapgenParams *mgparams)
{ {
std::map<std::string, MapgenFactory *>::const_iterator iter; u32 i;
iter = mglist.find(mgname); for (i = 0; i != ARRLEN(reg_mapgens) && mgname != reg_mapgens[i].name; i++);
if (iter == mglist.end()) { if (i == ARRLEN(reg_mapgens)) {
errorstream << "EmergeManager; mapgen " << mgname << errorstream << "EmergeManager; mapgen " << mgname <<
" not registered" << std::endl; " not registered" << std::endl;
return NULL; return NULL;
} }
MapgenFactory *mgfactory = iter->second; MapgenFactory *mgfactory = reg_mapgens[i].factory;
return mgfactory->createMapgen(mgid, mgparams, this); return mgfactory->createMapgen(mgid, mgparams, this);
} }
MapgenSpecificParams *EmergeManager::createMapgenParams(std::string mgname) MapgenSpecificParams *EmergeManager::createMapgenParams(const std::string &mgname)
{ {
std::map<std::string, MapgenFactory *>::const_iterator iter; u32 i;
iter = mglist.find(mgname); for (i = 0; i != ARRLEN(reg_mapgens) && mgname != reg_mapgens[i].name; i++);
if (iter == mglist.end()) { if (i == ARRLEN(reg_mapgens)) {
errorstream << "EmergeManager: mapgen " << mgname << errorstream << "EmergeManager; mapgen " << mgname <<
" not registered" << std::endl; " not registered" << std::endl;
return NULL; return NULL;
} }
MapgenFactory *mgfactory = iter->second; MapgenFactory *mgfactory = reg_mapgens[i].factory;
return mgfactory->createMapgenParams(); return mgfactory->createMapgenParams();
} }
@ -402,13 +409,6 @@ void EmergeManager::saveParamsToSettings(Settings *settings)
} }
void EmergeManager::registerMapgen(std::string mgname, MapgenFactory *mgfactory)
{
mglist.insert(std::make_pair(mgname, mgfactory));
infostream << "EmergeManager: registered mapgen " << mgname << std::endl;
}
////////////////////////////// Emerge Thread ////////////////////////////////// ////////////////////////////// Emerge Thread //////////////////////////////////
bool EmergeThread::popBlockEmerge(v3s16 *pos, u8 *flags) bool EmergeThread::popBlockEmerge(v3s16 *pos, u8 *flags)

@ -70,8 +70,6 @@ class EmergeManager {
public: public:
INodeDefManager *ndef; INodeDefManager *ndef;
std::map<std::string, MapgenFactory *> mglist;
std::vector<Mapgen *> mapgen; std::vector<Mapgen *> mapgen;
std::vector<EmergeThread *> emergethread; std::vector<EmergeThread *> emergethread;
@ -105,14 +103,14 @@ public:
void loadMapgenParams(); void loadMapgenParams();
void initMapgens(); void initMapgens();
Mapgen *getCurrentMapgen(); Mapgen *getCurrentMapgen();
Mapgen *createMapgen(std::string mgname, int mgid, Mapgen *createMapgen(const std::string &mgname, int mgid,
MapgenParams *mgparams); MapgenParams *mgparams);
MapgenSpecificParams *createMapgenParams(std::string mgname); MapgenSpecificParams *createMapgenParams(const std::string &mgname);
static void getMapgenNames(std::list<const char *> &mgnames);
void startThreads(); void startThreads();
void stopThreads(); void stopThreads();
bool enqueueBlockEmerge(u16 peer_id, v3s16 p, bool allow_generate); bool enqueueBlockEmerge(u16 peer_id, v3s16 p, bool allow_generate);
void registerMapgen(std::string name, MapgenFactory *mgfactory);
void loadParamsFromSettings(Settings *settings); void loadParamsFromSettings(Settings *settings);
void saveParamsToSettings(Settings *settings); void saveParamsToSettings(Settings *settings);

@ -31,6 +31,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "filesys.h" #include "filesys.h"
#include "convert_json.h" #include "convert_json.h"
#include "serverlist.h" #include "serverlist.h"
#include "emerge.h"
#include "sound.h" #include "sound.h"
#include "settings.h" #include "settings.h"
#include "main.h" // for g_settings #include "main.h" // for g_settings
@ -688,6 +689,25 @@ int ModApiMainMenu::l_set_topleft_text(lua_State *L)
return 0; return 0;
} }
/******************************************************************************/
int ModApiMainMenu::l_get_mapgen_names(lua_State *L)
{
lua_newtable(L);
std::list<const char *> names;
EmergeManager::getMapgenNames(names);
int i = 1;
for (std::list<const char *>::const_iterator
it = names.begin(); it != names.end(); ++it) {
lua_pushstring(L, *it);
lua_rawseti(L, -2, i++);
}
return 1;
}
/******************************************************************************/ /******************************************************************************/
int ModApiMainMenu::l_get_modpath(lua_State *L) int ModApiMainMenu::l_get_modpath(lua_State *L)
{ {
@ -1107,6 +1127,7 @@ void ModApiMainMenu::Initialize(lua_State *L, int top)
API_FCT(delete_favorite); API_FCT(delete_favorite);
API_FCT(set_background); API_FCT(set_background);
API_FCT(set_topleft_text); API_FCT(set_topleft_text);
API_FCT(get_mapgen_names);
API_FCT(get_modpath); API_FCT(get_modpath);
API_FCT(get_gamepath); API_FCT(get_gamepath);
API_FCT(get_texturepath); API_FCT(get_texturepath);
@ -1137,6 +1158,7 @@ void ModApiMainMenu::InitializeAsync(AsyncEngine& engine)
ASYNC_API_FCT(get_worlds); ASYNC_API_FCT(get_worlds);
ASYNC_API_FCT(get_games); ASYNC_API_FCT(get_games);
ASYNC_API_FCT(get_favorites); ASYNC_API_FCT(get_favorites);
ASYNC_API_FCT(get_mapgen_names);
ASYNC_API_FCT(get_modpath); ASYNC_API_FCT(get_modpath);
ASYNC_API_FCT(get_gamepath); ASYNC_API_FCT(get_gamepath);
ASYNC_API_FCT(get_texturepath); ASYNC_API_FCT(get_texturepath);

@ -73,6 +73,8 @@ private:
static int l_get_games(lua_State *L); static int l_get_games(lua_State *L);
static int l_get_mapgen_names(lua_State *L);
static int l_get_favorites(lua_State *L); static int l_get_favorites(lua_State *L);
static int l_delete_favorite(lua_State *L); static int l_delete_favorite(lua_State *L);
@ -112,7 +114,7 @@ private:
static int l_get_modpath(lua_State *L); static int l_get_modpath(lua_State *L);
static int l_get_gamepath(lua_State *L); static int l_get_gamepath(lua_State *L);
static int l_get_texturepath(lua_State *L); static int l_get_texturepath(lua_State *L);
static int l_get_texturepath_share(lua_State *L); static int l_get_texturepath_share(lua_State *L);