Add minetest.clear_registered_decorations() and clear_registered_ores()
[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         {"scatter_noisedensity", OREFLAG_DENSITY},
32         {"claylike_nodeisnt",    OREFLAG_NODEISNT},
33         {NULL,                   0}
34 };
35
36
37 ///////////////////////////////////////////////////////////////////////////////
38
39
40 OreManager::OreManager(IGameDef *gamedef) :
41         GenElementManager(gamedef)
42 {
43 }
44
45
46 size_t OreManager::placeAllOres(Mapgen *mg, u32 seed, v3s16 nmin, v3s16 nmax)
47 {
48         size_t nplaced = 0;
49
50         for (size_t i = 0; i != m_elements.size(); i++) {
51                 Ore *ore = (Ore *)m_elements[i];
52                 if (!ore)
53                         continue;
54
55                 nplaced += ore->placeOre(mg, seed, nmin, nmax);
56                 seed++;
57         }
58
59         return nplaced;
60 }
61
62
63 void OreManager::clear()
64 {
65         for (size_t i = 0; i < m_elements.size(); i++) {
66                 Ore *ore = (Ore *)m_elements[i];
67                 if (!ore)
68                         continue;
69
70                 m_resolver->cancelNodeList(&ore->c_wherein);
71                 m_resolver->cancelNode(&ore->c_ore);
72         }
73         m_elements.clear();
74 }
75
76
77 ///////////////////////////////////////////////////////////////////////////////
78
79
80 Ore::Ore()
81 {
82         flags = 0;
83         noise = NULL;
84 }
85
86
87 size_t Ore::placeOre(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
88 {
89         int in_range = 0;
90
91         in_range |= (nmin.Y <= height_max && nmax.Y >= height_min);
92         if (flags & OREFLAG_ABSHEIGHT)
93                 in_range |= (nmin.Y >= -height_max && nmax.Y <= -height_min) << 1;
94         if (!in_range)
95                 return 0;
96
97         int ymin, ymax;
98         if (in_range & ORE_RANGE_MIRROR) {
99                 ymin = MYMAX(nmin.Y, -height_max);
100                 ymax = MYMIN(nmax.Y, -height_min);
101         } else {
102                 ymin = MYMAX(nmin.Y, height_min);
103                 ymax = MYMIN(nmax.Y, height_max);
104         }
105         if (clust_size >= ymax - ymin + 1)
106                 return 0;
107
108         nmin.Y = ymin;
109         nmax.Y = ymax;
110         generate(mg->vm, mg->seed, blockseed, nmin, nmax);
111
112         return 1;
113 }
114
115
116 void OreScatter::generate(ManualMapVoxelManipulator *vm, int seed,
117         u32 blockseed, v3s16 nmin, v3s16 nmax)
118 {
119         PseudoRandom pr(blockseed);
120         MapNode n_ore(c_ore, 0, ore_param2);
121
122         int volume = (nmax.X - nmin.X + 1) *
123                                  (nmax.Y - nmin.Y + 1) *
124                                  (nmax.Z - nmin.Z + 1);
125         int csize     = clust_size;
126         int orechance = (csize * csize * csize) / clust_num_ores;
127         int nclusters = volume / clust_scarcity;
128
129         for (int i = 0; i != nclusters; i++) {
130                 int x0 = pr.range(nmin.X, nmax.X - csize + 1);
131                 int y0 = pr.range(nmin.Y, nmax.Y - csize + 1);
132                 int z0 = pr.range(nmin.Z, nmax.Z - csize + 1);
133
134                 if ((flags & OREFLAG_USE_NOISE) &&
135                         (NoisePerlin3D(&np, x0, y0, z0, seed) < nthresh))
136                         continue;
137
138                 for (int z1 = 0; z1 != csize; z1++)
139                 for (int y1 = 0; y1 != csize; y1++)
140                 for (int x1 = 0; x1 != csize; x1++) {
141                         if (pr.range(1, orechance) != 1)
142                                 continue;
143
144                         u32 i = vm->m_area.index(x0 + x1, y0 + y1, z0 + z1);
145                         if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
146                                 continue;
147
148                         vm->m_data[i] = n_ore;
149                 }
150         }
151 }
152
153
154 void OreSheet::generate(ManualMapVoxelManipulator *vm, int seed,
155         u32 blockseed, v3s16 nmin, v3s16 nmax)
156 {
157         PseudoRandom pr(blockseed + 4234);
158         MapNode n_ore(c_ore, 0, ore_param2);
159
160         int max_height = clust_size;
161         int y_start = pr.range(nmin.Y, nmax.Y - max_height);
162
163         if (!noise) {
164                 int sx = nmax.X - nmin.X + 1;
165                 int sz = nmax.Z - nmin.Z + 1;
166                 noise = new Noise(&np, seed, sx, sz);
167         }
168         noise->seed = seed + y_start;
169         noise->perlinMap2D(nmin.X, nmin.Z);
170
171         int index = 0;
172         for (int z = nmin.Z; z <= nmax.Z; z++)
173         for (int x = nmin.X; x <= nmax.X; x++) {
174                 float noiseval = noise->result[index++];
175                 if (noiseval < nthresh)
176                         continue;
177
178                 int height = max_height * (1. / pr.range(1, 3));
179                 int y0 = y_start + np.scale * noiseval; //pr.range(1, 3) - 1;
180                 int y1 = y0 + height;
181                 for (int y = y0; y != y1; y++) {
182                         u32 i = vm->m_area.index(x, y, z);
183                         if (!vm->m_area.contains(i))
184                                 continue;
185                         if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
186                                 continue;
187
188                         vm->m_data[i] = n_ore;
189                 }
190         }
191 }