fun_caves/cavegen.lua
2016-07-15 02:58:33 -05:00

131 lines
4.5 KiB
Lua

fun_caves.cave_width = 0.05 -- figurative width
local max_depth = 31000
fun_caves.cave_noise_1 = {offset = 0, scale = 1, seed = 3901, spread = {x = 40, y = 10, z = 40}, octaves = 3, persist = 1, lacunarity = 2}
fun_caves.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}
local ground_nodes = {}
ground_nodes[minetest.get_content_id('default:stone')] = true
ground_nodes[minetest.get_content_id('default:desert_stone')] = true
ground_nodes[minetest.get_content_id('default:sandstone')] = true
ground_nodes[minetest.get_content_id('default:dirt')] = true
ground_nodes[minetest.get_content_id('default:sand')] = true
ground_nodes[minetest.get_content_id('default:dirt_with_grass')] = true
ground_nodes[minetest.get_content_id('default:dirt_with_snow')] = true
ground_nodes[minetest.get_content_id('default:dirt_with_dry_grass')] = true
fun_caves.cavegen = function(minp, maxp, data, area, node, heightmap, underzone)
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 return_heightmap = false
if not heightmap then
return_heightmap = true
heightmap = {}
end
local cave_1 = minetest.get_perlin_map(fun_caves.cave_noise_1, map_max):get3dMap_flat(map_min)
local cave_2 = minetest.get_perlin_map(fun_caves.cave_noise_2, map_max):get3dMap_flat(map_min)
local 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 write = false
local index = 0
local index3d = 0
local cave_width = fun_caves.cave_width
local styx_sea_level = fun_caves.underzones['Styx'].sealevel
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-1, z)
local height = heightmap[index]
if height and height < maxp.y - 1 and height > minp.y then
--nop
else
height = - max_depth
local ivm2 = area:index(x, maxp.y + 8, z)
for y = maxp.y + 8, minp.y - 8, -1 do
if ground_nodes[data[ivm2]] then
height = (y < maxp.y + 8) and y or max_depth
break
end
ivm2 = ivm2 - area.ystride
end
heightmap[index] = height
end
local column = 0
if underzone then
if cave_3[index] < 30 then
column = 1
elseif cave_3[index] < 35 then
column = 2
end
end
for y = minp.y-1, maxp.y+1 do
if underzone and underzone.regular_columns and (x - minp.x) < 8 and (z - minp.z) < 8 then
data[ivm] = node[underzone.column_node]
write = true
elseif underzone and underzone.column_node and not underzone.regular_columns and column == 2 then
if underzone.column_node_rare and math.random(70) == 1 then
data[ivm] = node[underzone.column_node_rare]
else
data[ivm] = node[underzone.column_node]
end
write = true
elseif underzone and (y < underzone.ceiling - (underzone.vary and cave_3[index] or 0) and y > underzone.floor + (underzone.vary and cave_3[index] or 0)) then
if underzone.sealevel and y <= underzone.sealevel then
data[ivm] = node["default:water_source"]
else
data[ivm] = node["air"]
end
write = true
elseif underzone and (y < underzone.ceiling + 10 - (underzone.vary and cave_3[index] or 0) and y > underzone.floor - 10 + (underzone.vary and cave_3[index] or 0)) then
-- nop
elseif ((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
if y <= styx_sea_level then
data[ivm] = node["default:water_source"]
else
data[ivm] = node["air"]
end
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
if return_heightmap then
return write, heightmap
else
return write, nil
end
end