function mini_create()
  init_mini(true)
  create_mini(8128);
end
if flags.gems_17_14_in then
  miniCreate()
end;
-- local dx,dy = 0,0
local oldx0,oldy0 = 0,0
local gem = nil
local a =95
local e = 10

function gameinput (sender,x,y)
  MiniInterfaceHandler(sender, x, y);
  sendersProcessing(sender,x,y)
  if wrongItemWarning(sender,x,y,"gems_17_14") then
    return
  end
  if sender==1 then
    if flags.gems_17_14_in then
      if x >= 1 and x <= 16 then
        oldx0,oldy0 = GetCursorPos()
        -- dx = oldx0 - gems[x].x
        -- dy = oldy0 - gems[x].y
        --add_to_mouse(x)
        gem = x
        MouseMove = moveGem
      end
    else
      if x==100 then
        if obj.gems_17_14 and y==obj.gems_17_14.id then
          removeFromMouse()
          UnStoreObj(obj.gems_17_14.id)    
          speciale_action(2,1)
          speciale_action(2,5)
          speciale_action(2,9)
          speciale_action(2,13)
          setCursor(100,3)
          speciale_action(2,100)
          
          flags.gems_17_14_in=true
          playeffect("block")
          miniCreate()
        else
          CreatePlashechka("20013")
        end;        
      end;
    end;
  end
  if sender == 8129 then
    oldx0,oldy0 = 0,0
    MouseMove = moveEye
    gem = nil
  end
  if sender == 10 then
  playeffect("mini_complete")
    MouseMove = moveEye
    StartTimer(timerID,2,"CloseSatelite")
    neighbors.KoridorBratyevKrystal.lock = true
    flags.OlderOpen=true
    local coords=
    {
      -- "blue"
      [1]  = {["x"]=281; ["y"]=72 };
      [2]  = {["x"]=376; ["y"]=72 };
      [3]  = {["x"]=281; ["y"]=167};
      [4]  = {["x"]=376; ["y"]=167};
      -- "white"
      [5]  = {["x"]=566; ["y"]=72 };
      [6]  = {["x"]=661; ["y"]=72 };
      [7]  = {["x"]=566; ["y"]=167};
      [8]  = {["x"]=661; ["y"]=167};
      -- "yellow"
      [9]  = {["x"]=281; ["y"]=357};
      [10] = {["x"]=376; ["y"]=357};
      [11] = {["x"]=281; ["y"]=452};
      [12] = {["x"]=376; ["y"]=452};
      -- "red"
      [13] = {["x"]=566; ["y"]=357};
      [14] = {["x"]=661; ["y"]=357};
      [15] = {["x"]=566; ["y"]=452};
      [16] = {["x"]=661; ["y"]=452};
    }
    for i=1,16 do
      SetObjectPosition(i,coords[i].x,coords[i].y)
      setcolor(i,1,1,1,1)
    end
  end
end

function moveGem()

  moveEye()
  local x0,y0 = GetCursorPos()
  -- if (x0<gems[gem].x)or(x0>gems[gem].x+a)or(y0<gems[gem].y)or(y0>gems[gem].y+a) then
    -- return
  -- end
  local h = x0-oldx0
  local v = y0-oldy0
  local d = math.abs(h)>math.abs(v) -- true - двигаем по горизонтали; false - по вертикали
  local x1 = gems[gem].x
  local y1 = gems[gem].y
  if d then
    x1 = x1+h
  else
    y1 = y1+v
  end
  
  for k=1,21 do
    if k ~= gem then
      if  ((x1+a)>gems[k].x)and(x1<(gems[k].x+a))and((y1+a)>gems[k].y)and(y1<(gems[k].y+a))
      or(x1<281)or((x1+a)>756)or(y1<72)or((y1+a)>547) then
          --  oldx0,oldy0 = 0,0
          oldx0,oldy0 = x0,y0
          -- MouseMove = moveEye
          -- gem = nil
        return
      end
    end
  end
  
  local g = true
  for i=1,5 do
    if d then
      if (x1 >= xx[i]-e)and(x1 <= xx[i]+e) then
        x1 = xx[i]
        g = false
        break
      end
    else
      if (y1 >= yy[i]-e)and(y1 <= yy[i]+e) then
        y1 = yy[i]
        g = false
        break
      end
    end
  end
  if gems[gem].x~=x1 or gems[gem].y~=y1 then
  playeffect("move_block")
  end;
  gems[gem].x = x1
  gems[gem].y = y1
  
  
  SetObjectPosition(gem,gems[gem].x,gems[gem].y)
  if g then
    oldx0,oldy0 = x0,y0
  end
  checkMini()
end

function checkMini()
  local i = gem
  if i>= 1 and i <=4 then
    if ((gems[i].x == xx[1])or(gems[i].x == xx[2]))and((gems[i].y == yy[1])or(gems[i].y == yy[2])) then
      gems[i]["placed"] = true
      setcolor(i,1,1,1,1)
    else
      gems[i]["placed"] = false
      setcolor(i,0.5,0.5,0.5,1)
    end
  end
  if i>= 5 and i <=8 then
    if ((gems[i].x == xx[3])or(gems[i].x == xx[4]))and((gems[i].y == yy[1])or(gems[i].y == yy[2])) then
      gems[i]["placed"] = true
      setcolor(i,1,1,1,1)
    else
      gems[i]["placed"] = false
      setcolor(i,0.5,0.5,0.5,1)
    end
  end
  if i>= 9 and i <=12 then
    if ((gems[i].x == xx[1])or(gems[i].x == xx[2]))and((gems[i].y == yy[3])or(gems[i].y == yy[4])) then
      gems[i]["placed"] = true
      setcolor(i,1,1,1,1)
    else
      gems[i]["placed"] = false
      setcolor(i,0.5,0.5,0.5,1)
    end
  end
  if i>= 13 and i <=16 then
    if ((gems[i].x == xx[3])or(gems[i].x == xx[4]))and((gems[i].y == yy[3])or(gems[i].y == yy[4])) then
      gems[i]["placed"] = true
      setcolor(i,1,1,1,1)
    else
      gems[i]["placed"] = false
      setcolor(i,0.5,0.5,0.5,1)
    end
  end
  for i=1,16 do
    if not gems[i].placed then
      return
    end
  end
  finish_mini_game(true)
end















