From 4c8c6497799c83cb5bac773ac4eac7ea572ec78f Mon Sep 17 00:00:00 2001 From: sfan5 Date: Sun, 9 Jan 2022 21:15:35 +0100 Subject: 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) --- src/content/subgames.cpp | 17 ++++--------- src/map_settings_manager.cpp | 9 +------ src/mapgen/mapgen.cpp | 7 +++--- src/script/lua_api/l_mainmenu.cpp | 50 ++++++++++++++++++++++++++++++--------- src/settings.cpp | 12 +++++++--- src/settings.h | 5 +++- 6 files changed, 62 insertions(+), 38 deletions(-) (limited to 'src') diff --git a/src/content/subgames.cpp b/src/content/subgames.cpp index e834f40cd..62e82e0e4 100644 --- a/src/content/subgames.cpp +++ b/src/content/subgames.cpp @@ -24,7 +24,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "log.h" #include "util/strfnd.h" #include "defaultsettings.h" // for set_default_settings -#include "mapgen/mapgen.h" // for MapgenParams +#include "map_settings_manager.h" #include "util/string.h" #ifndef SERVER @@ -370,19 +370,12 @@ void loadGameConfAndInitWorld(const std::string &path, const std::string &name, // Create map_meta.txt if does not already exist std::string map_meta_path = final_path + DIR_DELIM + "map_meta.txt"; if (!fs::PathExists(map_meta_path)) { - verbosestream << "Creating map_meta.txt (" << map_meta_path << ")" - << std::endl; - std::ostringstream oss(std::ios_base::binary); + MapSettingsManager mgr(map_meta_path); - Settings conf; - MapgenParams params; - - params.readParams(g_settings); - params.writeParams(&conf); - conf.writeLines(oss); - oss << "[end_of_params]\n"; + mgr.setMapSetting("seed", g_settings->get("fixed_map_seed")); - fs::safeWriteToFile(map_meta_path, oss.str()); + mgr.makeMapgenParams(); + mgr.saveMapMeta(); } // The Settings object is no longer needed for created worlds diff --git a/src/map_settings_manager.cpp b/src/map_settings_manager.cpp index 7e86a9937..c75483edb 100644 --- a/src/map_settings_manager.cpp +++ b/src/map_settings_manager.cpp @@ -52,14 +52,7 @@ MapSettingsManager::~MapSettingsManager() bool MapSettingsManager::getMapSetting( const std::string &name, std::string *value_out) { - // Try getting it normally first - if (m_map_settings->getNoEx(name, *value_out)) - return true; - - // If not we may have to resolve some compatibility kludges - if (name == "seed") - return Settings::getLayer(SL_GLOBAL)->getNoEx("fixed_map_seed", *value_out); - return false; + return m_map_settings->getNoEx(name, *value_out); } diff --git a/src/mapgen/mapgen.cpp b/src/mapgen/mapgen.cpp index 7984ff609..d767bd264 100644 --- a/src/mapgen/mapgen.cpp +++ b/src/mapgen/mapgen.cpp @@ -1018,10 +1018,11 @@ MapgenParams::~MapgenParams() void MapgenParams::readParams(const Settings *settings) { - std::string seed_str; - const char *seed_name = (settings == g_settings) ? "fixed_map_seed" : "seed"; + // should always be used via MapSettingsManager + assert(settings != g_settings); - if (settings->getNoEx(seed_name, seed_str)) { + std::string seed_str; + if (settings->getNoEx("seed", seed_str)) { if (!seed_str.empty()) seed = read_seed(seed_str.c_str()); else 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 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; } diff --git a/src/settings.cpp b/src/settings.cpp index cf7ec1b72..0e44ee0bc 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -659,9 +659,7 @@ bool Settings::getNoiseParamsFromGroup(const std::string &name, bool Settings::exists(const std::string &name) const { - MutexAutoLock lock(m_mutex); - - if (m_settings.find(name) != m_settings.end()) + if (existsLocal(name)) return true; if (auto parent = getParent()) return parent->exists(name); @@ -669,6 +667,14 @@ bool Settings::exists(const std::string &name) const } +bool Settings::existsLocal(const std::string &name) const +{ + MutexAutoLock lock(m_mutex); + + return m_settings.find(name) != m_settings.end(); +} + + std::vector Settings::getNames() const { MutexAutoLock lock(m_mutex); diff --git a/src/settings.h b/src/settings.h index 4e32a3488..767d057f9 100644 --- a/src/settings.h +++ b/src/settings.h @@ -172,9 +172,12 @@ public: bool getNoiseParamsFromValue(const std::string &name, NoiseParams &np) const; bool getNoiseParamsFromGroup(const std::string &name, NoiseParams &np) const; - // return all keys used + // return all keys used in this object std::vector getNames() const; + // check if setting exists anywhere in the hierarchy bool exists(const std::string &name) const; + // check if setting exists in this object ("locally") + bool existsLocal(const std::string &name) const; /*************************************** -- cgit v1.2.3