3 Copyright (C) 2014-2016 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
4 Copyright (C) 2015-2017 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"
31 FlagDesc flagdesc_deco[] = {
32 {"place_center_x", DECO_PLACE_CENTER_X},
33 {"place_center_y", DECO_PLACE_CENTER_Y},
34 {"place_center_z", DECO_PLACE_CENTER_Z},
35 {"force_placement", DECO_FORCE_PLACEMENT},
36 {"liquid_surface", DECO_LIQUID_SURFACE},
37 {"all_floors", DECO_ALL_FLOORS},
38 {"all_ceilings", DECO_ALL_CEILINGS},
43 ///////////////////////////////////////////////////////////////////////////////
46 DecorationManager::DecorationManager(IGameDef *gamedef) :
47 ObjDefManager(gamedef, OBJDEF_DECORATION)
52 size_t DecorationManager::placeAllDecos(Mapgen *mg, u32 blockseed,
53 v3s16 nmin, v3s16 nmax)
57 for (size_t i = 0; i != m_objects.size(); i++) {
58 Decoration *deco = (Decoration *)m_objects[i];
62 nplaced += deco->placeDeco(mg, blockseed, nmin, nmax);
70 ///////////////////////////////////////////////////////////////////////////////
73 void Decoration::resolveNodeNames()
75 getIdsFromNrBacklog(&c_place_on);
76 getIdsFromNrBacklog(&c_spawnby);
80 bool Decoration::canPlaceDecoration(MMVManip *vm, v3s16 p)
82 // Check if the decoration can be placed on this node
83 u32 vi = vm->m_area.index(p);
84 if (!CONTAINS(c_place_on, vm->m_data[vi].getContent()))
87 // Don't continue if there are no spawnby constraints
92 static const v3s16 dirs[16] = {
112 // Check these 16 neighbouring nodes for enough spawnby nodes
113 for (size_t i = 0; i != ARRLEN(dirs); i++) {
114 u32 index = vm->m_area.index(p + dirs[i]);
115 if (!vm->m_area.contains(index))
118 if (CONTAINS(c_spawnby, vm->m_data[index].getContent()))
122 if (nneighs < nspawnby)
129 size_t Decoration::placeDeco(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
131 PcgRandom ps(blockseed + 53);
132 int carea_size = nmax.X - nmin.X + 1;
134 // Divide area into parts
135 // If chunksize is changed it may no longer be divisable by sidelen
136 if (carea_size % sidelen)
137 sidelen = carea_size;
139 s16 divlen = carea_size / sidelen;
140 int area = sidelen * sidelen;
142 for (s16 z0 = 0; z0 < divlen; z0++)
143 for (s16 x0 = 0; x0 < divlen; x0++) {
144 v2s16 p2d_center( // Center position of part of division
145 nmin.X + sidelen / 2 + sidelen * x0,
146 nmin.Z + sidelen / 2 + sidelen * z0
148 v2s16 p2d_min( // Minimum edge of part of division
149 nmin.X + sidelen * x0,
150 nmin.Z + sidelen * z0
152 v2s16 p2d_max( // Maximum edge of part of division
153 nmin.X + sidelen + sidelen * x0 - 1,
154 nmin.Z + sidelen + sidelen * z0 - 1
157 // Amount of decorations
158 float nval = (flags & DECO_USE_NOISE) ?
159 NoisePerlin2D(&np, p2d_center.X, p2d_center.Y, mapseed) :
162 float deco_count_f = (float)area * nval;
163 if (deco_count_f >= 1.f) {
164 deco_count = deco_count_f;
165 } else if (deco_count_f > 0.f) {
166 // For low density decorations calculate a chance for 1 decoration
167 if (ps.range(1000) <= deco_count_f * 1000.f)
171 for (u32 i = 0; i < deco_count; i++) {
172 s16 x = ps.range(p2d_min.X, p2d_max.X);
173 s16 z = ps.range(p2d_min.Y, p2d_max.Y);
174 int mapindex = carea_size * (z - nmin.Z) + (x - nmin.X);
176 if ((flags & DECO_ALL_FLOORS) ||
177 (flags & DECO_ALL_CEILINGS)) {
178 // All-surfaces decorations
179 // Check biome of column
180 if (mg->biomemap && !biomes.empty()) {
181 std::unordered_set<u8>::const_iterator iter =
182 biomes.find(mg->biomemap[mapindex]);
183 if (iter == biomes.end())
187 // Get all floors and ceilings in node column
188 u16 size = (nmax.Y - nmin.Y + 1) / 2;
192 u16 num_ceilings = 0;
194 mg->getSurfaces(v2s16(x, z), nmin.Y, nmax.Y,
195 floors, ceilings, &num_floors, &num_ceilings);
197 if ((flags & DECO_ALL_FLOORS) && num_floors > 0) {
199 for (u16 fi = 0; fi < num_floors; fi++) {
201 if (y < y_min || y > y_max)
205 if (generate(mg->vm, &ps, pos, false))
206 mg->gennotify.addEvent(
207 GENNOTIFY_DECORATION, pos, index);
211 if ((flags & DECO_ALL_CEILINGS) && num_ceilings > 0) {
212 // Ceiling decorations
213 for (u16 ci = 0; ci < num_ceilings; ci++) {
214 s16 y = ceilings[ci];
215 if (y < y_min || y > y_max)
219 if (generate(mg->vm, &ps, pos, true))
220 mg->gennotify.addEvent(
221 GENNOTIFY_DECORATION, pos, index);
224 } else { // Heightmap decorations
225 s16 y = -MAX_MAP_GENERATION_LIMIT;
226 if (flags & DECO_LIQUID_SURFACE)
227 y = mg->findLiquidSurface(v2s16(x, z), nmin.Y, nmax.Y);
228 else if (mg->heightmap)
229 y = mg->heightmap[mapindex];
231 y = mg->findGroundLevel(v2s16(x, z), nmin.Y, nmax.Y);
233 if (y < y_min || y > y_max || y < nmin.Y || y > nmax.Y)
236 if (mg->biomemap && !biomes.empty()) {
237 std::unordered_set<u8>::const_iterator iter =
238 biomes.find(mg->biomemap[mapindex]);
239 if (iter == biomes.end())
244 if (generate(mg->vm, &ps, pos, false))
245 mg->gennotify.addEvent(GENNOTIFY_DECORATION, pos, index);
254 ///////////////////////////////////////////////////////////////////////////////
257 void DecoSimple::resolveNodeNames()
259 Decoration::resolveNodeNames();
260 getIdsFromNrBacklog(&c_decos);
264 size_t DecoSimple::generate(MMVManip *vm, PcgRandom *pr, v3s16 p, bool ceiling)
266 // Don't bother if there aren't any decorations to place
270 if (!canPlaceDecoration(vm, p))
273 // Check for placement outside the voxelmanip volume
275 // Ceiling decorations
276 // 'place offset y' is inverted
277 if (p.Y - place_offset_y - std::max(deco_height, deco_height_max) <
278 vm->m_area.MinEdge.Y)
281 if (p.Y - 1 - place_offset_y > vm->m_area.MaxEdge.Y)
284 } else { // Heightmap and floor decorations
285 if (p.Y + place_offset_y + std::max(deco_height, deco_height_max) >
286 vm->m_area.MaxEdge.Y)
289 if (p.Y + 1 + place_offset_y < vm->m_area.MinEdge.Y)
293 content_t c_place = c_decos[pr->range(0, c_decos.size() - 1)];
294 s16 height = (deco_height_max > 0) ?
295 pr->range(deco_height, deco_height_max) : deco_height;
296 u8 param2 = (deco_param2_max > 0) ?
297 pr->range(deco_param2, deco_param2_max) : deco_param2;
298 bool force_placement = (flags & DECO_FORCE_PLACEMENT);
300 const v3s16 &em = vm->m_area.getExtent();
301 u32 vi = vm->m_area.index(p);
304 // Ceiling decorations
305 // 'place offset y' is inverted
306 vm->m_area.add_y(em, vi, -place_offset_y);
308 for (int i = 0; i < height; i++) {
309 vm->m_area.add_y(em, vi, -1);
310 content_t c = vm->m_data[vi].getContent();
311 if (c != CONTENT_AIR && c != CONTENT_IGNORE && !force_placement)
314 vm->m_data[vi] = MapNode(c_place, 0, param2);
316 } else { // Heightmap and floor decorations
317 vm->m_area.add_y(em, vi, place_offset_y);
319 for (int i = 0; i < height; i++) {
320 vm->m_area.add_y(em, vi, 1);
321 content_t c = vm->m_data[vi].getContent();
322 if (c != CONTENT_AIR && c != CONTENT_IGNORE && !force_placement)
325 vm->m_data[vi] = MapNode(c_place, 0, param2);
333 ///////////////////////////////////////////////////////////////////////////////
336 size_t DecoSchematic::generate(MMVManip *vm, PcgRandom *pr, v3s16 p, bool ceiling)
338 // Schematic could have been unloaded but not the decoration
339 // In this case generate() does nothing (but doesn't *fail*)
340 if (schematic == NULL)
343 if (!canPlaceDecoration(vm, p))
346 if (flags & DECO_PLACE_CENTER_Y) {
347 p.Y -= (schematic->size.Y - 1) / 2;
349 // Only apply 'place offset y' if not 'deco place center y'
351 // Shift down so that schematic top layer is level with ceiling
352 // 'place offset y' is inverted
353 p.Y -= (place_offset_y + schematic->size.Y - 1);
355 p.Y += place_offset_y;
358 // Check schematic top and base are in voxelmanip
359 if (p.Y + schematic->size.Y - 1 > vm->m_area.MaxEdge.Y)
362 if (p.Y < vm->m_area.MinEdge.Y)
365 if (flags & DECO_PLACE_CENTER_X)
366 p.X -= (schematic->size.X - 1) / 2;
367 if (flags & DECO_PLACE_CENTER_Z)
368 p.Z -= (schematic->size.Z - 1) / 2;
370 Rotation rot = (rotation == ROTATE_RAND) ?
371 (Rotation)pr->range(ROTATE_0, ROTATE_270) : rotation;
372 bool force_placement = (flags & DECO_FORCE_PLACEMENT);
374 schematic->blitToVManip(vm, p, rot, force_placement);