gCannon1Firing = false ;
gPuppyChestLocked = true ;

function openChest( id, param2, name )
	if ( getClickObjectTileState( id ) == 1 ) then
		-- chest is closed
		-- open it and disable highlightable
		
		if ( param2 == "locked" ) then
			-- locked
			setQuickInfoTag( getString( "chestlocked" ) ) ;
			playSound( "doorlocked", 0.7 ) ;
		elseif ( name == "fencerschest" and getBoolean( "fencerschestlocked" ) ) then
			-- locked
			setQuickInfoTag( getString( "chestlocked" ) ) ;
			playSound( "doorlocked", 0.7 ) ;
		elseif ( name == "dungeonchest" and getBoolean( "dungeonchestlocked" ) ) then
			-- locked
			setQuickInfoTag( getString( "chestlocked" ) ) ;
			playSound( "doorlocked", 0.7 ) ;
		elseif ( name == "puppychest" and gPuppyChestLocked ) then
			-- locked
			setQuickInfoTag( getString( "chestlocked" ) ) ;
			playSound( "doorlocked", 0.7 ) ;
		else
			-- unlocked
			setClickObjectTileState( id, 2 ) ;
			setClickObjectHighlightable( id, false ) ;
			
			playSound( "openchest", 1.0 ) ;
			
			local lootList = {} ;
			
			if ( name == "tutchest" ) then
				local playerX, playerY = getCharacterPos( "dartagnan" ) ;
				local level = getCharacterLevel( "dartagnan" )
				local dropX = playerX + 25 ;
				local dropY = playerY - 15 ;
							
				createItem( "tut_chest_gloves", "gloves_poor_1", level, dropX, dropY, true ) ;
			elseif ( name == "fencerschest" ) then
				--local enemyLootValue = getRewardLootValueForceJackpot( 3 ) ;
				local enemyLootValue = getSideQuestBaseLootValue( ) * 16 ;
				local item = getItemTypeWorthAround( enemyLootValue, false, false, false, false, false, false, false, false, false, false, true, false ) ;
				gFencersChestOpened = true ;
				table.insert( lootList, item ) ;
			elseif ( name == "dungeonchest" ) then
				gDungeonChestOpened = true ;
				table.insert( lootList, "familywine" ) ;
			elseif ( name == "puppychest" ) then
				createItem( "", "puppy_8", "calaishouseb", -36, -88, true ) ;
			else
				if ( param2 == "" ) then
					-- default loot
					lootList = getChestListOfStuff( ) ;
				elseif ( param2 == "poop" ) then
					table.insert( lootList, 10000 ) ;
					table.insert( lootList, 10000 ) ;
					table.insert( lootList, 10000 ) ;
					table.insert( lootList, 10000 ) ;
					table.insert( lootList, 10000 ) ;
					table.insert( lootList, 10000 ) ;
					table.insert( lootList, 10000 ) ;
					table.insert( lootList, 10000 ) ;
				end
			end
			
			createLoot( id, lootList ) ;
			
			if ( name == "" and param2 == "" ) then
				local minResetTime = 50 * 60 ; -- 50 minutes
				local maxResetTime = 120 * 60 ; -- 120 minutes
				local timeToReset = randomInt( minResetTime, maxResetTime ) ;
				setTimeToReset( id, timeToReset ) ;
			end
		end
	end
end

function openBarrel( id, param2, name )
	if ( getClickObjectTileState( id ) == 1 ) then
		-- chest is closed
		-- open it and disable highlightable
		setClickObjectTileState( id, 2 ) ;
		setClickObjectHighlightable( id, false ) ;
		
		playSound( "opencrate", 1.0 ) ;
		
		local lootList = {} ;
		
		if ( name == "tutbarrel" ) then
			local playerX, playerY = getCharacterPos( "dartagnan" ) ;
			local level = getCharacterLevel( "dartagnan" )
			local dropX = playerX - 25 ;
			local dropY = playerY - 10 ;
						
			createItem( "tut_barrel_ointment", "ointment", level, dropX, dropY, true ) ;
		elseif ( name == "stcloudbarrel6" and isMissionActive( "missionparisb4_tut4" ) ) then
			local playerX, playerY = getCharacterPos( "dartagnan" ) ;
			local level = getCharacterLevel( "dartagnan" )
			local dropX = playerX - 25 ;
			local dropY = playerY - 10 ;
						
			createItem( "tut_barrel_gloves_1", "gloves_poor_1", level, dropX, dropY, true ) ;
		else
			if ( param2 == "" ) then
				-- default loot
				local gaveOintment = false ;
				--local ratioHealth = getRatioHealth( "dartagnan" ) ;
				--local numOintments = numOfItemType( "ointment" ) ;
				--if ( numOintments == 0 ) then
				--	if ( ratioHealth < 0.9 ) then
				--		table.insert( lootList, "ointment" ) ;
				--		gaveOintment = true ;
				--	else
				--		local randNum = randomInt( 0, 101 ) ; --math.random( 0, 100 ) ;
				--		if ( randNum > 50 ) then
				--			table.insert( lootList, "ointment" ) ;
				--			gaveOintment = true ;
				--		end
				--	end
				--else
				--	local randNum = randomInt( 0, 101 ) ; --math.random( 0, 100 ) ;
				--	if ( randNum > 90 ) then
				--		table.insert( lootList, "ointment" ) ;
				--		gaveOintment = true ;
				--	end
				--end

				if ( not gaveOintment ) then
					-- either give ointment (above) or give valuable stuff
					lootList = getBarrelListOfStuff( ) ;
				end
			end
		end
			
		createLoot( id, lootList ) ;
		
		if ( not ( name == "tutbarrel" ) ) then
			local minResetTime = 20 * 60 ; -- 10 minutes
			local maxResetTime = 60 * 60 ; -- 30 minutes
			local timeToReset = randomInt( minResetTime, maxResetTime ) ;
			setTimeToReset( id, timeToReset ) ;
		end
	end
end

function openCrate( id, param2, name )
	if ( getClickObjectTileState( id ) == 1 ) then
		-- chest is closed
		-- open it and disable highlightable
		setClickObjectTileState( id, 2 ) ;
		setClickObjectHighlightable( id, false ) ;
		
		playSound( "opencrate", 1.0 ) ;
		
		local lootList = {} ;
		
		if ( name == "stcloudcrate8" and isMissionActive( "missionparisb4_tut3" ) ) then
			local playerX, playerY = getCharacterPos( "dartagnan" ) ;
			local level = getCharacterLevel( "dartagnan" )
			local dropX = playerX - 25 ;
			local dropY = playerY - 10 ;

			createItem( "tut_crate_ointment_1", "ointment", level, dropX, dropY, true ) ;
		else
			if ( param2 == "" ) then
				-- default loot
				lootList = getCrateListOfStuff( ) ;
			end
		end
			
		createLoot( id, lootList ) ;
		
		local minResetTime = 20 * 60 ; -- 10 minutes
		local maxResetTime = 60 * 60 ; -- 30 minutes
		local timeToReset = randomInt( minResetTime, maxResetTime ) ;
		setTimeToReset( id, timeToReset ) ;
	end
end

function fireCannonLeft( id, param2, name )
	local objX, objY = getClickObjectPos( id ) ;
	createCannonParticles( objX, objY, true ) ;
	playSound( "cannonfire", 1.0 ) ;
	fireCannon( id, param2 ) ;
end

function fireCannonRight( id, param2, name )
	local objX, objY = getClickObjectPos( id ) ;
	createCannonParticles( objX, objY, false ) ;
	playSound( "cannonfire", 1.0 ) ;
	fireCannon( id, param2 ) ;
end

function fireCannon( id, param2, name )
	local objX, objY = getClickObjectPos( id ) ;
	setClickObjectHighlightable( id, false ) ;
	
	if ( param2 == "cannon1" and not gCannon1Firing ) then
		gCannon1Firing = true ;
		
		lockPlayerInPlace( ) ;	
	
		wait( 0.3 ) ;
		setBaseFaceCharacter( "cannonguy", "dartagnan" ) ;
		setBaseFaceCharacter( "dartagnan", "cannonguy" ) ;
		coroutine.yield( ) ;
		coroutine.yield( ) ;
			
		freezeGame( ) ;
		local bubInd = characterSay( "cannonguy", true, -1, -1, true, getString( "firecannon_1" ) ) ;
		waitForBubbleClosed( bubInd ) ;
		bubInd = characterSay( "dartagnan", true, -1, -1, true, getString( "firecannon_2" ) ) ;
		waitForBubbleClosed( bubInd ) ;
		local text = getString( "firecannon_3" ) ;
		if ( getBoolean( "cannonFired" ) ) then
			text = getString( "firecannon_4" ) ;
		end
		local bubInd = characterSay( "cannonguy", true, -1, -1, true, text ) ;
		waitForBubbleClosed( bubInd ) ;
		unfreezeGame( ) ;
		
		setBaseFaceDirection( "cannonguy", 1, 0 ) ;
		setBaseFaceNone( "dartagnan" ) ;
				
		unlockPlayerInPlace( ) ;
		gCannon1Firing = false ;	
		
		setBoolean( "cannonFired", true ) ;
	end
	
	local reloadTime = 2.0 ;
	wait( reloadTime ) ;
	setClickObjectHighlightable( id, true ) ;
end

function activateClock( id, param2, name )
	setClickObjectHighlightable( id, false ) ;

	gClockActivated = true ;
end

function activatePuppy( id, param2, name )
	setClickObjectHighlightable( id, false ) ;

	playSound( "dog", 1.0 ) ;
	
	local reloadTime = 0.5 ;
	wait( reloadTime ) ;
	setClickObjectHighlightable( id, true ) ;
end

function activateConstanceTombstone( id, param2, name )
	setClickObjectHighlightable( id, false ) ;

	addTimedHeartMaker( "dartagnan", 0.5 ) ;
	playSound( "mancrying", 1.0 ) ;
	
	local reloadTime = 2.0 ;
	wait( reloadTime ) ;
	setClickObjectHighlightable( id, true ) ;
end

function explodeBarrel( id, param2, name )
	setClickObjectHighlightable( id, false ) ;
	playSound( "fuse", 1.0 ) ;

	gEspionageBarrelIgnited = true ;
end

function createLoot( id, lootList )
	local positions = {} ;

	for i, v in ipairs( lootList ) do
		local objX, objY = getClickObjectPos( id ) ;
		local level = getClickObjectLevel( id ) ;
		
		local goodDropSearchWidth = getDropWidth( id ) ; --50 ; -- 30
		local goodDropSearchHeight = getDropHeight( id ) ; --15 ;
		local offsetFromObjectX = getDropOffsetX( id ) ; --0 ;
		local offsetFromObjectY = getDropOffsetY( id ) ; --22 ;
		local bestX, bestY = getGoodDropPos( level, 
			objX + offsetFromObjectX, objY + offsetFromObjectY, 
			goodDropSearchWidth, goodDropSearchHeight ) ;
		
		table.insert( positions, { bestX, bestY } ) ;
	
		if ( type( v ) == "number" ) then
			-- money
			createMoney( "", v, level, bestX, bestY, true ) ;
		elseif ( type( v ) == "string" ) then
			-- item
			createItem( "", v, level, bestX, bestY, true ) ;
		end
		
		--wait( 0.2 ) ;
	end
end

function createNamedItem( id, itemName, type )
	local objX, objY = getClickObjectPos( id ) ;
	local level = getClickObjectLevel( id ) ;
	
	local goodDropSearchWidth = 50 ; -- 30
	local goodDropSearchHeight = 15 ;
	local offsetFromObjectY = 22 ;
	local bestX, bestY = getGoodDropPos( level, 
		objX, objY + offsetFromObjectY, 
		goodDropSearchWidth, goodDropSearchHeight ) ;
	
	createItem( itemName, type, level, bestX, bestY, true ) ;
end

function randomTreasure( id )
	local x, y = getClickObjectPos( id ) ;
	local level = getClickObjectLevel( id ) ;
	x = x + randomInt( -30, 31 ) ; --math.random( -30, 30 ) ;
	y = y + randomInt( 15, 31 ) ; --math.random( 15, 30 ) ;
	createMoney( "", randomInt( 100, 501 ), level, x, y, true ) ;
end