local Data={0,0,0,0,0,0,4};
local pt={235,145,336,145,437,154,222,422,329,422,437,422,336,282};
local iCellWidth=54;
local iCellHeight=52;
local iStartId=652;
local nCells=7;
function mini_create ()
  show_help_wnd=true;
  init_mini(1);
  create_mini(8128);
    SetObjectPosition(655,pt[2*7-1],pt[2*7]);
end;
function gameinput (sender,id,attachedID)
  if sender==30 then
  setcurentleaf("barton_zal",1,1);
    flags.nohints=false;
  if flags.minicomplete54 then
    StopEmiter("pu4ok1"); 
    playeffect("29");
    Set_Cursor(29,3);
    collaps_obj(109,0,0,10,0);
    playeffect("20");

  return; end;  
  return; end;
  
  if sender==10 then
    flags.minicomplete54=true;
    CreateWindow(200,250,"109",1,"m&i",63,107,"vizitka",246,13);
  return; end;

  if sender==37 then
     skipping=true;
  return;end;  
  if sender==26 then
    if id==100 then
    set_visible_object(5001,true)
    end;
    if id==1 then
      clear_satelite_leaf("barton_zal");
    return;end;
  return; end;  
  if sender==100 then
    if not help then
      CreateWindow(200,200,"4",100,"messageinfo",30,50)
      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.minicomplete54 then
        finish_mini_game(true);
      return;end;  
    return;end;
  if id==650 then
    playeffect("17");  

    finish_mini_game(false);
    clear_satelite_leaf("barton_zal");
    DeleteResource("m54_close"         );
    DeleteResource("m54_fon"           );
    DeleteResource("m54_lenta"           );
    DeleteResource("vizitka"           );
    DeleteResource("m53_lenta"           );


  return;end;
    if id>=652 and id<=658 and id~=655 then
      add_to_mouse(id);
      mousepresed=true;
      objectid=id;
    return 0; end

  return; end;
  
  if sender == 6
    then
      if helpbtn then
       CreateWindow(200,200,"4",100,"messageinfo",30,50)
         playeffect("11");  
      helpbtn=false;
    end;
      if mousepresed
        then
        playeffect("pclick");  
          RemoveFromMouse(objectid);
          if MagicHandle(objectid)
            then
              finish_mini_game(true);
            end;
          mousepresed=false;
          objectid=0;
        end;
      return
    end
end;
function mini_delete ()

end;
function MagicHandle(iId)
  local x,y;
  local k;
  
  x,y=GetCursorPos();
  for i=1,nCells,1 do
    if i~=7 and 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;
  Summa4=Data[3]+Data[7]+Data[4]
  Summa3=Data[2]+Data[7]+Data[5]
  Summa2=Data[1]+Data[7]+Data[6]
  Summa1=Data[4]+Data[5]+Data[6]
  CheckSum=Data[1]+Data[2]+Data[3];
  set_pos_in_img(665,(Summa4)*37,0,37,36);
  set_pos_in_img(666,(Summa3)*37,0,37,36);
  set_pos_in_img(667,(Summa2)*37,0,37,36);
  set_pos_in_img(668,(Summa1)*37,0,37,36);
  set_pos_in_img(669,(CheckSum)*37,0,37,36);
  
    -- SetWindowText(Summa4.." "..Summa3.."  "..Summa2.."   "..Summa1.."   "..CheckSum)
  if Data[4]+Data[5]+Data[6]~=CheckSum then return false end;
  if Data[1]+Data[7]+Data[6]~=CheckSum then return false end;
  if Data[2]+Data[7]+Data[5]~=CheckSum then return false end;
  if Data[3]+Data[7]+Data[4]~=CheckSum then return false end;
  
  return true;
end;