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