#include "mg_decoration.h"
#include "mg_schematic.h"
#include "mapgen.h"
+#include "noise.h"
#include "map.h"
#include "log.h"
#include "util/numeric.h"
+FlagDesc flagdesc_deco[] = {
+ {"place_center_x", DECO_PLACE_CENTER_X},
+ {"place_center_y", DECO_PLACE_CENTER_Y},
+ {"place_center_z", DECO_PLACE_CENTER_Z},
+ {"force_placement", DECO_FORCE_PLACEMENT},
+ {NULL, 0}
+};
+
+
///////////////////////////////////////////////////////////////////////////////
-Decoration *createDecoration(DecorationType type)
+DecorationManager::DecorationManager(IGameDef *gamedef) :
+ ObjDefManager(gamedef, OBJDEF_DECORATION)
+{
+}
+
+
+size_t DecorationManager::placeAllDecos(Mapgen *mg, u32 blockseed,
+ v3s16 nmin, v3s16 nmax)
{
- switch (type) {
- case DECO_SIMPLE:
- return new DecoSimple;
- case DECO_SCHEMATIC:
- return new DecoSchematic;
- //case DECO_LSYSTEM:
- // return new DecoLSystem;
- default:
- return NULL;
+ size_t nplaced = 0;
+
+ for (size_t i = 0; i != m_objects.size(); i++) {
+ Decoration *deco = (Decoration *)m_objects[i];
+ if (!deco)
+ continue;
+
+ nplaced += deco->placeDeco(mg, blockseed, nmin, nmax);
+ blockseed++;
}
+
+ return nplaced;
}
+///////////////////////////////////////////////////////////////////////////////
+
+
Decoration::Decoration()
{
mapseed = 0;
- np = NULL;
fill_ratio = 0;
sidelen = 1;
+ flags = 0;
}
Decoration::~Decoration()
{
- delete np;
}
-void Decoration::placeDeco(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
+void Decoration::resolveNodeNames()
+{
+ getIdsFromNrBacklog(&c_place_on);
+}
+
+
+size_t Decoration::placeDeco(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
{
PseudoRandom ps(blockseed + 53);
int carea_size = nmax.X - nmin.X + 1;
// Divide area into parts
- if (carea_size % sidelen) {
- errorstream << "Decoration::placeDeco: chunk size is not divisible by "
- "sidelen; setting sidelen to " << carea_size << std::endl;
+ // If chunksize is changed it may no longer be divisable by sidelen
+ if (carea_size % sidelen)
sidelen = carea_size;
- }
s16 divlen = carea_size / sidelen;
int area = sidelen * sidelen;
);
// Amount of decorations
- float nval = np ?
- NoisePerlin2D(np, p2d_center.X, p2d_center.Y, mapseed) :
+ float nval = (flags & DECO_USE_NOISE) ?
+ NoisePerlin2D(&np, p2d_center.X, p2d_center.Y, mapseed) :
fill_ratio;
u32 deco_count = area * MYMAX(nval, 0.f);
mg->heightmap[mapindex] :
mg->findGroundLevel(v2s16(x, z), nmin.Y, nmax.Y);
- if (y < nmin.Y || y > nmax.Y)
+ if (y < nmin.Y || y > nmax.Y ||
+ y < y_min || y > y_max)
continue;
- int height = getHeight();
- int max_y = nmax.Y;// + MAP_BLOCKSIZE - 1;
- if (y + 1 + height > max_y) {
+ if (y + getHeight() >= mg->vm->m_area.MaxEdge.Y) {
continue;
#if 0
printf("Decoration at (%d %d %d) cut off\n", x, y, z);
//add to queue
- JMutexAutoLock cutofflock(cutoff_mutex);
+ MutexAutoLock cutofflock(cutoff_mutex);
cutoffs.push_back(CutoffData(x, y, z, height));
#endif
}
if (mg->biomemap) {
std::set<u8>::iterator iter;
- if (biomes.size()) {
+ if (!biomes.empty()) {
iter = biomes.find(mg->biomemap[mapindex]);
if (iter == biomes.end())
continue;
}
}
- generate(mg, &ps, max_y, v3s16(x, y, z));
+ v3s16 pos(x, y, z);
+ if (generate(mg->vm, &ps, pos))
+ mg->gennotify.addEvent(GENNOTIFY_DECORATION, pos, index);
}
}
+
+ return 0;
}
// Copy over the cutoffs we're interested in so we don't needlessly hold a lock
{
- JMutexAutoLock cutofflock(cutoff_mutex);
+ MutexAutoLock cutofflock(cutoff_mutex);
for (std::list<CutoffData>::iterator i = cutoffs.begin();
i != cutoffs.end(); ++i) {
CutoffData cutoff = *i;
// Remove cutoffs that were handled from the cutoff list
{
- JMutexAutoLock cutofflock(cutoff_mutex);
+ MutexAutoLock cutofflock(cutoff_mutex);
for (std::list<CutoffData>::iterator i = cutoffs.begin();
i != cutoffs.end(); ++i) {
///////////////////////////////////////////////////////////////////////////////
-bool DecoSimple::canPlaceDecoration(ManualMapVoxelManipulator *vm, v3s16 p)
+
+void DecoSimple::resolveNodeNames()
+{
+ Decoration::resolveNodeNames();
+ getIdsFromNrBacklog(&c_decos);
+ getIdsFromNrBacklog(&c_spawnby);
+}
+
+
+bool DecoSimple::canPlaceDecoration(MMVManip *vm, v3s16 p)
{
// Don't bother if there aren't any decorations to place
if (c_decos.size() == 0)
return true;
int nneighs = 0;
- v3s16 dirs[8] = {
+ v3s16 dirs[16] = {
v3s16( 0, 0, 1),
v3s16( 0, 0, -1),
v3s16( 1, 0, 0),
v3s16( 1, 0, 1),
v3s16(-1, 0, 1),
v3s16(-1, 0, -1),
- v3s16( 1, 0, -1)
+ v3s16( 1, 0, -1),
+
+ v3s16( 0, 1, 1),
+ v3s16( 0, 1, -1),
+ v3s16( 1, 1, 0),
+ v3s16(-1, 1, 0),
+ v3s16( 1, 1, 1),
+ v3s16(-1, 1, 1),
+ v3s16(-1, 1, -1),
+ v3s16( 1, 1, -1)
};
// Check a Moore neighborhood if there are enough spawnby nodes
}
-void DecoSimple::generate(Mapgen *mg, PseudoRandom *pr, s16 max_y, v3s16 p)
+size_t DecoSimple::generate(MMVManip *vm, PseudoRandom *pr, v3s16 p)
{
- ManualMapVoxelManipulator *vm = mg->vm;
-
if (!canPlaceDecoration(vm, p))
- return;
+ return 0;
content_t c_place = c_decos[pr->range(0, c_decos.size() - 1)];
s16 height = (deco_height_max > 0) ?
pr->range(deco_height, deco_height_max) : deco_height;
- height = MYMIN(height, max_y - p.Y);
-
v3s16 em = vm->m_area.getExtent();
u32 vi = vm->m_area.index(p);
for (int i = 0; i < height; i++) {
vm->m_data[vi] = MapNode(c_place);
}
+
+ return 1;
}
}
-std::string DecoSimple::getName()
+///////////////////////////////////////////////////////////////////////////////
+
+
+DecoSchematic::DecoSchematic()
+{
+ schematic = NULL;
+}
+
+
+size_t DecoSchematic::generate(MMVManip *vm, PseudoRandom *pr, v3s16 p)
+{
+ // Schematic could have been unloaded but not the decoration
+ // In this case generate() does nothing (but doesn't *fail*)
+ if (schematic == NULL)
+ return 0;
+
+ u32 vi = vm->m_area.index(p);
+ content_t c = vm->m_data[vi].getContent();
+ if (!CONTAINS(c_place_on, c))
+ return 0;
+
+ if (flags & DECO_PLACE_CENTER_X)
+ p.X -= (schematic->size.X - 1) / 2;
+ if (flags & DECO_PLACE_CENTER_Y)
+ p.Y -= (schematic->size.Y - 1) / 2;
+ if (flags & DECO_PLACE_CENTER_Z)
+ p.Z -= (schematic->size.Z - 1) / 2;
+
+ Rotation rot = (rotation == ROTATE_RAND) ?
+ (Rotation)pr->range(ROTATE_0, ROTATE_270) : rotation;
+
+ bool force_placement = (flags & DECO_FORCE_PLACEMENT);
+
+ schematic->blitToVManip(p, vm, rot, force_placement);
+
+ return 1;
+}
+
+
+int DecoSchematic::getHeight()
{
- return "";
+ return schematic->size.Y;
}