From fca5520d6311ad8b34a19fb1e804bf8556d53c65 Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Sat, 1 Apr 2017 00:55:48 +0200 Subject: [PATCH] Make mushrooms spread over time --- mods/ITEMS/mcl_mushrooms/small.lua | 62 ++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/mods/ITEMS/mcl_mushrooms/small.lua b/mods/ITEMS/mcl_mushrooms/small.lua index 8510f87dee..b014e7dbc8 100644 --- a/mods/ITEMS/mcl_mushrooms/small.lua +++ b/mods/ITEMS/mcl_mushrooms/small.lua @@ -114,4 +114,66 @@ minetest.register_craft({ recipe = {'mcl_core:bowl', 'mcl_mushrooms:mushroom_brown', 'mcl_mushrooms:mushroom_red'} }) +--[[ Mushroom spread and death +Code based on information gathered from Minecraft Wiki + +]] +minetest.register_abm({ + label = "Mushroom spread", + nodenames = {"mcl_mushrooms:mushroom_brown", "mcl_mushrooms:mushroom_red"}, + interval = 11, + chance = 50, + action = function(pos, node) + if minetest.get_node_light(pos, nil) > 12 then + minetest.remove_node(pos) + return + end + local pos0 = vector.add(pos, {x=-4, y=-1, z=-4}) + local pos1 = vector.add(pos, {x=4, y=1, z=4}) + + -- Stop mushroom spread if a 9×3×9 box is too crowded + if #minetest.find_nodes_in_area(pos0, pos1, node.name) >= 5 then + return + end + + local selected_pos = table.copy(pos) + + -- Do two random selections which may place the new mushroom in a 5×5×5 cube + local random = { + x = selected_pos.x + math.random(-1, 1), + y = selected_pos.y + math.random(0, 1) - math.random(0, 1), + z = selected_pos.z + math.random(-1, 1) + } + 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_node_light(random, 0.5) > 12 or (minetest.get_item_group(node_under.name, "opaque") == 0) then + return + end + local random2 = { + x = random.x + math.random(-1, 1), + y = random.y, + z = random.z + math.random(-1, 1) + } + random_node = minetest.get_node_or_nil(random2) + if not random_node or random_node.name ~= "air" then + return + end + node_under = minetest.get_node_or_nil({x = random2.x, y = random2.y - 1, z = random2.z}) + if not node_under then + return + end + if minetest.get_node_light(random2, 0.5) > 12 or (minetest.get_item_group(node_under.name, "opaque") == 0) then + return + end + + minetest.set_node(random2, {name = node.name}) + end +})