Skip to content

Commit 406d9ba

Browse files
committedApr 17, 2015
Schematics: Remove referenced schematics from Decorations on clear
1 parent f0a1379 commit 406d9ba

File tree

6 files changed

+67
-38
lines changed

6 files changed

+67
-38
lines changed
 

‎src/mg_biome.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class BiomeManager : public ObjDefManager {
6161
static const char *OBJECT_TITLE;
6262

6363
BiomeManager(IGameDef *gamedef);
64-
~BiomeManager();
64+
virtual ~BiomeManager();
6565

6666
const char *getObjectTitle() const
6767
{
@@ -73,7 +73,7 @@ class BiomeManager : public ObjDefManager {
7373
return new Biome;
7474
}
7575

76-
void clear();
76+
virtual void clear();
7777

7878
void calcBiomes(s16 sx, s16 sy, float *heat_map, float *humidity_map,
7979
s16 *height_map, u8 *biomeid_map);

‎src/mg_decoration.cpp

+12-10
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,6 @@ size_t DecorationManager::placeAllDecos(Mapgen *mg, u32 blockseed,
6161
}
6262

6363

64-
void DecorationManager::clear()
65-
{
66-
for (size_t i = 0; i < m_objects.size(); i++) {
67-
Decoration *deco = (Decoration *)m_objects[i];
68-
delete deco;
69-
}
70-
m_objects.clear();
71-
}
72-
73-
7464
///////////////////////////////////////////////////////////////////////////////
7565

7666

@@ -320,8 +310,20 @@ int DecoSimple::getHeight()
320310
///////////////////////////////////////////////////////////////////////////////
321311

322312

313+
DecoSchematic::DecoSchematic() :
314+
Decoration::Decoration()
315+
{
316+
schematic = NULL;
317+
}
318+
319+
323320
size_t DecoSchematic::generate(MMVManip *vm, PseudoRandom *pr, v3s16 p)
324321
{
322+
// Schematic could have been unloaded but not the decoration
323+
// In this case generate() does nothing (but doesn't *fail*)
324+
if (schematic == NULL)
325+
return 0;
326+
325327
if (flags & DECO_PLACE_CENTER_X)
326328
p.X -= (schematic->size.X + 1) / 2;
327329
if (flags & DECO_PLACE_CENTER_Y)

‎src/mg_decoration.h

+21-24
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,16 @@ struct CutoffData {
6161

6262
class Decoration : public ObjDef, public NodeResolver {
6363
public:
64-
INodeDefManager *ndef;
64+
Decoration();
65+
virtual ~Decoration();
66+
67+
virtual void resolveNodeNames();
68+
69+
size_t placeDeco(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax);
70+
//size_t placeCutoffs(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax);
71+
72+
virtual size_t generate(MMVManip *vm, PseudoRandom *pr, v3s16 p) = 0;
73+
virtual int getHeight() = 0;
6574

6675
u32 flags;
6776
int mapseed;
@@ -75,42 +84,32 @@ class Decoration : public ObjDef, public NodeResolver {
7584
std::set<u8> biomes;
7685
//std::list<CutoffData> cutoffs;
7786
//JMutex cutoff_mutex;
78-
79-
Decoration();
80-
virtual ~Decoration();
81-
82-
virtual void resolveNodeNames();
83-
84-
size_t placeDeco(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax);
85-
//size_t placeCutoffs(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax);
86-
87-
virtual size_t generate(MMVManip *vm, PseudoRandom *pr, v3s16 p) = 0;
88-
virtual int getHeight() = 0;
8987
};
9088

9189
class DecoSimple : public Decoration {
9290
public:
91+
virtual size_t generate(MMVManip *vm, PseudoRandom *pr, v3s16 p);
92+
bool canPlaceDecoration(MMVManip *vm, v3s16 p);
93+
virtual int getHeight();
94+
95+
virtual void resolveNodeNames();
96+
9397
std::vector<content_t> c_decos;
9498
std::vector<content_t> c_spawnby;
9599
s16 deco_height;
96100
s16 deco_height_max;
97101
s16 nspawnby;
98-
99-
virtual void resolveNodeNames();
100-
101-
bool canPlaceDecoration(MMVManip *vm, v3s16 p);
102-
virtual size_t generate(MMVManip *vm, PseudoRandom *pr, v3s16 p);
103-
virtual int getHeight();
104102
};
105103

106104
class DecoSchematic : public Decoration {
107105
public:
108-
Rotation rotation;
109-
Schematic *schematic;
110-
std::string filename;
106+
DecoSchematic();
111107

112108
virtual size_t generate(MMVManip *vm, PseudoRandom *pr, v3s16 p);
113109
virtual int getHeight();
110+
111+
Rotation rotation;
112+
Schematic *schematic;
114113
};
115114

116115

@@ -124,7 +123,7 @@ class DecoLSystem : public Decoration {
124123
class DecorationManager : public ObjDefManager {
125124
public:
126125
DecorationManager(IGameDef *gamedef);
127-
~DecorationManager() {}
126+
virtual ~DecorationManager() {}
128127

129128
const char *getObjectTitle() const
130129
{
@@ -145,8 +144,6 @@ class DecorationManager : public ObjDefManager {
145144
}
146145
}
147146

148-
void clear();
149-
150147
size_t placeAllDecos(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax);
151148
};
152149

‎src/mg_ore.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class OreVein : public Ore {
115115
class OreManager : public ObjDefManager {
116116
public:
117117
OreManager(IGameDef *gamedef);
118-
~OreManager() {}
118+
virtual ~OreManager() {}
119119

120120
const char *getObjectTitle() const
121121
{

‎src/mg_schematic.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
1919

2020
#include <fstream>
2121
#include "mg_schematic.h"
22+
#include "gamedef.h"
2223
#include "mapgen.h"
24+
#include "emerge.h"
2325
#include "map.h"
2426
#include "mapblock.h"
2527
#include "log.h"
@@ -34,6 +36,28 @@ with this program; if not, write to the Free Software Foundation, Inc.,
3436
SchematicManager::SchematicManager(IGameDef *gamedef) :
3537
ObjDefManager(gamedef, OBJDEF_SCHEMATIC)
3638
{
39+
m_gamedef = gamedef;
40+
}
41+
42+
43+
void SchematicManager::clear()
44+
{
45+
EmergeManager *emerge = m_gamedef->getEmergeManager();
46+
47+
// Remove all dangling references in Decorations
48+
DecorationManager *decomgr = emerge->decomgr;
49+
for (size_t i = 0; i != decomgr->getNumObjects(); i++) {
50+
Decoration *deco = (Decoration *)decomgr->getRaw(i);
51+
52+
try {
53+
DecoSchematic *dschem = dynamic_cast<DecoSchematic *>(deco);
54+
if (dschem)
55+
dschem->schematic = NULL;
56+
} catch(std::bad_cast) {
57+
}
58+
}
59+
60+
ObjDefManager::clear();
3761
}
3862

3963

‎src/mg_schematic.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class Mapgen;
2929
class MMVManip;
3030
class PseudoRandom;
3131
class NodeResolver;
32+
class IGameDef;
3233

3334
/*
3435
Minetest Schematic File Format
@@ -121,7 +122,9 @@ class Schematic : public ObjDef, public NodeResolver {
121122
class SchematicManager : public ObjDefManager {
122123
public:
123124
SchematicManager(IGameDef *gamedef);
124-
~SchematicManager() {}
125+
virtual ~SchematicManager() {}
126+
127+
virtual void clear();
125128

126129
const char *getObjectTitle() const
127130
{
@@ -132,6 +135,9 @@ class SchematicManager : public ObjDefManager {
132135
{
133136
return new Schematic;
134137
}
138+
139+
private:
140+
IGameDef *m_gamedef;
135141
};
136142

137143
void build_nnlist_and_update_ids(MapNode *nodes, u32 nodecount,

0 commit comments

Comments
 (0)
Please sign in to comment.