local frame = -1;
local goals = 6;
local alpha = 0.2;
local sc_goals = 8;
local maiatnik=false;

-- CreateConsole()
local g=-1;
myid=-1;
shesterni = {obj.gear1.id,obj.gear2.id,obj.gear3.id,obj.gear4.id,obj.gear5.id, obj.pendulum.id}

nvg_daymode = false;
fucking_third_level = true;
do --частицы
AddParticleToObject(19,"Highlight");
AddParticleToObject(19,"Highlight");
AddParticleToObject(17,"Highlight1");
MoveEmiter("Highlight", 900,475);
MoveEmiter("Highlight1", 200,485);


AddParticleToObject(500,"SvetMig");
MoveEmiter("SvetMig",30,295);


AddParticleToObject(501,"SvetMig1");
MoveEmiter("SvetMig1",208,258);


AddParticleToObject(502,"SvetMig2");
MoveEmiter("SvetMig2",90,572);


AddParticleToObject(504,"SvetMig4");
MoveEmiter("SvetMig4",152,64);



AddParticleToObject(505,"SvetMig5");
MoveEmiter("SvetMig5",958,56);



AddParticleToObject(506,"SvetMig6");
MoveEmiter("SvetMig6",857,21);



AddParticleToObject(507,"SvetMig7");
MoveEmiter("SvetMig7",742,520);



AddParticleToObject(508,"SvetMig8");
MoveEmiter("SvetMig8",588,403);


AddParticleToObject(509,"SvetMig9");
MoveEmiter("SvetMig9",380,375);


AddParticleToObject(510,"SvetMig10");
MoveEmiter("SvetMig10",235,450);


AddParticleToObject(512,"SvetMig12");
MoveEmiter("SvetMig12",440,26);


AddParticleToObject(503,"SvetMig11");
MoveEmiter("SvetMig11",650,80);
end

--включили ПНВ-------------------------
function endOfAlpha()
	if flags.tutor2lvl then
		set_text(5654547,"63")	
	end
flags.tutor2lvl=false;
setcolor(obj.gear1.id,0,0,0,1,1);  
setcolor(obj.gear2.id,0,0,0,1,1);
setcolor(obj.gear3.id,0,0,0,1,1); 
setcolor(obj.gear4.id,0,0,0,1,1); 
setcolor(obj.gear5.id,0,0,0,1,1); 
setcolor(obj.pendulum.id,0,0,0,1,1);
StartTimer(2323,3,"endof63")
function endof63()
	set_visible_object(obj.plashechka.id,false)
	set_visible_object(obj.goodgirl.id,false)
	del_object_from_game(5654547)	
end
end


do -- loading

if flags.start_draw then
	ShowMenu(false);
end;

if  flags.gear1_on == true then
	SetAlfa(32,1.0,0.5,1);	
end;
if 	flags.gear2_on == true then
	SetAlfa(22,1.0,0.5,1);
end;
if  flags.gear3_on == true then
	SetAlfa(24,1.0,0.5,1);
end;
if  flags.gear4_on == true then
	SetAlfa(23,1.0,0.5,1);
end;
if  flags.gear5_on == true then
	SetAlfa(29,1.0,0.5,1);
end;
if  flags.gear6_on == true then
	SetAlfa(20,1.0,0.5,1);
end;

if flags.nvg_on and flags.dim == false then 



if flags.mechanism and not white then  --если механизм собран
			nvgselect=false;
			
			flags.light = false;
			flags.lit = true;
			flags.nvg_on = false;
			nvg_on = false;
			SetAlfa(1009,1.0,0.0,0.5);
			SetAlfa(1015,1.0,0.0,1);
			flags.glove_block= false;
			for i=17,33 do
				SetAlfa(i,0.1,1.0,1);
			end;
			StopEmiter("Highlight");
			if not flags.mini1_complete then
					StartEmiter("Highlight1");
					end;
			
		
			setcolor(obj.coin.id,0,0,0,1,1);
			setcolor(obj.nail.id,0,0,0,1,1);
			setcolor(obj.crown.id,0,0,0,1,1);
			setcolor(obj.bucket.id,0,0,0,1,1);
			setcolor(obj.shovel.id,0,0,0,1,1);
			setcolor(obj.coil_spring.id,0,0,0,1,1);
			setcolor(obj.nut.id,0,0,0,1,1);
			setcolor(obj.rope.id,0,0,0,1,1);
			setcolor(obj.spider.id,0,0,0,1,1);
			set_visible_object( obj.coin.id, true);
			set_visible_object( obj.nail.id, true);
			set_visible_object( obj.crown.id, true);
			set_visible_object( obj.bucket.id, true);
			set_visible_object( obj.shovel.id, true);
			set_visible_object( obj.coil_spring.id, true);
			set_visible_object( obj.nut.id, true);
			set_visible_object( obj.rope.id, true);
			set_visible_object( obj.spider.id, true);
			speciale_action(2, 3, true );
			speciale_action(2, 4, true );
			flags.nvg_block =true;	

else --иначе если не собран грузить зеленый фон
			Set_Cursor(1012,2);
			RotateWheel(414,270,342,487,198,2.5,4,3);
				
			glove_on = true;
			declineGadjet=true
			StartTimer(23232,3,"v")
			function v()
			nvgselect=true
			end
			
			
			
			canmenu=true;
			gameinput(100004,-1,-1)					
			setcolor(obj.gear1.id,0,0,0,1,1);  
			setcolor(obj.gear2.id,0,0,0,1,1);
			setcolor(obj.gear3.id,0,0,0,1,1); 
			setcolor(obj.gear4.id,0,0,0,1,1); 
			setcolor(obj.gear5.id,0,0,0,1,1); 
			setcolor(obj.pendulum.id,0,0,0,1,1);
			StartTimer(9000,1);
			StartTimer(8889,1);
			StartTimer(99,1);
			flags.nvg_block=false;
			nvg_on = true;
			flags.setnvg=false;			
			flags.light1=false;
			flags.nvg_on=true;		
			 flags.light1=false;
			 nvg_on=true;
			StartEmiter("Highlight");	
			set_visible_object(1012,true);
			set_visible_object(1025,false);	
			
	if not obj.gear1.is_collapsed then
		set_visible_object( obj.gear1.id, false );
		set_visible_object( obj.gear1_nvg.id, true );
		else
		setcolor( obj.gear1.id, 0, 0, 0, 0.5, 1 );
	end;
	if not obj.gear2.is_collapsed then
		set_visible_object( obj.gear2.id, false );
		set_visible_object( obj.gear2_nvg.id, true );
			else
		setcolor( obj.gear2.id, 0, 0, 0, 0.5, 1 );
	end;
	if not obj.gear3.is_collapsed then
		set_visible_object( obj.gear3.id, false );
		set_visible_object( obj.gear3_nvg.id, true );
			else
		setcolor( obj.gear3.id, 0, 0, 0, 0.5, 1 );
	end;
	if not obj.gear4.is_collapsed then
		set_visible_object( obj.gear4.id, false );
		set_visible_object( obj.gear4_nvg.id, true );
			else
		setcolor( obj.gear4.id, 0, 0, 0, 0.5, 1 );
	end;
	if not obj.gear5.is_collapsed then
		set_visible_object( obj.gear5.id, false );
		set_visible_object( obj.gear5_nvg.id, true );
			else
		setcolor( obj.gear5.id, 0, 0, 0, 0.5, 1 );
	end;
	if not obj.pendulum.is_collapsed then
		set_visible_object( obj.pendulum.id, false );
		set_visible_object( obj.pendulum_nvg.id, true );
			else
		setcolor( obj.pendulum.id, 0, 0, 0, 0.5, 1 );
	end;
	SetAlfa(1015,0.0,1.0,1);
	
	set_visible_object( obj.coin.id, false);
	set_visible_object( obj.nail.id, false);
	set_visible_object( obj.crown.id, false);
	set_visible_object( obj.bucket.id, false);
	set_visible_object( obj.shovel.id, false);
	set_visible_object( obj.coil_spring.id, false);
	set_visible_object( obj.nut.id, false);
	set_visible_object( obj.rope.id, false);
	set_visible_object( obj.spider.id, false);
	set_visible_object( 3, false );
	set_visible_object( 4, false );
	
	for i=17,33 do
		SetAlfa(i,1.0,0.2,1);
	end;
end
end;


if flags.mechanism then
StopEmiter("Highlight1");

end;




if flags.gears_on == 6 and not flags.mini1_complete  then
	Set_Cursor(17,4);	
	
	if not flags.mini1_complete then
					StartEmiter("Highlight1");
					end;
	

end;



if flags.glasses_on == false and flags.gears_on == 6 then
	set_visible_object(1025,false);		
			
			StartTimer(232134,2,"pachka1")
			
			
			function pachka1()
				StartEmiter("SvetMig11");
				StartEmiter("SvetMig12");
				StartEmiter("SvetMig10");
				StartTimer(232131,1,"pachka2")
			end
			
			function pachka2()
				StartEmiter("SvetMig9");
				StartEmiter("SvetMig8");
				StartEmiter("SvetMig7");
				StartTimer(232132,1,"pachka3")
			end
			
			function pachka3()
				StartEmiter("SvetMig6");
				StartEmiter("SvetMig5");
				StartEmiter("SvetMig4");
			end		
			
			StartEmiter("SvetMig2");
			StartEmiter("SvetMig1");
			StartEmiter("SvetMig");
			setcolor(obj.gear1.id,0,0,0,0.5,1);  
			setcolor(obj.gear2.id,0,0,0,0.5,1);
			setcolor(obj.gear3.id,0,0,0,0.5,1); 
			setcolor(obj.gear4.id,0,0,0,0.5,1); 
			setcolor(obj.gear5.id,0,0,0,0.5,1); 
			setcolor(obj.pendulum.id,0,0,0,0.5,1);
	for i=17,33 do
		SetAlfa(i,0.1,1.0,1);
	end;

	if not obj.coin.is_collapsed then	setcolor(obj.coin.id,0,0,0,1,1);		end;
	if not obj.nail.is_collapsed then	setcolor(obj.nail.id,0,0,0,1,1);	end;
	if not obj.crown.is_collapsed then	setcolor(obj.crown.id,0,0,0,1,1);	end;
	if not obj.bucket.is_collapsed then	setcolor(obj.bucket.id,0,0,0,1,1);	end;
	if not obj.shovel.is_collapsed then	setcolor(obj.shovel.id,0,0,0,1,1);	end;
	if not obj.coil_spring.is_collapsed then setcolor(obj.coil_spring.id,0,0,0,1,1);	end;
	if not obj.nut.is_collapsed then	setcolor(obj.nut.id,0,0,0,1,1);	end;
	if not obj.rope.is_collapsed then	setcolor(obj.rope.id,0,0,0,1,1);end;
	if not obj.spider.is_collapsed then	setcolor(obj.spider.id,0,0,0,1,1);	end;
end;

if flags.stone_removed then
	set_visible_object( 18, false );
	Set_Cursor( 17, 3 );
	SetObjectPosition( 17, 620, 420 );
	set_visible_object( 1010, true );
	set_visible_object( 1011, false );
	Set_Cursor(1010,2);
end;

if flags.mini1_complete then	
	-- playeffect("037")	
	set_visible_object(10144,true);		
	Set_Cursor(1010,3);		
end;
end -- end of loading


WaitRender();

function gameinput (sender,obj_id,attachedID)
	if SLevel==1.0 then return; end;
	
--обработка плашки
	 if (sender == 1  or sender==5) and not isTimes  and imagetex~=-1 then
			set_visible_object(obj.plashechka.id,false)
			set_visible_object(obj[imagetex].id,false)
			del_object_from_game(5654547)		
			gameinput(26,imageid)	
del_object_from_game(5654547)	
				del_object_from_game(5654547)
				del_object_from_game(5654547)
				del_object_from_game(5654547)
				del_object_from_game(5654547)
				del_object_from_game(5654547)
				del_object_from_game(5654547)	
	end
--меню
	if sender == 2 then
		RemoveFromMouse(object);
		MessageProc(sender,obj_id);
	return;end;	
	
	if sender==7 then
		if obj_id==20 then	
			if not maiatnik then
				RotateObject( 20, 15, -20, 3,false)
				maiatnik=not maiatnik;
			else
				RotateObject( 20, -20, 15, 3,false)
				maiatnik=not maiatnik;
			end	
		end	
	end

--наведение на камень
	if sender==35 and not flags.stone_removed then		
	
		if not glove_on and not canusegadjets and not nvgselect and flags.mechanism then		
			if obj_id == 17 and not flags.mini1_complete then
				set_visible_object(100004,false)
				set_visible_object(333,true)
				StartAnimation(333)					
			return;end		
		    end
	return;end
--уведение с камня	
	if sender==39 and flags.mechanism then		
		set_visible_object(333,false)
		set_visible_object(100004,true)
	return;end
	
	
--включили ПНВ	
	
	if sender==100002 then
	
		if flags.setnvg then
			flags.comics = false;	
			flags.useNVG=false
			set_visible_object(-100022, false);
			set_visible_object(obj.arrow11.id,false);
			flags.dim = false;			
			StartEmiter("Highlight");
			set_visible_object(1012,true);
			set_visible_object(1025,false);			
			flags.start_tutor4 = false;			
			set_visible_object( obj.gear1.id, false );
			set_visible_object( obj.gear2.id, false );
			set_visible_object( obj.gear3.id, false );
			set_visible_object( obj.gear4.id, false );
			set_visible_object( obj.gear5.id, false );
			set_visible_object( obj.pendulum.id, false );			
			set_visible_object( obj.coin.id, false);
			set_visible_object( obj.nail.id, false);
			set_visible_object( obj.crown.id, false);
			set_visible_object( obj.bucket.id, false);
			set_visible_object( obj.shovel.id, false);
			set_visible_object( obj.coil_spring.id, false);
			set_visible_object( obj.nut.id, false);
			set_visible_object( obj.rope.id, false);
			set_visible_object( obj.spider.id, false);
		
			set_visible_object( 3, false );
			set_visible_object( 4, false );
			
			set_visible_object( obj.gear1_nvg.id, true );
			set_visible_object( obj.gear2_nvg.id, true );
			set_visible_object( obj.gear3_nvg.id, true );
			set_visible_object( obj.gear4_nvg.id, true );
			set_visible_object( obj.gear5_nvg.id, true );
			set_visible_object( obj.pendulum_nvg.id, true );

			for i=17,33 do
				SetAlfa(i,1.0,0.2,1);
			end;
			flags.green =true;
			SetAlfa(1015,0.0,1.0,1,"endOfAlpha");
			nvg_on = true;
			flags.setnvg=false;
			--flags.nvg_block=true;
			flags.light1=false;
			flags.nvg_on=true;
			Set_Cursor(1012,2);
		return;end;
	
	
	return;end;
	
--выключили пнв когда вспышка в глаза	
	if sender==1000022 then
	
	if flags.light  then
			nvgselect=false;
			flags.light = false;
			flags.lit = true;
			flags.nvg_on = false;
			nvg_on = false;
			SetAlfa(1009,1.0,0.0,0.5);
			SetAlfa(1015,1.0,0.0,1);
			flags.declineNVG=false
			flags.glove_block= false;
			for i=17,33 do
				SetAlfa(i,0.1,1.0,1);
			end;
			StopEmiter("Highlight");
			StartEmiter("Highlight1");			
			setcolor(obj.coin.id,0,0,0,1,1);
			setcolor(obj.nail.id,0,0,0,1,1);
			setcolor(obj.crown.id,0,0,0,1,1);
			setcolor(obj.bucket.id,0,0,0,1,1);
			setcolor(obj.shovel.id,0,0,0,1,1);
			setcolor(obj.coil_spring.id,0,0,0,1,1);
			setcolor(obj.nut.id,0,0,0,1,1);
			setcolor(obj.rope.id,0,0,0,1,1);
			setcolor(obj.spider.id,0,0,0,1,1);
			speciale_action(2, obj.coin.id, true);
			speciale_action(2, obj.nail.id, true);
			speciale_action(2, obj.crown.id, true);
			speciale_action(2, obj.bucket.id, true);
			speciale_action(2, obj.shovel.id, true);
			speciale_action(2, obj.coil_spring.id, true);
			speciale_action(2, obj.nut.id, true);
			speciale_action(2, obj.rope.id, true);
			speciale_action(2, obj.spider.id, true);
			speciale_action(2, 3, true );
			speciale_action(2, 4, true );
			flags.nvg_block =true;
			SwitchToSheet("re");
			white=true;
			CreatePolygon(6678,4,"Object111");
			Set_Cursor(6678,1);		
		
			StartTimer(232134,2,"pachka1")
			
			
			function pachka1()
				StartEmiter("SvetMig11");
				StartEmiter("SvetMig12");
				StartEmiter("SvetMig10");
				StartTimer(232131,1,"pachka2")
			end
			
			function pachka2()
				StartEmiter("SvetMig9");
				StartEmiter("SvetMig8");
				StartEmiter("SvetMig7");
				StartTimer(232132,1,"pachka3")
			end
			
			function pachka3()
				StartEmiter("SvetMig6");
				StartEmiter("SvetMig5");
				StartEmiter("SvetMig4");
			end		
			
			StartEmiter("SvetMig2");
			StartEmiter("SvetMig1");
			StartEmiter("SvetMig");
			
			return;end;
	return;end

--стрекоза, гаджеты
	if sender ==41 then	
	RotateHandler(obj_id);		
	if obj_id ==obj.dragonfly.id then	
	StartAnimation( 5555555555,1,5 );
	return;end;
	return;end
	
	
	if sender == 33 then	
--сообщение после мини игры
	if flags.most then	
	CreatePlashechka(666,"101","goodgirl")
	flags.most=false;
	SwitchToSheet("re")
	end
	
--какаято херня	
	if flags.green then
		flags.green=false;
		if not flags.nadeliraz then		
		flags.nadeliraz= true
		end
	return; end;

--вышли и не нажали на гаджет со стрелкой
	if flags.after_save and flags.start_tutor4 then
		set_visible_object(5557, true);
		StartAnimation(5557);		
		playeffect("018")
		flags.after_save=false;
	end;
	
		if flags.mini1_complete and flags.mini1_comp then			
			flags.mini1_comp = false;		
			StopEmiter("Highlight1");
			ShowMenu(true);
		end;
		if obj_id == 1015 and flags.lit then
			flags.lit = false;			
			flags.sabj=true;
		end;		
		
	end;
	if sender==777 then		
		if top_plush_id and flags.comics then
				flags.useNVG=true
				StartTimer(6625253,1,"tutor11")
				BlockCursor(true)
			function tutor11()				
				set_visible_object(5557, true);
				StartAnimation(5557);	
				playeffect("018")
				flags.tutor2lvl=true;
				BlockCursor(false)
			end			
			CreatePlashechka(66,"100","goodgirl",true)			
				flags.start_tutor5 = false;				
				flags.after_save= true;			
			
			SwitchToSheet("re")
			--flags.comics = false;	
			end	
			
			
--старт мини игры
			if flags.mini1 and not flags.mini1_complete  then 		
			leafobj={2234}
			GetRenderTarget(2234,"rendertar");	
			mininame="mini1";
			mininum1=2;
			mininum2=10;
			mininame2="cave";
		return; end;
	 if top_plush_id then		
		ShowMenu(true)
		end	
	
	end
	if sender==28 then
	
--старт локации
		
	return;end

if sender == 100 then
		
		if flags.first_time_load then
				flags.first_time_load=false
			set_visible_object(obj.intro.id,true);
			speciale_action(4,obj.intro.id);			
		return; end;	
		
return; end;

--нажали на рентген	
	if sender == 100002	then		
		if flags.sabj then
			CreatePlashechka(9999,"64","goodgirl")
		end;
	return; end;

--===================================================================
	if sender == 14 then
	
		if obj_id == obj.gear11.id or obj_id == obj.gear21.id or obj_id == obj.gear31.id or obj_id == obj.gear41.id or obj_id == obj.gear51.id or obj_id == obj.pendulum1.id then
			addObjectToMouse(obj_id);
			takeobjecttohand=true;
		end;

	return; end; -- sender == 14 --

	
	if sender == 8 then
	if (flags.mini1_complete or flags.mechanism ) and not takeobjecttohand then
		HideGlove();		
	end
	if not flags.mechanism then
	if not flags.comics4 then
		CreatePlashechka(3442,"20002","goodgirl")
		takeobjecttohand=false
		return;end
		
		if flags.nvg_block then
			CreatePlashechka(3442,"10002","goodgirl")
		return; end;
		
		if not flags.light1 then
				CreatePlashechka(3442,"20002","goodgirl")
				takeobjecttohand=false
		return;end;
	end
	takeobjecttohand=false
	if flags.light then declineGadjet=false;
			nvgselect=false;
			 set_visible_object(-100022, false); 			 
			 gameinput(1000022, -1, -1);
			
			RotateWheelBack(414,270,342,487,198,3,4,3);	 
			set_visible_object(5561,false);	
			
	end
		
		
	if attachedID ==-1 then			
		HideNvg()	
	end
		if flags.stone_on then
			flags.stone_on = false;
			RemoveFromMouse(17);
			Set_Cursor(17,4);
			SetObjectPosition(17,154,439);
			set_visible_object(1011,false);
		end;
	end;
	-- End RMB --
	
--===================================================================


if sender ==36 then

--яркий свет и говорим что капец светит в глаза
if obj_id==88 then	
	CreatePlashechka(9999,"53","goodgirl")
end
if obj_id ==77 then
CreatePlashechka(9999,"66","goodgirl")
end
return;end;
	
	
	if sender == 12 then
--коллапс мусора
		if not flags.items_collected then
		if	obj_id == obj.coin.id or obj_id == obj.nail.id or obj_id == obj.crown.id or obj_id == obj.bucket.id or 
			obj_id == obj.shovel.id or obj_id == obj.coil_spring.id or obj_id == obj.nut.id or obj_id == obj.rope.id or obj_id == obj.spider.id then
				
				-- flags.items = flags.items + 1;
				if flags.items== 9 then
					CreatePlashechka(9999,"52","goodgirl")
					flags.items_collected = true					
					AlternateTimer("SwitchSheet1",1000)	
					
				end;
		end;
		end
--коллапс шестеренок			
		if obj_id == obj.gear1.id or obj_id == obj.gear2.id or obj_id == obj.gear3.id or obj_id == obj.gear4.id or obj_id == obj.gear5.id or obj_id == obj.pendulum.id then
			Set_Cursor(obj_id,4)			
				
			flags.gears = flags.gears + 1;
			for i,v in pairs(shesterni) do
			if shesterni[i] == obj_id then
			table.remove(shesterni, i );
			end;
			end
			
			if flags.gears == 6 then
				Set_Cursor(1012,2);
				flags.frame4 = true;
				for i,v in pairs(obj) do
					if obj[i].id == obj_id then
						AddElement(sheet,obj[i.."1"].id);
						MoveObjectToTree(obj[i.."1"].id);			
						obj[i.."1"].is_stored = true;
						switched=true;			
						takeobjecttohand=false
					return; end;
				end;
			return end;
			StoreObj(obj_id);		
		return; end;
	return; end;
					
		if sender == 1 then	
--яркий свет и тыкаем на свет	
		if flags.light then
			CreatePlashechka(9999,"61","goodgirl")
		return; end;

--переход на след локацию		
		if obj_id == 6678 and not canusegadjets  then
			if flags.items == 9 and flags.mini1_complete then
				flags.finish = true
				ShowMenu(false)
				HideGlove()											
				minigg=true				
				del_object_from_game(obj.dragonfly.id)
				leafobj={2234}
				GetRenderTarget(2234,"rendertar")					
				levelnext=3
				
				
			return; end;			
		if flags.mini1_complete then
		CreatePlashechka(9999,"67","goodgirl")
		return;end;		
		StartTimer(77,0.2)
			
		end;	
		
--коллапсы шестеренок		
		if obj_id == obj.gear1_nvg.id then
			set_visible_object( obj_id, false );
			set_visible_object( obj.gear1.id, true );
			speciale_action( 4, obj.gear1.id );
			CollapseObj( obj.gear1.id );
		return; end;
--коллапсы шестеренок			
		if obj_id == obj.gear2_nvg.id then
			set_visible_object( obj_id, false );
			set_visible_object( obj.gear2.id, true );
			speciale_action( 4, obj.gear2.id );
			CollapseObj( obj.gear2.id );
		return; end;
--коллапсы шестеренок			
		if obj_id == obj.gear3_nvg.id then
			set_visible_object( obj_id, false );
			set_visible_object( obj.gear3.id, true );
			speciale_action( 4, obj.gear3.id );
			CollapseObj( obj.gear3.id );
		return; end;
--коллапсы шестеренок			
		if obj_id == obj.gear4_nvg.id then
			set_visible_object( obj_id, false );
			set_visible_object( obj.gear4.id, true );
			speciale_action( 4, obj.gear4.id );
			CollapseObj( obj.gear4.id );
		return; end;
--коллапсы шестеренок			
		if obj_id == obj.gear5_nvg.id then
			set_visible_object( obj_id, false );
			set_visible_object( obj.gear5.id, true );
			speciale_action( 4, obj.gear5.id );
			CollapseObj( obj.gear5.id );
		return; end;
--коллапсы шестеренок			
		if obj_id == obj.pendulum_nvg.id then
			set_visible_object( obj_id, false );
			set_visible_object( obj.pendulum.id, true );
			speciale_action( 4, obj.pendulum.id );
			CollapseObj( obj.pendulum.id );
		return; end;
		-- End Gears. Nightvision --
--аттачим шестерни		
		if obj_id == 1012 then
			if attachedID~=-1 then
				if  attachedID == obj.gear11.id then
					playeffect("035")					
					SetAlfa(32,1.0,0.5,1);
					flags.gear1_on = true;
					RemoveFromMouse(obj.gear11.id );
						UnStoreObj(obj.gear11.id);
				end;
				if  attachedID == obj.gear21.id then
						playeffect("035")
					SetAlfa(22,1.0,0.5,1);
					flags.gear2_on = true;
					RemoveFromMouse( obj.gear21.id);
						UnStoreObj(obj.gear21.id);
				end;
				if  attachedID == obj.gear31.id then
						playeffect("035")
					SetAlfa(24,1.0,0.5,1);
					flags.gear3_on = true;
					RemoveFromMouse(obj.gear31.id);
						UnStoreObj(obj.gear31.id);
				end;
				if  attachedID == obj.gear41.id then
						playeffect("035")
					SetAlfa(23,1.0,0.5,1);
					flags.gear4_on = true;
					RemoveFromMouse( obj.gear41.id );
						UnStoreObj(obj.gear41.id);
				end;
				if  attachedID == obj.gear51.id then
						playeffect("035")
					SetAlfa(29,1.0,0.5,1);
					flags.gear5_on = true;
					RemoveFromMouse(obj.gear51.id );
						UnStoreObj(obj.gear51.id );
				end;
				if  attachedID == obj.pendulum1.id then
						playeffect("035")
					SetAlfa(20,1.0,0.5,1);
					flags.gear6_on = true;
					RemoveFromMouse(obj.pendulum1.id);
						UnStoreObj(obj.pendulum1.id);
				end;
			

				Set_Cursor(1012,2);
				flags.gears_on = flags.gears_on + 1;
				takeobjecttohand=false
				if flags.gears_on == 6 then
					playeffect("036")
					flags.light = true;
					flags.light1=true;
					declineGadjet=true
					flags.mechanism=true;
					takeobjecttohand=false
					Set_Cursor(17,4);
					speciale_action(2,1012);
					speciale_action(2,1009);
					-- CreatePlashechka(9999,"53","goodgirl")
					StartTimer(88,1)
					 set_visible_object(5561,true)
					StartAnimation(5561)
					playeffect("018")
					speciale_action(4,5561)
					StopEmiter("Highlight");
					flags.declineNVG=true
					if not flags.mini1_complete then
					StartEmiter("Highlight1");
					end;				
			
			end;
			else
			
				CreatePlashechka(9999,"55","goodgirl")
				end
		return; end; -- obj_id == x and attachedID == y --
		
		if	obj_id == obj.coin.id or obj_id == obj.nail.id or obj_id == obj.crown.id or obj_id == obj.bucket.id or 
			obj_id == obj.shovel.id or obj_id == obj.coil_spring.id or obj_id == obj.nut.id or obj_id == obj.rope.id or obj_id == obj.spider.id then
				if takeobjecttohand then return;end
				CollapseObj( obj_id );
				
				flags.items = flags.items + 1;
				if flags.items== 9 then					
					flags.items_collected = true					
				end
				
				
		return; end;
--берем камень в руку	
		if obj_id == 17 and not flags.stone_removed and flags.sabj then
			if glove_on then 
				flags.stone_on = true;
				speciale_action(4,obj_id);
				-- speciale_action(4,1013);
				--addObjectToMouse(obj_id,-30,-30);
				add_to_mouse(obj_id)
				takeobjecttohand=true
				SetCustomCursor(obj.glove.id);
				setcolor(18,0,0,0,0);
				set_visible_object(1011,true);				
			else
				CreatePlashechka(9999,"54","goodgirl")
			return; end;
		return; end;
--тычем на прозрачный камень		
		if obj_id == 1011 and glove_on and flags.stone_on and takeobjecttohand then
			flags.stone_on = false;
			flags.stone_removed = true;
			setcolor(1011,1,1,1,1);
			RemoveFromMouse(17);
			setcolor(17,1,1,1,0);
			takeobjecttohand=false
			set_visible_object(1010,true);
			Set_Cursor(1010,2);
			Set_Cursor(17,3);
			playeffect("032")
			HideGlove();
			
		
		return; end;
--жмем на рычаг		
		if obj_id == 1010 then -- lever
		
			if not flags.mini1 and not flags.mini1_complete then
				HideGlove()
				playeffect("033")
				flags.mini1 = true;
				ShowMenu(false);
				ClosePlushes();
			end;
			
		return; end;
	return; end;
end;



function SelectRandom()
local dist=-1;local a=-1;local b=-1;local v=100;local t=-1;local diag=-1;
local width = -1;local height = -1;local radius=1.2;


	if flags.items_collected then
		-- DO something		
		if not flags.stone_removed then
		computation(17);	--летим на камень	
		return; end;
		if not flags.mini1_complete then
		computation(1010);	--летим на рычаг	
		return; end;		
		computation(10144);	 --летим на переход	
	return;end;		

	
	ok = false
	while not ok do
		help_id = GetRandomObject();		
	
		for i,v in pairs(obj) do
			if v.id == help_id and not v.is_collapsed  then
				ok = true
				
				if not flags.mechanism then			
				
					if obj.gear1.is_collapsed and obj.gear2.is_collapsed and obj.gear3.is_collapsed and obj.gear4.is_collapsed and obj.gear5.is_collapsed and  obj.pendulum.is_collapsed then
						computation(19);
					return end
					
				
				if help_id ~=obj.gear1.id and help_id ~=obj.gear2.id and help_id ~=obj.gear3.id and help_id ~=obj.gear4.id and help_id ~=obj.gear5.id and help_id ~=obj.pendulum.id then
					SelectRandom()	
				--	c("\n help_id= "..help_id.."\n")
				return	end
                -- g=	GetRandom(1,table.maxn(shesterni))	
				-- for i,v in pairs(obj) do
					-- if g== obj[i].id  then
						-- if  not obj[i.."1"].is_stored then
							-- computation(shesterni[g]);
						-- else
							 -- g=	GetRandom(1,table.maxn(shesterni))	
						-- end
						
						
					-- return; end;
				-- end;
				
			--	table.remove(shesterni, g );
				x,y=GetObjectPosition(v.id);
				x1,y1=GetObjectPosition(obj.dragonfly.id);
				width,height = GetObjectSize(v.id)
				a= (x-x1); b= (y-y1);  dist= math.sqrt (a^2+b^2);				
		        t=dist/400;		
                r = (math.sqrt(width^2+height^2)*0.5);		
				
				if r<50 then	r=75;	radius=4;	end  -- при радиусе меньше 50 определяем время кружения и радиус стрекозы
				
				if r>50 and r <100 then	r=87 radius=2.5;  end  --при радиусе больше 50 и меньше 100
				
				if r>100 then 	r=100 	radius=0.75;  end  -- при радиусе больше 100
				
				StartBigTravel(obj.dragonfly.id, v.id, 0, t, r/90, t, 2 ,radius);
                else 
				x,y=GetObjectPosition(v.id);
				x1,y1=GetObjectPosition(obj.dragonfly.id);
				width,height = GetObjectSize(v.id)
				a= (x-x1); b= (y-y1);  dist= math.sqrt (a^2+b^2);				
		        t=dist/400;		
                r = (math.sqrt(width^2+height^2)*0.5);		
				
				if r<50 then	r=75;	radius=4;	end  -- при радиусе меньше 50 определяем время кружения и радиус стрекозы
				
				if r>50 and r <100 then	r=87 radius=2.5;  end  --при радиусе больше 50 и меньше 100
				
				if r>100 then 	r=100 	radius=0.75;  end  -- при радиусе больше 100
				
				StartBigTravel(obj.dragonfly.id, v.id, 0, t, r/90, t, 2 ,radius);
				return;end;
			return end
		end
	end
end