ROPE_SPOT = {384, 418}
OPENED_HOLE = {294, 383, 392, 469, 470, 482, 484, 485, 489}
CLOSED_HOLE = {468, 481, 483}
OPENED_TRAP = {462}
DOWN_LADDER = {369, 370, 408, 409, 427, 428, 430, 924, 3135, 3136}
JUNGLE_GRASS_TRANSFORM = {2782, 3985}
JUNGLE_GRASS_REMOVE = {1499}

POOLS = {2016, 2017, 2018, 2019, 2020, 2021, 2025, 2026, 2027, 2028, 2029, 2030}

WATER = {490, 4608, 491, 492, 493, 618, 619, 620, 621, 622, 623, 624, 625, 626, 627,
628, 629, 4609, 4610, 4611, 4612, 4613, 4614, 4615, 4616, 4617, 4618, 4619, 4620, 4621,
4622, 4623, 4624, 4625, 4664, 4665, 4666, 4820, 4821, 4822, 4823, 4824, 4825}

SWAMP = {602, 603, 604, 605, 4691, 4692, 4693, 4694, 4695, 4696, 4697, 4698, 4699, 4700, 4701, 4702, 4703, 4704, 4705, 4706, 4707, 4708, 4709, 4710, 4711, 4712, 4749, 4750, 4751, 4752, 4753, 4754, 4755, 4758}

OVEN_ON = {1786, 1788, 1790, 1792}
LADDER = {1386, 3678}
DECAYTO_ITEM_INCREMENT = {2041, 2044, 2047, 2050, 2052, 2054, 2162}

LOCKED_DOORS = {1209, 1212, 1231, 1234, 1249, 1252, 3535, 3544, 4913, 4916, 5098}

CONSTRUCTIONS = {
	[3901] = 1666, [3902] = 1670, [3903] = 1652, [3904] = 1674, [3905] = 1658,
	[3906] = 3813, [3907] = 3817, [3908] = 1619, [3909] = 1615, [3910] = 2105,
	[3911] = 1614, [3912] = 3806, [3913] = 3807, [3914] = 3809, [3915] = 1716,
	[3916] = 1724, [3917] = 1732, [3918] = 1775, [3919] = 1774, [3920] = 1750,
	[3921] = 3832, [3922] = 2095, [3923] = 2098, [3924] = 2064, [3925] = 2582,
	[3926] = 2117, [3927] = 1728, [3928] = 1442, [3929] = 1446, [3930] = 1447,
	[3931] = 2034, [3932] = 2604, [3933] = 2080, [3934] = 2084, [3935] = 3821,
	[3936] = 3811, [3937] = 2101, [3938] = 3812, [5086] = 5046, [5087] = 5055,
	[5088] = 5056,
}

FOODS = {
	[2328] = {84, "Gulp."},	[2362] = {48, "Yum."}, [2666] = {180, "Munch."}, [2667] = {144, "Munch."},
	[2668] = {120, "Mmmm."}, [2669] = {204, "Munch."}, [2670] = {48, "Gulp."}, [2671] = {360, "Chomp."},
	[2672] = {720, "Chomp."}, [2673] = {60, "Yum."}, [2674] = {72, "Yum."}, [2675] = {156, "Yum."},
	[2676] = {96, "Yum."}, [2677] = {12, "Yum."}, [2678] = {216, "Slurp."}, [2679] = {12, "Yum."},
	[2680] = {24, "Yum."}, [2681] = {108, "Yum."}, [2682] = {240, "Yum."}, [2683] = {204, "Munch."},
	[2684] = {60, "Crunch."}, [2685] = {72, "Munch."}, [2686] = {108, "Crunch."}, [2687] = {24, "Crunch."},
	[2688] = {24, "Mmmm."}, [2689] = {120, "Crunch."}, [2690] = {72, "Crunch."}, [2691] = {96, "Crunch."},
	[2695] = {72, "Gulp."}, [2696] = {108, "Smack."}, [2769] = {60, "Crunch."}, [2787] = {108, "Crunch."},
	[2788] = {48, "Crunch."}, [2789] = {264, "Munch."}, [2790] = {360, "Crunch."}, [2791] = {108, "Crunch."},
	[2792] = {72, "Crunch."}, [2793] = {144, "Crunch."}, [2794] = {36, "Crunch."}, [2795] = {432, "Crunch."},
	[2796] = {300, "Crunch."}
}

SMALL_SAPPHIRE	= 2146
SMALL_RUBY	= 2147
SMALL_EMERALD	= 2149
SMALL_AMETHYST	= 2150

MUSICAL_INSTRUMENTS = {2070, 2071, 2072, 2073, 2074, 2075, 2076, 2077, 2078, 2079, 2080, 2081, 2082, 2083, 2084, 2085, 2332, 2364, 2367, 2368, 2370, 2371, 2372, 2373, 2374, 3951, 3953, 3957}

NORTH_BEDS = {1754, 1758, 1762, 1766, 3836, 3840}
SOUTH_BEDS = {1755, 1759, 1763, 1767, 3837, 3841}
WEST_BEDS = {1756, 1760, 1764, 1768, 3838, 3842}
EAST_BEDS = {1757, 1761, 1765, 1769, 3839, 3843}

BREAKABLE_BY_WEAPONS = {
	[1619] = { remainings = 2255, chance = 15 },
	[1650] = { remainings = 2253, chance = 20 },
	[1651] = { remainings = 2253, chance = 20 },
	[1652] = { remainings = 2253, chance = 20 },
	[1653] = { remainings = 2253, chance = 20 },
	[3798] = { remainings = 3959, chance = 20 },
	[3799] = { remainings = 3958, chance = 20 },
}

function useRope(cid, item, frompos, item2, topos)
	if(topos.x == 0 and topos.y == 0 and topos.z == 0) then
		doPlayerSendDefaultCancel(cid, RETURNVALUE_NOTPOSSIBLE)
		return true
	end

	if(topos.x == CONTAINER_POSITION) then
		doPlayerSendDefaultCancel(cid, RETURNVALUE_NOTPOSSIBLE)
		return true
	end

	local newPos = {x = topos.x, y = topos.y, z = topos.z, stackpos = 0}
	local groundItem = getThingfromPos(newPos)
	if (isInArray(ROPE_SPOT, groundItem.itemid) ) then
		newPos.y = newPos.y + 1
		newPos.z = newPos.z - 1
		doTeleportThing(cid, newPos)
	elseif (isInArray(OPENED_HOLE, item2.itemid) or isInArray(OPENED_TRAP, item2.itemid) or isInArray(DOWN_LADDER, item2.itemid) ) then
		newPos.y = newPos.y + 1
		local downPos = {x = topos.x, y = topos.y, z = topos.z + 1, stackpos = 255}
		local downItem
        	if getBooleanFromString(getConfigValue("can_rope_creatures"), true) then
			downItem = getThingFromPos(downPos)
		else
			local topCreature = getTopCreature(downPos)
			if topCreature.uid ~= 0 and isPlayer(getCreatureMaster(topCreature.uid)) then --is summon of a player or a player itself
				downItem = topCreature
			else
				local ammountItems = getTileThingByPos({x = downPos.x , y = downPos.y, z = downPos.z, stackpos = -1})
				for aux = 1, ammountItems - 1 do
					local it = getTileThingByPos({x = downPos.x , y = downPos.y, z = downPos.z, stackpos = aux })
					if isValidUID(it.uid) and not isCreature(it.uid) and hasProperty(it.uid, CONST_PROP_MOVEABLE) then
						downItem = it
						break
					end
				end
			end
		end
		if downItem ~= nil and (downItem.itemid > 0) then
			doTeleportThing(downItem.uid, newPos)
		else
			doPlayerSendDefaultCancel(cid, RETURNVALUE_NOTPOSSIBLE)
		end
	else
		return false
	end
	return true
end

local function __doTransformHole__(parameters)
	local thing = getTileItemById(parameters.pos, MUD_HOLE)
	local newItem = doTransformItem(thing.uid, parameters.oldType)
	if parameters.oldaid ~= 0 and newItem then
		doSetItemActionId(thing.uid, parameters.oldaid)
	end
end

function useShovel(cid, item, frompos, item2, topos)
local TILE_SAND 		= 	9059
local ITEM_SCARAB_COIN 		= 	2159
local TUMB_ENTRANCE		= 	1345
local SCARAB_TILE		=	101
local MUD_HOLE		=	489
local SCARAB_COIN_TILE		= 	102
local duration = 5 * 60000 -- 5 minutes

	if (isInArray(CLOSED_HOLE, item2.itemid) ) then
		if item2.itemid == 8579 then
			doTransformItem(item2.uid, 8585)
		else
			doTransformItem(item2.uid, item2.itemid + 1)
		end
	elseif (item2.itemid == TILE_SAND) then
		if (item2.actionid == TUMB_ENTRANCE) then
			if (math.random(1, 5) == 1) then
				doTransformItem(item2.uid, MUD_HOLE)
				addEvent(__doTransformHole__, duration, {oldType = item2.itemid, pos = topos, oldaid = item2.actionid})
				if item2.actionid ~= 0 then
					doSetItemActionId(item2.uid, item2.actionid)
				end
			end
		elseif (item2.actionid == SCARAB_TILE) then
			if (math.random(1, 20) == 1) then
				doSummonCreature("Scarab", topos)
				doSetItemActionId(item2.uid, SCARAB_TILE + 2)
			end
		elseif (item2.actionid == SCARAB_COIN_TILE) then
			if (math.random(1, 20) == 1) then
				doCreateItem(ITEM_SCARAB_COIN, topos)
				doSetItemActionId(item2.uid, SCARAB_COIN_TILE + 2)
			end
		elseif (item2.actionid == SCARAB_TILE + 2) then
			if (math.random(1, 40) == 1) then
				doSetItemActionId(item2.uid, SCARAB_TILE)
			end
		elseif (item2.actionid == SCARAB_COIN_TILE + 2) then
			if (math.random(1, 40) == 1) then
				doSetItemActionId(item2.uid, SCARAB_COIN_TILE)
			end
		end
		doSendMagicEffect(topos, CONST_ME_POFF)
	else
		return false
	end

	for i = 1, #POOLS do
		local pool = getTileItemById(toPosition, POOLS[i]).uid
		if pool > 0 then
			doRemoveItem(pool,1)
		end
	end

	doDecayItem(item2.uid)
	return true
end


function usePick(cid, item, fromPosition, itemEx, toPosition)

local FRAGILE_ICE	=	7200
local ICE_FISHHOLE	=	7236
local MUD_HOLE = 392
local duration = 5 * 60000 -- 5 minutes

	local itemGround = getThingFromPos({x = toPosition.x, y = toPosition.y, z = toPosition.z + 1, stackpos = STACKPOS_GROUND})
	if(isInArray(ROPE_SPOT, itemGround.itemid) and isMoveable(itemEx.uid) == false and isCorpse(itemEx.uid) == false) then
		doTransformItem(itemEx.uid, MUD_HOLE)
		doSendMagicEffect(toPosition, CONST_ME_POFF)
		if itemEx.actionid ~= 0 then
			doSetItemActionId(itemEx.uid, itemEx.actionid)
		end
		addEvent(__doTransformHole__, duration, {oldType = itemEx.itemid, pos = toPosition, oldAid = itemEx.actionid})
		return true
	end

	if(itemEx.itemid == FRAGILE_ICE) then
		doTransformItem(itemEx.uid, ICE_FISHHOLE)
		if itemEx.actionid ~= 0 then
			doSetItemActionId(itemEx.uid, itemEx.actionid)
		end
		doSendMagicEffect(toPosition, CONST_ME_BLOCKHIT)
		return true
	end

	return false
end


function useMachete(cid, item, frompos, item2, topos)
	local ret = false

	if (isInArray(JUNGLE_GRASS_REMOVE, item2.itemid) ) then
		doRemoveItem(item2.uid)
		return true
	elseif (isInArray(JUNGLE_GRASS_TRANSFORM, item2.itemid) ) then
		doTransformItem(item2.uid, item2.itemid - 1)
		doDecayItem(item2.uid)
		ret = true
	elseif (isInArray(SPIDER_WEB, item2.itemid) ) then
		doTransformItem(item2.uid, item2.itemid +6)
		doDecayItem(item2.uid)
		ret = true
	end

	if item2.actionid ~= 0 and ret then
		doSetItemActionId(item2.uid, item2.actionid)
	end

	return ret
end

function useScythe(cid, item, frompos, item2, topos)

local ITEM_PRE_WHEAT = 2739
local ITEM_WHEAT = 2737
local ITEM_BUNCH_WHEAT = 2694
local ITEM_PRE_SUGAR_CANE = 5471
local ITEM_SUGAR_CANE = 5463
local ITEM_BUNCH_SUGAR_CANE = 5467

if (item2.itemid == ITEM_PRE_WHEAT) then
		doTransformItem(item2.uid, ITEM_WHEAT)
		doCreateItem(ITEM_BUNCH_WHEAT, 1, topos)
	elseif (item2.itemid == ITEM_PRE_SUGAR_CANE) then
		doTransformItem(item2.uid, ITEM_SUGAR_CANE)
		doCreateItem(ITEM_BUNCH_SUGAR_CANE, 1, topos)
	else
		return false
	end
	doDecayItem(item2.uid)
	return true
end
