From cf8213ea827f38ae5d4b8ef16c396545e3e59657 Mon Sep 17 00:00:00 2001 From: kwolekr Date: Fri, 12 Dec 2014 14:07:49 -0500 Subject: [PATCH] Add minetest.clear_registered_decorations() and clear_registered_ores() --- src/mapgen.cpp | 7 +++++++ src/mapgen.h | 3 ++- src/mg_biome.cpp | 5 ++--- src/mg_biome.h | 3 --- src/mg_decoration.cpp | 26 ++++++++++++++++++++++++++ src/mg_decoration.h | 6 +++++- src/mg_ore.cpp | 21 ++++++++++++++++++++- src/mg_ore.h | 4 +++- src/mg_schematic.cpp | 8 ++++++++ src/mg_schematic.h | 2 +- src/script/lua_api/l_mapgen.cpp | 17 +++++++++++++++++ src/script/lua_api/l_mapgen.h | 6 ++++++ 12 files changed, 97 insertions(+), 11 deletions(-) diff --git a/src/mapgen.cpp b/src/mapgen.cpp index 880ea1eea..ceaabbb1a 100644 --- a/src/mapgen.cpp +++ b/src/mapgen.cpp @@ -20,6 +20,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "mapgen.h" #include "voxel.h" #include "noise.h" +#include "gamedef.h" #include "mg_biome.h" #include "mapblock.h" #include "mapnode.h" @@ -381,6 +382,12 @@ void GenerateNotifier::getEvents( /////////////////////////////////////////////////////////////////////////////// +GenElementManager::GenElementManager(IGameDef *gamedef) +{ + m_resolver = gamedef->getNodeDefManager()->getResolver(); +} + + GenElementManager::~GenElementManager() { for (size_t i = 0; i != m_elements.size(); i++) diff --git a/src/mapgen.h b/src/mapgen.h index b4cfd67ba..03850cd19 100644 --- a/src/mapgen.h +++ b/src/mapgen.h @@ -178,7 +178,7 @@ public: static const char *ELEMENT_TITLE; static const size_t ELEMENT_LIMIT = -1; - GenElementManager() {} + GenElementManager(IGameDef *gamedef); virtual ~GenElementManager(); virtual GenElement *create(int type) = 0; @@ -192,6 +192,7 @@ public: virtual GenElement *getByName(const std::string &name); protected: + NodeResolver *m_resolver; std::vector m_elements; }; diff --git a/src/mg_biome.cpp b/src/mg_biome.cpp index 13dc67e74..96fe443ab 100644 --- a/src/mg_biome.cpp +++ b/src/mg_biome.cpp @@ -32,10 +32,9 @@ const char *BiomeManager::ELEMENT_TITLE = "biome"; /////////////////////////////////////////////////////////////////////////////// -BiomeManager::BiomeManager(IGameDef *gamedef) +BiomeManager::BiomeManager(IGameDef *gamedef) : + GenElementManager(gamedef) { - m_resolver = gamedef->getNodeDefManager()->getResolver(); - // Create default biome to be used in case none exist Biome *b = new Biome; diff --git a/src/mg_biome.h b/src/mg_biome.h index 04567267f..f1539dacb 100644 --- a/src/mg_biome.h +++ b/src/mg_biome.h @@ -71,9 +71,6 @@ public: void calcBiomes(s16 sx, s16 sy, float *heat_map, float *humidity_map, s16 *height_map, u8 *biomeid_map); Biome *getBiome(float heat, float humidity, s16 y); - -private: - NodeResolver *m_resolver; }; #endif diff --git a/src/mg_decoration.cpp b/src/mg_decoration.cpp index 20b9fbda6..14368c091 100644 --- a/src/mg_decoration.cpp +++ b/src/mg_decoration.cpp @@ -38,6 +38,12 @@ FlagDesc flagdesc_deco[] = { /////////////////////////////////////////////////////////////////////////////// +DecorationManager::DecorationManager(IGameDef *gamedef) : + GenElementManager(gamedef) +{ +} + + size_t DecorationManager::placeAllDecos(Mapgen *mg, u32 seed, v3s16 nmin, v3s16 nmax) { size_t nplaced = 0; @@ -55,6 +61,19 @@ size_t DecorationManager::placeAllDecos(Mapgen *mg, u32 seed, v3s16 nmin, v3s16 } +void DecorationManager::clear() +{ + for (size_t i = 0; i < m_elements.size(); i++) { + Decoration *deco = (Decoration *)m_elements[i]; + if (!deco) + continue; + + deco->dropResolverEntries(m_resolver); + } + m_elements.clear(); +} + + /////////////////////////////////////////////////////////////////////////////// @@ -291,6 +310,13 @@ int DecoSimple::getHeight() } +void DecoSimple::dropResolverEntries(NodeResolver *resolver) +{ + resolver->cancelNodeList(&c_decos); + resolver->cancelNodeList(&c_spawnby); +} + + /////////////////////////////////////////////////////////////////////////////// diff --git a/src/mg_decoration.h b/src/mg_decoration.h index f360e3b76..dffb524f3 100644 --- a/src/mg_decoration.h +++ b/src/mg_decoration.h @@ -81,6 +81,7 @@ public: virtual size_t generate(Mapgen *mg, PseudoRandom *pr, s16 max_y, v3s16 p) = 0; virtual int getHeight() = 0; + virtual void dropResolverEntries(NodeResolver *resolver) {} }; class DecoSimple : public Decoration { @@ -96,6 +97,7 @@ public: bool canPlaceDecoration(ManualMapVoxelManipulator *vm, v3s16 p); virtual size_t generate(Mapgen *mg, PseudoRandom *pr, s16 max_y, v3s16 p); virtual int getHeight(); + virtual void dropResolverEntries(NodeResolver *resolver); }; class DecoSchematic : public Decoration { @@ -123,7 +125,7 @@ public: static const char *ELEMENT_TITLE; static const size_t ELEMENT_LIMIT = 0x10000; - DecorationManager(IGameDef *gamedef) {} + DecorationManager(IGameDef *gamedef); ~DecorationManager() {} Decoration *create(int type) @@ -140,6 +142,8 @@ public: } } + void clear(); + size_t placeAllDecos(Mapgen *mg, u32 seed, v3s16 nmin, v3s16 nmax); }; diff --git a/src/mg_ore.cpp b/src/mg_ore.cpp index d94b861c2..de3f6b0e5 100644 --- a/src/mg_ore.cpp +++ b/src/mg_ore.cpp @@ -37,6 +37,12 @@ FlagDesc flagdesc_ore[] = { /////////////////////////////////////////////////////////////////////////////// +OreManager::OreManager(IGameDef *gamedef) : + GenElementManager(gamedef) +{ +} + + size_t OreManager::placeAllOres(Mapgen *mg, u32 seed, v3s16 nmin, v3s16 nmax) { size_t nplaced = 0; @@ -54,6 +60,20 @@ size_t OreManager::placeAllOres(Mapgen *mg, u32 seed, v3s16 nmin, v3s16 nmax) } +void OreManager::clear() +{ + for (size_t i = 0; i < m_elements.size(); i++) { + Ore *ore = (Ore *)m_elements[i]; + if (!ore) + continue; + + m_resolver->cancelNodeList(&ore->c_wherein); + m_resolver->cancelNode(&ore->c_ore); + } + m_elements.clear(); +} + + /////////////////////////////////////////////////////////////////////////////// @@ -169,4 +189,3 @@ void OreSheet::generate(ManualMapVoxelManipulator *vm, int seed, } } } - diff --git a/src/mg_ore.h b/src/mg_ore.h index b6db860b9..585d58f37 100644 --- a/src/mg_ore.h +++ b/src/mg_ore.h @@ -99,7 +99,7 @@ public: static const char *ELEMENT_TITLE; static const size_t ELEMENT_LIMIT = 0x10000; - OreManager(IGameDef *gamedef) {} + OreManager(IGameDef *gamedef); ~OreManager() {} Ore *create(int type) @@ -116,6 +116,8 @@ public: } } + void clear(); + size_t placeAllOres(Mapgen *mg, u32 seed, v3s16 nmin, v3s16 nmax); }; diff --git a/src/mg_schematic.cpp b/src/mg_schematic.cpp index bb60cf5fd..a78824647 100644 --- a/src/mg_schematic.cpp +++ b/src/mg_schematic.cpp @@ -30,6 +30,14 @@ with this program; if not, write to the Free Software Foundation, Inc., const char *SchematicManager::ELEMENT_TITLE = "schematic"; +/////////////////////////////////////////////////////////////////////////////// + + +SchematicManager::SchematicManager(IGameDef *gamedef) : + GenElementManager(gamedef) +{ +} + /////////////////////////////////////////////////////////////////////////////// diff --git a/src/mg_schematic.h b/src/mg_schematic.h index 9d4d4a716..df54d79ba 100644 --- a/src/mg_schematic.h +++ b/src/mg_schematic.h @@ -76,7 +76,7 @@ public: static const char *ELEMENT_TITLE; static const size_t ELEMENT_LIMIT = 0x10000; - SchematicManager(IGameDef *gamedef) {} + SchematicManager(IGameDef *gamedef); ~SchematicManager() {} Schematic *create(int type) diff --git a/src/script/lua_api/l_mapgen.cpp b/src/script/lua_api/l_mapgen.cpp index 3fe6fb991..177e0d85b 100644 --- a/src/script/lua_api/l_mapgen.cpp +++ b/src/script/lua_api/l_mapgen.cpp @@ -464,6 +464,20 @@ int ModApiMapgen::l_clear_registered_biomes(lua_State *L) return 0; } +int ModApiMapgen::l_clear_registered_decorations(lua_State *L) +{ + DecorationManager *dmgr = getServer(L)->getEmergeManager()->decomgr; + dmgr->clear(); + return 0; +} + +int ModApiMapgen::l_clear_registered_ores(lua_State *L) +{ + OreManager *omgr = getServer(L)->getEmergeManager()->oremgr; + omgr->clear(); + return 0; +} + // register_decoration({lots of stuff}) int ModApiMapgen::l_register_decoration(lua_State *L) { @@ -789,7 +803,10 @@ void ModApiMapgen::Initialize(lua_State *L, int top) API_FCT(register_biome); API_FCT(register_decoration); API_FCT(register_ore); + API_FCT(clear_registered_biomes); + API_FCT(clear_registered_decorations); + API_FCT(clear_registered_ores); API_FCT(create_schematic); API_FCT(place_schematic); diff --git a/src/script/lua_api/l_mapgen.h b/src/script/lua_api/l_mapgen.h index 76f60a2d7..87ae9fd81 100644 --- a/src/script/lua_api/l_mapgen.h +++ b/src/script/lua_api/l_mapgen.h @@ -55,6 +55,12 @@ private: // clear_registered_biomes() static int l_clear_registered_biomes(lua_State *L); + // clear_registered_decorations() + static int l_clear_registered_decorations(lua_State *L); + + // clear_registered_ores + static int l_clear_registered_ores(lua_State *L); + // create_schematic(p1, p2, probability_list, filename) static int l_create_schematic(lua_State *L); -- 2.25.1