Skip to content

Commit

Permalink
Add minetest.clear_registered_decorations() and clear_registered_ores()
Browse files Browse the repository at this point in the history
  • Loading branch information
kwolekr committed Dec 12, 2014
1 parent cec141a commit cf8213e
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 11 deletions.
7 changes: 7 additions & 0 deletions src/mapgen.cpp
Expand Up @@ -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"
Expand Down Expand Up @@ -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++)
Expand Down
3 changes: 2 additions & 1 deletion src/mapgen.h
Expand Up @@ -178,7 +178,7 @@ class GenElementManager {
static const char *ELEMENT_TITLE;
static const size_t ELEMENT_LIMIT = -1;

GenElementManager() {}
GenElementManager(IGameDef *gamedef);
virtual ~GenElementManager();

virtual GenElement *create(int type) = 0;
Expand All @@ -192,6 +192,7 @@ class GenElementManager {
virtual GenElement *getByName(const std::string &name);

protected:
NodeResolver *m_resolver;
std::vector<GenElement *> m_elements;
};

Expand Down
5 changes: 2 additions & 3 deletions src/mg_biome.cpp
Expand Up @@ -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;

Expand Down
3 changes: 0 additions & 3 deletions src/mg_biome.h
Expand Up @@ -71,9 +71,6 @@ class BiomeManager : public GenElementManager {
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
Expand Down
26 changes: 26 additions & 0 deletions src/mg_decoration.cpp
Expand Up @@ -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;
Expand All @@ -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();
}


///////////////////////////////////////////////////////////////////////////////


Expand Down Expand Up @@ -291,6 +310,13 @@ int DecoSimple::getHeight()
}


void DecoSimple::dropResolverEntries(NodeResolver *resolver)
{
resolver->cancelNodeList(&c_decos);
resolver->cancelNodeList(&c_spawnby);
}


///////////////////////////////////////////////////////////////////////////////


Expand Down
6 changes: 5 additions & 1 deletion src/mg_decoration.h
Expand Up @@ -81,6 +81,7 @@ class Decoration : public GenElement {

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 {
Expand All @@ -96,6 +97,7 @@ class DecoSimple : public Decoration {
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 {
Expand Down Expand Up @@ -123,7 +125,7 @@ class DecorationManager : public GenElementManager {
static const char *ELEMENT_TITLE;
static const size_t ELEMENT_LIMIT = 0x10000;

DecorationManager(IGameDef *gamedef) {}
DecorationManager(IGameDef *gamedef);
~DecorationManager() {}

Decoration *create(int type)
Expand All @@ -140,6 +142,8 @@ class DecorationManager : public GenElementManager {
}
}

void clear();

size_t placeAllDecos(Mapgen *mg, u32 seed, v3s16 nmin, v3s16 nmax);
};

Expand Down
21 changes: 20 additions & 1 deletion src/mg_ore.cpp
Expand Up @@ -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;
Expand All @@ -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();
}


///////////////////////////////////////////////////////////////////////////////


Expand Down Expand Up @@ -169,4 +189,3 @@ void OreSheet::generate(ManualMapVoxelManipulator *vm, int seed,
}
}
}

4 changes: 3 additions & 1 deletion src/mg_ore.h
Expand Up @@ -99,7 +99,7 @@ class OreManager : public GenElementManager {
static const char *ELEMENT_TITLE;
static const size_t ELEMENT_LIMIT = 0x10000;

OreManager(IGameDef *gamedef) {}
OreManager(IGameDef *gamedef);
~OreManager() {}

Ore *create(int type)
Expand All @@ -116,6 +116,8 @@ class OreManager : public GenElementManager {
}
}

void clear();

size_t placeAllOres(Mapgen *mg, u32 seed, v3s16 nmin, v3s16 nmax);
};

Expand Down
8 changes: 8 additions & 0 deletions src/mg_schematic.cpp
Expand Up @@ -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)
{
}


///////////////////////////////////////////////////////////////////////////////

Expand Down
2 changes: 1 addition & 1 deletion src/mg_schematic.h
Expand Up @@ -76,7 +76,7 @@ class SchematicManager : public GenElementManager {
static const char *ELEMENT_TITLE;
static const size_t ELEMENT_LIMIT = 0x10000;

SchematicManager(IGameDef *gamedef) {}
SchematicManager(IGameDef *gamedef);
~SchematicManager() {}

Schematic *create(int type)
Expand Down
17 changes: 17 additions & 0 deletions src/script/lua_api/l_mapgen.cpp
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions src/script/lua_api/l_mapgen.h
Expand Up @@ -55,6 +55,12 @@ class ModApiMapgen : public ModApiBase {
// 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);

Expand Down

0 comments on commit cf8213e

Please sign in to comment.