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 const char *OreManager::ELEMENT_TITLE = "ore";
29 FlagDesc flagdesc_ore[] = {
30 {"absheight", OREFLAG_ABSHEIGHT},
31 {"scatter_noisedensity", OREFLAG_DENSITY},
32 {"claylike_nodeisnt", OREFLAG_NODEISNT},
37 ///////////////////////////////////////////////////////////////////////////////
40 size_t OreManager::placeAllOres(Mapgen *mg, u32 seed, v3s16 nmin, v3s16 nmax)
44 for (size_t i = 0; i != m_elements.size(); i++) {
45 Ore *ore = (Ore *)m_elements[i];
49 nplaced += ore->placeOre(mg, seed, nmin, nmax);
57 ///////////////////////////////////////////////////////////////////////////////
67 size_t Ore::placeOre(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
71 in_range |= (nmin.Y <= height_max && nmax.Y >= height_min);
72 if (flags & OREFLAG_ABSHEIGHT)
73 in_range |= (nmin.Y >= -height_max && nmax.Y <= -height_min) << 1;
78 if (in_range & ORE_RANGE_MIRROR) {
79 ymin = MYMAX(nmin.Y, -height_max);
80 ymax = MYMIN(nmax.Y, -height_min);
82 ymin = MYMAX(nmin.Y, height_min);
83 ymax = MYMIN(nmax.Y, height_max);
85 if (clust_size >= ymax - ymin + 1)
90 generate(mg->vm, mg->seed, blockseed, nmin, nmax);
96 void OreScatter::generate(ManualMapVoxelManipulator *vm, int seed,
97 u32 blockseed, v3s16 nmin, v3s16 nmax)
99 PseudoRandom pr(blockseed);
100 MapNode n_ore(c_ore, 0, ore_param2);
102 int volume = (nmax.X - nmin.X + 1) *
103 (nmax.Y - nmin.Y + 1) *
104 (nmax.Z - nmin.Z + 1);
105 int csize = clust_size;
106 int orechance = (csize * csize * csize) / clust_num_ores;
107 int nclusters = volume / clust_scarcity;
109 for (int i = 0; i != nclusters; i++) {
110 int x0 = pr.range(nmin.X, nmax.X - csize + 1);
111 int y0 = pr.range(nmin.Y, nmax.Y - csize + 1);
112 int z0 = pr.range(nmin.Z, nmax.Z - csize + 1);
114 if ((flags & OREFLAG_USE_NOISE) &&
115 (NoisePerlin3D(&np, x0, y0, z0, seed) < nthresh))
118 for (int z1 = 0; z1 != csize; z1++)
119 for (int y1 = 0; y1 != csize; y1++)
120 for (int x1 = 0; x1 != csize; x1++) {
121 if (pr.range(1, orechance) != 1)
124 u32 i = vm->m_area.index(x0 + x1, y0 + y1, z0 + z1);
125 if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
128 vm->m_data[i] = n_ore;
134 void OreSheet::generate(ManualMapVoxelManipulator *vm, int seed,
135 u32 blockseed, v3s16 nmin, v3s16 nmax)
137 PseudoRandom pr(blockseed + 4234);
138 MapNode n_ore(c_ore, 0, ore_param2);
140 int max_height = clust_size;
141 int y_start = pr.range(nmin.Y, nmax.Y - max_height);
144 int sx = nmax.X - nmin.X + 1;
145 int sz = nmax.Z - nmin.Z + 1;
146 noise = new Noise(&np, seed, sx, sz);
148 noise->seed = seed + y_start;
149 noise->perlinMap2D(nmin.X, nmin.Z);
152 for (int z = nmin.Z; z <= nmax.Z; z++)
153 for (int x = nmin.X; x <= nmax.X; x++) {
154 float noiseval = noise->result[index++];
155 if (noiseval < nthresh)
158 int height = max_height * (1. / pr.range(1, 3));
159 int y0 = y_start + np.scale * noiseval; //pr.range(1, 3) - 1;
160 int y1 = y0 + height;
161 for (int y = y0; y != y1; y++) {
162 u32 i = vm->m_area.index(x, y, z);
163 if (!vm->m_area.contains(i))
165 if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
168 vm->m_data[i] = n_ore;