fun_caves/cavegen.lua
2017-01-24 19:54:46 -06:00

66 lines
2.4 KiB
Lua

-- Fun_Caves cavegen.lua
-- Copyright Duane Robertson (duane@duanerobertson.com), 2017
-- Distributed under the LGPLv2.1 (https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html)
fun_caves_mod.cave_width = 0.05 -- figurative width
local max_depth = 31000
fun_caves_mod.cave_noise_1 = {offset = 0, scale = 1, seed = 3901, spread = {x = 40, y = 10, z = 40}, octaves = 3, persist = 1, lacunarity = 2}
fun_caves_mod.cave_noise_2 = {offset = 0, scale = 1, seed = -8402, spread = {x = 40, y = 20, z = 40}, octaves = 3, persist = 1, lacunarity = 2}
local cave_noise_3 = {offset = 15, scale = 10, seed = 3721, spread = {x = 40, y = 40, z = 40}, octaves = 3, persist = 1, lacunarity = 2}
fun_caves_mod.cavegen = function(minp, maxp, data, area, node, heightmap)
if not (minp and maxp and data and area and node and type(data) == 'table') then
return
end
local csize = vector.add(vector.subtract(maxp, minp), 1)
local map_max = {x = csize.x, y = csize.y + 2, z = csize.z}
local map_min = {x = minp.x, y = minp.y - 1, z = minp.z}
local cave_1, cave_2, cave_3
cave_1 = minetest.get_perlin_map(fun_caves_mod.cave_noise_1, map_max):get3dMap_flat(map_min)
cave_2 = minetest.get_perlin_map(fun_caves_mod.cave_noise_2, map_max):get3dMap_flat(map_min)
cave_3 = minetest.get_perlin_map(cave_noise_3, {x=csize.x, y=csize.z}):get2dMap_flat({x=minp.x, y=minp.z})
if not (cave_1 and cave_2 and cave_3) then
return
end
local index = 0
local index3d = 0
local cave_width = fun_caves_mod.cave_width
local write
for z = minp.z, maxp.z do
for x = minp.x, maxp.x do
index = index + 1
index3d = (z - minp.z) * (csize.y + 2) * csize.x + (x - minp.x) + 1
local ivm = area:index(x, minp.y, z)
local height = heightmap[index]
for y = minp.y, maxp.y do
if ((y <= maxp.y and y >= minp.y) or (data[ivm] == node['default:stone'])) and y < height - cave_3[index] and cave_1[index3d] * cave_2[index3d] > cave_width then
data[ivm] = node["air"]
write = true
if y > 0 and cave_3[index] < 1 and y == height then
-- Clear the air above a cave mouth.
local ivm2 = ivm
for y2 = y + 1, maxp.y + 8 do
ivm2 = ivm2 + area.ystride
if data[ivm2] ~= node["default:water_source"] then
data[ivm2] = node["air"]
write = true
end
end
end
end
ivm = ivm + area.ystride
index3d = index3d + csize.x
end
end
end
return write
end