Skip to content

Commit

Permalink
Add core.get_mapgen_names() to Main Menu API (and use it)
Browse files Browse the repository at this point in the history
Also rewrite mapgen registration for static initialization
  • Loading branch information
kwolekr committed Dec 30, 2014
1 parent 5e2753c commit ca89e63
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 36 deletions.
2 changes: 1 addition & 1 deletion builtin/mainmenu/dlg_create_world.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
--51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

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_mg = core.setting_get("mg_name")
Expand Down
2 changes: 2 additions & 0 deletions doc/menu_lua_api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ core.get_game(index)
addon_mods_paths = {[1] = <path>,},
}
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:
core.get_favorites(location) -> list of favorites (possible in async calls)
Expand Down
58 changes: 29 additions & 29 deletions src/emerge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "mapgen_v7.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
{
Expand Down Expand Up @@ -84,12 +95,6 @@ class EmergeThread : public JThread

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->biomemgr = new BiomeManager(gamedef);
this->oremgr = new OreManager(gamedef);
Expand Down Expand Up @@ -147,11 +152,6 @@ EmergeManager::~EmergeManager()
emergethread.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 oremgr;
delete decomgr;
Expand Down Expand Up @@ -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)
{
std::map<std::string, MapgenFactory *>::const_iterator iter;
iter = mglist.find(mgname);
if (iter == mglist.end()) {
u32 i;
for (i = 0; i != ARRLEN(reg_mapgens) && mgname != reg_mapgens[i].name; i++);
if (i == ARRLEN(reg_mapgens)) {
errorstream << "EmergeManager; mapgen " << mgname <<
" not registered" << std::endl;
return NULL;
}

MapgenFactory *mgfactory = iter->second;
MapgenFactory *mgfactory = reg_mapgens[i].factory;
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;
iter = mglist.find(mgname);
if (iter == mglist.end()) {
errorstream << "EmergeManager: mapgen " << mgname <<
u32 i;
for (i = 0; i != ARRLEN(reg_mapgens) && mgname != reg_mapgens[i].name; i++);
if (i == ARRLEN(reg_mapgens)) {
errorstream << "EmergeManager; mapgen " << mgname <<
" not registered" << std::endl;
return NULL;
}

MapgenFactory *mgfactory = iter->second;
MapgenFactory *mgfactory = reg_mapgens[i].factory;
return mgfactory->createMapgenParams();
}

Expand Down Expand Up @@ -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 //////////////////////////////////

bool EmergeThread::popBlockEmerge(v3s16 *pos, u8 *flags)
Expand Down
8 changes: 3 additions & 5 deletions src/emerge.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ class EmergeManager {
public:
INodeDefManager *ndef;

std::map<std::string, MapgenFactory *> mglist;

std::vector<Mapgen *> mapgen;
std::vector<EmergeThread *> emergethread;

Expand Down Expand Up @@ -105,14 +103,14 @@ class EmergeManager {
void loadMapgenParams();
void initMapgens();
Mapgen *getCurrentMapgen();
Mapgen *createMapgen(std::string mgname, int mgid,
Mapgen *createMapgen(const std::string &mgname, int mgid,
MapgenParams *mgparams);
MapgenSpecificParams *createMapgenParams(std::string mgname);
MapgenSpecificParams *createMapgenParams(const std::string &mgname);
static void getMapgenNames(std::list<const char *> &mgnames);
void startThreads();
void stopThreads();
bool enqueueBlockEmerge(u16 peer_id, v3s16 p, bool allow_generate);

void registerMapgen(std::string name, MapgenFactory *mgfactory);
void loadParamsFromSettings(Settings *settings);
void saveParamsToSettings(Settings *settings);

Expand Down
22 changes: 22 additions & 0 deletions src/script/lua_api/l_mainmenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "filesys.h"
#include "convert_json.h"
#include "serverlist.h"
#include "emerge.h"
#include "sound.h"
#include "settings.h"
#include "main.h" // for g_settings
Expand Down Expand Up @@ -688,6 +689,25 @@ int ModApiMainMenu::l_set_topleft_text(lua_State *L)
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)
{
Expand Down Expand Up @@ -1107,6 +1127,7 @@ void ModApiMainMenu::Initialize(lua_State *L, int top)
API_FCT(delete_favorite);
API_FCT(set_background);
API_FCT(set_topleft_text);
API_FCT(get_mapgen_names);
API_FCT(get_modpath);
API_FCT(get_gamepath);
API_FCT(get_texturepath);
Expand Down Expand Up @@ -1137,6 +1158,7 @@ void ModApiMainMenu::InitializeAsync(AsyncEngine& engine)
ASYNC_API_FCT(get_worlds);
ASYNC_API_FCT(get_games);
ASYNC_API_FCT(get_favorites);
ASYNC_API_FCT(get_mapgen_names);
ASYNC_API_FCT(get_modpath);
ASYNC_API_FCT(get_gamepath);
ASYNC_API_FCT(get_texturepath);
Expand Down
4 changes: 3 additions & 1 deletion src/script/lua_api/l_mainmenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ class ModApiMainMenu : public ModApiBase {

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_delete_favorite(lua_State *L);
Expand Down Expand Up @@ -112,7 +114,7 @@ class ModApiMainMenu : public ModApiBase {
static int l_get_modpath(lua_State *L);

static int l_get_gamepath(lua_State *L);

static int l_get_texturepath(lua_State *L);

static int l_get_texturepath_share(lua_State *L);
Expand Down

0 comments on commit ca89e63

Please sign in to comment.