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