eleeet

автокрафт панели

Sep 8th, 2020
886
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --автокрафт
  2. local robot = component.proxy(component.list("robot")())
  3. local inv = component.proxy(component.list("inventory_controller")())
  4. local crafting = component.proxy(component.list("crafting")())
  5. local db = component.proxy(component.list("database")())
  6.  
  7. local slots = {
  8.     [1] = {database = 1, inventory = 1},
  9.     [2] = {database = 2, inventory = 2},
  10.     [3] = {database = 3, inventory = 3},
  11.     [4] = {database = 4, inventory = 5},
  12.     [5] = {database = 5, inventory = 6},
  13.     [6] = {database = 6, inventory = 7},
  14.     [7] = {database = 7, inventory = 9},
  15.     [8] = {database = 8, inventory = 10},
  16.     [9] = {database = 9, inventory = 11},
  17. }
  18.  
  19. local frontsize = inv.getInventorySize(3)
  20. local buffer = tonumber(robot.name()) or 64
  21. local real = {}
  22.  
  23. function getSlots() -- put database info into variable
  24.     for i = 1,9 do
  25.         local t = db.get(slots[i].database)
  26.         if t then
  27.             real[slots[i].inventory] = t.label
  28.         end
  29.     end
  30. end
  31.  
  32. function suckSlot(slot) -- sucks appropriate item into slot, returns true if sucked all
  33.     local toSuck = buffer - robot.count(slot)
  34.     if toSuck > 0 then
  35.         robot.select(slot)
  36.         for i = 1,frontsize do
  37.             local ttt = inv.getStackInSlot(3,i)
  38.                 if ttt then
  39.                 if ttt.label == real[slot] then
  40.                     if ttt.size >= toSuck then
  41.                         inv.suckFromSlot(3,i,toSuck)
  42.                         toSuck = 0
  43.                         return true
  44.                     else
  45.                         inv.suckFromSlot(3,i,ttt.size)
  46.                         toSuck = toSuck - ttt.size
  47.                     end
  48.                 end
  49.             end
  50.         end
  51.     else
  52.         return true
  53.     end
  54. end
  55.  
  56. function keysLeft(table)
  57.     for k,_ in pairs(table) do
  58.         computer.pullSignal(2)
  59.         return true
  60.     end
  61.     return false
  62. end
  63.  
  64. function suck() -- makes sure everything is in place to start crafting
  65.     local left = {}
  66.     for k,_ in pairs(real) do
  67.         left[k] = 1
  68.     end
  69.     repeat
  70.         for k,_ in pairs(left) do -- sucks only into slots that arent full yet
  71.             if suckSlot(k) == true then
  72.                 left[k] = nil
  73.             end
  74.         end
  75.     until keysLeft(left) == false
  76. end
  77.  
  78.  
  79. function craft()
  80.     robot.select(13)
  81.     crafting.craft(buffer)
  82.     for i = 13,16 do
  83.         robot.select(i)
  84.         robot.drop(3,buffer)
  85.     end
  86. end
  87.  
  88. --
  89. getSlots()
  90. while true do
  91.     suck()
  92.     craft()
  93. end
RAW Paste Data