Skip to content

Commit

Permalink
add an API to allow other mods to test positions for the presence of …
Browse files Browse the repository at this point in the history
…caverns
  • Loading branch information
FaceDeer committed Apr 10, 2021
1 parent 6cc2fe8 commit 1f385fc
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions init.lua
Expand Up @@ -39,6 +39,7 @@ local modpath = minetest.get_modpath(minetest.get_current_modname())
dofile(modpath.."/defaults.lua")
dofile(modpath.."/features.lua") -- some generic cave features useful for a variety of mapgens
dofile(modpath.."/player_spawn.lua") -- Function for spawning a player in a giant cavern
dofile(modpath.."/test_pos.lua") -- Function other mapgens can use to test if a position is inside a cavern
dofile(modpath.."/legacy.lua") -- contains old node definitions and functions, will be removed at some point in the future.

local disable_mapgen_caverns = function()
Expand Down
41 changes: 41 additions & 0 deletions test_pos.lua
@@ -0,0 +1,41 @@
-- If pos is located inside a cavern volume, returns the list of cavern definitions that
-- are responsible for that cavern volume. If not inside a cavern volume returns an empty list.

-- This is a somewhat expensive function, take care not to use it more than necessary
subterrane.is_in_cavern = function(pos)
local results = {}

for _, cave_layer_def in pairs(subterrane.registered_layers) do
local YMIN = cave_layer_def.y_min
local YMAX = cave_layer_def.y_max
local y = pos.y
if y <= YMAX and y >= YMIN then
local block_size = mapgen_helper.block_size
local BLEND = math.min(cave_layer_def.boundary_blend_range, (YMAX-YMIN)/2)
local TCAVE = cave_layer_def.cave_threshold
local np_cave = cave_layer_def.perlin_cave
local np_wave = cave_layer_def.perlin_wave
local y_blend_min = YMIN + BLEND * 1.5
local y_blend_max = YMAX - BLEND * 1.5
local nval_cave = minetest.get_perlin(np_cave):get_3d(pos) --cave noise for structure
local nval_wave = minetest.get_perlin(np_wave):get_3d(pos) --wavy structure of cavern ceilings and floors
nval_cave = (nval_cave + nval_wave)/2
local cave_local_threshold
if y < y_blend_min then
cave_local_threshold = TCAVE + ((y_blend_min - y) / BLEND) ^ 2
elseif y > y_blend_max then
cave_local_threshold = TCAVE + ((y - y_blend_max) / BLEND) ^ 2
else
cave_local_threshold = TCAVE
end
if cave_layer_def.double_frequency and nval_cave < 0 then
nval_cave = -nval_cave
end
if nval_cave > cave_local_threshold then
table.insert(results, cave_layer_def)
end
end
end

return results
end

0 comments on commit 1f385fc

Please sign in to comment.