
function OnSceneLoad(screen)
    LoadData(screen)

    g_screen = screen

    custom_cursors = {}
    locked_objects = {}
        
    OBJECTS_AMOUNT = 7
    Objects = {}
    
    for i = 1, OBJECTS_AMOUNT do
        Objects[i] = {}
        Objects[i].Entity = screen:GetObject('Obvodka_'..i)
        Objects[i].Area = screen:GetObject('Differens_'..i)
        Objects[i].Entity2 = screen:GetObject('Obvodka2_' .. i)
        Objects[i].Area2 = screen:GetObject('Differens2_' .. i)
        Objects[i].Found = false
        Objects[i].Index = i
        Objects[i].Entity:SetOpacity(0)
        Objects[i].Entity2:SetOpacity(0)
    end
    
    count_found = 0
    mouse_lock = nil
    
    txt = CreateTextEntity('Default', count_found..'/'..OBJECTS_AMOUNT);
    txt:AttachTo(screen:GetObject('Timer_diff'));
    txt:SetFont("Arial20");
    txt:SetZ(50);
    txt:SetScrXY(48, 48);
    
    g_screen = screen
    g_timer = screen:GetTimer()
    
    game_state.custom_cursor = GetGameApp():IsCustomCursorVisible()
    game_state.timeElapsed = 0.0;
    game_state.punishmentEnable = true
    game_state.now_punish = false
    game_state.punishmentDuration = 3.0;
	game_state.punishmentClickCount = 5;
	game_state.prevWrongClickTimeStamp = {-game_state.punishmentDuration,
                                          -game_state.punishmentDuration,
                                          -game_state.punishmentDuration,
                                          -game_state.punishmentDuration,
                                          -game_state.punishmentDuration,
                                          -game_state.punishmentDuration};
    
    screen:IgnoreOnTestPoint('area_menu')
    screen:IgnoreOnTestPoint('area_hint')
    
    screen:GetObject('Mouse_area'):SetXY(0, 0)
    if not game_state.custom_cursor then
        screen:GetObject('Mouse_area'):Hide()
        MouseAreaLock(false)
    else
        MouseAreaLock(true)
    end
end

function MouseAreaLock(enable)
    if enable == true then
        g_screen:GetObject('Cursor_mirror'):Show()
        mouse_lock = true
        g_screen:DoNotIgnoreOnTestPoint('area_menu')
        g_screen:DoNotIgnoreOnTestPoint('area_hint')
        g_screen:GetObject('Cursor_mirror'):Show()
    else
        g_screen:GetObject('Cursor_mirror'):Hide()
        mouse_lock = false
        g_screen:IgnoreOnTestPoint('area_menu')
        g_screen:IgnoreOnTestPoint('area_hint')
        g_screen:GetObject('Cursor_mirror'):Hide()
    end
end

function GetObjectByName(obj_name)
    for i = 1, OBJECTS_AMOUNT do
        if obj_name == Objects[i].Entity:GetName() then
            return Objects[i]
        end
    end
    
    return nil
end

function GetObjectByAreaName(area_name)
    for i = 1, OBJECTS_AMOUNT do
        if area_name == Objects[i].Area:GetName() or area_name == Objects[i].Area2:GetName() then
            return Objects[i]
        end
    end
    
    return nil
end

function OnSceneFree()
    FreeData()
end

function OnScreenShown()
end

function ShowMiniGameHelp()
    CreateDialogMiniGameHelp('MINI_GAME_HELP_ARMOR')
end

function OnUpdate(dt)
    game_state.timeElapsed = game_state.timeElapsed + dt;

    if g_screen:IsInputAllowed() and mouse_lock then
        local x, y
        if not game_state.now_punish then
            --x, y = MouseHolder:GetX(), MouseHolder:GetY()
            x, y = GetMousePos().x, GetMousePos().y
            if x < 515 then
                x = 515
                SetMousePos(x, y)
            end
        else
            x, y = g_screen:GetObject('Mouse_area'):GetX(), g_screen:GetObject('Mouse_area'):GetY()
        end
        g_screen:GetObject('Cursor_mirror'):SetXY(x - 460, y + 15)
    end
end

function OnLose()
end

function OnExit()
end

function OnObjectMouseEnter(object)
    DefaultOnObjectMouseEnter(object)
end

function OnObjectMouseLeave(object)
    DefaultOnObjectMouseLeave(object)
end

function MarkFound(index)
    Objects[index].Found = true
    Objects[index].Area:SetVisible(false)
    Objects[index].Area2:SetVisible(false)

    Objects[index].Entity:ChangeOpacity(0, 1, 0.5)
    Objects[index].Entity2:ChangeOpacity(0, 1, 0.5)
    --Objects[index].Entity:GetAnimation():SetColor('0xFFFFFFFF')

    count_found = count_found + 1;
    txt:SetText(count_found..'/'..OBJECTS_AMOUNT);
    
    PlaySound("item_found")
    
    if IsCompleted() then
        FinishMiniGame()
        MiniGameCompleted(0.5)
    end
end

function IsCompleted()
    for i = 1, OBJECTS_AMOUNT do
        if not Objects[i].Found then
            return false
        end
    end
    
    return true
end

function FinishMiniGame()
    game_state.punishmentEnable = false
    for i = 1, OBJECTS_AMOUNT do
        Objects[i].Found = true
    end
    count_found = OBJECTS_AMOUNT
    txt:SetText(count_found..'/'..OBJECTS_AMOUNT);

    for i = 1, OBJECTS_AMOUNT do
        do
            local af = CreateAnimationEffector()
            af:AttachTo(Objects[i].Entity)
            af:SetKey(_TRACK_O, 0, 1)
            af:SetKey(_TRACK_O, 0.1, 0.2)
            af:SetKey(_TRACK_O, 0.3, 1)
            af:SetKey(_TRACK_O, 0.4, 0.2)
            af:SetKey(_TRACK_O, 0.6, 1)
            af:SetKey(_TRACK_O, 0.7, 0.2)
            af:SetKey(_TRACK_O, 0.9, 1)
            af:Play()
        end
        do
            local af = CreateAnimationEffector()
            af:AttachTo(Objects[i].Entity2)
            af:SetKey(_TRACK_O, 0, 1)
            af:SetKey(_TRACK_O, 0.1, 0.2)
            af:SetKey(_TRACK_O, 0.3, 1)
            af:SetKey(_TRACK_O, 0.4, 0.2)
            af:SetKey(_TRACK_O, 0.6, 1)
            af:SetKey(_TRACK_O, 0.7, 0.2)
            af:SetKey(_TRACK_O, 0.9, 1)
            af:Play()
        end
    end
    
    MouseAreaLock(false)
end

function OnSkipMiniGame()
    if game_state.minigame_complete then
        return
    end

    FinishMiniGame()
    MiniGameCompleted(1.0)
end

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

    if game_state.now_punish then
        return false
    end
    
    obj = GetObjectByAreaName(obj_name)
    if obj and (not obj.Found) and (not game_state.now_punish) then
        MarkFound(obj.Index)
        is_wrong_click = false
        return is_wrong_click
    end
    
    -- Jerk with cursor emulation
    if obj_name == "area_menu" then
        OnButtonPress('MenuButtonMinigame')
        return is_wrong_click
    elseif obj_name == "area_hint" then
        OnButtonPress('HelpButtonMinigame')
        return is_wrong_click
    end
    
    -- Punishment for clicking
	if game_state.punishmentEnable and (object:GetName() == 'Punish_area' or not game_state.custom_cursor) and not game_state.now_punish then
	    assert(game_state ~= nil and 
			   game_state.timeElapsed ~= nil and game_state.prevWrongClickTimeStamp ~= nil and
               game_state.punishmentDuration ~= nil and game_state.punishmentClickCount ~= nil);
		assert(table.getn(game_state.prevWrongClickTimeStamp) > game_state.punishmentClickCount and
			   table.getn(game_state.prevWrongClickTimeStamp) < 10);

		for i = table.getn(game_state.prevWrongClickTimeStamp), 2, -1 do
			game_state.prevWrongClickTimeStamp[i] = game_state.prevWrongClickTimeStamp[i - 1];
		end
		game_state.prevWrongClickTimeStamp[1] = game_state.timeElapsed;

		if (game_state.prevWrongClickTimeStamp[1] - game_state.prevWrongClickTimeStamp[game_state.punishmentClickCount] < game_state.punishmentDuration) then
		    PunishClicking()
       		for i = table.getn(game_state.prevWrongClickTimeStamp), 2, -1 do
		        game_state.prevWrongClickTimeStamp[i] = -game_state.punishmentDuration;
	        end
		end
	end
    
    if is_wrong_click then
        PlaySound('wrong_click')
    end
    return is_wrong_click;
end

function PunishClicking()
    local curMouseX, curMouseY = MouseHolder:GetX(), MouseHolder:GetY()
    local endMouseX, endMouseY = 0, 0
        
    if (curMouseX < 512) then endMouseX = math.random(600, 900)
    else endMouseX = math.random(100, 500) end
    
    if (curMouseY < 384) then endMouseY = math.random(450, 668)
    else endMouseY = math.random(100, 300) end    
    
    local eff, eff1 = BuildCursorTrajectory(curMouseX, curMouseY, endMouseX, endMouseY)
    
    eff:AttachTo(MouseHolder)
    eff:Play()
    
    g_screen:GetObject('Mouse_area'):SetXY(MouseHolder:GetX(), MouseHolder:GetY())
    eff1:AttachTo(g_screen:GetObject('Mouse_area'))
    eff1:Play()
    
    PlaySound('mouse_punish')
            
    game_state.now_punish = true
    TimerDoString("game_state.now_punish = false", 1.55, g_timer)
end

function OnObjectMouseUp(object)
end

function OnObjectRightMouseDown(object)
end

function OnObjectRightMouseUp(object)
end

function OnObjectFound(object)
end

function OnToolFound(tool)
end

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

function OnObjectReachPanel(object)
end

function OnPhotographing(phone_screen_center_pos)
	DefaultOnPhotographing(phone_screen_center_pos);
end

function OnButtonPress(title)
    DefaultMinigameOnButtonPress(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()
end

function OnLostFocus()
    DefaultMinigameOnLostFocus()
end

function OnGainFocus()
    game_state.custom_cursor = GetGameApp():IsCustomCursorVisible()
    if not game_state.custom_cursor then
        g_screen:GetObject('Mouse_area'):Hide()
        MouseAreaLock(false)
    else
        g_screen:GetObject('Mouse_area'):Show()
        MouseAreaLock(true)
    end
    
    DefaultMinigameOnGainFocus()
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/Minigames/14_armor/background.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Background',anim,512,384,95,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/4_ferruginous_ship_Diff/Obvodka02.png',0,0,74,58,1,0)
	anim:SetHotSpot(37,29)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Obvodka_1',anim,667,526,105,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/4_ferruginous_ship_Diff/Obvodka02.png',0,0,74,58,1,0)
	anim:SetHotSpot(37,29)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Obvodka_2',anim,676,316,105,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/4_ferruginous_ship_Diff/Obvodka02.png',0,0,74,58,1,0)
	anim:SetHotSpot(37,29)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Obvodka_3',anim,668,414,105,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/4_ferruginous_ship_Diff/Obvodka01.png',0,0,90,70,1,0)
	anim:SetHotSpot(45,35)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Obvodka_4',anim,811,543,105,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/4_ferruginous_ship_Diff/Obvodka02.png',0,0,74,58,1,0)
	anim:SetHotSpot(37,29)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Obvodka_5',anim,866,225,105,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/4_ferruginous_ship_Diff/Obvodka03.png',0,0,129,86,1,0)
	anim:SetHotSpot(64,43)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Obvodka_6',anim,828,126,105,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/4_ferruginous_ship_Diff/Obvodka02.png',0,0,74,58,1,0)
	anim:SetHotSpot(37,29)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Obvodka_7',anim,612,296,105,-90,1,1)
	local anim = CreateAnimation('data/Texture/Cursors/Kursor02.png',0,0,39,33,1,0)
	anim:SetHotSpot(19,16)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Cursor_mirror',anim,312,384,650,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('Differens_1',anim,667,525,110,0,9,7)
	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('Differens_2',anim,679,316,110,0,9,7)
	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('Differens_3',anim,668,414,110,0,9,7)
	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('Differens_4',anim,807,540,110,0,10.5,8.5)
	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('Differens_5',anim,865,226,110,0,9,7)
	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('Differens_6',anim,830,128,110,0,15,10)
	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('Differens_7',anim,610,296,110,0,7,9)
	local anim = CreateAnimation('data/Texture/Scenes/Common/area.png',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('Punish_area',anim,743,368,101,0,53.5,82)
	local anim = CreateAnimation('data/Texture/Scenes/Common/timer_diff.png',0,0,114,123,1,0)
	anim:SetHotSpot(57,61)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Timer_diff',anim,55,60,150,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('area_menu',anim,537,741,120,0,10,6)
	local anim = CreateAnimation('data/Texture/Scenes/Common/circle.png',0,0,128,128,1,0)
	anim:SetHotSpot(64,64)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_hint',anim,610,742,120,0,0.4,0.4)
	local anim = CreateAnimation('data/Texture/Scenes/Common/area.png',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('Mouse_area',anim,512,384,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('0x01FFFFFF')
	screen:CreateObject('Differens2_1',anim,207,525,110,0,9,7)
	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('Differens2_2',anim,206,316,110,0,9,7)
	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('Differens2_3',anim,195,414,110,0,9,7)
	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('Differens2_4',anim,336,540,110,0,10.5,8.5)
	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('Differens2_5',anim,371,226,110,0,9,7)
	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('Differens2_6',anim,359,128,110,0,15,10)
	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('Differens2_7',anim,136,296,110,0,7,9)
	local anim = CreateAnimation('data/Texture/Minigames/4_ferruginous_ship_Diff/Obvodka02.png',0,0,74,58,1,0)
	anim:SetHotSpot(37,29)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Obvodka2_1',anim,207,525,105,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/4_ferruginous_ship_Diff/Obvodka02.png',0,0,74,58,1,0)
	anim:SetHotSpot(37,29)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Obvodka2_2',anim,204,322,105,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/4_ferruginous_ship_Diff/Obvodka02.png',0,0,74,58,1,0)
	anim:SetHotSpot(37,29)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Obvodka2_3',anim,200,413,105,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/4_ferruginous_ship_Diff/Obvodka01.png',0,0,90,70,1,0)
	anim:SetHotSpot(45,35)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Obvodka2_4',anim,331,551,105,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/4_ferruginous_ship_Diff/Obvodka02.png',0,0,74,58,1,0)
	anim:SetHotSpot(37,29)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Obvodka2_5',anim,389,227,105,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/4_ferruginous_ship_Diff/Obvodka03.png',0,0,129,86,1,0)
	anim:SetHotSpot(64,43)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Obvodka2_6',anim,355,125,105,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/4_ferruginous_ship_Diff/Obvodka02.png',0,0,74,58,1,0)
	anim:SetHotSpot(37,29)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Obvodka2_7',anim,144,293,105,-90,1,1)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
