add simple method for decorating native tunnels

This commit is contained in:
FaceDeer 2017-03-22 00:36:25 -06:00
parent b77e297302
commit 5b714d79d5

119
init.lua
View File

@ -6,9 +6,6 @@
subterrane = {} --create a container for functions and constants
-- set subterrane.mitigate_lava to true to attempt to mitigate lava spilling into the caves
-- set subterrane.get_param2_data to true to make this mod read and set param2 data (shaves a few milliseconds off when you don't, so mods that don't make use of this should leave this unset)
--grab a shorthand for the filepath of the mod
local modpath = minetest.get_modpath(minetest.get_current_modname())
@ -103,9 +100,7 @@ function subterrane:register_cave_layer(cave_layer_def)
local vm, emin, emax = minetest.get_mapgen_object("voxelmanip")
local area = VoxelArea:new{MinEdge=emin, MaxEdge=emax}
vm:get_data(data)
if subterrane.get_param2_data then
vm:get_param2_data(data_param2)
end
vm:get_param2_data(data_param2)
local biomemap = minetest.get_mapgen_object("biomemap")
@ -219,10 +214,8 @@ function subterrane:register_cave_layer(cave_layer_def)
biome._subterrane_floor_decor(area, data, ai, vi, bi, data_param2)
end
elseif (biome._subterrane_cave_floor_decor or biome._subterrane_cave_ceiling_decor)
and (nvals_cave[index_3d] + nvals_wave[index_3d])/2 <= tcave --if node falls outside cave threshold
then
-- decorate other "native" caves and tunnels
elseif (nvals_cave[index_3d] + nvals_wave[index_3d])/2 <= tcave then --if node falls outside cave threshold
-- decorate other "native" caves and tunnels
if biome._subterrane_cave_fill_node then
cave_fill_node = biome._subterrane_cave_fill_node
if data[vi] == c_air then
@ -261,9 +254,7 @@ function subterrane:register_cave_layer(cave_layer_def)
--send data back to voxelmanip
vm:set_data(data)
if subterrane.get_param2_data then
vm:set_param2_data(data_param2)
end
vm:set_param2_data(data_param2)
--calc lighting
vm:set_lighting({day = 0, night = 0})
vm:calc_lighting()
@ -273,7 +264,109 @@ function subterrane:register_cave_layer(cave_layer_def)
local chunk_generation_time = math.ceil((os.clock() - t_start) * 1000) --grab how long it took
print ("[subterrane] "..chunk_generation_time.." ms") --tell people how long
end)
end
function subterrane:register_cave_decor(minimum_depth, maximum_depth)
-- On generated function
minetest.register_on_generated(function(minp, maxp, seed)
--if out of range of cave definition limits, abort
if minp.y > minimum_depth or maxp.y < maximum_depth then
return
end
-- Create a table of biome ids for use with the biomemap.
if not subterrane.biome_ids then
subterrane.biome_ids = {}
for name, desc in pairs(minetest.registered_biomes) do
local i = minetest.get_biome_id(desc.name)
subterrane.biome_ids[i] = desc.name
end
end
--easy reference to commonly used values
local t_start = os.clock()
local x_max = maxp.x
local y_max = maxp.y
local z_max = maxp.z
local x_min = minp.x
local y_min = minp.y
local z_min = minp.z
print ("[subterrane] chunk minp ("..x_min.." "..y_min.." "..z_min..")") --tell people you are generating a chunk
local vm, emin, emax = minetest.get_mapgen_object("voxelmanip")
local area = VoxelArea:new{MinEdge=emin, MaxEdge=emax}
vm:get_data(data)
vm:get_param2_data(data_param2)
local biomemap = minetest.get_mapgen_object("biomemap")
local sidelen = x_max - x_min + 1 --length of a mapblock
local index_3d = 1 --3D node index
local index_2d = 1 --2D node index
for z = z_min, z_max do -- for each xy plane progressing northwards
--decoration loop, places nodes on floor and ceiling
for y = y_min, y_max do -- for each x row progressing upwards
local vi = area:index(x_min, y, z)
for x = x_min, x_max do -- for each node do
local biome_name = subterrane.biome_ids[biomemap[index_2d]]
local biome = minetest.registered_biomes[biome_name]
local cave_fill_node = c_air
if biome then
-- decorate "native" caves and tunnels
if biome._subterrane_cave_fill_node then
cave_fill_node = biome._subterrane_cave_fill_node
if data[vi] == c_air then
data[vi] = cave_fill_node
end
end
local ai = area:index(x,y+1,z) --above index
local bi = area:index(x,y-1,z) --below index
if biome._subterrane_cave_ceiling_decor
and data[ai] ~= cave_fill_node
and data[vi] == cave_fill_node
and y < y_max
then --ceiling
biome._subterrane_cave_ceiling_decor(area, data, ai, vi, bi, data_param2)
end
--ground
if biome._subterrane_cave_floor_decor
and data[bi] ~= cave_fill_node
and data[vi] == cave_fill_node
and y > y_min
then --ground
biome._subterrane_cave_floor_decor(area, data, ai, vi, bi, data_param2)
end
end
index_3d = index_3d + 1
index_2d = index_2d + 1
vi = vi + 1
end
index_2d = index_2d - sidelen --shift the 2D index back
end
index_2d = index_2d + sidelen --shift the 2D index up a layer
end
--send data back to voxelmanip
vm:set_data(data)
vm:set_param2_data(data_param2)
--calc lighting
vm:set_lighting({day = 0, night = 0})
vm:calc_lighting()
--write it to world
vm:write_to_map(data)
local chunk_generation_time = math.ceil((os.clock() - t_start) * 1000) --grab how long it took
print ("[subterrane] "..chunk_generation_time.." ms") --tell people how long
end)
end
print("[Subterrane] loaded!")