fun_caves/cavegen.lua
2016-08-11 00:13:37 -05:00

139 lines
4.7 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}
fun_caves.cavegen = function(minp, maxp, data, area, node, heightmap, underzone, ground_nodes)
if not (minp and maxp and data and area and node and type(data) == 'table' and fun_caves.underzones) 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
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 crater
if not underzone and math.random(10) == 1 then
crater = {
x = minp.x + math.random(math.floor(csize.x / 2)) + math.floor(csize.x / 4),
y = 15,
z = minp.z + math.random(math.floor(csize.z / 2)) + math.floor(csize.z / 4),
}
end
local crater_min = 33000
for z = minp.z, maxp.z do
for x = minp.x, maxp.x do
index = index + 1
local height = heightmap[index]
if crater then
local dist = crater.y - math.sqrt((x - crater.x) ^ 2 + (z - crater.z) ^ 2)
if dist > 0 and (height > maxp.y - 20 or height < minp.y + crater.y) then
crater = nil
end
if crater and dist > 0 and dist <= 1 and crater_min > height then
crater_min = height
end
end
end
end
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]
local column = 0
if underzone then
if cave_3[index] < 30 then
column = 1
elseif cave_3[index] < 35 then
column = 2
end
end
local dist
if crater then
dist = crater.y - math.sqrt((x - crater.x) ^ 2 + (z - crater.z) ^ 2)
end
for y = minp.y-1, maxp.y+1 do
if crater and dist and dist > 0 then
if y > crater_min - 1.5 * dist and y < maxp.y - 1.5 * dist then
data[ivm] = data[ivm + (math.floor((height - crater_min) * dist / crater.y) + math.ceil(dist / 2)) * area.ystride]
end
if y > crater_min - dist and ground_nodes[data[ivm]] and math.random(300) == 1 then
data[ivm] = node['fun_caves:stone_with_meteoritic_iron']
end
end
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
return write
end