--------------------------------------------------------------------------------
--  File     : Wizard's_tower_int
--  Author(s): Prokopovich Larisa
--
--  Summary  : Скрипт управления локацией.
--
--  Copyright © 2012 Floodlight Games.  All rights reserved.
--------------------------------------------------------------------------------
function OnSerialize(system)
	if room:GetItem("M_MANHOLE"):IsVisible() then
		local water = room:GetItem("TOWER_WATER_FLUSH")
		if water:IsVisible() then
			water:Hide()
		end
	end
		
	local manhole = room:GetItem("MANHOLE")
	if manhole:IsVisible() then
		manhole:Hide()
		room:GetItem("MANHOLE_OPENED"):Show()
		room:GetItem("WINGS_SWITCH"):Enable()
	end
end
--------------------------------------------------------------------------------
function OnFirstEntry()
	room:GetItem("WINGS_SWITCH"):Disable()
	room:GetItem("M_CONSOLE"):Disable()
	
	local raven = room:GetItem("TOWER_RAVEN")
	raven:PlayAnimation()
	raven:SetAnimPause(true)
		
	stars:Show("M_CONSOLE", "4")
	stars:Show("TOWER_RAVEN", "4")
	stars:Show("MAZE_SLAB", "4")
	stars:Show("M_BULB", "2")
	stars:Show("M_MANHOLE", "4")
	stars:Show("HANDCUFFS", "4")
	stars:Show("WINGS_SWITCH", "4")
	
	music:Stop(true)
	music:PlayLoopWithDelay(5, "reprise_1")
	room.sfx:Play ("jingle_10_boom")	--звук джингла при первом входе
	
	room.sfx:PlayEx ("water_loop", true, 1, 0)	--звук "булькает вода"
end
--------------------------------------------------------------------------------
function OnEntry()
	ambient:Play("Wizard's_tower_int")
	music:PlayLoopWithDelay(40, "rare_1") 
end
--------------------------------------------------------------------------------
function OnMouseClickOnItem(item)
	local itemName = item:GetName()
	
	if itemName == "M_TO_FOG_TOWER" then									--выход из башни	
		chapter:JumpRoom("Wizard's_tower_ext")
		return true
		
	elseif itemName == "MAZE_SLAB" then										--миниигра Лабиринт
		chapter:JumpRoom("Wizard's_tower_int_Maze_MG")
		return true
		
	elseif itemName == "M_TO_LAB" then										--переход в лабораторию
		chapter:JumpRoom("Wizard's_tower_lab")
		return true
		
	elseif itemName == "M_BULB" then										--переход в close-up с колбой
		chapter:JumpCloseup("Wizard's_tower_int_Tank")						
		return true	
		
	elseif itemName == "M_CONSOLE" then										--пульт
		if item:IsEnabled() then
			chapter:EnableCloseupShadow(false)
			chapter:JumpCloseup("Wizard's_tower_int_Console")
		else
			comment:Add("CMT_Console_Player")
		end
		return true	
	end
	
	
	if 	itemName == "TOWER_RAVEN" then					--ворон
		comment:Add("CMT_Raven_Player")
		return true

	elseif 	itemName == "TOWER_WATER" then				--вода
		comment:Add("CMT_Water_Player")
		return true

	elseif 	itemName == "M_MIRRORS" then				--зеркала
		comment:Add("CMT_Mirrors_Player")
		return true
		
	elseif 	itemName == "HANDCUFFS" then				--кандалы
		pick_panel:AddItems(itemName, false)
		return true
		
	elseif 	itemName == "M_MANHOLE" then				--сломанный люк
		comment:Add("CMT_Manhole_Player")
		return true

	elseif 	itemName == "WINGS_SWITCH" then				--деталь от пульта  ворона
		if item:IsEnabled() then     			
			pick_panel:AddItems(itemName, false)
		else
			comment:Add("CMT_Wingswitch_Player")
		end
		return true
	end
end
--------------------------------------------------------------------------------
function OnClickItemOnItem(item)					
	local itemName = item:GetName()
	
	if itemName == "M_BULB" then
		chapter:JumpCloseup("Wizard's_tower_int_Tank")						--переход в close-up с колбой
		return true	
		
	elseif (itemName == "M_CONSOLE") and
		   (item:IsEnabled()) then
		chapter:EnableCloseupShadow(false)
		chapter:JumpCloseup("Wizard's_tower_int_Console")					--переход в close-up с пультом
		return true	
	end
	
	if itemName == "M_MANHOLE" then						--кандалы на люк
		if cursor:IsItemNameEqual("HANDCUFFS") then			
			cursor:RemoveItem()
			item:Hide()
			room:GetItem("CHAIN_BROKEN"):Hide()
			room:GetItem("CHAIN"):Show()
			room.sfx:Play ("chain_part")	--звук "прицепляем звено цепи"
			cstore:SetKey("Tower_chain", true)
			return true
		end
	end
end
--------------------------------------------------------------------------------
function OnUpdate(elapsedTime)
	if cstore:GetKey("Close_up_Console") == 2 then					
		local door = room:GetItem("TOWER_LAB_DOOR")
	   
		if not(door:IsVisible()) then
			local raven = room:GetItem("TOWER_RAVEN")
			
			local frame = room:GetVideoFrame(raven:GetName())
				
			if frame >= 84 then
				room:GetItem("M_TO_LAB"):Show()
				
				door:Show()
				door:PlayAnimation()
				room:AddCtrlAnimation("TOWER_LAB_DOOR", -1)
			end
		end
	end
end
--------------------------------------------------------------------------------
function OnChapterStoreEvent_TowerIntMG(value)								--миниигра пройдена
	chapter:EnableCloseupShadow(false)
	chapter:JumpCloseup("Wizard's_tower_int_Console")
	
	room:GetItem("MAZE_SLAB"):Hide()
	room:GetItem("M_CONSOLE"):Enable()
	stars:Hide("M_CONSOLE")
	stars:Show("M_CONSOLE", "2")
end
--------------------------------------------------------------------------------
function OnChapterStoreEvent_Water_flush(value)								--сливаем воду
	room:GetItem("M_BULB"):Show()
	room:GetItem("M_MANHOLE"):Show()
	room:GetItem("WINGS_SWITCH"):Show()
	room:GetItem("HANDCUFFS"):Show()
		
	local bulbs_part = room:GetItem("BULBS_PART")
	bulbs_part:Show()
	bulbs_part:PlayAnimation()
	
	local tank_glow = room:GetItem("TANK_GLOW")
	tank_glow:Show()
	tank_glow:PlayAnimation()
	
	room:GetItem("WATER_GLOW"):Hide()
	
	room:GetItem("TOWER_WATER"):Hide()
	local videoWater = room:GetItem("TOWER_WATER_FLUSH")
	videoWater:Show()
	videoWater:PlayAnimation()
	room:AddCtrlAnimation("TOWER_WATER_FLUSH", -1)

	music:Stop(true) 
	music:PlayLoopWithDelay(5, "horror_2")

	room.sfx:Stop("water_loop")
end
--------------------------------------------------------------------------------
function OnChapterStoreEvent_Close_up_Console(value)						--закрыть close-up с пультом
	if value == 1 then
		room:GetItem("CHAIN"):Hide()
		room:GetItem("MANHOLE_CLOSED"):Hide()
		
		local manhole = room:GetItem("MANHOLE")
		manhole:Show()
		manhole:PlayAnimation()
		room:AddCtrlAnimation("MANHOLE", -1)

	elseif value == 2 then
		room:GetItem("M_MIRRORS"):Hide()
		room:GetItem("M_CONSOLE"):Hide()
		room:GetItem("TOWER_RAVEN"):SetAnimPause(false)
		room:AddCtrlAnimation("TOWER_RAVEN", -1)
		room.sfx:Play ("console_turn")	--звук "клацаем по контрольной панели "
		room.sfx:Play ("open_statue")	--звук "анимация ворона"
	end
end
--------------------------------------------------------------------------------
function OnChapterStoreEvent_Close_up_Tank(value)							--закрыть close-up с колбой
	if value == 1 then
		room:GetItem("BULB_CLOSED"):Hide()
		room:GetItem("BULB_OPENED"):Show()
		room:GetItem("TANK_GLOW"):Hide()
	elseif value == 2 then
		room:GetItem("M_BULB"):Hide()
	end
end
--------------------------------------------------------------------------------
function OnReturnFromCloseup(closeupName)
	if closeupName == "Wizard's_tower_int_Console" then
		chapter:EnableCloseupShadow(true)
	end
end
--------------------------------------------------------------------------------
-- Окончание анимации поднятия люка
function OnAnimationComplete_MANHOLE(forced)
	room:GetItem("MANHOLE"):Hide()
	room:GetItem("MANHOLE_OPENED"):Show()
	room:GetItem("WINGS_SWITCH"):Enable()
end
--------------------------------------------------------------------------------
-- Окончание анимации сливания воды
function OnAnimationComplete_TOWER_WATER_FLUSH(forced)
	room:GetItem("TOWER_WATER_FLUSH"):Hide()
end
--------------------------------------------------------------------------------
-- Окончание анимации поднятия крыльев
function OnAnimationComplete_TOWER_RAVEN(forced)
	
	if forced == true then
		room:VideoSeekToEnd(room:GetItem("TOWER_RAVEN"))
		
		room:GetItem("M_TO_LAB"):Show()
		stars:Show("M_TO_LAB", "3")
		
		local door = room:GetItem("TOWER_LAB_DOOR")
		door:Show()
		door:PlayAnimation()
		room:VideoSeekToEnd(door)
	end
end
--------------------------------------------------------------------------------
-- Окончание анимации открытия двери
function OnAnimationComplete_TOWER_LAB_DOOR(forced)
	stars:Show("M_TO_LAB", "3")
	
	if forced == true then
		room:VideoSeekToEnd(room:GetItem("TOWER_LAB_DOOR"))
	end
end
--------------------------------------------------------------------------------
function CheatSkip () 											--выполнить все в предыдущих локациях   !!!
	--добавить инвентарные предметы
	inventory:CheatLoadAndAddItem ("SPADE_Q", "Hut_1st_floor", 0)
	inventory:CheatLoadAndAddItem ("KNIFE_Q", "Hut_1st_floor_Cabinet", 0)
	
	if inventory:IsItemExisting("MATCHES") then
		inventory:EraseOneItemFromCell("MATCHES")
	end
	if inventory:IsItemExisting("KNIFE") then
		inventory:EraseOneItemFromCell("KNIFE")
	end

	-- отключить ветку хинтов
	hint:Cheat("NOTE_RUNES")
	hint:Cheat("EXPLOSION")				
	
	--локация Gate
	cstore:SetKey("Cheat_Gate", true)
	--локация Hut_1st_floor
	cstore:SetKey("Cheat_Hut1stFloor1", true)
	cstore:SetKey("Cheat_Hut1stFloor2", true)
	--локация Hut_2nd_floor
	cstore:SetKey("Cheat_Hut2ndFloor", true)
	--локация Scary_glade
	cstore:SetKey("Cheat_ScaryGlade1", true)
	cstore:SetKey("Cheat_ScaryGlade2", true)
	cstore:SetKey("Cheat_ScaryGlade3", true)
	--локация Witch's_house_ext
	cstore:SetKey("Cheat_WitchHouseExt1", true)
	cstore:SetKey("Cheat_WitchHouseExt2", true)
	cstore:SetKey("Cheat_WitchHouseExt3", true)
	--локация Witch's_house_int
	cstore:SetKey("Cheat_WitchHouseInt1", true)
	cstore:SetKey("Cheat_WitchHouseInt2", true)
	cstore:SetKey("Cheat_WitchHouseIntBook", true)
	--локация Scary_place_exit
	cstore:SetKey("Cheat_ScaryPlaceExit1", true)
	cstore:SetKey("Cheat_ScaryPlaceExit2", true)
	cstore:SetKey("Cheat_ScaryPlaceExit3", true)
	--локация Tavern_ext
	cstore:SetKey("Cheat_TavernExt", true)
	--локация Tavern_int
	cstore:SetKey("Cheat_TavernInt", true)
	--локация Bridge
	cstore:SetKey("Cheat_Bridge1", true)
	cstore:SetKey("Cheat_Bridge2", true)
	--локация Chapel_ext
	cstore:SetKey("Cheat_ChapelExt", true)
	--локация Chapel_int
	cstore:SetKey("Cheat_ChapelInt1", true)
	cstore:SetKey("Cheat_ChapelInt2", true)
	--локация Cemetery
	cstore:SetKey("Cheat_Cemetery", true)
	--локация Village_street
	cstore:SetKey("Cheat_Village2", true)
	--локация Smithy
	cstore:SetKey("Cheat_Smithy", true)
	--локация Mill_ext
	cstore:SetKey("Cheat_MillExt1", true)
	cstore:SetKey("Cheat_MillExt2", true)
	--локация Mill_int
	cstore:SetKey("Cheat_MillInt", true)
	--локация Dam
	cstore:SetKey("Cheat_Dam2", true)
	--локация Wizard's_tower_ext
	cstore:SetKey("Cheat_Wizard_tower", true)
end
