]> git.lizzy.rs Git - minetest.git/blob - src/mg_ore.cpp
Refactor the_game() to make it more understandable and maintainable.
[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 "util/numeric.h"
23 #include "map.h"
24 #include "log.h"
25
26
27 FlagDesc flagdesc_ore[] = {
28         {"absheight",            OREFLAG_ABSHEIGHT},
29         {"scatter_noisedensity", OREFLAG_DENSITY},
30         {"claylike_nodeisnt",    OREFLAG_NODEISNT},
31         {NULL,                   0}
32 };
33
34 ///////////////////////////////////////////////////////////////////////////////
35
36
37 Ore *createOre(OreType type)
38 {
39         switch (type) {
40         case ORE_SCATTER:
41                 return new OreScatter;
42         case ORE_SHEET:
43                 return new OreSheet;
44         //case ORE_CLAYLIKE: //TODO: implement this!
45         //      return new OreClaylike;
46         default:
47                 return NULL;
48         }
49 }
50
51
52 Ore::~Ore()
53 {
54         delete np;
55         delete noise;
56 }
57
58
59 void Ore::placeOre(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
60 {
61         int in_range = 0;
62
63         in_range |= (nmin.Y <= height_max && nmax.Y >= height_min);
64         if (flags & OREFLAG_ABSHEIGHT)
65                 in_range |= (nmin.Y >= -height_max && nmax.Y <= -height_min) << 1;
66         if (!in_range)
67                 return;
68
69         int ymin, ymax;
70         if (in_range & ORE_RANGE_MIRROR) {
71                 ymin = MYMAX(nmin.Y, -height_max);
72                 ymax = MYMIN(nmax.Y, -height_min);
73         } else {
74                 ymin = MYMAX(nmin.Y, height_min);
75                 ymax = MYMIN(nmax.Y, height_max);
76         }
77         if (clust_size >= ymax - ymin + 1)
78                 return;
79
80         nmin.Y = ymin;
81         nmax.Y = ymax;
82         generate(mg->vm, mg->seed, blockseed, nmin, nmax);
83 }
84
85
86 void OreScatter::generate(ManualMapVoxelManipulator *vm, int seed,
87         u32 blockseed, v3s16 nmin, v3s16 nmax)
88 {
89         PseudoRandom pr(blockseed);
90         MapNode n_ore(c_ore, 0, ore_param2);
91
92         int volume = (nmax.X - nmin.X + 1) *
93                                  (nmax.Y - nmin.Y + 1) *
94                                  (nmax.Z - nmin.Z + 1);
95         int csize     = clust_size;
96         int orechance = (csize * csize * csize) / clust_num_ores;
97         int nclusters = volume / clust_scarcity;
98
99         for (int i = 0; i != nclusters; i++) {
100                 int x0 = pr.range(nmin.X, nmax.X - csize + 1);
101                 int y0 = pr.range(nmin.Y, nmax.Y - csize + 1);
102                 int z0 = pr.range(nmin.Z, nmax.Z - csize + 1);
103
104                 if (np && (NoisePerlin3D(np, x0, y0, z0, seed) < nthresh))
105                         continue;
106
107                 for (int z1 = 0; z1 != csize; z1++)
108                 for (int y1 = 0; y1 != csize; y1++)
109                 for (int x1 = 0; x1 != csize; x1++) {
110                         if (pr.range(1, orechance) != 1)
111                                 continue;
112
113                         u32 i = vm->m_area.index(x0 + x1, y0 + y1, z0 + z1);
114                         if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
115                                 continue;
116
117                         vm->m_data[i] = n_ore;
118                 }
119         }
120 }
121
122
123 void OreSheet::generate(ManualMapVoxelManipulator *vm, int seed,
124         u32 blockseed, v3s16 nmin, v3s16 nmax)
125 {
126         PseudoRandom pr(blockseed + 4234);
127         MapNode n_ore(c_ore, 0, ore_param2);
128
129         int max_height = clust_size;
130         int y_start = pr.range(nmin.Y, nmax.Y - max_height);
131
132         if (!noise) {
133                 int sx = nmax.X - nmin.X + 1;
134                 int sz = nmax.Z - nmin.Z + 1;
135                 noise = new Noise(np, 0, sx, sz);
136         }
137         noise->seed = seed + y_start;
138         noise->perlinMap2D(nmin.X, nmin.Z);
139
140         int index = 0;
141         for (int z = nmin.Z; z <= nmax.Z; z++)
142         for (int x = nmin.X; x <= nmax.X; x++) {
143                 float noiseval = noise->result[index++];
144                 if (noiseval < nthresh)
145                         continue;
146
147                 int height = max_height * (1. / pr.range(1, 3));
148                 int y0 = y_start + np->scale * noiseval; //pr.range(1, 3) - 1;
149                 int y1 = y0 + height;
150                 for (int y = y0; y != y1; y++) {
151                         u32 i = vm->m_area.index(x, y, z);
152                         if (!vm->m_area.contains(i))
153                                 continue;
154                         if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
155                                 continue;
156
157                         vm->m_data[i] = n_ore;
158                 }
159         }
160 }
161