local tab = 
{
  [10]={x=453,y=390;id=10};
  [11]={x=549,y=412;id=11};
  [12]={x=573,y=304;id=12};
  [13]={x=483,y=282;id=13};
  [14]={x=512,y=341;id=14};
  [15]={x=618,y=295;id=15};
  [16]={x=600,y=431;id=16};
}

local a = 20

function mini_create()
  init_mini(true)
  create_mini(8128);
  for i=1,10 do
    local j = GetRandom(10,16)
    local k = GetRandom(10,16)
    tab[j].id,tab[k].id = tab[k].id,tab[j].id
  end
  for i=10,16 do
    SetObjectPosition(tab[i].id,tab[i].x,tab[i].y)
  end
end

-- if flags.broshka then
  -- speciale_action(2,18)
-- end
if flags.gems then
  setCursor(17,3)
  for i = 10,16 do
    speciale_action(2,i)
  end
  if flags.broshkuProshel then
    speciale_action(2,obj.pyatnashki_6_10.id)
    speciale_action(2,1)
  else
    if flags.videlBryuliki then
      miniCreate()
    end
  end
end

local nowAttached = nil
local last = nil

function gameinput (sender,x,y)
  MiniInterfaceHandler(sender, x);
  sendersProcessing(sender, x, y);
  if wrongItemWarning(sender, x, y,"brosh_3_13","gems_7_13",10,11,12,13,14,15,16) then
    return
  end
  if sender ~= 100 then
    --out("gameinput("..tostring(sender)..", "..tostring(x)..", "..tostring(y)..")\n")
  end
  if sender==1 then
    if x == 17 then
      if obj.brosh_3_13 and y == obj.brosh_3_13.id then
        removeFromMouse()
        UnStoreObj(obj.brosh_3_13.id)
        speciale_action(2,18,true,1)
        playeffect("broosh")
        flags.broshka = true
      elseif obj.gems_7_13 and y == obj.gems_7_13.id then
        if flags.broshka then
          removeFromMouse()
          UnStoreObj(obj.gems_7_13.id)
          playeffect("brill")
          for i = 10,16 do
            speciale_action(2,i,true,1)
          end
          flags.gems = true
          setCursor(17,3)
          if flags.videlBryuliki then
            miniCreate()
          else
            CreatePlashechka("636")
          end
        else
          if not flags.broshka then
            CreatePlashechka("612")
          end
        end
      else
        if not flags.broshka then
          CreatePlashechka("612")
        else
          if flags.gems then
            if not flags.videlBryuliki then
              CreatePlashechka("616")
            end
          else
            CreatePlashechka("664")
          end
        end
      end
    end
    if x == obj.pyatnashki_6_10.id then
      StoreObj(obj.pyatnashki_6_10.id)
      neighbors.TumbaSkameyka.lock = true
    end
  end
    
  if sender == 8128 then
    if flags.gems then
      for i=10,16 do
        if x >= tab[i].x and x <= tab[i].x + a and y >= tab[i].y and y <= tab[i].y + a then
          if nowAttached then
            removeFromMouse()
            SetObjectPosition(nowAttached,tab[i].x,tab[i].y)
            playeffect("brill")
            if tab[i].id then
              addToMouse(tab[i].id)
              tab[i].id,nowAttached = nowAttached,tab[i].id
            else
              tab[i].id = nowAttached
              nowAttached = nil
            end
            checkMini()
          else
            addToMouse(tab[i].id)
            nowAttached = tab[i].id
            last = i
            tab[i].id = nil
          end
          break
        end
      end
    end
  end
  
  if sender == 8 then
    if nowAttached then
      removeFromMouse()
      SetObjectPosition(nowAttached,tab[last].x,tab[last].y)
      tab[last].id = nowAttached
      nowAttached = nil
      checkMini()
    end
  end
  
  if sender == 10 then
    removeFromMouse()
    if last then
      SetObjectPosition(nowAttached,tab[last].x,tab[last].y)
    end
    flags.broshkuProshel = true
    Block_Show_Cur(true)
    for i=3,9 do
      speciale_action(2,i,true,1)
    end
    -- for i = 10,16 do
      -- speciale_action(2,i)
    -- end
    StartTimer(timerID,1.1,"frame1")
  end
  if sender == 26 then
  --  CloseSatelite()
  end
end

function checkMini()
  --out("state(")
  for i=10,16 do
    out(tostring(tab[i].id)..",")
    if tab[i].id ~= i then
      return
    end
  end
  --out(")\n")
  finish_mini_game(true)
end

function frame1()
  speciale_action(2,obj.pyatnashki_6_10.id,true,1)
  speciale_action(2,1,true,1)
  playeffect("mini_complete")
  Block_Show_Cur(false)
end