From 0752ed17d85c572979b399c05c853c62022fc0ab Mon Sep 17 00:00:00 2001 From: kno10 Date: Mon, 9 Sep 2024 20:22:04 +0200 Subject: [PATCH] Improve cacti and cane growth ABM (#4590) - local functions, as they are not called by anywhere else - delay water check of reed, first check height - reduce number of get_node calls (for height 1,2,3 the old code used 4,5,4 calls, the new only 2,3,3) - cane growth rate is also reduced This will make the ABM cheaper. Reviewed-on: https://git.minetest.land/VoxeLibre/VoxeLibre/pulls/4590 Reviewed-by: the-real-herowl Co-authored-by: kno10 Co-committed-by: kno10 --- mods/ITEMS/mcl_core/functions.lua | 77 ++++++++++++++----------------- 1 file changed, 35 insertions(+), 42 deletions(-) diff --git a/mods/ITEMS/mcl_core/functions.lua b/mods/ITEMS/mcl_core/functions.lua index 84a88b529..eec432184 100644 --- a/mods/ITEMS/mcl_core/functions.lua +++ b/mods/ITEMS/mcl_core/functions.lua @@ -57,44 +57,41 @@ minetest.register_abm({ -- -- Papyrus and cactus growing -- - --- Functions -function mcl_core.grow_cactus(pos, node) - pos.y = pos.y-1 - local name = minetest.get_node(pos).name - if minetest.get_item_group(name, "sand") ~= 0 then - pos.y = pos.y+1 - local height = 0 - while minetest.get_node(pos).name == "mcl_core:cactus" and height < 4 do - height = height+1 - pos.y = pos.y+1 - end - if height < 3 then - if minetest.get_node(pos).name == "air" then - minetest.set_node(pos, {name="mcl_core:cactus"}) - end - end +function grow_cactus(pos, node) + pos.y = pos.y - 1 -- below + if minetest.get_item_group(minetest.get_node(pos).name, "sand") == 0 then return end + pos.y = pos.y + 2 -- above + local above = minetest.get_node(pos).name + if above == "air" then + minetest.set_node(pos, {name="mcl_core:cactus"}) + return + end + if above ~= "mcl_core:cactus" then return end + pos.y = pos.y + 1 -- at max height 3 + if minetest.get_node(pos).name == "air" then + minetest.set_node(pos, {name="mcl_core:cactus"}) end end -function mcl_core.grow_reeds(pos, node) - pos.y = pos.y-1 - local name = minetest.get_node(pos).name - if minetest.get_item_group(name, "soil_sugarcane") ~= 0 then - if minetest.find_node_near(pos, 1, {"group:water"}) == nil and minetest.find_node_near(pos, 1, {"group:frosted_ice"}) == nil then - return - end - pos.y = pos.y+1 - local height = 0 - while minetest.get_node(pos).name == "mcl_core:reeds" and height < 3 do - height = height+1 - pos.y = pos.y+1 - end - if height < 3 then - if minetest.get_node(pos).name == "air" then - minetest.set_node(pos, {name="mcl_core:reeds"}) - end - end +function grow_reeds(pos, node) + pos.y = pos.y - 1 -- below + if minetest.get_item_group(minetest.get_node(pos).name, "soil_sugarcane") == 0 then return end + pos.y = pos.y + 2 -- above + local above = minetest.get_node(pos).name + if above == "air" then + pos.y = pos.y - 1 -- original position, check for water + if minetest.find_node_near(pos, 1, {"group:water", "group:frosted_ice"}) == nil then return end + pos.y = pos.y + 1 -- above + minetest.set_node(pos, {name="mcl_core:reeds"}) + return + end + if above ~= "mcl_core:reeds" then return end + pos.y = pos.y + 1 -- at max height 3 + if minetest.get_node(pos).name == "air" then + pos.y = pos.y - 2 -- original position, check for water + if minetest.find_node_near(pos, 1, {"group:water", "group:frosted_ice"}) == nil then return end + pos.y = pos.y + 2 -- above + minetest.set_node(pos, {name="mcl_core:reeds"}) end end @@ -192,9 +189,7 @@ minetest.register_abm({ neighbors = {"group:sand"}, interval = 25, chance = 40, - action = function(pos) - mcl_core.grow_cactus(pos) - end, + action = grow_cactus }) local function is_walkable(pos) @@ -239,10 +234,8 @@ minetest.register_abm({ nodenames = {"mcl_core:reeds"}, neighbors = {"group:soil_sugarcane"}, interval = 25, - chance = 10, - action = function(pos) - mcl_core.grow_reeds(pos) - end, + chance = 40, + action = grow_reeds }) --