]> git.lizzy.rs Git - minetest.git/blob - src/mg_decoration.cpp
Make limiting of the reflow liquids queue size optional
[minetest.git] / src / mg_decoration.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2014 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "mg_decoration.h"
21 #include "mg_schematic.h"
22 #include "mapgen.h"
23 #include "noise.h"
24 #include "map.h"
25 #include "log.h"
26 #include "util/numeric.h"
27
28 const char *DecorationManager::ELEMENT_TITLE = "decoration";
29
30 FlagDesc flagdesc_deco[] = {
31         {"place_center_x", DECO_PLACE_CENTER_X},
32         {"place_center_y", DECO_PLACE_CENTER_Y},
33         {"place_center_z", DECO_PLACE_CENTER_Z},
34         {NULL,             0}
35 };
36
37
38 ///////////////////////////////////////////////////////////////////////////////
39
40
41 DecorationManager::DecorationManager(IGameDef *gamedef) :
42         GenElementManager(gamedef)
43 {
44 }
45
46
47 size_t DecorationManager::placeAllDecos(Mapgen *mg, u32 seed, v3s16 nmin, v3s16 nmax)
48 {
49         size_t nplaced = 0;
50
51         for (size_t i = 0; i != m_elements.size(); i++) {
52                 Decoration *deco = (Decoration *)m_elements[i];
53                 if (!deco)
54                         continue;
55
56                 nplaced += deco->placeDeco(mg, seed, nmin, nmax);
57                 seed++;
58         }
59
60         return nplaced;
61 }
62
63
64 void DecorationManager::clear()
65 {
66         for (size_t i = 0; i < m_elements.size(); i++) {
67                 Decoration *deco = (Decoration *)m_elements[i];
68                 if (!deco)
69                         continue;
70
71                 deco->dropResolverEntries(m_resolver);
72         }
73         m_elements.clear();
74 }
75
76
77 ///////////////////////////////////////////////////////////////////////////////
78
79
80 Decoration::Decoration()
81 {
82         mapseed    = 0;
83         fill_ratio = 0;
84         sidelen    = 1;
85         flags      = 0;
86 }
87
88
89 Decoration::~Decoration()
90 {
91 }
92
93
94 size_t Decoration::placeDeco(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
95 {
96         PseudoRandom ps(blockseed + 53);
97         int carea_size = nmax.X - nmin.X + 1;
98
99         // Divide area into parts
100         if (carea_size % sidelen) {
101                 errorstream << "Decoration::placeDeco: chunk size is not divisible by "
102                         "sidelen; setting sidelen to " << carea_size << std::endl;
103                 sidelen = carea_size;
104         }
105
106         s16 divlen = carea_size / sidelen;
107         int area = sidelen * sidelen;
108
109         for (s16 z0 = 0; z0 < divlen; z0++)
110         for (s16 x0 = 0; x0 < divlen; x0++) {
111                 v2s16 p2d_center( // Center position of part of division
112                         nmin.X + sidelen / 2 + sidelen * x0,
113                         nmin.Z + sidelen / 2 + sidelen * z0
114                 );
115                 v2s16 p2d_min( // Minimum edge of part of division
116                         nmin.X + sidelen * x0,
117                         nmin.Z + sidelen * z0
118                 );
119                 v2s16 p2d_max( // Maximum edge of part of division
120                         nmin.X + sidelen + sidelen * x0 - 1,
121                         nmin.Z + sidelen + sidelen * z0 - 1
122                 );
123
124                 // Amount of decorations
125                 float nval = (flags & DECO_USE_NOISE) ?
126                         NoisePerlin2D(&np, p2d_center.X, p2d_center.Y, mapseed) :
127                         fill_ratio;
128                 u32 deco_count = area * MYMAX(nval, 0.f);
129
130                 for (u32 i = 0; i < deco_count; i++) {
131                         s16 x = ps.range(p2d_min.X, p2d_max.X);
132                         s16 z = ps.range(p2d_min.Y, p2d_max.Y);
133
134                         int mapindex = carea_size * (z - nmin.Z) + (x - nmin.X);
135
136                         s16 y = mg->heightmap ?
137                                         mg->heightmap[mapindex] :
138                                         mg->findGroundLevel(v2s16(x, z), nmin.Y, nmax.Y);
139
140                         if (y < nmin.Y || y > nmax.Y)
141                                 continue;
142
143                         int height = getHeight();
144                         int max_y = nmax.Y;// + MAP_BLOCKSIZE - 1;
145                         if (y + 1 + height > max_y) {
146                                 continue;
147 #if 0
148                                 printf("Decoration at (%d %d %d) cut off\n", x, y, z);
149                                 //add to queue
150                                 JMutexAutoLock cutofflock(cutoff_mutex);
151                                 cutoffs.push_back(CutoffData(x, y, z, height));
152 #endif
153                         }
154
155                         if (mg->biomemap) {
156                                 std::set<u8>::iterator iter;
157
158                                 if (!biomes.empty()) {
159                                         iter = biomes.find(mg->biomemap[mapindex]);
160                                         if (iter == biomes.end())
161                                                 continue;
162                                 }
163                         }
164
165                         v3s16 pos(x, y, z);
166                         if (generate(mg, &ps, max_y, pos))
167                                 mg->gennotify.addEvent(GENNOTIFY_DECORATION, pos, id);
168                 }
169         }
170
171         return 0;
172 }
173
174
175 #if 0
176 void Decoration::placeCutoffs(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
177 {
178         PseudoRandom pr(blockseed + 53);
179         std::vector<CutoffData> handled_cutoffs;
180
181         // Copy over the cutoffs we're interested in so we don't needlessly hold a lock
182         {
183                 JMutexAutoLock cutofflock(cutoff_mutex);
184                 for (std::list<CutoffData>::iterator i = cutoffs.begin();
185                         i != cutoffs.end(); ++i) {
186                         CutoffData cutoff = *i;
187                         v3s16 p    = cutoff.p;
188                         s16 height = cutoff.height;
189                         if (p.X < nmin.X || p.X > nmax.X ||
190                                 p.Z < nmin.Z || p.Z > nmax.Z)
191                                 continue;
192                         if (p.Y + height < nmin.Y || p.Y > nmax.Y)
193                                 continue;
194
195                         handled_cutoffs.push_back(cutoff);
196                 }
197         }
198
199         // Generate the cutoffs
200         for (size_t i = 0; i != handled_cutoffs.size(); i++) {
201                 v3s16 p    = handled_cutoffs[i].p;
202                 s16 height = handled_cutoffs[i].height;
203
204                 if (p.Y + height > nmax.Y) {
205                         //printf("Decoration at (%d %d %d) cut off again!\n", p.X, p.Y, p.Z);
206                         cuttoffs.push_back(v3s16(p.X, p.Y, p.Z));
207                 }
208
209                 generate(mg, &pr, nmax.Y, nmin.Y - p.Y, v3s16(p.X, nmin.Y, p.Z));
210         }
211
212         // Remove cutoffs that were handled from the cutoff list
213         {
214                 JMutexAutoLock cutofflock(cutoff_mutex);
215                 for (std::list<CutoffData>::iterator i = cutoffs.begin();
216                         i != cutoffs.end(); ++i) {
217
218                         for (size_t j = 0; j != handled_cutoffs.size(); j++) {
219                                 CutoffData coff = *i;
220                                 if (coff.p == handled_cutoffs[j].p)
221                                         i = cutoffs.erase(i);
222                         }
223                 }
224         }
225 }
226 #endif
227
228
229 ///////////////////////////////////////////////////////////////////////////////
230
231
232 bool DecoSimple::canPlaceDecoration(ManualMapVoxelManipulator *vm, v3s16 p)
233 {
234         // Don't bother if there aren't any decorations to place
235         if (c_decos.size() == 0)
236                 return false;
237
238         u32 vi = vm->m_area.index(p);
239
240         // Check if the decoration can be placed on this node
241         if (!CONTAINS(c_place_on, vm->m_data[vi].getContent()))
242                 return false;
243
244         // Don't continue if there are no spawnby constraints
245         if (nspawnby == -1)
246                 return true;
247
248         int nneighs = 0;
249         v3s16 dirs[8] = {
250                 v3s16( 0, 0,  1),
251                 v3s16( 0, 0, -1),
252                 v3s16( 1, 0,  0),
253                 v3s16(-1, 0,  0),
254                 v3s16( 1, 0,  1),
255                 v3s16(-1, 0,  1),
256                 v3s16(-1, 0, -1),
257                 v3s16( 1, 0, -1)
258         };
259
260         // Check a Moore neighborhood if there are enough spawnby nodes
261         for (size_t i = 0; i != ARRLEN(dirs); i++) {
262                 u32 index = vm->m_area.index(p + dirs[i]);
263                 if (!vm->m_area.contains(index))
264                         continue;
265
266                 if (CONTAINS(c_spawnby, vm->m_data[index].getContent()))
267                         nneighs++;
268         }
269
270         if (nneighs < nspawnby)
271                 return false;
272
273         return true;
274 }
275
276
277 size_t DecoSimple::generate(Mapgen *mg, PseudoRandom *pr, s16 max_y, v3s16 p)
278 {
279         ManualMapVoxelManipulator *vm = mg->vm;
280
281         if (!canPlaceDecoration(vm, p))
282                 return 0;
283
284         content_t c_place = c_decos[pr->range(0, c_decos.size() - 1)];
285
286         s16 height = (deco_height_max > 0) ?
287                 pr->range(deco_height, deco_height_max) : deco_height;
288
289         height = MYMIN(height, max_y - p.Y);
290
291         v3s16 em = vm->m_area.getExtent();
292         u32 vi = vm->m_area.index(p);
293         for (int i = 0; i < height; i++) {
294                 vm->m_area.add_y(em, vi, 1);
295
296                 content_t c = vm->m_data[vi].getContent();
297                 if (c != CONTENT_AIR && c != CONTENT_IGNORE)
298                         break;
299
300                 vm->m_data[vi] = MapNode(c_place);
301         }
302
303         return 1;
304 }
305
306
307 int DecoSimple::getHeight()
308 {
309         return (deco_height_max > 0) ? deco_height_max : deco_height;
310 }
311
312
313 void DecoSimple::dropResolverEntries(NodeResolver *resolver)
314 {
315         resolver->cancelNodeList(&c_decos);
316         resolver->cancelNodeList(&c_spawnby);
317 }
318
319
320 ///////////////////////////////////////////////////////////////////////////////
321
322
323 size_t DecoSchematic::generate(Mapgen *mg, PseudoRandom *pr, s16 max_y, v3s16 p)
324 {
325         ManualMapVoxelManipulator *vm = mg->vm;
326
327         if (flags & DECO_PLACE_CENTER_X)
328                 p.X -= (schematic->size.X + 1) / 2;
329         if (flags & DECO_PLACE_CENTER_Y)
330                 p.Y -= (schematic->size.Y + 1) / 2;
331         if (flags & DECO_PLACE_CENTER_Z)
332                 p.Z -= (schematic->size.Z + 1) / 2;
333
334         u32 vi = vm->m_area.index(p);
335         content_t c = vm->m_data[vi].getContent();
336         if (!CONTAINS(c_place_on, c))
337                 return 0;
338
339         Rotation rot = (rotation == ROTATE_RAND) ?
340                 (Rotation)pr->range(ROTATE_0, ROTATE_270) : rotation;
341
342         schematic->blitToVManip(p, vm, rot, false, mg->ndef);
343
344         return 1;
345 }
346
347
348 int DecoSchematic::getHeight()
349 {
350         return schematic->size.Y;
351 }