CoolisTheName007

dusty

May 4th, 2013
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.33 KB | None | 0 0
  1. delay=5
  2. ap=peripheral.wrap'left'
  3. size=108
  4. function getItems()
  5.     local t={}
  6.     for i=1,size do
  7.         local s,n=ap.getItem(i-1)
  8.         if s then
  9.             t[s]=t[s] or {n=0,i={}}
  10.             t[s].i[i]=n
  11.             t[s].n=t[s].n+n
  12.         end
  13.     end
  14.     return t
  15. end
  16. function pullItem(t,n)
  17.     local t_t={}
  18.     local p=0
  19.     for i=table.maxn(t.i),(next(t.i) or math.huge),-1 do
  20.         if t.i[i] then
  21.             local taken=math.min(n-p,t.i[i])
  22.             t_t[i]=taken
  23.             t.i[i]=t.i[i]-taken
  24.             if t.i[i]==0 then t.i[i]=nil end
  25.             t.n=t.n-taken
  26.             p=p+taken
  27.             if p>=n then
  28.                 break
  29.             end
  30.         end
  31.     end
  32.    
  33.     for i,n in pairs(t_t) do
  34.         ap.suck(i-1,n)
  35.     end
  36. end
  37.  
  38. local slots={1,2,5,6}
  39. function work()
  40.     local worked=false
  41.     local t=getItems()
  42.     for s,tt in pairs(t) do
  43.         local n=tt.n
  44.         n=n-(n%4)
  45.         local r=n%4
  46.         local q=(n-r)/4
  47.         if q>0 then
  48.            
  49.             local left=q
  50.             repeat
  51.                 local n=math.min(64,left)
  52.                 for _,i in pairs(slots) do
  53.                     turtle.select(i)
  54.                     pullItem(tt,n)
  55.                 end
  56.                 if not turtle.craft(n) then clear(turtle.dropUp,{1,2,3,5,6,7,9,10,11})
  57.                 else worked=true end
  58.                 turtle.dropUp()
  59.                 left=left-64
  60.             until left<0
  61.         end
  62.     end
  63.     return worked
  64. end
  65. function clear(drop,_slots)
  66.     _slots=_slots or slots
  67.     for _,i in pairs(_slots) do
  68.         turtle.select(i)
  69.         drop()
  70.     end
  71. end
  72.  
  73. clear(turtle.dropUp)
  74. repeat
  75.     while work() do
  76.     end
  77.     sleep(delay)
  78. until false
  79.  
  80.  
  81. ap.getItem()
Advertisement
Add Comment
Please, Sign In to add comment