3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
20 #include "util/numeric.h"
26 CaveV6::CaveV6(Mapgen *mg, PseudoRandom *ps, PseudoRandom *ps2,
27 bool is_large_cave, content_t c_water, content_t c_lava) {
29 this->water_level = mg->water_level;
30 this->large_cave = is_large_cave;
33 this->c_water_source = c_water;
34 this->c_lava_source = c_lava;
36 min_tunnel_diameter = 2;
37 max_tunnel_diameter = ps->range(2, 6);
38 dswitchint = ps->range(1, 14);
42 part_max_length_rs = ps->range(2,4);
43 tunnel_routepoints = ps->range(5, ps->range(15,30));
44 min_tunnel_diameter = 5;
45 max_tunnel_diameter = ps->range(7, ps->range(8,24));
47 part_max_length_rs = ps->range(2,9);
48 tunnel_routepoints = ps->range(10, ps->range(15,30));
51 large_cave_is_flat = (ps->range(0,1) == 0);
55 void CaveV6::makeCave(v3s16 nmin, v3s16 nmax, int max_stone_height) {
58 max_stone_y = max_stone_height;
59 main_direction = v3f(0, 0, 0);
61 // Allowed route area size in nodes
62 ar = node_max - node_min + v3s16(1, 1, 1);
63 // Area starting point in nodes
67 //(this should be more than the maximum radius of the tunnel)
68 const s16 max_spread_amount = MAP_BLOCKSIZE;
70 s16 more = max_spread_amount - max_tunnel_diameter / 2 - insure;
71 ar += v3s16(1,0,1) * more * 2;
72 of -= v3s16(1,0,1) * more;
75 // Allow half a diameter + 7 over stone surface
76 route_y_max = -of.Y + max_stone_y + max_tunnel_diameter / 2 + 7;
78 // Limit maximum to area
79 route_y_max = rangelim(route_y_max, 0, ar.Y - 1);
83 if (node_min.Y < water_level && node_max.Y > water_level) {
84 min = water_level - max_tunnel_diameter/3 - of.Y;
85 route_y_max = water_level + max_tunnel_diameter/3 - of.Y;
87 route_y_min = ps->range(min, min + max_tunnel_diameter);
88 route_y_min = rangelim(route_y_min, 0, route_y_max);
91 s16 route_start_y_min = route_y_min;
92 s16 route_start_y_max = route_y_max;
94 route_start_y_min = rangelim(route_start_y_min, 0, ar.Y-1);
95 route_start_y_max = rangelim(route_start_y_max, route_start_y_min, ar.Y-1);
97 // Randomize starting position
99 (float)(ps->next() % ar.X) + 0.5,
100 (float)(ps->range(route_start_y_min, route_start_y_max)) + 0.5,
101 (float)(ps->next() % ar.Z) + 0.5
104 // Generate some tunnel starting from orp
105 for (u16 j = 0; j < tunnel_routepoints; j++)
106 makeTunnel(j % dswitchint == 0);
110 void CaveV6::makeTunnel(bool dirswitch) {
111 if (dirswitch && !large_cave) {
112 main_direction = v3f(
113 ((float)(ps->next() % 20) - (float)10) / 10,
114 ((float)(ps->next() % 20) - (float)10) / 30,
115 ((float)(ps->next() % 20) - (float)10) / 10
117 main_direction *= (float)ps->range(0, 10) / 10;
121 s16 min_d = min_tunnel_diameter;
122 s16 max_d = max_tunnel_diameter;
123 rs = ps->range(min_d, max_d);
128 rs * part_max_length_rs,
129 rs * part_max_length_rs / 2,
130 rs * part_max_length_rs
134 rs * part_max_length_rs,
135 ps->range(1, rs * part_max_length_rs),
136 rs * part_max_length_rs
141 (float)(ps->next() % (maxlen.X * 1)) - (float)maxlen.X / 2,
142 (float)(ps->next() % (maxlen.Y * 1)) - (float)maxlen.Y / 2,
143 (float)(ps->next() % (maxlen.Z * 1)) - (float)maxlen.Z / 2
146 // Jump downward sometimes
147 if (!large_cave && ps->range(0, 12) == 0) {
149 (float)(ps->next() % (maxlen.X * 1)) - (float)maxlen.X / 2,
150 (float)(ps->next() % (maxlen.Y * 2)) - (float)maxlen.Y,
151 (float)(ps->next() % (maxlen.Z * 1)) - (float)maxlen.Z / 2
157 s16 h = find_ground_level_clever(vmanip, v2s16(p.X, p.Z), ndef);
158 route_y_min = h - rs/3;
159 route_y_max = h + rs;
162 vec += main_direction;
167 else if (rp.X >= ar.X)
170 if (rp.Y < route_y_min)
172 else if (rp.Y >= route_y_max)
173 rp.Y = route_y_max - 1;
177 else if (rp.Z >= ar.Z)
182 float veclen = vec.getLength();
183 // As odd as it sounds, veclen is *exactly* 0.0 sometimes, causing a FPE
187 // Every second section is rough
188 bool randomize_xz = (ps2->range(1, 2) == 1);
191 for (float f = 0; f < 1.0; f += 1.0 / veclen)
192 carveRoute(vec, f, randomize_xz);
198 void CaveV6::carveRoute(v3f vec, float f, bool randomize_xz) {
199 MapNode airnode(CONTENT_AIR);
200 MapNode waternode(c_water_source);
201 MapNode lavanode(c_lava_source);
203 v3s16 startp(orp.X, orp.Y, orp.Z);
206 v3f fp = orp + vec * f;
207 fp.X += 0.1 * ps->range(-10, 10);
208 fp.Z += 0.1 * ps->range(-10, 10);
209 v3s16 cp(fp.X, fp.Y, fp.Z);
214 d0 += ps->range(-1, 1);
215 d1 += ps->range(-1, 1);
218 for (s16 z0 = d0; z0 <= d1; z0++) {
219 s16 si = rs / 2 - MYMAX(0, abs(z0) - rs / 7 - 1);
220 for (s16 x0 = -si - ps->range(0,1); x0 <= si - 1 + ps->range(0,1); x0++) {
221 s16 maxabsxz = MYMAX(abs(x0), abs(z0));
222 s16 si2 = rs / 2 - MYMAX(0, maxabsxz - rs / 7 - 1);
223 for (s16 y0 = -si2; y0 <= si2; y0++) {
224 // Make better floors in small caves
225 //if(y0 <= -rs/2 && rs<=7)
228 if (large_cave_is_flat) {
229 // Make large caves not so tall
230 if (rs > 7 && abs(y0) >= rs / 3)
234 v3s16 p(cp.X + x0, cp.Y + y0, cp.Z + z0);
237 if (vm->m_area.contains(p) == false)
240 u32 i = vm->m_area.index(p);
243 int full_ymin = node_min.Y - MAP_BLOCKSIZE;
244 int full_ymax = node_max.Y + MAP_BLOCKSIZE;
246 if (flooded && full_ymin < water_level && full_ymax > water_level) {
247 vm->m_data[i] = (p.Y <= water_level) ? waternode : airnode;
248 } else if (flooded && full_ymax < water_level) {
249 vm->m_data[i] = (p.Y < startp.Y - 2) ? lavanode : airnode;
251 vm->m_data[i] = airnode;
254 // Don't replace air or water or lava or ignore
255 content_t c = vm->m_data[i].getContent();
256 if (c == CONTENT_IGNORE || c == CONTENT_AIR ||
257 c == c_water_source || c == c_lava_source)
260 vm->m_data[i] = airnode;
261 vm->m_flags[i] |= VMANIP_FLAG_CAVE;