function OnSceneLoad(screen)
    parent_screen = "data/Scenes/35_Prison_Corridor_Main.lua";
    LoadData(screen)
    DefaultOnSceneLoad('data/Scenes/35_Numbers_MG.lua', 'quest', parent_screen)
    LinkScene('area_exit', 'CursorDoorDown', parent_screen, false)
    
    Numbers_Left = {3, 8, 5, 9, 6, 6, 7, 6}
    Numbers_Right = {1, 7, 9, 8, 2, 9, 5, 9}
    Numbers_Revers = {false, true, true, true, false, true, false, true}
    
    screen:GetObject('area_2'):SetVisible(false);
    screen:GetObject('BG_Razdet'):SetVisible(false);
    screen:GetObject('Layer_Numbers_Over'):SetVisible(false);
		for i = 1,8 do
			screen:GetObject('Button_On_'..i):SetVisible(false);
			screen:GetObject('Button_Off_'..i):SetVisible(false);
		end
	screen:GetObject('BG_Minigame'):SetVisible(false);
    
    local left = 408;
    local right = 620;
    local top = 155;
    local vertical_step = 59;
    
    OBJECTS_AMOUNT = 8;
    
    ObjectsL = {}
    ObjectsR = {}
    
    for i = 1, OBJECTS_AMOUNT do
		ObjectsL[i] = {}
		ObjectsR[i] = {}
		
		local anim = nil;
		if(Numbers_Revers[i] == true) then
			anim = CreateAnimation('data/Texture/Minigames/Numbers/'..Numbers_Right[i]..'.PNG',0,0,37,46,1,0)
			ObjectsL[i].Number = Numbers_Right[i];
		else
			anim = CreateAnimation('data/Texture/Minigames/Numbers/'..Numbers_Left[i]..'.PNG',0,0,37,46,1,0)
			ObjectsL[i].Number = Numbers_Left[i];
		end
		anim:SetHotSpot(18,23)
		anim:SetBlendMode(2)
		anim:SetColor('0xFFFFFFFF')
		screen:CreateObject('L_'..i,anim,466,59,104,0,1,1)
		
		if(Numbers_Revers[i] == true) then
			anim = CreateAnimation('data/Texture/Minigames/Numbers/'..Numbers_Left[i]..'.PNG',0,0,37,46,1,0)
			ObjectsR[i].Number = Numbers_Left[i];
		else
			anim = CreateAnimation('data/Texture/Minigames/Numbers/'..Numbers_Right[i]..'.PNG',0,0,37,46,1,0)
			ObjectsR[i].Number = Numbers_Right[i];
		end
		anim:SetHotSpot(18,23)
		anim:SetBlendMode(2)
		anim:SetColor('0xFFFFFFFF')
		screen:CreateObject('R_'..i,anim,466,59,104,0,1,1)
		
		ObjectsL[i].Entity = screen:GetObject('L_'..i);
		ObjectsL[i].Entity:SetXY(left, top + vertical_step * (i - 1));
		ObjectsL[i].Active = true;
		ObjectsL[i].Entity:SetVisible(false);
		
		ObjectsR[i].Entity = screen:GetObject('R_'..i);
		ObjectsR[i].Entity:SetXY(right, top + vertical_step * (i - 1));
		ObjectsR[i].Active = false;
		ObjectsR[i].Entity:SetVisible(false);
    end
    
     game_screen = screen;
    
    --dressed одет
    --undressed разрезали китель
    --cut_body разрезали живот
    mini_game_state = "dressed";
    InitText();
    
    --------------------------------------------------------------------------------------------------------------------------------
    --tools
    --Нож
    local on_knife_apply = function(tool_name, object_name)
		mini_game_state = "undressed";
		game_screen:GetObject('BG_Razdet'):SetVisible(true);
		game_screen:GetObject('area'):SetVisible(false);
		game_screen:GetObject('BG_Odet'):ChangeOpacity(1.0, 0.0, 0.5);
		game_screen:GetObject('area_2'):SetVisible(true);
		MakeSoundEntity('35_Cutting_clothes'):Play(false);
	end
	
	local on_load_when_knife_applied = function(tool_name)
		game_screen:GetObject('BG_Razdet'):SetVisible(true);
		mini_game_state = "undressed";
		game_screen:GetObject('area'):SetVisible(false);
		game_screen:GetObject('BG_Odet'):SetVisible(false);
		game_screen:GetObject('area_2'):SetVisible(true);
	end
	RegisterToolApplyInfo('35_knife', 'area', nil, nil, on_knife_apply, on_load_when_knife_applied)

	------------------------------------------------------------

	--Горелка
	local on_gas_jet_apply = function(tool_name, object_name)
		game_state.level_type = 'mini_game';
		play_field:GetLevelScreen():GetPanel():SetMode(PM_MINIGAME);
		mini_game_state = "cut_body";
		game_screen:GetObject('BG_Razdet'):ChangeOpacity(1.0, 0.0, 0.5);
		game_screen:GetObject('area_2'):SetVisible(false);
		game_screen:GetObject('Layer_Numbers_Over'):SetVisible(true);
		for i = 1,OBJECTS_AMOUNT do
			screen:GetObject('Button_On_'..i):SetVisible(true);
			screen:GetObject('Button_Off_'..i):SetVisible(true);
			ObjectsL[i].Entity:SetVisible(true);
			ObjectsR[i].Entity:SetVisible(true);
		end
		screen:GetObject('BG_Minigame'):SetVisible(true);
		MakeSoundEntity('35_Cutting_doll_body'):Play(false);
		TimerDoString("ShowMonolog('C_35_NUMBERS_MG', 'C_35_NUMBERS_MG');",
                      1.5, play_field:GetTimer())
	end
	
	local on_load_when_gas_jet_applied = function(tool_name)
		game_state.level_type = 'mini_game';
		play_field:GetLevelScreen():GetPanel():SetMode(PM_MINIGAME);
		mini_game_state = "cut_body";
		game_screen:GetObject('BG_Razdet'):SetVisible(false);
		game_screen:GetObject('area_2'):SetVisible(false);
		game_screen:GetObject('Layer_Numbers_Over'):SetVisible(true);
		for i = 1,OBJECTS_AMOUNT do
			screen:GetObject('Button_On_'..i):SetVisible(true);
			screen:GetObject('Button_Off_'..i):SetVisible(true);
			ObjectsL[i].Entity:SetVisible(true);
			ObjectsR[i].Entity:SetVisible(true);
		end
		screen:GetObject('BG_Minigame'):SetVisible(true);
		ShowMonolog('C_35_NUMBERS_MG', 'C_35_NUMBERS_MG');
	end
	RegisterToolApplyInfo('37_gas_jet', 'area_2', nil, nil, on_gas_jet_apply, on_load_when_gas_jet_applied)
	--------------------------------------------------------------------------------------------------------------------------------
	
	LoadSound("35_Numbers_click", "data/Sound/35_Prison_Koridor/Numbers/Click.ogg")
    LoadSound("35_Numbers_success", "data/Sound/35_Prison_Koridor/Numbers/Success.ogg")
    LoadSound("35_Cutting_clothes", "data/Sound/35_Prison_Koridor/Numbers/Cutting_clothes.ogg")
    LoadSound("35_Cutting_doll_body", "data/Sound/35_Prison_Koridor/Numbers/Cutting_doll_body.ogg")
    
    play_field.numbersClick = MakeSoundEntity('35_Numbers_click');
    
end

function GetLeftSum()
	local Amount = 0;
	for i = 1, OBJECTS_AMOUNT do
		Amount = Amount + ObjectsL[i].Number;
	end
	
	return Amount
end

function GetRightSum()
	local Amount = 0;
	for i = 1, OBJECTS_AMOUNT do
		Amount = Amount + ObjectsR[i].Number;
	end
	
	return Amount
end

function InitText()
	textBox = CreateTextEntity(INGAME_MENU_FONT, ""..GetLeftSum());
	textBox:SetText(""..GetLeftSum());
	textBox:SetXY(422, 645);
	textBox:SetScale(0.9);
	textBox:SetZ(131);
	textBox:AttachTo(game_screen);
	
	textBox_2 = CreateTextEntity(INGAME_MENU_FONT, ""..GetRightSum());
	textBox_2:SetText(""..GetRightSum());
	textBox_2:SetXY(608, 645);
	textBox_2:SetScale(0.9);
	textBox_2:SetZ(131);
	textBox_2:AttachTo(game_screen);
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)
    
    if(mini_game_state == "cut_body") then
		local obj_name = object:GetName();
		local flag = true;
		
		for i = 1, 8 do
			if(obj_name == "Button_Off_"..i) then
				game_screen:GetObject('Button_Off_'..i):SetVisible(false);
				game_screen:GetObject('Button_On_'..i):SetVisible(true);
				flag = false;
			end
		end
		
		if(flag == true and obj_name ~= "Button_On_1" and obj_name ~= "Button_On_2" and obj_name ~= "Button_On_3" and obj_name ~= "Button_On_4" and obj_name ~= "Button_On_5"
								and obj_name ~= "Button_On_6" and obj_name ~= "Button_On_7"  and obj_name ~= "Button_On_8") then
			for i = 1, 8 do
				game_screen:GetObject('Button_Off_'..i):SetVisible(true);
				game_screen:GetObject('Button_On_'..i):SetVisible(false);
			end
		end
	end
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

	if(obj_name == "area" and mini_game_state == "dressed") then
	    ShowMonolog('C_35_CUT_CLOSES', 'C_35_CUT_CLOSES');
	end
	
	if(obj_name == "area_2" and mini_game_state == "undressed") then
	    ShowMonolog('C_35_OPEN_CHEST', 'C_35_OPEN_CHEST');
		--TODO: выод фразы, что надо разчленить куклу
	end
	
	if(mini_game_state == "cut_body") then
		for i = 1, 8 do
			if(obj_name == 'Button_Off_'..i or obj_name == 'Button_On_'..i and ObjectsL[i].Active == true) then
				HideNumbers(i);
				TimerDoString('ShowNumbers('..i..')', 0.22, game_screen:GetTimer())
				play_field.numbersClick:StopSound();
				play_field.numbersClick:Play(false);
			end
		end
	end
	
    DefaultOnObjectMouseDownProcessed(is_wrong_click);
end

function HideNumbers(index)
	ObjectsL[index].Entity:Move(50.0, 0.0, 0.2);
	ObjectsR[index].Entity:Move(-50.0, 0.0, 0.2);
	ObjectsL[index].Active = false;
	ObjectsR[index].Active = false;
end

function ShowNumbers(index)
	--меняем местами объекты
	local buf = ObjectsL[index].Entity; 
	ObjectsL[index].Entity = ObjectsR[index].Entity;
	ObjectsR[index].Entity = buf;
	--числа в них
	local num = ObjectsL[index].Number; 
	ObjectsL[index].Number = ObjectsR[index].Number;
	ObjectsR[index].Number = num;
	--...и позицию
	local pos = ObjectsL[index].Entity:GetPosition();
	ObjectsL[index].Entity:SetXY(ObjectsR[index].Entity:GetPosition().x, ObjectsR[index].Entity:GetPosition().y);
	ObjectsR[index].Entity:SetXY(pos.x, pos.y);
		
	ObjectsL[index].Entity:Move(-50.0, 0.0, 0.2);
	ObjectsR[index].Entity:Move(50.0, 0.0, 0.2);
	
	TimerDoString('ObjectsL['..index..'].Active = true', 0.2, game_screen:GetTimer())
	TimerDoString('ObjectsR['..index..'].Active = true', 0.2, game_screen:GetTimer())
	
	TimerDoString('textBox:SetText("'..GetLeftSum()..'");', 0.2, game_screen:GetTimer())
	TimerDoString('textBox_2:SetText("'..GetRightSum()..'");', 0.2, game_screen:GetTimer())
	
	if(IsMiniGameCompleted()) then
		FinishMiniGame();
	end
end

function IsMiniGameCompleted()
	if(GetLeftSum() == GetRightSum()) then
		return true;
	end
	
	return false;
end

function FinishMiniGame()
	mini_game_state = "completed";
	if HasGameState('MiniGameCompleted', '35_Numbers_MG') == false then
		AddGameState('MiniGameCompleted', '35_Numbers_MG');
	end
	
	MiniGameCompleted = true;
	TimerDoString('StartLevel("data/Scenes/35_Prison_Corridor_Main.lua");', 1.0, game_screen:GetTimer())
	MakeSoundEntity('35_Numbers_success'):Play(false);
end

function SkipMiniGame()
	FinishMiniGame();
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/Minigames/Numbers/BG_Minigame.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('BG_Minigame',anim,512,384,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Numbers/Button_On.PNG',0,0,121,62,1,0)
	anim:SetHotSpot(60,31)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Button_On_1',anim,512,156,110,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Numbers/Button_Off.PNG',0,0,121,62,1,0)
	anim:SetHotSpot(60,31)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Button_Off_1',anim,513,155,111,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Numbers/Button_On.PNG',0,0,121,62,1,0)
	anim:SetHotSpot(60,31)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Button_On_2',anim,513,216,110,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Numbers/Button_Off.PNG',0,0,121,62,1,0)
	anim:SetHotSpot(60,31)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Button_Off_2',anim,513,216,111,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Numbers/Button_On.PNG',0,0,121,62,1,0)
	anim:SetHotSpot(60,31)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Button_On_3',anim,512,277,110,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Numbers/Button_Off.PNG',0,0,121,62,1,0)
	anim:SetHotSpot(60,31)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Button_Off_3',anim,512,277,111,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Numbers/Button_On.PNG',0,0,121,62,1,0)
	anim:SetHotSpot(60,31)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Button_On_4',anim,513,337,110,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Numbers/Button_Off.PNG',0,0,121,62,1,0)
	anim:SetHotSpot(60,31)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Button_Off_4',anim,513,337,111,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Numbers/Button_On.PNG',0,0,121,62,1,0)
	anim:SetHotSpot(60,31)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Button_On_5',anim,514,396,110,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Numbers/Button_Off.PNG',0,0,121,62,1,0)
	anim:SetHotSpot(60,31)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Button_Off_5',anim,513,395,111,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Numbers/Button_On.PNG',0,0,121,62,1,0)
	anim:SetHotSpot(60,31)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Button_On_6',anim,512,454,110,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Numbers/Button_Off.PNG',0,0,121,62,1,0)
	anim:SetHotSpot(60,31)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Button_Off_6',anim,512,454,111,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Numbers/Button_On.PNG',0,0,121,62,1,0)
	anim:SetHotSpot(60,31)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Button_On_7',anim,512,512,110,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Numbers/Button_Off.PNG',0,0,121,62,1,0)
	anim:SetHotSpot(60,31)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Button_Off_7',anim,512,512,111,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Numbers/Button_On.PNG',0,0,121,62,1,0)
	anim:SetHotSpot(60,31)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Button_On_8',anim,513,569,110,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Numbers/Button_Off.PNG',0,0,121,62,1,0)
	anim:SetHotSpot(60,31)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Button_Off_8',anim,513,570,111,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Numbers/Layer_Numbers_Over.PNG',0,0,385,483,1,0)
	anim:SetHotSpot(192,241)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Layer_Numbers_Over',anim,515,360,105,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Numbers/BG_Razdet.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('BG_Razdet',anim,512,384,130,0,1,1)
	local anim = CreateAnimation('data/Texture/Minigames/Numbers/BG_Odet.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('BG_Odet',anim,512,384,150,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',anim,516,325,400,0,75,80)
	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_2',anim,516,325,140,0,75,80)
	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_exit',anim,544,748,450,0,100,20)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
