3 Copyright (C) 2010-2014 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "mg_decoration.h"
21 #include "mg_schematic.h"
26 #include "util/numeric.h"
28 const char *DecorationManager::ELEMENT_TITLE = "decoration";
30 FlagDesc flagdesc_deco[] = {
31 {"place_center_x", DECO_PLACE_CENTER_X},
32 {"place_center_y", DECO_PLACE_CENTER_Y},
33 {"place_center_z", DECO_PLACE_CENTER_Z},
38 ///////////////////////////////////////////////////////////////////////////////
41 size_t DecorationManager::placeAllDecos(Mapgen *mg, u32 seed, v3s16 nmin, v3s16 nmax)
45 for (size_t i = 0; i != m_elements.size(); i++) {
46 Decoration *deco = (Decoration *)m_elements[i];
50 nplaced += deco->placeDeco(mg, seed, nmin, nmax);
58 ///////////////////////////////////////////////////////////////////////////////
61 Decoration::Decoration()
70 Decoration::~Decoration()
75 size_t Decoration::placeDeco(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
77 PseudoRandom ps(blockseed + 53);
78 int carea_size = nmax.X - nmin.X + 1;
80 // Divide area into parts
81 if (carea_size % sidelen) {
82 errorstream << "Decoration::placeDeco: chunk size is not divisible by "
83 "sidelen; setting sidelen to " << carea_size << std::endl;
87 s16 divlen = carea_size / sidelen;
88 int area = sidelen * sidelen;
90 for (s16 z0 = 0; z0 < divlen; z0++)
91 for (s16 x0 = 0; x0 < divlen; x0++) {
92 v2s16 p2d_center( // Center position of part of division
93 nmin.X + sidelen / 2 + sidelen * x0,
94 nmin.Z + sidelen / 2 + sidelen * z0
96 v2s16 p2d_min( // Minimum edge of part of division
97 nmin.X + sidelen * x0,
100 v2s16 p2d_max( // Maximum edge of part of division
101 nmin.X + sidelen + sidelen * x0 - 1,
102 nmin.Z + sidelen + sidelen * z0 - 1
105 // Amount of decorations
106 float nval = (flags & DECO_USE_NOISE) ?
107 NoisePerlin2D(&np, p2d_center.X, p2d_center.Y, mapseed) :
109 u32 deco_count = area * MYMAX(nval, 0.f);
111 for (u32 i = 0; i < deco_count; i++) {
112 s16 x = ps.range(p2d_min.X, p2d_max.X);
113 s16 z = ps.range(p2d_min.Y, p2d_max.Y);
115 int mapindex = carea_size * (z - nmin.Z) + (x - nmin.X);
117 s16 y = mg->heightmap ?
118 mg->heightmap[mapindex] :
119 mg->findGroundLevel(v2s16(x, z), nmin.Y, nmax.Y);
121 if (y < nmin.Y || y > nmax.Y)
124 int height = getHeight();
125 int max_y = nmax.Y;// + MAP_BLOCKSIZE - 1;
126 if (y + 1 + height > max_y) {
129 printf("Decoration at (%d %d %d) cut off\n", x, y, z);
131 JMutexAutoLock cutofflock(cutoff_mutex);
132 cutoffs.push_back(CutoffData(x, y, z, height));
137 std::set<u8>::iterator iter;
140 iter = biomes.find(mg->biomemap[mapindex]);
141 if (iter == biomes.end())
147 if (generate(mg, &ps, max_y, pos))
148 mg->gennotify.addEvent(GENNOTIFY_DECORATION, pos, id);
157 void Decoration::placeCutoffs(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
159 PseudoRandom pr(blockseed + 53);
160 std::vector<CutoffData> handled_cutoffs;
162 // Copy over the cutoffs we're interested in so we don't needlessly hold a lock
164 JMutexAutoLock cutofflock(cutoff_mutex);
165 for (std::list<CutoffData>::iterator i = cutoffs.begin();
166 i != cutoffs.end(); ++i) {
167 CutoffData cutoff = *i;
169 s16 height = cutoff.height;
170 if (p.X < nmin.X || p.X > nmax.X ||
171 p.Z < nmin.Z || p.Z > nmax.Z)
173 if (p.Y + height < nmin.Y || p.Y > nmax.Y)
176 handled_cutoffs.push_back(cutoff);
180 // Generate the cutoffs
181 for (size_t i = 0; i != handled_cutoffs.size(); i++) {
182 v3s16 p = handled_cutoffs[i].p;
183 s16 height = handled_cutoffs[i].height;
185 if (p.Y + height > nmax.Y) {
186 //printf("Decoration at (%d %d %d) cut off again!\n", p.X, p.Y, p.Z);
187 cuttoffs.push_back(v3s16(p.X, p.Y, p.Z));
190 generate(mg, &pr, nmax.Y, nmin.Y - p.Y, v3s16(p.X, nmin.Y, p.Z));
193 // Remove cutoffs that were handled from the cutoff list
195 JMutexAutoLock cutofflock(cutoff_mutex);
196 for (std::list<CutoffData>::iterator i = cutoffs.begin();
197 i != cutoffs.end(); ++i) {
199 for (size_t j = 0; j != handled_cutoffs.size(); j++) {
200 CutoffData coff = *i;
201 if (coff.p == handled_cutoffs[j].p)
202 i = cutoffs.erase(i);
210 ///////////////////////////////////////////////////////////////////////////////
213 bool DecoSimple::canPlaceDecoration(ManualMapVoxelManipulator *vm, v3s16 p)
215 // Don't bother if there aren't any decorations to place
216 if (c_decos.size() == 0)
219 u32 vi = vm->m_area.index(p);
221 // Check if the decoration can be placed on this node
222 if (!CONTAINS(c_place_on, vm->m_data[vi].getContent()))
225 // Don't continue if there are no spawnby constraints
241 // Check a Moore neighborhood if there are enough spawnby nodes
242 for (size_t i = 0; i != ARRLEN(dirs); i++) {
243 u32 index = vm->m_area.index(p + dirs[i]);
244 if (!vm->m_area.contains(index))
247 if (CONTAINS(c_spawnby, vm->m_data[index].getContent()))
251 if (nneighs < nspawnby)
258 size_t DecoSimple::generate(Mapgen *mg, PseudoRandom *pr, s16 max_y, v3s16 p)
260 ManualMapVoxelManipulator *vm = mg->vm;
262 if (!canPlaceDecoration(vm, p))
265 content_t c_place = c_decos[pr->range(0, c_decos.size() - 1)];
267 s16 height = (deco_height_max > 0) ?
268 pr->range(deco_height, deco_height_max) : deco_height;
270 height = MYMIN(height, max_y - p.Y);
272 v3s16 em = vm->m_area.getExtent();
273 u32 vi = vm->m_area.index(p);
274 for (int i = 0; i < height; i++) {
275 vm->m_area.add_y(em, vi, 1);
277 content_t c = vm->m_data[vi].getContent();
278 if (c != CONTENT_AIR && c != CONTENT_IGNORE)
281 vm->m_data[vi] = MapNode(c_place);
288 int DecoSimple::getHeight()
290 return (deco_height_max > 0) ? deco_height_max : deco_height;
294 ///////////////////////////////////////////////////////////////////////////////
297 size_t DecoSchematic::generate(Mapgen *mg, PseudoRandom *pr, s16 max_y, v3s16 p)
299 ManualMapVoxelManipulator *vm = mg->vm;
301 if (flags & DECO_PLACE_CENTER_X)
302 p.X -= (schematic->size.X + 1) / 2;
303 if (flags & DECO_PLACE_CENTER_Y)
304 p.Y -= (schematic->size.Y + 1) / 2;
305 if (flags & DECO_PLACE_CENTER_Z)
306 p.Z -= (schematic->size.Z + 1) / 2;
308 u32 vi = vm->m_area.index(p);
309 content_t c = vm->m_data[vi].getContent();
310 if (!CONTAINS(c_place_on, c))
313 Rotation rot = (rotation == ROTATE_RAND) ?
314 (Rotation)pr->range(ROTATE_0, ROTATE_270) : rotation;
316 schematic->blitToVManip(p, vm, rot, false, mg->ndef);
322 int DecoSchematic::getHeight()
324 return schematic->size.Y;