]> git.lizzy.rs Git - minetest.git/blob - src/mg_decoration.cpp
Particles: Do not add digging particles for airlike nodes (#6392)
[minetest.git] / src / 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         {NULL,              0}
38 };
39
40
41 ///////////////////////////////////////////////////////////////////////////////
42
43
44 DecorationManager::DecorationManager(IGameDef *gamedef) :
45         ObjDefManager(gamedef, OBJDEF_DECORATION)
46 {
47 }
48
49
50 size_t DecorationManager::placeAllDecos(Mapgen *mg, u32 blockseed,
51         v3s16 nmin, v3s16 nmax, s16 deco_zero_level)
52 {
53         size_t nplaced = 0;
54
55         for (size_t i = 0; i != m_objects.size(); i++) {
56                 Decoration *deco = (Decoration *)m_objects[i];
57                 if (!deco)
58                         continue;
59
60                 nplaced += deco->placeDeco(mg, blockseed, nmin, nmax, deco_zero_level);
61                 blockseed++;
62         }
63
64         return nplaced;
65 }
66
67
68 ///////////////////////////////////////////////////////////////////////////////
69
70 void Decoration::resolveNodeNames()
71 {
72         getIdsFromNrBacklog(&c_place_on);
73         getIdsFromNrBacklog(&c_spawnby);
74 }
75
76
77 bool Decoration::canPlaceDecoration(MMVManip *vm, v3s16 p)
78 {
79         // Check if the decoration can be placed on this node
80         u32 vi = vm->m_area.index(p);
81         if (!CONTAINS(c_place_on, vm->m_data[vi].getContent()))
82                 return false;
83
84         // Don't continue if there are no spawnby constraints
85         if (nspawnby == -1)
86                 return true;
87
88         int nneighs = 0;
89         static const v3s16 dirs[16] = {
90                 v3s16( 0, 0,  1),
91                 v3s16( 0, 0, -1),
92                 v3s16( 1, 0,  0),
93                 v3s16(-1, 0,  0),
94                 v3s16( 1, 0,  1),
95                 v3s16(-1, 0,  1),
96                 v3s16(-1, 0, -1),
97                 v3s16( 1, 0, -1),
98
99                 v3s16( 0, 1,  1),
100                 v3s16( 0, 1, -1),
101                 v3s16( 1, 1,  0),
102                 v3s16(-1, 1,  0),
103                 v3s16( 1, 1,  1),
104                 v3s16(-1, 1,  1),
105                 v3s16(-1, 1, -1),
106                 v3s16( 1, 1, -1)
107         };
108
109         // Check these 16 neighbouring nodes for enough spawnby nodes
110         for (size_t i = 0; i != ARRLEN(dirs); i++) {
111                 u32 index = vm->m_area.index(p + dirs[i]);
112                 if (!vm->m_area.contains(index))
113                         continue;
114
115                 if (CONTAINS(c_spawnby, vm->m_data[index].getContent()))
116                         nneighs++;
117         }
118
119         if (nneighs < nspawnby)
120                 return false;
121
122         return true;
123 }
124
125
126 size_t Decoration::placeDeco(Mapgen *mg, u32 blockseed,
127         v3s16 nmin, v3s16 nmax, s16 deco_zero_level)
128 {
129         // Decoration y_min / y_max is displaced by deco_zero_level or remains
130         // unchanged. Any decoration with a limit at +-MAX_MAP_GENERATION_LIMIT is
131         // considered to have that limit at +-infinity, so we do not alter that limit.
132         s32 y_min_disp = (y_min <= -MAX_MAP_GENERATION_LIMIT) ?
133                 -MAX_MAP_GENERATION_LIMIT : y_min + deco_zero_level;
134
135         s32 y_max_disp = (y_max >= MAX_MAP_GENERATION_LIMIT) ?
136                 MAX_MAP_GENERATION_LIMIT : y_max + deco_zero_level;
137
138         PcgRandom ps(blockseed + 53);
139         int carea_size = nmax.X - nmin.X + 1;
140
141         // Divide area into parts
142         // If chunksize is changed it may no longer be divisable by sidelen
143         if (carea_size % sidelen)
144                 sidelen = carea_size;
145
146         s16 divlen = carea_size / sidelen;
147         int area = sidelen * sidelen;
148
149         for (s16 z0 = 0; z0 < divlen; z0++)
150         for (s16 x0 = 0; x0 < divlen; x0++) {
151                 v2s16 p2d_center( // Center position of part of division
152                         nmin.X + sidelen / 2 + sidelen * x0,
153                         nmin.Z + sidelen / 2 + sidelen * z0
154                 );
155                 v2s16 p2d_min( // Minimum edge of part of division
156                         nmin.X + sidelen * x0,
157                         nmin.Z + sidelen * z0
158                 );
159                 v2s16 p2d_max( // Maximum edge of part of division
160                         nmin.X + sidelen + sidelen * x0 - 1,
161                         nmin.Z + sidelen + sidelen * z0 - 1
162                 );
163
164                 // Amount of decorations
165                 float nval = (flags & DECO_USE_NOISE) ?
166                         NoisePerlin2D(&np, p2d_center.X, p2d_center.Y, mapseed) :
167                         fill_ratio;
168                 u32 deco_count = 0;
169                 float deco_count_f = (float)area * nval;
170                 if (deco_count_f >= 1.f) {
171                         deco_count = deco_count_f;
172                 } else if (deco_count_f > 0.f) {
173                         // For low density decorations calculate a chance for 1 decoration
174                         if (ps.range(1000) <= deco_count_f * 1000.f)
175                                 deco_count = 1;
176                 }
177
178                 for (u32 i = 0; i < deco_count; i++) {
179                         s16 x = ps.range(p2d_min.X, p2d_max.X);
180                         s16 z = ps.range(p2d_min.Y, p2d_max.Y);
181
182                         int mapindex = carea_size * (z - nmin.Z) + (x - nmin.X);
183
184                         s16 y = -MAX_MAP_GENERATION_LIMIT;
185                         if (flags & DECO_LIQUID_SURFACE)
186                                 y = mg->findLiquidSurface(v2s16(x, z), nmin.Y, nmax.Y);
187                         else if (mg->heightmap)
188                                 y = mg->heightmap[mapindex];
189                         else
190                                 y = mg->findGroundLevel(v2s16(x, z), nmin.Y, nmax.Y);
191
192                         if (y < y_min_disp || y > y_max_disp || y < nmin.Y || y > nmax.Y)
193                                 continue;
194
195                         if (y + getHeight() > mg->vm->m_area.MaxEdge.Y) {
196                                 continue;
197 #if 0
198                                 printf("Decoration at (%d %d %d) cut off\n", x, y, z);
199                                 //add to queue
200                                 MutexAutoLock cutofflock(cutoff_mutex);
201                                 cutoffs.push_back(CutoffData(x, y, z, height));
202 #endif
203                         }
204
205                         if (mg->biomemap && !biomes.empty()) {
206                                 std::unordered_set<u8>::const_iterator iter =
207                                         biomes.find(mg->biomemap[mapindex]);
208                                 if (iter == biomes.end())
209                                         continue;
210                         }
211
212                         v3s16 pos(x, y, z);
213                         if (generate(mg->vm, &ps, pos))
214                                 mg->gennotify.addEvent(GENNOTIFY_DECORATION, pos, index);
215                 }
216         }
217
218         return 0;
219 }
220
221
222 #if 0
223 void Decoration::placeCutoffs(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
224 {
225         PcgRandom pr(blockseed + 53);
226         std::vector<CutoffData> handled_cutoffs;
227
228         // Copy over the cutoffs we're interested in so we don't needlessly hold a lock
229         {
230                 MutexAutoLock cutofflock(cutoff_mutex);
231                 for (std::list<CutoffData>::iterator i = cutoffs.begin();
232                         i != cutoffs.end(); ++i) {
233                         CutoffData cutoff = *i;
234                         v3s16 p    = cutoff.p;
235                         s16 height = cutoff.height;
236                         if (p.X < nmin.X || p.X > nmax.X ||
237                                 p.Z < nmin.Z || p.Z > nmax.Z)
238                                 continue;
239                         if (p.Y + height < nmin.Y || p.Y > nmax.Y)
240                                 continue;
241
242                         handled_cutoffs.push_back(cutoff);
243                 }
244         }
245
246         // Generate the cutoffs
247         for (size_t i = 0; i != handled_cutoffs.size(); i++) {
248                 v3s16 p    = handled_cutoffs[i].p;
249                 s16 height = handled_cutoffs[i].height;
250
251                 if (p.Y + height > nmax.Y) {
252                         //printf("Decoration at (%d %d %d) cut off again!\n", p.X, p.Y, p.Z);
253                         cuttoffs.push_back(v3s16(p.X, p.Y, p.Z));
254                 }
255
256                 generate(mg, &pr, nmax.Y, nmin.Y - p.Y, v3s16(p.X, nmin.Y, p.Z));
257         }
258
259         // Remove cutoffs that were handled from the cutoff list
260         {
261                 MutexAutoLock cutofflock(cutoff_mutex);
262                 for (std::list<CutoffData>::iterator i = cutoffs.begin();
263                         i != cutoffs.end(); ++i) {
264
265                         for (size_t j = 0; j != handled_cutoffs.size(); j++) {
266                                 CutoffData coff = *i;
267                                 if (coff.p == handled_cutoffs[j].p)
268                                         i = cutoffs.erase(i);
269                         }
270                 }
271         }
272 }
273 #endif
274
275
276 ///////////////////////////////////////////////////////////////////////////////
277
278
279 void DecoSimple::resolveNodeNames()
280 {
281         Decoration::resolveNodeNames();
282         getIdsFromNrBacklog(&c_decos);
283 }
284
285
286 size_t DecoSimple::generate(MMVManip *vm, PcgRandom *pr, v3s16 p)
287 {
288         // Don't bother if there aren't any decorations to place
289         if (c_decos.empty())
290                 return 0;
291
292         if (!canPlaceDecoration(vm, p))
293                 return 0;
294
295         content_t c_place = c_decos[pr->range(0, c_decos.size() - 1)];
296
297         s16 height = (deco_height_max > 0) ?
298                 pr->range(deco_height, deco_height_max) : deco_height;
299
300         bool force_placement = (flags & DECO_FORCE_PLACEMENT);
301
302         const v3s16 &em = vm->m_area.getExtent();
303         u32 vi = vm->m_area.index(p);
304         for (int i = 0; i < height; i++) {
305                 vm->m_area.add_y(em, vi, 1);
306
307                 content_t c = vm->m_data[vi].getContent();
308                 if (c != CONTENT_AIR && c != CONTENT_IGNORE &&
309                                 !force_placement)
310                         break;
311
312                 vm->m_data[vi] = MapNode(c_place, 0, deco_param2);
313         }
314
315         return 1;
316 }
317
318
319 int DecoSimple::getHeight()
320 {
321         return (deco_height_max > 0) ? deco_height_max : deco_height;
322 }
323
324
325 ///////////////////////////////////////////////////////////////////////////////
326
327 size_t DecoSchematic::generate(MMVManip *vm, PcgRandom *pr, v3s16 p)
328 {
329         // Schematic could have been unloaded but not the decoration
330         // In this case generate() does nothing (but doesn't *fail*)
331         if (schematic == NULL)
332                 return 0;
333
334         if (!canPlaceDecoration(vm, p))
335                 return 0;
336
337         if (flags & DECO_PLACE_CENTER_X)
338                 p.X -= (schematic->size.X - 1) / 2;
339         if (flags & DECO_PLACE_CENTER_Y)
340                 p.Y -= (schematic->size.Y - 1) / 2;
341         if (flags & DECO_PLACE_CENTER_Z)
342                 p.Z -= (schematic->size.Z - 1) / 2;
343
344         Rotation rot = (rotation == ROTATE_RAND) ?
345                 (Rotation)pr->range(ROTATE_0, ROTATE_270) : rotation;
346
347         bool force_placement = (flags & DECO_FORCE_PLACEMENT);
348
349         schematic->blitToVManip(vm, p, rot, force_placement);
350
351         return 1;
352 }
353
354
355 int DecoSchematic::getHeight()
356 {
357         // Account for a schematic being sunk into the ground by flag.
358         // When placed normally account for how a schematic is placed
359         // sunk 1 node into the ground.
360         return (flags & DECO_PLACE_CENTER_Y) ?
361                 (schematic->size.Y - 1) / 2 : schematic->size.Y - 1;
362 }