Prevent param2 overflow when adding stems. Fix #1490

This commit is contained in:
iliekprogrammar 2021-04-08 12:04:29 +08:00
parent 8e30bc8dbd
commit 2db0e176b3

View file

@ -1,6 +1,9 @@
-- TODO: whenever it becomes possible to fully implement kelp without the -- TODO: whenever it becomes possible to fully implement kelp without the
-- plantlike_rooted limitation, please update accordingly. -- plantlike_rooted limitation, please update accordingly.
-- --
-- TODO: whenever it becomes possible to make kelp grow infinitely without
-- resorting to making intermediate kelp stem node, please update accordingly.
--
-- TODO: In MC, you can't actually destroy kelp by bucket'ing water in the middle. -- TODO: In MC, you can't actually destroy kelp by bucket'ing water in the middle.
-- However, because of the plantlike_rooted hack, we'll just allow it for now. -- However, because of the plantlike_rooted hack, we'll just allow it for now.
@ -191,17 +194,18 @@ end
-- Converts param2 to kelp height. -- Converts param2 to kelp height.
-- For the special case where the max param2 is reached, interpret that as the
-- 16th kelp stem.
function kelp.get_height(param2) function kelp.get_height(param2)
return math_floor(param2 / 16) return param2 ~= 255 and math_floor(param2 / 16) or 16 -- 256/16
end end
-- Obtain pos and node of the tip of kelp. -- Obtain pos and node of the tip of kelp.
function kelp.get_tip(pos, param2) function kelp.get_tip(pos, height)
-- Optional params: param2 -- Optional params: height
local height = kelp.get_height(param2 or mt_get_node(pos).param2) local height = height or kelp.get_height(mt_get_node(pos).param2)
local pos_tip = {x=pos.x, y=pos.y, z=pos.z} local pos_tip = {x=pos.x, y=pos.y+height+1, z=pos.z}
pos_tip.y = pos_tip.y + height + 1
return pos_tip, mt_get_node(pos_tip), height return pos_tip, mt_get_node(pos_tip), height
end end
@ -227,7 +231,8 @@ end
-- Obtain next param2. -- Obtain next param2.
function kelp.next_param2(param2) function kelp.next_param2(param2)
return param2+16 - param2 % 16 -- param2 max value is 255, so adding to 256 causes overflow.
return math_min(param2+16 - param2 % 16, 255);
end end
@ -526,7 +531,7 @@ function kelp.kelp_on_place(itemstack, placer, pointed_thing)
-- When placed on kelp. -- When placed on kelp.
if mt_get_item_group(nu_name, "kelp") == 1 then if mt_get_item_group(nu_name, "kelp") == 1 then
pos_tip,node_tip = kelp.get_tip(pos_under, node_under.param2) pos_tip,node_tip = kelp.get_tip(pos_under, kelp.get_height(node_under.param2))
def_tip = mt_registered_nodes[node_tip.name] def_tip = mt_registered_nodes[node_tip.name]
-- When placed on surface. -- When placed on surface.