]> git.lizzy.rs Git - minetest.git/blob - src/mapgen/mg_decoration.cpp
Move files to subdirectories (#6599)
[minetest.git] / src / mapgen / mg_decoration.cpp
1 /*
2 Minetest
3 Copyright (C) 2014-2016 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
4 Copyright (C) 2015-2017 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
30
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},
39         {NULL,              0}
40 };
41
42
43 ///////////////////////////////////////////////////////////////////////////////
44
45
46 DecorationManager::DecorationManager(IGameDef *gamedef) :
47         ObjDefManager(gamedef, OBJDEF_DECORATION)
48 {
49 }
50
51
52 size_t DecorationManager::placeAllDecos(Mapgen *mg, u32 blockseed,
53         v3s16 nmin, v3s16 nmax)
54 {
55         size_t nplaced = 0;
56
57         for (size_t i = 0; i != m_objects.size(); i++) {
58                 Decoration *deco = (Decoration *)m_objects[i];
59                 if (!deco)
60                         continue;
61
62                 nplaced += deco->placeDeco(mg, blockseed, nmin, nmax);
63                 blockseed++;
64         }
65
66         return nplaced;
67 }
68
69
70 ///////////////////////////////////////////////////////////////////////////////
71
72
73 void Decoration::resolveNodeNames()
74 {
75         getIdsFromNrBacklog(&c_place_on);
76         getIdsFromNrBacklog(&c_spawnby);
77 }
78
79
80 bool Decoration::canPlaceDecoration(MMVManip *vm, v3s16 p)
81 {
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()))
85                 return false;
86
87         // Don't continue if there are no spawnby constraints
88         if (nspawnby == -1)
89                 return true;
90
91         int nneighs = 0;
92         static const v3s16 dirs[16] = {
93                 v3s16( 0, 0,  1),
94                 v3s16( 0, 0, -1),
95                 v3s16( 1, 0,  0),
96                 v3s16(-1, 0,  0),
97                 v3s16( 1, 0,  1),
98                 v3s16(-1, 0,  1),
99                 v3s16(-1, 0, -1),
100                 v3s16( 1, 0, -1),
101
102                 v3s16( 0, 1,  1),
103                 v3s16( 0, 1, -1),
104                 v3s16( 1, 1,  0),
105                 v3s16(-1, 1,  0),
106                 v3s16( 1, 1,  1),
107                 v3s16(-1, 1,  1),
108                 v3s16(-1, 1, -1),
109                 v3s16( 1, 1, -1)
110         };
111
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))
116                         continue;
117
118                 if (CONTAINS(c_spawnby, vm->m_data[index].getContent()))
119                         nneighs++;
120         }
121
122         if (nneighs < nspawnby)
123                 return false;
124
125         return true;
126 }
127
128
129 size_t Decoration::placeDeco(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
130 {
131         PcgRandom ps(blockseed + 53);
132         int carea_size = nmax.X - nmin.X + 1;
133
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;
138
139         s16 divlen = carea_size / sidelen;
140         int area = sidelen * sidelen;
141
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
147                 );
148                 v2s16 p2d_min( // Minimum edge of part of division
149                         nmin.X + sidelen * x0,
150                         nmin.Z + sidelen * z0
151                 );
152                 v2s16 p2d_max( // Maximum edge of part of division
153                         nmin.X + sidelen + sidelen * x0 - 1,
154                         nmin.Z + sidelen + sidelen * z0 - 1
155                 );
156
157                 // Amount of decorations
158                 float nval = (flags & DECO_USE_NOISE) ?
159                         NoisePerlin2D(&np, p2d_center.X, p2d_center.Y, mapseed) :
160                         fill_ratio;
161                 u32 deco_count = 0;
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)
168                                 deco_count = 1;
169                 }
170
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);
175
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())
184                                                 continue;
185                                 }
186
187                                 // Get all floors and ceilings in node column
188                                 u16 size = (nmax.Y - nmin.Y + 1) / 2;
189                                 s16 floors[size];
190                                 s16 ceilings[size];
191                                 u16 num_floors = 0;
192                                 u16 num_ceilings = 0;
193
194                                 mg->getSurfaces(v2s16(x, z), nmin.Y, nmax.Y,
195                                         floors, ceilings, &num_floors, &num_ceilings);
196
197                                 if ((flags & DECO_ALL_FLOORS) && num_floors > 0) {
198                                         // Floor decorations
199                                         for (u16 fi = 0; fi < num_floors; fi++) {
200                                                 s16 y = floors[fi];
201                                                 if (y < y_min || y > y_max)
202                                                         continue;
203
204                                                 v3s16 pos(x, y, z);
205                                                 if (generate(mg->vm, &ps, pos, false))
206                                                         mg->gennotify.addEvent(
207                                                                         GENNOTIFY_DECORATION, pos, index);
208                                         }
209                                 }
210
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)
216                                                         continue;
217
218                                                 v3s16 pos(x, y, z);
219                                                 if (generate(mg->vm, &ps, pos, true))
220                                                         mg->gennotify.addEvent(
221                                                                         GENNOTIFY_DECORATION, pos, index);
222                                         }
223                                 }
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];
230                                 else
231                                         y = mg->findGroundLevel(v2s16(x, z), nmin.Y, nmax.Y);
232
233                                 if (y < y_min || y > y_max || y < nmin.Y || y > nmax.Y)
234                                         continue;
235
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())
240                                                 continue;
241                                 }
242
243                                 v3s16 pos(x, y, z);
244                                 if (generate(mg->vm, &ps, pos, false))
245                                         mg->gennotify.addEvent(GENNOTIFY_DECORATION, pos, index);
246                         }
247                 }
248         }
249
250         return 0;
251 }
252
253
254 ///////////////////////////////////////////////////////////////////////////////
255
256
257 void DecoSimple::resolveNodeNames()
258 {
259         Decoration::resolveNodeNames();
260         getIdsFromNrBacklog(&c_decos);
261 }
262
263
264 size_t DecoSimple::generate(MMVManip *vm, PcgRandom *pr, v3s16 p, bool ceiling)
265 {
266         // Don't bother if there aren't any decorations to place
267         if (c_decos.empty())
268                 return 0;
269
270         if (!canPlaceDecoration(vm, p))
271                 return 0;
272
273         // Check for placement outside the voxelmanip volume
274         if (ceiling) {
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)
279                         return 0;
280
281                 if (p.Y - 1 - place_offset_y > vm->m_area.MaxEdge.Y)
282                         return 0;
283
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)
287                         return 0;
288
289                 if (p.Y + 1 + place_offset_y < vm->m_area.MinEdge.Y)
290                         return 0;
291         }
292
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);
299
300         const v3s16 &em = vm->m_area.getExtent();
301         u32 vi = vm->m_area.index(p);
302
303         if (ceiling) {
304                 // Ceiling decorations
305                 // 'place offset y' is inverted
306                 vm->m_area.add_y(em, vi, -place_offset_y);
307
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)
312                                 break;
313
314                         vm->m_data[vi] = MapNode(c_place, 0, param2);
315                 }
316         } else { // Heightmap and floor decorations
317                 vm->m_area.add_y(em, vi, place_offset_y);
318
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)
323                                 break;
324
325                         vm->m_data[vi] = MapNode(c_place, 0, param2);
326                 }
327         }
328
329         return 1;
330 }
331
332
333 ///////////////////////////////////////////////////////////////////////////////
334
335
336 size_t DecoSchematic::generate(MMVManip *vm, PcgRandom *pr, v3s16 p, bool ceiling)
337 {
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)
341                 return 0;
342
343         if (!canPlaceDecoration(vm, p))
344                 return 0;
345
346         if (flags & DECO_PLACE_CENTER_Y) {
347                 p.Y -= (schematic->size.Y - 1) / 2;
348         } else {
349                 // Only apply 'place offset y' if not 'deco place center y'
350                 if (ceiling)
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);
354                 else
355                         p.Y += place_offset_y;
356         }
357
358         // Check schematic top and base are in voxelmanip
359         if (p.Y + schematic->size.Y - 1 > vm->m_area.MaxEdge.Y)
360                 return 0;
361
362         if (p.Y < vm->m_area.MinEdge.Y)
363                 return 0;
364
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;
369
370         Rotation rot = (rotation == ROTATE_RAND) ?
371                 (Rotation)pr->range(ROTATE_0, ROTATE_270) : rotation;
372         bool force_placement = (flags & DECO_FORCE_PLACEMENT);
373
374         schematic->blitToVManip(vm, p, rot, force_placement);
375
376         return 1;
377 }