summaryrefslogtreecommitdiff
path: root/src/script/lua_api/l_mainmenu.cpp
diff options
context:
space:
mode:
authorsfan5 <sfan5@live.de>2022-01-09 21:15:35 +0100
committerGitHub <noreply@github.com>2022-01-09 21:15:35 +0100
commit4c8c6497799c83cb5bac773ac4eac7ea572ec78f (patch)
treeffa67ce9002a94f6354eedbc94a1b768ed0cd336 /src/script/lua_api/l_mainmenu.cpp
parentb164e16d1be30220029729d63a1e621395ad00ad (diff)
downloadminetest-4c8c6497799c83cb5bac773ac4eac7ea572ec78f.tar.gz
minetest-4c8c6497799c83cb5bac773ac4eac7ea572ec78f.tar.bz2
minetest-4c8c6497799c83cb5bac773ac4eac7ea572ec78f.zip
Mainmenu game-related changes (#11887)
fixes: * Switching between games does not immediately hide creative mode / damage buttons if so specified * World creation menu has a game selection list even though the menu already provides a gamebar * Showing gameid in world list is unnecessary * Choice of mapgen parameters in menu persists between games (and was half-broken)
Diffstat (limited to 'src/script/lua_api/l_mainmenu.cpp')
-rw-r--r--src/script/lua_api/l_mainmenu.cpp50
1 files changed, 39 insertions, 11 deletions
diff --git a/src/script/lua_api/l_mainmenu.cpp b/src/script/lua_api/l_mainmenu.cpp
index 3d80bdafa..736ad022f 100644
--- a/src/script/lua_api/l_mainmenu.cpp
+++ b/src/script/lua_api/l_mainmenu.cpp
@@ -414,25 +414,53 @@ int ModApiMainMenu::l_create_world(lua_State *L)
const char *name = luaL_checkstring(L, 1);
int gameidx = luaL_checkinteger(L,2) -1;
+ StringMap use_settings;
+ luaL_checktype(L, 3, LUA_TTABLE);
+ lua_pushnil(L);
+ while (lua_next(L, 3) != 0) {
+ // key at index -2 and value at index -1
+ use_settings[luaL_checkstring(L, -2)] = luaL_checkstring(L, -1);
+ lua_pop(L, 1);
+ }
+ lua_pop(L, 1);
+
std::string path = porting::path_user + DIR_DELIM
"worlds" + DIR_DELIM
+ sanitizeDirName(name, "world_");
std::vector<SubgameSpec> games = getAvailableGames();
+ if (gameidx < 0 || gameidx >= (int) games.size()) {
+ lua_pushstring(L, "Invalid game index");
+ return 1;
+ }
- if ((gameidx >= 0) &&
- (gameidx < (int) games.size())) {
+ // Set the settings for world creation
+ // this is a bad hack but the best we have right now..
+ StringMap backup;
+ for (auto it : use_settings) {
+ if (g_settings->existsLocal(it.first))
+ backup[it.first] = g_settings->get(it.first);
+ g_settings->set(it.first, it.second);
+ }
- // Create world if it doesn't exist
- try {
- loadGameConfAndInitWorld(path, name, games[gameidx], true);
- lua_pushnil(L);
- } catch (const BaseException &e) {
- lua_pushstring(L, (std::string("Failed to initialize world: ") + e.what()).c_str());
- }
- } else {
- lua_pushstring(L, "Invalid game index");
+ // Create world if it doesn't exist
+ try {
+ loadGameConfAndInitWorld(path, name, games[gameidx], true);
+ lua_pushnil(L);
+ } catch (const BaseException &e) {
+ auto err = std::string("Failed to initialize world: ") + e.what();
+ lua_pushstring(L, err.c_str());
}
+
+ // Restore previous settings
+ for (auto it : use_settings) {
+ auto it2 = backup.find(it.first);
+ if (it2 == backup.end())
+ g_settings->remove(it.first); // wasn't set before
+ else
+ g_settings->set(it.first, it2->second); // was set before
+ }
+
return 1;
}