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: VoxeLibre/VoxeLibre#4590
Reviewed-by: the-real-herowl <the-real-herowl@noreply.git.minetest.land>
Co-authored-by: kno10 <erich.schubert@gmail.com>
Co-committed-by: kno10 <erich.schubert@gmail.com>
This commit is contained in:
kno10 2024-09-09 20:22:04 +02:00 committed by the-real-herowl
parent 72c7489976
commit 0752ed17d8
1 changed files with 35 additions and 42 deletions

View File

@ -57,44 +57,41 @@ minetest.register_abm({
-- --
-- Papyrus and cactus growing -- Papyrus and cactus growing
-- --
function grow_cactus(pos, node)
-- Functions pos.y = pos.y - 1 -- below
function mcl_core.grow_cactus(pos, node) if minetest.get_item_group(minetest.get_node(pos).name, "sand") == 0 then return end
pos.y = pos.y-1 pos.y = pos.y + 2 -- above
local name = minetest.get_node(pos).name local above = minetest.get_node(pos).name
if minetest.get_item_group(name, "sand") ~= 0 then if above == "air" then
pos.y = pos.y+1 minetest.set_node(pos, {name="mcl_core:cactus"})
local height = 0 return
while minetest.get_node(pos).name == "mcl_core:cactus" and height < 4 do end
height = height+1 if above ~= "mcl_core:cactus" then return end
pos.y = pos.y+1 pos.y = pos.y + 1 -- at max height 3
end if minetest.get_node(pos).name == "air" then
if height < 3 then minetest.set_node(pos, {name="mcl_core:cactus"})
if minetest.get_node(pos).name == "air" then
minetest.set_node(pos, {name="mcl_core:cactus"})
end
end
end end
end end
function mcl_core.grow_reeds(pos, node) function grow_reeds(pos, node)
pos.y = pos.y-1 pos.y = pos.y - 1 -- below
local name = minetest.get_node(pos).name if minetest.get_item_group(minetest.get_node(pos).name, "soil_sugarcane") == 0 then return end
if minetest.get_item_group(name, "soil_sugarcane") ~= 0 then pos.y = pos.y + 2 -- above
if minetest.find_node_near(pos, 1, {"group:water"}) == nil and minetest.find_node_near(pos, 1, {"group:frosted_ice"}) == nil then local above = minetest.get_node(pos).name
return if above == "air" then
end pos.y = pos.y - 1 -- original position, check for water
pos.y = pos.y+1 if minetest.find_node_near(pos, 1, {"group:water", "group:frosted_ice"}) == nil then return end
local height = 0 pos.y = pos.y + 1 -- above
while minetest.get_node(pos).name == "mcl_core:reeds" and height < 3 do minetest.set_node(pos, {name="mcl_core:reeds"})
height = height+1 return
pos.y = pos.y+1 end
end if above ~= "mcl_core:reeds" then return end
if height < 3 then pos.y = pos.y + 1 -- at max height 3
if minetest.get_node(pos).name == "air" then if minetest.get_node(pos).name == "air" then
minetest.set_node(pos, {name="mcl_core:reeds"}) pos.y = pos.y - 2 -- original position, check for water
end if minetest.find_node_near(pos, 1, {"group:water", "group:frosted_ice"}) == nil then return end
end pos.y = pos.y + 2 -- above
minetest.set_node(pos, {name="mcl_core:reeds"})
end end
end end
@ -192,9 +189,7 @@ minetest.register_abm({
neighbors = {"group:sand"}, neighbors = {"group:sand"},
interval = 25, interval = 25,
chance = 40, chance = 40,
action = function(pos) action = grow_cactus
mcl_core.grow_cactus(pos)
end,
}) })
local function is_walkable(pos) local function is_walkable(pos)
@ -239,10 +234,8 @@ minetest.register_abm({
nodenames = {"mcl_core:reeds"}, nodenames = {"mcl_core:reeds"},
neighbors = {"group:soil_sugarcane"}, neighbors = {"group:soil_sugarcane"},
interval = 25, interval = 25,
chance = 10, chance = 40,
action = function(pos) action = grow_reeds
mcl_core.grow_reeds(pos)
end,
}) })
-- --