local tabFreak={}
tabFreak[1]={36,36}
tabFreak[2]={36,36}
tabFreak[3]={80,50}
tabFreak[4]={36,36}
tabFreak[5]={36,36}
tabFreak[6]={80,50}
tabFreak[7]={80,50}
tabFreak[8]={80,50}
function mini_create()
  init_mini(true)
  create_mini(8128);
end
  if flags.PuzzlesIn then
miniCreate()
end
local nowAttached = nil
local nowActive = nil
local state = {false,false,false,false,false,false,false,false}
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 sender==1 then
    if not flags.PuzzlesIn then
      if obj_id==100 then
      
        if obj.parts_puz~=nil and attachedID==obj.parts_puz.id then
          removeFromMouse()
          UnStoreObj(obj.parts_puz.id)    
          speciale_action(2,11,true)
          speciale_action(2,12,true)
          setCursor(100,3)
          speciale_action(2,100)
          playeffect("block")
          flags.PuzzlesIn=true
          miniCreate()
        return;end;
        if (obj.parts_puz==nil or (obj.parts_puz~=nil and attachedID~=obj.parts_puz.id) )and attachedID~=-1 then
          CreatePlashechka("10000")
        return;end;  
        if   attachedID==-1 then
          CreatePlashechka("20013")
        return;end;    
            return;end;        
    return;end;
    if not nowAttached then
      if obj_id>=11 and obj_id<=18 then
        nowAttached = obj_id+10
        local x,y = GetCursorPos()
        local a,b = GetObjectSize(nowAttached)
        SetObjectPosition(nowAttached,x-a/2,y-b/2)
        
        set_visible_object(nowAttached,true)
        add_to_mouse(nowAttached)
        set_visible_object(obj_id,false)
      end
    end
  end
  if sender == 8129 then
    if nowAttached then
      removeFromMouse(nowAttached)
      Write("Attached object "..nowAttached.." removed from mouse\n",true)
      local _iIndex=CursorInEllipse(GetCursorPos())
      local _fFlag=false

      if (nowActive and nowActive+20 == nowAttached) or (nowAttached-_iIndex==20) then
        if not nowActive then
          nowActive=_iIndex
          _fFlag=true
        end
        SetAlfaValue(nowActive,1)
        state[nowActive] = true
        playeffect("block")
        checkMini()
        if _fFlag then
          _fFlag=false
          nowActivate=false
        end
      else
        set_visible_object(nowAttached-10,true)
      end
      set_visible_object(nowAttached,false)
      nowAttached = nil
    end
  end
  if sender == 10 then
    neighbors.KoridorBratyevLock.lock = true
    flags.MiddleOpen=true
    playeffect("mini_complete")
    StartTimer(timerID,2,"CloseSatelite")
    for i=11,18,1 do
    set_visible_object(i,false)
    end;
    for i=1,8,1 do
    set_visible_object(i,true)
    SetAlfaValue(i,1)
    end;    
    -- for i=21,28,1 do
    -- set_visible_object(i,true)
    -- end;    
  end

  if sender == 30 then
    if flags.MiddleOpen then
      setCursor(obj.door_Mid_15_0.id,7)
      speciale_action(2,12)
      StopEmiter("Standart_15_1");
    end;
  end
  if sender == 35 then
    nowActive = obj_id
  end
  if sender == 39 then
    if obj_id == nowActive then
      nowActive = nil
    end
  end
end

function checkMini()
  for i=1,8 do
    if not state[i] then
      return
    end
  end
  finish_mini_game(true)
end

function PtInEllipse(x,y,iIndex)
  local _a,_b=tabFreak[iIndex][1],tabFreak[iIndex][2];

  return x*x/_a/_a+y*y/_b/_b<1
end
function CursorInEllipse(x,y)
  local j=0;
  local xPos,yPos,w,h=-1,-1,-1,-1;

  for j=1,8,1 do
    xPos,yPos=GetObjectPosition(j);
    w,h=GetObjectSize(j);
    if PtInEllipse(x-xPos-w/2,y-yPos-h/2,j) then return j end
  end
  return -1
end