local S = minetest.get_translator("hades_vines")

local SPAWN_INTERVAL = 90


local tt_surv = S("Needs to hang on leaves or be close to a tree trunk to survive")


local walldir ={}

-- Returns true if the node supports vines
local supports_vines = function(nodename)
	local def = minetest.registered_nodes[nodename]
	if not def then
		return true
	end
	-- Rules: 1) walkable 2) full cube OR it's a tree node
	return minetest.get_item_group(nodename, "tree") ~= 0 or (def.walkable and
			(def.node_box == nil or def.node_box.type == "regular") and
			(def.collision_box == nil or def.collision_box.type == "regular"))
end

--[[ Call this for vines nodes only.
Given the pos and node of a vines node, this returns true if the vines are
supported and false if the vines are currently floating.
Vines are considered “supported” if they face a walkable+solid block or
“hang” from a vines node above. ]]
local check_vines_supported = function(pos, node)
	local supported = false
	local dir = minetest.wallmounted_to_dir(node.param2)
	local pos1 = vector.add(pos, dir)
	local node_neighbor = minetest.get_node(pos1)
	local pos2 = vector.add(pos, {x=0, y=1, z=0})
	local node2 = minetest.get_node(pos2)
	local vg = minetest.get_item_group(node.name, "vines")
	local vg2 = minetest.get_item_group(node2.name, "vines")
	-- Check if vines are attached to a solid block.
	-- If ignore, we assume its solid.
	if (vg == 1) and (node_neighbor.name == "ignore" or supports_vines(node_neighbor.name)) then
		supported = true
	elseif (vg == 1) and (dir.y == 0) then
		-- Vines are not attached, now we check if the vines are “hanging” below another vines block
		-- of equal orientation.
		-- Again, ignore means we assume its supported
		local v1_def = minetest.registered_nodes[node.name]
		local v2_def = minetest.registered_nodes[node2.name]
		local v1_p2, v2_p2 = node.param2, node2.param2
		if v1_def.paramtype2 == "colorwallmounted" then
			v1_p2 = v1_p2 % 8
		end
		if v2_def.paramtype2 == "colorwallmounted" then
			v2_p2 = v2_p2 % 8
		end
		if node2.name == "ignore" or (vg == vg2 and v1_p2 == v2_p2) then
			supported = true
		end
	elseif (vg == 2 and (supports_vines(node2.name) or vg2 == 2)) then
		supported = true
	end
	return supported
end

local function get_on_place(centered)
	-- Restrict placement of vines
	return function(itemstack, placer, pointed_thing)
		if pointed_thing.type ~= "node" then
			-- no interaction possible with entities
			return itemstack
		end

		local under = pointed_thing.under
		local above = pointed_thing.above
		local node = minetest.get_node(under)
		local def = minetest.registered_nodes[node.name]
		if not def then
			return itemstack
		end

		-- Check special rightclick action of pointed node
		if def and def.on_rightclick then
			if not placer:get_player_control().sneak then
				return def.on_rightclick(under, node, placer, itemstack,
					pointed_thing) or itemstack, false
			end
		end

		local cnode
		if centered then
			cnode = minetest.get_node({x=above.x, y=above.y+1, z=above.z})
		else
			-- Sideways vines may not be placed on top or below another block
			if under.y ~= above.y then
				return itemstack
			end
			cnode = node
		end
		-- Only place on full cubes
		if not supports_vines(cnode.name) then
			-- But centered vines can be placed below centered vines
			if not (centered and minetest.get_item_group(cnode.name, "vines") == 2) then
				return itemstack
			end
		end

		-- Set correct seasonal color on placement
		local idef = itemstack:get_definition()
		local p2
		local dir = vector.subtract(under, above)
		local wm = minetest.dir_to_wallmounted(dir)
		if dir.x ~= 0 or dir.y ~= 0 or dir.z ~= 0 then
			if idef and idef.paramtype2 == "colorwallmounted" then
				local pal = hades_seasons.get_seasonal_palette_color_param2()
				p2 = pal * 8 + wm
			else
				p2 = wm
			end
		end

		local itemstack, success = minetest.item_place_node(itemstack, placer, pointed_thing, p2)

		if success then
			if idef and idef.sounds and idef.sounds.place then
				minetest.sound_play(idef.sounds.place, {pos=above, gain=1}, true)
			end
		end
		return itemstack
	end
end

-- Get vines when using shears
local after_dig_node = function(pos, oldnode, oldmetadata, user)
	local wielded
	if not user or not user:is_player() then
		return
	end
	if user:get_wielded_item() ~= nil then
		wielded = user:get_wielded_item()
	else
		return
	end
	if minetest.get_item_group(wielded:get_name(), "shears") ~= 0 then
		local add = true
		local inv = user:get_inventory()
		local newstack = ItemStack(oldnode.name)
		if not inv then
			add = false
		elseif minetest.is_creative_enabled(user:get_player_name()) then
			if inv:contains_item("main", newstack) then
				add = false
			end
		end
		if add == true then
			-- Add into inventory or drop as item if no room
			if inv:room_for_item("main", newstack) then
				inv:add_item("main", newstack)
			else
				if not minetest.is_creative_enabled(user:get_player_name()) then
					minetest.add_item(pos, newstack)
				end
			end
		end
	end
end

-- If vine dug, also dig a “dependant” vine below it.
-- A vine is dependant if it hangs from this node and has no supporting block.
local on_dig = function(pos, node, digger)
	local below = {x=pos.x, y=pos.y-1, z=pos.z}
	local belownode = minetest.get_node(below)
	minetest.node_dig(pos, node, digger)
	local vg = minetest.get_item_group(node.name, "vines")
	local vg2 = minetest.get_item_group(belownode.name, "vines")
	if vg ~= 0 and vg == vg2 and (not check_vines_supported(below, belownode)) then
		return minetest.registered_nodes[belownode.name].on_dig(below, belownode, digger)
	end
end

local function register_vine(id, def)
	local paramtype2, drawtype, buildable_to, on_rotate, palette
	local groups = table.copy(def.groups)
	if def.vines_type == "center" then
		groups.vines = 2
		drawtype = "plantlike"
		buildable_to = false
		if def.colorable then
			paramtype2 = "color"
		end
	elseif def.vines_type == "side" then
		groups.vines = 1
		if def.colorable then
			paramtype2 = "colorwallmounted"
		else
			paramtype2 = "wallmounted"
		end
		drawtype = "signlike"
		buildable_to = true
		on_rotate = false
	end
	if def.colorable then
		palette = "hades_vines_"..id.."_palette.png"
		-- This marks the node as supporting seasonal coloring
		groups.seasonal_vines = 1
	end

	local tiles
	if def.colorable then
		tiles = {
			"hades_vines_"..id.."_colorable.png",
			"hades_vines_"..id.."_colorable.png",
			"blank.png",
		}
	else
		tiles = {
			"hades_vines_"..id..".png",
			"hades_vines_"..id..".png",
			"blank.png",
		}
	end
	local selection_box = def.selection_box or {
		type = "wallmounted",
	}

	local is_centered = groups.vines == 2

	local mnext
	if def.description_flowering then
		mnext = "hades_vines:"..id.."_flowering"
	elseif def.description_rotten then
		mnext = "hades_vines:"..id.."_rotten"
	end

	local image, image_overlay
	if def.colorable then
		if def.overlay then
			image = "hades_vines_"..id.."_colorable.png^[multiply:"..def.color_stereotype
			image_overlay = "hades_vines_"..id.."_overlay.png"
		else
			image = "hades_vines_"..id.."_colorable.png^[multiply:"..def.color_stereotype
		end
	else
		if def.overlay then
			image = "hades_vines_"..id..".png^hades_vines_"..id.."_overlay.png"
		else
			image = "hades_vines_"..id..".png"
		end
	end

	local groups_normal = table.copy(groups)
	groups_normal.vines_alive = 1
	minetest.register_node("hades_vines:"..id, {
		description = def.description_normal,
		_tt_help = def.help_normal,
		walkable = false,
		climbable = true,
		drop = "",
		buildable_to = buildable_to,
		floodable = true,
		sunlight_propagates = true,
		paramtype = "light",
		paramtype2 = paramtype2,
		palette = palette,
		tiles = tiles,
		inventory_image = image,
		wield_image = image,
		inventory_overlay = image_overlay,
		wield_overlay = image_overlay,
		drawtype = drawtype,
		groups = groups_normal,
		sounds = hades_sounds.node_sound_leaves_defaults(),
		selection_box = selection_box,
		node_placement_prediction = "",

		on_place = get_on_place(is_centered),
		on_dig = on_dig,
		on_rotate = on_rotate,
		after_dig_node = after_dig_node,
		_hades_magic_next = mnext,
	})

	if def.description_flowering then
		local groups_flowering = table.copy(groups_normal)
		groups_flowering.vines_flowering = 1
		groups_flowering.vines_alive = 1
		if def.description_rotten then
			mnext = "hades_vines:"..id.."_rotten"
		else
			mnext = "hades_vines:"..id
		end

		if def.colorable then
			tiles = {
				"hades_vines_"..id.."_colorable.png",
				"hades_vines_"..id.."_colorable.png",
				"blank.png",
			}
		else
			tiles = {
				"hades_vines_"..id..".png^hades_vines_"..id.."_flowering_overlay.png",
				"hades_vines_"..id..".png^hades_vines_"..id.."_flowering_overlay.png",
				"blank.png",
			}
		end
		local overlay_tiles
		if def.overlay_flowering then
			overlay_tiles = {
				{ name = "hades_vines_"..id.."_flowering_overlay.png", color = "white" },
				{ name = "hades_vines_"..id.."_flowering_overlay.png", color = "white" },
				"blank.png",
			}
		end
		if def.colorable then
			if def.overlay_flowering then
				image = "hades_vines_"..id.."_colorable.png^[multiply:"..def.color_stereotype
				image_overlay = "hades_vines_"..id.."_flowering_overlay.png"
			else
				image = "hades_vines_"..id.."_colorable.png^[multiply:"..def.color_stereotype
			end
		else
			if def.overlay_flowering then
				image = "hades_vines_"..id..".png^hades_vines_"..id.."_flowering_overlay.png"
			else
				image = "hades_vines_"..id..".png"
			end
		end

		minetest.register_node("hades_vines:"..id.."_flowering", {
			description = def.description_flowering,
			_tt_help = def.help_flowering,
			walkable = false,
			climbable = true,
			drop = "",
			buildable_to = buildable_to,
			floodable = true,
			sunlight_propagates = true,
			paramtype = "light",
			paramtype2 = paramtype2,
			palette = palette,
			tiles = tiles,
			overlay_tiles = overlay_tiles,
			drawtype = drawtype,
			inventory_image = image,
			wield_image = image,
			inventory_overlay = image_overlay,
			wield_overlay = image_overlay,
			groups = groups_flowering,
			sounds = hades_sounds.node_sound_leaves_defaults(),
			selection_box = selection_box,
			node_placement_prediction = "",
			on_place = get_on_place(is_centered),
			on_dig = on_dig,
			on_rotate = on_rotate,
			after_dig_node = after_dig_node,
			_hades_magic_next = mnext,
		})
	end

	if def.description_rotten then
		local groups_rotten = table.copy(groups)
		groups_rotten.vines_rotten = 1
		groups_rotten.seasonal_vines = nil

		minetest.register_node("hades_vines:"..id.."_rotten", {
			description = def.description_rotten,
			_tt_help = def.help_rotten,
			walkable = false,
			climbable = true,
			drop = "",
			buildable_to = buildable_to,
			floodable = true,
			sunlight_propagates = true,
			paramtype = "light",
			paramtype2 = paramtype2,
			tiles = { "hades_vines_"..id.."_rotten.png", "hades_vines_"..id.."_rotten.png", "blank.png" },
			drawtype = drawtype,
			inventory_image = "hades_vines_"..id.."_rotten.png",
			wield_image = "hades_vines_"..id.."_rotten.png",
			groups = groups_rotten,
			sounds = hades_sounds.node_sound_leaves_defaults(),
			selection_box = selection_box,
			node_placement_prediction = "",
			on_place = get_on_place(is_centered),
			on_dig = on_dig,
			on_rotate = on_rotate,
			after_dig_node = after_dig_node,
			_hades_magic_next = "hades_vines:"..id,
		})
	end
end

register_vine("jungle", {
	vines_type = "side",
	description_normal = S("Tropical Vine"),
	description_flowering = S("Flowering Tropical Vine"),
	description_rotten = S("Rotten Tropical Vine"),
	help_normal = tt_surv,
	help_flowering = tt_surv,
	groups = {vines_species_jungle=1,snappy=3,flammable=2},
	colorable = true,
	overlay_flowering = true,
	color_stereotype = "#4c9d34",
})

register_vine("willow", {
	vines_type = "side",
	description_normal = S("Willow Vine"),
	description_flowering = S("Flowering Willow Vine"),
	description_rotten = S("Rotten Willow Vine"),
	help_normal = tt_surv,
	help_flowering = tt_surv,
	groups = {vines_species_willow=1,snappy=3,flammable=2},
	colorable = true,
	overlay_flowering = true,
	color_stereotype = "#447b37",
})

register_vine("cave", {
	vines_type = "center",
	description_normal = S("Cave Vine"),
	description_flowering = S("Flowering Cave Vine"),
	description_rotten = S("Rotten Cave Vine"),
	help_normal = S("Needs to grow from dirt or clay or be close to water to survive"),
	help_flowering = S("Needs to grow from dirt or clay or be close to water to survive"),
	overlay_flowering = true,
	groups = {vines_species_cave=1,snappy=3,flammable=2},
	selection_box = {
		type = "fixed",
		fixed = {-0.3, -1/2, -0.3, 0.3, 1/2, 0.3},
	},
})

register_vine("root", {
	vines_type = "center",
	description_normal = S("Root Vine"),
	groups = {vines_species_root=1,snappy=3,flammable=2},
	selection_box = {
		type = "fixed",
		fixed = {-1/6, -1/2, -1/6, 1/6, 1/2, 1/6},
	},
})

--ABMs

minetest.register_abm({
	label = "Update vines flowers",
	nodenames = {"hades_vines:willow", "hades_vines:willow_flowering", "hades_vines:jungle", "hades_vines:jungle_flowering", "hades_vines:cave", "hades_vines:cave_flowering"},
	interval = 14,
	chance = 20,
	action = function(pos, node)
		local season = hades_seasons.get_season()
		local ll = minetest.get_node_light(pos, 0.5)

		-- Willow vines bloom in spring
		if ll > 8 and season == hades_seasons.SEASON_SPRING then
			if node.name == "hades_vines:willow" then
				minetest.set_node(pos, {name="hades_vines:willow_flowering", param2=node.param2})
				return
			end
		else
			if node.name == "hades_vines:willow_flowering" then
				minetest.set_node(pos, {name="hades_vines:willow", param2=node.param2})
				return
			end
		end

		-- Jungle vines bloom in summers every 2 years
		if ll > 13 and season == hades_seasons.SEASON_SUMMER and hades_seasons.get_year() % 2 == 1 then
			if node.name == "hades_vines:jungle" then
				minetest.set_node(pos, {name="hades_vines:jungle_flowering", param2=node.param2})
				return
			end
		else
			if node.name == "hades_vines:jungle_flowering" then
				minetest.set_node(pos, {name="hades_vines:jungle", param2=node.param2})
				return
			end
		end

		-- Cave vines bloom at narrow light levels
		if ll >= 3 and ll <= 6 then
			if node.name == "hades_vines:cave" then
				-- Flowers can't spawn when too close to each other
				for p=-2, 2 do
					if p ~= 0 and minetest.get_node(vector.offset(pos, 0, p, 0)).name == "hades_vines:cave_flowering" then
						return
					end
				end
				minetest.set_node(pos, {name="hades_vines:cave_flowering", param2=node.param2})
				return
			end
		else
			if node.name == "hades_vines:cave_flowering" then
				minetest.set_node(pos, {name="hades_vines:cave", param2=node.param2})
				return
			end
		end
	end,
})

minetest.register_abm({
	label = "Update seasonal vines color",
	nodenames = {"group:seasonal_vines"},
	interval = 7,
	chance = 20,
	action = function(pos, node)
		local def = minetest.registered_nodes[node.name]
		local old_param2 = node.param2
		local old_palette_index
		if def.paramtype2 == "colorwallmounted" then
			old_palette_index = math.floor(node.param2 / 8)
		elseif def.paramtype2 == "color" then
			old_palette_index = node.param2
		else
			minetest.log("error", "[hades_vines] Seasonal vines "..node.name.." uses unsupported paramtype2: "..tostring(def.paramtype2))
			return
		end
		local new_palette_index = hades_seasons.get_seasonal_palette_color_param2(old_palette_index)
		local new_param2
		if def.paramtype2 == "colorwallmounted" then
			new_param2 = (new_palette_index * 8) + (node.param2 % 8)
		else
			new_param2 = new_palette_index
		end

		if new_param2 ~= old_param2 then
			minetest.set_node(pos, {name = node.name, param2 = new_param2})
		end
	end
})

minetest.register_abm({
	label = "Vine rot",
	nodenames = {"hades_vines:cave", "hades_vines:jungle", "hades_vines:willow", "hades_vines:cave_flowering", "hades_vines:jungle_flowering", "hades_vines:willow_flowering"},
	interval = 300,
	chance = 8,
	action = function(pos, node, active_object_count, active_object_count_wider)
		-- Rough overview:
		-- Vines rot from the top. The top vine will check for survival conditions
		-- (near to tree, or near water, etc.) and may rot if the conditions are not met.
		-- Once the top vine has rotten, the survival checks apply to the top vine *that is not
		-- rotten yet*. This will cause a long vine in a bad environment to eventually
		-- rot from top to bottom.
		-- Vines hanging from a live vine are safe (for now), but they too may rot
		-- if the vine above has rotten

		-- Any vine: If hanging from a live vine of the same vine species, it always survives
		local above = vector.offset(pos, 0, 1, 0)
		local anode = minetest.get_node(above)
		if (minetest.get_item_group(anode.name, "vines") ~= 0 and minetest.get_item_group(anode.name, "vines_rotten") == 0) then
			if (minetest.get_item_group(node.name, "vines_species_jungle") == 1 and minetest.get_item_group(anode.name, "vines_species_jungle") == 1) or
						(minetest.get_item_group(node.name, "vines_species_willow") == 1 and minetest.get_item_group(anode.name, "vines_species_willow") == 1) then
				-- Also check node rotation (wallmounted)
				if (node.param2 % 8) == (anode.param2 % 8) then
					return
				end
			elseif minetest.get_item_group(node.name, "vines_species_cave") == 1 and
					minetest.get_item_group(anode.name, "vines_species_cave") == 1 then
				return
			end
		-- Cave vines also survive when hanging from dirt, clay or vines block
		elseif (anode.name == "hades_vines:vines_block" or minetest.get_item_group(anode.name, "dirt") ~= 0 or minetest.get_item_group(anode.name, "clay") ~= 0) then
			if minetest.get_item_group(node.name, "vines_species_cave") == 1 then
				return
			end
		end

		-- Otherwise ...

		-- Jungle and willow vine: Either is attached to leaves/vines block directly, or a tree node must be nearby
		if node.name == "hades_vines:jungle" or node.name == "hades_vines:willow" or node.name == "hades_vines:jungle_flowering" or node.name == "hades_vines:willow_flowering" then
			local wdir = minetest.wallmounted_to_dir(node.param2)
			local wnode = minetest.get_node(vector.add(pos, wdir))
			if wnode.name == "hades_vines:vines_block" or minetest.get_item_group(wnode.name, "leaves") ~= 0 or minetest.get_item_group(wnode.name, "tree") ~= 0 then
				return
			end
			local rot = minetest.find_node_near(pos, 5, "group:tree") == nil
			if not rot then
				return
			end
		-- Cave vine: Must be near water
		else
			local rot = minetest.find_node_near(pos, 5, "group:water") == nil
			if not rot then
				return
			end
		end

		-- Do the vine rot
		local basename = node.name
		if minetest.get_item_group(basename, "vines_flowering") ~= 0 then
			-- Remove the "_flowering" suffix to get name of normal vine
			-- (all flowering vines must end with "_flowering")
			basename = string.sub(basename, 1, -11)
		end
		walldir = node.param2 % 8
		minetest.add_node(pos, {name=basename.."_rotten", param2 = walldir})
	end
})

minetest.register_abm({
	label = "Grow vines",
	nodenames = {"hades_vines:cave", "haves_vines:cave_flowering", "hades_vines:jungle", "hades_vines:jungle_flowering", "hades_vines:willow", "hades_vines:willow_flowering"},
	interval = 300,
	chance = 2,
	action = function(pos, node, active_object_count, active_object_count_wider)
		if node.name ~= "hades_vines:cave" and node.name ~= "hades_vines:cave_flowering" then
			-- Non-cave vine only grows in spring and summer
			local season = hades_seasons.get_season()
			if season ~= hades_seasons.SEASON_SPRING and season ~= hades_seasons.SEASON_SUMMER then
				return
			end
		end

		-- Flowering vines will grow a normal vine
		local newnodename = node.name
		if node.name == "hades_vines:cave_flowering" then
			newnodename = "hades_vines:cave"
		elseif node.name == "hades_vines:willow_flowering" then
			newnodename = "hades_vines:willow"
		elseif node.name == "hades_vines:jungle_flowering" then
			newnodename = "hades_vines:jungle"
		end

		local p = {x=pos.x, y=pos.y-1, z=pos.z}
		local n = minetest.get_node(p)
		if n.name == "air" then
			minetest.add_node(p, {name=newnodename, param2=node.param2})
		end
	end
})

minetest.register_abm({
	label = "Update vines block color",
	nodenames = {"hades_vines:vines_block"},
	interval = 7,
	chance = 20,
	action = function(pos, node)
		local def = minetest.registered_nodes[node.name]
		local old_param2 = node.param2
		local new_param2 = hades_seasons.get_seasonal_palette_color_param2(old_param2)
		if new_param2 ~= old_param2 then
			minetest.set_node(pos, {name = node.name, param2 = new_param2})
		end
	end,
})

local function vinedecay_particles(pos, node)
	local g = minetest.get_item_group(node.name, "vines")
	local relpos1, relpos2
	if g == 1 then
		local dir = minetest.wallmounted_to_dir(node.param2)
		if dir.x < 0 then
			relpos1 = { x = -0.45, y = -0.4, z = -0.5 }
			relpos2 = { x = -0.4, y = 0.4, z = 0.5 }
		elseif dir.x > 0 then
			relpos1 = { x = 0.4, y = -0.4, z = -0.5 }
			relpos2 = { x = 0.45, y = 0.4, z = 0.5 }
		elseif dir.z < 0 then
			relpos1 = { x = -0.5, y = -0.4, z = -0.45 }
			relpos2 = { x = 0.5, y = 0.4, z = -0.4 }
		elseif dir.z > 0 then
			relpos1 = { x = -0.5, y = -0.4, z = 0.4 }
			relpos2 = { x = 0.5, y = 0.4, z = 0.45 }
		else
			return
		end
	elseif g == 2 then
		relpos1 = { x = -0.25, y = -0.5, z = -0.25 }
		relpos2 = { x = 0.25, y = 0.5, z = 0.25 }
	else
		return
	end

	minetest.add_particlespawner({
		amount = math.random(8, 16),
		time = 0.1,
		minpos = vector.add(pos, relpos1),
		maxpos = vector.add(pos, relpos2),
		minvel = {x=-0.2, y=-0.2, z=-0.2},
		maxvel = {x=0.2, y=0.1, z=0.2},
		minacc = {x=0, y=-9.81, z=0},
		maxacc = {x=0, y=-9.81, z=0},
		minexptime = 0.1,
		maxexptime = 0.5,
		minsize = 0.5,
		maxsize = 1.0,
		collisiondetection = true,
		vertical = false,
		node = node,
	})
end


minetest.register_abm({
	label = "Vines decay",
	nodenames = {"group:vines"},
	-- A low interval and a high inverse chance spreads the load
	interval = 2,
	chance = 5,

	action = function(pos, node)
		if not check_vines_supported(pos, node) then
			on_dig(pos, node)
			vinedecay_particles(pos, node)
			minetest.remove_node(pos)
			minetest.check_for_falling(pos)
		end
	end,
})

-- Spawn vines
hades_plantslib.spawn_on_surfaces({
	label = "Spawn cave vines",
	avoid_nodes = {"hades_vines:cave"},
	avoid_radius = 5,
	spawn_delay = SPAWN_INTERVAL,
	spawn_plants = {"hades_vines:cave"},
	spawn_chance = 50,
	spawn_surfaces = {"hades_core:dirt_with_grass","hades_core:dirt_with_grass_l1","hades_core:dirt_with_grass_l2","hades_core:dirt_with_grass_l3","hades_core:dirt","hades_core:clay"},
	spawn_on_bottom = true,
})

hades_plantslib.spawn_on_surfaces({
	label = "Spawn tropical vines",
	seasons = { hades_seasons.SEASON_SUMMER },
	avoid_nodes = {"hades_vines:cave", "hades_vines:jungle"},
	avoid_radius = 3,
	spawn_delay = SPAWN_INTERVAL,
	spawn_plants = {"hades_vines:jungle"},
	spawn_chance = 50,
	spawn_surfaces = {"group:lush_leaves"},
	spawn_on_side = true,
	near_nodes = {"hades_core:water_source", "hades_trees:jungle_tree", "hades_trees:banana_tree", "hades_trees:cocoa_tree"},
	near_nodes_size = 10,
	near_nodes_vertical = 5,
	near_nodes_count = 3,
})

hades_plantslib.spawn_on_surfaces({
	label = "Spawn willow vines",
	seasons = { hades_seasons.SEASON_SPRING },
	spawn_plants = {"hades_vines:willow"},
	avoid_radius = 3,
	spawn_delay = SPAWN_INTERVAL,
	spawn_chance = 50,
	spawn_surfaces = {"group:lush_leaves"},
	spawn_on_side = true,
	near_nodes = {"hades_core:water_source"},
	near_nodes_size = 7,
	near_nodes_vertical = 4,
	near_nodes_count = 3,
})

-- Shears: jojoa1997's shears

minetest.register_tool("hades_vines:shears", {
	description = S("Shears"),
	_tt_help = S("Cuts leaves, vines and plants"),
	inventory_image = "hades_vines_shears.png",
	wield_image = "hades_vines_shears.png",
	stack_max = 1,
	max_drop_level=3,
	groups = { shears = 1 },
	tool_capabilities = {
		full_punch_interval = 1.0,
		max_drop_level=0,
		groupcaps={
		snappy={times={[3]=0.2, [2]=0.5, [1]=1.5}, uses=20, maxlevel=3},
		}
	},
})

minetest.register_craft({
	output = 'hades_vines:shears',
	recipe = {
		{'', 'hades_core:steel_ingot', ''},
		{'group:stick', 'group:wood', 'hades_core:steel_ingot'},
		{'', 'group:stick', ''},
	}
})

-- This block is technically passive, but vines that grow
-- on or from it shall never rot (handled in vine rot ABM
-- instead of here).
minetest.register_node("hades_vines:vines_block",{
	description = S("Vines Block"),
	--~ Help text of vines block
	_tt_help = S("Prevents vines from rotting"),
	sunlight_propagates = true,
	tiles = {"hades_vines_block_colorable.png"},
	drawtype = "allfaces_optional",
	paramtype = "light",
	paramtype2 = "color",
	color = "#689403",
	palette = "hades_vines_block_palette.png",
	drop = "hades_vines:vines_block",
	groups = {snappy=2,flammable=2 },
	sounds = hades_sounds.node_sound_leaves_defaults(),
	on_place = function(itemstack, placer, pointed_thing)
		-- Pick the correct seasonal plant color
		local name = itemstack:get_name()
		local param2 = hades_seasons.get_seasonal_palette_color_param2()
		local ret = minetest.item_place(itemstack, placer, pointed_thing, param2)
		return ItemStack(name.." "..ret:get_count())
	end,
})

minetest.register_craft({
	output = 'hades_vines:vines_block',
	recipe = {
		{'hades_farming:string', 'group:vines_alive', 'hades_farming:string'},
		{'group:vines_alive', 'group:vines_alive', 'group:vines_alive'},
		{'hades_farming:string', 'group:vines_alive', 'hades_farming:string'},
	}
})
minetest.register_craft({
	type = "fuel",
	recipe = "group:vines",
	burntime = 1,
})
minetest.register_craft({
	type = "fuel",
	recipe = "hades_vines:vines_block",
	burntime = 3,
})

minetest.log("action", "[hades_vines] loaded!")
