function OnSceneLoad(screen)
    game_state = {}    
    LoadData(screen)
    local pf = play_field
    local level = pf:GetLevelScreen()
    game_state.custom_cursors = {}
    game_state.locked_objects = {}
    --level:AddTaskSafe(CreateTask('', '', ''))
    --zone = CreateZoomZone('')
    game_state.locker = ReferenceCounter()
    game_state.active_zone = nil
    game_state.inventory_zones = {}
	game_state.active_objects = {}
	
	--[[AddRandomTasks(6,
               CreateTask('MEDIKIT', '', 'medikit'),
               CreateTask('FLASK', '', 'flask'),
               CreateTask('TORCH', '', 'torch'),
               CreateTask('HAMMER', '', 'hammer'),
               CreateTask('PHONE', '', 'phone'),
               CreateTask('RADIO', '', 'radio')
    )]]
	
	play_field:GetObject('sparkling_place'):AddParticleSystem('data/ParticleSystem/Sparkles.psi',
													         play_field:GetObject('sparkle'):GetAnimationPtr())
															 

	zones = {}
	
	for i = 1, 4 do
		zones[i] = nil
		CreateResistorZone(i)
	end
	
	--zones[1].Unlock()
	
	game_state.resistors_left = 4
	
	local af = CreateAnimationEffector()
	local obj = screen:GetObject("impuls")
	local anim_len = GetAnimationLength(obj:GetAnimation())
	af:SetMode(_ANIM_LOOP)
	af:AttachTo(obj)	
	af:SetLifeTime(0, anim_len)
	af:SetAnimationTime(15 * anim_len)
	af:Play()	
	
	play_field:GetObject('dashboard'):GetAnimation():Stop()
	play_field:GetObject('dashboard'):GetAnimation():SetMode(HGEANIM_NOLOOP+HGEANIM_FWD)
		
	game_state.puzzle_complete = false
    game_state.items_found = false
    
    InitChain1(screen)
	game_state.chain1.final = true
	AddTOEyeLogic()
	AddSheriffEyeLogic()
	
	--game_state.OpenNotebook = OpenNotebook
	--[[OpenNotebook = function()
    end]]
	
	GetGameApp():SetCustomCursor('CursorArrow')
	
	game_state.OnScreenShown = function()
		zones[1].Unlock()
		play_field:GetLevelScreen():GetTaskList():CenterExtraTask()		
	end
	
	game_state.OnMinigameConditionsComplete = function()
	    if game_state.do_not_load_mini_game then
	        return
	    end
	    local anim = play_field:GetObject('dashboard'):GetAnimation()
	    anim:SetFrame(GetAnimationLength(anim) - 2)
	    setFibrillationEffector(play_field:GetObject('svet_ot_lampy'), 0.3)
		setFibrillationEffector(play_field:GetObject('l1'), 0.6)
		setFibrillationEffector(play_field:GetObject('l2'), 0.1)
		play_field:GetObject('l1'):ChangeOpacity(0, 1, 0.5)
		play_field:GetObject('l2'):ChangeOpacity(0, 1, 0.8)
	    load_screen('data/Scenes/Minigames/10_ats.lua');
	end
end

function InitChain1(screen)
    local comics1 = CreateComics(screen,
                                 'Sheriff',
								 'eyes')
								 
	local comics2 = CreateComics(screen,
                                 'Telephone_Operator',
								 'TOMask',
								 'eyes_to')
                                     
	local comics3 = CreateComics(screen,
                                 'Sheriff',
								 'eyes')

    comics3.OnHidden = function()
        --OpenNotebook = game_state.OpenNotebook
        OnWin()
    end
								 
    game_state.chain1 = CreateChain('pixel1',
                                    screen,
                                    comics1, GetString("COMICS_10_3"),
                                    comics2, GetString("COMICS_10_4"),
                                    comics3, GetString("COMICS_10_5"))
end

function CreateResistorZone(i)
	local object = play_field:GetObject('Resistor'..i..'_Found');
	zones[i] = CreateInventory(object:GetX(),
                               object:GetY(),
                               object:GetZ() + 15,
                               "Resistor"..i.."_Found",
                               nil,
                               CreateItem('resistor'..i..'_tool',
                               'resistor'..i),
                               nil,
                               play_field:GetTimer(),
                               "INVENTORY_TASK_10_2");
			
	zones[i].OnComplete = function()
		play_field:GetObject('Resistor'..i..'_Found'):GetAnimation():SetColor('0xFFFFFFFF')
		play_field:GetObject('Resistor'..i..'_Found'):ChangeOpacity(0, 1, 0.3)
		game_state.locked_objects['Resistor'..i..'_Found'] = true
		
		game_state.resistors_left = game_state.resistors_left - 1			

		CheckResistors()
		if i < table.getn(zones) then
			zones[i + 1].Unlock()
			play_field:GetLevelScreen():GetTaskList():CenterExtraTask()
		end
	end
	
	zones[i].Lock = function()
	    if zones[i].IsLocked() then
	        return
	    end
		zones[i].locked = true
		zones[i].bubbles:Hide()
		if play_field:HasObject(zones[i].bound_object) then
		    play_field:IgnoreOnTestPoint(zones[i].bound_object)
		end		
	end
	
	local state_changer_name = "OnZone"..tostring(i).."Complete"
	
	zones[i].state_changer = state_changer_name
	
	game_state[state_changer_name] = function()
		play_field:GetObject('Resistor'..i..'_Found'):GetAnimation():SetColor('0xFFFFFFFF')
		play_field:GetObject('Resistor'..i..'_Found'):SetOpacity(1)
		game_state.locked_objects['Resistor'..i..'_Found'] = true

		CheckResistors()
		if i < table.getn(zones) then
			zones[i + 1].Unlock()
			play_field:GetLevelScreen():GetTaskList():CenterExtraTask()
		end
	end
	
	CreateTool('resistor'..i, 'resistor'..i)
	play_field:GetObject('svet_ot_lampy'):SetOpacity(0)
	play_field:GetObject('l1'):SetOpacity(0)
	play_field:GetObject('l2'):SetOpacity(0)
end

function CheckResistors()
	if game_state.resistors_left == 0 then
		--Start minigame
		play_field:GetLevelScreen():GetTaskList():SetExtraTaskText("")
		game_state.resistors_left = game_state.resistors_left - 1
		--GTimerDoString("load_screen('data/Scenes/Minigames/10_ats.lua')", GetAnimationLength(play_field:GetObject('dashboard'):GetAnimation()) + 0.2);
		TimerDo(play_field:GetTimer(), GetAnimationLength(play_field:GetObject('dashboard'):GetAnimation()) + 0.2, load_screen, 'data/Scenes/Minigames/10_ats.lua')
		local pm = GetProfileManager();
		local currentProfile = pm:GetCurrentProfile();
		currentProfile:AddStateChanger('OnMinigameConditionsComplete')
		OnSceneStateChanged(play_field);
		setFibrillationEffector(play_field:GetObject('svet_ot_lampy'), 0.3)
		setFibrillationEffector(play_field:GetObject('l1'), 0.6)
		setFibrillationEffector(play_field:GetObject('l2'), 0.1)
		play_field:GetObject('l1'):ChangeOpacity(0, 1, 0.5)
		play_field:GetObject('l2'):ChangeOpacity(0, 1, 0.8)
		play_field:GetObject('dashboard'):GetAnimation():Play()
		PlaySound("10_OpenGate");
		play_field:GetLevelScreen():GetPanel():SetAllowInput(false)
	end
end

function OnSceneFree(screen)
    FreeData(screen)
end

function OnUpdate(dt)
    local sparkling_obj = play_field:GetObject('sparkling_place');
    if sparkling_obj ~= nil then
        local particlesystem_sparkling = play_field:GetObject('sparkling_place'):GetParticleSystem();
        if particlesystem_sparkling ~= nil and
           particlesystem_sparkling:GetParticleSystem():GetParticlesAlive() == 0 and
           particlesystem_sparkling:GetParticleSystem():GetAge() == -2.  then
            local random_time = math.random(1, 3);
            TimerDo(play_field:GetTimer(), random_time, GoParticleSparkling );
            play_field:GetObject('sparkling_place'):RemoveParticleSystem();
        end
    end
end

function GoParticleSparkling()
    play_field:GetObject('sparkling_place'):AddParticleSystem('data/ParticleSystem/Sparkles.psi', play_field:GetObject('sparkle'):GetAnimationPtr())
end

function OnScreenShown()
	game_state.OnScreenShown()
	local pm = GetProfileManager();
	local currentProfile = pm:GetCurrentProfile();
	currentProfile:AddStateChanger('OnScreenShown')
	OnSceneStateChanged(play_field);
end

function OnNotebookClosed()
    game_state.items_found = true
    if game_state.puzzle_complete then
        game_state.chain1.Start()
    end
end

function OnNotebookOpened()
end

function OnExit()
end

function OnObjectMouseEnter(object)
    DefaultOnObjectMouseEnter(object)
end

function OnObjectMouseLeave(object)
    DefaultOnObjectMouseLeave(object)
end

function OnObjectMouseDown(object)
    obj_name = object:GetName()
    local pf = play_field
    local is_wrong_click = true

    is_wrong_click = DefaultOnObjectMouseDown(object)

    if is_wrong_click then
        --pf:ShowWrongObjectEffect()
    end

    return is_wrong_click;    
end

function OnObjectMouseUp(object)
end

function OnObjectWillBeFound(object)
end

function OnToolFound(tool)
end

function OnToolApply(tool, object)
    DefaultOnToolApply(tool, object)
end

function OnObjectReachPanel(object)
end

function OnButtonPress(title)
    DefaultOnButtonPress(title)
end

function OnMouseMove(previous_pos, current_pos)
end

function OnSetTool(tool)
end

function CanBeHinted(obj)
    if locked_object[obj] ~= true then
        return true
    else
        return false
    end
end

function OnMinigameCompleted()
    game_state.puzzle_complete = true
    
    --[[local pm = GetProfileManager();
    local currentProfile = pm:GetCurrentProfile();
    currentProfile:AddStateChanger('OnWin')
    OnSceneStateChanged(play_field);]]
    
    local panel = play_field:GetLevelScreen():GetPanel()
    local cover_animation = panel:GetObject("Cover"):GetAnimation()
    if cover_animation:GetFrame() == 0 then
        TimerDo(play_field:GetTimer(), GetAnimationLength(cover_animation), OnWin)
        game_state.chain1.Start()
    end
    
    --if game_state.items_found then
        CloseNotebook()
    
        --game_state.chain1.Start()
		--OnWin()
    --end
end

-- CLEAR_ON_SAVE_BEGIN
-- Do not edit code listed below (generated by the editor on each save)
function LoadData(screen)
	local anim = CreateAnimation('data/Texture/Scenes/10.5_electric_shield_4/Items/flask.png',0,0,64,62,1,0)
	anim:SetHotSpot(32,31)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('flask',anim,409,716,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/10.5_electric_shield_4/Items/radio.png',0,0,72,45,1,0)
	anim:SetHotSpot(36,22)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('radio',anim,947,146,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/10.5_electric_shield_4/Items/resistor2.png',0,0,92,29,1,0)
	anim:SetHotSpot(46,14)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('resistor2',anim,259,419,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/10.5_electric_shield_4/Items/resistor1.png',0,0,67,33,1,0)
	anim:SetHotSpot(33,16)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('resistor1',anim,489,632,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/10.5_electric_shield_4/Items/resistor3.png',0,0,39,71,1,0)
	anim:SetHotSpot(19,35)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('resistor3',anim,880,581,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/10.5_electric_shield_4/Items/resistor4.png',0,0,90,27,1,0)
	anim:SetHotSpot(45,13)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('resistor4',anim,583,749,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/10.5_electric_shield_4/Layers/sprait_chesov.png',0,0,42,48,1,0)
	anim:SetHotSpot(21,24)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('sprait_chesov',anim,200,551,105,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/10.5_electric_shield_4/Layers/sprait_karobki.png',0,0,33,76,1,0)
	anim:SetHotSpot(16,38)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('sprait_karobki',anim,381,713,105,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/10.5_electric_shield_4/Layers/sprait_kirpicha.png',0,0,40,21,1,0)
	anim:SetHotSpot(20,10)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('sprait_kirpicha',anim,883,706,105,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/10.5_electric_shield_4/Layers/sprait_lampy.png',0,0,609,89,1,0)
	anim:SetHotSpot(304,44)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('sprait_lampy',anim,491,58,110,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/10.5_electric_shield_4/Layers/sprait_topora.png',0,0,31,42,1,0)
	anim:SetHotSpot(15,21)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('sprait_topora',anim,820,722,105,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/10.5_electric_shield_4/Layers/sprait_ventilyatora.png',0,0,77,45,1,0)
	anim:SetHotSpot(38,22)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('sprait_ventilyatora',anim,191,731,110,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/10.5_electric_shield_4/Layers/svet_ot_lampy.png',0,0,736,150,1,0)
	anim:SetHotSpot(368,75)
	anim:SetBlendMode(1)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('svet_ot_lampy',anim,491,89,105,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/Common/pixel.jpg',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('sparkling_place',anim,508,441,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Effect/particles.png',64,96,32,32,1,0)
	anim:SetHotSpot(16,16)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('sparkle',anim,584,254,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/10.5_electric_shield_4/Layers/Resistor1_Found.jpg',0,0,44,88,1,0)
	anim:SetHotSpot(22,44)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('Resistor1_Found',anim,708,522,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/10.5_electric_shield_4/Layers/Resistor2_Found.jpg',0,0,27,86,1,0)
	anim:SetHotSpot(13,43)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('Resistor2_Found',anim,777,522,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/10.5_electric_shield_4/Layers/Resistor3_Found.jpg',0,0,43,85,1,0)
	anim:SetHotSpot(21,42)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('Resistor3_Found',anim,976,391,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/10.5_electric_shield_4/Layers/Resistor4_Found.jpg',0,0,87,27,1,0)
	anim:SetHotSpot(43,13)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('Resistor4_Found',anim,745,621,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/10.5_electric_shield_4/Layers/BG.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('BG',anim,512,384,90,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/10.5_electric_shield_4/Items/resistor1.png',0,0,67,33,1,0)
	anim:SetHotSpot(33,16)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('resistor1_tool',anim,971,60,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/10.5_electric_shield_4/Items/resistor2.png',0,0,92,29,1,0)
	anim:SetHotSpot(46,14)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('resistor2_tool',anim,966,106,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/10.5_electric_shield_4/Items/resistor3.png',0,0,39,71,1,0)
	anim:SetHotSpot(19,35)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('resistor3_tool',anim,974,181,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/10.5_electric_shield_4/Items/resistor4.png',0,0,90,27,1,0)
	anim:SetHotSpot(45,13)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('resistor4_tool',anim,959,138,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/10.5_electric_shield_4/Animations/impuls.png',0,0,103,23,10,8)
	anim:SetHotSpot(52,12)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('impuls',anim,742,244,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/10.5_electric_shield_4/Animations/dashboard.dds',0,0,265,292,6,6)
	anim:SetHotSpot(133,146)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('dashboard',anim,510,274,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/Common/pixel.jpg',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('pixel1',anim,512,384,401,0,128,96)
	local anim = CreateAnimation('data/Texture/Comics/Telephone_Operator.png',0,0,473,725,1,0)
	anim:SetHotSpot(236,362)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('Telephone_Operator',anim,775,404,400,0,1,1)
	local anim = CreateAnimation('data/Texture/Comics/TOMask.png',0,0,134,64,1,0)
	anim:SetHotSpot(67,32)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('TOMask',anim,744,125,410,0,1,1)
	local anim = CreateAnimation('data/Texture/Comics/eyes_to.png',0,0,113,30,1,0)
	anim:SetHotSpot(56,15)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('eyes_to',anim,739,130,405,0,1,1)
	local anim = CreateAnimation('data/Texture/Comics/Sheriff.dds',0,0,530,636,1,0)
	anim:SetHotSpot(266,256)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('Sheriff',anim,505,390,400,0,1,1)
	local anim = CreateAnimation('data/Texture/Comics/eyes.png',0,0,79,33,1,0)
	anim:SetHotSpot(39,16)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('eyes',anim,499,212,500,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/10.5_electric_shield_4/Layers/svechenie_lampy.png',0,0,219,206,1,0)
	anim:SetHotSpot(109,103)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('l1',anim,74,80,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/10.5_electric_shield_4/Layers/svechenie_lampy.png',0,0,219,206,1,0)
	anim:SetHotSpot(109,103)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('l2',anim,847,83,100,0,1,1)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
