From: tenplus1 Date: Sun, 30 Jul 2017 13:02:10 +0000 (+0100) Subject: Mushroom spread: Optimise and make overridable X-Git-Tag: 5.0.0~182 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=bcf98df5fac3eb59b02d9fdb066ab92c6228d787;p=oweals%2Fminetest_game.git Mushroom spread: Optimise and make overridable Move mushroom spread ABM action into a global and overridable function. Optimise spread code. Reduce spread range to reduce spread through walls. --- diff --git a/mods/flowers/init.lua b/mods/flowers/init.lua index 826c52ad..227b6aee 100644 --- a/mods/flowers/init.lua +++ b/mods/flowers/init.lua @@ -204,38 +204,34 @@ minetest.register_node("flowers:mushroom_brown", { -- Mushroom spread and death +function flowers.mushroom_spread(pos, node) + if minetest.get_node_light(pos, nil) == 15 then + minetest.remove_node(pos) + return + end + local positions = minetest.find_nodes_in_area_under_air( + {x = pos.x - 1, y = pos.y - 2, z = pos.z - 1}, + {x = pos.x + 1, y = pos.y + 1, z = pos.z + 1}, + {"group:soil", "group:tree"}) + if #positions == 0 then + return + end + local pos2 = positions[math.random(#positions)] + pos2.y = pos2.y + 1 + if minetest.get_node_light(pos, 0.5) <= 3 and + minetest.get_node_light(pos2, 0.5) <= 3 then + minetest.set_node(pos2, {name = node.name}) + end +end + minetest.register_abm({ label = "Mushroom spread", nodenames = {"flowers:mushroom_brown", "flowers:mushroom_red"}, interval = 11, - chance = 50, - action = function(pos, node) - if minetest.get_node_light(pos, nil) == 15 then - minetest.remove_node(pos) - return - end - local random = { - x = pos.x + math.random(-2, 2), - y = pos.y + math.random(-1, 1), - z = pos.z + math.random(-2, 2) - } - local random_node = minetest.get_node_or_nil(random) - if not random_node or random_node.name ~= "air" then - return - end - local node_under = minetest.get_node_or_nil({x = random.x, - y = random.y - 1, z = random.z}) - if not node_under then - return - end - - if (minetest.get_item_group(node_under.name, "soil") ~= 0 or - minetest.get_item_group(node_under.name, "tree") ~= 0) and - minetest.get_node_light(pos, 0.5) <= 3 and - minetest.get_node_light(random, 0.5) <= 3 then - minetest.set_node(random, {name = node.name}) - end - end + chance = 150, + action = function(...) + flowers.mushroom_spread(...) + end, })