X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Fmg_decoration.cpp;h=4e53e037f5d3f3505251fbeffdf375f32b565d41;hb=d382483fa76028c2d34f75067bff45306c6da34e;hp=846d6130b6fc1f9ecfb967e3d863004f4379adc5;hpb=1384108f8c32f309852c1d1665a613f2a3e3fcc2;p=dragonfireclient.git diff --git a/src/mg_decoration.cpp b/src/mg_decoration.cpp index 846d6130b..4e53e037f 100644 --- a/src/mg_decoration.cpp +++ b/src/mg_decoration.cpp @@ -1,6 +1,7 @@ /* Minetest -Copyright (C) 2010-2014 kwolekr, Ryan Kwolek +Copyright (C) 2014-2016 kwolekr, Ryan Kwolek +Copyright (C) 2015-2017 paramat This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by @@ -24,14 +25,16 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "map.h" #include "log.h" #include "util/numeric.h" +#include + FlagDesc flagdesc_deco[] = { - {"place_center_x", DECO_PLACE_CENTER_X}, - {"place_center_y", DECO_PLACE_CENTER_Y}, - {"place_center_z", DECO_PLACE_CENTER_Z}, + {"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} + {"liquid_surface", DECO_LIQUID_SURFACE}, + {NULL, 0} }; @@ -45,7 +48,7 @@ DecorationManager::DecorationManager(IGameDef *gamedef) : size_t DecorationManager::placeAllDecos(Mapgen *mg, u32 blockseed, - v3s16 nmin, v3s16 nmax) + v3s16 nmin, v3s16 nmax, s16 deco_zero_level) { size_t nplaced = 0; @@ -54,7 +57,7 @@ size_t DecorationManager::placeAllDecos(Mapgen *mg, u32 blockseed, if (!deco) continue; - nplaced += deco->placeDeco(mg, blockseed, nmin, nmax); + nplaced += deco->placeDeco(mg, blockseed, nmin, nmax, deco_zero_level); blockseed++; } @@ -64,30 +67,75 @@ size_t DecorationManager::placeAllDecos(Mapgen *mg, u32 blockseed, /////////////////////////////////////////////////////////////////////////////// - -Decoration::Decoration() +void Decoration::resolveNodeNames() { - mapseed = 0; - fill_ratio = 0; - sidelen = 1; - flags = 0; + getIdsFromNrBacklog(&c_place_on); + getIdsFromNrBacklog(&c_spawnby); } -Decoration::~Decoration() +bool Decoration::canPlaceDecoration(MMVManip *vm, v3s16 p) { -} + // Check if the decoration can be placed on this node + u32 vi = vm->m_area.index(p); + if (!CONTAINS(c_place_on, vm->m_data[vi].getContent())) + return false; + // Don't continue if there are no spawnby constraints + if (nspawnby == -1) + return true; -void Decoration::resolveNodeNames() -{ - getIdsFromNrBacklog(&c_place_on); + int nneighs = 0; + static const v3s16 dirs[16] = { + v3s16( 0, 0, 1), + v3s16( 0, 0, -1), + v3s16( 1, 0, 0), + v3s16(-1, 0, 0), + 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 these 16 neighbouring nodes for enough spawnby nodes + for (size_t i = 0; i != ARRLEN(dirs); i++) { + u32 index = vm->m_area.index(p + dirs[i]); + if (!vm->m_area.contains(index)) + continue; + + if (CONTAINS(c_spawnby, vm->m_data[index].getContent())) + nneighs++; + } + + if (nneighs < nspawnby) + return false; + + return true; } -size_t Decoration::placeDeco(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax) +size_t Decoration::placeDeco(Mapgen *mg, u32 blockseed, + v3s16 nmin, v3s16 nmax, s16 deco_zero_level) { - PseudoRandom ps(blockseed + 53); + // Decoration y_min / y_max is displaced by deco_zero_level or remains + // unchanged. Any decoration with a limit at +-MAX_MAP_GENERATION_LIMIT is + // considered to have that limit at +-infinity, so we do not alter that limit. + s32 y_min_disp = (y_min <= -MAX_MAP_GENERATION_LIMIT) ? + -MAX_MAP_GENERATION_LIMIT : y_min + deco_zero_level; + + s32 y_max_disp = (y_max >= MAX_MAP_GENERATION_LIMIT) ? + MAX_MAP_GENERATION_LIMIT : y_max + deco_zero_level; + + PcgRandom ps(blockseed + 53); int carea_size = nmax.X - nmin.X + 1; // Divide area into parts @@ -117,7 +165,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); @@ -133,11 +189,10 @@ size_t Decoration::placeDeco(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax) else y = mg->findGroundLevel(v2s16(x, z), nmin.Y, nmax.Y); - if (y < nmin.Y || y > nmax.Y || - y < y_min || y > y_max) + if (y < y_min_disp || y > y_max_disp || y < nmin.Y || y > nmax.Y) continue; - if (y + getHeight() >= mg->vm->m_area.MaxEdge.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); @@ -147,14 +202,11 @@ size_t Decoration::placeDeco(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax) #endif } - if (mg->biomemap) { - std::set::iterator iter; - - if (!biomes.empty()) { - iter = biomes.find(mg->biomemap[mapindex]); - if (iter == biomes.end()) - continue; - } + if (mg->biomemap && !biomes.empty()) { + std::unordered_set::const_iterator iter = + biomes.find(mg->biomemap[mapindex]); + if (iter == biomes.end()) + continue; } v3s16 pos(x, y, z); @@ -170,7 +222,7 @@ 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 handled_cutoffs; // Copy over the cutoffs we're interested in so we don't needlessly hold a lock @@ -228,66 +280,15 @@ void DecoSimple::resolveNodeNames() { Decoration::resolveNodeNames(); getIdsFromNrBacklog(&c_decos); - getIdsFromNrBacklog(&c_spawnby); } -bool DecoSimple::canPlaceDecoration(MMVManip *vm, v3s16 p) +size_t DecoSimple::generate(MMVManip *vm, PcgRandom *pr, v3s16 p) { // Don't bother if there aren't any decorations to place if (c_decos.size() == 0) - return false; - - u32 vi = vm->m_area.index(p); - - // Check if the decoration can be placed on this node - if (!CONTAINS(c_place_on, vm->m_data[vi].getContent())) - return false; - - // Don't continue if there are no spawnby constraints - if (nspawnby == -1) - return true; - - int nneighs = 0; - v3s16 dirs[16] = { - v3s16( 0, 0, 1), - v3s16( 0, 0, -1), - v3s16( 1, 0, 0), - v3s16(-1, 0, 0), - 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 - for (size_t i = 0; i != ARRLEN(dirs); i++) { - u32 index = vm->m_area.index(p + dirs[i]); - if (!vm->m_area.contains(index)) - continue; - - if (CONTAINS(c_spawnby, vm->m_data[index].getContent())) - nneighs++; - } - - if (nneighs < nspawnby) - return false; - - return true; -} - + return 0; -size_t DecoSimple::generate(MMVManip *vm, PseudoRandom *pr, v3s16 p) -{ if (!canPlaceDecoration(vm, p)) return 0; @@ -296,16 +297,19 @@ size_t DecoSimple::generate(MMVManip *vm, PseudoRandom *pr, v3s16 p) s16 height = (deco_height_max > 0) ? pr->range(deco_height, deco_height_max) : deco_height; - v3s16 em = vm->m_area.getExtent(); + bool force_placement = (flags & DECO_FORCE_PLACEMENT); + + const v3s16 &em = vm->m_area.getExtent(); u32 vi = vm->m_area.index(p); for (int i = 0; i < height; i++) { 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); + vm->m_data[vi] = MapNode(c_place, 0, deco_param2); } return 1; @@ -320,23 +324,14 @@ int DecoSimple::getHeight() /////////////////////////////////////////////////////////////////////////////// - -DecoSchematic::DecoSchematic() -{ - schematic = NULL; -} - - -size_t DecoSchematic::generate(MMVManip *vm, PseudoRandom *pr, v3s16 p) +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)) + if (!canPlaceDecoration(vm, p)) return 0; if (flags & DECO_PLACE_CENTER_X) @@ -359,5 +354,9 @@ size_t DecoSchematic::generate(MMVManip *vm, PseudoRandom *pr, v3s16 p) int DecoSchematic::getHeight() { - return schematic->size.Y; + // Account for a schematic being sunk into the ground by flag. + // When placed normally account for how a schematic is placed + // sunk 1 node into the ground. + return (flags & DECO_PLACE_CENTER_Y) ? + (schematic->size.Y - 1) / 2 : schematic->size.Y - 1; }