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