3 Copyright (C) 2010-2013 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "mapgen/mapgen.h"
26 #include "map_settings_manager.h"
28 MapSettingsManager::MapSettingsManager(Settings *user_settings,
29 const std::string &map_meta_path):
30 m_map_meta_path(map_meta_path),
31 m_map_settings(new Settings()),
32 m_user_settings(user_settings)
34 assert(m_user_settings != NULL);
36 Mapgen::setDefaultSettings(m_map_settings);
37 // This inherits the combined defaults provided by loadGameConfAndInitWorld.
38 m_map_settings->overrideDefaults(user_settings);
42 MapSettingsManager::~MapSettingsManager()
44 delete m_map_settings;
49 bool MapSettingsManager::getMapSetting(
50 const std::string &name, std::string *value_out)
52 if (m_map_settings->getNoEx(name, *value_out))
55 // Compatibility kludge
56 if (m_user_settings == g_settings && name == "seed")
57 return m_user_settings->getNoEx("fixed_map_seed", *value_out);
59 return m_user_settings->getNoEx(name, *value_out);
63 bool MapSettingsManager::getMapSettingNoiseParams(
64 const std::string &name, NoiseParams *value_out)
66 return m_map_settings->getNoiseParams(name, *value_out) ||
67 m_user_settings->getNoiseParams(name, *value_out);
71 bool MapSettingsManager::setMapSetting(
72 const std::string &name, const std::string &value, bool override_meta)
78 m_map_settings->set(name, value);
80 m_map_settings->setDefault(name, value);
86 bool MapSettingsManager::setMapSettingNoiseParams(
87 const std::string &name, const NoiseParams *value, bool override_meta)
92 m_map_settings->setNoiseParams(name, *value, !override_meta);
97 bool MapSettingsManager::loadMapMeta()
99 std::ifstream is(m_map_meta_path.c_str(), std::ios_base::binary);
102 errorstream << "loadMapMeta: could not open "
103 << m_map_meta_path << std::endl;
107 if (!m_map_settings->parseConfigLines(is, "[end_of_params]")) {
108 errorstream << "loadMapMeta: [end_of_params] not found!" << std::endl;
116 bool MapSettingsManager::saveMapMeta()
118 // If mapgen params haven't been created yet; abort
122 if (!fs::CreateAllDirs(fs::RemoveLastPathComponent(m_map_meta_path))) {
123 errorstream << "saveMapMeta: could not create dirs to "
128 std::ostringstream oss(std::ios_base::binary);
131 mapgen_params->MapgenParams::writeParams(&conf);
132 mapgen_params->writeParams(&conf);
133 conf.writeLines(oss);
135 // NOTE: If there are ever types of map settings other than
136 // those relating to map generation, save them here
138 oss << "[end_of_params]\n";
140 if (!fs::safeWriteToFile(m_map_meta_path, oss.str())) {
141 errorstream << "saveMapMeta: could not write "
142 << m_map_meta_path << std::endl;
150 MapgenParams *MapSettingsManager::makeMapgenParams()
153 return mapgen_params;
155 assert(m_user_settings != NULL);
156 assert(m_map_settings != NULL);
158 // At this point, we have (in order of precedence):
159 // 1). m_mapgen_settings->m_settings containing map_meta.txt settings or
160 // explicit overrides from scripts
161 // 2). m_mapgen_settings->m_defaults containing script-set mgparams without
163 // 3). g_settings->m_settings containing all user-specified config file
165 // 4). g_settings->m_defaults containing any low-priority settings from
166 // scripts, e.g. mods using Lua as an enhanced config file)
168 // Now, get the mapgen type so we can create the appropriate MapgenParams
170 MapgenType mgtype = getMapSetting("mg_name", &mg_name) ?
171 Mapgen::getMapgenType(mg_name) : MAPGEN_DEFAULT;
172 if (mgtype == MAPGEN_INVALID) {
173 errorstream << "EmergeManager: mapgen '" << mg_name <<
174 "' not valid; falling back to " <<
175 Mapgen::getMapgenName(MAPGEN_DEFAULT) << std::endl;
176 mgtype = MAPGEN_DEFAULT;
179 // Create our MapgenParams
180 MapgenParams *params = Mapgen::createMapgenParams(mgtype);
184 params->mgtype = mgtype;
186 // Load the rest of the mapgen params from our active settings
187 params->MapgenParams::readParams(m_map_settings);
188 params->readParams(m_map_settings);
190 // Hold onto our params
191 mapgen_params = params;