]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/mg_decoration.cpp
Fix two reconnect bugs
[dragonfireclient.git] / src / mg_decoration.cpp
index dec82a6380a08bd5f62f7b0e9dba91307097a8cb..8b6abb5d5598da883ef654478ed89884bf6d97d6 100644 (file)
@@ -25,12 +25,12 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #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},
+       {"force_placement", DECO_FORCE_PLACEMENT},
+       {"liquid_surface", DECO_LIQUID_SURFACE},
        {NULL,             0}
 };
 
@@ -39,38 +39,29 @@ FlagDesc flagdesc_deco[] = {
 
 
 DecorationManager::DecorationManager(IGameDef *gamedef) :
-       GenElementManager(gamedef)
+       ObjDefManager(gamedef, OBJDEF_DECORATION)
 {
 }
 
 
-size_t DecorationManager::placeAllDecos(Mapgen *mg, u32 seed, v3s16 nmin, v3s16 nmax)
+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];
+       for (size_t i = 0; i != m_objects.size(); i++) {
+               Decoration *deco = (Decoration *)m_objects[i];
                if (!deco)
                        continue;
 
-               nplaced += deco->placeDeco(mg, seed, nmin, nmax);
-               seed++;
+               nplaced += deco->placeDeco(mg, blockseed, nmin, nmax);
+               blockseed++;
        }
 
        return nplaced;
 }
 
 
-void DecorationManager::clear()
-{
-       for (size_t i = 0; i < m_elements.size(); i++) {
-               Decoration *deco = (Decoration *)m_elements[i];
-               delete deco;
-       }
-       m_elements.clear();
-}
-
-
 ///////////////////////////////////////////////////////////////////////////////
 
 
@@ -88,23 +79,21 @@ Decoration::~Decoration()
 }
 
 
-void Decoration::resolveNodeNames(NodeResolveInfo *nri)
+void Decoration::resolveNodeNames()
 {
-       m_ndef->getIdsFromResolveInfo(nri, c_place_on);
+       getIdsFromNrBacklog(&c_place_on);
 }
 
 
 size_t Decoration::placeDeco(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
 {
-       PseudoRandom ps(blockseed + 53);
+       PcgRandom 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;
@@ -128,7 +117,15 @@ size_t Decoration::placeDeco(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
                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);
+               u32 deco_count = 0;
+               float deco_count_f = (float)area * nval;
+               if (deco_count_f >= 1.f) {
+                       deco_count = deco_count_f;
+               } else if (deco_count_f > 0.f) {
+                       // For low density decorations calculate a chance for 1 decoration
+                       if (ps.range(1000) <= deco_count_f * 1000.f)
+                               deco_count = 1;
+               }
 
                for (u32 i = 0; i < deco_count; i++) {
                        s16 x = ps.range(p2d_min.X, p2d_max.X);
@@ -136,22 +133,24 @@ size_t Decoration::placeDeco(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
 
                        int mapindex = carea_size * (z - nmin.Z) + (x - nmin.X);
 
-                       s16 y = mg->heightmap ?
-                                       mg->heightmap[mapindex] :
-                                       mg->findGroundLevel(v2s16(x, z), nmin.Y, nmax.Y);
+                       s16 y = -MAX_MAP_GENERATION_LIMIT;
+                       if (flags & DECO_LIQUID_SURFACE)
+                               y = mg->findLiquidSurface(v2s16(x, z), nmin.Y, nmax.Y);
+                       else if (mg->heightmap)
+                               y = mg->heightmap[mapindex];
+                       else
+                               y = mg->findGroundLevel(v2s16(x, z), nmin.Y, nmax.Y);
 
-                       if (y < nmin.Y     || y > nmax.Y ||
-                               y < height_min || y > height_max)
+                       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
                        }
@@ -167,8 +166,8 @@ size_t Decoration::placeDeco(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
                        }
 
                        v3s16 pos(x, y, z);
-                       if (generate(mg, &ps, max_y, pos))
-                               mg->gennotify.addEvent(GENNOTIFY_DECORATION, pos, id);
+                       if (generate(mg->vm, &ps, pos))
+                               mg->gennotify.addEvent(GENNOTIFY_DECORATION, pos, index);
                }
        }
 
@@ -179,12 +178,12 @@ size_t Decoration::placeDeco(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
 #if 0
 void Decoration::placeCutoffs(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
 {
-       PseudoRandom pr(blockseed + 53);
+       PcgRandom pr(blockseed + 53);
        std::vector<CutoffData> handled_cutoffs;
 
        // 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;
@@ -215,7 +214,7 @@ void Decoration::placeCutoffs(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
 
        // 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) {
 
@@ -233,15 +232,15 @@ void Decoration::placeCutoffs(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
 ///////////////////////////////////////////////////////////////////////////////
 
 
-void DecoSimple::resolveNodeNames(NodeResolveInfo *nri)
+void DecoSimple::resolveNodeNames()
 {
-       Decoration::resolveNodeNames(nri);
-       m_ndef->getIdsFromResolveInfo(nri, c_decos);
-       m_ndef->getIdsFromResolveInfo(nri, c_spawnby);
+       Decoration::resolveNodeNames();
+       getIdsFromNrBacklog(&c_decos);
+       getIdsFromNrBacklog(&c_spawnby);
 }
 
 
-bool DecoSimple::canPlaceDecoration(ManualMapVoxelManipulator *vm, v3s16 p)
+bool DecoSimple::canPlaceDecoration(MMVManip *vm, v3s16 p)
 {
        // Don't bother if there aren't any decorations to place
        if (c_decos.size() == 0)
@@ -258,7 +257,7 @@ bool DecoSimple::canPlaceDecoration(ManualMapVoxelManipulator *vm, v3s16 p)
                return true;
 
        int nneighs = 0;
-       v3s16 dirs[8] = {
+       v3s16 dirs[16] = {
                v3s16( 0, 0,  1),
                v3s16( 0, 0, -1),
                v3s16( 1, 0,  0),
@@ -266,7 +265,16 @@ bool DecoSimple::canPlaceDecoration(ManualMapVoxelManipulator *vm, v3s16 p)
                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
@@ -286,10 +294,8 @@ bool DecoSimple::canPlaceDecoration(ManualMapVoxelManipulator *vm, v3s16 p)
 }
 
 
-size_t DecoSimple::generate(Mapgen *mg, PseudoRandom *pr, s16 max_y, v3s16 p)
+size_t DecoSimple::generate(MMVManip *vm, PcgRandom *pr, v3s16 p)
 {
-       ManualMapVoxelManipulator *vm = mg->vm;
-
        if (!canPlaceDecoration(vm, p))
                return 0;
 
@@ -298,7 +304,7 @@ size_t DecoSimple::generate(Mapgen *mg, PseudoRandom *pr, s16 max_y, v3s16 p)
        s16 height = (deco_height_max > 0) ?
                pr->range(deco_height, deco_height_max) : deco_height;
 
-       height = MYMIN(height, max_y - p.Y);
+       bool force_placement = (flags & DECO_FORCE_PLACEMENT);
 
        v3s16 em = vm->m_area.getExtent();
        u32 vi = vm->m_area.index(p);
@@ -306,7 +312,8 @@ size_t DecoSimple::generate(Mapgen *mg, PseudoRandom *pr, s16 max_y, v3s16 p)
                vm->m_area.add_y(em, vi, 1);
 
                content_t c = vm->m_data[vi].getContent();
-               if (c != CONTENT_AIR && c != CONTENT_IGNORE)
+               if (c != CONTENT_AIR && c != CONTENT_IGNORE &&
+                               !force_placement)
                        break;
 
                vm->m_data[vi] = MapNode(c_place);
@@ -325,26 +332,37 @@ int DecoSimple::getHeight()
 ///////////////////////////////////////////////////////////////////////////////
 
 
-size_t DecoSchematic::generate(Mapgen *mg, PseudoRandom *pr, s16 max_y, v3s16 p)
+DecoSchematic::DecoSchematic()
 {
-       ManualMapVoxelManipulator *vm = mg->vm;
+       schematic = NULL;
+}
 
-       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;
+
+size_t DecoSchematic::generate(MMVManip *vm, PcgRandom *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;
 
-       schematic->blitToVManip(p, vm, rot, false, mg->ndef);
+       bool force_placement = (flags & DECO_FORCE_PLACEMENT);
+
+       schematic->blitToVManip(vm, p, rot, force_placement);
 
        return 1;
 }