]> git.lizzy.rs Git - minetest.git/blob - src/subgame.cpp
Respect game mapgen flags and save world noise params
[minetest.git] / src / subgame.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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 "subgame.h"
21 #include "porting.h"
22 #include "filesys.h"
23 #include "settings.h"
24 #include "main.h"
25 #include "log.h"
26 #include "strfnd.h"
27 #include "defaultsettings.h"  // for override_default_settings
28 #include "mapgen.h"  // for MapgenParams
29 #include "main.h" // for g_settings
30 #ifndef SERVER
31 #include "client/tile.h" // getImagePath
32 #endif
33 #include "util/string.h"
34
35 bool getGameMinetestConfig(const std::string &game_path, Settings &conf)
36 {
37         std::string conf_path = game_path + DIR_DELIM + "minetest.conf";
38         return conf.readConfigFile(conf_path.c_str());
39 }
40
41 bool getGameConfig(const std::string &game_path, Settings &conf)
42 {
43         std::string conf_path = game_path + DIR_DELIM + "game.conf";
44         return conf.readConfigFile(conf_path.c_str());
45 }
46
47 std::string getGameName(const std::string &game_path)
48 {
49         Settings conf;
50         if(!getGameConfig(game_path, conf))
51                 return "";
52         if(!conf.exists("name"))
53                 return "";
54         return conf.get("name");
55 }
56
57 struct GameFindPath
58 {
59         std::string path;
60         bool user_specific;
61         GameFindPath(const std::string &path, bool user_specific):
62                 path(path),
63                 user_specific(user_specific)
64         {}
65 };
66
67 Strfnd getSubgamePathEnv() {
68         std::string sp;
69         char *subgame_path = getenv("MINETEST_SUBGAME_PATH");
70
71         if(subgame_path) {
72                 sp = std::string(subgame_path);
73         }
74
75         return Strfnd(sp);
76 }
77
78 SubgameSpec findSubgame(const std::string &id)
79 {
80         if(id == "")
81                 return SubgameSpec();
82         std::string share = porting::path_share;
83         std::string user = porting::path_user;
84         std::vector<GameFindPath> find_paths;
85
86         Strfnd search_paths = getSubgamePathEnv();
87
88         while(!search_paths.atend()) {
89                 std::string path = search_paths.next(":");
90                 find_paths.push_back(GameFindPath(
91                                 path + DIR_DELIM + id, false));
92                 find_paths.push_back(GameFindPath(
93                                 path + DIR_DELIM + id + "_game", false));
94         }
95
96         find_paths.push_back(GameFindPath(
97                         user + DIR_DELIM + "games" + DIR_DELIM + id + "_game", true));
98         find_paths.push_back(GameFindPath(
99                         user + DIR_DELIM + "games" + DIR_DELIM + id, true));
100         find_paths.push_back(GameFindPath(
101                         share + DIR_DELIM + "games" + DIR_DELIM + id + "_game", false));
102         find_paths.push_back(GameFindPath(
103                         share + DIR_DELIM + "games" + DIR_DELIM + id, false));
104         // Find game directory
105         std::string game_path;
106         bool user_game = true; // Game is in user's directory
107         for(u32 i=0; i<find_paths.size(); i++){
108                 const std::string &try_path = find_paths[i].path;
109                 if(fs::PathExists(try_path)){
110                         game_path = try_path;
111                         user_game = find_paths[i].user_specific;
112                         break;
113                 }
114         }
115         if(game_path == "")
116                 return SubgameSpec();
117         std::string gamemod_path = game_path + DIR_DELIM + "mods";
118         // Find mod directories
119         std::set<std::string> mods_paths;
120         if(!user_game)
121                 mods_paths.insert(share + DIR_DELIM + "mods");
122         if(user != share || user_game)
123                 mods_paths.insert(user + DIR_DELIM + "mods");
124         std::string game_name = getGameName(game_path);
125         if(game_name == "")
126                 game_name = id;
127         std::string menuicon_path;
128 #ifndef SERVER
129         menuicon_path = getImagePath(game_path + DIR_DELIM + "menu" + DIR_DELIM + "icon.png");
130 #endif
131         return SubgameSpec(id, game_path, gamemod_path, mods_paths, game_name,
132                         menuicon_path);
133 }
134
135 SubgameSpec findWorldSubgame(const std::string &world_path)
136 {
137         std::string world_gameid = getWorldGameId(world_path, true);
138         // See if world contains an embedded game; if so, use it.
139         std::string world_gamepath = world_path + DIR_DELIM + "game";
140         if(fs::PathExists(world_gamepath)){
141                 SubgameSpec gamespec;
142                 gamespec.id = world_gameid;
143                 gamespec.path = world_gamepath;
144                 gamespec.gamemods_path= world_gamepath + DIR_DELIM + "mods";
145                 gamespec.name = getGameName(world_gamepath);
146                 if(gamespec.name == "")
147                         gamespec.name = "unknown";
148                 return gamespec;
149         }
150         return findSubgame(world_gameid);
151 }
152
153 std::set<std::string> getAvailableGameIds()
154 {
155         std::set<std::string> gameids;
156         std::set<std::string> gamespaths;
157         gamespaths.insert(porting::path_share + DIR_DELIM + "games");
158         gamespaths.insert(porting::path_user + DIR_DELIM + "games");
159
160         Strfnd search_paths = getSubgamePathEnv();
161
162         while(!search_paths.atend()) {
163                 gamespaths.insert(search_paths.next(":"));
164         }
165
166         for(std::set<std::string>::const_iterator i = gamespaths.begin();
167                         i != gamespaths.end(); i++){
168                 std::vector<fs::DirListNode> dirlist = fs::GetDirListing(*i);
169                 for(u32 j=0; j<dirlist.size(); j++){
170                         if(!dirlist[j].dir)
171                                 continue;
172                         // If configuration file is not found or broken, ignore game
173                         Settings conf;
174                         if(!getGameConfig(*i + DIR_DELIM + dirlist[j].name, conf))
175                                 continue;
176                         // Add it to result
177                         const char *ends[] = {"_game", NULL};
178                         std::string shorter = removeStringEnd(dirlist[j].name, ends);
179                         if(shorter != "")
180                                 gameids.insert(shorter);
181                         else
182                                 gameids.insert(dirlist[j].name);
183                 }
184         }
185         return gameids;
186 }
187
188 std::vector<SubgameSpec> getAvailableGames()
189 {
190         std::vector<SubgameSpec> specs;
191         std::set<std::string> gameids = getAvailableGameIds();
192         for(std::set<std::string>::const_iterator i = gameids.begin();
193                         i != gameids.end(); i++)
194                 specs.push_back(findSubgame(*i));
195         return specs;
196 }
197
198 #define LEGACY_GAMEID "minetest"
199
200 bool getWorldExists(const std::string &world_path)
201 {
202         return (fs::PathExists(world_path + DIR_DELIM + "map_meta.txt") ||
203                         fs::PathExists(world_path + DIR_DELIM + "world.mt"));
204 }
205
206 std::string getWorldGameId(const std::string &world_path, bool can_be_legacy)
207 {
208         std::string conf_path = world_path + DIR_DELIM + "world.mt";
209         Settings conf;
210         bool succeeded = conf.readConfigFile(conf_path.c_str());
211         if(!succeeded){
212                 if(can_be_legacy){
213                         // If map_meta.txt exists, it is probably an old minetest world
214                         if(fs::PathExists(world_path + DIR_DELIM + "map_meta.txt"))
215                                 return LEGACY_GAMEID;
216                 }
217                 return "";
218         }
219         if(!conf.exists("gameid"))
220                 return "";
221         // The "mesetint" gameid has been discarded
222         if(conf.get("gameid") == "mesetint")
223                 return "minetest";
224         return conf.get("gameid");
225 }
226
227 std::vector<WorldSpec> getAvailableWorlds()
228 {
229         std::vector<WorldSpec> worlds;
230         std::set<std::string> worldspaths;
231         worldspaths.insert(porting::path_user + DIR_DELIM + "worlds");
232         infostream<<"Searching worlds..."<<std::endl;
233         for(std::set<std::string>::const_iterator i = worldspaths.begin();
234                         i != worldspaths.end(); i++){
235                 infostream<<"  In "<<(*i)<<": "<<std::endl;
236                 std::vector<fs::DirListNode> dirvector = fs::GetDirListing(*i);
237                 for(u32 j=0; j<dirvector.size(); j++){
238                         if(!dirvector[j].dir)
239                                 continue;
240                         std::string fullpath = *i + DIR_DELIM + dirvector[j].name;
241                         std::string name = dirvector[j].name;
242                         // Just allow filling in the gameid always for now
243                         bool can_be_legacy = true;
244                         std::string gameid = getWorldGameId(fullpath, can_be_legacy);
245                         WorldSpec spec(fullpath, name, gameid);
246                         if(!spec.isValid()){
247                                 infostream<<"(invalid: "<<name<<") ";
248                         } else {
249                                 infostream<<name<<" ";
250                                 worlds.push_back(spec);
251                         }
252                 }
253                 infostream<<std::endl;
254         }
255         // Check old world location
256         do{
257                 std::string fullpath = porting::path_user + DIR_DELIM + "world";
258                 if(!fs::PathExists(fullpath))
259                         break;
260                 std::string name = "Old World";
261                 std::string gameid = getWorldGameId(fullpath, true);
262                 WorldSpec spec(fullpath, name, gameid);
263                 infostream<<"Old world found."<<std::endl;
264                 worlds.push_back(spec);
265         }while(0);
266         infostream<<worlds.size()<<" found."<<std::endl;
267         return worlds;
268 }
269
270 bool initializeWorld(const std::string &path, const std::string &gameid)
271 {
272         infostream << "Initializing world at " << path << std::endl;
273
274         fs::CreateAllDirs(path);
275
276         // Initialize default settings and override defaults with those
277         // provided by the game
278         Settings game_defaults;
279         getGameMinetestConfig(path, game_defaults);
280         override_default_settings(g_settings, &game_defaults);
281
282         // Create world.mt if does not already exist
283         std::string worldmt_path = path + DIR_DELIM "world.mt";
284         if (!fs::PathExists(worldmt_path)) {
285                 std::ostringstream ss(std::ios_base::binary);
286                 ss << "gameid = " << gameid << "\nbackend = sqlite3\n";
287                 if (!fs::safeWriteToFile(worldmt_path, ss.str()))
288                         return false;
289
290                 infostream << "Wrote world.mt (" << worldmt_path << ")" << std::endl;
291         }
292
293         // Create map_meta.txt if does not already exist
294         std::string map_meta_path = path + DIR_DELIM + "map_meta.txt";
295         if (!fs::PathExists(map_meta_path)){
296                 verbosestream << "Creating map_meta.txt (" << map_meta_path << ")" << std::endl;
297                 fs::CreateAllDirs(path);
298                 std::ostringstream oss(std::ios_base::binary);
299
300                 Settings conf;
301                 MapgenParams params;
302
303                 params.load(*g_settings);
304                 params.save(conf);
305                 conf.writeLines(oss);
306                 oss << "[end_of_params]\n";
307
308                 fs::safeWriteToFile(map_meta_path, oss.str());
309         }
310         return true;
311 }
312