|
| 1 | +/* |
| 2 | +Minetest |
| 3 | +Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com> |
| 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 "util/numeric.h" |
| 21 | +#include "map.h" |
| 22 | +#include "mapgen.h" |
| 23 | +#include "cavegen.h" |
| 24 | + |
| 25 | + |
| 26 | +CaveV6::CaveV6(Mapgen *mg, PseudoRandom *ps, PseudoRandom *ps2, |
| 27 | + bool is_large_cave, content_t c_water, content_t c_lava) { |
| 28 | + this->vm = mg->vm; |
| 29 | + this->water_level = mg->water_level; |
| 30 | + this->large_cave = is_large_cave; |
| 31 | + this->ps = ps; |
| 32 | + this->ps2 = ps2; |
| 33 | + this->c_water_source = c_water; |
| 34 | + this->c_lava_source = c_lava; |
| 35 | + |
| 36 | + min_tunnel_diameter = 2; |
| 37 | + max_tunnel_diameter = ps->range(2, 6); |
| 38 | + dswitchint = ps->range(1, 14); |
| 39 | + flooded = true; |
| 40 | + |
| 41 | + if (large_cave) { |
| 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)); |
| 46 | + } else { |
| 47 | + part_max_length_rs = ps->range(2,9); |
| 48 | + tunnel_routepoints = ps->range(10, ps->range(15,30)); |
| 49 | + } |
| 50 | + |
| 51 | + large_cave_is_flat = (ps->range(0,1) == 0); |
| 52 | +} |
| 53 | + |
| 54 | + |
| 55 | +void CaveV6::makeCave(v3s16 nmin, v3s16 nmax, int max_stone_height) { |
| 56 | + node_min = nmin; |
| 57 | + node_max = nmax; |
| 58 | + max_stone_y = max_stone_height; |
| 59 | + main_direction = v3f(0, 0, 0); |
| 60 | + |
| 61 | + // Allowed route area size in nodes |
| 62 | + ar = node_max - node_min + v3s16(1, 1, 1); |
| 63 | + // Area starting point in nodes |
| 64 | + of = node_min; |
| 65 | + |
| 66 | + // Allow a bit more |
| 67 | + //(this should be more than the maximum radius of the tunnel) |
| 68 | + const s16 max_spread_amount = MAP_BLOCKSIZE; |
| 69 | + s16 insure = 10; |
| 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; |
| 73 | + |
| 74 | + route_y_min = 0; |
| 75 | + // Allow half a diameter + 7 over stone surface |
| 76 | + route_y_max = -of.Y + max_stone_y + max_tunnel_diameter / 2 + 7; |
| 77 | + |
| 78 | + // Limit maximum to area |
| 79 | + route_y_max = rangelim(route_y_max, 0, ar.Y - 1); |
| 80 | + |
| 81 | + if (large_cave) { |
| 82 | + s16 min = 0; |
| 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; |
| 86 | + } |
| 87 | + route_y_min = ps->range(min, min + max_tunnel_diameter); |
| 88 | + route_y_min = rangelim(route_y_min, 0, route_y_max); |
| 89 | + } |
| 90 | + |
| 91 | + s16 route_start_y_min = route_y_min; |
| 92 | + s16 route_start_y_max = route_y_max; |
| 93 | + |
| 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); |
| 96 | + |
| 97 | + // Randomize starting position |
| 98 | + orp = v3f( |
| 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 |
| 102 | + ); |
| 103 | + |
| 104 | + // Generate some tunnel starting from orp |
| 105 | + for (u16 j = 0; j < tunnel_routepoints; j++) |
| 106 | + makeTunnel(j % dswitchint == 0); |
| 107 | +} |
| 108 | + |
| 109 | + |
| 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 |
| 116 | + ); |
| 117 | + main_direction *= (float)ps->range(0, 10) / 10; |
| 118 | + } |
| 119 | + |
| 120 | + // Randomize size |
| 121 | + s16 min_d = min_tunnel_diameter; |
| 122 | + s16 max_d = max_tunnel_diameter; |
| 123 | + rs = ps->range(min_d, max_d); |
| 124 | + |
| 125 | + v3s16 maxlen; |
| 126 | + if (large_cave) { |
| 127 | + maxlen = v3s16( |
| 128 | + rs * part_max_length_rs, |
| 129 | + rs * part_max_length_rs / 2, |
| 130 | + rs * part_max_length_rs |
| 131 | + ); |
| 132 | + } else { |
| 133 | + maxlen = v3s16( |
| 134 | + rs * part_max_length_rs, |
| 135 | + ps->range(1, rs * part_max_length_rs), |
| 136 | + rs * part_max_length_rs |
| 137 | + ); |
| 138 | + } |
| 139 | + |
| 140 | + v3f vec( |
| 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 |
| 144 | + ); |
| 145 | + |
| 146 | + // Jump downward sometimes |
| 147 | + if (!large_cave && ps->range(0, 12) == 0) { |
| 148 | + vec = v3f( |
| 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 |
| 152 | + ); |
| 153 | + } |
| 154 | + |
| 155 | + /*if(large_cave){ |
| 156 | + v3f p = orp + vec; |
| 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; |
| 160 | + }*/ |
| 161 | + |
| 162 | + vec += main_direction; |
| 163 | + |
| 164 | + v3f rp = orp + vec; |
| 165 | + if (rp.X < 0) |
| 166 | + rp.X = 0; |
| 167 | + else if (rp.X >= ar.X) |
| 168 | + rp.X = ar.X - 1; |
| 169 | + |
| 170 | + if (rp.Y < route_y_min) |
| 171 | + rp.Y = route_y_min; |
| 172 | + else if (rp.Y >= route_y_max) |
| 173 | + rp.Y = route_y_max - 1; |
| 174 | + |
| 175 | + if (rp.Z < 0) |
| 176 | + rp.Z = 0; |
| 177 | + else if (rp.Z >= ar.Z) |
| 178 | + rp.Z = ar.Z - 1; |
| 179 | + |
| 180 | + vec = rp - orp; |
| 181 | + |
| 182 | + float veclen = vec.getLength(); |
| 183 | + // As odd as it sounds, veclen is *exactly* 0.0 sometimes, causing a FPE |
| 184 | + if (veclen == 0.0) |
| 185 | + veclen = 1.0; |
| 186 | + |
| 187 | + // Every second section is rough |
| 188 | + bool randomize_xz = (ps2->range(1, 2) == 1); |
| 189 | + |
| 190 | + // Carve routes |
| 191 | + for (float f = 0; f < 1.0; f += 1.0 / veclen) |
| 192 | + carveRoute(vec, f, randomize_xz); |
| 193 | + |
| 194 | + orp = rp; |
| 195 | +} |
| 196 | + |
| 197 | + |
| 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); |
| 202 | + |
| 203 | + v3s16 startp(orp.X, orp.Y, orp.Z); |
| 204 | + startp += of; |
| 205 | + |
| 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); |
| 210 | + |
| 211 | + s16 d0 = -rs/2; |
| 212 | + s16 d1 = d0 + rs; |
| 213 | + if (randomize_xz) { |
| 214 | + d0 += ps->range(-1, 1); |
| 215 | + d1 += ps->range(-1, 1); |
| 216 | + } |
| 217 | + |
| 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) |
| 226 | + // continue; |
| 227 | + |
| 228 | + if (large_cave_is_flat) { |
| 229 | + // Make large caves not so tall |
| 230 | + if (rs > 7 && abs(y0) >= rs / 3) |
| 231 | + continue; |
| 232 | + } |
| 233 | + |
| 234 | + v3s16 p(cp.X + x0, cp.Y + y0, cp.Z + z0); |
| 235 | + p += of; |
| 236 | + |
| 237 | + if (vm->m_area.contains(p) == false) |
| 238 | + continue; |
| 239 | + |
| 240 | + u32 i = vm->m_area.index(p); |
| 241 | + |
| 242 | + if (large_cave) { |
| 243 | + int full_ymin = node_min.Y - MAP_BLOCKSIZE; |
| 244 | + int full_ymax = node_max.Y + MAP_BLOCKSIZE; |
| 245 | + |
| 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; |
| 250 | + } else { |
| 251 | + vm->m_data[i] = airnode; |
| 252 | + } |
| 253 | + } else { |
| 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) |
| 258 | + continue; |
| 259 | + |
| 260 | + vm->m_data[i] = airnode; |
| 261 | + vm->m_flags[i] |= VMANIP_FLAG_CAVE; |
| 262 | + } |
| 263 | + } |
| 264 | + } |
| 265 | + } |
| 266 | +} |
0 commit comments