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},
33 ///////////////////////////////////////////////////////////////////////////////
36 OreManager::OreManager(IGameDef *gamedef) :
37 ObjDefManager(gamedef, OBJDEF_ORE)
42 size_t OreManager::placeAllOres(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
46 for (size_t i = 0; i != m_objects.size(); i++) {
47 Ore *ore = (Ore *)m_objects[i];
51 nplaced += ore->placeOre(mg, blockseed, nmin, nmax);
59 void OreManager::clear()
61 for (size_t i = 0; i < m_objects.size(); i++) {
62 Ore *ore = (Ore *)m_objects[i];
69 ///////////////////////////////////////////////////////////////////////////////
85 void Ore::resolveNodeNames()
87 getIdFromNrBacklog(&c_ore, "", CONTENT_AIR);
88 getIdsFromNrBacklog(&c_wherein);
92 size_t Ore::placeOre(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
96 in_range |= (nmin.Y <= y_max && nmax.Y >= y_min);
97 if (flags & OREFLAG_ABSHEIGHT)
98 in_range |= (nmin.Y >= -y_max && nmax.Y <= -y_min) << 1;
102 int actual_ymin, actual_ymax;
103 if (in_range & ORE_RANGE_MIRROR) {
104 actual_ymin = MYMAX(nmin.Y, -y_max);
105 actual_ymax = MYMIN(nmax.Y, -y_min);
107 actual_ymin = MYMAX(nmin.Y, y_min);
108 actual_ymax = MYMIN(nmax.Y, y_max);
110 if (clust_size >= actual_ymax - actual_ymin + 1)
113 nmin.Y = actual_ymin;
114 nmax.Y = actual_ymax;
115 generate(mg->vm, mg->seed, blockseed, nmin, nmax, mg->biomemap);
121 ///////////////////////////////////////////////////////////////////////////////
124 void OreScatter::generate(MMVManip *vm, int mapseed, u32 blockseed,
125 v3s16 nmin, v3s16 nmax, u8 *biomemap)
127 PseudoRandom pr(blockseed);
128 MapNode n_ore(c_ore, 0, ore_param2);
130 u32 sizex = (nmax.X - nmin.X + 1);
131 u32 volume = (nmax.X - nmin.X + 1) *
132 (nmax.Y - nmin.Y + 1) *
133 (nmax.Z - nmin.Z + 1);
134 u32 csize = clust_size;
135 u32 orechance = (csize * csize * csize) / clust_num_ores;
136 u32 nclusters = volume / clust_scarcity;
138 for (u32 i = 0; i != nclusters; i++) {
139 int x0 = pr.range(nmin.X, nmax.X - csize + 1);
140 int y0 = pr.range(nmin.Y, nmax.Y - csize + 1);
141 int z0 = pr.range(nmin.Z, nmax.Z - csize + 1);
143 if ((flags & OREFLAG_USE_NOISE) &&
144 (NoisePerlin3D(&np, x0, y0, z0, mapseed) < nthresh))
147 if (biomemap && !biomes.empty()) {
148 u32 index = sizex * (z0 - nmin.Z) + (x0 - nmin.X);
149 std::set<u8>::iterator it = biomes.find(biomemap[index]);
150 if (it == biomes.end())
154 for (u32 z1 = 0; z1 != csize; z1++)
155 for (u32 y1 = 0; y1 != csize; y1++)
156 for (u32 x1 = 0; x1 != csize; x1++) {
157 if (pr.range(1, orechance) != 1)
160 u32 i = vm->m_area.index(x0 + x1, y0 + y1, z0 + z1);
161 if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
164 vm->m_data[i] = n_ore;
170 ///////////////////////////////////////////////////////////////////////////////
173 void OreSheet::generate(MMVManip *vm, int mapseed, u32 blockseed,
174 v3s16 nmin, v3s16 nmax, u8 *biomemap)
176 PseudoRandom pr(blockseed + 4234);
177 MapNode n_ore(c_ore, 0, ore_param2);
179 int max_height = clust_size;
180 int y_start = pr.range(nmin.Y, nmax.Y - max_height);
183 int sx = nmax.X - nmin.X + 1;
184 int sz = nmax.Z - nmin.Z + 1;
185 noise = new Noise(&np, 0, sx, sz);
187 noise->seed = mapseed + y_start;
188 noise->perlinMap2D(nmin.X, nmin.Z);
191 for (int z = nmin.Z; z <= nmax.Z; z++)
192 for (int x = nmin.X; x <= nmax.X; x++, index++) {
193 float noiseval = noise->result[index];
194 if (noiseval < nthresh)
197 if (biomemap && !biomes.empty()) {
198 std::set<u8>::iterator it = biomes.find(biomemap[index]);
199 if (it == biomes.end())
203 int height = max_height * (1. / pr.range(1, 3));
204 int y0 = y_start + np.scale * noiseval; //pr.range(1, 3) - 1;
205 int y1 = y0 + height;
206 for (int y = y0; y != y1; y++) {
207 u32 i = vm->m_area.index(x, y, z);
208 if (!vm->m_area.contains(i))
210 if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
213 vm->m_data[i] = n_ore;
219 ///////////////////////////////////////////////////////////////////////////////
221 void OreBlob::generate(MMVManip *vm, int mapseed, u32 blockseed,
222 v3s16 nmin, v3s16 nmax, u8 *biomemap)
224 PseudoRandom pr(blockseed + 2404);
225 MapNode n_ore(c_ore, 0, ore_param2);
227 u32 sizex = (nmax.X - nmin.X + 1);
228 u32 volume = (nmax.X - nmin.X + 1) *
229 (nmax.Y - nmin.Y + 1) *
230 (nmax.Z - nmin.Z + 1);
231 u32 csize = clust_size;
232 u32 nblobs = volume / clust_scarcity;
235 noise = new Noise(&np, mapseed, csize, csize, csize);
237 for (u32 i = 0; i != nblobs; i++) {
238 int x0 = pr.range(nmin.X, nmax.X - csize + 1);
239 int y0 = pr.range(nmin.Y, nmax.Y - csize + 1);
240 int z0 = pr.range(nmin.Z, nmax.Z - csize + 1);
242 if (biomemap && !biomes.empty()) {
243 u32 bmapidx = sizex * (z0 - nmin.Z) + (x0 - nmin.X);
244 std::set<u8>::iterator it = biomes.find(biomemap[bmapidx]);
245 if (it == biomes.end())
249 bool noise_generated = false;
250 noise->seed = blockseed + i;
253 for (u32 z1 = 0; z1 != csize; z1++)
254 for (u32 y1 = 0; y1 != csize; y1++)
255 for (u32 x1 = 0; x1 != csize; x1++, index++) {
256 u32 i = vm->m_area.index(x0 + x1, y0 + y1, z0 + z1);
257 if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
260 // Lazily generate noise only if there's a chance of ore being placed
261 // This simple optimization makes calls 6x faster on average
262 if (!noise_generated) {
263 noise_generated = true;
264 noise->perlinMap3D(x0, y0, z0);
267 float noiseval = noise->result[index];
269 float xdist = x1 - csize / 2;
270 float ydist = y1 - csize / 2;
271 float zdist = z1 - csize / 2;
273 noiseval -= (sqrt(xdist * xdist + ydist * ydist + zdist * zdist) / csize);
275 if (noiseval < nthresh)
278 vm->m_data[i] = n_ore;
284 ///////////////////////////////////////////////////////////////////////////////
298 void OreVein::generate(MMVManip *vm, int mapseed, u32 blockseed,
299 v3s16 nmin, v3s16 nmax, u8 *biomemap)
301 PseudoRandom pr(blockseed + 520);
302 MapNode n_ore(c_ore, 0, ore_param2);
304 u32 sizex = (nmax.X - nmin.X + 1);
307 int sx = nmax.X - nmin.X + 1;
308 int sy = nmax.Y - nmin.Y + 1;
309 int sz = nmax.Z - nmin.Z + 1;
310 noise = new Noise(&np, mapseed, sx, sy, sz);
311 noise2 = new Noise(&np, mapseed + 436, sx, sy, sz);
313 bool noise_generated = false;
316 for (int z = nmin.Z; z <= nmax.Z; z++)
317 for (int y = nmin.Y; y <= nmax.Y; y++)
318 for (int x = nmin.X; x <= nmax.X; x++, index++) {
319 u32 i = vm->m_area.index(x, y, z);
320 if (!vm->m_area.contains(i))
322 if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
325 if (biomemap && !biomes.empty()) {
326 u32 bmapidx = sizex * (z - nmin.Z) + (x - nmin.X);
327 std::set<u8>::iterator it = biomes.find(biomemap[bmapidx]);
328 if (it == biomes.end())
332 // Same lazy generation optimization as in OreBlob
333 if (!noise_generated) {
334 noise_generated = true;
335 noise->perlinMap3D(nmin.X, nmin.Y, nmin.Z);
336 noise2->perlinMap3D(nmin.X, nmin.Y, nmin.Z);
339 // randval ranges from -1..1
340 float randval = (float)pr.next() / (pr.RANDOM_RANGE / 2) - 1.f;
341 float noiseval = contour(noise->result[index]);
342 float noiseval2 = contour(noise2->result[index]);
343 if (noiseval * noiseval2 + randval * random_factor < nthresh)
346 vm->m_data[i] = n_ore;