3 Copyright (C) 2014-2018 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
4 Copyright (C) 2015-2018 paramat
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "mg_decoration.h"
22 #include "mg_schematic.h"
27 #include "util/numeric.h"
32 FlagDesc flagdesc_deco[] = {
33 {"place_center_x", DECO_PLACE_CENTER_X},
34 {"place_center_y", DECO_PLACE_CENTER_Y},
35 {"place_center_z", DECO_PLACE_CENTER_Z},
36 {"force_placement", DECO_FORCE_PLACEMENT},
37 {"liquid_surface", DECO_LIQUID_SURFACE},
38 {"all_floors", DECO_ALL_FLOORS},
39 {"all_ceilings", DECO_ALL_CEILINGS},
44 ///////////////////////////////////////////////////////////////////////////////
47 DecorationManager::DecorationManager(IGameDef *gamedef) :
48 ObjDefManager(gamedef, OBJDEF_DECORATION)
53 size_t DecorationManager::placeAllDecos(Mapgen *mg, u32 blockseed,
54 v3s16 nmin, v3s16 nmax)
58 for (size_t i = 0; i != m_objects.size(); i++) {
59 Decoration *deco = (Decoration *)m_objects[i];
63 nplaced += deco->placeDeco(mg, blockseed, nmin, nmax);
70 DecorationManager *DecorationManager::clone() const
72 auto mgr = new DecorationManager();
73 ObjDefManager::cloneTo(mgr);
78 ///////////////////////////////////////////////////////////////////////////////
81 void Decoration::resolveNodeNames()
83 getIdsFromNrBacklog(&c_place_on);
84 getIdsFromNrBacklog(&c_spawnby);
88 bool Decoration::canPlaceDecoration(MMVManip *vm, v3s16 p)
90 // Check if the decoration can be placed on this node
91 u32 vi = vm->m_area.index(p);
92 if (!CONTAINS(c_place_on, vm->m_data[vi].getContent()))
95 // Don't continue if there are no spawnby constraints
100 static const v3s16 dirs[16] = {
120 // Check these 16 neighbouring nodes for enough spawnby nodes
121 for (size_t i = 0; i != ARRLEN(dirs); i++) {
122 u32 index = vm->m_area.index(p + dirs[i]);
123 if (!vm->m_area.contains(index))
126 if (CONTAINS(c_spawnby, vm->m_data[index].getContent()))
130 if (nneighs < nspawnby)
137 size_t Decoration::placeDeco(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
139 PcgRandom ps(blockseed + 53);
140 int carea_size = nmax.X - nmin.X + 1;
142 // Divide area into parts
143 // If chunksize is changed it may no longer be divisable by sidelen
144 if (carea_size % sidelen)
145 sidelen = carea_size;
147 s16 divlen = carea_size / sidelen;
148 int area = sidelen * sidelen;
150 for (s16 z0 = 0; z0 < divlen; z0++)
151 for (s16 x0 = 0; x0 < divlen; x0++) {
152 v2s16 p2d_center( // Center position of part of division
153 nmin.X + sidelen / 2 + sidelen * x0,
154 nmin.Z + sidelen / 2 + sidelen * z0
156 v2s16 p2d_min( // Minimum edge of part of division
157 nmin.X + sidelen * x0,
158 nmin.Z + sidelen * z0
160 v2s16 p2d_max( // Maximum edge of part of division
161 nmin.X + sidelen + sidelen * x0 - 1,
162 nmin.Z + sidelen + sidelen * z0 - 1
166 // Amount of decorations
167 float nval = (flags & DECO_USE_NOISE) ?
168 NoisePerlin2D(&np, p2d_center.X, p2d_center.Y, mapseed) :
173 // Complete coverage. Disable random placement to avoid
174 // redundant multiple placements at one position.
178 float deco_count_f = (float)area * nval;
179 if (deco_count_f >= 1.0f) {
180 deco_count = deco_count_f;
181 } else if (deco_count_f > 0.0f) {
182 // For very low density calculate a chance for 1 decoration
183 if (ps.range(1000) <= deco_count_f * 1000.0f)
188 s16 x = p2d_min.X - 1;
191 for (u32 i = 0; i < deco_count; i++) {
193 x = ps.range(p2d_min.X, p2d_max.X);
194 z = ps.range(p2d_min.Y, p2d_max.Y);
197 if (x == p2d_max.X + 1) {
202 int mapindex = carea_size * (z - nmin.Z) + (x - nmin.X);
204 if ((flags & DECO_ALL_FLOORS) ||
205 (flags & DECO_ALL_CEILINGS)) {
206 // All-surfaces decorations
207 // Check biome of column
208 if (mg->biomemap && !biomes.empty()) {
209 std::unordered_set<u8>::const_iterator iter =
210 biomes.find(mg->biomemap[mapindex]);
211 if (iter == biomes.end())
215 // Get all floors and ceilings in node column
216 u16 size = (nmax.Y - nmin.Y + 1) / 2;
217 std::vector<s16> floors;
218 std::vector<s16> ceilings;
219 floors.reserve(size);
220 ceilings.reserve(size);
222 mg->getSurfaces(v2s16(x, z), nmin.Y, nmax.Y, floors, ceilings);
224 if (flags & DECO_ALL_FLOORS) {
226 for (const s16 y : floors) {
227 if (y < y_min || y > y_max)
231 if (generate(mg->vm, &ps, pos, false))
232 mg->gennotify.addEvent(
233 GENNOTIFY_DECORATION, pos, index);
237 if (flags & DECO_ALL_CEILINGS) {
238 // Ceiling decorations
239 for (const s16 y : ceilings) {
240 if (y < y_min || y > y_max)
244 if (generate(mg->vm, &ps, pos, true))
245 mg->gennotify.addEvent(
246 GENNOTIFY_DECORATION, pos, index);
249 } else { // Heightmap decorations
250 s16 y = -MAX_MAP_GENERATION_LIMIT;
251 if (flags & DECO_LIQUID_SURFACE)
252 y = mg->findLiquidSurface(v2s16(x, z), nmin.Y, nmax.Y);
253 else if (mg->heightmap)
254 y = mg->heightmap[mapindex];
256 y = mg->findGroundLevel(v2s16(x, z), nmin.Y, nmax.Y);
258 if (y < y_min || y > y_max || y < nmin.Y || y > nmax.Y)
261 if (mg->biomemap && !biomes.empty()) {
262 std::unordered_set<u8>::const_iterator iter =
263 biomes.find(mg->biomemap[mapindex]);
264 if (iter == biomes.end())
269 if (generate(mg->vm, &ps, pos, false))
270 mg->gennotify.addEvent(GENNOTIFY_DECORATION, pos, index);
279 void Decoration::cloneTo(Decoration *def) const
281 ObjDef::cloneTo(def);
283 def->mapseed = mapseed;
284 def->c_place_on = c_place_on;
285 def->sidelen = sidelen;
288 def->fill_ratio = fill_ratio;
290 def->c_spawnby = c_spawnby;
291 def->nspawnby = nspawnby;
292 def->place_offset_y = place_offset_y;
293 def->biomes = biomes;
297 ///////////////////////////////////////////////////////////////////////////////
300 ObjDef *DecoSimple::clone() const
302 auto def = new DecoSimple();
303 Decoration::cloneTo(def);
305 def->c_decos = c_decos;
306 def->deco_height = deco_height;
307 def->deco_height_max = deco_height_max;
308 def->deco_param2 = deco_param2;
309 def->deco_param2_max = deco_param2_max;
315 void DecoSimple::resolveNodeNames()
317 Decoration::resolveNodeNames();
318 getIdsFromNrBacklog(&c_decos);
322 size_t DecoSimple::generate(MMVManip *vm, PcgRandom *pr, v3s16 p, bool ceiling)
324 // Don't bother if there aren't any decorations to place
328 if (!canPlaceDecoration(vm, p))
331 // Check for placement outside the voxelmanip volume
333 // Ceiling decorations
334 // 'place offset y' is inverted
335 if (p.Y - place_offset_y - std::max(deco_height, deco_height_max) <
336 vm->m_area.MinEdge.Y)
339 if (p.Y - 1 - place_offset_y > vm->m_area.MaxEdge.Y)
342 } else { // Heightmap and floor decorations
343 if (p.Y + place_offset_y + std::max(deco_height, deco_height_max) >
344 vm->m_area.MaxEdge.Y)
347 if (p.Y + 1 + place_offset_y < vm->m_area.MinEdge.Y)
351 content_t c_place = c_decos[pr->range(0, c_decos.size() - 1)];
352 s16 height = (deco_height_max > 0) ?
353 pr->range(deco_height, deco_height_max) : deco_height;
354 u8 param2 = (deco_param2_max > 0) ?
355 pr->range(deco_param2, deco_param2_max) : deco_param2;
356 bool force_placement = (flags & DECO_FORCE_PLACEMENT);
358 const v3s16 &em = vm->m_area.getExtent();
359 u32 vi = vm->m_area.index(p);
362 // Ceiling decorations
363 // 'place offset y' is inverted
364 VoxelArea::add_y(em, vi, -place_offset_y);
366 for (int i = 0; i < height; i++) {
367 VoxelArea::add_y(em, vi, -1);
368 content_t c = vm->m_data[vi].getContent();
369 if (c != CONTENT_AIR && c != CONTENT_IGNORE && !force_placement)
372 vm->m_data[vi] = MapNode(c_place, 0, param2);
374 } else { // Heightmap and floor decorations
375 VoxelArea::add_y(em, vi, place_offset_y);
377 for (int i = 0; i < height; i++) {
378 VoxelArea::add_y(em, vi, 1);
379 content_t c = vm->m_data[vi].getContent();
380 if (c != CONTENT_AIR && c != CONTENT_IGNORE && !force_placement)
383 vm->m_data[vi] = MapNode(c_place, 0, param2);
391 ///////////////////////////////////////////////////////////////////////////////
394 DecoSchematic::~DecoSchematic()
401 ObjDef *DecoSchematic::clone() const
403 auto def = new DecoSchematic();
404 Decoration::cloneTo(def);
405 NodeResolver::cloneTo(def);
407 def->rotation = rotation;
408 /* FIXME: We do not own this schematic, yet we only have a pointer to it
409 * and not a handle. We are left with no option but to clone it ourselves.
410 * This is a waste of memory and should be replaced with an alternative
411 * approach sometime. */
412 def->schematic = dynamic_cast<Schematic*>(schematic->clone());
413 def->was_cloned = true;
419 size_t DecoSchematic::generate(MMVManip *vm, PcgRandom *pr, v3s16 p, bool ceiling)
421 // Schematic could have been unloaded but not the decoration
422 // In this case generate() does nothing (but doesn't *fail*)
423 if (schematic == NULL)
426 if (!canPlaceDecoration(vm, p))
429 if (flags & DECO_PLACE_CENTER_Y) {
430 p.Y -= (schematic->size.Y - 1) / 2;
432 // Only apply 'place offset y' if not 'deco place center y'
434 // Shift down so that schematic top layer is level with ceiling
435 // 'place offset y' is inverted
436 p.Y -= (place_offset_y + schematic->size.Y - 1);
438 p.Y += place_offset_y;
441 // Check schematic top and base are in voxelmanip
442 if (p.Y + schematic->size.Y - 1 > vm->m_area.MaxEdge.Y)
445 if (p.Y < vm->m_area.MinEdge.Y)
448 Rotation rot = (rotation == ROTATE_RAND) ?
449 (Rotation)pr->range(ROTATE_0, ROTATE_270) : rotation;
451 if (flags & DECO_PLACE_CENTER_X) {
452 if (rot == ROTATE_0 || rot == ROTATE_180)
453 p.X -= (schematic->size.X - 1) / 2;
455 p.Z -= (schematic->size.X - 1) / 2;
457 if (flags & DECO_PLACE_CENTER_Z) {
458 if (rot == ROTATE_0 || rot == ROTATE_180)
459 p.Z -= (schematic->size.Z - 1) / 2;
461 p.X -= (schematic->size.Z - 1) / 2;
464 bool force_placement = (flags & DECO_FORCE_PLACEMENT);
466 schematic->blitToVManip(vm, p, rot, force_placement);