1
0
Fork 0

optimized out some for loops, cleaned up code for bamboo placement.

tested the changes.
This commit is contained in:
Michieal 2023-01-03 18:40:40 -05:00
parent 50e50e2904
commit d1a017f6b2
2 changed files with 37 additions and 27 deletions

View File

@ -25,6 +25,7 @@ if minetest.get_modpath("screwdriver") then
on_rotate = screwdriver.disallow on_rotate = screwdriver.disallow
end end
-- basic bamboo nodes. -- basic bamboo nodes.
local bamboo_def = { local bamboo_def = {
description = "Bamboo", description = "Bamboo",
@ -92,6 +93,7 @@ local bamboo_def = {
local node = minetest.get_node(pointed_thing.under) local node = minetest.get_node(pointed_thing.under)
local pos = pointed_thing.under local pos = pointed_thing.under
local nodename = node.name local nodename = node.name
mcl_bamboo.mcl_log("node name: " .. nodename)
-- check the nodename to see if it is one of the bamboo's -- check the nodename to see if it is one of the bamboo's
local bamboo_node = substr(nodename, 1, strlen(bamboo)) local bamboo_node = substr(nodename, 1, strlen(bamboo))
@ -116,14 +118,8 @@ local bamboo_def = {
if bamboo_node ~= bamboo and nodename ~= "mcl_bamboo:bamboo_endcap" then if bamboo_node ~= bamboo and nodename ~= "mcl_bamboo:bamboo_endcap" then
-- not bamboo... -- not bamboo...
if nodename ~= "mcl_flowerpots:flower_pot" then if nodename ~= "mcl_flowerpots:flower_pot" then
local found = false if mcl_bamboo.is_dirt(nodename) == false then
for i = 1, #mcl_bamboo.bamboo_dirt_nodes do mcl_bamboo.mcl_log("bamboo dirt node not found; node name: " .. nodename)
if nodename == mcl_bamboo.bamboo_dirt_nodes[i] then
found = true
break
end
end
if not found then
return return
end end
end end
@ -139,18 +135,14 @@ local bamboo_def = {
end end
local place_item = ItemStack(itemstack) -- make a copy so that we don't indirectly mess with the original. local place_item = ItemStack(itemstack) -- make a copy so that we don't indirectly mess with the original.
itemstack:set_count(itemstack:get_count() - 1)
if nodename == bamboo then mcl_bamboo.mcl_log("node name: " .. nodename)
-- return the missing item, so that we can lower the code
-- complexity and duplication. local bamboo_node = mcl_bamboo.is_bamboo(nodename)
itemstack:set_count(itemstack:get_count() + 1) mcl_bamboo.mcl_log("bamboo_node: " .. bamboo_node)
return minetest.item_place(itemstack, placer, pointed_thing, fdir)
elseif nodename == bamboo_one then if bamboo_node ~= -1 then
place_item = ItemStack(bamboo_one) place_item = ItemStack(mcl_bamboo.bamboo_index[bamboo_node])
elseif nodename == bamboo_two then
place_item = ItemStack(bamboo_two)
elseif nodename == bamboo_three then
place_item = ItemStack(bamboo_three)
else else
local placed_type = pr:next(0, 3) -- randomly choose which one to place. local placed_type = pr:next(0, 3) -- randomly choose which one to place.
mcl_bamboo.mcl_log("Place_Bamboo_Shoot--Type: " .. placed_type) mcl_bamboo.mcl_log("Place_Bamboo_Shoot--Type: " .. placed_type)
@ -165,6 +157,7 @@ local bamboo_def = {
end end
end end
minetest.item_place(place_item, placer, pointed_thing, fdir) minetest.item_place(place_item, placer, pointed_thing, fdir)
itemstack:set_count(itemstack:get_count() - 1)
return itemstack, pointed_thing.under return itemstack, pointed_thing.under
end, end,

View File

@ -33,6 +33,21 @@ mcl_bamboo.bamboo_dirt_nodes = {
"mcl_mud:mud", "mcl_mud:mud",
} }
function mcl_bamboo.is_dirt(node_name)
return table.indexof(mcl_bamboo.bamboo_dirt_nodes, node_name) ~= -1
end
mcl_bamboo.bamboo_index = {
"mcl_bamboo:bamboo",
"mcl_bamboo:bamboo_1",
"mcl_bamboo:bamboo_2",
"mcl_bamboo:bamboo_3",
}
function mcl_bamboo.is_bamboo(node_name)
return table.indexof(mcl_bamboo.bamboo_index, node_name)
end
--- pos: node position; placer: ObjectRef that is placing the item --- pos: node position; placer: ObjectRef that is placing the item
--- returns: true if protected, otherwise false. --- returns: true if protected, otherwise false.
function mcl_bamboo.is_protected(pos, placer) function mcl_bamboo.is_protected(pos, placer)
@ -56,12 +71,9 @@ function mcl_bamboo.grow_bamboo(pos, _)
for py = -1, BAMBOO_SOIL_DIST, -1 do for py = -1, BAMBOO_SOIL_DIST, -1 do
chk_pos = vector.offset(pos, 0, py, 0) chk_pos = vector.offset(pos, 0, py, 0)
local name = minetest.get_node(chk_pos).name local name = minetest.get_node(chk_pos).name
for i = 1, #mcl_bamboo.bamboo_dirt_nodes do if mcl_bamboo.is_dirt(name) then
if name == mcl_bamboo.bamboo_dirt_nodes[i] then found = true
found = true soil_pos = chk_pos
soil_pos = chk_pos
break
end
end end
if found then if found then
break break
@ -134,10 +146,15 @@ function mcl_bamboo.add_groups(name, ...)
end end
function mcl_bamboo.mcl_log(m, l) function mcl_bamboo.mcl_log(m, l)
if not m then
minetest.log("error", "expected string, received: " .. m)
return
end
if DEBUG then if DEBUG then
if not l then if not l then
minetest.log("[mcl_bamboo]: " .. m) minetest.log("[mcl_bamboo]: " .. m)
else
minetest.log(l, "[mcl_bamboo]: " .. m)
end end
minetest.log(l, "[mcl_bamboo]: " .. m)
end end
end end