modularize get_building function from mapgen helper

This commit is contained in:
BuckarooBanzay 2024-06-14 07:24:08 +02:00
parent 27bc4f5fe3
commit 5b3cbd2513

View File

@ -104,25 +104,7 @@ function building_lib.create_mapgen(opts)
return mapblock_pos.y == opts.water_level and height <= mapblock_pos.y
end
local function on_generated(minp, maxp)
local min_mapblock = mapblock_lib.get_mapblock(minp)
local max_mapblock = mapblock_lib.get_mapblock(maxp)
if max_mapblock.y < opts.from_y or min_mapblock.y > opts.to_y then
-- check broad y-range
return
end
for x=min_mapblock.x,max_mapblock.x do
for y=min_mapblock.y,max_mapblock.y do
for z=min_mapblock.z,max_mapblock.z do
if y < opts.from_y or y > opts.to_y then
-- check exact y-range
break
end
local mapblock_pos = { x=x, y=y, z=z }
local function get_building(mapblock_pos)
local height = get_height(mapblock_pos)
local temperature, humidity = get_temperature_humidity(mapblock_pos)
@ -130,10 +112,10 @@ function building_lib.create_mapgen(opts)
if is_water(mapblock_pos) then
-- nothing above, place water building
building_lib.build_mapgen(mapblock_pos, biome.buildings.water, 0)
return biome.buildings.water, 0
elseif mapblock_pos.y < height or mapblock_pos.y < opts.water_level then
-- underground
building_lib.build_mapgen(mapblock_pos, biome.buildings.underground, 0)
return biome.buildings.underground, 0
elseif mapblock_pos.y == height then
-- surface
@ -184,6 +166,31 @@ function building_lib.create_mapgen(opts)
rotation = 0
end
return building_name, rotation
end
end
local function on_generated(minp, maxp)
local min_mapblock = mapblock_lib.get_mapblock(minp)
local max_mapblock = mapblock_lib.get_mapblock(maxp)
if max_mapblock.y < opts.from_y or min_mapblock.y > opts.to_y then
-- check broad y-range
return
end
for x=min_mapblock.x,max_mapblock.x do
for y=min_mapblock.y,max_mapblock.y do
for z=min_mapblock.z,max_mapblock.z do
if y < opts.from_y or y > opts.to_y then
-- check exact y-range
break
end
local mapblock_pos = { x=x, y=y, z=z }
local building_name, rotation = get_building(mapblock_pos)
if building_name then
building_lib.build_mapgen(mapblock_pos, building_name, rotation)
end
end --z
@ -191,7 +198,9 @@ function building_lib.create_mapgen(opts)
end --x
end
-- exposed mapgen helper functions
return {
-- main function for "minetest.register_on_generated"
on_generated = on_generated,
get_height = get_height,
get_temperature_humidity = get_temperature_humidity,
@ -199,6 +208,7 @@ function building_lib.create_mapgen(opts)
local temperature, humidity = get_temperature_humidity(mapblock_pos)
return select_biome(opts.biomes, temperature, humidity)
end,
is_water = is_water
is_water = is_water,
get_building = get_building
}
end