KaoSDlanor

turtle factory

Jul 20th, 2013
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.70 KB | None | 0 0
  1. if KaoShell and KaoShell.getCo("Peripheral monitoring") then KaoShell.remCo("Peripheral monitoring") end --stop those irritating peripheral attachment notifications for all of the chests
  2. setfenv(nil,_G)
  3.  
  4. while not turtle.detectDown() do turtle.down() end
  5. local level=1
  6.  
  7. local chest=peripheral.wrap("front")
  8. local sDir
  9. for _,dir in ipairs({"north","east","south","west"}) do
  10.     if pcall(chest.push,"dir",0,0) then
  11.         sDir=dir
  12.         break
  13.     end
  14. end
  15.  
  16. tLevels={
  17.     [1]={}, --dump
  18.     [2]={"diamond pickaxe","computer","chest","turtle","mining turtle"}, --temp
  19.     [3]={"oak wood","spruce wood","birch wood","jungle wood","stick",}, --wood
  20.     [4]={"iron ingot",}, --iron
  21.     [5]={"redstone","redstone torch",}, --redstone
  22.     [6]={"diamond",}, --diamond
  23.     [7]={"stone",}, --stone
  24.     [8]={"glass","glass pane",}, --glass
  25.     [9]={"coal","charcoal",}, --fuel
  26.     [10]={{"sand",8},{"cobblestone",7},}, --forge
  27. }
  28.  
  29. for i=1,4 do --add planks to wood chest
  30.     tLevels[3][i+5]=tLevels[3][i].." planks"
  31. end
  32.  
  33. local tRecipes={
  34.     stick={
  35.         [1]={"oak wood planks","spruce wood planks","birch wood planks","jungle wood planks",},
  36.         [5]={"oak wood planks","spruce wood planks","birch wood planks","jungle wood planks",},
  37.         yield=4,
  38.     },
  39.     ["oak wood planks"]={"oak wood",yield=4,},
  40.     ["spruce wood planks"]={"spruce wood",yield=4,},
  41.     ["birch wood planks"]={"birch wood",yield=4,},
  42.     ["jungle wood planks"]={"jungle wood",yield=4,},
  43.     ["glass pane"]={
  44.         [1]="glass",[2]="glass",[3]="glass",
  45.         [5]="glass",[6]="glass",[7]="glass",
  46.     },
  47.     ["redstone torch"]={
  48.         [1]="redstone",
  49.         [5]="stick",
  50.     },
  51.     ["chest"]={
  52.         [1]={"oak wood planks","spruce wood planks","birch wood planks","jungle wood planks",},[2]={"oak wood planks","spruce wood planks","birch wood planks","jungle wood planks",},[3]={"oak wood planks","spruce wood planks","birch wood planks","jungle wood planks",},
  53.         [5]={"oak wood planks","spruce wood planks","birch wood planks","jungle wood planks",},[7]={"oak wood planks","spruce wood planks","birch wood planks","jungle wood planks",},
  54.         [9]={"oak wood planks","spruce wood planks","birch wood planks","jungle wood planks",},[10]={"oak wood planks","spruce wood planks","birch wood planks","jungle wood planks",},[11]={"oak wood planks","spruce wood planks","birch wood planks","jungle wood planks",},
  55.     },
  56.     ["computer"]={
  57.         [1]="stone",[2]="stone",[3]="stone",
  58.         [5]="stone",[6]="redstone",[7]="stone",
  59.         [9]="stone",[10]="glass pane",[11]="stone",
  60.     },
  61.     ["diamond pickaxe"]={
  62.         [1]="diamond",[2]="diamond",[3]="diamond",
  63.         [6]="stick",
  64.         [10]="stick",
  65.     },
  66.     turtle={
  67.         [1]="iron ingot",[2]="iron ingot",[3]="iron ingot",
  68.         [5]="iron ingot",[6]="computer",[7]="iron ingot",
  69.         [9]="iron ingot",[10]="chest",[11]="iron ingot",
  70.     },
  71.     ["mining turtle"]={
  72.         [2]="turtle",[3]="diamond pickaxe",
  73.     },
  74. }
  75.  
  76. local function gotoLevel(nLevel)
  77.     while nLevel~=level do
  78.         if nLevel>level then
  79.             if turtle.up() then level=level+1 end
  80.         else
  81.             if turtle.down() then level=level-1 end
  82.         end
  83.     end
  84. end
  85.  
  86. local function findLevel(sItem)
  87.     if type(sItem)~="string" then error("invalid param, string expected, got "..type(sItem).." - "..(tostring(sItem) or ""),2) end
  88.     for nLevel,tItems in ipairs(tLevels) do
  89.         for _,item in ipairs(tItems) do
  90.             if sItem:lower()==(type(item)=="table" and item[1] or item):lower() then
  91.                 return nLevel
  92.             end
  93.         end
  94.     end
  95.     return false
  96. end
  97.  
  98. function dump(sItem,nSlot)
  99.     local nLevel=findLevel(sItem)
  100.     if not nLevel then return false end
  101.     gotoLevel(nLevel)
  102.     chest.pull(sDir,nSlot-1,turtle.getItemCount(nSlot))
  103.     return true
  104. end
  105.  
  106. function sort()
  107.     gotoLevel(1)
  108.     turtle.select(1)
  109.     for nSlot=1,16 do
  110.         if turtle.getItemCount(nSlot)>0 then
  111.             chest.pull(sDir,nSlot-1,turtle.getItemCount(nSlot))
  112.         end
  113.     end
  114.     while true do
  115.         gotoLevel(1)
  116.         chest.condense()
  117.         local tStack=chest.getStackInSlot(0)
  118.         if tStack then
  119.             chest.pushIntoSlot(sDir,0,tStack.qty,0)
  120.             if not dump(tStack.name,1) then
  121.                 turtle.dropUp()
  122.             end
  123.         else
  124.             break
  125.         end
  126.     end
  127. end
  128.  
  129. function forge(sItem)
  130.     do --empty inv
  131.         gotoLevel(1)
  132.         for nSlot=1,16 do
  133.             if turtle.getItemCount(nSlot)>0 then
  134.                 chest.pull(sDir,nSlot-1,turtle.getItemCount(nSlot))
  135.             end
  136.         end
  137.     end
  138.     local nLevel
  139.     do --refuel the furnace
  140.         gotoLevel(9)
  141.         chest.condense()
  142.         if not chest.getStackInSlot(0) then sort() gotoLevel(9) chest.condense() end
  143.         chest.push(sDir,0,64)
  144.         gotoLevel(10)
  145.         chest.condense()
  146.         chest.pushIntoSlot(sDir,0,64,1) --swop item in slot 1 for fuel and try to push fuel into furnace
  147.         chest.pullIntoSlot(sDir,0,64,0)
  148.         chest.pushIntoSlot("up",0,64,1)
  149.         chest.pushIntoSlot(sDir,0,64,0)
  150.  
  151.         chest.pullIntoSlot("up",2,64,0) --retrieve forged items, record their destination and place then in the turtle's slot 3
  152.         if chest.getStackInSlot(0) then
  153.             nLevel=findLevel(chest.getStackInSlot(0).name)
  154.         else
  155.             nLevel=false
  156.         end
  157.         chest.pushIntoSlot(sDir,0,64,2)
  158.  
  159.         chest.pullIntoSlot(sDir,1,64,0) --return item taken for fuel and try to forge it or the requested item
  160.         if sItem then
  161.             for nSlot=0,chest.getSizeInventory()-1 do
  162.                 local tStack=chest.getStackInSlot(nSlot)
  163.                 if tStack and tStack.name:lower()==sItem:lower() then
  164.                     chest.pushIntoSlot("up",nSlot,64,0)
  165.                 end
  166.             end
  167.         end
  168.         chest.pushIntoSlot("up",0,64,0)
  169.     end
  170.     do --dump the forged items and leftover fuel
  171.         gotoLevel(9)
  172.         chest.pull(sDir,0,64)
  173.         if nLevel then
  174.             gotoLevel(nLevel)
  175.             chest.pull(sDir,2,64)
  176.         end
  177.     end
  178. end
  179.  
  180. function checkForItems(sItem,nAmount)
  181.     nAmount=nAmount or 1
  182.     local nLevel=findLevel(sItem)
  183.     if not nLevel then error("cannot check for unknown item: "..sItem,2) end
  184.     gotoLevel(nLevel)
  185.     local nFound=0
  186.     for slot=0,chest.getSizeInventory()-1 do
  187.         local tStack=chest.getStackInSlot(slot)
  188.         if tStack and tStack.name:lower()==sItem:lower() then
  189.             nFound=nFound+tStack.qty
  190.             if nFound>=nAmount then
  191.                 return true
  192.             end
  193.         end
  194.     end
  195.     return false
  196. end
  197.  
  198. function retrieve(sItem,nAmount,nSlot)
  199.     nAmount=nAmount or 1
  200.     nSlot=nSlot or 1
  201.     local nLevel=findLevel(sItem)
  202.     if not nLevel then error("cannot retrieve unknown item: "..sItem,2) end
  203.     gotoLevel(nLevel)
  204.     local nRetrieved=0
  205.     for slot=0,chest.getSizeInventory()-1 do
  206.         local tStack=chest.getStackInSlot(slot)
  207.         if tStack and tStack.name:lower()==sItem:lower() then
  208.             nRetrieved=nRetrieved+chest.pushIntoSlot(sDir,slot,nAmount-nRetrieved,nSlot-1)
  209.             if nRetrieved>=nAmount then
  210.                 return true
  211.             end
  212.         end
  213.     end
  214.     return false
  215. end
  216.  
  217. function craft(sItem,destination,nAmount,bDeposit)
  218.     sItem=sItem:lower()
  219.     if not tRecipes[sItem] then return false,"recipe for '"..sItem.."' not known" end
  220.     if type(destination)=="number" and turtle.getItemCount(destination)>0 then
  221.         return false,"slot "..destination.." is already occupied"
  222.     end
  223.  
  224.     local bInvOccupied=false
  225.     for i=1,16 do
  226.         if turtle.getItemCount(i)>0 then
  227.             bInvOccupied=true
  228.             break
  229.         end
  230.     end
  231.     local tempStorage
  232.     if bInvOccupied then
  233.         gotoLevel(10)
  234.         tempStorage=peripheral.wrap("top")
  235.         for nSlot=0,15 do
  236.             tempStorage.pullIntoSlot("down",nSlot,64,nSlot)
  237.         end
  238.     end
  239.  
  240.     local tItemsUsed={}
  241.     local tItemsFound={}
  242.  
  243.     for k,v in pairs(tRecipes[sItem]) do --check that we have everything we need
  244.         sleep(0)
  245.         if type(k)=="number" and k<=16 then
  246.             if type(v)=="table" then
  247.                 local found=false
  248.                 for _,item in ipairs(v) do
  249.                     if checkForItems(item,(tItemsFound[item] or 0)+1) then
  250.                         tItemsUsed[k]=item
  251.                         tItemsFound[item]=(tItemsFound[item] or 0)+1
  252.                         found=true
  253.                         break
  254.                     end
  255.                 end
  256.                 if not found then
  257.                     local crafted=false
  258.                     for _,item in ipairs(v) do
  259.                         if craft(item,nil,nil,true) then
  260.                             tItemsUsed[k]=item
  261.                             tItemsFound[item]=(tItemsFound[item] or 0)+1
  262.                             crafted=true
  263.                             break
  264.                         end
  265.                     end
  266.                     if not crafted then
  267.                         return false,"cannot craft or find "..table.concat(v," / ")
  268.                     end
  269.                 end
  270.             else
  271.                 if checkForItems(v,(tItemsFound[v] or 0)+1) then
  272.                     tItemsFound[v]=(tItemsFound[v] or 0)+1
  273.                     tItemsUsed[k]=v
  274.                 else
  275.                     if not craft(v,nil,nil,true) then
  276.                         return false,"cannot craft or find "..v
  277.                     end
  278.                     tItemsFound[v]=(tItemsFound[v] or 0)+1
  279.                     tItemsUsed[k]=v
  280.                 end
  281.             end
  282.         end
  283.     end
  284.  
  285.     sleep(0)
  286.  
  287.     for k,v in pairs(tItemsUsed) do
  288.         if not retrieve(v,1,k) then
  289.             error("stop fucking with the contents of the chests while I am working please",2)
  290.         end
  291.     end
  292.  
  293.     turtle.craft()
  294.     if type(nAmount)=="number" and turtle.getItemCount(1)>nAmount then
  295.         gotoLevel(findLevel(sItem))
  296.         chest.pull(sDir,0,turtle.getItemCount(1)-nAmount)
  297.     end
  298.     if type(destination)=="number" then
  299.         turtle.select(1)
  300.         turtle.transferTo(destination)
  301.     end
  302.     if bDeposit then
  303.         dump(sItem,destination or 1)
  304.     end
  305.     if bInvOccupied then
  306.         gotoLevel(10)
  307.         for nSlot=0,15 do
  308.             tempStorage.pushIntoSlot("down",nSlot,64,nSlot)
  309.         end
  310.     end
  311.     return true
  312. end
Advertisement
Add Comment
Please, Sign In to add comment