From 3c38b5844a352798911d39a7849fab128781e31b Mon Sep 17 00:00:00 2001 From: Bram van den Heuvel Date: Tue, 30 Apr 2024 17:00:37 +0200 Subject: [PATCH] Refactor decorations Split them into pre- and post-data voxelmanip operations --- init.lua | 138 ++++++++++++++++++++++++++----------------------------- 1 file changed, 64 insertions(+), 74 deletions(-) diff --git a/init.lua b/init.lua index 04d56b1..37999b0 100644 --- a/init.lua +++ b/init.lua @@ -298,6 +298,7 @@ function internal.clean_deco_def(def) decoration = def.decoration, schematic = def.schematic, fill_ratio = def.fill_ratio, + biomes = def.biomes, } if def.place_on == "floor" or def.place_on == "ceiling" then @@ -782,7 +783,7 @@ function internal.generate_caves(data, minp, maxp) end end) - return vmanip.arr, schems + return vmanip, schems end -- Get the noise params for the cave biome temperature. @@ -905,76 +906,51 @@ end -- minetest.chat_send_all(os.time() .. " - " .. text) -- end --- Place a table full of schematics into the world -function internal.place_schematics(schems) +-- Place all schematic decorations into the world +function internal.place_schematic_decorations(vmanip, schems) for _, schem in ipairs(schems) do local pos = schem.pos local deco = schem.deco - if deco.deco_type == "simple" then - local h_min = deco.height - local h_max = math.max(deco.height_max, deco.height) - local dy = deco.place_offset_y - 1 - - if deco.place_on == "floor" then - for y = 1, math.random(h_min, h_max), 1 do - - minetest.set_node( - { x = pos.x, y = pos.y + y + dy, z = pos.z }, - { name = deco.decoration } - ) - end - elseif deco.place_on == "ceiling" then - for y = 1, math.random(h_min, h_max), 1 do - - minetest.set_node( - { x = pos.x, y = pos.y - y - dy, z = pos.z }, - { name = deco.decoration } - ) - end - else - error("Unknown place_on parameter `" .. deco.place_on .. "` for simple decoration!") - end - elseif deco.deco_type == "schematic" then - local name = deco.schematic - local force_placement = true - if string.find("force_placement", deco.flags) then - force_placement = true - end - - if deco.place_on == "floor" then - local n = minetest.place_schematic( - pos, name, deco.rotation, deco.replacements, - force_placement, deco.flags - ) - - if type(n) == "nil" then - minetest.chat_send_all("Squeak could not load!") - else - minetest.chat_send_all("Placed a Squeak!") - if not tpd_yet then - minetest.get_player_by_name("singleplayer"):move_to(pos) - tpd_yet = true - minetest.chat_send_all(internal.pos_to_str(pos)) - end - end - elseif deco.place_on == "ceiling" then - -- Aim to place schematic against the ceiling, not through it - local h = minetest.read_schematic(name).size.y - local c_pos = { x = pos.x, y = pos.y - h, z = pos.z } - if string.find("place_center_y", deco.flags) then - c_pos = pos - end - - minetest.place_schematic( - pos, name, deco.rotation, deco.replacements, - force_placement, deco.flags - ) - else - error("Unknown place_on parameter `" .. deco.place_on .. "` for simple decoration!") - end + if deco.deco_type ~= "schematic" then else - error("Unknown decoration type `" .. deco.deco_type .. "`") + minetest.place_schematic_on_vmanip( + vmanip, pos, + deco.schematic, deco.rotation, deco.replacement, + true, deco.flags + ) + end + end +end + +-- Place all simple decorations into the world +function internal.place_simple_decorations(flat, schems) + for _, schem in ipairs(schems) do + if schem.deco.deco_type == "simple" then + local pos = schem.pos + local deco = schem.deco + + local i = flat:pos_to_index(pos) + + local dir = nil + + if deco.place_on == "floor" then + dir = flat.up + elseif deco.place_on == "ceiling" then + dir = flat.down + else + error("Invalid place_on value `" .. deco.place_on .. "`") + end + + local h_min = deco.height + local h_max = math.max(deco.height, deco.height_max) + + for dy = 0, math.random(h_min, h_max) - 1, 1 do + local li = i + dir * dy + + flat:set_index(li, minetest.get_content_id(deco.decoration)) + end + end end end @@ -1203,14 +1179,14 @@ minetest.register_on_generated(function(minp, maxp, blockseed) math.randomseed(blockseed) local vm = minetest.get_mapgen_object("voxelmanip") - local data = vm:get_data() + local flat, schems = internal.generate_caves(vm:get_data(), minp, maxp) - data, schems = internal.generate_caves(data, minp, maxp) + internal.place_simple_decorations(flat, schems) - vm:set_data(data) + vm:set_data(flat.arr) vm:write_to_map() - internal.place_schematics(schems) + internal.place_schematic_decorations(vm, schems) end) ------------------------------------------------------------------------------- ------------------------------------------------------------------------------- @@ -1317,8 +1293,8 @@ noordstar_caves.register_biome({ node_wall = "mcl_core:sand", node_roof = "mcl_ocean:sea_lantern", node_dust = "mcl_core:snow", - heat_point = 0, - humidity_point = 0, + heat_point = 80, + humidity_point = 80, }) noordstar_caves.register_biome({ name = "test2", @@ -1328,6 +1304,11 @@ noordstar_caves.register_biome({ heat_point = 100, humidity_point = 100, }) +-- noordstar_caves.register_biome({ +-- name = "test3", +-- heat_point = 50, +-- humidity_point = 50, +-- }) noordstar_caves.register_decoration({ deco_type = "simple", @@ -1338,11 +1319,20 @@ noordstar_caves.register_decoration({ height_max = 8, biomes = { "test" }, }) +noordstar_caves.register_decoration({ + deco_type = "simple", + place_on = "floor", + fill_ratio = 0.1, + decoration = "mcl_ocean:sea_lantern", + height = 1, + height_max = 4, + biomes = { "test2" }, +}) noordstar_caves.register_decoration({ deco_type = "schematic", place_on = "floor", fill_ratio = 0.005, - schematic = "test.mts", + schematic = squeak, rotation = "random", - place_offset_y = 5, + -- place_offset_y = 5, })