Respect game mapgen flags and save world noise params

pull/2404/head
ngosang 2015-01-26 12:44:49 +01:00 committed by ShadowNinja
parent 9da99efca2
commit f6e4c5d9cf
15 changed files with 128 additions and 151 deletions

View 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)

View 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);

View 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);
std::string fullpath = m_savedir + DIR_DELIM + "map_meta.txt";
std::ostringstream oss(std::ios_base::binary);
Settings conf;
Settings params;
m_emerge->params.save(conf);
conf.writeLines(oss);
m_emerge->saveParamsToSettings(&params);
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 "<<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");
}
if (!params.parseConfigLines(is, "[end_of_params]")) {
throw SerializationError("ServerMap::loadMapMeta(): "
"[end_of_params] not found!");
}
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");
}
m_emerge->loadParamsFromSettings(&params);
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;

View 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);
}

View 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 {

View 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)

View 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 {

View 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);

View 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;
};

View 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);

View 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 {

View 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);

View 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 {

View 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);

View 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;
}