]> git.lizzy.rs Git - dragonfireclient.git/blob - src/map_settings_manager.cpp
RemotePlayer/LocalPlayer Player base class proper separation (code cleanup) (patch...
[dragonfireclient.git] / src / map_settings_manager.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2013 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
4
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.
9
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.
14
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.
18 */
19
20 #include "debug.h"
21 #include "filesys.h"
22 #include "log.h"
23 #include "mapgen.h"
24 #include "settings.h"
25
26 #include "map_settings_manager.h"
27
28 MapSettingsManager::MapSettingsManager(
29         Settings *user_settings, const std::string &map_meta_path)
30 {
31         m_map_meta_path = map_meta_path;
32         m_user_settings = user_settings;
33         m_map_settings  = new Settings;
34         mapgen_params   = NULL;
35
36         assert(m_user_settings != NULL);
37 }
38
39
40 MapSettingsManager::~MapSettingsManager()
41 {
42         delete m_map_settings;
43         delete mapgen_params;
44 }
45
46
47 bool MapSettingsManager::getMapSetting(
48         const std::string &name, std::string *value_out)
49 {
50         if (m_map_settings->getNoEx(name, *value_out))
51                 return true;
52
53         // Compatibility kludge
54         if (m_user_settings == g_settings && name == "seed")
55                 return m_user_settings->getNoEx("fixed_map_seed", *value_out);
56
57         return m_user_settings->getNoEx(name, *value_out);
58 }
59
60
61 bool MapSettingsManager::getMapSettingNoiseParams(
62         const std::string &name, NoiseParams *value_out)
63 {
64         return m_map_settings->getNoiseParams(name, *value_out) ||
65                 m_user_settings->getNoiseParams(name, *value_out);
66 }
67
68
69 bool MapSettingsManager::setMapSetting(
70         const std::string &name, const std::string &value, bool override_meta)
71 {
72         if (mapgen_params)
73                 return false;
74
75         if (override_meta)
76                 m_map_settings->set(name, value);
77         else
78                 m_map_settings->setDefault(name, value);
79
80         return true;
81 }
82
83
84 bool MapSettingsManager::setMapSettingNoiseParams(
85         const std::string &name, const NoiseParams *value, bool override_meta)
86 {
87         if (mapgen_params)
88                 return false;
89
90         m_map_settings->setNoiseParams(name, *value, !override_meta);
91         return true;
92 }
93
94
95 bool MapSettingsManager::loadMapMeta()
96 {
97         std::ifstream is(m_map_meta_path.c_str(), std::ios_base::binary);
98
99         if (!is.good()) {
100                 errorstream << "loadMapMeta: could not open "
101                         << m_map_meta_path << std::endl;
102                 return false;
103         }
104
105         if (!m_map_settings->parseConfigLines(is, "[end_of_params]")) {
106                 errorstream << "loadMapMeta: [end_of_params] not found!" << std::endl;
107                 return false;
108         }
109
110         return true;
111 }
112
113
114 bool MapSettingsManager::saveMapMeta()
115 {
116         // If mapgen params haven't been created yet; abort
117         if (!mapgen_params)
118                 return false;
119
120         if (!fs::CreateAllDirs(fs::RemoveLastPathComponent(m_map_meta_path))) {
121                 errorstream << "saveMapMeta: could not create dirs to "
122                         << m_map_meta_path;
123                 return false;
124         }
125
126         std::ostringstream oss(std::ios_base::binary);
127         Settings conf;
128
129         mapgen_params->MapgenParams::writeParams(&conf);
130         mapgen_params->writeParams(&conf);
131         conf.writeLines(oss);
132
133         // NOTE: If there are ever types of map settings other than
134         // those relating to map generation, save them here
135
136         oss << "[end_of_params]\n";
137
138         if (!fs::safeWriteToFile(m_map_meta_path, oss.str())) {
139                 errorstream << "saveMapMeta: could not write "
140                         << m_map_meta_path << std::endl;
141                 return false;
142         }
143
144         return true;
145 }
146
147
148 MapgenParams *MapSettingsManager::makeMapgenParams()
149 {
150         if (mapgen_params)
151                 return mapgen_params;
152
153         assert(m_user_settings != NULL);
154         assert(m_map_settings != NULL);
155
156         // At this point, we have (in order of precedence):
157         // 1). m_mapgen_settings->m_settings containing map_meta.txt settings or
158         //     explicit overrides from scripts
159         // 2). m_mapgen_settings->m_defaults containing script-set mgparams without
160         //     overrides
161         // 3). g_settings->m_settings containing all user-specified config file
162         //     settings
163         // 4). g_settings->m_defaults containing any low-priority settings from
164         //     scripts, e.g. mods using Lua as an enhanced config file)
165
166         // Now, get the mapgen type so we can create the appropriate MapgenParams
167         std::string mg_name;
168         MapgenType mgtype = getMapSetting("mg_name", &mg_name) ?
169                 Mapgen::getMapgenType(mg_name) : MAPGEN_DEFAULT;
170         if (mgtype == MAPGEN_INVALID) {
171                 errorstream << "EmergeManager: mapgen '" << mg_name <<
172                         "' not valid; falling back to " <<
173                         Mapgen::getMapgenName(MAPGEN_DEFAULT) << std::endl;
174                 mgtype = MAPGEN_DEFAULT;
175         }
176
177         // Create our MapgenParams
178         MapgenParams *params = Mapgen::createMapgenParams(mgtype);
179         if (params == NULL)
180                 return NULL;
181
182         params->mgtype = mgtype;
183
184         // Load the rest of the mapgen params from our active settings
185         params->MapgenParams::readParams(m_user_settings);
186         params->MapgenParams::readParams(m_map_settings);
187         params->readParams(m_user_settings);
188         params->readParams(m_map_settings);
189
190         // Hold onto our params
191         mapgen_params = params;
192
193         return params;
194 }