From f6e4c5d9cf459e8278a76a2beaee59732e841458 Mon Sep 17 00:00:00 2001 From: ngosang Date: Mon, 26 Jan 2015 12:44:49 +0100 Subject: [PATCH] Respect game mapgen flags and save world noise params --- src/emerge.cpp | 56 +++------------------------------------ src/emerge.h | 5 +--- src/map.cpp | 52 +++++++++++++++--------------------- src/mapgen.cpp | 44 ++++++++++++++++++++++++++++++ src/mapgen.h | 31 ++++++++++++---------- src/mapgen_singlenode.cpp | 12 --------- src/mapgen_singlenode.h | 4 +-- src/mapgen_v5.cpp | 4 +-- src/mapgen_v5.h | 4 +-- src/mapgen_v6.cpp | 4 +-- src/mapgen_v6.h | 4 +-- src/mapgen_v7.cpp | 4 +-- src/mapgen_v7.h | 4 +-- src/server.cpp | 13 +++------ src/subgame.cpp | 38 ++++++++++++++++---------- 15 files changed, 128 insertions(+), 151 deletions(-) diff --git a/src/emerge.cpp b/src/emerge.cpp index a697bcb07..89153cdb6 100644 --- a/src/emerge.cpp +++ b/src/emerge.cpp @@ -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) diff --git a/src/emerge.h b/src/emerge.h index 8bcc96ee0..1653199ec 100644 --- a/src/emerge.h +++ b/src/emerge.h @@ -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 &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); diff --git a/src/map.cpp b/src/map.cpp index 14a237c0a..899c80b27 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -3005,26 +3005,20 @@ void ServerMap::saveMapMeta() { DSTACK(__FUNCTION_NAME); - /*infostream<<"ServerMap::saveMapMeta(): " - <<"seed="<params.save(conf); + conf.writeLines(oss); - m_emerge->saveParamsToSettings(¶ms); - params.writeLines(ss); + oss << "[end_of_params]\n"; - ss<<"[end_of_params]\n"; - - if(!fs::safeWriteToFile(fullpath, ss.str())) - { - infostream<<"ERROR: ServerMap::saveMapMeta(): " - <<"could not write "<loadParamsFromSettings(¶ms); + if (!conf.parseConfigLines(is, "[end_of_params]")) { + throw SerializationError("ServerMap::loadMapMeta(): " + "[end_of_params] not found!"); + } + + m_emerge->params.load(conf); verbosestream << "ServerMap::loadMapMeta(): seed=" << m_emerge->params.seed << std::endl; diff --git a/src/mapgen.cpp b/src/mapgen.cpp index 17aa1dd92..67cf3d065 100644 --- a/src/mapgen.cpp +++ b/src/mapgen.cpp @@ -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); +} + diff --git a/src/mapgen.h b/src/mapgen.h index 01710786b..c9aee5ef5 100644 --- a/src/mapgen.h +++ b/src/mapgen.h @@ -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 { diff --git a/src/mapgen_singlenode.cpp b/src/mapgen_singlenode.cpp index acf811dea..a40020ab3 100644 --- a/src/mapgen_singlenode.cpp +++ b/src/mapgen_singlenode.cpp @@ -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) diff --git a/src/mapgen_singlenode.h b/src/mapgen_singlenode.h index 9fd1d75b3..35f2ba385 100644 --- a/src/mapgen_singlenode.h +++ b/src/mapgen_singlenode.h @@ -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 { diff --git a/src/mapgen_v5.cpp b/src/mapgen_v5.cpp index ffd164774..48f524e6e 100644 --- a/src/mapgen_v5.cpp +++ b/src/mapgen_v5.cpp @@ -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); diff --git a/src/mapgen_v5.h b/src/mapgen_v5.h index e95874e84..28bc37f92 100644 --- a/src/mapgen_v5.h +++ b/src/mapgen_v5.h @@ -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; }; diff --git a/src/mapgen_v6.cpp b/src/mapgen_v6.cpp index b7bfaef0c..89efcb9d3 100644 --- a/src/mapgen_v6.cpp +++ b/src/mapgen_v6.cpp @@ -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); diff --git a/src/mapgen_v6.h b/src/mapgen_v6.h index 64aa2d87a..c805b4d25 100644 --- a/src/mapgen_v6.h +++ b/src/mapgen_v6.h @@ -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 { diff --git a/src/mapgen_v7.cpp b/src/mapgen_v7.cpp index 923f85a0f..4b5a10ca1 100644 --- a/src/mapgen_v7.cpp +++ b/src/mapgen_v7.cpp @@ -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); diff --git a/src/mapgen_v7.h b/src/mapgen_v7.h index 8a02bf564..87d67da9a 100644 --- a/src/mapgen_v7.h +++ b/src/mapgen_v7.h @@ -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 { diff --git a/src/server.cpp b/src/server.cpp index de23820dd..1e2c445f7 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -222,12 +222,9 @@ Server::Server( infostream<<"- world: "<