local zoom_barrel;
local zoom_boar;
local zoom_wolf;

function OnSceneLoad(screen)
    LoadData(screen)
    DefaultOnSceneLoad('data/Scenes/20_Mill_Inside_Main.lua', 'quest', 'data/Scenes/19_Mill_Outside_Main.lua')
    
    HideFindedHiddenObjects();
    
    AddNoteToNotebook('BOIS_DE_BOULOGNE');
    
    game_state.kettle_founded = HasGameState('ToolFound', '21_kettle');
    game_state.razor_founded  = HasGameState('ToolFound', '20_razor');
    
    zoom_barrel = CreateZoomZone('area_barrel',
                                 'CloseZoneBarrel',
                                 'ZOOM_barrel',     true,
                                 'Layer_button',    true);
    
    zoom_boar = CreateZoomZone('area_boar',
                               'CloseZoneBoar',
                               'ZOOM_boar',      true,
                               'area_boar_wool', true);
                               
    zoom_wolf = CreateZoomZone('area_wolf_zone',
                               'CloseZoneWolf',
                               'ZOOM_wolf',      true,
                               'area_wolf_wool', true);
    
    
    LinkScene(  'area_lock',
                'CursorGlass',
                'data/Scenes/Minigames/20_lock.lua',
                false);
    
    LinkScene(  'area_ho',
                'CursorGlass',
                'data/Scenes/20_Mill_Inside_HO.lua',
                true);
    
    LinkScene(  'area_mill_workshop',
                'CursorUpRight',
                'data/Scenes/21_Mill_Workshop_Main.lua',
                false);
    
    RegisterToolFindInfo('20_button', 'Layer_button',   function(tool_name)
                                                            zoom_barrel.Disable();
                                                        end,
                                                        function(tool_name)
                                                            zoom_barrel.Disable();
                                                        end);
    
    function on_comb_applied(tool_name, object_name)
        if object_name == 'area_boar_wool' then
            zoom_boar.SetVisible(false);
            zoom_boar.Disable();
            FindTool('20_boar_wool');
            
            game_state.boar_wool_founded = true;
            if not game_state.wolf_wool_founded then
                DropTool(0.)
            end
        end
        
        if object_name == 'area_wolf_wool' then
            zoom_wolf.SetVisible(false);
            zoom_wolf.Disable();
            FindTool('20_wolf_wool');
            
            game_state.wolf_wool_founded = true;
            if not game_state.boar_wool_founded then
                DropTool(0.)
            end
        end
        
        if object_name == nil then
            AddNoteToNotebook('BOIS_DE_BOULOGNE_HAIR');
        end
    end
    
    function on_load_when_comb_applied(tool_name, object_name)
        if object_name == 'area_boar_wool' then
            zoom_boar.Disable();
            
            game_state.boar_wool_founded = true;
        end
        
        if object_name == 'area_wolf_wool' then
            zoom_wolf.Disable();
            
            game_state.wolf_wool_founded = true;
        end
        
        if not object_name then
            game_state.boar_wool_founded = true;
            game_state.wolf_wool_founded = true;
            
            zoom_wolf.Disable();
            zoom_boar.Disable();
        end
    end
    
    function on_comb_target_click(tool_name, target_name)
        if target_name == 'area_boar_wool' then
            ShowMonolog('C_20_BOAR_WOOL_HINT', 'C_20_BOAR_WOOL_HINT');
        end
        
        if target_name == 'area_wolf_wool' then
            ShowMonolog('C_20_WOLF_WOOL_HINT', 'C_20_WOLF_WOOL_HINT');
        end
    end
    
    RegisterToolApplyInfo('21_comb',
                          {'area_boar_wool', 'area_wolf_wool'},
                          nil,
                          nil,
                          on_comb_applied,
                          on_load_when_comb_applied,
                          on_comb_target_click);

    if not HasGameState('MiniGameCompleted', '20_lock') then
        play_field:GetObject('Layer_door_open'):SetOpacity(0);
        play_field:GetObject('area_mill_workshop'):SetVisible(false);
    else
        if not HasGameState('ActionCompleted', game_state.scene_path.."(DoorOpened)") then
            AddGameState('ActionCompleted', game_state.scene_path.."(DoorOpened)");
            
            play_field:GetObject('Layer_door_open'):SetOpacity(0);
            play_field:GetObject('area_mill_workshop'):SetVisible(false);
            
            TimerDoString("play_field:GetObject('Layer_door_open'):ChangeOpacity(0, 1, OPEN_DOOR_TIME)"..
                          "play_field:GetObject('area_mill_workshop'):SetVisible(true);"..
                          "PlaySound('20_OpenDoorInside');",
                          1,
                          play_field:GetTimer());
        end
    end
end

function RunFinalSceneDialog()
    CreateDupenAnimations(play_field, 'body', 'head1'); 
    
    local comics = CreateComics('21_SceneFinal',
                                    CreateComicsFrame('Dupen', 'C_21_FINAL_DUPLEIX1_1_DUPEN', 'body', 'head1'),
                                    CreateComicsFrame('Dupen', 'C_21_FINAL_DUPLEIX1_2_DUPEN', 'body', 'head1'));
    if comics then
        comics.on_finish = function()
            CompleteLevel('Mill');
        end
        comics.SetVisible(true);
    else
        CompleteLevel('Mill');
    end
end

function HideFindedHiddenObjects()
    local ho_table_path = 'data/Scenes/20_Mill_Inside_HO.lua';
    
    local correspond_objs = {}
    
    correspond_objs['Bowl1']   = 'kubok01'
    correspond_objs['Bowl2']   = 'kubok02'
    correspond_objs['Bowl3']   = 'kubok03'
    correspond_objs['Pistol1'] = 'pistol01'
    correspond_objs['Pistol2'] = 'pistol02'
    correspond_objs['Pistol3'] = 'pistol03'
    correspond_objs['Horse1']  = 'horse01'
    correspond_objs['Horse2']  = 'horse02'
    correspond_objs['Bat']     = 'bat'
    correspond_objs['Boots']   = 'sapogi'
    correspond_objs['Broom']   = 'metelka'
    correspond_objs['Dagger']  = 'kinzhal'
    correspond_objs['Fan']     = 'veer'
    correspond_objs['Flask']   = 'fljaga'
    correspond_objs['Inkpot']  = 'chernilnica'
    correspond_objs['Machete'] = 'machete'
    correspond_objs['Pipe']    = 'trubka'
    correspond_objs['Skull']   = 'skull'
    correspond_objs['Stick']   = 'trost'
    correspond_objs['Violin']  = 'skripka'
    
    for ho_obj, obj in pairs(correspond_objs) do
        if HasGameState('TaskObjectFound', ho_table_path..'('..ho_obj..')') then
            play_field:GetObject(obj):SetVisible(false);
        end
    end
end

function OnSceneFree(screen)
    DefaultOnSceneFree()
    FreeData(screen)
end

function OnUpdate(dt)
    
    if not game_state.all_completed
        and game_state.boar_wool_founded
        and game_state.wolf_wool_founded
        and game_state.kettle_founded
        and game_state.razor_founded
        and game_state.locker:GetNReferences() == 0
        and game_state.fly_tools:GetNReferences() == 0
    then
        game_state.all_completed = true;
        
        play_field:TimerDoString("RunFinalSceneDialog()", 0.5);
    end
    
    DefaultOnUpdate(dt)
end

function OnAllItemsFound()
    DefaultOnAllItemsFound()
end

function OnLose()
    DefaultOnLose()
end

function OnExit()
    DefaultOnExit()
end

function OnClose()
    DefaultOnClose()
end

function OnObjectMouseEnter(object)
    DefaultOnObjectMouseEnter(object)
end

function OnObjectMouseLeave(object)
    DefaultOnObjectMouseLeave(object)
end

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

    DefaultOnObjectMouseDownProcessed(is_wrong_click);
end

function OnObjectMouseUp(object)
    DefaultOnObjectMouseUp(object)
end

function OnObjectFound(object)
    DefaultOnObjectFound(object)
end

function OnToolFound(tool)
    DefaultOnToolFound(tool)
end

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

function OnObjectReachPanel(object)
    DefaultOnObjectReachPanel(object)
end

function OnButtonPress(title)
    DefaultOnButtonPress(title)
end

function OnMouseMove(previous_pos, current_pos)
    DefaultOnMouseMove(previous_pos, current_pos)
end

function OnSetTool(tool)
    DefaultOnSetTool(tool)
end

function LevelLoaded(level)
end

function OnSceneDestroy(playfield)
    DefaultOnSceneDestroy(playfield)
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/20_Mill_inside/Zone_main/Layer/background.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('background',anim,512,384,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/20_Mill_inside/Zone_main/Items/bat.png',0,0,54,32,1,0)
	anim:SetHotSpot(27,16)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('bat',anim,108,177,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/20_Mill_inside/Zone_main/Items/chernilnica.png',0,0,38,70,1,0)
	anim:SetHotSpot(19,35)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('chernilnica',anim,227,321,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/20_Mill_inside/Zone_main/Items/fljaga.png',0,0,57,56,1,0)
	anim:SetHotSpot(28,28)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('fljaga',anim,273,396,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/20_Mill_inside/Zone_main/Items/horse01.png',0,0,45,48,1,0)
	anim:SetHotSpot(22,24)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('horse01',anim,262,248,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/20_Mill_inside/Zone_main/Items/horse02.png',0,0,57,38,1,0)
	anim:SetHotSpot(28,19)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('horse02',anim,447,352,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/20_Mill_inside/Zone_main/Items/kinzhal.png',0,0,24,57,1,0)
	anim:SetHotSpot(12,28)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('kinzhal',anim,455,431,103,-20,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/20_Mill_inside/Zone_main/Items/kubok01.png',0,0,18,29,1,0)
	anim:SetHotSpot(9,14)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('kubok01',anim,152,360,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/20_Mill_inside/Zone_main/Items/kubok02.png',0,0,47,28,1,0)
	anim:SetHotSpot(23,14)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('kubok02',anim,441,392,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/20_Mill_inside/Zone_main/Items/kubok03.png',0,0,18,31,1,0)
	anim:SetHotSpot(9,15)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('kubok03',anim,139,250,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/20_Mill_inside/Zone_main/Items/machete.png',0,0,30,126,1,0)
	anim:SetHotSpot(15,63)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('machete',anim,29,315,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/20_Mill_inside/Zone_main/Items/metelka.png',0,0,52,64,1,0)
	anim:SetHotSpot(26,32)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('metelka',anim,502,294,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/20_Mill_inside/Zone_main/Items/pistol01.png',0,0,67,27,1,0)
	anim:SetHotSpot(33,13)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('pistol01',anim,419,413,101,20,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/20_Mill_inside/Zone_main/Items/pistol02.png',0,0,55,31,1,0)
	anim:SetHotSpot(27,15)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('pistol02',anim,97,379,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/20_Mill_inside/Zone_main/Items/pistol03.png',0,0,73,33,1,0)
	anim:SetHotSpot(36,16)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('pistol03',anim,347,155,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/20_Mill_inside/Zone_main/Items/sapogi.png',0,0,60,50,1,0)
	anim:SetHotSpot(30,25)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('sapogi',anim,71,242,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/20_Mill_inside/Zone_main/Items/skripka.png',0,0,65,33,1,0)
	anim:SetHotSpot(32,16)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('skripka',anim,203,179,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/20_Mill_inside/Zone_main/Items/skull.png',0,0,29,22,1,0)
	anim:SetHotSpot(14,11)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('skull',anim,176,217,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/20_Mill_inside/Zone_main/Items/trost.png',0,0,30,97,1,0)
	anim:SetHotSpot(15,48)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('trost',anim,369,278,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/20_Mill_inside/Zone_main/Items/trubka.png',0,0,40,23,1,0)
	anim:SetHotSpot(20,11)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('trubka',anim,145,446,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/20_Mill_inside/Zone_main/Items/veer.png',0,0,75,53,1,0)
	anim:SetHotSpot(37,26)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('veer',anim,495,213,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/20_Mill_inside/Zone_main/Layer/BG_fljagi.png',0,0,65,65,1,0)
	anim:SetHotSpot(32,32)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('BG_fljagi',anim,271,392,102,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/20_Mill_inside/Zone_main/Layer/BG_kinzhal.png',0,0,20,18,1,0)
	anim:SetHotSpot(10,9)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('BG_kinzhal',anim,466,451,104,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/20_Mill_inside/Zone_main/Layer/BG_metelki.png',0,0,32,35,1,0)
	anim:SetHotSpot(16,17)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('BG_metelki',anim,492,279,102,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/20_Mill_inside/Zone_main/Layer/BG_pistol01.png',0,0,55,42,1,0)
	anim:SetHotSpot(27,21)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('BG_pistol01',anim,430,417,102,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/20_Mill_inside/Zone_main/Layer/BG_sapog.png',0,0,59,65,1,0)
	anim:SetHotSpot(29,32)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('BG_sapog',anim,86,237,102,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/20_Mill_inside/Zone_main/Layer/BG_skripki.png',0,0,62,36,1,0)
	anim:SetHotSpot(31,18)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('BG_skripki',anim,212,183,102,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/20_Mill_inside/Zone_main/Layer/BG_trosti.png',0,0,26,56,1,0)
	anim:SetHotSpot(13,28)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('BG_trosti',anim,379,282,102,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/20_Mill_inside/Zone_main/Layer/BG_veera.png',0,0,40,34,1,0)
	anim:SetHotSpot(20,17)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('BG_veera',anim,494,231,102,0,1,1)
	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('area_lock',anim,605,350,102,0,10,10)
	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('area_ho',anim,226,368,103,0,30,20)
	local anim = CreateAnimation('data/Texture/Scenes/20_Mill_inside/Zone_main/Layer/Layer_door_open.png',0,0,189,413,1,0)
	anim:SetHotSpot(94,206)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Layer_door_open',anim,679,361,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/20_Mill_inside/Zone_main/Zoom_barrel/Zoom_barrel.jpg',0,0,640,480,1,0)
	anim:SetHotSpot(320,240)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('ZOOM_barrel',anim,512,384,150,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/20_Mill_inside/Zone_main/Zoom_barrel/Layer_button.png',0,0,135,45,1,0)
	anim:SetHotSpot(67,22)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Layer_button',anim,500,513,151,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/Common/area_circle.png',0,0,128,128,1,0)
	anim:SetHotSpot(64,64)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_barrel',anim,707,608,102,0,1.5,2)
	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('area_wolf_zone',anim,494,255,103,0,10,10)
	local anim = CreateAnimation('data/Texture/Scenes/Common/area_circle.png',0,0,128,128,1,0)
	anim:SetHotSpot(64,64)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_boar',anim,886,232,101,0,1.2,1.2)
	local anim = CreateAnimation('data/Texture/Scenes/20_Mill_inside/Zone_main/Zoom_boar/ZOOM_boar.jpg',0,0,640,480,1,0)
	anim:SetHotSpot(320,240)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('ZOOM_boar',anim,512,384,160,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/20_Mill_inside/Zone_main/Zoom_wolf/ZOOM_wolf.jpg',0,0,640,480,1,0)
	anim:SetHotSpot(320,240)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('ZOOM_wolf',anim,512,384,170,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/Common/area_circle.png',0,0,128,128,1,0)
	anim:SetHotSpot(64,64)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_wolf_wool',anim,501,358,171,0,2,2)
	local anim = CreateAnimation('data/Texture/Scenes/Common/area_circle.png',0,0,128,128,1,0)
	anim:SetHotSpot(64,64)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_boar_wool',anim,505,329,161,0,2,2)
	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('area_mill_workshop',anim,606,337,103,0,15,40)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
