]> git.lizzy.rs Git - minetest.git/blob - src/mg_decoration.cpp
Settings: Make setting entry group and values mutually exclusive
[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_schematic[] = {
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 size_t DecorationManager::placeAllDecos(Mapgen *mg, u32 seed, v3s16 nmin, v3s16 nmax)
42 {
43         size_t nplaced = 0;
44
45         for (size_t i = 0; i != m_elements.size(); i++) {
46                 Decoration *deco = (Decoration *)m_elements[i];
47                 if (!deco)
48                         continue;
49
50                 nplaced += deco->placeDeco(mg, seed, nmin, nmax);
51                 seed++;
52         }
53
54         return nplaced;
55 }
56
57
58 ///////////////////////////////////////////////////////////////////////////////
59
60
61 Decoration::Decoration()
62 {
63         mapseed    = 0;
64         np         = NULL;
65         fill_ratio = 0;
66         sidelen    = 1;
67 }
68
69
70 Decoration::~Decoration()
71 {
72         delete np;
73 }
74
75
76 size_t Decoration::placeDeco(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
77 {
78         PseudoRandom ps(blockseed + 53);
79         int carea_size = nmax.X - nmin.X + 1;
80
81         // Divide area into parts
82         if (carea_size % sidelen) {
83                 errorstream << "Decoration::placeDeco: chunk size is not divisible by "
84                         "sidelen; setting sidelen to " << carea_size << std::endl;
85                 sidelen = carea_size;
86         }
87
88         s16 divlen = carea_size / sidelen;
89         int area = sidelen * sidelen;
90
91         for (s16 z0 = 0; z0 < divlen; z0++)
92         for (s16 x0 = 0; x0 < divlen; x0++) {
93                 v2s16 p2d_center( // Center position of part of division
94                         nmin.X + sidelen / 2 + sidelen * x0,
95                         nmin.Z + sidelen / 2 + sidelen * z0
96                 );
97                 v2s16 p2d_min( // Minimum edge of part of division
98                         nmin.X + sidelen * x0,
99                         nmin.Z + sidelen * z0
100                 );
101                 v2s16 p2d_max( // Maximum edge of part of division
102                         nmin.X + sidelen + sidelen * x0 - 1,
103                         nmin.Z + sidelen + sidelen * z0 - 1
104                 );
105
106                 // Amount of decorations
107                 float nval = np ?
108                         NoisePerlin2D(np, p2d_center.X, p2d_center.Y, mapseed) :
109                         fill_ratio;
110                 u32 deco_count = area * MYMAX(nval, 0.f);
111
112                 for (u32 i = 0; i < deco_count; i++) {
113                         s16 x = ps.range(p2d_min.X, p2d_max.X);
114                         s16 z = ps.range(p2d_min.Y, p2d_max.Y);
115
116                         int mapindex = carea_size * (z - nmin.Z) + (x - nmin.X);
117
118                         s16 y = mg->heightmap ?
119                                         mg->heightmap[mapindex] :
120                                         mg->findGroundLevel(v2s16(x, z), nmin.Y, nmax.Y);
121
122                         if (y < nmin.Y || y > nmax.Y)
123                                 continue;
124
125                         int height = getHeight();
126                         int max_y = nmax.Y;// + MAP_BLOCKSIZE - 1;
127                         if (y + 1 + height > max_y) {
128                                 continue;
129 #if 0
130                                 printf("Decoration at (%d %d %d) cut off\n", x, y, z);
131                                 //add to queue
132                                 JMutexAutoLock cutofflock(cutoff_mutex);
133                                 cutoffs.push_back(CutoffData(x, y, z, height));
134 #endif
135                         }
136
137                         if (mg->biomemap) {
138                                 std::set<u8>::iterator iter;
139
140                                 if (biomes.size()) {
141                                         iter = biomes.find(mg->biomemap[mapindex]);
142                                         if (iter == biomes.end())
143                                                 continue;
144                                 }
145                         }
146
147                         v3s16 pos(x, y, z);
148                         if (generate(mg, &ps, max_y, pos))
149                                 mg->gennotify.addEvent(GENNOTIFY_DECORATION, pos, id);
150                 }
151         }
152
153         return 0;
154 }
155
156
157 #if 0
158 void Decoration::placeCutoffs(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
159 {
160         PseudoRandom pr(blockseed + 53);
161         std::vector<CutoffData> handled_cutoffs;
162
163         // Copy over the cutoffs we're interested in so we don't needlessly hold a lock
164         {
165                 JMutexAutoLock cutofflock(cutoff_mutex);
166                 for (std::list<CutoffData>::iterator i = cutoffs.begin();
167                         i != cutoffs.end(); ++i) {
168                         CutoffData cutoff = *i;
169                         v3s16 p    = cutoff.p;
170                         s16 height = cutoff.height;
171                         if (p.X < nmin.X || p.X > nmax.X ||
172                                 p.Z < nmin.Z || p.Z > nmax.Z)
173                                 continue;
174                         if (p.Y + height < nmin.Y || p.Y > nmax.Y)
175                                 continue;
176
177                         handled_cutoffs.push_back(cutoff);
178                 }
179         }
180
181         // Generate the cutoffs
182         for (size_t i = 0; i != handled_cutoffs.size(); i++) {
183                 v3s16 p    = handled_cutoffs[i].p;
184                 s16 height = handled_cutoffs[i].height;
185
186                 if (p.Y + height > nmax.Y) {
187                         //printf("Decoration at (%d %d %d) cut off again!\n", p.X, p.Y, p.Z);
188                         cuttoffs.push_back(v3s16(p.X, p.Y, p.Z));
189                 }
190
191                 generate(mg, &pr, nmax.Y, nmin.Y - p.Y, v3s16(p.X, nmin.Y, p.Z));
192         }
193
194         // Remove cutoffs that were handled from the cutoff list
195         {
196                 JMutexAutoLock cutofflock(cutoff_mutex);
197                 for (std::list<CutoffData>::iterator i = cutoffs.begin();
198                         i != cutoffs.end(); ++i) {
199
200                         for (size_t j = 0; j != handled_cutoffs.size(); j++) {
201                                 CutoffData coff = *i;
202                                 if (coff.p == handled_cutoffs[j].p)
203                                         i = cutoffs.erase(i);
204                         }
205                 }
206         }
207 }
208 #endif
209
210
211 ///////////////////////////////////////////////////////////////////////////////
212
213
214 bool DecoSimple::canPlaceDecoration(ManualMapVoxelManipulator *vm, v3s16 p)
215 {
216         // Don't bother if there aren't any decorations to place
217         if (c_decos.size() == 0)
218                 return false;
219
220         u32 vi = vm->m_area.index(p);
221
222         // Check if the decoration can be placed on this node
223         if (!CONTAINS(c_place_on, vm->m_data[vi].getContent()))
224                 return false;
225
226         // Don't continue if there are no spawnby constraints
227         if (nspawnby == -1)
228                 return true;
229
230         int nneighs = 0;
231         v3s16 dirs[8] = {
232                 v3s16( 0, 0,  1),
233                 v3s16( 0, 0, -1),
234                 v3s16( 1, 0,  0),
235                 v3s16(-1, 0,  0),
236                 v3s16( 1, 0,  1),
237                 v3s16(-1, 0,  1),
238                 v3s16(-1, 0, -1),
239                 v3s16( 1, 0, -1)
240         };
241
242         // Check a Moore neighborhood if there are enough spawnby nodes
243         for (size_t i = 0; i != ARRLEN(dirs); i++) {
244                 u32 index = vm->m_area.index(p + dirs[i]);
245                 if (!vm->m_area.contains(index))
246                         continue;
247
248                 if (CONTAINS(c_spawnby, vm->m_data[index].getContent()))
249                         nneighs++;
250         }
251
252         if (nneighs < nspawnby)
253                 return false;
254
255         return true;
256 }
257
258
259 size_t DecoSimple::generate(Mapgen *mg, PseudoRandom *pr, s16 max_y, v3s16 p)
260 {
261         ManualMapVoxelManipulator *vm = mg->vm;
262
263         if (!canPlaceDecoration(vm, p))
264                 return 0;
265
266         content_t c_place = c_decos[pr->range(0, c_decos.size() - 1)];
267
268         s16 height = (deco_height_max > 0) ?
269                 pr->range(deco_height, deco_height_max) : deco_height;
270
271         height = MYMIN(height, max_y - p.Y);
272
273         v3s16 em = vm->m_area.getExtent();
274         u32 vi = vm->m_area.index(p);
275         for (int i = 0; i < height; i++) {
276                 vm->m_area.add_y(em, vi, 1);
277
278                 content_t c = vm->m_data[vi].getContent();
279                 if (c != CONTENT_AIR && c != CONTENT_IGNORE)
280                         break;
281
282                 vm->m_data[vi] = MapNode(c_place);
283         }
284
285         return 1;
286 }
287
288
289 int DecoSimple::getHeight()
290 {
291         return (deco_height_max > 0) ? deco_height_max : deco_height;
292 }
293
294
295 ///////////////////////////////////////////////////////////////////////////////
296
297
298 size_t DecoSchematic::generate(Mapgen *mg, PseudoRandom *pr, s16 max_y, v3s16 p)
299 {
300         ManualMapVoxelManipulator *vm = mg->vm;
301
302         if (flags & DECO_PLACE_CENTER_X)
303                 p.X -= (schematic->size.X + 1) / 2;
304         if (flags & DECO_PLACE_CENTER_Y)
305                 p.Y -= (schematic->size.Y + 1) / 2;
306         if (flags & DECO_PLACE_CENTER_Z)
307                 p.Z -= (schematic->size.Z + 1) / 2;
308
309         u32 vi = vm->m_area.index(p);
310         content_t c = vm->m_data[vi].getContent();
311         if (!CONTAINS(c_place_on, c))
312                 return 0;
313
314         Rotation rot = (rotation == ROTATE_RAND) ?
315                 (Rotation)pr->range(ROTATE_0, ROTATE_270) : rotation;
316
317         schematic->blitToVManip(p, vm, rot, false, mg->ndef);
318
319         return 1;
320 }
321
322
323 int DecoSchematic::getHeight()
324 {
325         return schematic->size.Y;
326 }