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