Lion4ever

turtle storage with misc peripherals

Sep 6th, 2013
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.90 KB | None | 0 0
  1. local slotsPerChest=27
  2. local chestsPerRow=5
  3. local turtleSide=
  4. local InputSide=
  5. local OutputSide=
  6. local m=peripheral.wrap("right")
  7. local function load(name)
  8.     if fs.exists(name) then
  9.         local f= fs.open(name,"r")
  10.         local result=textutils.unserialize(f.readAll())
  11.         f.close()
  12.         return result
  13.     else
  14.         return {}
  15.     end
  16. end
  17. storage=load("storage")
  18. names=load("names")
  19. local x,y=1,1
  20. local function go(tx,ty)
  21.     for q=y+1,ty do
  22.         repeat until turtle.up()
  23.     end
  24.     for q=ty+1,y do
  25.         repeat until turtle.down()
  26.     end
  27.     if x<tx then
  28.         turtle.turnRight()
  29.         for q=x+1,tx do
  30.             repeat until turtle.forward()
  31.         end
  32.         turtle.turnLeft()
  33.     end
  34.     if x>tx then
  35.         turtle.turnLeft()
  36.         for q=tx+1,x do
  37.             repeat until turtle.forward()
  38.         end
  39.         turtle.turnRight()
  40.     end
  41.     x,y=tx,ty
  42. end
  43. local function save()
  44. local f=fs.open("storage","w")
  45. f.write(textutils.serialize(storage))
  46. f.close()
  47. local f=fs.open("names","w")
  48. f.write(textutils.serialize(names))
  49. f.close()
  50. end
  51. function outputAll()
  52.     go(1,1)
  53.     local sorter = peripheral.wrap("left")
  54.     for i,j in pairs(sorter.list(turtleSide)) do
  55.         sorter.extract(turtleSide,i,OutputSide,j)
  56.     end
  57. end
  58. function getItem(id,amount)
  59.     if countItem(id)<amount then
  60.         return "Not enough items in storage"
  61.     end
  62.     turtle.select(1)
  63.     for j,k in ipairs(storage) do
  64.         for o=1,chestsPerRow do
  65.             local w=(j%2==0 and chestsPerRow+1-o or o)
  66.             for p=0,slotsPerChest-1 do
  67.                 if k[w][p] and k[w][p][1]==id then
  68.                     go(w,j)
  69.                     if k[w][p][2]<=amount then
  70.                         m.suck(p,k[w][p][2])
  71.                         amount=amount-k[w][p][2]
  72.                         k[w][p] = nil
  73.                     else
  74.                         m.suck(p,amount)
  75.                         k[w][p][2]=k[w][p][2]-amount
  76.                         amount=0
  77.                     end
  78.                     if amount<=0 then
  79.                         outputAll()
  80.                         return "Success"
  81.                     end
  82.                     if turtle.getItemCount(16)>0 then
  83.                         outputAll()
  84.                     end
  85.                 end
  86.             end
  87.         end
  88.     end
  89. end
  90. function countItem(id)
  91.     local result=0
  92.     for j,k in ipairs(storage) do
  93.         for o=1,chestsPerRow do
  94.             for p=0,slotsPerChest-1 do
  95.                 if k[o][p] then
  96.                     if k[o][p][1]==id then
  97.                         result=result+k[o][p][2]
  98.                     end
  99.                 end
  100.             end
  101.         end
  102.     end
  103.     return result
  104. end
  105. print("name <name> to set a name for the item in slot 1\nget <name or id> <amount> to get items\ndo not change the chest manually!")
  106. local running=true
  107. while running do
  108.     local emptySlots=0
  109.     for i,j in pairs(storage) do
  110.         for k,l in pairs(j) do
  111.             for o=0,slotsPerChest-1 do
  112.                 if not l[o] then
  113.                     emptySlots=emptySlots+1
  114.                 end
  115.             end
  116.         end
  117.     end
  118.     if emptySlots<16 then
  119.         turtle.select(1)
  120.         local slotInv =0
  121.         repeat
  122.             m.suckDown(slotInv,chestsPerRow-turtle.getItemCount(1))
  123.             slotInv=(slotInv+1)%slotsPerChest
  124.             sleep(0)
  125.         until turtle.getItemCount(1)==chestsPerRow
  126.         for i=1,chestsPerRow do
  127.             go(i,#storage+1)
  128.             turtle.place()
  129.         end
  130.         storage[#storage+1]={}
  131.         for i=1,chestsPerRow do
  132.             table.insert(storage[#storage],{})
  133.         end
  134.         save()
  135.         go(1,1)
  136.     end
  137.     local inputItems=peripheral.call("left","list",InputSide)
  138.     local countItems=0
  139.     local sortedInput={}
  140.     local reverse={}
  141.     for i,j in pairs(inputItems) do
  142.         for k=j,1,-64 do
  143.             countItems=countItems+1
  144.             table.insert(sortedInput,i)
  145.             reverse[i]=countItems
  146.         end
  147.     end
  148.     if countItems>=16 then
  149.         local inTurtle={}
  150.         local itemsToBeSorted={}
  151.         local sorter=peripheral.wrap("left")
  152.         for i=1,16 do
  153.             sorter.extract(InputSide,sortedInput[i],turtleSide,1)
  154.             sorter.extract(InputSide,sortedInput[i],turtleSide,math.min(inputItems[sortedInput[i]]-1,turtle.getItemSpace(i)))
  155.             if turtle.getItemCount(i)~=inputItems[sortedInput[i]] and turtle.getItemSpace(i)~=0 then
  156.                 error("Items did not reach turtle")
  157.             end
  158.             itemsToBeSorted[i]=turtle.getItemCount(i)
  159.         end
  160.         for j,k in ipairs(storage) do
  161.             for o=1,chestsPerRow do
  162.                 for p=0,slotsPerChest-1 do
  163.                     if k[o][p] then
  164.                         for l=1,16 do
  165.                             if k[o][p][1]==sortedInput[l] then
  166.                                 itemsToBeSorted[l]=itemsToBeSorted[l]-(turtle.getItemSpace(l)-k[o][p][2])
  167.                                 break
  168.                             end
  169.                         end
  170.                     end
  171.                 end
  172.             end
  173.         end
  174.         for j,k in ipairs(storage) do
  175.             for o=1,chestsPerRow do
  176.                 local w=(j%2==0 and chestsPerRow+1-o or o)
  177.                 for p=0,slotsPerChest-1 do
  178.                     if k[w][p] then
  179.                         for l=1,16 do
  180.                             if k[w][p][1]==sortedInput[l] then
  181.                                 go(w,j)
  182.                                 local oldCount=turtle.getItemCount(l)
  183.                                 turtle.select(l)
  184.                                 m.drop(p,(turtle.getItemSpace(l)-k[w][p][2]))
  185.                                 k[w][p][2]=k[w][p][2]+(oldCount-turtle.getItemCount(l))
  186.                                 break
  187.                             end
  188.                         end
  189.                     else
  190.                         for l=1,16 do
  191.                             if itemsToBeSorted[l]>0 then
  192.                                 go(w,j)
  193.                                 k[w][p]={}
  194.                                 k[w][p][1]=sortedInput[l]
  195.                                 k[w][p][2]=turtle.getItemCount(l)
  196.                                 turtle.select(l)
  197.                                 m.drop(p,turtle.getItemCount(l))
  198.                                 itemsToBeSorted[l]=0
  199.                                 break
  200.                             end
  201.                         end
  202.                     end
  203.                 end
  204.             end
  205.         end
  206.         go(1,1)
  207.         turtle.select(1)
  208.         save()
  209.     end
  210.     os.startTimer(30)
  211.     term.setCursorBlink(true)
  212.     local event={os.pullEvent()}
  213.     if event[1] and event[1]=="char" then
  214.         os.queueEvent(unpack(event))
  215.         print("> ")
  216.         local command=read()
  217.         if command:sub(1,5)=="name " then
  218.             local i=0
  219.             for j=2,16 do
  220.                 i=i+turtle.getItemCount(j)
  221.             end
  222.             if turtle.getItemCount(1)==0 or i>0 then
  223.                 print("The only Item in the turtle need to be in Slot 1")
  224.             else
  225.                 local sorter=peripheral.wrap("left")
  226.                 for i,j in pairs(sorter.list(turtleSide)) do
  227.                     names[command:sub(6)]=i
  228.                     print("Name set to: "..command:sub(6)or "")
  229.                 end
  230.             end
  231.         elseif command:sub(1,4) =="get " then
  232.             local space=command:find(" ",5)
  233.             local name=command:sub(5,space-1)
  234.             print(getItem(names[name] or tonumber(name),tonumber(command:sub(space+1))))
  235.         elseif command:sub(1,6) =="count " then
  236.             print("There are "..tostring(countItem(names[command:sub(7)] or tonumber(command:sub(7)))).." "..(names[command:sub(7)] or "Items"))
  237.         elseif command:sub(1,4) =="exit" then
  238.             running=false
  239.         else
  240.             print("Unknown command")
  241.         end
  242.     end
  243.     term.setCursorBlink(false)
  244. end
Advertisement
Add Comment
Please, Sign In to add comment