Respect game mapgen flags and save world noise params 2404/head
authorngosang <diegodelasheras@gmail.com>
Mon, 26 Jan 2015 11:44:49 +0000 (12:44 +0100)
committerShadowNinja <shadowninja@minetest.net>
Sat, 7 Mar 2015 20:53:39 +0000 (15:53 -0500)
15 files changed:
src/emerge.cpp
src/emerge.h
src/map.cpp
src/mapgen.cpp
src/mapgen.h
src/mapgen_singlenode.cpp
src/mapgen_singlenode.h
src/mapgen_v5.cpp
src/mapgen_v5.h
src/mapgen_v6.cpp
src/mapgen_v6.h
src/mapgen_v7.cpp
src/mapgen_v7.h
src/server.cpp
src/subgame.cpp

index a697bcb0750d33057e7046887e07f0ea11fd6216..89153cdb6017be87b1edfc3fd9c106c268658f38 100644 (file)
@@ -166,7 +166,7 @@ EmergeManager::~EmergeManager()
 
 void EmergeManager::loadMapgenParams()
 {
-       loadParamsFromSettings(g_settings);
+       params.load(*g_settings);
 }
 
 
@@ -344,9 +344,9 @@ Mapgen *EmergeManager::createMapgen(const std::string &mgname, int mgid,
 MapgenSpecificParams *EmergeManager::createMapgenParams(const std::string &mgname)
 {
        u32 i;
-       for (i = 0; i != ARRLEN(reg_mapgens) && mgname != reg_mapgens[i].name; i++);
+       for (i = 0; i < ARRLEN(reg_mapgens) && mgname != reg_mapgens[i].name; i++);
        if (i == ARRLEN(reg_mapgens)) {
-               errorstream << "EmergeManager; mapgen " << mgname <<
+               errorstream << "EmergeManager: Mapgen " << mgname <<
                        " not registered" << std::endl;
                return NULL;
        }
@@ -356,56 +356,6 @@ MapgenSpecificParams *EmergeManager::createMapgenParams(const std::string &mgnam
 }
 
 
-void EmergeManager::loadParamsFromSettings(Settings *settings)
-{
-       std::string seed_str;
-       const char *setname = (settings == g_settings) ? "fixed_map_seed" : "seed";
-
-       if (!settings->getNoEx("seed", seed_str)) {
-               g_settings->getNoEx(setname, seed_str);
-       }
-       if (!seed_str.empty()) {
-               params.seed = read_seed(seed_str.c_str());
-       } else {
-               params.seed =
-                       ((u64)(myrand() & 0xffff) << 0)  |
-                       ((u64)(myrand() & 0xffff) << 16) |
-                       ((u64)(myrand() & 0xffff) << 32) |
-                       ((u64)(myrand() & 0xffff) << 48);
-       }
-
-       settings->getNoEx("mg_name",         params.mg_name);
-       settings->getS16NoEx("water_level",  params.water_level);
-       settings->getS16NoEx("chunksize",    params.chunksize);
-       settings->getFlagStrNoEx("mg_flags", params.flags, flagdesc_mapgen);
-       settings->getNoiseParams("mg_biome_np_heat",     params.np_biome_heat);
-       settings->getNoiseParams("mg_biome_np_humidity", params.np_biome_humidity);
-
-       delete params.sparams;
-       params.sparams = createMapgenParams(params.mg_name);
-
-       if (params.sparams) {
-               params.sparams->readParams(g_settings);
-               params.sparams->readParams(settings);
-       }
-}
-
-
-void EmergeManager::saveParamsToSettings(Settings *settings)
-{
-       settings->set("mg_name",         params.mg_name);
-       settings->setU64("seed",         params.seed);
-       settings->setS16("water_level",  params.water_level);
-       settings->setS16("chunksize",    params.chunksize);
-       settings->setFlagStr("mg_flags", params.flags, flagdesc_mapgen, (u32)-1);
-       settings->setNoiseParams("mg_biome_np_heat",     params.np_biome_heat);
-       settings->setNoiseParams("mg_biome_np_humidity", params.np_biome_humidity);
-
-       if (params.sparams)
-               params.sparams->writeParams(settings);
-}
-
-
 ////////////////////////////// Emerge Thread //////////////////////////////////
 
 bool EmergeThread::popBlockEmerge(v3s16 *pos, u8 *flags)
index 8bcc96ee0ce648367528b9e16ef65ba59990069f..1653199ecbb15d5f59fb83b4c79b7cfab3a79d59 100644 (file)
@@ -101,19 +101,16 @@ public:
        ~EmergeManager();
 
        void loadMapgenParams();
+       static MapgenSpecificParams *createMapgenParams(const std::string &mgname);
        void initMapgens();
        Mapgen *getCurrentMapgen();
        Mapgen *createMapgen(const std::string &mgname, int mgid,
                MapgenParams *mgparams);
-       MapgenSpecificParams *createMapgenParams(const std::string &mgname);
        static void getMapgenNames(std::list<const char *> &mgnames);
        void startThreads();
        void stopThreads();
        bool enqueueBlockEmerge(u16 peer_id, v3s16 p, bool allow_generate);
 
-       void loadParamsFromSettings(Settings *settings);
-       void saveParamsToSettings(Settings *settings);
-
        //mapgen helper methods
        Biome *getBiomeAtPoint(v3s16 p);
        int getGroundLevelAtPoint(v2s16 p);
index 14a237c0a822688694206de7349db885a7c4ea06..899c80b272314ded8ce5e5ca4774a22b398b5ddc 100644 (file)
@@ -3005,26 +3005,20 @@ void ServerMap::saveMapMeta()
 {
        DSTACK(__FUNCTION_NAME);
 
-       /*infostream<<"ServerMap::saveMapMeta(): "
-                       <<"seed="<<m_seed
-                       <<std::endl;*/
-
        createDirs(m_savedir);
 
-       std::string fullpath = m_savedir + DIR_DELIM "map_meta.txt";
-       std::ostringstream ss(std::ios_base::binary);
-
-       Settings params;
+       std::string fullpath = m_savedir + DIR_DELIM + "map_meta.txt";
+       std::ostringstream oss(std::ios_base::binary);
+       Settings conf;
 
-       m_emerge->saveParamsToSettings(&params);
-       params.writeLines(ss);
+       m_emerge->params.save(conf);
+       conf.writeLines(oss);
 
-       ss<<"[end_of_params]\n";
+       oss << "[end_of_params]\n";
 
-       if(!fs::safeWriteToFile(fullpath, ss.str()))
-       {
-               infostream<<"ERROR: ServerMap::saveMapMeta(): "
-                               <<"could not write "<<fullpath<<std::endl;
+       if(!fs::safeWriteToFile(fullpath, oss.str())) {
+               errorstream << "ServerMap::saveMapMeta(): "
+                               << "could not write " << fullpath << std::endl;
                throw FileNotGoodException("Cannot save chunk metadata");
        }
 
@@ -3035,24 +3029,22 @@ void ServerMap::loadMapMeta()
 {
        DSTACK(__FUNCTION_NAME);
 
-       Settings params;
-       std::string fullpath = m_savedir + DIR_DELIM "map_meta.txt";
+       Settings conf;
+       std::string fullpath = m_savedir + DIR_DELIM "map_meta.txt";
 
-       if (fs::PathExists(fullpath)) {
-               std::ifstream is(fullpath.c_str(), std::ios_base::binary);
-               if (!is.good()) {
-                       errorstream << "ServerMap::loadMapMeta(): "
-                               "could not open " << fullpath << std::endl;
-                       throw FileNotGoodException("Cannot open map metadata");
-               }
+       std::ifstream is(fullpath.c_str(), std::ios_base::binary);
+       if (!is.good()) {
+               errorstream << "ServerMap::loadMapMeta(): "
+                       "could not open " << fullpath << std::endl;
+               throw FileNotGoodException("Cannot open map metadata");
+       }
 
-               if (!params.parseConfigLines(is, "[end_of_params]")) {
-                       throw SerializationError("ServerMap::loadMapMeta(): "
+       if (!conf.parseConfigLines(is, "[end_of_params]")) {
+               throw SerializationError("ServerMap::loadMapMeta(): "
                                "[end_of_params] not found!");
-               }
        }
 
-       m_emerge->loadParamsFromSettings(&params);
+       m_emerge->params.load(conf);
 
        verbosestream << "ServerMap::loadMapMeta(): seed="
                << m_emerge->params.seed << std::endl;
index 17aa1dd92e2c33847bd02af326694653945485b6..67cf3d065f3869d1fe9fe0c6d0ed5cbd8b36fff0 100644 (file)
@@ -522,3 +522,47 @@ void GenElementManager::clear()
 {
        m_elements.clear();
 }
+
+
+void MapgenParams::load(const Settings &settings)
+{
+       std::string seed_str;
+       const char *seed_name = (&settings == g_settings) ? "fixed_map_seed" : "seed";
+
+       if (settings.getNoEx(seed_name, seed_str) && !seed_str.empty()) {
+               seed = read_seed(seed_str.c_str());
+       } else {
+               seed = ((u64)(myrand() & 0xFFFF) << 0) |
+                       ((u64)(myrand() & 0xFFFF) << 16) |
+                       ((u64)(myrand() & 0xFFFF) << 32) |
+                       ((u64)(myrand() & 0xFFFF) << 48);
+       }
+
+       settings.getNoEx("mg_name", mg_name);
+       settings.getS16NoEx("water_level", water_level);
+       settings.getS16NoEx("chunksize", chunksize);
+       settings.getFlagStrNoEx("mg_flags", flags, flagdesc_mapgen);
+       settings.getNoiseParams("mg_biome_np_heat", np_biome_heat);
+       settings.getNoiseParams("mg_biome_np_humidity", np_biome_humidity);
+
+       delete sparams;
+       sparams = EmergeManager::createMapgenParams(mg_name);
+       if (sparams)
+               sparams->readParams(&settings);
+}
+
+
+void MapgenParams::save(Settings &settings) const
+{
+       settings.set("mg_name", mg_name);
+       settings.setU64("seed", seed);
+       settings.setS16("water_level", water_level);
+       settings.setS16("chunksize", chunksize);
+       settings.setFlagStr("mg_flags", flags, flagdesc_mapgen, (u32)-1);
+       settings.setNoiseParams("mg_biome_np_heat", np_biome_heat);
+       settings.setNoiseParams("mg_biome_np_humidity", np_biome_humidity);
+
+       if (sparams)
+               sparams->writeParams(&settings);
+}
+
index 01710786b0766cbffc9d07d6da3b19f829e63317..c9aee5ef58cc40da00e472972d6292d383d95fee 100644 (file)
@@ -95,12 +95,13 @@ private:
 };
 
 struct MapgenSpecificParams {
-       virtual void readParams(Settings *settings) = 0;
-       virtual void writeParams(Settings *settings) = 0;
+       virtual void readParams(const Settings *settings) = 0;
+       virtual void writeParams(Settings *settings) const = 0;
        virtual ~MapgenSpecificParams() {}
 };
 
-struct MapgenParams {
+class MapgenParams {
+public:
        std::string mg_name;
        s16 chunksize;
        u64 seed;
@@ -112,17 +113,19 @@ struct MapgenParams {
 
        MapgenSpecificParams *sparams;
 
-       MapgenParams()
-       {
-               mg_name     = DEFAULT_MAPGEN;
-               seed        = 0;
-               water_level = 1;
-               chunksize   = 5;
-               flags       = MG_TREES | MG_CAVES | MG_LIGHT;
-               sparams     = NULL;
-               np_biome_heat     = NoiseParams(50, 50, v3f(500.0, 500.0, 500.0), 5349, 3, 0.5, 2.0);
-               np_biome_humidity = NoiseParams(50, 50, v3f(500.0, 500.0, 500.0), 842, 3, 0.5, 2.0);
-       }
+       MapgenParams() :
+               mg_name(DEFAULT_MAPGEN),
+               chunksize(5),
+               seed(0),
+               water_level(1),
+               flags(MG_TREES | MG_CAVES | MG_LIGHT),
+               np_biome_heat(NoiseParams(50, 50, v3f(500.0, 500.0, 500.0), 5349, 3, 0.5, 2.0)),
+               np_biome_humidity(NoiseParams(50, 50, v3f(500.0, 500.0, 500.0), 842, 3, 0.5, 2.0)),
+               sparams(NULL)
+       {}
+
+       void load(const Settings &settings);
+       void save(Settings &settings) const;
 };
 
 class Mapgen {
index acf811dea4e332ef4a605e5283a6de443156670e..a40020ab356197f569aee6f4b8f5dfda4214e2f2 100644 (file)
@@ -27,18 +27,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "profiler.h"
 #include "emerge.h"
 
-//////////////////////// Mapgen Singlenode parameter read/write
-
-void MapgenSinglenodeParams::readParams(Settings *settings)
-{
-}
-
-
-void MapgenSinglenodeParams::writeParams(Settings *settings)
-{
-}
-
-///////////////////////////////////////////////////////////////////////////////
 
 MapgenSinglenode::MapgenSinglenode(int mapgenid,
        MapgenParams *params, EmergeManager *emerge)
index 9fd1d75b394cde151583ad14fb4d031f4bc1500d..35f2ba3851738f1c8b77ca0f4aa2e269a8a5c6c2 100644 (file)
@@ -27,8 +27,8 @@ struct MapgenSinglenodeParams : public MapgenSpecificParams {
        MapgenSinglenodeParams() {}
        ~MapgenSinglenodeParams() {}
        
-       void readParams(Settings *settings);
-       void writeParams(Settings *settings);
+       void readParams(const Settings *settings) {}
+       void writeParams(Settings *settings) const {}
 };
 
 class MapgenSinglenode : public Mapgen {
index ffd16477440009db3da0af70f5e54c746835afdb..48f524e6efcd0a502f3d85a34afe30b39c425c78 100644 (file)
@@ -142,7 +142,7 @@ MapgenV5Params::MapgenV5Params()
 //#define CAVE_NOISE_THRESHOLD (1.5/CAVE_NOISE_SCALE) = 0.125
 
 
-void MapgenV5Params::readParams(Settings *settings)
+void MapgenV5Params::readParams(const Settings *settings)
 {
        settings->getFlagStrNoEx("mgv5_spflags", spflags, flagdesc_mapgen_v5);
 
@@ -155,7 +155,7 @@ void MapgenV5Params::readParams(Settings *settings)
 }
 
 
-void MapgenV5Params::writeParams(Settings *settings)
+void MapgenV5Params::writeParams(Settings *settings) const
 {
        settings->setFlagStr("mgv5_spflags", spflags, flagdesc_mapgen_v5, (u32)-1);
 
index e95874e846dcb2c1ad303561461ded3aab58fca4..28bc37f92a13770b0a73aaddd55126e9367861be 100644 (file)
@@ -42,8 +42,8 @@ struct MapgenV5Params : public MapgenSpecificParams {
        MapgenV5Params();
        ~MapgenV5Params() {}
 
-       void readParams(Settings *settings);
-       void writeParams(Settings *settings);
+       void readParams(const Settings *settings);
+       void writeParams(Settings *settings) const;
 };
 
 
index b7bfaef0c04158b02da9327bdce5b5f6c763b09b..89efcb9d30c88eba083d31b635380aa78257909e 100644 (file)
@@ -144,7 +144,7 @@ MapgenV6Params::MapgenV6Params()
 }
 
 
-void MapgenV6Params::readParams(Settings *settings)
+void MapgenV6Params::readParams(const Settings *settings)
 {
        settings->getFlagStrNoEx("mgv6_spflags", spflags, flagdesc_mapgen_v6);
        settings->getFloatNoEx("mgv6_freq_desert", freq_desert);
@@ -164,7 +164,7 @@ void MapgenV6Params::readParams(Settings *settings)
 }
 
 
-void MapgenV6Params::writeParams(Settings *settings)
+void MapgenV6Params::writeParams(Settings *settings) const
 {
        settings->setFlagStr("mgv6_spflags", spflags, flagdesc_mapgen_v6, (u32)-1);
        settings->setFloat("mgv6_freq_desert", freq_desert);
index 64aa2d87a58463151c483379a147c35b606af7ed..c805b4d25d4ec295a98ba988cd58e7fdee08a2db 100644 (file)
@@ -59,8 +59,8 @@ struct MapgenV6Params : public MapgenSpecificParams {
        MapgenV6Params();
        ~MapgenV6Params() {}
 
-       void readParams(Settings *settings);
-       void writeParams(Settings *settings);
+       void readParams(const Settings *settings);
+       void writeParams(Settings *settings) const;
 };
 
 class MapgenV6 : public Mapgen {
index 923f85a0fa96e3e4e57fc5026226798b2166d238..4b5a10ca14b5c0d6ad0b34fa8c0caed1f7b3c339 100644 (file)
@@ -144,7 +144,7 @@ MapgenV7Params::MapgenV7Params()
 }
 
 
-void MapgenV7Params::readParams(Settings *settings)
+void MapgenV7Params::readParams(const Settings *settings)
 {
        settings->getFlagStrNoEx("mgv7_spflags", spflags, flagdesc_mapgen_v7);
 
@@ -162,7 +162,7 @@ void MapgenV7Params::readParams(Settings *settings)
 }
 
 
-void MapgenV7Params::writeParams(Settings *settings)
+void MapgenV7Params::writeParams(Settings *settings) const
 {
        settings->setFlagStr("mgv7_spflags", spflags, flagdesc_mapgen_v7, (u32)-1);
 
index 8a02bf56465728499614fbef7e44e61578d40a1c..87d67da9a03c6841ab50f89319a905ca9fc883d0 100644 (file)
@@ -48,8 +48,8 @@ struct MapgenV7Params : public MapgenSpecificParams {
        MapgenV7Params();
        ~MapgenV7Params() {}
 
-       void readParams(Settings *settings);
-       void writeParams(Settings *settings);
+       void readParams(const Settings *settings);
+       void writeParams(Settings *settings) const;
 };
 
 class MapgenV7 : public Mapgen {
index de23820dde3c6842bec58c9f5fe25ab63803bfc9..1e2c445f7e21b66bf8b65f9f25d94502266441a1 100644 (file)
@@ -222,12 +222,9 @@ Server::Server(
        infostream<<"- world:  "<<m_path_world<<std::endl;
        infostream<<"- game:   "<<m_gamespec.path<<std::endl;
 
-       // Initialize default settings and override defaults with those provided
-       // by the game
-       set_default_settings(g_settings);
-       Settings gamedefaults;
-       getGameMinetestConfig(gamespec.path, gamedefaults);
-       override_default_settings(g_settings, &gamedefaults);
+       // Create world if it doesn't exist
+       if(!initializeWorld(m_path_world, m_gamespec.id))
+               throw ServerError("Failed to initialize world");
 
        // Create server thread
        m_thread = new ServerThread(this);
@@ -235,10 +232,6 @@ Server::Server(
        // Create emerge manager
        m_emerge = new EmergeManager(this);
 
-       // Create world if it doesn't exist
-       if(!initializeWorld(m_path_world, m_gamespec.id))
-               throw ServerError("Failed to initialize world");
-
        // Create ban manager
        std::string ban_path = m_path_world + DIR_DELIM "ipban.txt";
        m_banmanager = new BanManager(ban_path);
index ccf477e8f4476b8089898306d3f55b34b5b93e3a..4e8777d13a7ef30691fad1e6efa7e59c9469ee07 100644 (file)
@@ -24,6 +24,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "main.h"
 #include "log.h"
 #include "strfnd.h"
+#include "defaultsettings.h"  // for override_default_settings
+#include "mapgen.h"  // for MapgenParams
+#include "main.h" // for g_settings
 #ifndef SERVER
 #include "client/tile.h" // getImagePath
 #endif
@@ -270,6 +273,12 @@ bool initializeWorld(const std::string &path, const std::string &gameid)
 
        fs::CreateAllDirs(path);
 
+       // Initialize default settings and override defaults with those
+       // provided by the game
+       Settings game_defaults;
+       getGameMinetestConfig(path, game_defaults);
+       override_default_settings(g_settings, &game_defaults);
+
        // Create world.mt if does not already exist
        std::string worldmt_path = path + DIR_DELIM "world.mt";
        if (!fs::PathExists(worldmt_path)) {
@@ -282,21 +291,22 @@ bool initializeWorld(const std::string &path, const std::string &gameid)
        }
 
        // Create map_meta.txt if does not already exist
-       std::string mapmeta_path = path + DIR_DELIM "map_meta.txt";
-       if (!fs::PathExists(mapmeta_path)) {
-               std::ostringstream ss(std::ios_base::binary);
-               ss
-                       << "mg_name = "       << g_settings->get("mg_name")
-                       << "\nseed = "        << g_settings->get("fixed_map_seed")
-                       << "\nchunksize = "   << g_settings->get("chunksize")
-                       << "\nwater_level = " << g_settings->get("water_level")
-                       << "\nmg_flags = "    << g_settings->get("mg_flags")
-                       << "\n[end_of_params]\n";
-               if (!fs::safeWriteToFile(mapmeta_path, ss.str()))
-                       return false;
+       std::string map_meta_path = path + DIR_DELIM + "map_meta.txt";
+       if (!fs::PathExists(map_meta_path)){
+               verbosestream << "Creating map_meta.txt (" << map_meta_path << ")" << std::endl;
+               fs::CreateAllDirs(path);
+               std::ostringstream oss(std::ios_base::binary);
 
-               infostream << "Wrote map_meta.txt (" << mapmeta_path << ")" << std::endl;
-       }
+               Settings conf;
+               MapgenParams params;
 
+               params.load(*g_settings);
+               params.save(conf);
+               conf.writeLines(oss);
+               oss << "[end_of_params]\n";
+
+               fs::safeWriteToFile(map_meta_path, oss.str());
+       }
        return true;
 }
+