-- Convert grass into mud if under something else than air
minetest.register_abm({
	label = "Mud to Grass",
	nodenames = { "default:dirt" },
	neighbors = { "group:air_equivalent" },
	interval = 1,
	chance = 20,
	catch_up = false, -- Back in my day we had no such thing
	action = function(pos, node)
		local p1 = vector.offset(pos, 0, 1, 0)
		local above = minetest.get_node(p1)
		if minetest.get_item_group(above.name, "air_equivalent") > 0 and minetest.get_node_light(p1) >= 13 then
			node.name = "default:dirt_with_grass"
			minetest.swap_node(pos, node)
		end
	end,
})

-- Convert grass into mud if under something else than air
minetest.register_abm({
	label = "Grass to Mud",
	nodenames = { "default:dirt_with_grass" },
	interval = 3,
	catch_up = false,
	action = function(pos, node)
		local above = minetest.get_node(vector.offset(pos, 0, 1, 0))
		if above.name ~= "farming:bush" and minetest.get_item_group(above.name, "air_equivalent") == 0 then
			node.name = "default:dirt"
			minetest.swap_node(pos, node)
		end
	end,
})

-- Natural cow spawning
minetest.register_abm({
	label = "Cow spawning",
	nodenames = { "default:dirt_with_grass" },
	interval = 25,
	chance = 10000,
	action = function(pos, node, active_object_count, active_object_count_wider)
		minetest.add_entity(vector.offset(pos, 0, 1, 0), "default:cow")
	end,
})

-- Rats spawn around regular trees
minetest.register_abm({
	label = "Rat Spawning",
	nodenames = { "default:tree", "default:jungletree" },
	interval = 10,
	chance = 200,
	catch_up = false,
	action = function(pos, node, active_object_count, active_object_count_wider)
		-- FIXME: this becomes problematic if users use other mods that spawn entities
		if active_object_count_wider > 0 then
			return
		end
		pos.x = pos.x + math.random(-2, 2)
		pos.z = pos.z + math.random(-2, 2)
		node = minetest.get_node(pos)
		local below = minetest.get_node(vector.offset(pos, 0, -1, 0))
		if below.name == "default:dirt_with_grass" and node.name == "air" then
			minetest.add_entity(pos, "default:rat")
		end
	end,
})

-- Fun things spawn in caves and dungeons
minetest.register_abm({
	label = "Mob Spawning",
	nodenames = { "default:stone", "default:mossycobble" },
	interval = 10,
	chance = 200,
	catch_up = false,
	action = function(pos, node, active_object_count, active_object_count_wider)
		if active_object_count_wider > 0 then
			return
		end
		local p1 = vector.offset(pos, 0, 1, 0)
		if (minetest.get_node_light(p1) or 0) <= 3 then
			if minetest.get_node(p1).name == "air" and minetest.get_node(vector.offset(p1, 0, 1, 0)).name == "air" then
				local i = math.random(0, 5)
				if i == 2 or i == 3 then
					minetest.log("action", "Rats spawn at " .. minetest.pos_to_string(p1))
					for j = 1, 3 do
						minetest.add_entity(p1, "default:rat")
					end
				end
			end
		end
	end,
})

minetest.register_abm({
	label = "Saplings",
	nodenames = { "default:sapling" },
	interval = 5,
	chance = 10,
	catch_up = false,
	action = function(pos, node)
		minetest.log("action", "A sapling grows into a tree at " .. minetest.pos_to_string(pos))
		local trees = {"tree.mts", "tree1.mts", "tree2.mts"}
		minetest.place_schematic(pos, minetest.get_modpath("cl_default") .. "/schematics/" .. trees[math.random(2) + 1], math.random(4) * 90, {}, false, {})
	end,
})

minetest.register_abm({
	label = "Leaf decay",
	nodenames = { "default:leaves" },
	interval = 1,
	chance = 5,
	action = function(pos, node)
		if not minetest.find_node_near(pos, 5, {"default:tree"}) then
			minetest.set_node(pos, { name = "air" })
			minetest.add_item(pos, node)
		end
	end,
})
