1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-09-15 18:57:08 +00:00

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)
This commit is contained in:
sfan5 2022-01-09 21:15:35 +01:00 committed by GitHub
parent b164e16d1b
commit 4c8c649779
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 262 additions and 210 deletions

View file

@ -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;
mgr.setMapSetting("seed", g_settings->get("fixed_map_seed"));
params.readParams(g_settings);
params.writeParams(&conf);
conf.writeLines(oss);
oss << "[end_of_params]\n";
fs::safeWriteToFile(map_meta_path, oss.str());
mgr.makeMapgenParams();
mgr.saveMapMeta();
}
// The Settings object is no longer needed for created worlds

View file

@ -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);
}

View file

@ -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

View file

@ -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())) {
// 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 {
if (gameidx < 0 || gameidx >= (int) games.size()) {
lua_pushstring(L, "Invalid game index");
return 1;
}
// 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) {
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;
}

View file

@ -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<std::string> Settings::getNames() const
{
MutexAutoLock lock(m_mutex);

View file

@ -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<std::string> 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;
/***************************************