Allow ObjDefManager instances to be cloned
[oweals/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 DecorationManager *DecorationManager::clone() const
71 {
72         auto mgr = new DecorationManager();
73         ObjDefManager::cloneTo(mgr);
74         return mgr;
75 }
76
77
78 ///////////////////////////////////////////////////////////////////////////////
79
80
81 void Decoration::resolveNodeNames()
82 {
83         getIdsFromNrBacklog(&c_place_on);
84         getIdsFromNrBacklog(&c_spawnby);
85 }
86
87
88 bool Decoration::canPlaceDecoration(MMVManip *vm, v3s16 p)
89 {
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()))
93                 return false;
94
95         // Don't continue if there are no spawnby constraints
96         if (nspawnby == -1)
97                 return true;
98
99         int nneighs = 0;
100         static const v3s16 dirs[16] = {
101                 v3s16( 0, 0,  1),
102                 v3s16( 0, 0, -1),
103                 v3s16( 1, 0,  0),
104                 v3s16(-1, 0,  0),
105                 v3s16( 1, 0,  1),
106                 v3s16(-1, 0,  1),
107                 v3s16(-1, 0, -1),
108                 v3s16( 1, 0, -1),
109
110                 v3s16( 0, 1,  1),
111                 v3s16( 0, 1, -1),
112                 v3s16( 1, 1,  0),
113                 v3s16(-1, 1,  0),
114                 v3s16( 1, 1,  1),
115                 v3s16(-1, 1,  1),
116                 v3s16(-1, 1, -1),
117                 v3s16( 1, 1, -1)
118         };
119
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))
124                         continue;
125
126                 if (CONTAINS(c_spawnby, vm->m_data[index].getContent()))
127                         nneighs++;
128         }
129
130         if (nneighs < nspawnby)
131                 return false;
132
133         return true;
134 }
135
136
137 size_t Decoration::placeDeco(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
138 {
139         PcgRandom ps(blockseed + 53);
140         int carea_size = nmax.X - nmin.X + 1;
141
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;
146
147         s16 divlen = carea_size / sidelen;
148         int area = sidelen * sidelen;
149
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
155                 );
156                 v2s16 p2d_min( // Minimum edge of part of division
157                         nmin.X + sidelen * x0,
158                         nmin.Z + sidelen * z0
159                 );
160                 v2s16 p2d_max( // Maximum edge of part of division
161                         nmin.X + sidelen + sidelen * x0 - 1,
162                         nmin.Z + sidelen + sidelen * z0 - 1
163                 );
164
165                 bool cover = false;
166                 // Amount of decorations
167                 float nval = (flags & DECO_USE_NOISE) ?
168                         NoisePerlin2D(&np, p2d_center.X, p2d_center.Y, mapseed) :
169                         fill_ratio;
170                 u32 deco_count = 0;
171
172                 if (nval >= 10.0f) {
173                         // Complete coverage. Disable random placement to avoid
174                         // redundant multiple placements at one position.
175                         cover = true;
176                         deco_count = area;
177                 } else {
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)
184                                         deco_count = 1;
185                         }
186                 }
187
188                 s16 x = p2d_min.X - 1;
189                 s16 z = p2d_min.Y;
190
191                 for (u32 i = 0; i < deco_count; i++) {
192                         if (!cover) {
193                                 x = ps.range(p2d_min.X, p2d_max.X);
194                                 z = ps.range(p2d_min.Y, p2d_max.Y);
195                         } else {
196                                 x++;
197                                 if (x == p2d_max.X + 1) {
198                                         z++;
199                                         x = p2d_min.X;
200                                 }
201                         }
202                         int mapindex = carea_size * (z - nmin.Z) + (x - nmin.X);
203
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())
212                                                 continue;
213                                 }
214
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);
221
222                                 mg->getSurfaces(v2s16(x, z), nmin.Y, nmax.Y, floors, ceilings);
223
224                                 if (flags & DECO_ALL_FLOORS) {
225                                         // Floor decorations
226                                         for (const s16 y : floors) {
227                                                 if (y < y_min || y > y_max)
228                                                         continue;
229
230                                                 v3s16 pos(x, y, z);
231                                                 if (generate(mg->vm, &ps, pos, false))
232                                                         mg->gennotify.addEvent(
233                                                                         GENNOTIFY_DECORATION, pos, index);
234                                         }
235                                 }
236
237                                 if (flags & DECO_ALL_CEILINGS) {
238                                         // Ceiling decorations
239                                         for (const s16 y : ceilings) {
240                                                 if (y < y_min || y > y_max)
241                                                         continue;
242
243                                                 v3s16 pos(x, y, z);
244                                                 if (generate(mg->vm, &ps, pos, true))
245                                                         mg->gennotify.addEvent(
246                                                                         GENNOTIFY_DECORATION, pos, index);
247                                         }
248                                 }
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];
255                                 else
256                                         y = mg->findGroundLevel(v2s16(x, z), nmin.Y, nmax.Y);
257
258                                 if (y < y_min || y > y_max || y < nmin.Y || y > nmax.Y)
259                                         continue;
260
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())
265                                                 continue;
266                                 }
267
268                                 v3s16 pos(x, y, z);
269                                 if (generate(mg->vm, &ps, pos, false))
270                                         mg->gennotify.addEvent(GENNOTIFY_DECORATION, pos, index);
271                         }
272                 }
273         }
274
275         return 0;
276 }
277
278
279 void Decoration::cloneTo(Decoration *def) const
280 {
281         ObjDef::cloneTo(def);
282         def->flags = flags;
283         def->mapseed = mapseed;
284         def->c_place_on = c_place_on;
285         def->sidelen = sidelen;
286         def->y_min = y_min;
287         def->y_max = y_max;
288         def->fill_ratio = fill_ratio;
289         def->np = np;
290         def->c_spawnby = c_spawnby;
291         def->nspawnby = nspawnby;
292         def->place_offset_y = place_offset_y;
293         def->biomes = biomes;
294 }
295
296
297 ///////////////////////////////////////////////////////////////////////////////
298
299
300 ObjDef *DecoSimple::clone() const
301 {
302         auto def = new DecoSimple();
303         Decoration::cloneTo(def);
304
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;
310
311         return def;
312 }
313
314
315 void DecoSimple::resolveNodeNames()
316 {
317         Decoration::resolveNodeNames();
318         getIdsFromNrBacklog(&c_decos);
319 }
320
321
322 size_t DecoSimple::generate(MMVManip *vm, PcgRandom *pr, v3s16 p, bool ceiling)
323 {
324         // Don't bother if there aren't any decorations to place
325         if (c_decos.empty())
326                 return 0;
327
328         if (!canPlaceDecoration(vm, p))
329                 return 0;
330
331         // Check for placement outside the voxelmanip volume
332         if (ceiling) {
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)
337                         return 0;
338
339                 if (p.Y - 1 - place_offset_y > vm->m_area.MaxEdge.Y)
340                         return 0;
341
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)
345                         return 0;
346
347                 if (p.Y + 1 + place_offset_y < vm->m_area.MinEdge.Y)
348                         return 0;
349         }
350
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);
357
358         const v3s16 &em = vm->m_area.getExtent();
359         u32 vi = vm->m_area.index(p);
360
361         if (ceiling) {
362                 // Ceiling decorations
363                 // 'place offset y' is inverted
364                 VoxelArea::add_y(em, vi, -place_offset_y);
365
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)
370                                 break;
371
372                         vm->m_data[vi] = MapNode(c_place, 0, param2);
373                 }
374         } else { // Heightmap and floor decorations
375                 VoxelArea::add_y(em, vi, place_offset_y);
376
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)
381                                 break;
382
383                         vm->m_data[vi] = MapNode(c_place, 0, param2);
384                 }
385         }
386
387         return 1;
388 }
389
390
391 ///////////////////////////////////////////////////////////////////////////////
392
393
394 ObjDef *DecoSchematic::clone() const
395 {
396         auto def = new DecoSchematic();
397         Decoration::cloneTo(def);
398         NodeResolver::cloneTo(def);
399
400         def->rotation = rotation;
401         /* FIXME: This is not ideal, we only have a pointer to the schematic despite
402          * not owning it. Optimally this would be a handle. */
403         def->schematic = schematic; // not cloned
404
405         return def;
406 }
407
408
409 size_t DecoSchematic::generate(MMVManip *vm, PcgRandom *pr, v3s16 p, bool ceiling)
410 {
411         // Schematic could have been unloaded but not the decoration
412         // In this case generate() does nothing (but doesn't *fail*)
413         if (schematic == NULL)
414                 return 0;
415
416         if (!canPlaceDecoration(vm, p))
417                 return 0;
418
419         if (flags & DECO_PLACE_CENTER_Y) {
420                 p.Y -= (schematic->size.Y - 1) / 2;
421         } else {
422                 // Only apply 'place offset y' if not 'deco place center y'
423                 if (ceiling)
424                         // Shift down so that schematic top layer is level with ceiling
425                         // 'place offset y' is inverted
426                         p.Y -= (place_offset_y + schematic->size.Y - 1);
427                 else
428                         p.Y += place_offset_y;
429         }
430
431         // Check schematic top and base are in voxelmanip
432         if (p.Y + schematic->size.Y - 1 > vm->m_area.MaxEdge.Y)
433                 return 0;
434
435         if (p.Y < vm->m_area.MinEdge.Y)
436                 return 0;
437
438         Rotation rot = (rotation == ROTATE_RAND) ?
439                 (Rotation)pr->range(ROTATE_0, ROTATE_270) : rotation;
440
441         if (flags & DECO_PLACE_CENTER_X) {
442                 if (rot == ROTATE_0 || rot == ROTATE_180)
443                         p.X -= (schematic->size.X - 1) / 2;
444                 else
445                         p.Z -= (schematic->size.X - 1) / 2;
446         }
447         if (flags & DECO_PLACE_CENTER_Z) {
448                 if (rot == ROTATE_0 || rot == ROTATE_180)
449                         p.Z -= (schematic->size.Z - 1) / 2;
450                 else
451                         p.X -= (schematic->size.Z - 1) / 2;
452         }
453
454         bool force_placement = (flags & DECO_FORCE_PLACEMENT);
455
456         schematic->blitToVManip(vm, p, rot, force_placement);
457
458         return 1;
459 }