Skip to content
Permalink
Browse files

Mapgen: Refactor mapgen creation and management

- Move mapgen creation logic out of EmergeManager and into Mapgen
- Internally represent mapgen type as an enum value, instead of a string
- Remove the need for a MapgenFactory per mapgen
  • Loading branch information
kwolekr committed Jul 3, 2016
1 parent 70e2c1c commit 92705306bfb4994107a43514f29997cea15d48dc
Showing with 160 additions and 164 deletions.
  1. +9 −59 src/emerge.cpp
  2. +0 −3 src/emerge.h
  3. +109 −3 src/mapgen.cpp
  4. +24 −9 src/mapgen.h
  5. +2 −12 src/mapgen_flat.h
  6. +2 −12 src/mapgen_fractal.h
  7. +4 −13 src/mapgen_singlenode.h
  8. +2 −13 src/mapgen_v5.h
  9. +2 −14 src/mapgen_v6.h
  10. +2 −12 src/mapgen_v7.h
  11. +2 −12 src/mapgen_valleys.h
  12. +2 −2 src/script/lua_api/l_mainmenu.cpp
@@ -34,13 +34,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "log.h"
#include "map.h"
#include "mapblock.h"
#include "mapgen_flat.h"
#include "mapgen_fractal.h"
#include "mapgen_v5.h"
#include "mapgen_v6.h"
#include "mapgen_v7.h"
#include "mapgen_valleys.h"
#include "mapgen_singlenode.h"
#include "mg_biome.h"
#include "mg_ore.h"
#include "mg_decoration.h"
@@ -53,13 +46,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "settings.h"
#include "voxel.h"


struct MapgenDesc {
const char *name;
MapgenFactory *factory;
bool is_user_visible;
};

class EmergeThread : public Thread {
public:
bool enable_mapgen_debug_info;
@@ -99,20 +85,6 @@ class EmergeThread : public Thread {
friend class EmergeManager;
};

////
//// Built-in mapgens
////

MapgenDesc g_reg_mapgens[] = {
{"v5", new MapgenFactoryV5, true},
{"v6", new MapgenFactoryV6, true},
{"v7", new MapgenFactoryV7, true},
{"flat", new MapgenFactoryFlat, true},
{"fractal", new MapgenFactoryFractal, true},
{"valleys", new MapgenFactoryValleys, true},
{"singlenode", new MapgenFactorySinglenode, false},
};

////
//// EmergeManager
////
@@ -195,24 +167,24 @@ void EmergeManager::initMapgens()
if (m_mapgens.size())
return;

MapgenFactory *mgfactory = getMapgenFactory(params.mg_name);
if (!mgfactory) {
MapgenType mgtype = Mapgen::getMapgenType(params.mg_name);
if (mgtype == MAPGEN_INVALID) {
const char *default_mapgen_name = Mapgen::getMapgenName(MAPGEN_DEFAULT);
errorstream << "EmergeManager: mapgen " << params.mg_name <<
" not registered; falling back to " << DEFAULT_MAPGEN << std::endl;

params.mg_name = DEFAULT_MAPGEN;
" not registered; falling back to " <<
default_mapgen_name << std::endl;

mgfactory = getMapgenFactory(params.mg_name);
FATAL_ERROR_IF(mgfactory == NULL, "Couldn't use any mapgen!");
params.mg_name = default_mapgen_name;
mgtype = MAPGEN_DEFAULT;
}

if (!params.sparams) {
params.sparams = mgfactory->createMapgenParams();
params.sparams = Mapgen::createMapgenParams(mgtype);
params.sparams->readParams(g_settings);
}

for (u32 i = 0; i != m_threads.size(); i++) {
Mapgen *mg = mgfactory->createMapgen(i, &params, this);
Mapgen *mg = Mapgen::createMapgen(mgtype, i, &params, this);
m_mapgens.push_back(mg);
}
}
@@ -369,28 +341,6 @@ bool EmergeManager::isBlockUnderground(v3s16 blockpos)
return blockpos.Y * (MAP_BLOCKSIZE + 1) <= params.water_level;
}


void EmergeManager::getMapgenNames(
std::vector<const char *> *mgnames, bool include_hidden)
{
for (u32 i = 0; i != ARRLEN(g_reg_mapgens); i++) {
if (include_hidden || g_reg_mapgens[i].is_user_visible)
mgnames->push_back(g_reg_mapgens[i].name);
}
}


MapgenFactory *EmergeManager::getMapgenFactory(const std::string &mgname)
{
for (u32 i = 0; i != ARRLEN(g_reg_mapgens); i++) {
if (mgname == g_reg_mapgens[i].name)
return g_reg_mapgens[i].factory;
}

return NULL;
}


bool EmergeManager::pushBlockEmergeData(
v3s16 pos,
u16 peer_requested,
@@ -140,9 +140,6 @@ class EmergeManager {
int getGroundLevelAtPoint(v2s16 p);
bool isBlockUnderground(v3s16 blockpos);

static MapgenFactory *getMapgenFactory(const std::string &mgname);
static void getMapgenNames(
std::vector<const char *> *mgnames, bool include_hidden);
static v3s16 getContainingChunk(v3s16 blockpos, s16 chunksize);

private:
@@ -39,6 +39,13 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "util/numeric.h"
#include "filesys.h"
#include "log.h"
#include "mapgen_flat.h"
#include "mapgen_fractal.h"
#include "mapgen_v5.h"
#include "mapgen_v6.h"
#include "mapgen_v7.h"
#include "mapgen_valleys.h"
#include "mapgen_singlenode.h"
#include "cavegen.h"
#include "dungeongen.h"

@@ -63,6 +70,28 @@ FlagDesc flagdesc_gennotify[] = {
{NULL, 0}
};

struct MapgenDesc {
const char *name;
bool is_user_visible;
};

////
//// Built-in mapgens
////

static MapgenDesc g_reg_mapgens[] = {
{"v5", true},
{"v6", true},
{"v7", true},
{"flat", true},
{"fractal", true},
{"valleys", true},
{"singlenode", false},
};

STATIC_ASSERT(
ARRLEN(g_reg_mapgens) == MAPGEN_INVALID,
registered_mapgens_is_wrong_size);

////
//// Mapgen
@@ -121,6 +150,83 @@ Mapgen::~Mapgen()
}


MapgenType Mapgen::getMapgenType(const std::string &mgname)
{
for (size_t i = 0; i != ARRLEN(g_reg_mapgens); i++) {
if (mgname == g_reg_mapgens[i].name)
return (MapgenType)i;
}

return MAPGEN_INVALID;
}


const char *Mapgen::getMapgenName(MapgenType mgtype)
{
size_t index = (size_t)mgtype;
if (index == MAPGEN_INVALID || index >= ARRLEN(g_reg_mapgens))
return "invalid";

return g_reg_mapgens[index].name;
}


Mapgen *Mapgen::createMapgen(MapgenType mgtype, int mgid,
MapgenParams *params, EmergeManager *emerge)
{
switch (mgtype) {
case MAPGEN_FLAT:
return new MapgenFlat(mgid, params, emerge);
case MAPGEN_FRACTAL:
return new MapgenFractal(mgid, params, emerge);
case MAPGEN_SINGLENODE:
return new MapgenSinglenode(mgid, params, emerge);
case MAPGEN_V5:
return new MapgenV5(mgid, params, emerge);
case MAPGEN_V6:
return new MapgenV6(mgid, params, emerge);
case MAPGEN_V7:
return new MapgenV7(mgid, params, emerge);
case MAPGEN_VALLEYS:
return new MapgenValleys(mgid, params, emerge);
default:
return NULL;
}
}


MapgenSpecificParams *Mapgen::createMapgenParams(MapgenType mgtype)
{
switch (mgtype) {
case MAPGEN_FLAT:
return new MapgenFlatParams;
case MAPGEN_FRACTAL:
return new MapgenFractalParams;
case MAPGEN_SINGLENODE:
return new MapgenSinglenodeParams;
case MAPGEN_V5:
return new MapgenV5Params;
case MAPGEN_V6:
return new MapgenV6Params;
case MAPGEN_V7:
return new MapgenV7Params;
case MAPGEN_VALLEYS:
return new MapgenValleysParams;
default:
return NULL;
}
}


void Mapgen::getMapgenNames(std::vector<const char *> *mgnames, bool include_hidden)
{
for (u32 i = 0; i != ARRLEN(g_reg_mapgens); i++) {
if (include_hidden || g_reg_mapgens[i].is_user_visible)
mgnames->push_back(g_reg_mapgens[i].name);
}
}


u32 Mapgen::getBlockSeed(v3s16 p, s32 seed)
{
return (u32)seed +
@@ -891,9 +997,9 @@ void MapgenParams::load(const Settings &settings)
}

delete sparams;
MapgenFactory *mgfactory = EmergeManager::getMapgenFactory(mg_name);
if (mgfactory) {
sparams = mgfactory->createMapgenParams();
MapgenType mgtype = Mapgen::getMapgenType(mg_name);
if (mgtype != MAPGEN_INVALID) {
sparams = Mapgen::createMapgenParams(mgtype);
sparams->readParams(&settings);
}
}
@@ -26,7 +26,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "util/string.h"
#include "util/container.h"

#define DEFAULT_MAPGEN "v6"
#define MAPGEN_DEFAULT MAPGEN_V6
#define MAPGEN_DEFAULT_NAME "v6"

/////////////////// Mapgen flags
#define MG_TREES 0x01
@@ -107,6 +108,17 @@ class GenerateNotifier {
std::list<GenNotifyEvent> m_notify_events;
};

enum MapgenType {
MAPGEN_V5,
MAPGEN_V6,
MAPGEN_V7,
MAPGEN_FLAT,
MAPGEN_FRACTAL,
MAPGEN_VALLEYS,
MAPGEN_SINGLENODE,
MAPGEN_INVALID,
};

struct MapgenSpecificParams {
virtual void readParams(const Settings *settings) = 0;
virtual void writeParams(Settings *settings) const = 0;
@@ -124,7 +136,7 @@ struct MapgenParams {
MapgenSpecificParams *sparams;

MapgenParams() :
mg_name(DEFAULT_MAPGEN),
mg_name(MAPGEN_DEFAULT_NAME),
chunksize(5),
seed(0),
water_level(1),
@@ -173,6 +185,8 @@ class Mapgen {
Mapgen(int mapgenid, MapgenParams *params, EmergeManager *emerge);
virtual ~Mapgen();

virtual MapgenType getType() const { return MAPGEN_INVALID; }

static u32 getBlockSeed(v3s16 p, s32 seed);
static u32 getBlockSeed2(v3s16 p, s32 seed);
s16 findGroundLevelFull(v2s16 p2d);
@@ -198,6 +212,14 @@ class Mapgen {
// signify this and to cause Server::findSpawnPos() to try another (X, Z).
virtual int getSpawnLevelAtPoint(v2s16 p) { return 0; }

// Mapgen management functions
static MapgenType getMapgenType(const std::string &mgname);
static const char *getMapgenName(MapgenType mgtype);
static Mapgen *createMapgen(MapgenType mgtype, int mgid,
MapgenParams *params, EmergeManager *emerge);
static MapgenSpecificParams *createMapgenParams(MapgenType mgtype);
static void getMapgenNames(std::vector<const char *> *mgnames, bool include_hidden);

private:
// isLiquidHorizontallyFlowable() is a helper function for updateLiquid()
// that checks whether there are floodable nodes without liquid beneath
@@ -267,11 +289,4 @@ class MapgenBasic : public Mapgen {
float cave_width;
};

struct MapgenFactory {
virtual Mapgen *createMapgen(int mgid, MapgenParams *params,
EmergeManager *emerge) = 0;
virtual MapgenSpecificParams *createMapgenParams() = 0;
virtual ~MapgenFactory() {}
};

#endif
@@ -58,6 +58,8 @@ class MapgenFlat : public MapgenBasic {
MapgenFlat(int mapgenid, MapgenParams *params, EmergeManager *emerge);
~MapgenFlat();

virtual MapgenType getType() const { return MAPGEN_FLAT; }

virtual void makeChunk(BlockMakeData *data);
int getSpawnLevelAtPoint(v2s16 p);
s16 generateTerrain();
@@ -72,16 +74,4 @@ class MapgenFlat : public MapgenBasic {
Noise *noise_terrain;
};

struct MapgenFactoryFlat : public MapgenFactory {
Mapgen *createMapgen(int mgid, MapgenParams *params, EmergeManager *emerge)
{
return new MapgenFlat(mgid, params, emerge);
};

MapgenSpecificParams *createMapgenParams()
{
return new MapgenFlatParams();
};
};

#endif
@@ -62,6 +62,8 @@ class MapgenFractal : public MapgenBasic {
MapgenFractal(int mapgenid, MapgenParams *params, EmergeManager *emerge);
~MapgenFractal();

virtual MapgenType getType() const { return MAPGEN_FRACTAL; }

virtual void makeChunk(BlockMakeData *data);
int getSpawnLevelAtPoint(v2s16 p);
bool getFractalAtPoint(s16 x, s16 y, s16 z);
@@ -83,16 +85,4 @@ class MapgenFractal : public MapgenBasic {
Noise *noise_seabed;
};

struct MapgenFactoryFractal : public MapgenFactory {
Mapgen *createMapgen(int mgid, MapgenParams *params, EmergeManager *emerge)
{
return new MapgenFractal(mgid, params, emerge);
};

MapgenSpecificParams *createMapgenParams()
{
return new MapgenFractalParams();
};
};

#endif

0 comments on commit 9270530

Please sign in to comment.