local Data={0,0,0,0,0,0,0,0,0};
local pt={450,105,536,213,605,339,660,460,526,460,392,460,257,460,312,339,369,213};
local iCellWidth=86;
local iCellHeight=86;
local iStartId=646;
local nCells=9;
function mini_create ()
  show_help_wnd=true;
  init_mini(1);
  create_mini(8128);
end;
--function mini_msg_proc (id)
function gameinput (sender,id,attachedID)

  if sender==30 then    
BlockCursor(false);  
  setcurentleaf("kabinet",4,0);
    flags.nohints=false;
    if flags.minicomplete48 then
      setcurentleaf("kabinet",4,0);
      StopEmiter("tel"); 
      playeffect("29");
      DeleteElement("gr",109);
      Set_Cursor(40,3);
      flags.phone=true;
      ShowMenu(false);
      frame=0;
      add_free_leaf("comics1",4,51);
      set_satelite_leaf("kabinet");
      DoFile(4,51)
    return; end;  
  return; end;  
  if sender==10 then
    flags.minicomplete48=true;
    StartTimer(2100,1)
    BlockCursor(true);
  return; end;
  if sender==37 then
     skipping=true;
  return;end;  
  if sender==36 and id==2100 then
clear_satelite_leaf("kabinet");
  return;end;  
    if sender==26 then
    if id==100 then
    set_visible_object(5001,true)
    end;
  return; end;
  if sender==100 then
    if not help then
      CreateWindow(200,200,"1",100,"messageinfo",30,50)
      --playeffect("01");
      help=true;
    end;
  return;end;
    if pause_state then
    if sender==1 or sender==6 or sender==14 then
      pause_state=false;
      --set_visible_object(11111,false);
    end;  
  return;end;
  if sender==1 then
    if id==6000 then
       helpbtn=true;
    return;end
    if id==5000 and skipping then
      if not flags.minicomplete48 then
        finish_mini_game(true);
      return;end;  
    return;end;
    if id==645 then
    playeffect("17");
      finish_mini_game(false);
      clear_satelite_leaf("kabinet");
      DeleteResource("m48_close"       );
      DeleteResource("m48_fon"         );
      DeleteResource("m48_lenta"         );
      DeleteResource("m48_lenta2"         );

    return;end;

      if not flags.minicomplete48 then      
        if id>=646 and id<=654 then
          add_to_mouse(id);
          mousepresed=true;
          objectid=id;
        return 0; end  
      return;end;
  return; end;
  
  if sender == 6 then
    if helpbtn then
       CreateWindow(200,200,"1",100,"messageinfo",30,50)
       playeffect("11");  
      helpbtn=false;
    end;
    if mousepresed==true then
      playeffect("pclick");
      RemoveFromMouse(objectid);
      if MagicHandle(objectid)
        then
          finish_mini_game(true);
        end;
      mousepresed=false;
      CheckSumma()
      objectid=0;
    end;
    return;end;
end;
function MagicHandle(iId)
  local x,y;
  local k;
  
  x,y=GetCursorPos();
  for i=1,nCells,1 do
    if x>pt[2*i-1] and y>pt[2*i] and x<pt[2*i-1]+iCellWidth and y<pt[2*i]+iCellHeight
      then
        if Data[i]~=0
          then
            SetObjectPosition(iStartId+Data[i]-1,xReset[Data[i]],yReset[Data[i]]);
          end;
        k=FindNumberPlace(iId-iStartId+1);
        if k~=-1 then Data[k]=0 end;
        SetObjectPosition(iId,pt[2*i-1],pt[2*i]);
        Data[i]=iId-iStartId+1;
        return Check()
      end
  end;
  SetObjectPosition(iId,xReset[iId-iStartId+1],yReset[iId-iStartId+1]);
  k=FindNumberPlace(iId-iStartId+1);
  if k~=-1 then Data[k]=0 end;
  return false;
end;
function FindNumberPlace(iNumber)
  for i=1,nCells,1 do
    if iNumber==Data[i]
      then
        return i
      end
  end;
  return -1
end;
function Check()
  local CheckSum,s;
 CheckSumma()
  s=Data[1];
  for i=2,4,1 do
    s=s+Data[i]
  end;
  CheckSum=s;
  s=Data[4];
  for i=5,7,1 do
    s=s+Data[i]
  end;
  if s~=CheckSum then return false end;
  s=Data[1];
  for i=7,9,1 do
    s=s+Data[i]
  end;
-- y=0
-- if s>15 then
-- y=y+50
-- end;
  if s~=CheckSum then return false end;
  for i=1,9,1 do
    if Data[i]==0 then return false end
  end
  return true;
end;
function CheckSumma()
y1=0
 y2=0
 y3=0
  s1=Data[1]+Data[2]+Data[3]+Data[4]
  s2=Data[4]+Data[5]+Data[6]+Data[7]
  s3=Data[7]+Data[8]+Data[9]+Data[1]
  if s1>15 then
  y1=50
s1=s1-16
  end
  if s2>15 then
  y2=50
  s2=s2-16
  end 
  if s3>15 then
  y3=50
  s3=s3-16
  end
  set_pos_in_img(657,(s1)*50,y1,50,50);----справа
  set_pos_in_img(658,(s2)*50,y2,50,50);----снизу
  set_pos_in_img(656,(s3)*50,y3,50,50);
end;