Shorten ManualMapVoxelManipulator to MMVManip
[oweals/minetest.git] / src / mg_ore.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_ore.h"
21 #include "mapgen.h"
22 #include "noise.h"
23 #include "util/numeric.h"
24 #include "map.h"
25 #include "log.h"
26
27 const char *OreManager::ELEMENT_TITLE = "ore";
28
29 FlagDesc flagdesc_ore[] = {
30         {"absheight", OREFLAG_ABSHEIGHT},
31         {NULL,        0}
32 };
33
34
35 ///////////////////////////////////////////////////////////////////////////////
36
37
38 OreManager::OreManager(IGameDef *gamedef) :
39         GenElementManager(gamedef)
40 {
41 }
42
43
44 size_t OreManager::placeAllOres(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
45 {
46         size_t nplaced = 0;
47
48         for (size_t i = 0; i != m_elements.size(); i++) {
49                 Ore *ore = (Ore *)m_elements[i];
50                 if (!ore)
51                         continue;
52
53                 nplaced += ore->placeOre(mg, blockseed, nmin, nmax);
54                 blockseed++;
55         }
56
57         return nplaced;
58 }
59
60
61 void OreManager::clear()
62 {
63         for (size_t i = 0; i < m_elements.size(); i++) {
64                 Ore *ore = (Ore *)m_elements[i];
65                 delete ore;
66         }
67         m_elements.clear();
68 }
69
70
71 ///////////////////////////////////////////////////////////////////////////////
72
73
74 Ore::Ore()
75 {
76         flags = 0;
77         noise = NULL;
78 }
79
80
81 Ore::~Ore()
82 {
83         delete noise;
84 }
85
86
87 void Ore::resolveNodeNames(NodeResolveInfo *nri)
88 {
89         m_ndef->getIdFromResolveInfo(nri, "", CONTENT_AIR, c_ore);
90         m_ndef->getIdsFromResolveInfo(nri, c_wherein);
91 }
92
93
94 size_t Ore::placeOre(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
95 {
96         int in_range = 0;
97
98         in_range |= (nmin.Y <= y_max && nmax.Y >= y_min);
99         if (flags & OREFLAG_ABSHEIGHT)
100                 in_range |= (nmin.Y >= -y_max && nmax.Y <= -y_min) << 1;
101         if (!in_range)
102                 return 0;
103
104         int actual_ymin, actual_ymax;
105         if (in_range & ORE_RANGE_MIRROR) {
106                 actual_ymin = MYMAX(nmin.Y, -y_max);
107                 actual_ymax = MYMIN(nmax.Y, -y_min);
108         } else {
109                 actual_ymin = MYMAX(nmin.Y, y_min);
110                 actual_ymax = MYMIN(nmax.Y, y_max);
111         }
112         if (clust_size >= actual_ymax - actual_ymin + 1)
113                 return 0;
114
115         nmin.Y = actual_ymin;
116         nmax.Y = actual_ymax;
117         generate(mg->vm, mg->seed, blockseed, nmin, nmax);
118
119         return 1;
120 }
121
122
123 ///////////////////////////////////////////////////////////////////////////////
124
125
126 void OreScatter::generate(MMVManip *vm, int mapseed, u32 blockseed,
127         v3s16 nmin, v3s16 nmax)
128 {
129         PseudoRandom pr(blockseed);
130         MapNode n_ore(c_ore, 0, ore_param2);
131
132         int volume = (nmax.X - nmin.X + 1) *
133                                  (nmax.Y - nmin.Y + 1) *
134                                  (nmax.Z - nmin.Z + 1);
135         int csize     = clust_size;
136         int orechance = (csize * csize * csize) / clust_num_ores;
137         int nclusters = volume / clust_scarcity;
138
139         for (int i = 0; i != nclusters; i++) {
140                 int x0 = pr.range(nmin.X, nmax.X - csize + 1);
141                 int y0 = pr.range(nmin.Y, nmax.Y - csize + 1);
142                 int z0 = pr.range(nmin.Z, nmax.Z - csize + 1);
143
144                 if ((flags & OREFLAG_USE_NOISE) &&
145                         (NoisePerlin3D(&np, x0, y0, z0, mapseed) < nthresh))
146                         continue;
147
148                 for (int z1 = 0; z1 != csize; z1++)
149                 for (int y1 = 0; y1 != csize; y1++)
150                 for (int x1 = 0; x1 != csize; x1++) {
151                         if (pr.range(1, orechance) != 1)
152                                 continue;
153
154                         u32 i = vm->m_area.index(x0 + x1, y0 + y1, z0 + z1);
155                         if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
156                                 continue;
157
158                         vm->m_data[i] = n_ore;
159                 }
160         }
161 }
162
163
164 ///////////////////////////////////////////////////////////////////////////////
165
166
167 void OreSheet::generate(MMVManip *vm, int mapseed, u32 blockseed,
168         v3s16 nmin, v3s16 nmax)
169 {
170         PseudoRandom pr(blockseed + 4234);
171         MapNode n_ore(c_ore, 0, ore_param2);
172
173         int max_height = clust_size;
174         int y_start = pr.range(nmin.Y, nmax.Y - max_height);
175
176         if (!noise) {
177                 int sx = nmax.X - nmin.X + 1;
178                 int sz = nmax.Z - nmin.Z + 1;
179                 noise = new Noise(&np, 0, sx, sz);
180         }
181         noise->seed = mapseed + y_start;
182         noise->perlinMap2D(nmin.X, nmin.Z);
183
184         size_t index = 0;
185         for (int z = nmin.Z; z <= nmax.Z; z++)
186         for (int x = nmin.X; x <= nmax.X; x++) {
187                 float noiseval = noise->result[index++];
188                 if (noiseval < nthresh)
189                         continue;
190
191                 int height = max_height * (1. / pr.range(1, 3));
192                 int y0 = y_start + np.scale * noiseval; //pr.range(1, 3) - 1;
193                 int y1 = y0 + height;
194                 for (int y = y0; y != y1; y++) {
195                         u32 i = vm->m_area.index(x, y, z);
196                         if (!vm->m_area.contains(i))
197                                 continue;
198                         if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
199                                 continue;
200
201                         vm->m_data[i] = n_ore;
202                 }
203         }
204 }
205
206
207 ///////////////////////////////////////////////////////////////////////////////
208
209 void OreBlob::generate(MMVManip *vm, int mapseed, u32 blockseed,
210         v3s16 nmin, v3s16 nmax)
211 {
212         PseudoRandom pr(blockseed + 2404);
213         MapNode n_ore(c_ore, 0, ore_param2);
214
215         int volume = (nmax.X - nmin.X + 1) *
216                                  (nmax.Y - nmin.Y + 1) *
217                                  (nmax.Z - nmin.Z + 1);
218         int csize  = clust_size;
219         int nblobs = volume / clust_scarcity;
220
221         if (!noise)
222                 noise = new Noise(&np, mapseed, csize, csize, csize);
223
224         for (int i = 0; i != nblobs; i++) {
225                 int x0 = pr.range(nmin.X, nmax.X - csize + 1);
226                 int y0 = pr.range(nmin.Y, nmax.Y - csize + 1);
227                 int z0 = pr.range(nmin.Z, nmax.Z - csize + 1);
228
229                 bool noise_generated = false;
230                 noise->seed = blockseed + i;
231
232                 size_t index = 0;
233                 for (int z1 = 0; z1 != csize; z1++)
234                 for (int y1 = 0; y1 != csize; y1++)
235                 for (int x1 = 0; x1 != csize; x1++, index++) {
236                         u32 i = vm->m_area.index(x0 + x1, y0 + y1, z0 + z1);
237                         if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
238                                 continue;
239
240                         // Lazily generate noise only if there's a chance of ore being placed
241                         // This simple optimization makes calls 6x faster on average
242                         if (!noise_generated) {
243                                 noise_generated = true;
244                                 noise->perlinMap3D(x0, y0, z0);
245                         }
246
247                         float noiseval = noise->result[index];
248
249                         float xdist = x1 - csize / 2;
250                         float ydist = y1 - csize / 2;
251                         float zdist = z1 - csize / 2;
252
253                         noiseval -= (sqrt(xdist * xdist + ydist * ydist + zdist * zdist) / csize);
254
255                         if (noiseval < nthresh)
256                                 continue;
257
258                         vm->m_data[i] = n_ore;
259                 }
260         }
261 }
262
263
264 ///////////////////////////////////////////////////////////////////////////////
265
266 OreVein::~OreVein()
267 {
268         delete noise2;
269 }
270
271
272 void OreVein::generate(MMVManip *vm, int mapseed, u32 blockseed,
273         v3s16 nmin, v3s16 nmax)
274 {
275         PseudoRandom pr(blockseed + 520);
276         MapNode n_ore(c_ore, 0, ore_param2);
277
278         if (!noise) {
279                 int sx = nmax.X - nmin.X + 1;
280                 int sy = nmax.Y - nmin.Y + 1;
281                 int sz = nmax.Z - nmin.Z + 1;
282                 noise  = new Noise(&np, mapseed, sx, sy, sz);
283                 noise2 = new Noise(&np, mapseed + 436, sx, sy, sz);
284         }
285         bool noise_generated = false;
286
287         size_t index = 0;
288         for (int z = nmin.Z; z <= nmax.Z; z++)
289         for (int y = nmin.Y; y <= nmax.Y; y++)
290         for (int x = nmin.X; x <= nmax.X; x++, index++) {
291                 u32 i = vm->m_area.index(x, y, z);
292                 if (!vm->m_area.contains(i))
293                         continue;
294                 if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
295                         continue;
296
297                 // Same lazy generation optimization as in OreBlob
298                 if (!noise_generated) {
299                         noise_generated = true;
300                         noise->perlinMap3D(nmin.X, nmin.Y, nmin.Z);
301                         noise2->perlinMap3D(nmin.X, nmin.Y, nmin.Z);
302                 }
303
304                 // randval ranges from -1..1
305                 float randval   = (float)pr.next() / (PSEUDORANDOM_MAX / 2) - 1.f;
306                 float noiseval  = contour(noise->result[index]);
307                 float noiseval2 = contour(noise2->result[index]);
308                 if (noiseval * noiseval2 + randval * random_factor < nthresh)
309                         continue;
310
311                 vm->m_data[i] = n_ore;
312         }
313 }