From 1cf9e3701d51e6e61bb6bf8d06b2cc260f3eef6c Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Wed, 8 Mar 2017 01:54:04 +0100 Subject: [PATCH] Simplify sapling growth code --- mods/ITEMS/mcl_core/functions.lua | 43 +++++++++++-------------------- 1 file changed, 15 insertions(+), 28 deletions(-) diff --git a/mods/ITEMS/mcl_core/functions.lua b/mods/ITEMS/mcl_core/functions.lua index 802f7a137..52c90e5e2 100644 --- a/mods/ITEMS/mcl_core/functions.lua +++ b/mods/ITEMS/mcl_core/functions.lua @@ -415,17 +415,12 @@ minetest.register_abm({ local treelight = 9 --- Oak tree -minetest.register_abm({ - nodenames = {"mcl_core:sapling"}, - neighbors = {"group:soil_sapling"}, - interval = 20, - chance = 1, - action = function(pos) +local sapling_grow_action = function(trunknode, leafnode, tree_id, soil_needed) + return function(pos) local light = minetest.get_node_light(pos) local soilnode = minetest.get_node({x=pos.x, y=pos.y-1, z=pos.z}) local soiltype = minetest.get_item_group(soilnode.name, "soil_sapling") - if soiltype >= 1 and light and light >= treelight then + if soiltype >= soil_needed and light and light >= treelight then -- Increase and check growth stage local meta = minetest.get_meta(pos) local stage = meta:get_int("stage") @@ -433,12 +428,21 @@ minetest.register_abm({ stage = stage + 1 if stage == 2 then minetest.set_node(pos, {name="air"}) - mcl_core.generate_tree(pos, "mcl_core:tree", "mcl_core:leaves", 1) + mcl_core.generate_tree(pos, trunknode, leafnode, tree_id) else meta:set_int("stage", stage) end end - end, + end +end + +-- Oak tree +minetest.register_abm({ + nodenames = {"mcl_core:sapling"}, + neighbors = {"group:soil_sapling"}, + interval = 20, + chance = 1, + action = sapling_grow_action("mcl_core:tree", "mcl_core:leaves", 1, 1), }) -- Jungle Tree @@ -447,24 +451,7 @@ minetest.register_abm({ neighbors = {"group:soil_sapling"}, interval = 20, chance = 1, - action = function(pos) - local light = minetest.get_node_light(pos) - local soilnode = minetest.get_node({x=pos.x, y=pos.y-1, z=pos.z}) - local soiltype = minetest.get_item_group(soilnode.name, "soil_sapling") - if soiltype == 2 and light and light >= treelight then - -- Increase and check growth stage - local meta = minetest.get_meta(pos) - local stage = meta:get_int("stage") - if stage == nil then stage = 0 end - stage = stage + 1 - if stage == 2 then - minetest.set_node(pos, {name="air"}) - mcl_core.generate_tree(pos, "mcl_core:jungletree", "mcl_core:jungleleaves", 2) - else - meta:set_int("stage", stage) - end - end - end, + action = sapling_grow_action("mcl_core:jungletree", "mcl_core:jungleleaves", 1, 2) }) ---------------------