function add_fibrillation_effector_door_light(obj, delay)
	local duration = 7.0

	eff= CreateAnimationEffector()
	eff:AttachTo(obj)
	eff:SetMode(_ANIM_LOOP)

    local dopacity = -0.

	eff:SetKey(_TRACK_O, 0.0 + delay, 1.0 + dopacity)
	eff:SetKey(_TRACK_O, duration * 1.0/4.0 + delay, 0.75 + dopacity)
	eff:SetKey(_TRACK_O, duration * 2.0/4.0 + delay, 1.0 + dopacity)
	eff:SetKey(_TRACK_O, duration * 3.0/4.0 + delay, 0.59 + dopacity)
	eff:SetKey(_TRACK_O, duration * 4.0/4.0 + delay, 1.0 + dopacity)
	eff:Play()
end

function OnSceneLoad(screen)
    LoadData(screen)
    DefaultOnSceneLoad('data/Scenes/18_Theatre_Hall_Main.lua', 'quest')

	RegisterQuestObjects('area_ho', 'area_minigame', 'area_Corridor')
	
	LoadSound('18_Door_open', 'data/Sound/18_Theatre_Hall/Door_open.ogg');
	LoadSound('18_Spider', 'data/Sound/18_Theatre_Hall/18_Spider.ogg');

    LinkScene('area_ho', 'CursorGlass', 'data/Scenes/18_Theatre_Hall_HO.lua', true)
    LinkScene('area_Theatre_Entrance', 'CursorDoorDown', 'data/Scenes/17_Theatre_Entrance_Main.lua', false)

	if HasGameState('MiniGameCompleted', '18_numbers') then
		play_field:GetObject('area_minigame'):Hide()
		LinkScene('area_Balcony', 'CursorGlass', 'data/Scenes/25_Balcony_HO.lua', true)
		if not HasGameState('DoorOpenSound', '18')
		then
		    MakeSoundEntity('18_Door_open'):Play(false);
		    AddGameState('DoorOpenSound', '18')
		end
	else
		LinkScene('area_minigame', 'CursorGlass', 'data/Scenes/18_Balkon_MG.lua', false)
		play_field:GetObject('Layer_door_opened'):Hide()
		play_field:GetObject('Layer_Doorlight'):Hide()
	end

    local condition = function()
        return HasGameState('ToolFound', '18_lantern')
    end
    local on_condition_fall = function()
        ShowMonolog('18_Darkness', 'C_18_DARKNESS')
    end
    LinkScene('area_Corridor', 'CursorDoorLeft', 'data/Scenes/19_Corridor_Main.lua', false, condition, on_condition_fall)

	if HasGameState('ToolFound', '24_pimpa') and
	   HasGameState('ToolFound', '24_runa') and
	   HasGameState('ToolFound', '24_chain') and
	   HasGameState('ToolFound', '22_switch')
	then
		game_state.ignore_on_highlight['area_Corridor'] = true
	end

    PlaySpiderManFleeingAnimation();

    RegisterMonologObject('area_doll_broken', 'C_18_DOLL_RUINED', true)
    
    AddRatsComposition();
end

function PlaySpiderManFleeingAnimation()

    local state = GetGameManager():GetCurrPlayer().m_gameState;
    if (state:HasState('SpiderManVanished') and state:GetState('SpiderManVanished'):HasValue('18'))
    then
        return;
    else
        state:AddState('SpiderManVanished', '18');
    end
    
    MakeSoundEntity('18_Spider'):Play(false);

    createCompositionSpider_Man_Fleeing = function()
    
        local compositionSpider_Man_Fleeing = CreateCompositionSequence();
        local compositionDuration = 4.33333333333333;
        compositionSpider_Man_Fleeing:SetDuration(compositionDuration);
    
        -- Layer Web
        local element = CreateCompositionElement("data/Texture/Scenes/18_Theatre_Hall/Zone_Main/Animations/pautina.png");
        element:SetName("Web");
        compositionSpider_Man_Fleeing:AddElement(element);
        element:SetHotSpot(1.5, -0.5);
        element:SetParentElement(compositionSpider_Man_Fleeing);
        element:SetXY(259, 109);
    
        element:SetAngle(0);
        element:SetOpacity(1);
    
        effector = element:GetEffector();
        effector:SetLifeTime(0, 15);
    
        effector:SetKey(_TRACK_SX, 0, 0.44);
        effector:SetKey(_TRACK_SY, 0, 0.06);
        effector:SetKey(_TRACK_SX, 0.33333333333333, 0.44);
        effector:SetKey(_TRACK_SY, 0.33333333333333, 0.20428571428571);
        effector:SetKey(_TRACK_SX, 0.75, 1.07);
        effector:SetKey(_TRACK_SY, 0.75, 0.78714285714286);
        effector:SetKey(_TRACK_SX, 1, 0.44);
        effector:SetKey(_TRACK_SY, 1, 0.96485714285714);
        effector:SetKey(_TRACK_SX, 1.16666666666667, 0.99);
        effector:SetKey(_TRACK_SY, 1.16666666666667, 0.95);
        effector:SetKey(_TRACK_SX, 1.41666666666667, 0.44);
        effector:SetKey(_TRACK_SY, 1.41666666666667, 0.03);
    
        effector:Play();
    
    
        -- Layer Spider_fleeing_downstairs
        local element = CreateCompositionElement("data/Texture/Scenes/18_Theatre_Hall/Zone_Main/Animations/kr_c.png");
        element:SetName("Spider_fleeing_downstairs");
        compositionSpider_Man_Fleeing:AddElement(element);
        element:SetHotSpot(116.5, 47.5);
        element:SetParentElement(compositionSpider_Man_Fleeing);
        element:SetXY(156, 635);
        
            anim = element:GetTexture();
			anim:SetSpeed(20);
			anim:SetFrames(19);
			anim:SetFrame(0);
			anim:SetTextureRect(0, 0, 233, 95);
			anim:SetMode(HGEANIM_FWD);
			anim:Play();
    
        element:SetScaleX(1);
        element:SetScaleY(1);
        element:SetAngle(0);
        element:SetOpacity(1);
    
        effector = element:GetEffector();
        effector:SetLifeTime(3, 3.95);
    
    
        effector:Play();
    
    
        -- Layer Spider_scouting_area
        local element = CreateCompositionElement("data/Texture/Scenes/18_Theatre_Hall/Zone_Main/Animations/kr_b.png");
        element:SetName("Spider_scouting_area");
        compositionSpider_Man_Fleeing:AddElement(element);
        element:SetHotSpot(34.5, 45);
        element:SetParentElement(compositionSpider_Man_Fleeing);
        element:SetXY(249, 638);
        
            anim = element:GetTexture();
			anim:SetSpeed(20);
			anim:SetFrames(40);
			anim:SetFrame(0);
			anim:SetTextureRect(0, 0, 69, 90);
			anim:SetMode(HGEANIM_FWD);
			anim:Play();
    
        element:SetScaleX(1);
        element:SetScaleY(1);
        element:SetAngle(0);
        element:SetOpacity(1);
    
        effector = element:GetEffector();
        effector:SetLifeTime(1, 3);
    
    
        effector:Play();
    
    ---[[
        -- Layer Spider_going_down_1
        local element = CreateCompositionElement("data/Texture/Scenes/18_Theatre_Hall/Zone_Main/Animations/kr_a_1.png");
        element:SetName("Spider_going_down");
        compositionSpider_Man_Fleeing:AddElement(element);
        element:SetHotSpot(33.5, 150);
        element:SetParentElement(compositionSpider_Man_Fleeing);
        element:SetXY(262, 256.5);
        
            anim = element:GetTexture();
			anim:SetSpeed(20);
			anim:SetFrames(10);
			anim:SetFrame(0);
			anim:SetTextureRect(0, 0, 67, 300);
			anim:SetMode(HGEANIM_FWD);
			anim:Play();
    
        element:SetScaleX(1);
        element:SetScaleY(1);
        element:SetAngle(0);
        element:SetOpacity(1);
    
        effector = element:GetEffector();
        effector:SetLifeTime(0, 0.5);
    
    
        effector:Play();
        --]]
        -- Layer Spider_going_down_2
        local element = CreateCompositionElement("data/Texture/Scenes/18_Theatre_Hall/Zone_Main/Animations/kr_a_2.png");
        element:SetName("Spider_going_down");
        compositionSpider_Man_Fleeing:AddElement(element);
        element:SetHotSpot(33.5, 175);
        element:SetParentElement(compositionSpider_Man_Fleeing);
        element:SetXY(262, 399 + 175 / 2);
        
            anim = element:GetTexture();
			anim:SetSpeed(20);
			anim:SetFrames(10);
			anim:SetFrame(0);
			anim:SetTextureRect(0, 0, 67, 350);
			anim:SetMode(HGEANIM_FWD);
			anim:Play();
    
        element:SetScaleX(1);
        element:SetScaleY(1);
        element:SetAngle(0);
        element:SetOpacity(1);
    
        effector = element:GetEffector();
        effector:SetLifeTime(0.5, 1);
    
    
        effector:Play();
    
    
        compositionSpider_Man_Fleeing:Play();
    
        return compositionSpider_Man_Fleeing;
    end
    
    local compos = createCompositionSpider_Man_Fleeing();
    compos:SetZ(101);
    compos:AttachTo(play_field);

end

function AddRatsComposition()

    createComposition17_Theatre_Entrance_rat = function()
    
        local composition17_Theatre_Entrance_rat = CreateCompositionSequence();
        local compositionDuration = 3.5;
        composition17_Theatre_Entrance_rat:SetDuration(compositionDuration);
    
    
        -- Layer Rat_eating
        local element = BuildRatEatingComposition();
        element:SetName("Rat_eating");
        composition17_Theatre_Entrance_rat:AddElement(element);
        element:SetHotSpot(512, 384);
        element:SetParentElement(composition17_Theatre_Entrance_rat);
        element:SetXY(350.418716430664, 708.460418701172);
    
        element:SetScaleX(-0.22510791366906);
        element:SetScaleY(0.25187050359712);
        element:SetAngle(0);
        element:SetOpacity(1);
    
        effector = element:GetEffector();
        effector:SetLifeTime(0, 3.5);
    
    
        effector:Play();
    
    
        composition17_Theatre_Entrance_rat:Play();
    
        return composition17_Theatre_Entrance_rat;
    end
    
    local createCompositionRatRunning1 = function()
    
        local compositionRatRunning1 = CreateCompositionSequence();
        local compositionDuration = 3.5;
        compositionRatRunning1:SetDuration(compositionDuration);
        
        -- Layer Rat_running
        local element = BuildRatRunningComposition();
        element:SetName("Rat_running");
        compositionRatRunning1:AddElement(element);
        element:SetHotSpot(512, 384);
        element:SetParentElement(compositionRatRunning1);
        element:SetScaleX(-0.3);
        element:SetScaleY(0.3);
        element:SetAngle(0);
        element:SetOpacity(1);
    
        effector = element:GetEffector();
        effector:SetLifeTime(0, 5);
    
        effector:SetKey(_TRACK_X, 0, -102.474822998047);
        effector:SetKey(_TRACK_Y, 0, 665.424453735352);
        effector:SetKey(_TRACK_X, 1.30000813802083, 604);
        effector:SetKey(_TRACK_Y, 1.30000813802083, 834);
    
        effector:Play();
        
        --compositionRatRunning1:Play();
        
        return compositionRatRunning1;
    end
    
    local createCompositionRatDrugging = function()
    
        local compositionRatDrugging = CreateCompositionSequence();
        local compositionDuration = 14.7916666666667;
        compositionRatDrugging:SetDuration(compositionDuration);
        
         -- Layer Layer_1
        local element = CreateCompositionElement("data/Texture/Scenes/18_Theatre_Hall/Zone_Main/Animations/mask_lok_18.png");
        element:SetName("Layer_1");
        compositionRatDrugging:AddElement(element);
        element:SetHotSpot(61.5, 44);
        element:SetParentElement(compositionRatDrugging);
        element:SetXY(599.240280151367, 694.252517700195);
    
        element:SetScaleX(1);
        element:SetScaleY(1);
        element:SetAngle(0);
        element:SetOpacity(1);
    
        effector = element:GetEffector();
        effector:SetLifeTime(0, 60);
    
    
        effector:Play();
        
        -- Layer Rat_drugging
        local element = BuildRatDruggingComposition();
        element:SetName("Rat_dragging");
        compositionRatDrugging:AddElement(element);
        element:SetHotSpot(2800, 384);
        element:SetParentElement(compositionRatDrugging);
        element:SetXY(805, 652);
    
        element:SetScaleX(0.27);
        element:SetScaleY(0.27);
        element:SetAngle(0);
        element:SetOpacity(1);
    
        effector = element:GetEffector();
        effector:SetLifeTime(0, 38.9999985694885);
    
        effector:Play();
        
        --compositionRatRunning2:Play();
        
        return compositionRatDrugging;
    end
    
    local composes = {};
    composes.ratRunning1 = createCompositionRatRunning1();
    --composes.ratRunning1:SetCyclic(true);
    composes.ratRunning1:SetZ(43);
    composes.ratRunning1:SetSpeed(0.8);
    composes.ratRunning1:AttachTo(play_field);
    composes.ratRunning1:SetDestroysOnEnd(false);
    composes.ratRunning1:SetEndAction("RatRunningEnded");
    RatRunningEnded(composes.ratRunning1);
    
    composes.ratDrugging = createCompositionRatDrugging();
    --composes.ratDrugging:SetCyclic(true);
    composes.ratDrugging:SetZ(44);
    composes.ratDrugging:SetSpeed(0.8);
    composes.ratDrugging:AttachTo(play_field);
    composes.ratDrugging:SetDestroysOnEnd(false);
    composes.ratDrugging:SetEndAction("RatRunningEnded");
    RatRunningEnded(composes.ratDrugging);
    
    local compos = createComposition17_Theatre_Entrance_rat();
    compos:SetCyclic(true);
    compos:SetZ(45);
    compos:SetSpeed(0.8);
    compos:AttachTo(play_field);
    compos:SetCyclic(true);

end

function RatRunningEnded(compos)
    compos:PlayAt(math.random(5, 10));
end

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

function OnUpdate(dt)
    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/Common/area.png',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_Corridor',anim,102,535,100,0,15,30)
	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,471,615,100,0,40,20)
	local anim = CreateAnimation('data/Texture/Scenes/Common/area.png',0,0,8,8,1,0)
	anim:SetHotSpot(2,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_Theatre_Entrance',anim,316,740,100,0,90,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_minigame',anim,965,527,103,0,15,35)
	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_Balcony',anim,965,527,102,0,15,35)
	local anim = CreateAnimation('data/Texture/Scenes/18_Theatre_Hall/Zone_Main/Layers/BG_Theatre_Hall.JPG',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('BG_Theatre_Hall',anim,512,384,40,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/18_Theatre_Hall/Zone_Main/Layers/Layer_Doorlight.png',0,0,209,375,1,0)
	anim:SetHotSpot(104,187)
	anim:SetBlendMode(0)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('Layer_Doorlight',anim,902,577,51,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/18_Theatre_Hall/Zone_Main/Layers/Layer_door.jpg',0,0,83,279,1,0)
	anim:SetHotSpot(41,139)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Layer_door_opened',anim,971,534,50,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_doll_broken',anim,236,558,100,0,10,23.5)
	local anim = CreateAnimation('data/Texture/Scenes/18_Theatre_Hall/Zone_Main/Layers/Layer_Dolls.png',0,0,89,77,1,0)
	anim:SetHotSpot(44,38)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Layer_Dolls',anim,708,701,45,0,1,1)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
