]> git.lizzy.rs Git - minetest.git/blobdiff - src/mg_decoration.cpp
Fix logic of checkbox formspec element validity checking
[minetest.git] / src / mg_decoration.cpp
index 7f6126bfcf8dcdf5312aee683c9cb0c71d882989..a67c3cd8cfd325ce863b5b058f066ba9654bece8 100644 (file)
@@ -20,44 +20,82 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "mg_decoration.h"
 #include "mg_schematic.h"
 #include "mapgen.h"
+#include "noise.h"
 #include "map.h"
 #include "log.h"
 #include "util/numeric.h"
 
+const char *DecorationManager::ELEMENT_TITLE = "decoration";
+
+FlagDesc flagdesc_deco[] = {
+       {"place_center_x", DECO_PLACE_CENTER_X},
+       {"place_center_y", DECO_PLACE_CENTER_Y},
+       {"place_center_z", DECO_PLACE_CENTER_Z},
+       {NULL,             0}
+};
+
+
 ///////////////////////////////////////////////////////////////////////////////
 
 
-Decoration *createDecoration(DecorationType type)
+DecorationManager::DecorationManager(IGameDef *gamedef) :
+       GenElementManager(gamedef)
+{
+}
+
+
+size_t DecorationManager::placeAllDecos(Mapgen *mg, u32 blockseed,
+       v3s16 nmin, v3s16 nmax)
+{
+       size_t nplaced = 0;
+
+       for (size_t i = 0; i != m_elements.size(); i++) {
+               Decoration *deco = (Decoration *)m_elements[i];
+               if (!deco)
+                       continue;
+
+               nplaced += deco->placeDeco(mg, blockseed, nmin, nmax);
+               blockseed++;
+       }
+
+       return nplaced;
+}
+
+
+void DecorationManager::clear()
 {
-       switch (type) {
-       case DECO_SIMPLE:
-               return new DecoSimple;
-       case DECO_SCHEMATIC:
-               return new DecoSchematic;
-       //case DECO_LSYSTEM:
-       //      return new DecoLSystem;
-       default:
-               return NULL;
+       for (size_t i = 0; i < m_elements.size(); i++) {
+               Decoration *deco = (Decoration *)m_elements[i];
+               delete deco;
        }
+       m_elements.clear();
 }
 
 
+///////////////////////////////////////////////////////////////////////////////
+
+
 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(NodeResolveInfo *nri)
+{
+       m_ndef->getIdsFromResolveInfo(nri, 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;
@@ -88,8 +126,8 @@ void Decoration::placeDeco(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
                );
 
                // 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);
 
@@ -103,7 +141,8 @@ void Decoration::placeDeco(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
                                        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();
@@ -121,16 +160,20 @@ void Decoration::placeDeco(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
                        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, max_y, pos))
+                               mg->gennotify.addEvent(GENNOTIFY_DECORATION, pos, id);
                }
        }
+
+       return 0;
 }
 
 
@@ -190,7 +233,16 @@ void Decoration::placeCutoffs(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
 
 ///////////////////////////////////////////////////////////////////////////////
 
-bool DecoSimple::canPlaceDecoration(ManualMapVoxelManipulator *vm, v3s16 p)
+
+void DecoSimple::resolveNodeNames(NodeResolveInfo *nri)
+{
+       Decoration::resolveNodeNames(nri);
+       m_ndef->getIdsFromResolveInfo(nri, c_decos);
+       m_ndef->getIdsFromResolveInfo(nri, 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)
@@ -235,12 +287,10 @@ bool DecoSimple::canPlaceDecoration(ManualMapVoxelManipulator *vm, v3s16 p)
 }
 
 
-void DecoSimple::generate(Mapgen *mg, PseudoRandom *pr, s16 max_y, v3s16 p)
+size_t DecoSimple::generate(MMVManip *vm, PseudoRandom *pr, s16 max_y, 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)];
 
@@ -260,6 +310,8 @@ void DecoSimple::generate(Mapgen *mg, PseudoRandom *pr, s16 max_y, v3s16 p)
 
                vm->m_data[vi] = MapNode(c_place);
        }
+
+       return 1;
 }
 
 
@@ -269,7 +321,36 @@ int DecoSimple::getHeight()
 }
 
 
-std::string DecoSimple::getName()
+///////////////////////////////////////////////////////////////////////////////
+
+
+size_t DecoSchematic::generate(MMVManip *vm, PseudoRandom *pr, s16 max_y, v3s16 p)
+{
+       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;
+
+       if (!vm->m_area.contains(p))
+               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;
+
+       Rotation rot = (rotation == ROTATE_RAND) ?
+               (Rotation)pr->range(ROTATE_0, ROTATE_270) : rotation;
+
+       schematic->blitToVManip(p, vm, rot, false, m_ndef);
+
+       return 1;
+}
+
+
+int DecoSchematic::getHeight()
 {
-       return "";
+       return schematic->size.Y;
 }