diff options
author | kwolekr <kwolekr@minetest.net> | 2016-06-14 00:10:55 -0400 |
---|---|---|
committer | kwolekr <kwolekr@minetest.net> | 2016-07-03 14:04:11 -0400 |
commit | 92705306bfb4994107a43514f29997cea15d48dc (patch) | |
tree | d1919cc2b5091ef6e31cdd312f5cd92ba058e76e | |
parent | 70e2c1c7d413d20757f571e3f1e02b05c95418f1 (diff) | |
download | minetest-92705306bfb4994107a43514f29997cea15d48dc.tar.gz minetest-92705306bfb4994107a43514f29997cea15d48dc.tar.bz2 minetest-92705306bfb4994107a43514f29997cea15d48dc.zip |
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
-rw-r--r-- | src/emerge.cpp | 68 | ||||
-rw-r--r-- | src/emerge.h | 3 | ||||
-rw-r--r-- | src/mapgen.cpp | 112 | ||||
-rw-r--r-- | src/mapgen.h | 33 | ||||
-rw-r--r-- | src/mapgen_flat.h | 14 | ||||
-rw-r--r-- | src/mapgen_fractal.h | 14 | ||||
-rw-r--r-- | src/mapgen_singlenode.h | 17 | ||||
-rw-r--r-- | src/mapgen_v5.h | 15 | ||||
-rw-r--r-- | src/mapgen_v6.h | 16 | ||||
-rw-r--r-- | src/mapgen_v7.h | 14 | ||||
-rw-r--r-- | src/mapgen_valleys.h | 14 | ||||
-rw-r--r-- | src/script/lua_api/l_mainmenu.cpp | 4 |
12 files changed, 160 insertions, 164 deletions
diff --git a/src/emerge.cpp b/src/emerge.cpp index a2a10a177..48de6cb1a 100644 --- a/src/emerge.cpp +++ b/src/emerge.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; @@ -100,20 +86,6 @@ private: }; //// -//// 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, ¶ms, this); + Mapgen *mg = Mapgen::createMapgen(mgtype, i, ¶ms, 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, diff --git a/src/emerge.h b/src/emerge.h index 825ac1c0f..303a35529 100644 --- a/src/emerge.h +++ b/src/emerge.h @@ -140,9 +140,6 @@ public: 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: diff --git a/src/mapgen.cpp b/src/mapgen.cpp index 1e0e3aa44..e45233b33 100644 --- a/src/mapgen.cpp +++ b/src/mapgen.cpp @@ -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); } } diff --git a/src/mapgen.h b/src/mapgen.h index 90ac84bd8..618a2f6b8 100644 --- a/src/mapgen.h +++ b/src/mapgen.h @@ -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 @@ private: 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 @@ public: 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 @@ public: // 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 @@ protected: float cave_width; }; -struct MapgenFactory { - virtual Mapgen *createMapgen(int mgid, MapgenParams *params, - EmergeManager *emerge) = 0; - virtual MapgenSpecificParams *createMapgenParams() = 0; - virtual ~MapgenFactory() {} -}; - #endif diff --git a/src/mapgen_flat.h b/src/mapgen_flat.h index afe67b04e..39b6dc302 100644 --- a/src/mapgen_flat.h +++ b/src/mapgen_flat.h @@ -58,6 +58,8 @@ public: 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 @@ private: 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 diff --git a/src/mapgen_fractal.h b/src/mapgen_fractal.h index 065142b84..cbd5567c4 100644 --- a/src/mapgen_fractal.h +++ b/src/mapgen_fractal.h @@ -62,6 +62,8 @@ public: 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 @@ private: 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 diff --git a/src/mapgen_singlenode.h b/src/mapgen_singlenode.h index 2c6496c00..58672a0ed 100644 --- a/src/mapgen_singlenode.h +++ b/src/mapgen_singlenode.h @@ -23,10 +23,9 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "mapgen.h" struct MapgenSinglenodeParams : public MapgenSpecificParams { - MapgenSinglenodeParams() {} ~MapgenSinglenodeParams() {} - + void readParams(const Settings *settings) {} void writeParams(Settings *settings) const {} }; @@ -39,19 +38,11 @@ public: MapgenSinglenode(int mapgenid, MapgenParams *params, EmergeManager *emerge); ~MapgenSinglenode(); - + + virtual MapgenType getType() const { return MAPGEN_SINGLENODE; } + void makeChunk(BlockMakeData *data); int getSpawnLevelAtPoint(v2s16 p); }; -struct MapgenFactorySinglenode : public MapgenFactory { - Mapgen *createMapgen(int mgid, MapgenParams *params, EmergeManager *emerge) { - return new MapgenSinglenode(mgid, params, emerge); - }; - - MapgenSpecificParams *createMapgenParams() { - return new MapgenSinglenodeParams(); - }; -}; - #endif diff --git a/src/mapgen_v5.h b/src/mapgen_v5.h index 4e1772a64..5f6b10383 100644 --- a/src/mapgen_v5.h +++ b/src/mapgen_v5.h @@ -53,6 +53,8 @@ public: MapgenV5(int mapgenid, MapgenParams *params, EmergeManager *emerge); ~MapgenV5(); + virtual MapgenType getType() const { return MAPGEN_V5; } + virtual void makeChunk(BlockMakeData *data); int getSpawnLevelAtPoint(v2s16 p); int generateBaseTerrain(); @@ -63,17 +65,4 @@ private: Noise *noise_ground; }; - -struct MapgenFactoryV5 : public MapgenFactory { - Mapgen *createMapgen(int mgid, MapgenParams *params, EmergeManager *emerge) - { - return new MapgenV5(mgid, params, emerge); - }; - - MapgenSpecificParams *createMapgenParams() - { - return new MapgenV5Params(); - }; -}; - #endif diff --git a/src/mapgen_v6.h b/src/mapgen_v6.h index a55fc6d53..20b0bf92e 100644 --- a/src/mapgen_v6.h +++ b/src/mapgen_v6.h @@ -127,6 +127,8 @@ public: MapgenV6(int mapgenid, MapgenParams *params, EmergeManager *emerge); ~MapgenV6(); + virtual MapgenType getType() const { return MAPGEN_V6; } + void makeChunk(BlockMakeData *data); int getGroundLevelAtPoint(v2s16 p); int getSpawnLevelAtPoint(v2s16 p); @@ -162,18 +164,4 @@ public: virtual void generateCaves(int max_stone_y); }; - -struct MapgenFactoryV6 : public MapgenFactory { - Mapgen *createMapgen(int mgid, MapgenParams *params, EmergeManager *emerge) - { - return new MapgenV6(mgid, params, emerge); - }; - - MapgenSpecificParams *createMapgenParams() - { - return new MapgenV6Params(); - }; -}; - - #endif diff --git a/src/mapgen_v7.h b/src/mapgen_v7.h index 9ed89a511..c75f18a93 100644 --- a/src/mapgen_v7.h +++ b/src/mapgen_v7.h @@ -59,6 +59,8 @@ public: MapgenV7(int mapgenid, MapgenParams *params, EmergeManager *emerge); ~MapgenV7(); + virtual MapgenType getType() const { return MAPGEN_V7; } + virtual void makeChunk(BlockMakeData *data); int getSpawnLevelAtPoint(v2s16 p); @@ -80,16 +82,4 @@ private: Noise *noise_ridge; }; -struct MapgenFactoryV7 : public MapgenFactory { - Mapgen *createMapgen(int mgid, MapgenParams *params, EmergeManager *emerge) - { - return new MapgenV7(mgid, params, emerge); - }; - - MapgenSpecificParams *createMapgenParams() - { - return new MapgenV7Params(); - }; -}; - #endif diff --git a/src/mapgen_valleys.h b/src/mapgen_valleys.h index faaffa905..00f6aff07 100644 --- a/src/mapgen_valleys.h +++ b/src/mapgen_valleys.h @@ -91,6 +91,8 @@ public: MapgenValleys(int mapgenid, MapgenParams *params, EmergeManager *emerge); ~MapgenValleys(); + virtual MapgenType getType() const { return MAPGEN_VALLEYS; } + virtual void makeChunk(BlockMakeData *data); int getSpawnLevelAtPoint(v2s16 p); @@ -137,16 +139,4 @@ private: virtual void generateCaves(s16 max_stone_y, s16 large_cave_depth); }; -struct MapgenFactoryValleys : public MapgenFactory { - Mapgen *createMapgen(int mgid, MapgenParams *params, EmergeManager *emerge) - { - return new MapgenValleys(mgid, params, emerge); - }; - - MapgenSpecificParams *createMapgenParams() - { - return new MapgenValleysParams(); - }; -}; - #endif diff --git a/src/script/lua_api/l_mainmenu.cpp b/src/script/lua_api/l_mainmenu.cpp index 7b29db159..8b078eafd 100644 --- a/src/script/lua_api/l_mainmenu.cpp +++ b/src/script/lua_api/l_mainmenu.cpp @@ -31,7 +31,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "filesys.h" #include "convert_json.h" #include "serverlist.h" -#include "emerge.h" +#include "mapgen.h" #include "sound.h" #include "settings.h" #include "log.h" @@ -707,7 +707,7 @@ int ModApiMainMenu::l_set_topleft_text(lua_State *L) int ModApiMainMenu::l_get_mapgen_names(lua_State *L) { std::vector<const char *> names; - EmergeManager::getMapgenNames(&names, lua_toboolean(L, 1)); + Mapgen::getMapgenNames(&names, lua_toboolean(L, 1)); lua_newtable(L); for (size_t i = 0; i != names.size(); i++) { |