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

221 lines
6.9 KiB
Lua

-- Fun_Caves deco.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)
dofile(fun_caves_mod.path .. "/nodes.lua")
dofile(fun_caves_mod.path .. "/fungal_tree.lua")
local deco_depth = -30 -- place cave stuff this far beneath the surface
local water_level = 1
local fluid_compression = -200 -- the depth to start planting lava/water
local max_depth = 31000
local csize
local node_match_cache = {}
local biome_noise = {offset = 0.0, scale = 1.0, spread = {x = 400, y = 400, z = 400}, seed = 903, octaves = 3, persist = 0.5, lacunarity = 2.0}
-- Air needs to be placed prior to decorations.
fun_caves_mod.decogen = function(minp, maxp, data, area, node, heightmap)
if not (minp and maxp and data and area and node and type(data) == 'table' and fun_caves_mod.cave_biomes and fun_caves_mod.make_fungal_tree) then
return
end
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 biome_n = minetest.get_perlin_map(biome_noise, map_max):get3dMap_flat(map_min)
local write
local index = 0
local index3d = 0
local math_max = math.max
local math_min = math.min
local math_log = math.log
local math_random = math.random
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-1, maxp.y+1 do
if y <= height + deco_depth and (height < max_depth or y < 0) then
for deco_non_loop = 1, 1 do
if not (data[ivm] == node["air"] or data[ivm] == node["default:stone"]) then
break
end
local biome
local biome_val = biome_n[index3d]
-- Compress biomes at the surface to avoid fluids.
if y > fluid_compression then
biome_val = biome_val / math_max(1, math_log(y - fluid_compression))
end
for _, bi in pairs(fun_caves_mod.cave_biomes) do
if biome_val >= bi.biome_val_low and biome_val < bi.biome_val_high then
biome = bi
end
end
--biome = fun_caves_mod.cave_biomes['salt']
if not biome then
print(("* Fun Caves: Error in biome selection: %s"):format(biome_val))
biome = fun_caves_mod.cave_biomes['algae']
end
local node_below
if y > minp.y then
node_below = data[ivm - area.ystride]
end
local node_above = data[ivm + area.ystride]
if data[ivm] == node["default:stone"] then
if node_above == node["air"] and biome and biome.dirt and math_random(biome.dirt_chance) == 1 then
data[ivm] = node[biome.dirt]
write = true
break
end
local air_above = false
for i = 1, biome.stone_depth do
if data[ivm + area.ystride * i] == node["air"] then
air_above = true
end
end
if air_above then
if biome and biome.deco and math_random(biome.deco_chance) == 1 then
data[ivm] = node[biome.deco]
write = true
break
else
data[ivm] = node[biome.floor_node]
write = true
break
end
end
local air_below = false
for i = 1, biome.stone_depth do
if data[ivm - area.ystride * i] == node["air"] then
air_below = true
end
end
-- Prevent server-crashing sandslides.
if not air_above and biome.floor_node == "default:sand" then
data[ivm] = node["default:sandstone"]
write = true
break
end
if air_below then
if biome and biome.deco and math_random(biome.deco_chance) == 1 then
data[ivm] = node[biome.deco]
write = true
break
else
data[ivm] = node[biome.ceiling_node]
write = true
break
end
end
end
if data[ivm] == node["air"] and y < maxp.y then
-- hanging down
-- stone hasn't yet been changed
if biome and biome.stalactite and node_above == node["default:stone"] and math_random(biome.stalactite_chance) == 1 then
data[ivm] = node[biome.stalactite]
write = true
break
end
-- fluids
if y > minp.y and biome and biome.fluid and node_below == node[biome.floor_node] and math_random(biome.fluid_chance) == 1 then
data[ivm] = node[biome.fluid]
write = true
break
-- standing up
elseif node_below == node[biome.floor_node] and biome and biome.stalagmite and math_random(biome.stalagmite_chance) == 1 then
if type(biome.stalagmite) == 'table' then
data[ivm] = node[biome.stalagmite[math_random(#biome.stalagmite)]]
else
data[ivm] = node[biome.stalagmite]
end
write = true
break
-- vegetation
elseif node_below == node["default:dirt"] and biome and biome.fungi then
if math_random(10) == 1 then
data[ivm] = node["flowers:mushroom_red"]
write = true
break
elseif math_random(10) == 1 then
data[ivm] = node["flowers:mushroom_brown"]
write = true
break
elseif node_above == node["air"] and math_random(10) == 1 then
data[ivm] = node["fun_caves:giant_mushroom_stem"]
write = true
break
elseif math_random(30) == 1 then
local air_count = 0
local j
for i = 1, 12 do
j = ivm + area.ystride * i
if j <= #data and data[j] == node["air"] then
air_count = air_count + 1
end
end
if air_count > 5 then
fun_caves_mod.make_fungal_tree(data, area, ivm, math_random(2, math_min(air_count, 12)))
end
end
elseif node_below == node["fun_caves:giant_mushroom_stem"] and data[ivm - area.ystride * 2] == node["fun_caves:giant_mushroom_stem"] then
data[ivm] = node["fun_caves:giant_mushroom_cap"]
write = true
break
elseif node_below == node["fun_caves:giant_mushroom_stem"] then
if node_above == node["air"] and math_random(3) == 1 then
data[ivm] = node["fun_caves:giant_mushroom_stem"]
write = true
break
else
data[ivm] = node["fun_caves:huge_mushroom_cap"]
write = true
break
end
end
end
end
elseif y < height and data[ivm] == node["air"] and (data[ivm - area.ystride] == node['default:stone'] or data[ivm - area.ystride] == node['default:sandstone']) then
-- This just places non-abm dirt inside caves.
-- Its value is questionable.
data[ivm - area.ystride] = node["fun_caves:dirt"]
write = true
end
ivm = ivm + area.ystride
index3d = index3d + csize.x
end
end
end
return write
end