]> git.lizzy.rs Git - dragonfireclient.git/blob - src/mapgen/mg_decoration.cpp
Merge pull request #59 from PrairieAstronomer/readme_irrlicht_change
[dragonfireclient.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                                         auto iter = biomes.find(mg->biomemap[mapindex]);
210                                         if (iter == biomes.end())
211                                                 continue;
212                                 }
213
214                                 // Get all floors and ceilings in node column
215                                 u16 size = (nmax.Y - nmin.Y + 1) / 2;
216                                 std::vector<s16> floors;
217                                 std::vector<s16> ceilings;
218                                 floors.reserve(size);
219                                 ceilings.reserve(size);
220
221                                 mg->getSurfaces(v2s16(x, z), nmin.Y, nmax.Y, floors, ceilings);
222
223                                 if (flags & DECO_ALL_FLOORS) {
224                                         // Floor decorations
225                                         for (const s16 y : floors) {
226                                                 if (y < y_min || y > y_max)
227                                                         continue;
228
229                                                 v3s16 pos(x, y, z);
230                                                 if (generate(mg->vm, &ps, pos, false))
231                                                         mg->gennotify.addEvent(
232                                                                         GENNOTIFY_DECORATION, pos, index);
233                                         }
234                                 }
235
236                                 if (flags & DECO_ALL_CEILINGS) {
237                                         // Ceiling decorations
238                                         for (const s16 y : ceilings) {
239                                                 if (y < y_min || y > y_max)
240                                                         continue;
241
242                                                 v3s16 pos(x, y, z);
243                                                 if (generate(mg->vm, &ps, pos, true))
244                                                         mg->gennotify.addEvent(
245                                                                         GENNOTIFY_DECORATION, pos, index);
246                                         }
247                                 }
248                         } else { // Heightmap decorations
249                                 s16 y = -MAX_MAP_GENERATION_LIMIT;
250                                 if (flags & DECO_LIQUID_SURFACE)
251                                         y = mg->findLiquidSurface(v2s16(x, z), nmin.Y, nmax.Y);
252                                 else if (mg->heightmap)
253                                         y = mg->heightmap[mapindex];
254                                 else
255                                         y = mg->findGroundLevel(v2s16(x, z), nmin.Y, nmax.Y);
256
257                                 if (y < y_min || y > y_max || y < nmin.Y || y > nmax.Y)
258                                         continue;
259
260                                 if (mg->biomemap && !biomes.empty()) {
261                                         auto iter = biomes.find(mg->biomemap[mapindex]);
262                                         if (iter == biomes.end())
263                                                 continue;
264                                 }
265
266                                 v3s16 pos(x, y, z);
267                                 if (generate(mg->vm, &ps, pos, false))
268                                         mg->gennotify.addEvent(GENNOTIFY_DECORATION, pos, index);
269                         }
270                 }
271         }
272
273         return 0;
274 }
275
276
277 void Decoration::cloneTo(Decoration *def) const
278 {
279         ObjDef::cloneTo(def);
280         def->flags = flags;
281         def->mapseed = mapseed;
282         def->c_place_on = c_place_on;
283         def->sidelen = sidelen;
284         def->y_min = y_min;
285         def->y_max = y_max;
286         def->fill_ratio = fill_ratio;
287         def->np = np;
288         def->c_spawnby = c_spawnby;
289         def->nspawnby = nspawnby;
290         def->place_offset_y = place_offset_y;
291         def->biomes = biomes;
292 }
293
294
295 ///////////////////////////////////////////////////////////////////////////////
296
297
298 ObjDef *DecoSimple::clone() const
299 {
300         auto def = new DecoSimple();
301         Decoration::cloneTo(def);
302
303         def->c_decos = c_decos;
304         def->deco_height = deco_height;
305         def->deco_height_max = deco_height_max;
306         def->deco_param2 = deco_param2;
307         def->deco_param2_max = deco_param2_max;
308
309         return def;
310 }
311
312
313 void DecoSimple::resolveNodeNames()
314 {
315         Decoration::resolveNodeNames();
316         getIdsFromNrBacklog(&c_decos);
317 }
318
319
320 size_t DecoSimple::generate(MMVManip *vm, PcgRandom *pr, v3s16 p, bool ceiling)
321 {
322         // Don't bother if there aren't any decorations to place
323         if (c_decos.empty())
324                 return 0;
325
326         if (!canPlaceDecoration(vm, p))
327                 return 0;
328
329         // Check for placement outside the voxelmanip volume
330         if (ceiling) {
331                 // Ceiling decorations
332                 // 'place offset y' is inverted
333                 if (p.Y - place_offset_y - std::max(deco_height, deco_height_max) <
334                                 vm->m_area.MinEdge.Y)
335                         return 0;
336
337                 if (p.Y - 1 - place_offset_y > vm->m_area.MaxEdge.Y)
338                         return 0;
339
340         } else { // Heightmap and floor decorations
341                 if (p.Y + place_offset_y + std::max(deco_height, deco_height_max) >
342                                 vm->m_area.MaxEdge.Y)
343                         return 0;
344
345                 if (p.Y + 1 + place_offset_y < vm->m_area.MinEdge.Y)
346                         return 0;
347         }
348
349         content_t c_place = c_decos[pr->range(0, c_decos.size() - 1)];
350         s16 height = (deco_height_max > 0) ?
351                 pr->range(deco_height, deco_height_max) : deco_height;
352         u8 param2 = (deco_param2_max > 0) ?
353                 pr->range(deco_param2, deco_param2_max) : deco_param2;
354         bool force_placement = (flags & DECO_FORCE_PLACEMENT);
355
356         const v3s16 &em = vm->m_area.getExtent();
357         u32 vi = vm->m_area.index(p);
358
359         if (ceiling) {
360                 // Ceiling decorations
361                 // 'place offset y' is inverted
362                 VoxelArea::add_y(em, vi, -place_offset_y);
363
364                 for (int i = 0; i < height; i++) {
365                         VoxelArea::add_y(em, vi, -1);
366                         content_t c = vm->m_data[vi].getContent();
367                         if (c != CONTENT_AIR && c != CONTENT_IGNORE && !force_placement)
368                                 break;
369
370                         vm->m_data[vi] = MapNode(c_place, 0, param2);
371                 }
372         } else { // Heightmap and floor decorations
373                 VoxelArea::add_y(em, vi, place_offset_y);
374
375                 for (int i = 0; i < height; i++) {
376                         VoxelArea::add_y(em, vi, 1);
377                         content_t c = vm->m_data[vi].getContent();
378                         if (c != CONTENT_AIR && c != CONTENT_IGNORE && !force_placement)
379                                 break;
380
381                         vm->m_data[vi] = MapNode(c_place, 0, param2);
382                 }
383         }
384
385         return 1;
386 }
387
388
389 ///////////////////////////////////////////////////////////////////////////////
390
391
392 DecoSchematic::~DecoSchematic()
393 {
394         if (was_cloned)
395                 delete schematic;
396 }
397
398
399 ObjDef *DecoSchematic::clone() const
400 {
401         auto def = new DecoSchematic();
402         Decoration::cloneTo(def);
403         NodeResolver::cloneTo(def);
404
405         def->rotation = rotation;
406         /* FIXME: We do not own this schematic, yet we only have a pointer to it
407          * and not a handle. We are left with no option but to clone it ourselves.
408          * This is a waste of memory and should be replaced with an alternative
409          * approach sometime. */
410         def->schematic = dynamic_cast<Schematic*>(schematic->clone());
411         def->was_cloned = true;
412
413         return def;
414 }
415
416
417 size_t DecoSchematic::generate(MMVManip *vm, PcgRandom *pr, v3s16 p, bool ceiling)
418 {
419         // Schematic could have been unloaded but not the decoration
420         // In this case generate() does nothing (but doesn't *fail*)
421         if (schematic == NULL)
422                 return 0;
423
424         if (!canPlaceDecoration(vm, p))
425                 return 0;
426
427         if (flags & DECO_PLACE_CENTER_Y) {
428                 p.Y -= (schematic->size.Y - 1) / 2;
429         } else {
430                 // Only apply 'place offset y' if not 'deco place center y'
431                 if (ceiling)
432                         // Shift down so that schematic top layer is level with ceiling
433                         // 'place offset y' is inverted
434                         p.Y -= (place_offset_y + schematic->size.Y - 1);
435                 else
436                         p.Y += place_offset_y;
437         }
438
439         // Check schematic top and base are in voxelmanip
440         if (p.Y + schematic->size.Y - 1 > vm->m_area.MaxEdge.Y)
441                 return 0;
442
443         if (p.Y < vm->m_area.MinEdge.Y)
444                 return 0;
445
446         Rotation rot = (rotation == ROTATE_RAND) ?
447                 (Rotation)pr->range(ROTATE_0, ROTATE_270) : rotation;
448
449         if (flags & DECO_PLACE_CENTER_X) {
450                 if (rot == ROTATE_0 || rot == ROTATE_180)
451                         p.X -= (schematic->size.X - 1) / 2;
452                 else
453                         p.Z -= (schematic->size.X - 1) / 2;
454         }
455         if (flags & DECO_PLACE_CENTER_Z) {
456                 if (rot == ROTATE_0 || rot == ROTATE_180)
457                         p.Z -= (schematic->size.Z - 1) / 2;
458                 else
459                         p.X -= (schematic->size.Z - 1) / 2;
460         }
461
462         bool force_placement = (flags & DECO_FORCE_PLACEMENT);
463
464         schematic->blitToVManip(vm, p, rot, force_placement);
465
466         return 1;
467 }