]> git.lizzy.rs Git - minetest.git/blob - src/mapgen/mg_decoration.cpp
Randomwalk cave liquids: Remove deprecated 'lava depth' parameter (#9105)
[minetest.git] / src / mapgen / mg_decoration.cpp
1 /*
2 Minetest
3 Copyright (C) 2014-2018 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
4 Copyright (C) 2015-2018 paramat
5
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.
10
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.
15
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.
19 */
20
21 #include "mg_decoration.h"
22 #include "mg_schematic.h"
23 #include "mapgen.h"
24 #include "noise.h"
25 #include "map.h"
26 #include "log.h"
27 #include "util/numeric.h"
28 #include <algorithm>
29 #include <vector>
30
31
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},
40         {NULL,              0}
41 };
42
43
44 ///////////////////////////////////////////////////////////////////////////////
45
46
47 DecorationManager::DecorationManager(IGameDef *gamedef) :
48         ObjDefManager(gamedef, OBJDEF_DECORATION)
49 {
50 }
51
52
53 size_t DecorationManager::placeAllDecos(Mapgen *mg, u32 blockseed,
54         v3s16 nmin, v3s16 nmax)
55 {
56         size_t nplaced = 0;
57
58         for (size_t i = 0; i != m_objects.size(); i++) {
59                 Decoration *deco = (Decoration *)m_objects[i];
60                 if (!deco)
61                         continue;
62
63                 nplaced += deco->placeDeco(mg, blockseed, nmin, nmax);
64                 blockseed++;
65         }
66
67         return nplaced;
68 }
69
70
71 ///////////////////////////////////////////////////////////////////////////////
72
73
74 void Decoration::resolveNodeNames()
75 {
76         getIdsFromNrBacklog(&c_place_on);
77         getIdsFromNrBacklog(&c_spawnby);
78 }
79
80
81 bool Decoration::canPlaceDecoration(MMVManip *vm, v3s16 p)
82 {
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()))
86                 return false;
87
88         // Don't continue if there are no spawnby constraints
89         if (nspawnby == -1)
90                 return true;
91
92         int nneighs = 0;
93         static const v3s16 dirs[16] = {
94                 v3s16( 0, 0,  1),
95                 v3s16( 0, 0, -1),
96                 v3s16( 1, 0,  0),
97                 v3s16(-1, 0,  0),
98                 v3s16( 1, 0,  1),
99                 v3s16(-1, 0,  1),
100                 v3s16(-1, 0, -1),
101                 v3s16( 1, 0, -1),
102
103                 v3s16( 0, 1,  1),
104                 v3s16( 0, 1, -1),
105                 v3s16( 1, 1,  0),
106                 v3s16(-1, 1,  0),
107                 v3s16( 1, 1,  1),
108                 v3s16(-1, 1,  1),
109                 v3s16(-1, 1, -1),
110                 v3s16( 1, 1, -1)
111         };
112
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))
117                         continue;
118
119                 if (CONTAINS(c_spawnby, vm->m_data[index].getContent()))
120                         nneighs++;
121         }
122
123         if (nneighs < nspawnby)
124                 return false;
125
126         return true;
127 }
128
129
130 size_t Decoration::placeDeco(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
131 {
132         PcgRandom ps(blockseed + 53);
133         int carea_size = nmax.X - nmin.X + 1;
134
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;
139
140         s16 divlen = carea_size / sidelen;
141         int area = sidelen * sidelen;
142
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
148                 );
149                 v2s16 p2d_min( // Minimum edge of part of division
150                         nmin.X + sidelen * x0,
151                         nmin.Z + sidelen * z0
152                 );
153                 v2s16 p2d_max( // Maximum edge of part of division
154                         nmin.X + sidelen + sidelen * x0 - 1,
155                         nmin.Z + sidelen + sidelen * z0 - 1
156                 );
157
158                 bool cover = false;
159                 // Amount of decorations
160                 float nval = (flags & DECO_USE_NOISE) ?
161                         NoisePerlin2D(&np, p2d_center.X, p2d_center.Y, mapseed) :
162                         fill_ratio;
163                 u32 deco_count = 0;
164
165                 if (nval >= 10.0f) {
166                         // Complete coverage. Disable random placement to avoid
167                         // redundant multiple placements at one position.
168                         cover = true;
169                         deco_count = area;
170                 } else {
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)
177                                         deco_count = 1;
178                         }
179                 }
180
181                 s16 x = p2d_min.X - 1;
182                 s16 z = p2d_min.Y;
183
184                 for (u32 i = 0; i < deco_count; i++) {
185                         if (!cover) {
186                                 x = ps.range(p2d_min.X, p2d_max.X);
187                                 z = ps.range(p2d_min.Y, p2d_max.Y);
188                         } else {
189                                 x++;
190                                 if (x == p2d_max.X + 1) {
191                                         z++;
192                                         x = p2d_min.X;
193                                 }
194                         }
195                         int mapindex = carea_size * (z - nmin.Z) + (x - nmin.X);
196
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())
205                                                 continue;
206                                 }
207
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);
214
215                                 mg->getSurfaces(v2s16(x, z), nmin.Y, nmax.Y, floors, ceilings);
216
217                                 if (flags & DECO_ALL_FLOORS) {
218                                         // Floor decorations
219                                         for (const s16 y : floors) {
220                                                 if (y < y_min || y > y_max)
221                                                         continue;
222
223                                                 v3s16 pos(x, y, z);
224                                                 if (generate(mg->vm, &ps, pos, false))
225                                                         mg->gennotify.addEvent(
226                                                                         GENNOTIFY_DECORATION, pos, index);
227                                         }
228                                 }
229
230                                 if (flags & DECO_ALL_CEILINGS) {
231                                         // Ceiling decorations
232                                         for (const s16 y : ceilings) {
233                                                 if (y < y_min || y > y_max)
234                                                         continue;
235
236                                                 v3s16 pos(x, y, z);
237                                                 if (generate(mg->vm, &ps, pos, true))
238                                                         mg->gennotify.addEvent(
239                                                                         GENNOTIFY_DECORATION, pos, index);
240                                         }
241                                 }
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];
248                                 else
249                                         y = mg->findGroundLevel(v2s16(x, z), nmin.Y, nmax.Y);
250
251                                 if (y < y_min || y > y_max || y < nmin.Y || y > nmax.Y)
252                                         continue;
253
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())
258                                                 continue;
259                                 }
260
261                                 v3s16 pos(x, y, z);
262                                 if (generate(mg->vm, &ps, pos, false))
263                                         mg->gennotify.addEvent(GENNOTIFY_DECORATION, pos, index);
264                         }
265                 }
266         }
267
268         return 0;
269 }
270
271
272 ///////////////////////////////////////////////////////////////////////////////
273
274
275 void DecoSimple::resolveNodeNames()
276 {
277         Decoration::resolveNodeNames();
278         getIdsFromNrBacklog(&c_decos);
279 }
280
281
282 size_t DecoSimple::generate(MMVManip *vm, PcgRandom *pr, v3s16 p, bool ceiling)
283 {
284         // Don't bother if there aren't any decorations to place
285         if (c_decos.empty())
286                 return 0;
287
288         if (!canPlaceDecoration(vm, p))
289                 return 0;
290
291         // Check for placement outside the voxelmanip volume
292         if (ceiling) {
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)
297                         return 0;
298
299                 if (p.Y - 1 - place_offset_y > vm->m_area.MaxEdge.Y)
300                         return 0;
301
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)
305                         return 0;
306
307                 if (p.Y + 1 + place_offset_y < vm->m_area.MinEdge.Y)
308                         return 0;
309         }
310
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);
317
318         const v3s16 &em = vm->m_area.getExtent();
319         u32 vi = vm->m_area.index(p);
320
321         if (ceiling) {
322                 // Ceiling decorations
323                 // 'place offset y' is inverted
324                 VoxelArea::add_y(em, vi, -place_offset_y);
325
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)
330                                 break;
331
332                         vm->m_data[vi] = MapNode(c_place, 0, param2);
333                 }
334         } else { // Heightmap and floor decorations
335                 VoxelArea::add_y(em, vi, place_offset_y);
336
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)
341                                 break;
342
343                         vm->m_data[vi] = MapNode(c_place, 0, param2);
344                 }
345         }
346
347         return 1;
348 }
349
350
351 ///////////////////////////////////////////////////////////////////////////////
352
353
354 size_t DecoSchematic::generate(MMVManip *vm, PcgRandom *pr, v3s16 p, bool ceiling)
355 {
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)
359                 return 0;
360
361         if (!canPlaceDecoration(vm, p))
362                 return 0;
363
364         if (flags & DECO_PLACE_CENTER_Y) {
365                 p.Y -= (schematic->size.Y - 1) / 2;
366         } else {
367                 // Only apply 'place offset y' if not 'deco place center y'
368                 if (ceiling)
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);
372                 else
373                         p.Y += place_offset_y;
374         }
375
376         // Check schematic top and base are in voxelmanip
377         if (p.Y + schematic->size.Y - 1 > vm->m_area.MaxEdge.Y)
378                 return 0;
379
380         if (p.Y < vm->m_area.MinEdge.Y)
381                 return 0;
382
383         Rotation rot = (rotation == ROTATE_RAND) ?
384                 (Rotation)pr->range(ROTATE_0, ROTATE_270) : rotation;
385
386         if (flags & DECO_PLACE_CENTER_X) {
387                 if (rot == ROTATE_0 || rot == ROTATE_180)
388                         p.X -= (schematic->size.X - 1) / 2;
389                 else
390                         p.Z -= (schematic->size.X - 1) / 2;
391         }
392         if (flags & DECO_PLACE_CENTER_Z) {
393                 if (rot == ROTATE_0 || rot == ROTATE_180)
394                         p.Z -= (schematic->size.Z - 1) / 2;
395                 else
396                         p.X -= (schematic->size.Z - 1) / 2;
397         }
398
399         bool force_placement = (flags & DECO_FORCE_PLACEMENT);
400
401         schematic->blitToVManip(vm, p, rot, force_placement);
402
403         return 1;
404 }