local circles =
{
  {x0,y0,x1,y1,ugol=0,id=21,nodes={6,10,11,8,5,4}};
  {x0,y0,x1,y1,ugol=0,id=22,nodes={9,12,13,11,7,6}};
  {x0,y0,x1,y1,ugol=0,id=23,nodes={3,6,7,5,2,1}};
}

local IDs = {9,2,1,3,7,5,6,4,10,13,8,12,11}

function shuffle()
  for i=1,10 do
    local n1 = GetRandom(1,13)
    local n2 = GetRandom(1,13)
    local x1,y1 = GetObjectPosition(IDs[n1])
    local x2,y2 = GetObjectPosition(IDs[n2])
    SetObjectPosition(IDs[n1],x2,y2)
    SetObjectPosition(IDs[n2],x1,y1)
    IDs[n1],IDs[n2] = IDs[n2],IDs[n1]
  end
end

function mini_create()
  init_mini(true)
  create_mini(8128);
end

shuffle()

local a,b = GetObjectSize(21)
local a1,b1 = GetObjectSize(5)
local delta = 6

local x,y = GetObjectPosition(21)
circles[1].x0 = x + a/2 - a1/2
circles[1].y0 = y + b/2 - b1/2
circles[1].x1 = x + a1 + delta
circles[1].y1 = y + b/2 + b1

local x,y = GetObjectPosition(22)
circles[2].x0 = x + a/2 - a1/2
circles[2].y0 = y + b/2 - b1/2
circles[2].x1 = x + a1 + delta
circles[2].y1 = y + b/2 + b1

local x,y = GetObjectPosition(23)
circles[3].x0 = x + a/2 - a1/2
circles[3].y0 = y + b/2 - b1/2
circles[3].x1 = x + a1 + delta
circles[3].y1 = y + b/2 + b1

local testNodes = {5,6,7,11}
local neededIDs = {5,6,7,8}

local pressed = nil
local t = 0.5

if flags.krugiActive then
  set_visible_object(50,false)
  if not flags.proshelKrugi then
    miniCreate()
  end
else
  setcolor(21,1,1,1,0.01)
  for i=1,6 do
    setcolor(IDs[circles[1].nodes[i]],1,1,1,0.01)
  end
end
if not flags.proshelKrugi then
  speciale_action(2,101)
  speciale_action(2,obj.almaz_9_14.id)
end

function gameinput (sender,obj_id,attachedID)
  -- Write("sender="..sender.." ; obj_id="..obj_id.."\n");
  MiniInterfaceHandler(sender, obj_id,attachedID);
  sendersProcessing(sender,obj_id,attachedID)
  if wrongItemWarning(sender, obj_id, attachedID,"krug_s_otmetinami_11_1") then
    return
  end
  if sender==30 and neighbors.BedroomKrugi.lock then
    CreatePlashechka("481")
  end;
  if sender == 1 then
    if flags.krugiActive then
      if obj_id >= 31 and obj_id <= 36 then
        pressed = obj_id
        SetAlfaValue(pressed,0.01)
      end
    end
    if obj_id == 50 then
      if obj.krug_s_otmetinami_11_1 and attachedID == obj.krug_s_otmetinami_11_1.id then
        removeFromMouse()
        UnStoreObj(obj.krug_s_otmetinami_11_1.id)
        speciale_action(2,50,true,0.1)
        Block_Show_Cur(true)
        playeffect("krug")
        SetAlfa(21,0.01,1,1,"unBlock_Show_Cur")
        for i=1,6 do
          SetAlfa(IDs[circles[1].nodes[i]],0.01,1,1)
        end
        flags.krugiActive = true
        miniCreate()
      else
        CreatePlashechka("612")
      end
    end
    if obj_id == obj.almaz_9_14.id then
      StoreObj(obj.almaz_9_14.id)
      neighbors.BedroomKrugi.lock = true
    end
  end
  if sender == 8129 then
    if pressed then
      SetAlfaValue(pressed,1)
      if pressed == 31 then
        rotate(1,1,t)
      elseif pressed == 32 then
        rotate(1,-1,t)
      elseif pressed == 33 then
        rotate(2,1,t)
      elseif pressed == 34 then
        rotate(2,-1,t)        
      elseif pressed == 35 then
        rotate(3,1,t)        
      elseif pressed == 36 then
        rotate(3,-1,t)
      end
      pressed = nil
    end
  end
  if sender == 10 then
    flags.proshelKrugi = true
    Block_Show_Cur(true)
    speciale_action(2,101,true,2)
    speciale_action(2,obj.almaz_9_14.id,true,2)
    playeffect("mini_complete")
    StartTimer(timerID,2,"unBlock_Show_Cur")
    -- neighbors.BedroomKrugi.lock = true
    -- CreateSatelite("BedroomTumba")
  end
  if sender == 26 then
  --  CloseSatelite()
  end
  if flags.krugiActive then
  if sender == 35 then
    if obj_id >= 31 and obj_id <= 36 then
  --    setcolor(obj_id,1,1,1,1)
      set_visible_object(obj_id+10,true)
    end
  end
  if sender == 39 then
    if obj_id >= 31 and obj_id <= 36 then
    --  setcolor(obj_id,0.7,0.7,0.7,1)
    set_visible_object(obj_id+10,false)
    end
  end
  end;
end

function rotate(n,k,t)
  Block_Show_Cur(true)
  playeffect("krug_rot")
  circle = circles[n]
  StartTimer(timerID,t+0.1,"frame1")
  local x0 = circle.x0
  local y0 = circle.y0
  local x1 = circle.x1
  local y1 = circle.y1
  for i=1,6 do
    from = -math.pi/2 + (i-1)*math.pi/3
    to = from + k*math.pi/3
    path = {"InstantiationOfCircleR",3,x0,y0,x1,y1,from,to,t,false}
    StartTravel(IDs[circle.nodes[i]],1,nil,nil,path)
  end
  RotateObject(circle.id,circle.ugol,circle.ugol+k*60,t)
  circle.ugol = (circle.ugol+k*60)%360
  if k>0 then
    IDs[circle.nodes[2]],IDs[circle.nodes[3]],IDs[circle.nodes[4]],IDs[circle.nodes[5]],IDs[circle.nodes[6]],IDs[circle.nodes[1]] =
    IDs[circle.nodes[1]],IDs[circle.nodes[2]],IDs[circle.nodes[3]],IDs[circle.nodes[4]],IDs[circle.nodes[5]],IDs[circle.nodes[6]]
  else
    IDs[circle.nodes[6]],IDs[circle.nodes[1]],IDs[circle.nodes[2]],IDs[circle.nodes[3]],IDs[circle.nodes[4]],IDs[circle.nodes[5]] =
    IDs[circle.nodes[1]],IDs[circle.nodes[2]],IDs[circle.nodes[3]],IDs[circle.nodes[4]],IDs[circle.nodes[5]],IDs[circle.nodes[6]]
  end
end

function frame1()
  Block_Show_Cur(false)
  local n = 0
  for i=1,4 do
    for j=1,4 do
      if IDs[testNodes[i]] == neededIDs[j] then
        n = n+1
        break
      end
    end
  end
  if n == 4 then
    finish_mini_game(true)
  end
end