Improve cutting leaves workaround

This commit is contained in:
Hybrid Dog 2017-03-23 14:38:22 +01:00
parent 13889abee8
commit 337756eb41
3 changed files with 100 additions and 42 deletions

View File

@ -246,48 +246,20 @@ end
-- adds the stem to the trunks -- adds the stem to the trunks
local function get_stem(trunktop_ps, trunks, tr, head_ps) local function get_stem(trunktop_ps, trunks, tr, head_ps)
local trees = tr.trees if tr.cutting_leaves then
if not tr.cutting_leaves then treecapitator.moretrees34(trunktop_ps, trunks, tr, head_ps,
for i = 1,#trunktop_ps do get_node, is_trunk_of_tree)
local pos = trunktop_ps[i]
local node = get_node(pos)
while is_trunk_of_tree(trees, node) do
trunks[#trunks+1] = {pos, node}
pos = {x=pos.x, y=pos.y+1, z=pos.z}
node = get_node(pos)
end
-- renew trunk top position
pos.y = pos.y-1
trunktop_ps[i] = pos
end
return return
end end
for i = 1,#trunktop_ps do for i = 1,#trunktop_ps do
local pos = trunktop_ps[i] local pos = trunktop_ps[i]
local node = get_node(pos) local node = get_node(pos)
while true do while is_trunk_of_tree(tr.trees, node) do
while is_trunk_of_tree(trees, node) do trunks[#trunks+1] = {pos, node}
trunks[#trunks+1] = {pos, node} pos = {x=pos.x, y=pos.y+1, z=pos.z}
pos = {x=pos.x, y=pos.y+1, z=pos.z} node = get_node(pos)
node = get_node(pos)
end
local p_nxtt = {x=pos.x, y=pos.y+1, z=pos.z}
local n_nxtt = get_node(p_nxtt)
if not is_trunk_of_tree(trees, n_nxtt)
or not is_trunk_of_tree(trees, get_node{
x = pos.x,
y = pos.y+2,
z = pos.z
}) then
break
end
-- add leaves or whatsoever to the head positions
head_ps[#head_ps+1] = pos
pos = p_nxtt
node = n_nxtt
end end
-- renew trunk top position -- renew trunk top position
pos.y = pos.y-1 pos.y = pos.y-1
trunktop_ps[i] = pos trunktop_ps[i] = pos
@ -455,9 +427,7 @@ function capitate_funcs.default(pos, tr, _, digger)
end end
elseif is_trunk elseif is_trunk
and tr.trunk_fruit_vertical and tr.trunk_fruit_vertical
and fruits ^ nodename and fruits ^ nodename then
and not trees ^ get_node{x=p.x, y=p.y+1, z=p.z}.name
and not trees ^ get_node{x=p.x, y=p.y-1, z=p.z}.name then
trunks[#trunks+1] = {p, node} trunks[#trunks+1] = {p, node}
end end
end end
@ -745,6 +715,7 @@ end
local path = minetest.get_modpath"treecapitator" .. DIR_DELIM local path = minetest.get_modpath"treecapitator" .. DIR_DELIM
dofile(path .. "api.lua") dofile(path .. "api.lua")
dofile(path .. "trees.lua") dofile(path .. "trees.lua")
dofile(path .. "moretrees34.lua")
local time = (minetest.get_us_time() - load_time_start) / 1000000 local time = (minetest.get_us_time() - load_time_start) / 1000000

87
moretrees34.lua Normal file
View File

@ -0,0 +1,87 @@
-- This provides a workaround for the cutting leaves moretrees problem.
function treecapitator.moretrees34(trunktop_ps, trunks, tr, head_ps,
get_node, is_trunk_of_tree
)
local trees = tr.trees
for i = 1,#trunktop_ps do
-- add the usual trunks
local pos = trunktop_ps[i]
local node = get_node(pos)
while is_trunk_of_tree(trees, node) do
trunks[#trunks+1] = {pos, node}
pos = {x=pos.x, y=pos.y+1, z=pos.z}
node = get_node(pos)
end
-- meddle with the lacunarity
local ys = pos.y
local ye
local detected_trunks = {}
-- search upwards until the gap is big enough or the tree ended
local foundleaves = 0
while true do
if is_trunk_of_tree(trees, node) then
foundleaves = 0
detected_trunks[pos.y] = node
pos.y = pos.y+1
node = get_node(pos)
elseif tr.leaves ^ node.name
or tr.fruits ^ node.name then
foundleaves = foundleaves+1
if foundleaves > tr.cutting_leaves then
-- cutting leaves count exceeded
ye = pos.y-foundleaves
break
end
pos.y = pos.y+1
node = get_node(pos)
else
-- above the tree
ye = pos.y-1
break
end
end
-- search downwards until enough trunks are found above each other
-- or no such trunks are found
local ytop = ys-1
local y = ye
local last_test = ys + tr.stem_height_min
while y >= last_test do
if detected_trunks[y] then
local too_short
for ty = y - tr.stem_height_min + 1, y-1 do
if not detected_trunks[y] then
too_short = true
y = ty-1
break
end
end
if not too_short then
-- upper end found
ytop = y
break
end
end
y = y-1
end
if ytop >= ys then
-- add trunks and leaves/fruits
for y = ys, ytop do
local p = {x=pos.x, y=y, z=pos.z}
if detected_trunks[y] then
trunks[#trunks+1] = {p, detected_trunks[y]}
else
head_ps[#head_ps+1] = p
end
end
end
-- renew trunk top position
pos.y = ytop
trunktop_ps[i] = pos
end
end

View File

@ -153,9 +153,9 @@ and minetest.get_modpath("moretrees") then
trunk_fruit_vertical = true, trunk_fruit_vertical = true,
cutting_leaves = 3, cutting_leaves = 3,
stem_height_min = 4, stem_height_min = 4,
range = 6, range = 8,
range_down = 11, range_down = 13,
range_up = 4, range_up = 10,
} }
treecapitator.register_tree{ treecapitator.register_tree{