Split up mapgen.cpp
[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 "map.h"
24 #include "log.h"
25 #include "util/numeric.h"
26
27 ///////////////////////////////////////////////////////////////////////////////
28
29
30 Decoration *createDecoration(DecorationType type)
31 {
32         switch (type) {
33         case DECO_SIMPLE:
34                 return new DecoSimple;
35         case DECO_SCHEMATIC:
36                 return new DecoSchematic;
37         //case DECO_LSYSTEM:
38         //      return new DecoLSystem;
39         default:
40                 return NULL;
41         }
42 }
43
44
45 Decoration::Decoration()
46 {
47         mapseed    = 0;
48         np         = NULL;
49         fill_ratio = 0;
50         sidelen    = 1;
51 }
52
53
54 Decoration::~Decoration()
55 {
56         delete np;
57 }
58
59
60 void Decoration::placeDeco(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
61 {
62         PseudoRandom ps(blockseed + 53);
63         int carea_size = nmax.X - nmin.X + 1;
64
65         // Divide area into parts
66         if (carea_size % sidelen) {
67                 errorstream << "Decoration::placeDeco: chunk size is not divisible by "
68                         "sidelen; setting sidelen to " << carea_size << std::endl;
69                 sidelen = carea_size;
70         }
71
72         s16 divlen = carea_size / sidelen;
73         int area = sidelen * sidelen;
74
75         for (s16 z0 = 0; z0 < divlen; z0++)
76         for (s16 x0 = 0; x0 < divlen; x0++) {
77                 v2s16 p2d_center( // Center position of part of division
78                         nmin.X + sidelen / 2 + sidelen * x0,
79                         nmin.Z + sidelen / 2 + sidelen * z0
80                 );
81                 v2s16 p2d_min( // Minimum edge of part of division
82                         nmin.X + sidelen * x0,
83                         nmin.Z + sidelen * z0
84                 );
85                 v2s16 p2d_max( // Maximum edge of part of division
86                         nmin.X + sidelen + sidelen * x0 - 1,
87                         nmin.Z + sidelen + sidelen * z0 - 1
88                 );
89
90                 // Amount of decorations
91                 float nval = np ?
92                         NoisePerlin2D(np, p2d_center.X, p2d_center.Y, mapseed) :
93                         fill_ratio;
94                 u32 deco_count = area * MYMAX(nval, 0.f);
95
96                 for (u32 i = 0; i < deco_count; i++) {
97                         s16 x = ps.range(p2d_min.X, p2d_max.X);
98                         s16 z = ps.range(p2d_min.Y, p2d_max.Y);
99
100                         int mapindex = carea_size * (z - nmin.Z) + (x - nmin.X);
101
102                         s16 y = mg->heightmap ?
103                                         mg->heightmap[mapindex] :
104                                         mg->findGroundLevel(v2s16(x, z), nmin.Y, nmax.Y);
105
106                         if (y < nmin.Y || y > nmax.Y)
107                                 continue;
108
109                         int height = getHeight();
110                         int max_y = nmax.Y;// + MAP_BLOCKSIZE - 1;
111                         if (y + 1 + height > max_y) {
112                                 continue;
113 #if 0
114                                 printf("Decoration at (%d %d %d) cut off\n", x, y, z);
115                                 //add to queue
116                                 JMutexAutoLock cutofflock(cutoff_mutex);
117                                 cutoffs.push_back(CutoffData(x, y, z, height));
118 #endif
119                         }
120
121                         if (mg->biomemap) {
122                                 std::set<u8>::iterator iter;
123
124                                 if (biomes.size()) {
125                                         iter = biomes.find(mg->biomemap[mapindex]);
126                                         if (iter == biomes.end())
127                                                 continue;
128                                 }
129                         }
130
131                         generate(mg, &ps, max_y, v3s16(x, y, z));
132                 }
133         }
134 }
135
136
137 #if 0
138 void Decoration::placeCutoffs(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
139 {
140         PseudoRandom pr(blockseed + 53);
141         std::vector<CutoffData> handled_cutoffs;
142
143         // Copy over the cutoffs we're interested in so we don't needlessly hold a lock
144         {
145                 JMutexAutoLock cutofflock(cutoff_mutex);
146                 for (std::list<CutoffData>::iterator i = cutoffs.begin();
147                         i != cutoffs.end(); ++i) {
148                         CutoffData cutoff = *i;
149                         v3s16 p    = cutoff.p;
150                         s16 height = cutoff.height;
151                         if (p.X < nmin.X || p.X > nmax.X ||
152                                 p.Z < nmin.Z || p.Z > nmax.Z)
153                                 continue;
154                         if (p.Y + height < nmin.Y || p.Y > nmax.Y)
155                                 continue;
156
157                         handled_cutoffs.push_back(cutoff);
158                 }
159         }
160
161         // Generate the cutoffs
162         for (size_t i = 0; i != handled_cutoffs.size(); i++) {
163                 v3s16 p    = handled_cutoffs[i].p;
164                 s16 height = handled_cutoffs[i].height;
165
166                 if (p.Y + height > nmax.Y) {
167                         //printf("Decoration at (%d %d %d) cut off again!\n", p.X, p.Y, p.Z);
168                         cuttoffs.push_back(v3s16(p.X, p.Y, p.Z));
169                 }
170
171                 generate(mg, &pr, nmax.Y, nmin.Y - p.Y, v3s16(p.X, nmin.Y, p.Z));
172         }
173
174         // Remove cutoffs that were handled from the cutoff list
175         {
176                 JMutexAutoLock cutofflock(cutoff_mutex);
177                 for (std::list<CutoffData>::iterator i = cutoffs.begin();
178                         i != cutoffs.end(); ++i) {
179
180                         for (size_t j = 0; j != handled_cutoffs.size(); j++) {
181                                 CutoffData coff = *i;
182                                 if (coff.p == handled_cutoffs[j].p)
183                                         i = cutoffs.erase(i);
184                         }
185                 }
186         }
187 }
188 #endif
189
190
191 ///////////////////////////////////////////////////////////////////////////////
192
193 bool DecoSimple::canPlaceDecoration(ManualMapVoxelManipulator *vm, v3s16 p)
194 {
195         // Don't bother if there aren't any decorations to place
196         if (c_decos.size() == 0)
197                 return false;
198
199         u32 vi = vm->m_area.index(p);
200
201         // Check if the decoration can be placed on this node
202         if (!CONTAINS(c_place_on, vm->m_data[vi].getContent()))
203                 return false;
204
205         // Don't continue if there are no spawnby constraints
206         if (nspawnby == -1)
207                 return true;
208
209         int nneighs = 0;
210         v3s16 dirs[8] = {
211                 v3s16( 0, 0,  1),
212                 v3s16( 0, 0, -1),
213                 v3s16( 1, 0,  0),
214                 v3s16(-1, 0,  0),
215                 v3s16( 1, 0,  1),
216                 v3s16(-1, 0,  1),
217                 v3s16(-1, 0, -1),
218                 v3s16( 1, 0, -1)
219         };
220
221         // Check a Moore neighborhood if there are enough spawnby nodes
222         for (size_t i = 0; i != ARRLEN(dirs); i++) {
223                 u32 index = vm->m_area.index(p + dirs[i]);
224                 if (!vm->m_area.contains(index))
225                         continue;
226
227                 if (CONTAINS(c_spawnby, vm->m_data[index].getContent()))
228                         nneighs++;
229         }
230
231         if (nneighs < nspawnby)
232                 return false;
233
234         return true;
235 }
236
237
238 void DecoSimple::generate(Mapgen *mg, PseudoRandom *pr, s16 max_y, v3s16 p)
239 {
240         ManualMapVoxelManipulator *vm = mg->vm;
241
242         if (!canPlaceDecoration(vm, p))
243                 return;
244
245         content_t c_place = c_decos[pr->range(0, c_decos.size() - 1)];
246
247         s16 height = (deco_height_max > 0) ?
248                 pr->range(deco_height, deco_height_max) : deco_height;
249
250         height = MYMIN(height, max_y - p.Y);
251
252         v3s16 em = vm->m_area.getExtent();
253         u32 vi = vm->m_area.index(p);
254         for (int i = 0; i < height; i++) {
255                 vm->m_area.add_y(em, vi, 1);
256
257                 content_t c = vm->m_data[vi].getContent();
258                 if (c != CONTENT_AIR && c != CONTENT_IGNORE)
259                         break;
260
261                 vm->m_data[vi] = MapNode(c_place);
262         }
263 }
264
265
266 int DecoSimple::getHeight()
267 {
268         return (deco_height_max > 0) ? deco_height_max : deco_height;
269 }
270
271
272 std::string DecoSimple::getName()
273 {
274         return "";
275 }