From fe3c5a709047fbdd51e0c8086d5ea0465d7ecb3b Mon Sep 17 00:00:00 2001
From: HybridDog <adresse_test@t-online.de>
Date: Tue, 26 Aug 2014 13:55:53 +0200
Subject: [PATCH] allow overriding papyrus and cactus grow functions

dont grow cactus on a lying one

use minetest.get_node 2 times less

do a bit what ShadowNinja wrote

add comment

return true if the plant is set
---
 mods/default/functions.lua | 104 ++++++++++++++++++++++---------------
 1 file changed, 63 insertions(+), 41 deletions(-)

diff --git a/mods/default/functions.lua b/mods/default/functions.lua
index 82c9282a..cd4477b4 100644
--- a/mods/default/functions.lua
+++ b/mods/default/functions.lua
@@ -83,6 +83,7 @@ function default.node_sound_glass_defaults(table)
 	return table
 end
 
+
 --
 -- Lavacooling
 --
@@ -102,8 +103,8 @@ minetest.register_abm({
 	neighbors = {"group:water"},
 	interval = 1,
 	chance = 1,
-	action = function(pos, node, active_object_count, active_object_count_wider)
-		default.cool_lava_flowing(pos, node, active_object_count, active_object_count_wider)
+	action = function(...)
+		default.cool_lava_flowing(...)
 	end,
 })
 
@@ -112,37 +113,73 @@ minetest.register_abm({
 	neighbors = {"group:water"},
 	interval = 1,
 	chance = 1,
-	action = function(pos, node, active_object_count, active_object_count_wider)
-		default.cool_lava_source(pos, node, active_object_count, active_object_count_wider)
+	action = function(...)
+		default.cool_lava_source(...)
 	end,
 })
 
+
 --
 -- Papyrus and cactus growing
 --
 
+function default.grow_cactus(pos, node)
+	if node.param2 ~= 0 then
+		return
+	end
+	pos.y = pos.y-1
+	if minetest.get_item_group(minetest.get_node(pos).name, "sand") == 0 then
+		return
+	end
+	pos.y = pos.y+1
+	local height = 0
+	while node.name == "default:cactus" and height < 4 and node.param2 == 0 do
+		height = height+1
+		pos.y = pos.y+1
+		node = minetest.get_node(pos)
+	end
+	if height == 4
+	or node.name ~= "air" then
+		return
+	end
+	minetest.set_node(pos, {name="default:cactus"})
+	return true
+end
+
+function default.grow_papyrus(pos, node)
+	pos.y = pos.y-1
+	local name = minetest.get_node(pos).name
+	if name ~= "default:dirt_with_grass"
+	and name ~= "default:dirt" then
+		return
+	end
+	if not minetest.find_node_near(pos, 3, {"group:water"}) then
+		return
+	end
+	pos.y = pos.y+1
+	local height = 0
+	while node.name == "default:papyrus" and height < 4 do
+		height = height+1
+		pos.y = pos.y+1
+		node = minetest.get_node(pos)
+	end
+	if height == 4
+	or node.name ~= "air" then
+		return
+	end
+	minetest.set_node(pos, {name="default:papyrus"})
+	return true
+end
+
+-- wrapping the functions in abm action is necessary to make overriding them possible
 minetest.register_abm({
 	nodenames = {"default:cactus"},
 	neighbors = {"group:sand"},
 	interval = 50,
 	chance = 20,
-	action = function(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 == "default:cactus" and height < 4 do
-				height = height+1
-				pos.y = pos.y+1
-			end
-			if height < 4 then
-				if minetest.get_node(pos).name == "air" then
-					minetest.set_node(pos, {name="default:cactus"})
-				end
-			end
-		end
-	end,
+	action = function(...)
+		default.grow_cactus(...)
+	end
 })
 
 minetest.register_abm({
@@ -150,28 +187,12 @@ minetest.register_abm({
 	neighbors = {"default:dirt", "default:dirt_with_grass"},
 	interval = 50,
 	chance = 20,
-	action = function(pos, node)
-		pos.y = pos.y-1
-		local name = minetest.get_node(pos).name
-		if name == "default:dirt" or name == "default:dirt_with_grass" then
-			if minetest.find_node_near(pos, 3, {"group:water"}) == nil then
-				return
-			end
-			pos.y = pos.y+1
-			local height = 0
-			while minetest.get_node(pos).name == "default:papyrus" and height < 4 do
-				height = height+1
-				pos.y = pos.y+1
-			end
-			if height < 4 then
-				if minetest.get_node(pos).name == "air" then
-					minetest.set_node(pos, {name="default:papyrus"})
-				end
-			end
-		end
-	end,
+	action = function(...)
+		default.grow_papyrus(...)
+	end
 })
 
+
 --
 -- dig upwards
 --
@@ -185,6 +206,7 @@ function default.dig_up(pos, node, digger)
 	end
 end
 
+
 --
 -- Leafdecay
 --
-- 
GitLab