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