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 FlagDesc flagdesc_deco[] = {
29 {"place_center_x", DECO_PLACE_CENTER_X},
30 {"place_center_y", DECO_PLACE_CENTER_Y},
31 {"place_center_z", DECO_PLACE_CENTER_Z},
32 {"force_placement", DECO_FORCE_PLACEMENT},
33 {"liquid_surface", DECO_LIQUID_SURFACE},
38 ///////////////////////////////////////////////////////////////////////////////
41 DecorationManager::DecorationManager(IGameDef *gamedef) :
42 ObjDefManager(gamedef, OBJDEF_DECORATION)
47 size_t DecorationManager::placeAllDecos(Mapgen *mg, u32 blockseed,
48 v3s16 nmin, v3s16 nmax)
52 for (size_t i = 0; i != m_objects.size(); i++) {
53 Decoration *deco = (Decoration *)m_objects[i];
57 nplaced += deco->placeDeco(mg, blockseed, nmin, nmax);
65 ///////////////////////////////////////////////////////////////////////////////
68 Decoration::Decoration()
77 Decoration::~Decoration()
82 void Decoration::resolveNodeNames()
84 getIdsFromNrBacklog(&c_place_on);
88 size_t Decoration::placeDeco(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
90 PcgRandom ps(blockseed + 53);
91 int carea_size = nmax.X - nmin.X + 1;
93 // Divide area into parts
94 // If chunksize is changed it may no longer be divisable by sidelen
95 if (carea_size % sidelen)
98 s16 divlen = carea_size / sidelen;
99 int area = sidelen * sidelen;
101 for (s16 z0 = 0; z0 < divlen; z0++)
102 for (s16 x0 = 0; x0 < divlen; x0++) {
103 v2s16 p2d_center( // Center position of part of division
104 nmin.X + sidelen / 2 + sidelen * x0,
105 nmin.Z + sidelen / 2 + sidelen * z0
107 v2s16 p2d_min( // Minimum edge of part of division
108 nmin.X + sidelen * x0,
109 nmin.Z + sidelen * z0
111 v2s16 p2d_max( // Maximum edge of part of division
112 nmin.X + sidelen + sidelen * x0 - 1,
113 nmin.Z + sidelen + sidelen * z0 - 1
116 // Amount of decorations
117 float nval = (flags & DECO_USE_NOISE) ?
118 NoisePerlin2D(&np, p2d_center.X, p2d_center.Y, mapseed) :
121 float deco_count_f = (float)area * nval;
122 if (deco_count_f >= 1.f) {
123 deco_count = deco_count_f;
124 } else if (deco_count_f > 0.f) {
125 // For low density decorations calculate a chance for 1 decoration
126 if (ps.range(1000) <= deco_count_f * 1000.f)
130 for (u32 i = 0; i < deco_count; i++) {
131 s16 x = ps.range(p2d_min.X, p2d_max.X);
132 s16 z = ps.range(p2d_min.Y, p2d_max.Y);
134 int mapindex = carea_size * (z - nmin.Z) + (x - nmin.X);
136 s16 y = -MAX_MAP_GENERATION_LIMIT;
137 if (flags & DECO_LIQUID_SURFACE)
138 y = mg->findLiquidSurface(v2s16(x, z), nmin.Y, nmax.Y);
139 else if (mg->heightmap)
140 y = mg->heightmap[mapindex];
142 y = mg->findGroundLevel(v2s16(x, z), nmin.Y, nmax.Y);
144 if (y < nmin.Y || y > nmax.Y ||
145 y < y_min || y > y_max)
148 if (y + getHeight() >= mg->vm->m_area.MaxEdge.Y) {
151 printf("Decoration at (%d %d %d) cut off\n", x, y, z);
153 MutexAutoLock cutofflock(cutoff_mutex);
154 cutoffs.push_back(CutoffData(x, y, z, height));
159 std::set<u8>::iterator iter;
161 if (!biomes.empty()) {
162 iter = biomes.find(mg->biomemap[mapindex]);
163 if (iter == biomes.end())
169 if (generate(mg->vm, &ps, pos))
170 mg->gennotify.addEvent(GENNOTIFY_DECORATION, pos, index);
179 void Decoration::placeCutoffs(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
181 PcgRandom pr(blockseed + 53);
182 std::vector<CutoffData> handled_cutoffs;
184 // Copy over the cutoffs we're interested in so we don't needlessly hold a lock
186 MutexAutoLock cutofflock(cutoff_mutex);
187 for (std::list<CutoffData>::iterator i = cutoffs.begin();
188 i != cutoffs.end(); ++i) {
189 CutoffData cutoff = *i;
191 s16 height = cutoff.height;
192 if (p.X < nmin.X || p.X > nmax.X ||
193 p.Z < nmin.Z || p.Z > nmax.Z)
195 if (p.Y + height < nmin.Y || p.Y > nmax.Y)
198 handled_cutoffs.push_back(cutoff);
202 // Generate the cutoffs
203 for (size_t i = 0; i != handled_cutoffs.size(); i++) {
204 v3s16 p = handled_cutoffs[i].p;
205 s16 height = handled_cutoffs[i].height;
207 if (p.Y + height > nmax.Y) {
208 //printf("Decoration at (%d %d %d) cut off again!\n", p.X, p.Y, p.Z);
209 cuttoffs.push_back(v3s16(p.X, p.Y, p.Z));
212 generate(mg, &pr, nmax.Y, nmin.Y - p.Y, v3s16(p.X, nmin.Y, p.Z));
215 // Remove cutoffs that were handled from the cutoff list
217 MutexAutoLock cutofflock(cutoff_mutex);
218 for (std::list<CutoffData>::iterator i = cutoffs.begin();
219 i != cutoffs.end(); ++i) {
221 for (size_t j = 0; j != handled_cutoffs.size(); j++) {
222 CutoffData coff = *i;
223 if (coff.p == handled_cutoffs[j].p)
224 i = cutoffs.erase(i);
232 ///////////////////////////////////////////////////////////////////////////////
235 void DecoSimple::resolveNodeNames()
237 Decoration::resolveNodeNames();
238 getIdsFromNrBacklog(&c_decos);
239 getIdsFromNrBacklog(&c_spawnby);
243 bool DecoSimple::canPlaceDecoration(MMVManip *vm, v3s16 p)
245 // Don't bother if there aren't any decorations to place
246 if (c_decos.size() == 0)
249 u32 vi = vm->m_area.index(p);
251 // Check if the decoration can be placed on this node
252 if (!CONTAINS(c_place_on, vm->m_data[vi].getContent()))
255 // Don't continue if there are no spawnby constraints
280 // Check a Moore neighborhood if there are enough spawnby nodes
281 for (size_t i = 0; i != ARRLEN(dirs); i++) {
282 u32 index = vm->m_area.index(p + dirs[i]);
283 if (!vm->m_area.contains(index))
286 if (CONTAINS(c_spawnby, vm->m_data[index].getContent()))
290 if (nneighs < nspawnby)
297 size_t DecoSimple::generate(MMVManip *vm, PcgRandom *pr, v3s16 p)
299 if (!canPlaceDecoration(vm, p))
302 content_t c_place = c_decos[pr->range(0, c_decos.size() - 1)];
304 s16 height = (deco_height_max > 0) ?
305 pr->range(deco_height, deco_height_max) : deco_height;
307 bool force_placement = (flags & DECO_FORCE_PLACEMENT);
309 v3s16 em = vm->m_area.getExtent();
310 u32 vi = vm->m_area.index(p);
311 for (int i = 0; i < height; i++) {
312 vm->m_area.add_y(em, vi, 1);
314 content_t c = vm->m_data[vi].getContent();
315 if (c != CONTENT_AIR && c != CONTENT_IGNORE &&
319 vm->m_data[vi] = MapNode(c_place);
326 int DecoSimple::getHeight()
328 return (deco_height_max > 0) ? deco_height_max : deco_height;
332 ///////////////////////////////////////////////////////////////////////////////
335 DecoSchematic::DecoSchematic()
341 size_t DecoSchematic::generate(MMVManip *vm, PcgRandom *pr, v3s16 p)
343 // Schematic could have been unloaded but not the decoration
344 // In this case generate() does nothing (but doesn't *fail*)
345 if (schematic == NULL)
348 u32 vi = vm->m_area.index(p);
349 content_t c = vm->m_data[vi].getContent();
350 if (!CONTAINS(c_place_on, c))
353 if (flags & DECO_PLACE_CENTER_X)
354 p.X -= (schematic->size.X - 1) / 2;
355 if (flags & DECO_PLACE_CENTER_Y)
356 p.Y -= (schematic->size.Y - 1) / 2;
357 if (flags & DECO_PLACE_CENTER_Z)
358 p.Z -= (schematic->size.Z - 1) / 2;
360 Rotation rot = (rotation == ROTATE_RAND) ?
361 (Rotation)pr->range(ROTATE_0, ROTATE_270) : rotation;
363 bool force_placement = (flags & DECO_FORCE_PLACEMENT);
365 schematic->blitToVManip(vm, p, rot, force_placement);
371 int DecoSchematic::getHeight()
373 return schematic->size.Y;