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);
71 ///////////////////////////////////////////////////////////////////////////////
74 void Decoration::resolveNodeNames()
76 getIdsFromNrBacklog(&c_place_on);
77 getIdsFromNrBacklog(&c_spawnby);
81 bool Decoration::canPlaceDecoration(MMVManip *vm, v3s16 p)
83 // Check if the decoration can be placed on this node
84 u32 vi = vm->m_area.index(p);
85 if (!CONTAINS(c_place_on, vm->m_data[vi].getContent()))
88 // Don't continue if there are no spawnby constraints
93 static const v3s16 dirs[16] = {
113 // Check these 16 neighbouring nodes for enough spawnby nodes
114 for (size_t i = 0; i != ARRLEN(dirs); i++) {
115 u32 index = vm->m_area.index(p + dirs[i]);
116 if (!vm->m_area.contains(index))
119 if (CONTAINS(c_spawnby, vm->m_data[index].getContent()))
123 if (nneighs < nspawnby)
130 size_t Decoration::placeDeco(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
132 PcgRandom ps(blockseed + 53);
133 int carea_size = nmax.X - nmin.X + 1;
135 // Divide area into parts
136 // If chunksize is changed it may no longer be divisable by sidelen
137 if (carea_size % sidelen)
138 sidelen = carea_size;
140 s16 divlen = carea_size / sidelen;
141 int area = sidelen * sidelen;
143 for (s16 z0 = 0; z0 < divlen; z0++)
144 for (s16 x0 = 0; x0 < divlen; x0++) {
145 v2s16 p2d_center( // Center position of part of division
146 nmin.X + sidelen / 2 + sidelen * x0,
147 nmin.Z + sidelen / 2 + sidelen * z0
149 v2s16 p2d_min( // Minimum edge of part of division
150 nmin.X + sidelen * x0,
151 nmin.Z + sidelen * z0
153 v2s16 p2d_max( // Maximum edge of part of division
154 nmin.X + sidelen + sidelen * x0 - 1,
155 nmin.Z + sidelen + sidelen * z0 - 1
159 // Amount of decorations
160 float nval = (flags & DECO_USE_NOISE) ?
161 NoisePerlin2D(&np, p2d_center.X, p2d_center.Y, mapseed) :
166 // Complete coverage. Disable random placement to avoid
167 // redundant multiple placements at one position.
171 float deco_count_f = (float)area * nval;
172 if (deco_count_f >= 1.0f) {
173 deco_count = deco_count_f;
174 } else if (deco_count_f > 0.0f) {
175 // For very low density calculate a chance for 1 decoration
176 if (ps.range(1000) <= deco_count_f * 1000.0f)
181 s16 x = p2d_min.X - 1;
184 for (u32 i = 0; i < deco_count; i++) {
186 x = ps.range(p2d_min.X, p2d_max.X);
187 z = ps.range(p2d_min.Y, p2d_max.Y);
190 if (x == p2d_max.X + 1) {
195 int mapindex = carea_size * (z - nmin.Z) + (x - nmin.X);
197 if ((flags & DECO_ALL_FLOORS) ||
198 (flags & DECO_ALL_CEILINGS)) {
199 // All-surfaces decorations
200 // Check biome of column
201 if (mg->biomemap && !biomes.empty()) {
202 std::unordered_set<u8>::const_iterator iter =
203 biomes.find(mg->biomemap[mapindex]);
204 if (iter == biomes.end())
208 // Get all floors and ceilings in node column
209 u16 size = (nmax.Y - nmin.Y + 1) / 2;
210 std::vector<s16> floors;
211 std::vector<s16> ceilings;
212 floors.reserve(size);
213 ceilings.reserve(size);
215 mg->getSurfaces(v2s16(x, z), nmin.Y, nmax.Y, floors, ceilings);
217 if (flags & DECO_ALL_FLOORS) {
219 for (const s16 y : floors) {
220 if (y < y_min || y > y_max)
224 if (generate(mg->vm, &ps, pos, false))
225 mg->gennotify.addEvent(
226 GENNOTIFY_DECORATION, pos, index);
230 if (flags & DECO_ALL_CEILINGS) {
231 // Ceiling decorations
232 for (const s16 y : ceilings) {
233 if (y < y_min || y > y_max)
237 if (generate(mg->vm, &ps, pos, true))
238 mg->gennotify.addEvent(
239 GENNOTIFY_DECORATION, pos, index);
242 } else { // Heightmap decorations
243 s16 y = -MAX_MAP_GENERATION_LIMIT;
244 if (flags & DECO_LIQUID_SURFACE)
245 y = mg->findLiquidSurface(v2s16(x, z), nmin.Y, nmax.Y);
246 else if (mg->heightmap)
247 y = mg->heightmap[mapindex];
249 y = mg->findGroundLevel(v2s16(x, z), nmin.Y, nmax.Y);
251 if (y < y_min || y > y_max || y < nmin.Y || y > nmax.Y)
254 if (mg->biomemap && !biomes.empty()) {
255 std::unordered_set<u8>::const_iterator iter =
256 biomes.find(mg->biomemap[mapindex]);
257 if (iter == biomes.end())
262 if (generate(mg->vm, &ps, pos, false))
263 mg->gennotify.addEvent(GENNOTIFY_DECORATION, pos, index);
272 ///////////////////////////////////////////////////////////////////////////////
275 void DecoSimple::resolveNodeNames()
277 Decoration::resolveNodeNames();
278 getIdsFromNrBacklog(&c_decos);
282 size_t DecoSimple::generate(MMVManip *vm, PcgRandom *pr, v3s16 p, bool ceiling)
284 // Don't bother if there aren't any decorations to place
288 if (!canPlaceDecoration(vm, p))
291 // Check for placement outside the voxelmanip volume
293 // Ceiling decorations
294 // 'place offset y' is inverted
295 if (p.Y - place_offset_y - std::max(deco_height, deco_height_max) <
296 vm->m_area.MinEdge.Y)
299 if (p.Y - 1 - place_offset_y > vm->m_area.MaxEdge.Y)
302 } else { // Heightmap and floor decorations
303 if (p.Y + place_offset_y + std::max(deco_height, deco_height_max) >
304 vm->m_area.MaxEdge.Y)
307 if (p.Y + 1 + place_offset_y < vm->m_area.MinEdge.Y)
311 content_t c_place = c_decos[pr->range(0, c_decos.size() - 1)];
312 s16 height = (deco_height_max > 0) ?
313 pr->range(deco_height, deco_height_max) : deco_height;
314 u8 param2 = (deco_param2_max > 0) ?
315 pr->range(deco_param2, deco_param2_max) : deco_param2;
316 bool force_placement = (flags & DECO_FORCE_PLACEMENT);
318 const v3s16 &em = vm->m_area.getExtent();
319 u32 vi = vm->m_area.index(p);
322 // Ceiling decorations
323 // 'place offset y' is inverted
324 VoxelArea::add_y(em, vi, -place_offset_y);
326 for (int i = 0; i < height; i++) {
327 VoxelArea::add_y(em, vi, -1);
328 content_t c = vm->m_data[vi].getContent();
329 if (c != CONTENT_AIR && c != CONTENT_IGNORE && !force_placement)
332 vm->m_data[vi] = MapNode(c_place, 0, param2);
334 } else { // Heightmap and floor decorations
335 VoxelArea::add_y(em, vi, place_offset_y);
337 for (int i = 0; i < height; i++) {
338 VoxelArea::add_y(em, vi, 1);
339 content_t c = vm->m_data[vi].getContent();
340 if (c != CONTENT_AIR && c != CONTENT_IGNORE && !force_placement)
343 vm->m_data[vi] = MapNode(c_place, 0, param2);
351 ///////////////////////////////////////////////////////////////////////////////
354 size_t DecoSchematic::generate(MMVManip *vm, PcgRandom *pr, v3s16 p, bool ceiling)
356 // Schematic could have been unloaded but not the decoration
357 // In this case generate() does nothing (but doesn't *fail*)
358 if (schematic == NULL)
361 if (!canPlaceDecoration(vm, p))
364 if (flags & DECO_PLACE_CENTER_Y) {
365 p.Y -= (schematic->size.Y - 1) / 2;
367 // Only apply 'place offset y' if not 'deco place center y'
369 // Shift down so that schematic top layer is level with ceiling
370 // 'place offset y' is inverted
371 p.Y -= (place_offset_y + schematic->size.Y - 1);
373 p.Y += place_offset_y;
376 // Check schematic top and base are in voxelmanip
377 if (p.Y + schematic->size.Y - 1 > vm->m_area.MaxEdge.Y)
380 if (p.Y < vm->m_area.MinEdge.Y)
383 Rotation rot = (rotation == ROTATE_RAND) ?
384 (Rotation)pr->range(ROTATE_0, ROTATE_270) : rotation;
386 if (flags & DECO_PLACE_CENTER_X) {
387 if (rot == ROTATE_0 || rot == ROTATE_180)
388 p.X -= (schematic->size.X - 1) / 2;
390 p.Z -= (schematic->size.X - 1) / 2;
392 if (flags & DECO_PLACE_CENTER_Z) {
393 if (rot == ROTATE_0 || rot == ROTATE_180)
394 p.Z -= (schematic->size.Z - 1) / 2;
396 p.X -= (schematic->size.Z - 1) / 2;
399 bool force_placement = (flags & DECO_FORCE_PLACEMENT);
401 schematic->blitToVManip(vm, p, rot, force_placement);