3 Copyright (C) 2010-2014 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
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.
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.
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.
23 #include "util/numeric.h"
27 FlagDesc flagdesc_ore[] = {
28 {"absheight", OREFLAG_ABSHEIGHT},
29 {"puff_cliffs", OREFLAG_PUFF_CLIFFS},
30 {"puff_additive_composition", OREFLAG_PUFF_ADDITIVE},
35 ///////////////////////////////////////////////////////////////////////////////
38 OreManager::OreManager(IGameDef *gamedef) :
39 ObjDefManager(gamedef, OBJDEF_ORE)
44 size_t OreManager::placeAllOres(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
48 for (size_t i = 0; i != m_objects.size(); i++) {
49 Ore *ore = (Ore *)m_objects[i];
53 nplaced += ore->placeOre(mg, blockseed, nmin, nmax);
61 void OreManager::clear()
63 for (size_t i = 0; i < m_objects.size(); i++) {
64 Ore *ore = (Ore *)m_objects[i];
71 ///////////////////////////////////////////////////////////////////////////////
87 void Ore::resolveNodeNames()
89 getIdFromNrBacklog(&c_ore, "", CONTENT_AIR);
90 getIdsFromNrBacklog(&c_wherein);
94 size_t Ore::placeOre(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
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;
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);
109 actual_ymin = MYMAX(nmin.Y, y_min);
110 actual_ymax = MYMIN(nmax.Y, y_max);
112 if (clust_size >= actual_ymax - actual_ymin + 1)
115 nmin.Y = actual_ymin;
116 nmax.Y = actual_ymax;
117 generate(mg->vm, mg->seed, blockseed, nmin, nmax, mg->biomemap);
123 ///////////////////////////////////////////////////////////////////////////////
126 void OreScatter::generate(MMVManip *vm, int mapseed, u32 blockseed,
127 v3s16 nmin, v3s16 nmax, u8 *biomemap)
129 PcgRandom pr(blockseed);
130 MapNode n_ore(c_ore, 0, ore_param2);
132 u32 sizex = (nmax.X - nmin.X + 1);
133 u32 volume = (nmax.X - nmin.X + 1) *
134 (nmax.Y - nmin.Y + 1) *
135 (nmax.Z - nmin.Z + 1);
136 u32 csize = clust_size;
137 u32 cvolume = csize * csize * csize;
138 u32 nclusters = volume / clust_scarcity;
140 for (u32 i = 0; i != nclusters; i++) {
141 int x0 = pr.range(nmin.X, nmax.X - csize + 1);
142 int y0 = pr.range(nmin.Y, nmax.Y - csize + 1);
143 int z0 = pr.range(nmin.Z, nmax.Z - csize + 1);
145 if ((flags & OREFLAG_USE_NOISE) &&
146 (NoisePerlin3D(&np, x0, y0, z0, mapseed) < nthresh))
149 if (biomemap && !biomes.empty()) {
150 u32 index = sizex * (z0 - nmin.Z) + (x0 - nmin.X);
151 std::set<u8>::iterator it = biomes.find(biomemap[index]);
152 if (it == biomes.end())
156 for (u32 z1 = 0; z1 != csize; z1++)
157 for (u32 y1 = 0; y1 != csize; y1++)
158 for (u32 x1 = 0; x1 != csize; x1++) {
159 if (pr.range(1, cvolume) > clust_num_ores)
162 u32 i = vm->m_area.index(x0 + x1, y0 + y1, z0 + z1);
163 if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
166 vm->m_data[i] = n_ore;
172 ///////////////////////////////////////////////////////////////////////////////
175 void OreSheet::generate(MMVManip *vm, int mapseed, u32 blockseed,
176 v3s16 nmin, v3s16 nmax, u8 *biomemap)
178 PcgRandom pr(blockseed + 4234);
179 MapNode n_ore(c_ore, 0, ore_param2);
181 u16 max_height = column_height_max;
182 int y_start_min = nmin.Y + max_height;
183 int y_start_max = nmax.Y - max_height;
185 int y_start = y_start_min < y_start_max ?
186 pr.range(y_start_min, y_start_max) :
187 (y_start_min + y_start_max) / 2;
190 int sx = nmax.X - nmin.X + 1;
191 int sz = nmax.Z - nmin.Z + 1;
192 noise = new Noise(&np, 0, sx, sz);
194 noise->seed = mapseed + y_start;
195 noise->perlinMap2D(nmin.X, nmin.Z);
198 for (int z = nmin.Z; z <= nmax.Z; z++)
199 for (int x = nmin.X; x <= nmax.X; x++, index++) {
200 float noiseval = noise->result[index];
201 if (noiseval < nthresh)
204 if (biomemap && !biomes.empty()) {
205 std::set<u8>::iterator it = biomes.find(biomemap[index]);
206 if (it == biomes.end())
210 u16 height = pr.range(column_height_min, column_height_max);
211 int ymidpoint = y_start + noiseval;
212 int y0 = MYMAX(nmin.Y, ymidpoint - height * (1 - column_midpoint_factor));
213 int y1 = MYMIN(nmax.Y, y0 + height - 1);
215 for (int y = y0; y <= y1; y++) {
216 u32 i = vm->m_area.index(x, y, z);
217 if (!vm->m_area.contains(i))
219 if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
222 vm->m_data[i] = n_ore;
228 ///////////////////////////////////////////////////////////////////////////////
233 noise_puff_top = NULL;
234 noise_puff_bottom = NULL;
240 delete noise_puff_top;
241 delete noise_puff_bottom;
245 void OrePuff::generate(MMVManip *vm, int mapseed, u32 blockseed,
246 v3s16 nmin, v3s16 nmax, u8 *biomemap)
248 PcgRandom pr(blockseed + 4234);
249 MapNode n_ore(c_ore, 0, ore_param2);
251 int y_start = pr.range(nmin.Y, nmax.Y);
254 int sx = nmax.X - nmin.X + 1;
255 int sz = nmax.Z - nmin.Z + 1;
256 noise = new Noise(&np, 0, sx, sz);
257 noise_puff_top = new Noise(&np_puff_top, 0, sx, sz);
258 noise_puff_bottom = new Noise(&np_puff_bottom, 0, sx, sz);
261 noise->seed = mapseed + y_start;
262 noise->perlinMap2D(nmin.X, nmin.Z);
263 bool noise_generated = false;
266 for (int z = nmin.Z; z <= nmax.Z; z++)
267 for (int x = nmin.X; x <= nmax.X; x++, index++) {
268 float noiseval = noise->result[index];
269 if (noiseval < nthresh)
272 if (biomemap && !biomes.empty()) {
273 std::set<u8>::iterator it = biomes.find(biomemap[index]);
274 if (it == biomes.end())
278 if (!noise_generated) {
279 noise_generated = true;
280 noise_puff_top->perlinMap2D(nmin.X, nmin.Z);
281 noise_puff_bottom->perlinMap2D(nmin.X, nmin.Z);
284 float ntop = noise_puff_top->result[index];
285 float nbottom = noise_puff_bottom->result[index];
287 if (!(flags & OREFLAG_PUFF_CLIFFS)) {
288 float ndiff = noiseval - nthresh;
296 int y0 = ymid - nbottom;
297 int y1 = ymid + ntop;
299 if ((flags & OREFLAG_PUFF_ADDITIVE) && (y0 > y1))
302 for (int y = y0; y <= y1; y++) {
303 u32 i = vm->m_area.index(x, y, z);
304 if (!vm->m_area.contains(i))
306 if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
309 vm->m_data[i] = n_ore;
315 ///////////////////////////////////////////////////////////////////////////////
318 void OreBlob::generate(MMVManip *vm, int mapseed, u32 blockseed,
319 v3s16 nmin, v3s16 nmax, u8 *biomemap)
321 PcgRandom pr(blockseed + 2404);
322 MapNode n_ore(c_ore, 0, ore_param2);
324 u32 sizex = (nmax.X - nmin.X + 1);
325 u32 volume = (nmax.X - nmin.X + 1) *
326 (nmax.Y - nmin.Y + 1) *
327 (nmax.Z - nmin.Z + 1);
328 u32 csize = clust_size;
329 u32 nblobs = volume / clust_scarcity;
332 noise = new Noise(&np, mapseed, csize, csize, csize);
334 for (u32 i = 0; i != nblobs; i++) {
335 int x0 = pr.range(nmin.X, nmax.X - csize + 1);
336 int y0 = pr.range(nmin.Y, nmax.Y - csize + 1);
337 int z0 = pr.range(nmin.Z, nmax.Z - csize + 1);
339 if (biomemap && !biomes.empty()) {
340 u32 bmapidx = sizex * (z0 - nmin.Z) + (x0 - nmin.X);
341 std::set<u8>::iterator it = biomes.find(biomemap[bmapidx]);
342 if (it == biomes.end())
346 bool noise_generated = false;
347 noise->seed = blockseed + i;
350 for (u32 z1 = 0; z1 != csize; z1++)
351 for (u32 y1 = 0; y1 != csize; y1++)
352 for (u32 x1 = 0; x1 != csize; x1++, index++) {
353 u32 i = vm->m_area.index(x0 + x1, y0 + y1, z0 + z1);
354 if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
357 // Lazily generate noise only if there's a chance of ore being placed
358 // This simple optimization makes calls 6x faster on average
359 if (!noise_generated) {
360 noise_generated = true;
361 noise->perlinMap3D(x0, y0, z0);
364 float noiseval = noise->result[index];
366 float xdist = (s32)x1 - (s32)csize / 2;
367 float ydist = (s32)y1 - (s32)csize / 2;
368 float zdist = (s32)z1 - (s32)csize / 2;
370 noiseval -= (sqrt(xdist * xdist + ydist * ydist + zdist * zdist) / csize);
372 if (noiseval < nthresh)
375 vm->m_data[i] = n_ore;
381 ///////////////////////////////////////////////////////////////////////////////
396 void OreVein::generate(MMVManip *vm, int mapseed, u32 blockseed,
397 v3s16 nmin, v3s16 nmax, u8 *biomemap)
399 PcgRandom pr(blockseed + 520);
400 MapNode n_ore(c_ore, 0, ore_param2);
402 u32 sizex = (nmax.X - nmin.X + 1);
405 int sx = nmax.X - nmin.X + 1;
406 int sy = nmax.Y - nmin.Y + 1;
407 int sz = nmax.Z - nmin.Z + 1;
408 noise = new Noise(&np, mapseed, sx, sy, sz);
409 noise2 = new Noise(&np, mapseed + 436, sx, sy, sz);
411 bool noise_generated = false;
414 for (int z = nmin.Z; z <= nmax.Z; z++)
415 for (int y = nmin.Y; y <= nmax.Y; y++)
416 for (int x = nmin.X; x <= nmax.X; x++, index++) {
417 u32 i = vm->m_area.index(x, y, z);
418 if (!vm->m_area.contains(i))
420 if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
423 if (biomemap && !biomes.empty()) {
424 u32 bmapidx = sizex * (z - nmin.Z) + (x - nmin.X);
425 std::set<u8>::iterator it = biomes.find(biomemap[bmapidx]);
426 if (it == biomes.end())
430 // Same lazy generation optimization as in OreBlob
431 if (!noise_generated) {
432 noise_generated = true;
433 noise->perlinMap3D(nmin.X, nmin.Y, nmin.Z);
434 noise2->perlinMap3D(nmin.X, nmin.Y, nmin.Z);
437 // randval ranges from -1..1
438 float randval = (float)pr.next() / (pr.RANDOM_RANGE / 2) - 1.f;
439 float noiseval = contour(noise->result[index]);
440 float noiseval2 = contour(noise2->result[index]);
441 if (noiseval * noiseval2 + randval * random_factor < nthresh)
444 vm->m_data[i] = n_ore;