2022-09-18 23:18:03 +00:00
|
|
|
local wea_c = worldeditadditions_core
|
|
|
|
local Vector3 = wea_c.Vector3
|
|
|
|
|
2020-09-13 14:43:49 +00:00
|
|
|
--- Overlap command. Places a specified node on top of each column.
|
|
|
|
-- @module worldeditadditions.layers
|
|
|
|
|
|
|
|
|
2020-09-14 00:19:15 +00:00
|
|
|
function worldeditadditions.forest(pos1, pos2, density_multiplier, sapling_weights)
|
2022-09-18 23:18:03 +00:00
|
|
|
pos1, pos2 = Vector3.sort(pos1, pos2)
|
2020-09-13 14:43:49 +00:00
|
|
|
|
2020-09-14 00:19:15 +00:00
|
|
|
local weight_total = 0
|
|
|
|
for _name,weight in pairs(sapling_weights) do
|
|
|
|
weight_total = weight_total + weight
|
|
|
|
end
|
|
|
|
|
|
|
|
sapling_weights["air"] = math.ceil(weight_total * 100 * 1/density_multiplier)
|
|
|
|
|
2020-09-13 14:43:49 +00:00
|
|
|
-- This command requires the bonemeal mod to be installed
|
|
|
|
-- We check here too because other mods might call this function directly and bypass the chat command system
|
|
|
|
if not minetest.get_modpath("bonemeal") then
|
|
|
|
return false, "Bonemeal mod not loaded"
|
|
|
|
end
|
|
|
|
|
2020-09-14 00:19:15 +00:00
|
|
|
local overlay_stats = worldeditadditions.overlay(pos1, pos2, sapling_weights)
|
2020-09-13 14:43:49 +00:00
|
|
|
|
|
|
|
-- Fetch the nodes in the specified area
|
|
|
|
local manip, area = worldedit.manip_helpers.init(pos1, pos2)
|
|
|
|
local data = manip:get_data()
|
|
|
|
local id_air = minetest.get_content_id("air")
|
|
|
|
|
|
|
|
local group_cache = {}
|
2020-09-14 00:19:15 +00:00
|
|
|
local stats = { attempts = {}, failures = 0, placed = {} }
|
2020-09-13 14:43:49 +00:00
|
|
|
for z = pos2.z, pos1.z, -1 do
|
|
|
|
for x = pos2.x, pos1.x, -1 do
|
|
|
|
for y = pos2.y, pos1.y, -1 do
|
|
|
|
local i = area:index(x, y, z)
|
2020-09-14 00:19:15 +00:00
|
|
|
local node_id = data[i]
|
|
|
|
if not group_cache[node_id] then
|
2022-09-18 23:18:03 +00:00
|
|
|
group_cache[node_id] = wea_c.is_sapling(node_id)
|
2020-09-13 14:43:49 +00:00
|
|
|
end
|
|
|
|
|
2020-09-14 00:19:15 +00:00
|
|
|
if group_cache[node_id] then
|
2020-09-13 14:43:49 +00:00
|
|
|
local did_grow = false
|
2020-09-14 00:19:15 +00:00
|
|
|
local new_id_at_pos
|
|
|
|
local new_name_at_pos
|
2021-07-30 18:45:09 +00:00
|
|
|
for attempt_number=1,100 do
|
2020-09-13 14:43:49 +00:00
|
|
|
bonemeal:on_use(
|
|
|
|
{ x = x, y = y, z = z },
|
|
|
|
4,
|
|
|
|
nil
|
|
|
|
)
|
|
|
|
|
2020-09-14 00:19:15 +00:00
|
|
|
new_name_at_pos = minetest.get_node({ z = z, y = y, x = x }).name
|
|
|
|
new_id_at_pos = minetest.get_content_id(new_name_at_pos)
|
2020-09-13 21:57:26 +00:00
|
|
|
if not group_cache[new_id_at_pos] then
|
2022-09-18 23:18:03 +00:00
|
|
|
group_cache[new_id_at_pos] = wea_c.is_sapling(new_id_at_pos)
|
2020-09-13 14:43:49 +00:00
|
|
|
end
|
|
|
|
if not group_cache[new_id_at_pos] then
|
|
|
|
did_grow = true
|
2020-09-14 00:19:15 +00:00
|
|
|
-- Log the number of attempts it took to grow
|
2021-07-30 18:45:09 +00:00
|
|
|
table.insert(stats.attempts, attempt_number)
|
2020-09-14 00:19:15 +00:00
|
|
|
-- Update the running total of saplings that grew
|
|
|
|
if not stats.placed[node_id] then
|
|
|
|
stats.placed[node_id] = 0
|
|
|
|
end
|
|
|
|
stats.placed[node_id] = stats.placed[node_id] + 1
|
2020-09-20 21:07:47 +00:00
|
|
|
-- print("incrementing id", node_id, "to", stats.placed[node_id])
|
2020-09-13 14:43:49 +00:00
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if not did_grow then
|
2021-07-04 13:35:02 +00:00
|
|
|
-- print("[//forest] Failed to grow sapling, detected node id", new_id_at_pos, "name", new_name_at_pos, "was originally", minetest.get_name_from_content_id(node_id))
|
2020-09-13 14:43:49 +00:00
|
|
|
-- We can't set it to air here because then when we save back we would overwrite all the newly grown trees
|
|
|
|
stats.failures = stats.failures + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Re-fetch the Voxel Manipulator to accountn for the new trees
|
|
|
|
manip, area = worldedit.manip_helpers.init(pos1, pos2)
|
|
|
|
data = manip:get_data()
|
|
|
|
|
|
|
|
for z = pos2.z, pos1.z, -1 do
|
|
|
|
for x = pos2.x, pos1.x, -1 do
|
|
|
|
for y = pos2.y, pos1.y, -1 do
|
|
|
|
local i = area:index(x, y, z)
|
2020-09-13 21:57:26 +00:00
|
|
|
if not group_cache[data[i]] then
|
2022-09-18 23:18:03 +00:00
|
|
|
group_cache[data[i]] = wea_c.is_sapling(data[i])
|
2020-09-13 14:43:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if group_cache[data[i]] then
|
2020-09-14 00:19:15 +00:00
|
|
|
data[i] = id_air
|
2020-09-13 14:43:49 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-14 00:19:15 +00:00
|
|
|
stats.successes = #stats.attempts
|
2022-09-18 23:18:03 +00:00
|
|
|
stats.attempts_avg = wea_c.average(stats.attempts)
|
2020-09-13 14:43:49 +00:00
|
|
|
|
|
|
|
-- Save the modified nodes back to disk & return
|
2020-09-14 12:43:05 +00:00
|
|
|
worldedit.manip_helpers.finish(manip, data)
|
2020-09-13 14:43:49 +00:00
|
|
|
return true, stats
|
|
|
|
end
|