local ACode=string.byte("a");
is_plashechka=false    
plush_id=9000000    
text_id=0        
local textp_id=5654547        
rep_plashka=false        
PrevText=0
CurrCur=0
CurrItem=0
ItemsCur={}
PointingItems={}   
TargetID=987654
Target2ID=987655
hint_objg=0;
TimeHScaling=0.15
g_attached=0
count_CPU=0
timer_starting=false;
take_subject=false
not_input=false
replay=false
to_render=false
globalX=0
globalY=0

function Coords(idR)
  xR,yR=GetObjectPosition(idR)
  wR,hR=GetObjectSize(idR)
  clX=xR+wR-34-10
  clY=yR+10
  skX=xR+wR/2-52
  skY=yR-3
  hlX=xR+10
  hlY=yR+10
  Corners={}
  Corners["xl"]=xR-5
  Corners["yl"]=yR-5
  Corners["xr"]=xR+wR+5
  Corners["yr"]=yR+hR+5  
return clX,clY,skX,skY,hlX,hlY
end;

function OnMouseOverMenu(ncount)
  if not flags.minipen then
    ClearCursor()
  end;    
end;

function RemObjFromMouse()
    if flags.Obj_In_Cur then
        FreeObjectFromCursor()
        flags.Obj_In_Cur=false
    end;
end;

function AddObjToMouse(obj_id,at_id)
    RemObjFromMouse()
    if obj_id~=at_id then
        AddObjectToCursor(10000001,obj_id,8,12)
        flags.Obj_In_Cur=true
    return;end;
    flags.Obj_In_Cur=false
end;

function hintinput(sender,id)
    if sender==36 then
        h_step=1;
        h_timer=2;
        ScaleObject(hint_objg,1,1.3,TimeHScaling,globalX,globalY)
    return;end;
    if sender==29 then
        if h_step==1 or h_step==3 then
            ScaleObject(id,1.3,1,TimeHScaling,globalX,globalY)
        end;
        if h_step==2  then
            ScaleObject(id,1,1.3,TimeHScaling,globalX,globalY)
        end;        
        if h_step==4 then
            if h_timer <2 then
                StartTimer(67849,TimeHScaling*2)
            else
			speciale_action(4,TargetID) 
            SetAlfa(TargetID,1,0,0.5)
              --  if not take_subject then
                HintFinish()
             --   end;
            end;
        end;
        h_step=h_step+1;
    end;
    if sender==1 then
        StopEmiter("ushelp")
        g_sender=sender;    
        take_subject=true    
 		 h_step=4
		  h_timer=2		
      --  HintFinish()
    return;end;
end;
function HintFinish ()
    StopEmiter("ushelp")
    set_inpun_fun("gameinput")
    DeleteRenderTarget(TargetID)    
    ReturnHiddenObjects()        
    leafobj={}    
	
	hintobject=-1
    flags.blockrender=false
    WriteToConsole("In Hint flags.blockrender false = "..tostring(flags.blockrender).."\n")
    if take_subject then
        take_subject=false
        gameinput(g_sender,hint_objg)        
    end;
    if flags.is_final_loc then
            speciale_action(2,100)
            PauseTravel(100,false)
        set_inpun_fun("gameinput")
    end;
    hint_objg=0
end;

function selectrandomobjectus(param1,idObject)
    if not flags.blockrender then
	hintobject=idObject
	--speciale_action(2,hintobject)
    flags.blockrender=true
    WriteToConsole("In Hint flags.blockrender true = "..tostring(flags.blockrender).."\n")
    flags.Hint_using=true;
    hint_objg=idObject
    set_inpun_fun("hintinput")
    ShowCursor(false)
    if flags.is_final_loc then
        speciale_action(2,100)
    end;
    GetRenderTarget(TargetID,"rendertar");
--	speciale_action(2,hintobject)
    if not flags.is_final_loc then
    leafobj={TargetID,idObject}    
    else

        leafobj={TargetID,idObject}    
        PauseTravel(100,true)
    end;    
    return;end;
end;

function  ClearCursor()
   SetObjectPosition(500001,2000,2000)
   SetObjectPosition(500002,2000,2000)
   SetObjectPosition(500004,2000,2000)

   FreeCustomCursor(500001)
   FreeCustomCursor(500002)
   FreeCustomCursor(500004)
   for i=500011,500018,1 do
    SetObjectPosition(i,2000,2000)
    FreeCustomCursor(i)
   end;
   CurrCur=0;    
end;
   
function ClearCursor1()
   SetObjectPosition(CurrCur,2000,2000)
   FreeCustomCursor(CurrCur)
   CurrCur=0;    
end;

function ClearCurTables()
   for i,v in pairs(ItemsCur) do
        Set_Anim_Cur(i,3)
   end;
   ItemsCur={}
end;

function AddObjForCur()
  Corners={}
  Corners["xl"]=0
  Corners["yl"]=0
  Corners["xr"]=1024
  Corners["yr"]=768

    add_animation(500002)
        add_animation_frame(500002,"all_cursor",2000,0,63 ,0,64,64,1);
        add_animation_frame(500002,"all_cursor",2000,0,127,0,64,64,0.3);
    StartAnimation(500002)    
    add_animation(500001)
        add_animation_frame(500001,"all_cursor",2000,0,0  ,64,64,64,0.4);
        add_animation_frame(500001,"all_cursor",2000,0,64 ,64,64,64,0.4);
    StartAnimation(500001)    
    add_animation(500004)
        add_animation_frame(500004,"all_cursor",2000,0,0 ,129,63,63,1);
    add_animation_frame(500004,"all_cursor",2000,0,129,129,63,63,0.3);
        add_animation_frame(500004,"all_cursor",2000,0,64,129,64,64,0.3);
    add_animation_frame(500004,"all_cursor",2000,0,129,129,63,63,0.3);
    StartAnimation(500004)    
  
---- вниз ---------
    add_animation(500011)
        add_animation_frame(500011,"all_cursor",2000,0,192,0,63,63,1);
    add_animation_frame(500011,"all_cursor",2000,0,258,0,63,63,0.3);
        add_animation_frame(500011,"all_cursor",2000,0,322,0,65,63,0.3);
    add_animation_frame(500011,"all_cursor",2000,0,258,0,63,63,0.3);
    StartAnimation(500011) 
---вправо-вниз
    add_animation(500012)
        add_animation_frame(500012,"all_cursor",2000,0,192,64,63,63,1);
    add_animation_frame(500012,"all_cursor",2000,0,258,64,63,63,0.3);
        add_animation_frame(500012,"all_cursor",2000,0,322,64,64,64,0.3);
    add_animation_frame(500012,"all_cursor",2000,0,258,64,63,63,0.3);
    StartAnimation(500012) 
--влево-вниз
    add_animation(500013)
        add_animation_frame(500013,"all_cursor",2000,0,192,128,63,63,1);
    add_animation_frame(500013,"all_cursor",2000,0,258,128,63,63,0.3);
        add_animation_frame(500013,"all_cursor",2000,0,322,128,64,64,0.3);
    add_animation_frame(500013,"all_cursor",2000,0,258,128,63,63,0.3);
    StartAnimation(500013) 
--вверх
    add_animation(500014)
        add_animation_frame(500014,"all_cursor",2000,0,192,192,63,63,1);
    add_animation_frame(500014,"all_cursor",2000,0,258,192,63,63,0.3);
        add_animation_frame(500014,"all_cursor",2000,0,322,192,64,64,0.3);
    add_animation_frame(500014,"all_cursor",2000,0,258,192,63,63,0.3);
    StartAnimation(500014) 
--вверх-вправо
    add_animation(500015)
        add_animation_frame(500015,"all_cursor",2000,0,192,256,63,63,1);
    add_animation_frame(500015,"all_cursor",2000,0,258,256,63,63,0.3);
        add_animation_frame(500015,"all_cursor",2000,0,322,256,64,64,0.3);
    add_animation_frame(500015,"all_cursor",2000,0,258,256,63,63,0.3);
    StartAnimation(500015) 
--вверх-влево
    add_animation(500016)
        add_animation_frame(500016,"all_cursor",2000,0,192,320,63,63,1);
    add_animation_frame(500016,"all_cursor",2000,0,258,320,63,63,0.3);
        add_animation_frame(500016,"all_cursor",2000,0,322,320,64,64,0.3);
    add_animation_frame(500016,"all_cursor",2000,0,258,320,63,63,0.3);
    StartAnimation(500016) 
--вправо
    add_animation(500017)
        add_animation_frame(500017,"all_cursor",2000,0,192,384,63,63,1);
    add_animation_frame(500017,"all_cursor",2000,0,258,384,63,63,0.3);
        add_animation_frame(500017,"all_cursor",2000,0,322,384,64,64,0.3);
    add_animation_frame(500017,"all_cursor",2000,0,258,384,63,63,0.3);
    StartAnimation(500017) 
--влево
    add_animation(500018)
        add_animation_frame(500018,"all_cursor",2000,0,192,448,63,63,1);
    add_animation_frame(500018,"all_cursor",2000,0,258,448,63,63,0.3);
        add_animation_frame(500018,"all_cursor",2000,0,322,448,64,64,0.3);
    add_animation_frame(500018,"all_cursor",2000,0,258,448,63,63,0.3);
    StartAnimation(500018) 
  
end;

function CursorTreatment(Item_id,sender,attachedID)



    if sender==2 then
        MessageProc(sender,obj_id);
    return;end;

    if sender==8 then
        RemObjFromMouse()
    end;
    
    if sender==14 then
        if  objectid==0 then
            AddObjToMouse(Item_id,attachedID)
        end;
    return; end;
    
    if sender==44 then
        AboutItem(Item_id)
    return;end;  
    
    if sender==35 then
        if CurrCur~=0 and  CurrItem~=Item_id then
            ClearCursor()
            CurrItem=Item_id    
        end;
        if ItemsCur[Item_id]~=nil then 
                if ItemsCur[Item_id]==1 then
                    CurrCur=500001;
                end;    
                if ItemsCur[Item_id]==2 then
                    CurrCur=500002;
                end;    
                if ItemsCur[Item_id]==4 then
                    CurrCur=500004;        
                end;

                if ItemsCur[Item_id]==11 then
                    CurrCur=500011;        
                end;
                if ItemsCur[Item_id]==12 then
                    CurrCur=500012;        
                end;
                if ItemsCur[Item_id]==13 then
                    CurrCur=500013;        
                end;
                if ItemsCur[Item_id]==14 then
                    CurrCur=500014;        
                end;
                if ItemsCur[Item_id]==15 then
                    CurrCur=500015;        
                end;
                if ItemsCur[Item_id]==16 then
                    CurrCur=500016;        
                end;
                if ItemsCur[Item_id]==17 then
                    CurrCur=500017;        
                end;
                if ItemsCur[Item_id]==18 then
                    CurrCur=500018;        
                end;
           
            CurrItem=Item_id
            if not flags.Obj_In_Cur then
                SetCustomCursor(CurrCur,-32,-32)
            else
                SetCustomCursor(CurrCur,-15,-15)
            end;
        end;
    end;
    if sender==39 then
        if CurrItem~=0 and CurrItem~=nil and ItemsCur[CurrItem]~=nil and CurrItem==Item_id then
            ClearCursor1()
            CurrItem=0
        end;    
    end;
end;

function Set_Anim_Cur(Item_id,Cur_id)
    ItemsCur[Item_id]=Cur_id;
    if Cur_id~=3 then
        OnMouseOver(Item_id,true)
    else
        ClearCursor()
        StopOnMouseOver(Item_id)
    end;    

end;



function AboutItem(Item_id)

StartAbout=true;
function AboutInput(sender,id)
    WriteToConsole(" AboutInput(sender,id)"..sender.." "..id.." \n")
    if sender==36 and id==909099 then
        set_inpun_fun("gameinput");
        StartAbout=false
    end;
    if sender==26 then
        DeleteResource(res_name);
        WriteToConsole("Delete Resource "..res_name.."\n")
        g_img=""
        res_name=""
        StartTimer(909099,0.5)
    end;    
end;
function LoadAbout(img)
    set_inpun_fun("AboutInput");
    res_str=("data\\window_items\\"..tostring(img)..".png")
    WriteToConsole("Load Resourse"..res_name.."\n")
    loadresource(res_str,res_name,654)    
end;
    RemObjFromMouse()    
    g_x=281
    g_y=220
    if obj.med~=nil and Item_id==obj.med.id then
        g_img="med_o"
        res_name=g_img.."w"        
        LoadAbout(g_img);
        WriteToConsole("AboutItem \n")
        CreateWindow1(100,100,"2011",nil,"okno",285,350,res_name,g_x,g_y,obj.med.notice_ext,"AboutInput")
    return;end;
    if obj.jhndk~=nil and Item_id==obj.jhndk.id then
        g_img="dnevnik"
        res_name=g_img.."w"        
        LoadAbout(g_img);    
        CreateWindow1(100,100,"291",nil,"okno",335,360,res_name,g_x,g_y,obj.jhndk.notice_ext,"AboutInput")
    return;end;
    if obj.vint~=nil and Item_id==obj.vint.id then
        g_img="rujjo"
        res_name=g_img.."w"        
        LoadAbout(g_img);        
        CreateWindow1(100,100,"49",nil,"okno",335,360,res_name,g_x,g_y,obj.vint.notice_ext,"AboutInput")
    return;end;
    if obj.zakl~=nil and Item_id==obj.zakl.id then
        g_img="zakl"
        res_name=g_img.."w"        
        LoadAbout(g_img);        
        CreateWindow1(100,100,"50",nil,"okno",335,350,res_name,g_x,g_y,obj.zakl.notice_ext,"AboutInput")
    return;end;
    if obj.photo~=nil and Item_id==obj.photo.id then
        g_img="foto"
        res_name=g_img.."w"        
        LoadAbout(g_img);            
        CreateWindow1(100,100,"122",nil,"okno",335,350,res_name,g_x,g_y,obj.photo.notice_ext,"AboutInput")
    return;end;
    if obj.scrtng~=nil and Item_id==obj.scrtng.id then
        g_img="t_nog"
        res_name=g_img.."w"        
        LoadAbout(g_img);        
        CreateWindow1(100,100,"192",nil,"okno",335,450,res_name,g_x,g_y,obj.scrtng.notice_ext,"AboutInput")
    return;end;
    if obj.prbira~=nil and Item_id==obj.prbira.id then
        g_img="probira"
        res_name=g_img.."w"        
        LoadAbout(g_img);        
        CreateWindow1(100,100,"111",nil,"okno",285,350,res_name,g_x,g_y,obj.prbira.notice_ext,"AboutInput")
    return;end;
    if obj.merdn~=nil and Item_id==obj.merdn.id then
        g_img="vot_ono"
        res_name=g_img.."w"        
        LoadAbout(g_img);    
        CreateWindow1(100,100,"246",nil,"okno",285,350,res_name,g_x,g_y,obj.merdn.notice_ext,"AboutInput")
    return;end;

end;

function Spirit_Particles()
    if flags.is_spirit then
    forduh={
    0,0,
    0,1,
    1,1,
    1,0
    }
    CreatePolygon(987,4,"forduh");
    CreatePolygon(988,4,"forduh");
    CreatePolygon(989,4,"forduh");
    CreatePolygon(990,4,"forduh");
    AddParticleToObject(987,"duh_up",512 ,0);
    MoveEmiter("duh_up",512 ,0);
    StartEmiter("duh_up");
    AddParticleToObject(988,"duh_down",512 ,768);
    MoveEmiter("duh_down",512 ,768);
    StartEmiter("duh_down");
    AddParticleToObject(989,"duh_left",0 ,384);
    MoveEmiter("duh_left",0 ,384);
    StartEmiter("duh_left");
    AddParticleToObject(990,"duh_right",1024 ,384);
    MoveEmiter("duh_right",1024 ,384);
    StartEmiter("duh_right");    
    end;
end;


function load()

end


function SetFlagsForSatButtons()
    btn_over={}
    btn_over[10001]=false
    btn_over[10011]=false
    btn_over[10021]=false
    btn_press={}
    btn_press[10001]=false
    btn_press[10011]=false
    btn_press[10021]=false    
end


function HideSkip()
    set_visible_object(2000,false)
    set_visible_object(2002,false)
    set_visible_object(2006,false)
    set_visible_object(2007,false)
    set_visible_object(2004,false)
    set_visible_object(10011,false)
    set_visible_object(10013,false)
    set_visible_object(10012,false)    
end;

function AddSkipBtn(SkipX,SkipY)
    if not from_menu then
    addstaticobjecttogame(2000,"buttons_wind",SkipX,SkipY-7 ,"",142,416,113,46); --ветки скипа малые
    speciale_action(10,2000);
    addtexttogame(2002,SkipX+55,SkipY,"1013",71); 
    setcolor(2002,0.8,0.8,0.55,1,1);
    set_text_property(2002,100,100,2)
    speciale_action(10,2000);
    speciale_action(10,2002);
    addstaticobjecttogame(2006,"buttons_wind",SkipX-2,SkipY-2,"",5,357,116,35); --рамка скипа
    speciale_action(10,2006);
    addstaticobjecttogame(10013,"buttons_wind",SkipX,SkipY,"",8,401,111,31); --желтая скипа
    speciale_action(2,10013);
    addstaticobjecttogame(10012,"buttons_wind",SkipX,SkipY,"",8,440,111,31); --салатов скипа
    speciale_action(2,10012);	
    addstaticobjecttogame(10011,"buttons_wind",SkipX+2,SkipY,"",131,361,111,31); --зеленая скипа
    speciale_action(2,10011)

    addstaticobjecttogame(2004,"buttons_wind",SkipX,SkipY,"",257,361,111,31); --подложка скипа
    OnMouseOver(10011);
    addstaticobjecttogame(2007,"buttons_wind",SkipX-103,SkipY,"",11,96,319,43); --ветки скипа большие
    speciale_action(4,2007)
    speciale_action(4,2004)

    speciale_action(4,10011)
	    speciale_action(4,10012)
    speciale_action(4,10013)
    speciale_action(4,2006)
    speciale_action(4,2000)
    speciale_action(4,2002); 
    StartProgress(10011,120,true)
    else
        addstaticobjecttogame(2007,"buttons_wind",SkipX-103,SkipY,"",11,96,319,43);
        addstaticobjecttogame(2004,"buttons_wind",SkipX,SkipY,"",257,361,111,31); --подложка скипа
        addstaticobjecttogame(10012,"buttons_wind",SkipX,SkipY,"",8,440,111,31); --салатов скипа
        addstaticobjecttogame(10011,"buttons_wind",SkipX+2,SkipY,"",131,361,111,31); --зеленая скипа
        addstaticobjecttogame(2006,"buttons_wind",SkipX-2,SkipY-2,"",5,357,116,35); --рамка скипа
        addtexttogame(2002,SkipX+55,SkipY,"1013",71); 
        setcolor(2002,0.8,0.8,0.55,1,1);
        set_text_property(2002,100,100,2)        
        addstaticobjecttogame(2000,"buttons_wind",SkipX,SkipY-7 ,"",142,416,113,46); --ветки скипа малые
        StartProgress(10011,30,true)
    end;
    

end;

function CreateCloseBtn(btnX,btnY,btnWidth,btnHeight)
  addstaticobjecttogame(20003,"buttons_wind",btnX-7,btnY+3,"",130,1,47,32); --пимпа
  speciale_action(10,20003)
  addstaticobjecttogame(10003,"buttons_wind",btnX,btnY,"",0,0,34,34); --нажат кнопка
  speciale_action(2,10003);
  addstaticobjecttogame(10002,"buttons_wind",btnX,btnY,"",37,0,34,34); --навели-
  speciale_action(2,10002);
  addstaticobjecttogame(10001,"buttons_wind",btnX,btnY,"",75,0,34,34); --активная кнопка
  OnMouseOver(10001);
  speciale_action(4,10001)
  speciale_action(4,10002)
  speciale_action(4,10003)
  speciale_action(4,20003)
end;


function CreateHelpBtn(btnX,btnY,btnWidth,btnHeight)
  addstaticobjecttogame(10040,"buttons_wind", btnX-7,btnY+3 ,"",130,1,47,32); --пимпа хелпа 
  addstaticobjecttogame(10023,"buttons_wind",btnX+1,btnY-1,"",0,39,btnWidth,btnWidth); --нажат кнопка
  speciale_action(2,10023);
  addstaticobjecttogame(10022,"buttons_wind",btnX,btnY,"",38,39,btnWidth,btnWidth); --навели-
  speciale_action(2,10022);
  addstaticobjecttogame(10021,"buttons_wind",btnX,btnY,"",78,39,btnWidth,btnWidth); --активная кнопка
  OnMouseOver(10021);
  addstaticobjecttogame(10042,"buttons_wind", btnX-10,btnY-10,"",10,169,134,148); --ветки хелпа 
  speciale_action(4,10042)
  speciale_action(4,10021)
  speciale_action(4,10022)
  speciale_action(4,10023)  
  speciale_action(4,10040)  
end;

function HideHelp()
    set_visible_object(10040,false)
    set_visible_object(10023,false)
    set_visible_object(10022,false)
    set_visible_object(10021,false)
    set_visible_object(10042,false)
end;

function ButtonInput (sender,id)
  if sender==6 then
    if btn_press[lastbtn] then
        btn_press[lastbtn]=false;
        speciale_action(2,lastbtn+2)
        speciale_action(2,lastbtn)    
        if is_plashechka then
            stopping=true
            HidePlashechka()            
        end;            
        FinishFunc()
    end;
   else
   end;
end; 

function ButtonTreatment(obj_id,btn_id1,btn_id2,btn_id3,btnX,btnY,btnWidth,btnHeight,sender,SatName,is_mini,msgText,msgX,msgY) 
function FinishFunc()
   
   if lastbtn==10001 then
      if is_mini then
        finish_mini_game(false);
      end;
      WriteToConsole("1 \n")
      clear_satelite_leaf(SatName);    
      WriteToConsole("2 \n")
      set_inpun_fun("gameinput")
      WriteToConsole("3 \n")
      WaitBtnClick=false
      WriteToConsole("4 \n")
      lastbtn=0
   return;end;
   if lastbtn==10021 then
      btn_press[lastbtn]=false
      CreatePlashechka(900000,tostring(real_msgText))
      lastbtn=0
   return;end;     
   if lastbtn==10011 then
      SkipActions()
      skip=false
      set_inpun_fun("gameinput")
      lastbtn=0
   return;end;      
end;
 WriteToConsole("sender IN BUTTONS = "..sender.."\n")

  if sender == 5  then
    cur_x,cur_y=GetCursorPos()
    if cur_x>=btnX-10 and cur_x<=btnX+btnWidth+10 and cur_y>=btnY-10 and cur_y<=btnY+btnHeight+10 then
        if not btn_press[btn_id1] then     
          lastbtn=btn_id1 
          WaitBtnClick=true
          WriteToConsole("last btn = "..lastbtn.."\n")
          btn_press[lastbtn]=true    
          if msgText~=nil then
          real_msgText=msgText
          end;
          speciale_action(2,lastbtn+2)
          speciale_action(2,lastbtn+1)       

          -- set_inpun_fun("ButtonInput")
          end;
    end;
  
  
  return;end;
  
  if sender==6 or sender==1 then
  xx,yy=GetCursorPos()

    if btn_press[lastbtn] then
        WriteToConsole("Have TO press = "..lastbtn.."\n")
        btn_press[lastbtn]=false;
        speciale_action(2,lastbtn+2)
       -- speciale_action(2,lastbtn)    
        if is_plashechka then
            stopping=true
            HidePlashechka()            
        end;        
        FinishFunc()        
    return;end;
  if xx<=Corners["xl"] or yy<=Corners["yl"] or xx>=Corners["xr"]  or yy>=Corners["yr"] then
     if not (objectid~=0 and mini_puzzle) then
    --clear_satelite_leaf("kabinet");
    lastbtn=10001    
    FinishFunc()
    end
  return; end;    
   end;
  
  if sender == 35 then
    if obj_id == btn_id1 and not btn_press[btn_id1] then
      btn_over[btn_id1]=true
      speciale_action(2,btn_id1+1);
    --  speciale_action(2,btn_id1);      
     end;    
  end;
  
  if sender == 39 and (not btn_press[btn_id1]) then
    if btn_over[btn_id1] then
      btn_over[btn_id1]=false;
      speciale_action(2,btn_id1+1);
     -- speciale_action(2,btn_id1);    
     end;
  end;

end;

function StartHint()
  --SelectRandom();
  playeffect("hint")
  StartTimer(444,hint_time,"FinishHint",true)
 
end;

function FinishHint()

  flags.GlobalHint = false;
  MessageProc(5,id)
end;

function StringToInt(param1)
  local numb = 0;
  local str = param1;
  local len = string.len(str)
  local pow = 1;
  for i = 1,len do
    numb = numb + (string.byte(str, len - i + 1) - ACode) * pow;
    pow = pow * 26;    
  end;
return numb; end;
----------------------------------------------------------------------------------------------------
function SetActivLeaf(leafname)
  set_activ_leaf(leafname,level,sublevel);
return; end;

function Block_Show_Cur(param,AttachedItem)
   if param then
      ClearCursor1()
      if not flags.IngameMenu then
      ShowCursor(false)
      BlockCursor(true)
    end
   return;end;
      ShowCursor(true)
      BlockCursor(false)   
return;end;

leafobj={}
name_g=""
sat_ng=0;
sat_sg=0;
leaf_nameg=""

function Set_Sat(name,sat_n,sat_s,leaf_name)
satelliteopen=true   
    flags.Is_Satellite=true
    name_g=name
    sat_ng=sat_n;
    sat_sg=sat_s;
    leaf_nameg=leaf_name;

    if is_plashechka then
        stopping=true
        HidePlashechka()            
        to_render=true;
    return;
    end;    
    ClearCursor()
    ClearCurTables()
    ShowCursor(false)
    ShowSheet("gr","move",1000,false);
    leafobj={TargetID}
    GetRenderTarget(TargetID,"rendertar");
end;

function Set_Sat1(name,sat_n,sat_s,leaf_name)
    flags.Is_Satellite=true
    name_g=name
    sat_ng=sat_n;
    sat_sg=sat_s;
    leaf_nameg=leaf_name;
    ClearCursor()
    ShowCursor(false)
    --ShowMenu(false)
    HidePlashechka()
 if flags.Is_Satellite then


  add_free_leaf("Root",name_g,sat_ng,sat_sg);-- Добавляет чистый лист
  set_satelite_leaf(leaf_nameg);
  DoFile(sat_ng,sat_sg) 
      if IsMiniGame then
        IsMiniGame=false
        speciale_action(2,10011)
        mini_create();
      end;
  end;
    
end;

function rendertar1(tex_name,tex_id)
function targetinput(sender,id)
if sender==100 then
    WriteToConsole("Target Input 1 \n")
    DeleteRenderTarget(TargetID1)    
    WriteToConsole("Target Input 2 \n")
    ReturnHiddenObjects()    
    WriteToConsole("Target Input 3 \n")    
    leafobj1={}
    WriteToConsole("Target Input 4 \n")
    set_inpun_fun("gameinput")
    WriteToConsole("Input Fun = gameinput \n")    
end;    
end;
    WriteToConsole("make Satellite target \n")
    SetTextureID(TargetID1,tex_id)
  --  FlipVertical(TargetID1);
   -- SetObjectPosition(TargetID1,0,-256);
    -- speciale_action(2,TargetID1);
    -- speciale_action(2,TargetID1,true,2);
    SetAlfa(TargetID1,0,1,3);
    LeaveObjectsOnReder("leafobj1");
    WriteToConsole("SetAlfa target \n")    
    --set_inpun_fun("targetinput")
--    WaitRender()

    

end;


-- effect = PostprocessEffect.BLUR
-- function rendertar(tex_name,tex_id)
 -- WriteToConsole("In rendertarhet \n")
 -- FlipVertical(TargetID);
 -- SetObjectPosition(TargetID,0,-256);
 -- LeaveObjectsOnReder("leafobj"); 
 -- if not tree_fall then
    
    
    -- if effect == PostprocessEffect.BLUR then
        -- effect = PostprocessEffect.EMBOSS
    -- elseif effect == PostprocessEffect.EMBOSS then
        -- effect = PostprocessEffect.WAVE
    -- elseif effect == PostprocessEffect.WAVE then
        -- effect = PostprocessEffect.SWAP_COLOURS
    -- elseif effect == PostprocessEffect.SWAP_COLOURS then
        -- effect = PostprocessEffect.BLUR
    -- end
    
    -- Postprocess( TargetID, effect )
    
    
 -- end;
 -- SetAlfa(TargetID,0,1,0)
 -- ShowCursor(true) 
 -- if flags.Is_Satellite then
  -- WriteToConsole("Target in Satellite \n")
  -- add_free_leaf("Root",name_g,sat_ng,sat_sg);-- Добавляет чистый лист
  -- set_satelite_leaf(leaf_nameg);
  -- DoFile(sat_ng,sat_sg) 
 -- if IsMiniGame then
-- IsMiniGame=false
-- speciale_action(2,10011)
-- mini_create();
 -- end;
 
  -- end;
  -- if flags.Shtraf_using then       
-- flags.Shtraf_using=false
-- WriteToConsole("Shtraf Render \n");
-- LeaveObjectsOnReder("leafobj"); 
-- set_visible_object(12345000,true)
-- StartAnimation(12345000,0,4)
-- speciale_action(4,12345000)
-- WriteToConsole("Start Shtraf Timer \n");
-- StartTimer(7674834,4,"ShtrafFinish")
-- end;
  -- if flags.Hint_using then
-- speciale_action(4,hint_objg) 
-- WriteToConsole("In HintUsing subject "..hint_objg.." \n")
-- flags.Hint_using=false
-- LeaveObjectsOnReder("leafobj"); 
-- wo,ho=GetObjectSize(hint_objg,true)
-- SetHotPoint(hint_objg,math.floor(wo/2),math.floor(ho/2))
-- RotateParticle(hint_objg,"collaps2",2,1);
-- h_timer=1;
-- h_step=1;
-- ScaleObject(hint_objg,1,1.3,TimeHScaling)
-- end;
-- return end

function rendertar(tex_name,tex_id)
 WriteToConsole("In rendertarhet \n")
 --FlipVertical(TargetID);
-- SetObjectPosition(TargetID,0,-256);
 LeaveObjectsOnReder("leafobj"); 
 if not tree_fall then
    BlackAndWite(TargetID);
 end;
 SetAlfa(TargetID,0,1,0)
 ShowCursor(true) 
 if flags.Is_Satellite then
  WriteToConsole("Target in Satellite \n")
  add_free_leaf("Root",name_g,sat_ng,sat_sg);-- Добавляет чистый лист
  set_satelite_leaf(leaf_nameg);
  DoFile(sat_ng,sat_sg) 
      if IsMiniGame then
        IsMiniGame=false
        speciale_action(2,10011)
        mini_create();
      end;
   satelliteopen=false   
  end;
  if flags.Shtraf_using then           
        flags.Shtraf_using=false
        WriteToConsole("Shtraf Render \n");
    --    LeaveObjectsOnReder("leafobj"); 
        set_visible_object(12345000,true)        
        StartAnimation(12345000,0,4)
        speciale_action(4,12345000)
        WriteToConsole("Start Shtraf Timer \n");
        StartTimer(7674834,4,"ShtrafFinish")
    end;    
  if flags.Hint_using then    
        speciale_action(4,hint_objg) 
         WriteToConsole("In HintUsing subject "..hint_objg.." \n")
        flags.Hint_using=false
    --    LeaveObjectsOnReder("leafobj"); 
        wo,ho=GetObjectSize(hint_objg,true)    
     --   SetHotPoint(hint_objg,math.floor(wo/2),math.floor(ho/2))
		globalX=math.floor(wo/2)
		globalY=math.floor(ho/2)
        RotateParticle(hint_objg,"collaps2",2,1);
        h_timer=1;
        h_step=1;         
        ScaleObject(hint_objg,1,1.3,TimeHScaling,globalX,globalY)
    end;    
return end


function DoFile(level,sub_level)
-- HidePlashechka()
 
 if obj.plashechka~=nil then
 set_visible_object(obj.plashechka.id,false);
 set_visible_object(5654547,false);
 end;
ClearCursor()
 UpdateStack();
 dofile("./data/"..level.."/"..sub_level.."/res.lua");
 dofile("./data/"..level.."/"..sub_level.."/level.lua");
    -- leafobj1={TargetID1}
    -- WriteToConsole("Create Satellite target \n")
    -- GetRenderTarget(TargetID1,"rendertar1"); 
end

-- function CurrLeaf()
    -- BlockCursor(false)
-- end;

function setcurentleaf(leafname,level,sublevel)        
    ClearCursor()
    WriteToConsole("7 \n")
    ClearCurTables()
    WriteToConsole("8 \n")    
    if is_plashechka then
        HidePlashechka()
    end;
    SetActivLeaf(leafname,level,sublevel);
    WriteToConsole("9 \n")
     dofile("data\\"..level.."\\"..sublevel.."\\level.lua");
     WriteToConsole("10 \n")
     if not flags.is_hidden_loc then
       SwitchToSheet("gr");
       SetDrawSheet("gr",true);
     end;
     if flags.is_hidden_loc then
       SwitchToSheet("re");
       SetDrawSheet("gr",false);
     end;
     g_leafname=leafname;
     g_level=level;
     g_sublevel=sublevel;
     if flags.Is_Satellite then
        flags.Is_Satellite=false
        DeleteRenderTarget(TargetID)    
        ReturnHiddenObjects()        
        leafobj={}
        WriteToConsole("11 \n")
        -- BlockCursor(true)
        -- StartTimer(765432,0.5,"CurrLeaf",true)
     end;
     SetCursors()  
WaitRender()	 
     WriteToConsole("12 \n")
return; end;

function setcurentleaf1(leafname,level,sublevel)          
    ClearCursor()
    ClearCurTables()      
    SetActivLeaf(leafname,level,sublevel);
     dofile("data\\"..level.."\\"..sublevel.."\\level.lua");
     if not flags.is_hidden_loc then
       SwitchToSheet("gr");
       SetDrawSheet("gr",true);
     end;
     if flags.is_hidden_loc then
       SwitchToSheet("re");
       SetDrawSheet("gr",false);
     end;
     g_leafname=leafname;
     g_level=level;
     g_sublevel=sublevel;
     if flags.Is_Satellite then
        flags.Is_Satellite=false
        -- DeleteRenderTarget(TargetID)    
        -- ReturnHiddenObjects()        
        -- leafobj={}
     end;
     SetCursors()        
return; end;
--[[
Значения fMethod:
  0x00000008 - Окно выезжает из-за экрана слева направо на указанную позицию.
  0x00000010 - Окно проявляется в указанной позиции.
--]]
function f1()

    if pl_process then 
        is_plashechka=false
        pl_process=false
        BlockCursor(false)
        if rep_plashka  then
            rep_plashka=false
            CreatePlashechka(plush_id,text_id)
        return;end;        
        if not not_input then 
             if StartWin then
                WriteToConsole("f1 \n")
                set_inpun_fun("CreateWindow1Input")
             else
                WriteToConsole("f2 \n")
                set_inpun_fun("gameinput")
                gameinput (26,plush_id) 
				
                WriteToConsole("16 \n")
             end;    

        else
        
        end;
		if is_tutorial then
		set_inpun_fun("tutorialGameInput")
		end;
        if to_render then
            to_render=false
            ClearCursor()
            ClearCurTables()
            ShowCursor(false)
            ShowSheet("gr","move",1000,false);            
            leafobj={TargetID}
            GetRenderTarget(TargetID,"rendertar");
        end;    
        WriteToConsole("17 \n")
    end;
end;
function HidePlashechka()
  if is_plashechka and not pl_process  then
    pl_process=true
    WriteToConsole("14 \n")
  if not flags.IngameMenu then
    BlockCursor(true)
  end
    set_visible_object(5654547,false)
    set_visible_object(obj.plashechka.id,false)
     StartTimer(23413,0.1,"f1")
     WriteToConsole("24 \n")
  end;    
end;

function f2()
    if pl_process then 
        BlockCursor(false)
        pl_process=false
        addtexttogame(5654547,512,2,text_id,900)
        set_text_property(5654547,100,100,2)
        PrevText=text_id
        set_text(5654547,text_id)
        set_visible_object(5654547,true);
        speciale_action(4,5654547)        
        setcolor(5654547,1,1,1,1,1)
        speciale_action(4,5654547)
        AlternateTimer("HidePlashechka",10*1000);
    end;
end

function CreatePlashechka(id,text)
function PlashechkaInput(sender,id,attachedID)

    gameinput(sender,id,attachedID)
    if sender==6 or sender==1 or sender==10 or sender==14 or sender==50 and is_plashechka and not pl_process then
        stopping=true
        if is_plashechka then
            stopping=true
            WriteToConsole("13 \n")    
            HidePlashechka()
        end;    
    end;        
    
end;
    if not pl_process then
    plush_id=id
    text_id=text
    if not is_plashechka  then
        set_inpun_fun("PlashechkaInput")
        WriteToConsole("Input Fun = PlashechkaInput \n")   
        NewObj("plashechka","MWind",0,0,"",0,0,1024,90);
        set_visible_object(obj.plashechka.id,true);
        speciale_action(4,obj.plashechka.id)
        speciale_action(10,obj.plashechka.id)
        WriteToConsole("Start P \n")
        is_plashechka=true                        
        if not flags.IngameMenu then BlockCursor(true) end
        SetAlfa(obj.plashechka.id,0,1,0.1)
        pl_process=true
        StartTimer(1222,0.12,"f2")
    else
        if text~=PrevText then
            WriteToConsole("An click P \n")
            rep_plashka=true
            HidePlashechka()
        end;
    end;
    end;
end


function CreateWindow1(posx,posy,textid,windowId,image,textX,textY,item,itemX,itemY,notice_ext,inputfun)
    function CreateWindow1Input(sender,obj_id)        
        WriteToConsole("IN WINDOW sender = "..sender.."\n")
        if sender==41 and obj_id==1000028 then
        StartTravel(1000028,2,nil,nil,travel_obj1,travel_obj2);
        end;
        if sender==6 then
            DeleteResource(item)
            del_object_from_game(windowId)
            del_object_from_game(5654548)
            del_object_from_game(1000028)
            del_object_from_game(5654549)
            StartWin=false
            if inputfun=="AboutInput" then
            WriteToConsole(" Set InPUT FUN \n")
                set_inpun_fun("AboutInput")
                AboutInput(26,windowId)                
            end;
            if inputfun=="StoreInput" then
                set_inpun_fun("StoreInput")
                StoreInput(26,windowId)
            end;
            if inputfun==nil or inputfun=="" then
                set_inpun_fun("gameinput")
                gameinput(26,windowId)
                WriteToConsole("INPUT FUN = GAMEINPUT")
            end;            
        end;
    end;
 ClearCursor()
 StartWin=true
  set_inpun_fun("CreateWindow1Input")
  if windowId==nil then windowId=987654321 end;
  if notice_ext=="" or notice_ext==nil then    
    ww,hh=GetTextureSize("okno1")
    winx=(1024-693)/2
    winy=(600-526)/2    
    WriteToConsole("CreateWindow okno1 \n")
    addstaticobjecttogame(windowId,"okno1",winx,winy,"");
    textX=290
    textY=350     
    itemX=279    
    itemY=220    
  else
    ww,hh=GetTextureSize("okno")
    winx=(1024-547)/2
    winy=(600-509)/2   
    WriteToConsole("CreateWindow okno \n")
    addstaticobjecttogame(windowId,"okno",winx,winy,"");    
    textX=250
    textY=300 
    itemX=229    
    itemY=180
  end;    
  set_visible_object(windowId,true)
  speciale_action(4,windowId)
  addtexttogame(5654548,512,textY+winy,textid,200)
  setcolor(5654548,0.93,0.88,0.8,1,1)
  set_text_property(5654548,100,100,2)
  set_text(5654548,textid)
  speciale_action(4,5654548)
  if item~=nil then 
    addstaticobjecttogame(1000028,item,itemX+winx+5,itemY+winy+5,"") 
    WriteToConsole("Add static object "..item.."\n")
    speciale_action(4,1000028)
    travel_obj1={"InstantiationOfCircleR",3,itemX+winx,itemY+winy,itemX+winx+5,itemY+winy+2, 0, -math.pi,3,false}
    travel_obj2={"InstantiationOfCircleR",3,itemX+winx,itemY+winy,itemX+winx+5,itemY+winy+2, -math.pi,0, 3,false}
    StartTravel(1000028,2,nil,nil,travel_obj1,travel_obj2);
    WriteToConsole("Start Travel \n")
   end;
   WriteToConsole("notice_ext before = "..tostring(notice_ext).."\n")
  if notice_ext~=nil then
      WriteToConsole("notice_ext = "..notice_ext.."\n")
      addtexttogame(5654549,512,348+winy,notice_ext,300)
      set_text_property(5654549,100,70,2)
      setcolor(5654549,0.93,0.88,0.8,1,1)
      set_text(5654549,notice_ext)
      speciale_action(4,5654549)
  end;
end;


function CreateWindow(posx,posy,textid,windowId,image,textX,textY,item,itemX,itemY,notice_ext,fMethod)
  playeffect("page")
  ClearCursor()
  if textX==nil then textX=200 end;
  if textY==nil then textY=100 end;
  if fMethod==nil then fMethod=16 end;
  if image=="MWind" then
     AddWindow(0,0,1024,100,windowId,fMethod);
     SetWindowElementTextColor(textid,1,0,1,1);
  end;
  if image~="MWind" then
    AddWindow(posx,posy,460,256,windowId,fMethod);    
  end;
  AddWindowElementText(1000025,textX,textY,textid);
  setcolor(1000025,1,1,1,1,1); 
  if item~=nil then AddWindowElementImage(1000028,item,itemX,itemY) end;
  if image==nil
    then
      AddWindowElementImage(1000024,"message",0,0);
    else
      AddWindowElementImage(1000024,image,0,0); 
  end;
  if notice_ext~=nil then
    SetWindowElementTextColor(notice_ext,1,0,1,1);
    AddWindowElementText(1000026,300,300,notice_ext);
  end;
  SetWindowElementTextColor(textid,0,0,0,1);
end;

function DarkScreen(todark)
  if todark then
    if not whole_darkness then
        WriteToConsole("Half dark true \n")
        Block_Show_Cur(true);
    --    speciale_action(2,501,true);
        whole_darkness=true
        speciale_action(4,501);
        SetAlfa(501,0,0.7,0.1);            
     return;end;    
     WriteToConsole("Can't half dark true \n")
  return;end;
    if whole_darkness then
        WriteToConsole("Half dark false \n")
        whole_darkness=false;
        Block_Show_Cur(true);
    --    speciale_action(2,501,true);
        SetAlfa(501,0.7,0,0.1);
        --emitters(true);
    return; end;    
    WriteToConsole("Can't half dark false \n")
end;

function DarkScreen1(todark)
  if todark then
    if not whole_darkness then
        BlockCursor(true);
    --    speciale_action(2,501,true);
        whole_darkness=true
        SetAlfa(501,0,1,1);            
        speciale_action(4,501);
        
     return;end;        
  return;end;
    if whole_darkness then
        whole_darkness=false;
        BlockCursor(true);
    --    speciale_action(2,501,true);
        SetAlfa(501,1,0,1);
        --emitters(true);
        -- messagebox("qweqweqeqw");
    end;    
end;

-- function lalala()
    -- WriteToConsole("lalala \n")

-- end;






function SaveLavel(tabe)
    save_this(tabe);
end;

function ShtrafFinish()
    set_visible_object(12345000,false)
    WriteToConsole("Finish Shtrafing \n");
    DeleteRenderTarget(TargetID)    
    ReturnHiddenObjects()        
    leafobj={}    
    WriteToConsole("Return From Render \n");
    BlockCursor(false)
    flags.blockrender=false
    WriteToConsole("In Shtrafing flags.blockrender false = "..tostring(flags.blockrender).."\n")
    if flags.is_final_loc then
    --    CPU_Random()
        speciale_action(2,100)
        PauseTravel(100,false)
    end;
end;

function S_I()

end;


function Shtrafing()
    if not flags.blockrender then
    if flags.is_hidden_loc then
         flags.blockrender=true
         WriteToConsole("In Shtrafing flags.blockrender true = "..tostring(flags.blockrender).."\n")
        ClearCursor()    
        ShowCursor(false)
        xCur,yCur=GetCursorPos()
        WriteToConsole("Start Shtrafing \n");
        BlockCursor(true)
        xAnim=xCur-69
        yAnim=yCur-98
        add_animation(12345000,xAnim,yAnim);
        add_animation_frame(12345000, "tresh1"  ,xAnim,yAnim,0,0,138,196,0.1)     --799
        add_animation_frame(12345000, "tresh1"  ,xAnim,yAnim,138,0,138,196,0.1)
        add_animation_frame(12345000, "tresh1"  ,xAnim,yAnim,276,0,138,196,3)
        add_animation_frame(12345000, "tresh1"  ,xAnim,yAnim,138,0,138,196,0.1)
        add_animation_frame(12345000, "tresh1"  ,xAnim,yAnim,0,0,138,196,0.1)
        
        if flags.is_final_loc then
            speciale_action(2,100)
        end;
        
        GetRenderTarget(TargetID,"rendertar");
        
        if not flags.is_final_loc then
    --        leafobj={TargetID,idObject}    
            leafobj={TargetID,12345000}    
        else
            leafobj={TargetID,12345000}    
            PauseTravel(100,true)
            -- speciale_action(2,100)
            -- speciale_action(4,100)
            -- WriteToConsole("idCPUObject = "..idCPUObject.."\n")
            -- leafobj={TargetID,8887,100,idCPUObject,12345000}    
        end;
        
        flags.Shtraf_using=true
    end;
    end;
end;


function InvitationTutorial ()
    function invitationGameInput(sender,obj_id)
  --  sendersProcessing(sender,obj_id,attachedID)
      if sender==35 then
        if obj_id==45127 or obj_id==45128 then
          setcolor(obj_id-2,0.7,0.5,0.5,1,1)
        end;
      end;
      if sender==39 then
        if obj_id==45127 or obj_id==45128 then
          setcolor(obj_id-2,0.93,0.88,0.8,1,1)
        end;
      end;      
      if sender == 1  then
        windowtutorial=false
        if obj_id==45127 then
          flags.ShowTutorial=true
  StopOnMouseOver(45127)
  StopOnMouseOver(45128)		  
          del_object_from_game(45123)
          del_object_from_game(45124)
          del_object_from_game(45125)
          del_object_from_game(45126)
		   StartTimer(7465873,0.5,"CloseTutorial")
       set_inpun_fun("gameinput")
          -- gameinput(154)            
        return;end;
        if obj_id==45128 then
          flags.ShowTutorial=false
  StopOnMouseOver(45127)
  StopOnMouseOver(45128)			  
          del_object_from_game(45123)
          del_object_from_game(45124)
          del_object_from_game(45125)
          del_object_from_game(45126)
		   StartTimer(7465873,0.5,"CloseTutorial")
         set_inpun_fun("gameinput")
          -- gameinput(154)            
        return;end;      
      
      end
    end
    
  addstaticobjecttogame(45123,"tutorial_win",314,200,"")
  speciale_action(4,45123)
  speciale_action(2,45123)
  speciale_action(2,45123,true,0.5)
  winw,winh=GetObjectSize(45123)
  addtexttogame(45124,512,250,"10002")
  speciale_action(4,45124)
  set_text_property(45124,100,100,2)
  setcolor(45124,0.93,0.88,0.8,1,1)
  set_text(45124,"10002")    
  addtexttogame(45125,400,350,"10003")
  speciale_action(4,45125)
  set_text_property(45125,100,100,2)
  setcolor(45125,0.93,0.88,0.8,1,1)
  set_text(45125,"10003")  
  addtexttogame(45126,600,350,"10004")
  speciale_action(4,45126)
  set_text_property(45126,100,100,2)
  setcolor(45126,0.93,0.88,0.8,1,1)
  set_text(45126,"10004")  
  for_yes={
    370,350,
    430,350,
    430,400,
    370,400
  }
  for_no={
    580,350,
    630,350,
    630,400,
    580,400  
  }  
  CreatePolygon(45127,4,"for_yes") 
  CreatePolygon(45128,4,"for_no") 
    speciale_action(4,45127)
  speciale_action(4,45128)
  OnMouseOver(45127,true)
  OnMouseOver(45128,true)
  set_inpun_fun("invitationGameInput")
end;

function CloseTutorial()
	nexttutorial=true
	tutorialprocessing=false	
	BlockCursor(false)		
	if gl_sender==1 then		  
		  gameinput(gl_sender,gl_obj,gl_attach)  
          gl_sender=0
		  gl_obj=0
		  gl_attach=0	
	else	  
	WaitRender()
	end;
end;

function CreateTutorial(text,x,y,arr_type,object)
  
    function tutorialGameInput(sender,obj_id,attachedID)
    CursorTreatment(obj_id,sender,attachedID);
    
  
    if sender==36 then
      if obj_id==45632 then
        Block_Show_Cur(false);
      return;end;
    return;end;
    if sender==41 then
      if obj_id==45125 or obj_id==45126 then
        if arr_type==0 then
          if direction==0 then
            StartTravel(obj_id,1,nil,nil,{"InstantiationOfLine",2,x+winw,y+winh,x+winw-30,y+winh-30,0.5,false})
            direction=1
          else
            StartTravel(obj_id,1,nil,nil,{"InstantiationOfLine",2,x+winw-30,y+winh-30,x+winw,y+winh,0.5,false})
            direction=0
          end;
        end;
        if arr_type==1 then
          if direction==0 then
            StartTravel(obj_id,1,nil,nil,{"InstantiationOfLine",2,x-56,y+winh,x-56+30,y+winh-30,0.5,false})
            direction=1
          else
            StartTravel(obj_id,1,nil,nil,{"InstantiationOfLine",2,x-56+30,y+winh-30,x-56,y+winh,0.5,false})
            direction=0
          end;        
        end;  
        if arr_type==2 then
          if direction==0 then
            StartTravel(obj_id,1,nil,nil,{"InstantiationOfLine",2,x+winw,y-86,x+winw-30,y-86+30,0.5,false})
            direction=1
          else
            StartTravel(obj_id,1,nil,nil,{"InstantiationOfLine",2,x+winw-30,y-86+30,x+winw,y-86,0.5,false})
            direction=0
          end;        
        end;  
        if arr_type==3 then
          if direction==0 then
            StartTravel(obj_id,1,nil,nil,{"InstantiationOfLine",2,x-56,y-56,x-56-30,y-56-30,0.5,false})
            direction=1
          else
            StartTravel(obj_id,1,nil,nil,{"InstantiationOfLine",2,x-56-30,y-56-30,x-56,y-56,0.5,false})
            direction=0
          end;        
        end;       
      end;
    end;
        if sender == 1 or sender==6 then         
		gl_sender=sender
		  gl_obj=obj_id
		  gl_attach=attachedID
		 
          BlockCursor(true)
		  is_tutorial=false
          del_object_from_game(45123)
          del_object_from_game(45124)
          del_object_from_game(45125)
          del_object_from_game(45126)
	set_inpun_fun("gameinput")	  
		  StartTimer(7465873,0.2,"CloseTutorial")

     --     gameinput(154)  
		  
		  -- if sender==1 then		  
		  -- gameinput(sender,obj_id,attachedID)  
		  -- end;
		  ClearCursor() 
          WriteToConsole("Zakrili okno tutoriala \n")
        end
   -- end
    end

  set_inpun_fun("tutorialGameInput")
  BlockCursor(true);
  StartTimer(45632,1)
  is_tutorial=true  
  addstaticobjecttogame(45123,"tutorial_win",x,y,"")
  speciale_action(4,45123)
  speciale_action(2,45123)
  speciale_action(2,45123,true,0.5)
  winw,winh=GetObjectSize(45123)
  tx=winw/2+x
  ty=y+50
  addtexttogame(45124,tx,ty,text,winw-20)
  speciale_action(4,45124)
  set_text_property(45124,100,100,2)
  setcolor(45124,0.93,0.88,0.8,1,1)
  set_text(45124,text)  
  direction=0 
  if arr_type==0 then
  addstaticobjecttogame(45125,"tutorial_arrow",x+winw,y+winh,"")
  --MoveByVector(45125,x+winw-30,y+winh-30,0.5,100)
  StartTravel(45125,1,nil,nil,{"InstantiationOfLine",2,x+winw,y+winh,x+winw-30,y+winh-30,0.5,false})
  end;
  if arr_type==1 then
  addstaticobjecttogame(45125,"tutorial_arrow1",x-56,y+winh,"")
  --MoveByVector(45125,x-56+30,y+winh-30,0.5,100)
  StartTravel(45125,1,nil,nil,{"InstantiationOfLine",2,x-56,y+winh,x-56+30,y+winh-30,0.5,false})
  end;
  if arr_type==2 then
  addstaticobjecttogame(45125,"tutorial_arrow2",x+winw,y-86,"")
  --MoveByVector(45125,x+winw-30,y-86+30,0.5,100)
  StartTravel(45125,1,nil,nil,{"InstantiationOfLine",2,x+winw,y-86,x+winw-30,y-86+30,0.5,false})
  end;
  if arr_type==3 then
  addstaticobjecttogame(45125,"tutorial_arrow3",x-56,y-56,"")
  --MoveByVector(45125,x-56-30,y-56-30,0.5,100)
  StartTravel(45125,1,nil,nil,{"InstantiationOfLine",2,x-56,y-56,x-56-30,y-56-30,0.5,false})
  end;  
  -- if arr_type==4 then
  -- addstaticobjecttogame(45125,"tutorial_arrow3",x-56,y-56,"")
  -- MoveByVector(45125,x-56+30,y-56+30,0.5,100)
  -- addstaticobjecttogame(45126,"tutorial_arrow2",x+winw,y-86,"")
  -- MoveByVector(45126,x+winw+30,y-86-30,0.5,100)  
  --end;  
  speciale_action(4,45125)
  speciale_action(4,45126)
  speciale_action(2,45125)
  speciale_action(2,45126)
  speciale_action(2,45125,true,0.5)  
  speciale_action(2,45126,true,0.5)  
end; 


function ChangeAlfa()
    if stepAlfa==1 then
        SetAlfa(g_curr_anim,0.7,0.2,6,"ChangeAlfa")
        stepAlfa=2
    return;end;
    if stepAlfa==2 then
        SetAlfa(g_curr_anim,0.2,1,6,"ChangeAlfa")
        stepAlfa=3
    return;end;
    if stepAlfa==3 then
        SetAlfa(g_curr_anim,1,0.4,6,"ChangeAlfa")
        stepAlfa=4
    return;end;
    if stepAlfa==4 then
        SetAlfa(g_curr_anim,0.4,0,6,"ChangeAlfa")
        stepAlfa=5
    return;end;    
end;
function ChangeAnim()
   g_moving=math.random()
   n_moving=math.random(0,1);
   s_moving=math.random(3,7);
    m=g_curr_anim
    WriteToConsole("Zamena "..g_curr_anim.." "..m.."\n")
        StartTimer(78321,30,"ChangeAnim",true)    
        StartPosX,StartPosY=GetObjectPosition(g_curr_anim)
        g_moving=math.pi*g_moving
        flyMenu={"InstantiationOfCircleR",3,StartPosX,StartPosY,StartPosX-s_moving,StartPosY-s_moving, 0, g_moving,6,false}
        StartMoving=g_moving
        WriteToConsole("StartMoving = "..g_moving.."\n")
        g_moving=math.random()
        g_moving=math.pi*g_moving
        if g_moving-StartMoving<=1 then
            g_moving=g_moving+1
        end;    
        flyMenu1={"InstantiationOfCircleR",3,StartPosX,StartPosY,StartPosX-s_moving,StartPosY-s_moving, StartMoving, g_moving,6,false}
        StartMoving=g_moving
        WriteToConsole("StartMoving = "..g_moving.."\n")
        g_moving=math.random()
        g_moving=math.pi*g_moving
        if g_moving-StartMoving<=1 then
            g_moving=g_moving+1
        end;    
        flyMenu2={"InstantiationOfCircleR",3,StartPosX,StartPosY,StartPosX-s_moving,StartPosY-s_moving, StartMoving, g_moving,6,false}
        StartMoving=g_moving
        WriteToConsole("StartMoving = "..g_moving.."\n")
        g_moving=math.random()
        g_moving=math.pi*g_moving
        if g_moving-StartMoving<=1 then
            g_moving=g_moving+1
        end;    
        flyMenu3={"InstantiationOfCircleR",3,StartPosX,StartPosY,StartPosX-s_moving,StartPosY-s_moving, StartMoving, g_moving,6,false}        
        StartMoving=g_moving
        WriteToConsole("StartMoving = "..g_moving.."\n")
        flyMenu4={"InstantiationOfCircleR",3,StartPosX,StartPosY,StartPosX-s_moving,StartPosY-s_moving, StartMoving, 0,6,false}        
        StartTravel(g_curr_anim,5,nil,nil,flyMenu,flyMenu1,flyMenu2,flyMenu3,flyMenu4,true);
        stepAlfa=1;
end;
function RestoreSheet(name)
addstaticobjecttogame(777777777,"buttons",75,698,"",0,0,61,61);
SetCurrentRM("menu");
    AddSheet(1001,"gr","green_b",75,698,61,61,"green",155,663,650,60,23,40,17,10,1,48,777777777);
SetCurrentRM("main");
end;
function SaveLevel()
    local val="";

    for key,value in pairs(flags) do
        if(value) then
            val=val.."t"; 
        else
            val=val.."f";
        end;
    end
return val; end;            
function LoadLevel(param)
    local c=1;
    local val="";
    c=1;
    for key,value in pairs(flags) do
        val=string.char(string.byte (param,c));
        if(val=="t") then
            flags[key]=true;
        else
            flags[key]=false;        
        end;
        c=c+1;
    end
return; end;