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