--------------------------------------------------------------------------------
--  File     : Mill_ext
--  Author(s): Prokopovich Larisa
--
--  Summary  : Скрипт управления локацией.
--
--  Copyright © 2012 Floodlight Games.  All rights reserved.
--------------------------------------------------------------------------------
chance_init_soldier = 3		--максимально возможное количество повторений анимации простоя солдата
chance_action_soldier = 5	--шанс появления анимации действия вместо анимации простоя солдата (1 к chance_action_soldier)
chance_init_woman = 3		--максимально возможное количество повторений анимации простоя женщины
chance_action_woman = 5		--шанс появления анимации действия вместо анимации простоя женщины (1 к chance_action_woman)
--------------------------------------------------------------------------------
function OnSerialize(system)
	chance_soldier = system:Serialize(chance_soldier)
	chance_dt_soldier = system:Serialize(chance_dt_soldier)
	chance_woman = system:Serialize(chance_woman)
	chance_dt_woman = system:Serialize(chance_dt_woman)
end
--------------------------------------------------------------------------------
function OnFirstEntry()
	if not(cstore:IsKeyExists("Cheat_MillExt2")) then 	 		--!!! (условие для читов)
		room.sfx:PlayEx ("ghosts_in_village", true, 1, 0)	--звук "привидения шепчут"
		comment:Add("CMT_Ghosts1_Player")
	end
	
	chance_soldier = 0
	chance_dt_soldier = 0
	chance_woman = 0
	chance_dt_woman = 0
	
	room:GetItem("M_TO_MILL_INT"):Disable()
	
	stars:Show("M_TO_DAM", "3")
	stars:Show("M_TO_MILL_INT", "3")
	stars:Show("M_CELLAR", "2")
	stars:Show("CIRCLE", "4")
end
--------------------------------------------------------------------------------
function OnEntry()
	ambient:Play("Mill_ext")
end
--------------------------------------------------------------------------------
function OnMouseClickOnItem(item)
	local itemName = item:GetName()
	
	if itemName == "M_TO_STREET" then						--переход в деревню
		chapter:JumpRoom("Village_street")
		return true
	elseif itemName == "M_TO_DAM" then						--переход к дамбе
		chapter:JumpRoom("Dam")
		return true 
	elseif itemName == "M_TO_MILL_INT" then					--переход в мельницу
		if item:IsEnabled() then
			chapter:JumpRoom("Mill_int")
		else
			comment:Add("CMT_Lock_Player")
			if cstore:GetKey("Boat_HO") < 2 then
				cstore:SetKey("Boat_HO", 2)
			end
		end
		return true
	
	elseif itemName == "M_CELLAR"  then						--переход в close-up с погребом
		chapter:JumpCloseup("Mill_ext_Cellar")
		return true	
	end
	
	if itemName == "M_GHOSTS"  then					--духи	
		comment:Add("CMT_Ghosts2_Player")
		return true	
	
	elseif itemName == "CIRCLE"  then				--кольцо для двери в церкви
		pick_panel:AddItems(itemName, false)
		return true	
	end
end
--------------------------------------------------------------------------------
function OnClickItemOnItem(item)					
	local itemName = item:GetName()
	
	if itemName == "M_CELLAR"  then							--переход в close-up с погребом
		chapter:JumpCloseup("Mill_ext_Cellar")
		return true	
	end
	
	if itemName == "M_TO_MILL_INT" then				--лом на дверь
		if cursor:IsItemNameEqual("CROWBAR") then	
			cursor:RemoveItem()
			room:GetItem("PADLOCK"):Hide()
			local lock_break = room:GetItem("LOCK_BREAK")
			lock_break:Show()
			lock_break:PlayAnimation()
			room:AddCtrlAnimation("LOCK_BREAK", -1)
			room.sfx:Play ("unlock")
			return true
		end
	end
end
--------------------------------------------------------------------------------
function OnUpdate (elapsedTime)
	
	if room:GetItem("M_GHOSTS"):IsVisible() then
		
		local ghost_soldier_action = room:GetItem("GHOSTSOLDIERACTION")
		local ghost_soldier_idle = room:GetItem("GHOSTSOLDIERIDLE")
		local ghost_soldier_walk = room:GetItem("GHOSTSOLDIERWALK")
		local ghost_woman_action = room:GetItem("GHOSTWOMANACTION")
		local ghost_woman_idle = room:GetItem("GHOSTWOMANIDLE")
		local ghost_woman_walk = room:GetItem("GHOSTWOMANWALK")
		
		if (ghost_soldier_walk:IsVisible()) and not(ghost_soldier_walk:IsAnimPlaying()) then
			ghost_soldier_walk:Hide()
						
			chance_soldier = math.random(chance_init_soldier)
			chance_dt_soldier = 0
			
			if math.random(chance_action_soldier) == 1 then
				ghost_soldier_action:Show()
				ghost_soldier_action:PlayAnimation()
			else
				ghost_soldier_idle:Show()
				ghost_soldier_idle:PlayAnimation()
			end
		end
		
		if (ghost_soldier_idle:IsVisible()) and not(ghost_soldier_idle:IsAnimPlaying()) or
		   (ghost_soldier_action:IsVisible()) and not(ghost_soldier_action:IsAnimPlaying()) then
			ghost_soldier_idle:Hide()
			ghost_soldier_action:Hide()
			
			chance_dt_soldier = chance_dt_soldier + 1
			
			if chance_dt_soldier < chance_soldier then
				if math.random(chance_action_soldier) == 1 then
					ghost_soldier_action:Show()
					ghost_soldier_action:PlayAnimation()
				else
					ghost_soldier_idle:Show()
					ghost_soldier_idle:PlayAnimation()
				end
			else
				ghost_soldier_walk:Show()
				ghost_soldier_walk:PlayAnimation()
			end
		end
		
		if (ghost_woman_walk:IsVisible()) and not(ghost_woman_walk:IsAnimPlaying()) then
			ghost_woman_walk:Hide()
						
			chance_woman = math.random(chance_init_woman)
			chance_dt_woman = 0
			
			if math.random(chance_action_woman) == 1 then
				ghost_woman_action:Show()
				ghost_woman_action:PlayAnimation()
			else
				ghost_woman_idle:Show()
				ghost_woman_idle:PlayAnimation()
			end
		end
		
		if (ghost_woman_idle:IsVisible()) and not(ghost_woman_idle:IsAnimPlaying()) or
		   (ghost_woman_action:IsVisible()) and not(ghost_woman_action:IsAnimPlaying()) then
			ghost_woman_idle:Hide()
			ghost_woman_action:Hide()
			
			chance_dt_woman = chance_dt_woman + 1
			
			if chance_dt_woman < chance_woman then
				if math.random(chance_action_woman) == 1 then
					ghost_woman_action:Show()
					ghost_woman_action:PlayAnimation()
				else
					ghost_woman_idle:Show()
					ghost_woman_idle:PlayAnimation()
				end
			else
				ghost_woman_walk:Show()
				ghost_woman_walk:PlayAnimation()
			end
		end
	end
	
	--убрать закрытую дверь
	local door_closed = room:GetItem("DOOR_CLOSED")
	if (door_closed:IsVisible()) and not(door_closed.effects:IsPlayingColor ()) then
		local color = door_closed:GetColor()
		if color:GetNormalAlpha() == 0 then
			door_closed:Hide()
		end
	end
	
end
--------------------------------------------------------------------------------
-- Окончание анимации взламывания замка
function OnAnimationComplete_LOCK_BREAK(forced)
	room:GetItem("LOCK_BREAK"):Hide()
	room:GetItem("M_TO_MILL_INT"):Enable()
	
	local door_closed = room:GetItem("DOOR_CLOSED")	
	if forced == true then
		door_closed:Hide()
	else
		local color = door_closed:GetColor()
		color:SetNormalAlpha(0)
		door_closed.effects:PlayColor(color, 0.5)
		room.sfx:Play ("open_door")
	end
end
--------------------------------------------------------------------------------
function OnChapterStoreEvent_Close_Cellar(value)			--закрыть в close-up с погребом
	if value == 1 then
		room:GetItem("CELLAR_OPEN"):Show()
	elseif value == 2 then
		room:GetItem("M_CELLAR"):Hide()
	end
end
--------------------------------------------------------------------------------
function OnChapterStoreEvent_Mill_Ghosts(value)				--убрать духов
	room:GetItem("M_GHOSTS"):Hide()
		
	room:GetItem("GHOSTPEASANT"):Hide()
	room:GetItem("GHOSTSOLDIERACTION"):Hide()
	room:GetItem("GHOSTSOLDIERIDLE"):Hide()
	room:GetItem("GHOSTSOLDIERWALK"):Hide()
	room:GetItem("GHOSTWOMANACTION"):Hide()
	room:GetItem("GHOSTWOMANIDLE"):Hide()
	room:GetItem("GHOSTWOMANWALK"):Hide()
		
	room.sfx:Stop("ghosts_in_village")
		
	room:GetItem("M_TO_DAM"):Show()
	room:GetItem("M_TO_MILL_INT"):Show()
	room:GetItem("M_CELLAR"):Show()
end
--------------------------------------------------------------------------------
function OnChapterStoreEvent_Explosion_Tower(value)
	room:GetItem("DAM_DESTROYED"):Show()
end
--------------------------------------------------------------------------------
function OnChapterStoreEvent_Cheat_MillExt1(value)				--выполнить все в локации  !!!
	cstore:SetKey("Mill_Ghosts", true)
end
function OnChapterStoreEvent_Cheat_MillExt2(value)				
	room:GetItem("CIRCLE"):Hide()
	cstore:SetKey("Close_Cellar", 1)
	cstore:SetKey("Close_Cellar", 2)
	room:GetItem("M_TO_MILL_INT"):Enable()
	room:GetItem("DOOR_CLOSED"):Hide()
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("M_DIG_PLACE2")				
	
	--локация 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)
	--локация 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)
	--локация Chapel_ext
	cstore:SetKey("Cheat_ChapelExt", true)
	--локация Chapel_int
	cstore:SetKey("Cheat_ChapelInt1", true)
	--локация Cemetery
	cstore:SetKey("Cheat_Cemetery", true)
	--локация Village_street
	cstore:SetKey("Cheat_Village1", true)
	--локация Mill_ext
	cstore:SetKey("Cheat_MillExt1", true)
	--локация Dam
	cstore:SetKey("Cheat_Dam1", true)
end
