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

View File

@ -33,6 +33,21 @@ mcl_bamboo.bamboo_dirt_nodes = {
"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
--- returns: true if protected, otherwise false.
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
chk_pos = vector.offset(pos, 0, py, 0)
local name = minetest.get_node(chk_pos).name
for i = 1, #mcl_bamboo.bamboo_dirt_nodes do
if name == mcl_bamboo.bamboo_dirt_nodes[i] then
found = true
soil_pos = chk_pos
break
end
if mcl_bamboo.is_dirt(name) then
found = true
soil_pos = chk_pos
end
if found then
break
@ -134,10 +146,15 @@ function mcl_bamboo.add_groups(name, ...)
end
function mcl_bamboo.mcl_log(m, l)
if not m then
minetest.log("error", "expected string, received: " .. m)
return
end
if DEBUG then
if not l then
minetest.log("[mcl_bamboo]: " .. m)
else
minetest.log(l, "[mcl_bamboo]: " .. m)
end
minetest.log(l, "[mcl_bamboo]: " .. m)
end
end