3 Copyright (C) 2014-2016 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
4 Copyright (C) 2015-2017 paramat
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 FlagDesc flagdesc_ore[] = {
30 {"absheight", OREFLAG_ABSHEIGHT}, // Non-functional
31 {"puff_cliffs", OREFLAG_PUFF_CLIFFS},
32 {"puff_additive_composition", OREFLAG_PUFF_ADDITIVE},
37 ///////////////////////////////////////////////////////////////////////////////
40 OreManager::OreManager(IGameDef *gamedef) :
41 ObjDefManager(gamedef, OBJDEF_ORE)
46 size_t OreManager::placeAllOres(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
50 for (size_t i = 0; i != m_objects.size(); i++) {
51 Ore *ore = (Ore *)m_objects[i];
55 nplaced += ore->placeOre(mg, blockseed, nmin, nmax);
63 void OreManager::clear()
65 for (size_t i = 0; i < m_objects.size(); i++) {
66 Ore *ore = (Ore *)m_objects[i];
73 ///////////////////////////////////////////////////////////////////////////////
81 void Ore::resolveNodeNames()
83 getIdFromNrBacklog(&c_ore, "", CONTENT_AIR);
84 getIdsFromNrBacklog(&c_wherein);
88 size_t Ore::placeOre(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
90 if (!(nmin.Y <= y_max && nmax.Y >= y_min))
93 int actual_ymin = MYMAX(nmin.Y, y_min);
94 int actual_ymax = MYMIN(nmax.Y, y_max);
95 if (clust_size >= actual_ymax - actual_ymin + 1)
100 generate(mg->vm, mg->seed, blockseed, nmin, nmax, mg->biomemap);
106 ///////////////////////////////////////////////////////////////////////////////
109 void OreScatter::generate(MMVManip *vm, int mapseed, u32 blockseed,
110 v3s16 nmin, v3s16 nmax, u8 *biomemap)
112 PcgRandom pr(blockseed);
113 MapNode n_ore(c_ore, 0, ore_param2);
115 u32 sizex = (nmax.X - nmin.X + 1);
116 u32 volume = (nmax.X - nmin.X + 1) *
117 (nmax.Y - nmin.Y + 1) *
118 (nmax.Z - nmin.Z + 1);
119 u32 csize = clust_size;
120 u32 cvolume = csize * csize * csize;
121 u32 nclusters = volume / clust_scarcity;
123 for (u32 i = 0; i != nclusters; i++) {
124 int x0 = pr.range(nmin.X, nmax.X - csize + 1);
125 int y0 = pr.range(nmin.Y, nmax.Y - csize + 1);
126 int z0 = pr.range(nmin.Z, nmax.Z - csize + 1);
128 if ((flags & OREFLAG_USE_NOISE) &&
129 (NoisePerlin3D(&np, x0, y0, z0, mapseed) < nthresh))
132 if (biomemap && !biomes.empty()) {
133 u32 index = sizex * (z0 - nmin.Z) + (x0 - nmin.X);
134 std::unordered_set<u8>::const_iterator it = biomes.find(biomemap[index]);
135 if (it == biomes.end())
139 for (u32 z1 = 0; z1 != csize; z1++)
140 for (u32 y1 = 0; y1 != csize; y1++)
141 for (u32 x1 = 0; x1 != csize; x1++) {
142 if (pr.range(1, cvolume) > clust_num_ores)
145 u32 i = vm->m_area.index(x0 + x1, y0 + y1, z0 + z1);
146 if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
149 vm->m_data[i] = n_ore;
155 ///////////////////////////////////////////////////////////////////////////////
158 void OreSheet::generate(MMVManip *vm, int mapseed, u32 blockseed,
159 v3s16 nmin, v3s16 nmax, u8 *biomemap)
161 PcgRandom pr(blockseed + 4234);
162 MapNode n_ore(c_ore, 0, ore_param2);
164 u16 max_height = column_height_max;
165 int y_start_min = nmin.Y + max_height;
166 int y_start_max = nmax.Y - max_height;
168 int y_start = y_start_min < y_start_max ?
169 pr.range(y_start_min, y_start_max) :
170 (y_start_min + y_start_max) / 2;
173 int sx = nmax.X - nmin.X + 1;
174 int sz = nmax.Z - nmin.Z + 1;
175 noise = new Noise(&np, 0, sx, sz);
177 noise->seed = mapseed + y_start;
178 noise->perlinMap2D(nmin.X, nmin.Z);
181 for (int z = nmin.Z; z <= nmax.Z; z++)
182 for (int x = nmin.X; x <= nmax.X; x++, index++) {
183 float noiseval = noise->result[index];
184 if (noiseval < nthresh)
187 if (biomemap && !biomes.empty()) {
188 std::unordered_set<u8>::const_iterator it = biomes.find(biomemap[index]);
189 if (it == biomes.end())
193 u16 height = pr.range(column_height_min, column_height_max);
194 int ymidpoint = y_start + noiseval;
195 int y0 = MYMAX(nmin.Y, ymidpoint - height * (1 - column_midpoint_factor));
196 int y1 = MYMIN(nmax.Y, y0 + height - 1);
198 for (int y = y0; y <= y1; y++) {
199 u32 i = vm->m_area.index(x, y, z);
200 if (!vm->m_area.contains(i))
202 if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
205 vm->m_data[i] = n_ore;
211 ///////////////////////////////////////////////////////////////////////////////
221 delete noise_puff_top;
222 delete noise_puff_bottom;
226 void OrePuff::generate(MMVManip *vm, int mapseed, u32 blockseed,
227 v3s16 nmin, v3s16 nmax, u8 *biomemap)
229 PcgRandom pr(blockseed + 4234);
230 MapNode n_ore(c_ore, 0, ore_param2);
232 int y_start = pr.range(nmin.Y, nmax.Y);
235 int sx = nmax.X - nmin.X + 1;
236 int sz = nmax.Z - nmin.Z + 1;
237 noise = new Noise(&np, 0, sx, sz);
238 noise_puff_top = new Noise(&np_puff_top, 0, sx, sz);
239 noise_puff_bottom = new Noise(&np_puff_bottom, 0, sx, sz);
242 noise->seed = mapseed + y_start;
243 noise->perlinMap2D(nmin.X, nmin.Z);
244 bool noise_generated = false;
247 for (int z = nmin.Z; z <= nmax.Z; z++)
248 for (int x = nmin.X; x <= nmax.X; x++, index++) {
249 float noiseval = noise->result[index];
250 if (noiseval < nthresh)
253 if (biomemap && !biomes.empty()) {
254 std::unordered_set<u8>::const_iterator it = biomes.find(biomemap[index]);
255 if (it == biomes.end())
259 if (!noise_generated) {
260 noise_generated = true;
261 noise_puff_top->perlinMap2D(nmin.X, nmin.Z);
262 noise_puff_bottom->perlinMap2D(nmin.X, nmin.Z);
265 float ntop = noise_puff_top->result[index];
266 float nbottom = noise_puff_bottom->result[index];
268 if (!(flags & OREFLAG_PUFF_CLIFFS)) {
269 float ndiff = noiseval - nthresh;
277 int y0 = ymid - nbottom;
278 int y1 = ymid + ntop;
280 if ((flags & OREFLAG_PUFF_ADDITIVE) && (y0 > y1))
283 for (int y = y0; y <= y1; y++) {
284 u32 i = vm->m_area.index(x, y, z);
285 if (!vm->m_area.contains(i))
287 if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
290 vm->m_data[i] = n_ore;
296 ///////////////////////////////////////////////////////////////////////////////
299 void OreBlob::generate(MMVManip *vm, int mapseed, u32 blockseed,
300 v3s16 nmin, v3s16 nmax, u8 *biomemap)
302 PcgRandom pr(blockseed + 2404);
303 MapNode n_ore(c_ore, 0, ore_param2);
305 u32 sizex = (nmax.X - nmin.X + 1);
306 u32 volume = (nmax.X - nmin.X + 1) *
307 (nmax.Y - nmin.Y + 1) *
308 (nmax.Z - nmin.Z + 1);
309 u32 csize = clust_size;
310 u32 nblobs = volume / clust_scarcity;
313 noise = new Noise(&np, mapseed, csize, csize, csize);
315 for (u32 i = 0; i != nblobs; i++) {
316 int x0 = pr.range(nmin.X, nmax.X - csize + 1);
317 int y0 = pr.range(nmin.Y, nmax.Y - csize + 1);
318 int z0 = pr.range(nmin.Z, nmax.Z - csize + 1);
320 if (biomemap && !biomes.empty()) {
321 u32 bmapidx = sizex * (z0 - nmin.Z) + (x0 - nmin.X);
322 std::unordered_set<u8>::const_iterator it = biomes.find(biomemap[bmapidx]);
323 if (it == biomes.end())
327 bool noise_generated = false;
328 noise->seed = blockseed + i;
331 for (u32 z1 = 0; z1 != csize; z1++)
332 for (u32 y1 = 0; y1 != csize; y1++)
333 for (u32 x1 = 0; x1 != csize; x1++, index++) {
334 u32 i = vm->m_area.index(x0 + x1, y0 + y1, z0 + z1);
335 if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
338 // Lazily generate noise only if there's a chance of ore being placed
339 // This simple optimization makes calls 6x faster on average
340 if (!noise_generated) {
341 noise_generated = true;
342 noise->perlinMap3D(x0, y0, z0);
345 float noiseval = noise->result[index];
347 float xdist = (s32)x1 - (s32)csize / 2;
348 float ydist = (s32)y1 - (s32)csize / 2;
349 float zdist = (s32)z1 - (s32)csize / 2;
351 noiseval -= (sqrt(xdist * xdist + ydist * ydist + zdist * zdist) / csize);
353 if (noiseval < nthresh)
356 vm->m_data[i] = n_ore;
362 ///////////////////////////////////////////////////////////////////////////////
376 void OreVein::generate(MMVManip *vm, int mapseed, u32 blockseed,
377 v3s16 nmin, v3s16 nmax, u8 *biomemap)
379 PcgRandom pr(blockseed + 520);
380 MapNode n_ore(c_ore, 0, ore_param2);
382 u32 sizex = (nmax.X - nmin.X + 1);
385 int sx = nmax.X - nmin.X + 1;
386 int sy = nmax.Y - nmin.Y + 1;
387 int sz = nmax.Z - nmin.Z + 1;
388 noise = new Noise(&np, mapseed, sx, sy, sz);
389 noise2 = new Noise(&np, mapseed + 436, sx, sy, sz);
391 bool noise_generated = false;
394 for (int z = nmin.Z; z <= nmax.Z; z++)
395 for (int y = nmin.Y; y <= nmax.Y; y++)
396 for (int x = nmin.X; x <= nmax.X; x++, index++) {
397 u32 i = vm->m_area.index(x, y, z);
398 if (!vm->m_area.contains(i))
400 if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
403 if (biomemap && !biomes.empty()) {
404 u32 bmapidx = sizex * (z - nmin.Z) + (x - nmin.X);
405 std::unordered_set<u8>::const_iterator it = biomes.find(biomemap[bmapidx]);
406 if (it == biomes.end())
410 // Same lazy generation optimization as in OreBlob
411 if (!noise_generated) {
412 noise_generated = true;
413 noise->perlinMap3D(nmin.X, nmin.Y, nmin.Z);
414 noise2->perlinMap3D(nmin.X, nmin.Y, nmin.Z);
417 // randval ranges from -1..1
418 float randval = (float)pr.next() / (pr.RANDOM_RANGE / 2) - 1.f;
419 float noiseval = contour(noise->result[index]);
420 float noiseval2 = contour(noise2->result[index]);
421 if (noiseval * noiseval2 + randval * random_factor < nthresh)
424 vm->m_data[i] = n_ore;